2022-02-17 21:07:15 +00:00
|
|
|
package pages
|
|
|
|
|
|
|
|
import (
|
2022-05-23 15:10:50 +00:00
|
|
|
"strings"
|
2022-02-23 00:06:39 +00:00
|
|
|
|
2022-02-17 21:07:15 +00:00
|
|
|
"codeberg.org/video-prize-ranch/rimgo/api"
|
|
|
|
"codeberg.org/video-prize-ranch/rimgo/types"
|
|
|
|
"codeberg.org/video-prize-ranch/rimgo/utils"
|
|
|
|
"github.com/gofiber/fiber/v2"
|
|
|
|
)
|
|
|
|
|
|
|
|
func HandlePost(c *fiber.Ctx) error {
|
|
|
|
utils.SetHeaders(c)
|
2022-04-10 15:21:41 +00:00
|
|
|
c.Set("Content-Security-Policy", "default-src 'none'; media-src 'self'; style-src 'self'; img-src 'self'; font-src 'self'; manifest-src 'self'; block-all-mixed-content")
|
2022-02-17 21:07:15 +00:00
|
|
|
|
2022-05-23 15:10:50 +00:00
|
|
|
post, err := types.Album{}, error(nil)
|
|
|
|
switch {
|
|
|
|
case strings.HasPrefix(c.Path(), "/a"):
|
|
|
|
post, err = api.FetchAlbum(c.Params("postID"))
|
|
|
|
case strings.HasPrefix(c.Path(), "/gallery"):
|
2022-02-23 00:06:39 +00:00
|
|
|
post, err = api.FetchPosts(c.Params("postID"))
|
2022-05-23 15:10:50 +00:00
|
|
|
default:
|
|
|
|
post, err = api.FetchMedia(c.Params("postID"))
|
|
|
|
}
|
2022-05-23 15:30:17 +00:00
|
|
|
if post.Id == "" || (err != nil && strings.Contains(err.Error(), "404")) {
|
|
|
|
c.Status(404)
|
|
|
|
return c.Render("errors/404", nil)
|
2022-02-23 00:06:39 +00:00
|
|
|
}
|
|
|
|
if err != nil {
|
2022-02-17 21:07:15 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
comments := []types.Comment{}
|
|
|
|
if post.SharedWithCommunity {
|
|
|
|
c.Set("Cache-Control", "public,max-age=604800")
|
|
|
|
comments, err = api.FetchComments(c.Params("postID"))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
c.Set("Cache-Control", "public,max-age=31557600")
|
|
|
|
}
|
|
|
|
|
|
|
|
return c.Render("post", fiber.Map{
|
|
|
|
"post": post,
|
|
|
|
"comments": comments,
|
|
|
|
})
|
|
|
|
}
|