mirror of
https://codeberg.org/video-prize-ranch/rimgo.git
synced 2026-01-28 09:31:13 +00:00
refactor api package comments
* use encoding/json for comment parsing * refactor by moving loop code to an UnmarshalJSON * use a preallocated array and indices to maintain order while using goroutines again, this was removed a while ago * use new struct in comment.hbs and contextComment.hbs * rewriteUrl partial to reduce rimgo-specific code in api * move RenderError into pages package to avoid import cycle between render and utils
This commit is contained in:
186
api/comments.go
186
api/comments.go
@@ -1,13 +1,15 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"regexp"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"codeberg.org/rimgo/rimgo/utils"
|
||||
"github.com/dustin/go-humanize"
|
||||
"github.com/microcosm-cc/bluemonday"
|
||||
"github.com/patrickmn/go-cache"
|
||||
"github.com/tidwall/gjson"
|
||||
@@ -15,49 +17,51 @@ import (
|
||||
)
|
||||
|
||||
type Comment struct {
|
||||
Comments []Comment
|
||||
User User
|
||||
Post Submission
|
||||
Id string
|
||||
Comment string
|
||||
Upvotes int64
|
||||
Downvotes int64
|
||||
Platform string
|
||||
CreatedAt string
|
||||
RelTime string
|
||||
UpdatedAt string
|
||||
DeletedAt string
|
||||
ID int `json:"id"`
|
||||
ParentID int `json:"parent_id"`
|
||||
Comment string `json:"comment"`
|
||||
PostID string `json:"post_id"`
|
||||
UpvoteCount int `json:"upvote_count"`
|
||||
DownvoteCount int `json:"downvote_count"`
|
||||
PointCount int `json:"point_count"`
|
||||
Vote struct{} `json:"vote"` // ???
|
||||
PlatformID int `json:"platform_id"`
|
||||
Platform string `json:"platform"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
UpdatedAt time.Time `json:"updated_at"`
|
||||
DeletedAt time.Time `json:"deleted_at"`
|
||||
Next struct{} `json:"next"` // ???
|
||||
Comments commentArray `json:"comments"`
|
||||
AccountID int `json:"account_id"`
|
||||
Account _ApiUser `json:"account"`
|
||||
Post commentPost `json:"post"`
|
||||
}
|
||||
|
||||
type _ApiUser struct {
|
||||
ID int `json:"id"`
|
||||
Username string `json:"username"`
|
||||
Avatar string `json:"avatar"`
|
||||
}
|
||||
|
||||
func (client *Client) FetchComments(galleryID string) ([]Comment, error) {
|
||||
cacheData, found := client.Cache.Get(galleryID + "-comments")
|
||||
if found {
|
||||
return cacheData.([]Comment), nil
|
||||
return cacheData.(commentArray), nil
|
||||
}
|
||||
|
||||
data, err := utils.GetJSON("https://api.imgur.com/comment/v1/comments?client_id=" + client.ClientID + "&filter[post]=eq:" + galleryID + "&include=account,adconfig&per_page=30&sort=best")
|
||||
data, err := utils.GetJSONNew("https://api.imgur.com/comment/v1/comments?client_id=" + client.ClientID + "&filter[post]=eq:" + galleryID + "&include=account,adconfig&per_page=30&sort=best")
|
||||
if err != nil {
|
||||
return []Comment{}, nil
|
||||
}
|
||||
|
||||
wg := sync.WaitGroup{}
|
||||
comments := make([]Comment, 0)
|
||||
data.Get("data").ForEach(
|
||||
func(key, value gjson.Result) bool {
|
||||
wg.Add(1)
|
||||
var parsed commentApiResponse
|
||||
err = json.Unmarshal(data, &parsed)
|
||||
if err != nil {
|
||||
return []Comment{}, err
|
||||
}
|
||||
|
||||
go func() {
|
||||
defer wg.Done()
|
||||
comments = append(comments, parseComment(value))
|
||||
}()
|
||||
|
||||
return true
|
||||
},
|
||||
)
|
||||
wg.Wait()
|
||||
|
||||
client.Cache.Set(galleryID+"-comments", comments, cache.DefaultExpiration)
|
||||
return comments, nil
|
||||
client.Cache.Set(galleryID+"-comments", parsed.Data, cache.DefaultExpiration)
|
||||
return parsed.Data, nil
|
||||
}
|
||||
|
||||
var imgurRe = regexp.MustCompile(`https?://imgur\.com/(gallery|a)?/(.*)`)
|
||||
@@ -67,51 +71,32 @@ var vidRe = regexp.MustCompile(`https?://i\.imgur\.com/(.*)\.(mp4|webm)`)
|
||||
var vidFormatRe = regexp.MustCompile(`\.(mp4|webm)`)
|
||||
var iImgurRe = regexp.MustCompile(`https?://i\.imgur\.com`)
|
||||
|
||||
func parseComment(data gjson.Result) Comment {
|
||||
createdTime, _ := time.Parse("2006-01-02T15:04:05Z", data.Get("created_at").String())
|
||||
createdAt := createdTime.Format("January 2, 2006 3:04 PM")
|
||||
updatedAt, _ := utils.FormatDate(data.Get("updated_at").String())
|
||||
deletedAt, _ := utils.FormatDate(data.Get("deleted_at").String())
|
||||
func parseComment(data json.RawMessage, out *Comment) {
|
||||
err := json.Unmarshal(data, &out)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
userAvatar := strings.ReplaceAll(data.Get("account.avatar").String(), "https://i.imgur.com", "")
|
||||
comment := &out.Comment
|
||||
*comment = strings.ReplaceAll(*comment, "\n", "<br>")
|
||||
|
||||
wg := sync.WaitGroup{}
|
||||
comments := make([]Comment, 0)
|
||||
data.Get("comments").ForEach(
|
||||
func(key, value gjson.Result) bool {
|
||||
wg.Add(1)
|
||||
|
||||
go func() {
|
||||
defer wg.Done()
|
||||
comments = append(comments, parseComment(value))
|
||||
}()
|
||||
|
||||
return true
|
||||
},
|
||||
)
|
||||
wg.Wait()
|
||||
|
||||
comment := data.Get("comment").String()
|
||||
|
||||
comment = strings.ReplaceAll(comment, "\n", "<br>")
|
||||
|
||||
for _, match := range imgRe.FindAllString(comment, -1) {
|
||||
for _, match := range imgRe.FindAllString(*comment, -1) {
|
||||
img := iImgurRe.ReplaceAllString(match, "")
|
||||
img = `<img src="` + img + `" class="comment__media" loading="lazy"/>`
|
||||
comment = strings.Replace(comment, match, img, 1)
|
||||
*comment = strings.Replace(*comment, match, img, 1)
|
||||
}
|
||||
for _, match := range vidRe.FindAllString(comment, -1) {
|
||||
for _, match := range vidRe.FindAllString(*comment, -1) {
|
||||
vid := iImgurRe.ReplaceAllString(match, "")
|
||||
vid = `<video class="comment__media" controls loop preload="none" poster="` + vidFormatRe.ReplaceAllString(vid, ".webp") + `"><source type="` + strings.Split(vid, ".")[1] + `" src="` + vid + `" /></video>`
|
||||
comment = strings.Replace(comment, match, vid, 1)
|
||||
*comment = strings.Replace(*comment, match, vid, 1)
|
||||
}
|
||||
for _, l := range linkify.Links(comment) {
|
||||
origLink := comment[l.Start:l.End]
|
||||
for _, l := range linkify.Links(*comment) {
|
||||
origLink := (*comment)[l.Start:l.End]
|
||||
link := `<a href="` + origLink + `">` + origLink + `</a>`
|
||||
comment = strings.Replace(comment, origLink, link, 1)
|
||||
*comment = strings.Replace(*comment, origLink, link, 1)
|
||||
}
|
||||
comment = imgurRe.ReplaceAllString(comment, "/$1/$2")
|
||||
comment = imgurRe2.ReplaceAllString(comment, "/$1")
|
||||
*comment = imgurRe.ReplaceAllString(*comment, "/$1/$2")
|
||||
*comment = imgurRe2.ReplaceAllString(*comment, "/$1")
|
||||
|
||||
p := bluemonday.UGCPolicy()
|
||||
p.AllowImages()
|
||||
@@ -122,24 +107,51 @@ func parseComment(data gjson.Result) Comment {
|
||||
p.RequireNoReferrerOnLinks(true)
|
||||
p.RequireNoFollowOnLinks(true)
|
||||
p.RequireCrossOriginAnonymous(true)
|
||||
comment = p.Sanitize(comment)
|
||||
|
||||
return Comment{
|
||||
Comments: comments,
|
||||
User: User{
|
||||
Id: data.Get("account.id").Int(),
|
||||
Username: data.Get("account.username").String(),
|
||||
Avatar: userAvatar,
|
||||
},
|
||||
Post: parseSubmission(data.Get("post")),
|
||||
Id: data.Get("id").String(),
|
||||
Comment: comment,
|
||||
Upvotes: data.Get("upvote_count").Int(),
|
||||
Downvotes: data.Get("downvote_count").Int(),
|
||||
Platform: data.Get("platform").String(),
|
||||
CreatedAt: createdAt,
|
||||
RelTime: humanize.Time(createdTime),
|
||||
UpdatedAt: updatedAt,
|
||||
DeletedAt: deletedAt,
|
||||
}
|
||||
*comment = p.Sanitize(*comment)
|
||||
}
|
||||
|
||||
type commentArray []Comment
|
||||
|
||||
func (arr *commentArray) UnmarshalJSON(data []byte) error {
|
||||
var rawArr []json.RawMessage
|
||||
err := json.Unmarshal(data, &rawArr)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
*arr = make(commentArray, len(rawArr))
|
||||
panics := make([]error, 0, len(rawArr))
|
||||
wg := sync.WaitGroup{}
|
||||
var handlePanic = func() {
|
||||
if v := recover(); v != nil {
|
||||
v, ok := v.(error)
|
||||
if !ok {
|
||||
v = fmt.Errorf("%v", v)
|
||||
}
|
||||
panics = append(panics, v)
|
||||
}
|
||||
}
|
||||
for i, value := range rawArr {
|
||||
wg.Add(1)
|
||||
go func() {
|
||||
defer handlePanic()
|
||||
defer wg.Done()
|
||||
parseComment(value, &(*arr)[i])
|
||||
}()
|
||||
}
|
||||
wg.Wait()
|
||||
if len(panics) != 0 {
|
||||
return errors.Join(panics...)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
type commentPost Submission
|
||||
|
||||
func (post *commentPost) UnmarshalJSON(data []byte) error {
|
||||
*post = commentPost(parseSubmission(gjson.Parse(string(data))))
|
||||
return nil
|
||||
}
|
||||
|
||||
type commentApiResponse struct {
|
||||
Data commentArray `json:"data"`
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user