Files
rimgo/api/comments.go
orangix 14192e2943 change Album and Tag to use encoding/json
* move sanitization code to views

* split Tag into Tag and the subset TagMeta to match Imgur API

* make the code to parse an array generic

* change some of the views to match
2026-01-26 03:08:54 +01:00

63 lines
1.7 KiB
Go

package api
import (
"encoding/json"
"time"
"codeberg.org/rimgo/rimgo/utils"
"github.com/patrickmn/go-cache"
"github.com/tidwall/gjson"
)
type Comment struct {
ID int `json:"id"`
Comment string `json:"comment"`
UpvoteCount int `json:"upvote_count"`
DownvoteCount int `json:"downvote_count"`
PointCount int `json:"point_count"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
DeletedAt time.Time `json:"deleted_at"`
Comments array[Comment] `json:"comments"`
AccountID int `json:"account_id"`
Account _ApiUser `json:"account"`
Post Submission `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.(array[Comment]), 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
}
// temporary
func (post *Submission) UnmarshalJSON(data []byte) error {
*post = parseSubmission(gjson.ParseBytes(data))
return nil
}
type commentApiResponse struct {
Data array[Comment] `json:"data"`
}