Fix comments and votes on albums

This commit is contained in:
video-prize-ranch 2022-01-20 16:09:57 -05:00
parent 20a1895642
commit 10ace75c19
No known key found for this signature in database
GPG Key ID: D8EAA4C5B12A7281
6 changed files with 42 additions and 37 deletions

View File

@ -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(),

View File

@ -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)*/

View File

@ -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,
})
}

View File

@ -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,
})
}

View File

@ -6,6 +6,7 @@ type Album struct {
Views int64
Upvotes int64
Downvotes int64
Privacy string
CreatedAt string
UpdatedAt string
Comments int64

View File

@ -26,10 +26,10 @@
<span class="material-icons-outlined" title="Views">visibility</span>
<p>{{album.Views}}</p>
</div>
{{#unless isAlbum}}
{{#equal album.Privacy "public"}}
<p><span class="material-icons-outlined" title="Likes">thumb_up</span> {{album.Upvotes}}</p>
<p><span class="material-icons-outlined" title="Dislilkes">thumb_down</span> {{album.Downvotes}}</p>
{{/unless}}
{{/equal}}
</div>
<!--<div class="videoDesc__channel">
<a href="{{claim.Channel.RelUrl}}">
@ -69,6 +69,7 @@
<br>
{{/each}}
{{#equal album.Privacy "public"}}
<div>
<hr>
<input id="comments__expandBtn" type="checkbox">
@ -83,6 +84,7 @@
</div>
</div>
<hr>
{{/equal}}
</main>
{{> partials/footer }}