mirror of
https://codeberg.org/video-prize-ranch/rimgo.git
synced 2026-01-27 17:11:13 +00:00
115 lines
2.8 KiB
Go
115 lines
2.8 KiB
Go
package api
|
|
|
|
import (
|
|
"encoding/json"
|
|
"errors"
|
|
"fmt"
|
|
"sync"
|
|
"time"
|
|
|
|
"codeberg.org/rimgo/rimgo/utils"
|
|
"github.com/patrickmn/go-cache"
|
|
"github.com/tidwall/gjson"
|
|
)
|
|
|
|
type Comment struct {
|
|
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.(commentArray), nil
|
|
}
|
|
|
|
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
|
|
}
|
|
|
|
var parsed commentApiResponse
|
|
err = json.Unmarshal(data, &parsed)
|
|
if err != nil {
|
|
return []Comment{}, err
|
|
}
|
|
|
|
client.Cache.Set(galleryID+"-comments", parsed.Data, cache.DefaultExpiration)
|
|
return parsed.Data, nil
|
|
}
|
|
|
|
func parseComment(data json.RawMessage, out *Comment) {
|
|
err := json.Unmarshal(data, &out)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
}
|
|
|
|
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"`
|
|
}
|