From e0efe6caca5714633cf06b50eb7edd3a97db78a2 Mon Sep 17 00:00:00 2001 From: video-prize-ranch Date: Fri, 22 Jul 2022 11:55:22 -0400 Subject: [PATCH] Move types to api --- api/album.go | 66 ++++++++++++++++++++++++++++++--------------- api/comments.go | 31 ++++++++++++++------- api/tag.go | 28 ++++++++++++------- api/user.go | 50 ++++++++++++++++++++++++---------- pages/embed.go | 3 +-- pages/post.go | 5 ++-- pages/user.go | 5 ++-- types/Album.go | 16 ----------- types/Comment.go | 15 ----------- types/Media.go | 11 -------- types/Submission.go | 14 ---------- types/Tag.go | 10 ------- types/User.go | 11 -------- 13 files changed, 126 insertions(+), 139 deletions(-) delete mode 100644 types/Album.go delete mode 100644 types/Comment.go delete mode 100644 types/Media.go delete mode 100644 types/Submission.go delete mode 100644 types/Tag.go delete mode 100644 types/User.go diff --git a/api/album.go b/api/album.go index 6cb2a74..93e9efb 100644 --- a/api/album.go +++ b/api/album.go @@ -4,82 +4,106 @@ import ( "strings" "time" - "codeberg.org/video-prize-ranch/rimgo/types" "codeberg.org/video-prize-ranch/rimgo/utils" "github.com/patrickmn/go-cache" "github.com/tidwall/gjson" ) +type Album struct { + Id string + Title string + Views int64 + Upvotes int64 + Downvotes int64 + SharedWithCommunity bool + CreatedAt string + UpdatedAt string + Comments int64 + User User + Media []Media + Tags []Tag +} + +type Media struct { + Id string + Name string + Title string + Description string + Url string + Type string + MimeType string +} + var albumCache = cache.New(1*time.Hour, 15*time.Minute) -func FetchAlbum(albumID string) (types.Album, error) { +func FetchAlbum(albumID string) (Album, error) { cacheData, found := albumCache.Get(albumID + "-album") if found { - return cacheData.(types.Album), nil + return cacheData.(Album), nil } data, err := utils.GetJSON("https://api.imgur.com/post/v1/albums/" + albumID + "?client_id=" + utils.Config["imgurId"].(string) + "&include=media%2Caccount") if err != nil { - return types.Album{}, err + return Album{}, err } album, err := ParseAlbum(data) if err != nil { - return types.Album{}, err + return Album{}, err } albumCache.Set(albumID + "-album", album, cache.DefaultExpiration) return album, err } -func FetchPosts(albumID string) (types.Album, error) { +func FetchPosts(albumID string) (Album, error) { cacheData, found := albumCache.Get(albumID + "-posts") if found { - return cacheData.(types.Album), nil + return cacheData.(Album), nil } data, err := utils.GetJSON("https://api.imgur.com/post/v1/posts/" + albumID + "?client_id=" + utils.Config["imgurId"].(string) + "&include=media%2Caccount%2Ctags") if err != nil { - return types.Album{}, err + return Album{}, err } album, err := ParseAlbum(data) if err != nil { - return types.Album{}, err + return Album{}, err } albumCache.Set(albumID + "-posts", album, cache.DefaultExpiration) return album, nil } -func FetchMedia(mediaID string) (types.Album, error) { +func FetchMedia(mediaID string) (Album, error) { cacheData, found := albumCache.Get(mediaID + "-media") if found { - return cacheData.(types.Album), nil + return cacheData.(Album), nil } data, err := utils.GetJSON("https://api.imgur.com/post/v1/media/" + mediaID + "?client_id=" + utils.Config["imgurId"].(string) + "&include=media%2Caccount") if err != nil { - return types.Album{}, err + return Album{}, err } album, err := ParseAlbum(data) if err != nil { - return types.Album{}, err + return Album{}, err } albumCache.Set(mediaID + "-media", album, cache.DefaultExpiration) return album, nil } -func ParseAlbum(data gjson.Result) (types.Album, error) { - media := make([]types.Media, 0) +func ParseAlbum(data gjson.Result) (Album, error) { + media := make([]Media, 0) data.Get("media").ForEach( func(key gjson.Result, value gjson.Result) bool { url := value.Get("url").String() url = strings.ReplaceAll(url, "https://i.imgur.com", "") - media = append(media, types.Media{ + media = append(media, Media{ Id: value.Get("id").String(), Name: value.Get("name").String(), MimeType: value.Get("mime_type").String(), @@ -93,10 +117,10 @@ func ParseAlbum(data gjson.Result) (types.Album, error) { }, ) - tags := make([]types.Tag, 0) + tags := make([]Tag, 0) data.Get("tags").ForEach( func(key gjson.Result, value gjson.Result) bool { - tags = append(tags, types.Tag{ + tags = append(tags, Tag{ Tag: value.Get("tag").String(), Display: value.Get("display").String(), Background: "/" + value.Get("background_id").String() + ".webp", @@ -107,10 +131,10 @@ func ParseAlbum(data gjson.Result) (types.Album, error) { createdAt, err := utils.FormatDate(data.Get("created_at").String()) if err != nil { - return types.Album{}, err + return Album{}, err } - album := types.Album{ + album := Album{ Id: data.Get("id").String(), Title: data.Get("title").String(), SharedWithCommunity: data.Get("shared_with_community").Bool(), @@ -125,7 +149,7 @@ func ParseAlbum(data gjson.Result) (types.Album, error) { account := data.Get("account") if account.Raw != "" { - album.User = types.User{ + album.User = User{ Id: account.Get("id").Int(), Username: account.Get("username").String(), Avatar: strings.ReplaceAll(account.Get("avatar_url").String(), "https://i.imgur.com", ""), diff --git a/api/comments.go b/api/comments.go index 6e05d51..f6263c4 100644 --- a/api/comments.go +++ b/api/comments.go @@ -5,28 +5,41 @@ import ( "sync" "time" - "codeberg.org/video-prize-ranch/rimgo/types" "codeberg.org/video-prize-ranch/rimgo/utils" "github.com/dustin/go-humanize" "github.com/patrickmn/go-cache" "github.com/tidwall/gjson" ) +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 +} + var commentCache = cache.New(15*time.Minute, 15*time.Minute) -func FetchComments(galleryID string) ([]types.Comment, error) { +func FetchComments(galleryID string) ([]Comment, error) { cacheData, found := commentCache.Get(galleryID) if found { - return cacheData.([]types.Comment), nil + return cacheData.([]Comment), nil } data, err := utils.GetJSON("https://api.imgur.com/comment/v1/comments?client_id=" + utils.Config["imgurId"].(string) + "&filter[post]=eq:" + galleryID + "&include=account,adconfig&per_page=30&sort=best") if err != nil { - return []types.Comment{}, nil + return []Comment{}, nil } wg := sync.WaitGroup{} - comments := make([]types.Comment, 0) + comments := make([]Comment, 0) data.Get("data").ForEach( func(key, value gjson.Result) bool { wg.Add(1) @@ -45,7 +58,7 @@ func FetchComments(galleryID string) ([]types.Comment, error) { return comments, nil } -func ParseComment(data gjson.Result) types.Comment { +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()) @@ -54,7 +67,7 @@ func ParseComment(data gjson.Result) types.Comment { userAvatar := strings.ReplaceAll(data.Get("account.avatar").String(), "https://i.imgur.com", "") wg := sync.WaitGroup{} - comments := make([]types.Comment, 0) + comments := make([]Comment, 0) data.Get("comments").ForEach( func(key, value gjson.Result) bool { wg.Add(1) @@ -69,9 +82,9 @@ func ParseComment(data gjson.Result) types.Comment { ) wg.Wait() - return types.Comment{ + return Comment{ Comments: comments, - User: types.User{ + User: User{ Id: data.Get("account.id").Int(), Username: data.Get("account.username").String(), Avatar: userAvatar, diff --git a/api/tag.go b/api/tag.go index e8d172e..9e016df 100644 --- a/api/tag.go +++ b/api/tag.go @@ -7,23 +7,31 @@ import ( "sync" "time" - "codeberg.org/video-prize-ranch/rimgo/types" "codeberg.org/video-prize-ranch/rimgo/utils" "github.com/patrickmn/go-cache" "github.com/tidwall/gjson" ) +type Tag struct { + Tag string + Display string + Sort string + PostCount int64 + Posts []Submission + Background string +} + var tagCache = cache.New(15*time.Minute, 15*time.Minute) -func FetchTag(tag string, sort string, page string) (types.Tag, error) { +func FetchTag(tag string, sort string, page string) (Tag, error) { cacheData, found := tagCache.Get(tag + sort + page) if found { - return cacheData.(types.Tag), nil + return cacheData.(Tag), nil } req, err := http.NewRequest("GET", "https://api.imgur.com/post/v1/posts/t/"+tag, nil) if err != nil { - return types.Tag{}, err + return Tag{}, err } q := req.URL.Query() @@ -49,29 +57,29 @@ func FetchTag(tag string, sort string, page string) (types.Tag, error) { res, err := http.DefaultClient.Do(req) if err != nil { - return types.Tag{}, err + return Tag{}, err } body, err := ioutil.ReadAll(res.Body) if err != nil { - return types.Tag{}, err + return Tag{}, err } data := gjson.Parse(string(body)) wg := sync.WaitGroup{} - posts := make([]types.Submission, 0) + posts := make([]Submission, 0) data.Get("posts").ForEach( func(key, value gjson.Result) bool { wg.Add(1) go func() { defer wg.Done() - posts = append(posts, types.Submission{ + posts = append(posts, Submission{ Id: value.Get("id").String(), Title: value.Get("title").String(), Link: strings.ReplaceAll(value.Get("url").String(), "https://imgur.com", ""), - Cover: types.Media{ + Cover: Media{ Id: value.Get("cover_id").String(), Type: value.Get("cover.type").String(), Url: strings.ReplaceAll(value.Get("cover.url").String(), "https://i.imgur.com", ""), @@ -91,7 +99,7 @@ func FetchTag(tag string, sort string, page string) (types.Tag, error) { wg.Wait() - tagData := types.Tag{ + tagData := Tag{ Tag: tag, Display: data.Get("display").String(), Sort: sort, diff --git a/api/user.go b/api/user.go index 1c9c70f..895ea78 100644 --- a/api/user.go +++ b/api/user.go @@ -7,35 +7,57 @@ import ( "sync" "time" - "codeberg.org/video-prize-ranch/rimgo/types" "codeberg.org/video-prize-ranch/rimgo/utils" "github.com/patrickmn/go-cache" "github.com/tidwall/gjson" ) +type User struct { + Id int64 + Bio string + Username string + Points int64 + Cover string + Avatar string + CreatedAt string +} + +type Submission struct { + Id string + Title string + Link string + Cover Media + Points int64 + Upvotes int64 + Downvotes int64 + Comments int64 + Views int64 + IsAlbum bool +} + var userCache = cache.New(30*time.Minute, 15*time.Minute) -func FetchUser(username string) (types.User, error) { +func FetchUser(username string) (User, error) { cacheData, found := userCache.Get(username) if found { - return cacheData.(types.User), nil + return cacheData.(User), nil } res, err := http.Get("https://api.imgur.com/account/v1/accounts/" + username + "?client_id=" + utils.Config["imgurId"].(string)) if err != nil { - return types.User{}, err + return User{}, err } body, err := io.ReadAll(res.Body) if err != nil { - return types.User{}, err + return User{}, err } data := gjson.Parse(string(body)) createdTime, _ := time.Parse(time.RFC3339, data.Get("created_at").String()) - user := types.User{ + user := User{ Id: data.Get("id").Int(), Bio: data.Get("bio").String(), Username: data.Get("username").String(), @@ -49,18 +71,18 @@ func FetchUser(username string) (types.User, error) { return user, nil } -func FetchSubmissions(username string, sort string, page string) ([]types.Submission, error) { +func FetchSubmissions(username string, sort string, page string) ([]Submission, error) { cacheData, found := userCache.Get(username + "-submissions") if found { - return cacheData.([]types.Submission), nil + return cacheData.([]Submission), nil } data, err := utils.GetJSON("https://api.imgur.com/3/account/" + username + "/submissions/" + page + "/" + sort + "?album_previews=1&client_id=" + utils.Config["imgurId"].(string)) if err != nil { - return []types.Submission{}, err + return []Submission{}, err } - submissions := []types.Submission{} + submissions := []Submission{} wg := sync.WaitGroup{} data.Get("data").ForEach( @@ -71,16 +93,16 @@ func FetchSubmissions(username string, sort string, page string) ([]types.Submis defer wg.Done() coverData := value.Get("images.#(id==\"" + value.Get("cover").String() + "\")") - cover := types.Media{} + cover := Media{} if coverData.Exists() { - cover = types.Media{ + 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", ""), } } else { - cover = types.Media{ + cover = Media{ Id: value.Get("id").String(), Description: value.Get("description").String(), Type: strings.Split(value.Get("type").String(), "/")[0], @@ -90,7 +112,7 @@ func FetchSubmissions(username string, sort string, page string) ([]types.Submis id := value.Get("id").String() - submissions = append(submissions, types.Submission{ + submissions = append(submissions, Submission{ Id: id, Link: "/a/" + id, Title: value.Get("title").String(), diff --git a/pages/embed.go b/pages/embed.go index 3471337..3adbef3 100644 --- a/pages/embed.go +++ b/pages/embed.go @@ -4,7 +4,6 @@ import ( "strings" "codeberg.org/video-prize-ranch/rimgo/api" - "codeberg.org/video-prize-ranch/rimgo/types" "codeberg.org/video-prize-ranch/rimgo/utils" "github.com/gofiber/fiber/v2" ) @@ -14,7 +13,7 @@ func HandleEmbed(c *fiber.Ctx) error { c.Set("Cache-Control", "public,max-age=31557600") c.Set("Content-Security-Policy", "default-src 'none'; media-src 'self'; style-src 'self'; img-src 'self'; font-src 'self'; block-all-mixed-content") - post, err := types.Album{}, error(nil) + post, err := api.Album{}, error(nil) switch { case strings.HasPrefix(c.Path(), "/a"): post, err = api.FetchAlbum(c.Params("postID")) diff --git a/pages/post.go b/pages/post.go index 6a241d7..4999b40 100644 --- a/pages/post.go +++ b/pages/post.go @@ -4,7 +4,6 @@ import ( "strings" "codeberg.org/video-prize-ranch/rimgo/api" - "codeberg.org/video-prize-ranch/rimgo/types" "codeberg.org/video-prize-ranch/rimgo/utils" "github.com/gofiber/fiber/v2" ) @@ -14,7 +13,7 @@ func HandlePost(c *fiber.Ctx) error { c.Set("X-Frame-Options", "DENY") c.Set("Content-Security-Policy", "default-src 'none'; media-src 'self'; style-src 'self'; img-src 'self'; font-src 'self'; manifest-src 'self'; block-all-mixed-content") - post, err := types.Album{}, error(nil) + post, err := api.Album{}, error(nil) switch { case strings.HasPrefix(c.Path(), "/a"): post, err = api.FetchAlbum(c.Params("postID")) @@ -31,7 +30,7 @@ func HandlePost(c *fiber.Ctx) error { return err } - comments := []types.Comment{} + comments := []api.Comment{} if post.SharedWithCommunity { c.Set("Cache-Control", "public,max-age=604800") comments, err = api.FetchComments(c.Params("postID")) diff --git a/pages/user.go b/pages/user.go index 9a59c74..cb8f301 100644 --- a/pages/user.go +++ b/pages/user.go @@ -5,7 +5,6 @@ import ( "sync" "codeberg.org/video-prize-ranch/rimgo/api" - "codeberg.org/video-prize-ranch/rimgo/types" "codeberg.org/video-prize-ranch/rimgo/utils" "github.com/gofiber/fiber/v2" ) @@ -28,7 +27,7 @@ func HandleUser(c *fiber.Ctx) error { wg := sync.WaitGroup{} wg.Add(2) - user, err := types.User{}, error(nil) + user, err := api.User{}, error(nil) go func() { defer wg.Done() user, err = api.FetchUser(c.Params("userID")) @@ -37,7 +36,7 @@ func HandleUser(c *fiber.Ctx) error { return err } - submissions, err := []types.Submission{}, error(nil) + submissions, err := []api.Submission{}, error(nil) go func() { defer wg.Done() submissions, err = api.FetchSubmissions(c.Params("userID"), "newest", page) diff --git a/types/Album.go b/types/Album.go deleted file mode 100644 index 2cfb88c..0000000 --- a/types/Album.go +++ /dev/null @@ -1,16 +0,0 @@ -package types - -type Album struct { - Id string - Title string - Views int64 - Upvotes int64 - Downvotes int64 - SharedWithCommunity bool - CreatedAt string - UpdatedAt string - Comments int64 - User User - Media []Media - Tags []Tag -} diff --git a/types/Comment.go b/types/Comment.go deleted file mode 100644 index 3541ed1..0000000 --- a/types/Comment.go +++ /dev/null @@ -1,15 +0,0 @@ -package types - -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 -} diff --git a/types/Media.go b/types/Media.go deleted file mode 100644 index 6dfe614..0000000 --- a/types/Media.go +++ /dev/null @@ -1,11 +0,0 @@ -package types - -type Media struct { - Id string - Name string - Title string - Description string - Url string - Type string - MimeType string -} diff --git a/types/Submission.go b/types/Submission.go deleted file mode 100644 index 33cd535..0000000 --- a/types/Submission.go +++ /dev/null @@ -1,14 +0,0 @@ -package types - -type Submission struct { - Id string - Title string - Link string - Cover Media - Points int64 - Upvotes int64 - Downvotes int64 - Comments int64 - Views int64 - IsAlbum bool -} \ No newline at end of file diff --git a/types/Tag.go b/types/Tag.go deleted file mode 100644 index 82db55b..0000000 --- a/types/Tag.go +++ /dev/null @@ -1,10 +0,0 @@ -package types - -type Tag struct { - Tag string - Display string - Sort string - PostCount int64 - Posts []Submission - Background string -} \ No newline at end of file diff --git a/types/User.go b/types/User.go deleted file mode 100644 index 868975f..0000000 --- a/types/User.go +++ /dev/null @@ -1,11 +0,0 @@ -package types - -type User struct { - Id int64 - Bio string - Username string - Points int64 - Cover string - Avatar string - CreatedAt string -} \ No newline at end of file