mirror of
https://codeberg.org/video-prize-ranch/rimgo.git
synced 2025-12-13 19:55:21 +00:00
User comments support (#131)
#10 Reviewed-on: https://codeberg.org/rimgo/rimgo/pulls/131 Co-authored-by: orangix <uleo8b8g@anonaddy.me> Co-committed-by: orangix <uleo8b8g@anonaddy.me>
This commit is contained in:
committed by
video-prize-ranch
parent
e5e1c38058
commit
34ecc2a32b
@@ -16,14 +16,15 @@ import (
|
||||
|
||||
type Comment struct {
|
||||
Comments []Comment
|
||||
User User
|
||||
User User
|
||||
Post Submission
|
||||
Id string
|
||||
Comment string
|
||||
Upvotes int64
|
||||
Downvotes int64
|
||||
Platform string
|
||||
CreatedAt string
|
||||
RelTime string
|
||||
RelTime string
|
||||
UpdatedAt string
|
||||
DeletedAt string
|
||||
}
|
||||
@@ -55,7 +56,7 @@ func (client *Client) FetchComments(galleryID string) ([]Comment, error) {
|
||||
)
|
||||
wg.Wait()
|
||||
|
||||
client.Cache.Set(galleryID + "-comments", comments, cache.DefaultExpiration)
|
||||
client.Cache.Set(galleryID+"-comments", comments, cache.DefaultExpiration)
|
||||
return comments, nil
|
||||
}
|
||||
|
||||
@@ -130,13 +131,14 @@ func parseComment(data gjson.Result) Comment {
|
||||
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),
|
||||
RelTime: humanize.Time(createdTime),
|
||||
UpdatedAt: updatedAt,
|
||||
DeletedAt: deletedAt,
|
||||
}
|
||||
|
||||
137
api/user.go
137
api/user.go
@@ -8,6 +8,7 @@ import (
|
||||
"time"
|
||||
|
||||
"codeberg.org/rimgo/rimgo/utils"
|
||||
"github.com/patrickmn/go-cache"
|
||||
"github.com/tidwall/gjson"
|
||||
)
|
||||
|
||||
@@ -64,7 +65,7 @@ func (client *Client) FetchUser(username string) (User, error) {
|
||||
CreatedAt: createdTime.Format("January 2, 2006"),
|
||||
}
|
||||
|
||||
client.Cache.Set(username + "-user", user, 1*time.Hour)
|
||||
client.Cache.Set(username+"-user", user, 1*time.Hour)
|
||||
return user, nil
|
||||
}
|
||||
|
||||
@@ -89,41 +90,7 @@ func (client *Client) FetchSubmissions(username string, sort string, page string
|
||||
go func() {
|
||||
defer wg.Done()
|
||||
|
||||
coverData := value.Get("images.#(id==\"" + value.Get("cover").String() + "\")")
|
||||
cover := Media{
|
||||
Id: value.Get("id").String(),
|
||||
Description: value.Get("description").String(),
|
||||
Type: strings.Split(value.Get("type").String(), "/")[0],
|
||||
Url: strings.ReplaceAll(value.Get("link").String(), "https://i.imgur.com", ""),
|
||||
}
|
||||
if coverData.Exists() {
|
||||
cover = Media{
|
||||
Id: coverData.Get("id").String(),
|
||||
Description: coverData.Get("description").String(),
|
||||
Type: strings.Split(coverData.Get("type").String(), "/")[0],
|
||||
Url: strings.ReplaceAll(coverData.Get("link").String(), "https://i.imgur.com", ""),
|
||||
}
|
||||
}
|
||||
|
||||
id := value.Get("id").String()
|
||||
|
||||
link := "/a/" + id
|
||||
if value.Get("in_gallery").Bool() {
|
||||
link = "/gallery/" + id
|
||||
}
|
||||
|
||||
submissions = append(submissions, Submission{
|
||||
Id: id,
|
||||
Link: link,
|
||||
Title: value.Get("title").String(),
|
||||
Cover: cover,
|
||||
Points: value.Get("points").Int(),
|
||||
Upvotes: value.Get("ups").Int(),
|
||||
Downvotes: value.Get("downs").Int(),
|
||||
Comments: value.Get("comment_count").Int(),
|
||||
Views: value.Get("views").Int(),
|
||||
IsAlbum: value.Get("is_album").Bool(),
|
||||
})
|
||||
submissions = append(submissions, parseSubmission(value))
|
||||
}()
|
||||
|
||||
return true
|
||||
@@ -131,6 +98,102 @@ func (client *Client) FetchSubmissions(username string, sort string, page string
|
||||
)
|
||||
wg.Wait()
|
||||
|
||||
client.Cache.Set(username + "-submissions", submissions, 15*time.Minute)
|
||||
client.Cache.Set(username+"-submissions", submissions, 15*time.Minute)
|
||||
return submissions, nil
|
||||
}
|
||||
|
||||
func (client *Client) FetchUserComments(username string) ([]Comment, error) {
|
||||
cacheData, found := client.Cache.Get(username + "-usercomments")
|
||||
if found {
|
||||
return cacheData.([]Comment), nil
|
||||
}
|
||||
|
||||
req, err := http.NewRequest("GET", "https://api.imgur.com/comment/v1/comments", nil)
|
||||
if err != nil {
|
||||
return []Comment{}, err
|
||||
}
|
||||
utils.SetReqHeaders(req)
|
||||
|
||||
q := req.URL.Query()
|
||||
q.Add("client_id", client.ClientID)
|
||||
q.Add("filter[account]", "eq:"+username)
|
||||
q.Add("include", "account,post")
|
||||
q.Add("sort", "new")
|
||||
|
||||
req.URL.RawQuery = q.Encode()
|
||||
|
||||
res, err := http.DefaultClient.Do(req)
|
||||
if err != nil {
|
||||
return []Comment{}, err
|
||||
}
|
||||
|
||||
body, err := io.ReadAll(res.Body)
|
||||
if err != nil {
|
||||
return []Comment{}, err
|
||||
}
|
||||
|
||||
data := gjson.Parse(string(body))
|
||||
|
||||
comments := make([]Comment, 0)
|
||||
data.Get("data").ForEach(
|
||||
func(key, value gjson.Result) bool {
|
||||
comments = append(comments, parseComment(value))
|
||||
return true
|
||||
},
|
||||
)
|
||||
|
||||
client.Cache.Set(username+"-usercomments", comments, cache.DefaultExpiration)
|
||||
return comments, nil
|
||||
}
|
||||
|
||||
func parseSubmission(value gjson.Result) Submission {
|
||||
var cover Media
|
||||
c := value.Get("cover")
|
||||
coverData := value.Get("images.#(id==\"" + c.String() + "\")")
|
||||
switch {
|
||||
case c.Type == gjson.String && coverData.Exists():
|
||||
cover = Media{
|
||||
Id: coverData.Get("id").String(),
|
||||
Description: coverData.Get("description").String(),
|
||||
Type: strings.Split(coverData.Get("type").String(), "/")[0],
|
||||
Url: strings.ReplaceAll(coverData.Get("link").String(), "https://i.imgur.com", ""),
|
||||
}
|
||||
// This case is when fetching comments
|
||||
case c.Type != gjson.Null:
|
||||
cover = Media{
|
||||
Id: c.Get("id").String(),
|
||||
Url: strings.ReplaceAll(c.Get("url").String(), "https://i.imgur.com", ""),
|
||||
}
|
||||
// Replace with thumbnails here because it's easier.
|
||||
if strings.HasSuffix(cover.Url, ".mp4") {
|
||||
cover.Url = cover.Url[:len(cover.Url)-3] + "webp"
|
||||
}
|
||||
default:
|
||||
cover = Media{
|
||||
Id: value.Get("id").String(),
|
||||
Description: value.Get("description").String(),
|
||||
Type: strings.Split(value.Get("type").String(), "/")[0],
|
||||
Url: strings.ReplaceAll(value.Get("link").String(), "https://i.imgur.com", ""),
|
||||
}
|
||||
}
|
||||
|
||||
id := value.Get("id").String()
|
||||
|
||||
link := "/a/" + id
|
||||
if value.Get("in_gallery").Bool() {
|
||||
link = "/gallery/" + id
|
||||
}
|
||||
|
||||
return Submission{
|
||||
Id: id,
|
||||
Link: link,
|
||||
Title: value.Get("title").String(),
|
||||
Cover: cover,
|
||||
Points: value.Get("points").Int(),
|
||||
Upvotes: value.Get("ups").Int(),
|
||||
Downvotes: value.Get("downs").Int(),
|
||||
Comments: value.Get("comment_count").Int(),
|
||||
Views: value.Get("views").Int(),
|
||||
IsAlbum: value.Get("is_album").Bool(),
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user