website/routes/news.go

164 lines
3.5 KiB
Go
Raw Permalink Normal View History

2023-12-09 16:25:38 +00:00
package routes
import (
2024-08-23 11:11:48 +00:00
"fmt"
"path"
2023-12-22 21:22:23 +00:00
"sort"
2024-08-23 11:11:48 +00:00
"time"
2023-12-09 16:25:38 +00:00
2024-01-18 17:01:30 +00:00
"git.matterlinux.xyz/Matter/website/lib"
2024-08-13 21:32:48 +00:00
"git.matterlinux.xyz/Matter/website/log"
2023-12-09 16:25:38 +00:00
"github.com/gofiber/fiber/v2"
2024-08-23 11:11:48 +00:00
"github.com/gorilla/feeds"
2023-12-09 16:25:38 +00:00
)
2024-08-23 11:11:48 +00:00
func GetContent() ([]lib.Content, error) {
var (
contents []lib.Content
err error
)
if contents, err = lib.ListContent("news"); err != nil {
return nil, err
2024-08-13 21:32:48 +00:00
}
2023-12-09 16:25:38 +00:00
2024-08-13 21:32:48 +00:00
sort.Slice(contents, func(i, j int) bool {
time1, err := lib.TimeFromString(contents[i].Date)
if err != nil {
log.Error("Bad date while sorting: %s", err.Error())
return false
}
2023-12-22 21:22:23 +00:00
2024-08-13 21:32:48 +00:00
time2, err := lib.TimeFromString(contents[j].Date)
if err != nil {
log.Error("Bad date while sorting: %s", err.Error())
return false
}
2023-12-22 21:22:23 +00:00
2024-08-13 21:32:48 +00:00
return time1.After(time2)
})
2024-08-23 11:11:48 +00:00
return contents, nil
}
func GET_News(c *fiber.Ctx) error {
contents, err := GetContent()
if err != nil {
log.Error("GetContent failed: %s", err.Error())
return lib.RenderError(c, 500)
}
2024-08-13 21:32:48 +00:00
return c.Render("news", fiber.Map{
"news": contents,
})
2023-12-09 16:25:38 +00:00
}
2024-08-13 21:32:48 +00:00
func GET_New(c *fiber.Ctx) error {
postid := c.Params("id")
if len(postid) == 0 {
return lib.RenderError(c, 404)
}
2023-12-09 16:25:38 +00:00
2024-08-13 21:32:48 +00:00
contents, err := lib.ListContent("news")
if err != nil {
log.Error("ListContent failed: %s", err.Error())
return lib.RenderError(c, 500)
}
2023-12-09 16:25:38 +00:00
2024-08-13 21:32:48 +00:00
for _, con := range contents {
if con.ID != postid {
continue
}
2023-12-09 16:25:38 +00:00
2024-08-13 21:32:48 +00:00
con, err = lib.GetContent(con.Dir, con.Name)
if err != nil {
log.Error("ListContent failed: %s", err.Error())
return lib.RenderError(c, 500)
}
2023-12-09 16:25:38 +00:00
2024-08-13 21:32:48 +00:00
return c.Render("post", fiber.Map{
"title": con.Title,
"post": con,
})
}
2023-12-09 16:25:38 +00:00
2024-08-13 21:32:48 +00:00
return lib.RenderError(c, 404)
2023-12-09 16:25:38 +00:00
}
2024-08-23 11:11:48 +00:00
func GET_NewsFeed(c *fiber.Ctx) error {
var (
feed_type string
feed *feeds.Feed
contents []lib.Content
res string
err error
)
feed_type = path.Base(c.Path())
if contents, err = GetContent(); err != nil {
log.Error("GetContent failed: %s", err.Error())
return lib.RenderError(c, 500)
}
feed = &feeds.Feed{
Title: "MatterLinux | News",
Link: &feeds.Link{Href: "https://matterlinux.xyz/news"}, // lets hardcode this for now
Description: "News regarding the MatterLinux project",
Created: time.Now(),
}
feed.Items = []*feeds.Item{}
for _, con := range contents {
parsed, err := time.Parse("02/01/06", con.Date)
if err != nil {
log.Error("Failed to parse post time (%s): %s", con.ID, err.Error())
return lib.RenderError(c, 500)
}
feed.Items = append(feed.Items, &feeds.Item{
Id: con.ID,
Title: con.Title,
Link: &feeds.Link{Href: fmt.Sprintf("https://matterlinux.xyz/news/%s", con.ID)},
Author: &feeds.Author{Name: con.Author},
Created: parsed,
Content: con.Md,
})
}
switch feed_type {
case "feed.atom":
if res, err = feed.ToAtom(); err != nil {
log.Error("Failed to convert feed to atom: %s", err.Error())
return lib.RenderError(c, 500)
}
c.Set("Content-Type", "application/atom+xml")
return c.Send([]byte(res))
case "feed.rss":
if res, err = feed.ToRss(); err != nil {
log.Error("Failed to convert feed to RSS: %s", err.Error())
return lib.RenderError(c, 500)
}
c.Set("Content-Type", "application/rss+xml")
return c.Send([]byte(res))
case "feed.json":
if res, err = feed.ToJSON(); err != nil {
log.Error("Failed to convert feed to JSON: %s", err.Error())
return lib.RenderError(c, 500)
}
c.Set("Content-Type", "application/feed+json")
return c.Send([]byte(res))
}
log.Error("Cannot handle the feed type: %s", feed_type)
return lib.RenderError(c, 404)
}