From 10ace75c195a488ccfb1736c4d396b919cda2a0c Mon Sep 17 00:00:00 2001 From: video-prize-ranch Date: Thu, 20 Jan 2022 16:09:57 -0500 Subject: [PATCH] Fix comments and votes on albums --- api/album.go | 30 +++++++++++++++++++++++++++++- main.go | 2 +- pages/album.go | 29 ----------------------------- pages/gallery.go | 11 +++++++---- types/Album.go | 1 + views/gallery.hbs | 6 ++++-- 6 files changed, 42 insertions(+), 37 deletions(-) delete mode 100644 pages/album.go diff --git a/api/album.go b/api/album.go index 89f5c0a..56a98cb 100644 --- a/api/album.go +++ b/api/album.go @@ -26,6 +26,33 @@ func FetchAlbum(albumID string) (types.Album, error) { data := gjson.Parse(string(body)) + album := types.Album{} + if data.Get("privacy").String() == "private" { + album, err = ParseAlbum(data) + } else { + album, err = FetchPosts(albumID) + } + + return album, err +} + +func FetchPosts(albumID string) (types.Album, error) { + res, err := http.Get("https://api.imgur.com/post/v1/posts/" + albumID + "?client_id=" + viper.GetString("RIMGU_IMGUR_CLIENT_ID") + "&include=media%2Caccount") + if err != nil { + return types.Album{}, err + } + + body, err := io.ReadAll(res.Body) + if err != nil { + return types.Album{}, err + } + + data := gjson.Parse(string(body)) + + return ParseAlbum(data) +} + +func ParseAlbum(data gjson.Result) (types.Album, error) { media := make([]types.Media, 0) data.Get("media").ForEach( func(key gjson.Result, value gjson.Result) bool { @@ -54,6 +81,7 @@ func FetchAlbum(albumID string) (types.Album, error) { return types.Album{ Id: data.Get("id").String(), Title: data.Get("title").String(), + Privacy: data.Get("privacy").String(), Views: data.Get("view_count").Int(), Upvotes: data.Get("upvote_count").Int(), Downvotes: data.Get("downvote_count").Int(), @@ -61,4 +89,4 @@ func FetchAlbum(albumID string) (types.Album, error) { CreatedAt: createdAt, Media: media, }, nil -} +} \ No newline at end of file diff --git a/main.go b/main.go index a1e7c81..7938d0a 100644 --- a/main.go +++ b/main.go @@ -45,7 +45,7 @@ func main() { app.Get("/", pages.FrontpageHandler) app.Get("/:baseName.:extension", pages.HandleMedia) - app.Get("/a/:albumID", pages.HandleAlbum) + app.Get("/a/:galleryID", pages.HandleGallery) //app.Get("/t/:tagID", pages.HandleAlbum) /*app.Get("/user/:userID", pages.HandleUser) app.Get("/user/:userID/cover", pages.HandleUserCover)*/ diff --git a/pages/album.go b/pages/album.go deleted file mode 100644 index 9b21b9c..0000000 --- a/pages/album.go +++ /dev/null @@ -1,29 +0,0 @@ -package pages - -import ( - "codeberg.org/video-prize-ranch/rimgo/api" - "github.com/gofiber/fiber/v2" -) - -func HandleAlbum(c *fiber.Ctx) error { - c.Set("Cache-Control", "public,max-age=604800") - c.Set("X-Frame-Options", "DENY") - c.Set("Referrer-Policy", "no-referrer") - c.Set("X-Content-Type-Options", "nosniff") - c.Set("X-Robots-Tag", "noindex, noimageindex, nofollow") - c.Set("Strict-Transport-Security", "max-age=31557600") - c.Set("Permissions-Policy", "accelerometer=(), ambient-light-sensor=(), autoplay=(), battery=(), camera=(), cross-origin-isolated=(), display-capture=(), document-domain=(), encrypted-media=(), execution-while-not-rendered=(), execution-while-out-of-viewport=(), fullscreen=(), geolocation=(), gyroscope=(), interest-cohort=(), magnetometer=(), microphone=(), midi=(), navigation-override=(), payment=(), picture-in-picture=(), publickey-credentials-get=(), screen-wake-lock=(), sync-xhr=(), usb=(), web-share=(), xr-spatial-tracking=()") - c.Set("Content-Security-Policy", "default-src 'none'; style-src 'self'; script-src 'none'; img-src 'self'; font-src 'self'; block-all-mixed-content; manifest-src 'self'") - - // https://imgur.com/a/DfEsrAB - - album, err := api.FetchAlbum(c.Params("albumID")) - if err != nil { - return err - } - - return c.Render("gallery", fiber.Map{ - "album": album, - "isAlbum": true, - }) -} diff --git a/pages/gallery.go b/pages/gallery.go index eb614cd..83557c3 100644 --- a/pages/gallery.go +++ b/pages/gallery.go @@ -2,6 +2,7 @@ package pages import ( "codeberg.org/video-prize-ranch/rimgo/api" + "codeberg.org/video-prize-ranch/rimgo/types" "github.com/gofiber/fiber/v2" ) @@ -20,14 +21,16 @@ func HandleGallery(c *fiber.Ctx) error { return err } - comments, err := api.FetchComments(c.Params("galleryID")) - if err != nil { - return err + comments := []types.Comment{} + if album.Privacy != "private" { + comments, err = api.FetchComments(c.Params("galleryID")) + if err != nil { + return err + } } return c.Render("gallery", fiber.Map{ "album": album, "comments": comments, - "isAlbum": false, }) } diff --git a/types/Album.go b/types/Album.go index e1da2b2..daeb181 100644 --- a/types/Album.go +++ b/types/Album.go @@ -6,6 +6,7 @@ type Album struct { Views int64 Upvotes int64 Downvotes int64 + Privacy string CreatedAt string UpdatedAt string Comments int64 diff --git a/views/gallery.hbs b/views/gallery.hbs index 01c2d5f..ee0a4d4 100644 --- a/views/gallery.hbs +++ b/views/gallery.hbs @@ -26,10 +26,10 @@ visibility

{{album.Views}}

- {{#unless isAlbum}} + {{#equal album.Privacy "public"}}

thumb_up {{album.Upvotes}}

thumb_down {{album.Downvotes}}

- {{/unless}} + {{/equal}}