rimgo/api/comments.go

140 lines
4.0 KiB
Go
Raw Normal View History

2022-01-18 23:05:06 +00:00
package api
import (
2022-10-08 21:50:08 +00:00
"regexp"
2022-01-18 23:05:06 +00:00
"strings"
"sync"
"time"
"codeberg.org/video-prize-ranch/rimgo/utils"
"github.com/dustin/go-humanize"
2022-10-08 21:50:08 +00:00
"github.com/microcosm-cc/bluemonday"
2022-03-16 02:56:34 +00:00
"github.com/patrickmn/go-cache"
2022-01-18 23:05:06 +00:00
"github.com/tidwall/gjson"
2022-10-08 21:50:08 +00:00
"gitlab.com/golang-commonmark/linkify"
2022-01-18 23:05:06 +00:00
)
2022-07-22 15:55:22 +00:00
type Comment struct {
Comments []Comment
User User
Id string
Comment string
Upvotes int64
Downvotes int64
Platform string
CreatedAt string
RelTime string
UpdatedAt string
DeletedAt string
}
2023-01-01 20:12:03 +00:00
func (client *Client) FetchComments(galleryID string) ([]Comment, error) {
cacheData, found := client.Cache.Get(galleryID + "-comments")
2022-03-16 02:56:34 +00:00
if found {
2022-07-22 15:55:22 +00:00
return cacheData.([]Comment), nil
2022-03-16 02:56:34 +00:00
}
2022-01-18 23:05:06 +00:00
2023-01-01 20:12:03 +00:00
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")
2022-01-18 23:05:06 +00:00
if err != nil {
2022-07-22 15:55:22 +00:00
return []Comment{}, nil
2022-01-18 23:05:06 +00:00
}
wg := sync.WaitGroup{}
2022-07-22 15:55:22 +00:00
comments := make([]Comment, 0)
2022-01-18 23:05:06 +00:00
data.Get("data").ForEach(
func(key, value gjson.Result) bool {
wg.Add(1)
go func() {
defer wg.Done()
2023-01-01 20:12:03 +00:00
comments = append(comments, parseComment(value))
2022-01-18 23:05:06 +00:00
}()
return true
},
)
wg.Wait()
2023-01-01 20:12:03 +00:00
client.Cache.Set(galleryID + "-comments", comments, cache.DefaultExpiration)
2022-01-18 23:05:06 +00:00
return comments, nil
}
2022-10-08 21:50:08 +00:00
var imgRe = regexp.MustCompile(`https?://i\.imgur\.com/(.*)\.(png|gif|jpg|webp)`)
var vidRe = regexp.MustCompile(`https?://i\.imgur\.com/(.*)\.(mp4|webm)`)
var vidFormatRe = regexp.MustCompile(`\.(mp4|webm)`)
var iImgurRe = regexp.MustCompile(`https?://i\.imgur\.com`)
2023-01-01 20:12:03 +00:00
func parseComment(data gjson.Result) Comment {
2022-01-18 23:05:06 +00:00
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())
userAvatar := strings.ReplaceAll(data.Get("account.avatar").String(), "https://i.imgur.com", "")
wg := sync.WaitGroup{}
2022-07-22 15:55:22 +00:00
comments := make([]Comment, 0)
2022-01-18 23:05:06 +00:00
data.Get("comments").ForEach(
func(key, value gjson.Result) bool {
wg.Add(1)
go func() {
defer wg.Done()
2023-01-01 20:12:03 +00:00
comments = append(comments, parseComment(value))
2022-01-18 23:05:06 +00:00
}()
return true
},
)
wg.Wait()
2022-10-08 21:50:08 +00:00
comment := data.Get("comment").String()
2022-10-13 21:29:05 +00:00
comment = strings.ReplaceAll(comment, "\n", "<br>")
2022-10-08 21:50:08 +00:00
for _, match := range imgRe.FindAllString(comment, -1) {
img := iImgurRe.ReplaceAllString(match, "")
2022-10-08 22:14:00 +00:00
img = `<img src="` + img + `" class="comment__media" loading="lazy"/>`
2022-10-08 21:50:08 +00:00
comment = strings.Replace(comment, match, img, 1)
}
for _, match := range vidRe.FindAllString(comment, -1) {
vid := iImgurRe.ReplaceAllString(match, "")
2022-10-08 22:14:00 +00:00
vid = `<video class="comment__media" controls loop preload="none" poster="` + vidFormatRe.ReplaceAllString(vid, ".webp") + `"><source type="` + strings.Split(vid, ".")[1] + `" src="` + vid + `" /></video>`
2022-10-08 21:50:08 +00:00
comment = strings.Replace(comment, match, vid, 1)
}
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)
}
p := bluemonday.UGCPolicy()
p.AllowImages()
p.AllowElements("video", "source")
p.AllowAttrs("src", "tvpe").OnElements("source")
p.AllowAttrs("controls", "loop", "preload", "poster").OnElements("video")
p.AllowAttrs("class", "loading").OnElements("img", "video")
p.RequireNoReferrerOnLinks(true)
p.RequireNoFollowOnLinks(true)
p.RequireCrossOriginAnonymous(true)
comment = p.Sanitize(comment)
2022-07-22 15:55:22 +00:00
return Comment{
2022-01-18 23:05:06 +00:00
Comments: comments,
2022-07-22 15:55:22 +00:00
User: User{
2022-01-28 01:41:10 +00:00
Id: data.Get("account.id").Int(),
2022-01-18 23:05:06 +00:00
Username: data.Get("account.username").String(),
Avatar: userAvatar,
},
Id: data.Get("id").String(),
2022-10-08 21:50:08 +00:00
Comment: comment,
2022-01-18 23:05:06 +00:00
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,
}
}