mirror of
https://codeberg.org/video-prize-ranch/rimgo.git
synced 2025-06-27 23:48:23 +00:00
added next button for tagged posts
This commit is contained in:
parent
29203a1a78
commit
5c3289d5db
18
api/tag.go
18
api/tag.go
@ -3,8 +3,8 @@ package api
|
|||||||
import (
|
import (
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"net/url"
|
||||||
"strings"
|
"strings"
|
||||||
"sync"
|
|
||||||
|
|
||||||
"github.com/patrickmn/go-cache"
|
"github.com/patrickmn/go-cache"
|
||||||
"github.com/tidwall/gjson"
|
"github.com/tidwall/gjson"
|
||||||
@ -64,18 +64,21 @@ func (client *Client) FetchTag(tag string, sort string, page string) (Tag, error
|
|||||||
|
|
||||||
data := gjson.Parse(string(body))
|
data := gjson.Parse(string(body))
|
||||||
|
|
||||||
wg := sync.WaitGroup{}
|
|
||||||
posts := make([]Submission, 0)
|
posts := make([]Submission, 0)
|
||||||
data.Get("posts").ForEach(
|
data.Get("posts").ForEach(
|
||||||
func(key, value gjson.Result) bool {
|
func(key, value gjson.Result) bool {
|
||||||
wg.Add(1)
|
url, _ := url.Parse(strings.ReplaceAll(value.Get("url").String(), "https://imgur.com", ""))
|
||||||
|
q := url.Query()
|
||||||
|
q.Add("tag", tag)
|
||||||
|
q.Add("sort", sort)
|
||||||
|
q.Add("page", page)
|
||||||
|
q.Add("i", key.String())
|
||||||
|
url.RawQuery = q.Encode()
|
||||||
|
|
||||||
go func() {
|
|
||||||
defer wg.Done()
|
|
||||||
posts = append(posts, Submission{
|
posts = append(posts, Submission{
|
||||||
Id: value.Get("id").String(),
|
Id: value.Get("id").String(),
|
||||||
Title: value.Get("title").String(),
|
Title: value.Get("title").String(),
|
||||||
Link: strings.ReplaceAll(value.Get("url").String(), "https://imgur.com", ""),
|
Link: url.String(),
|
||||||
Cover: Media{
|
Cover: Media{
|
||||||
Id: value.Get("cover_id").String(),
|
Id: value.Get("cover_id").String(),
|
||||||
Type: value.Get("cover.type").String(),
|
Type: value.Get("cover.type").String(),
|
||||||
@ -88,14 +91,11 @@ func (client *Client) FetchTag(tag string, sort string, page string) (Tag, error
|
|||||||
Views: value.Get("view_count").Int(),
|
Views: value.Get("view_count").Int(),
|
||||||
IsAlbum: value.Get("is_album").Bool(),
|
IsAlbum: value.Get("is_album").Bool(),
|
||||||
})
|
})
|
||||||
}()
|
|
||||||
|
|
||||||
return true
|
return true
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
|
|
||||||
wg.Wait()
|
|
||||||
|
|
||||||
tagData := Tag{
|
tagData := Tag{
|
||||||
Tag: tag,
|
Tag: tag,
|
||||||
Display: data.Get("display").String(),
|
Display: data.Get("display").String(),
|
||||||
|
@ -3,6 +3,7 @@ package pages
|
|||||||
import (
|
import (
|
||||||
"crypto/rand"
|
"crypto/rand"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"codeberg.org/rimgo/rimgo/api"
|
"codeberg.org/rimgo/rimgo/api"
|
||||||
@ -10,6 +11,27 @@ import (
|
|||||||
"github.com/gofiber/fiber/v2"
|
"github.com/gofiber/fiber/v2"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// Cursed function
|
||||||
|
func nextInTag(client *api.Client, tagname, sort, page, I string) string {
|
||||||
|
i, err := strconv.Atoi(I)
|
||||||
|
if err != nil || i < 0 {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
tag, err := client.FetchTag(tagname, sort, page)
|
||||||
|
if err != nil {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
if i >= len(tag.Posts)-1 {
|
||||||
|
pageNumber, _ := strconv.Atoi(page)
|
||||||
|
tagn, err := client.FetchTag(tagname, sort, strconv.Itoa(pageNumber+1))
|
||||||
|
if err != nil {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
return tagn.Posts[0].Link
|
||||||
|
}
|
||||||
|
return tag.Posts[i+1].Link
|
||||||
|
}
|
||||||
|
|
||||||
func HandlePost(c *fiber.Ctx) error {
|
func HandlePost(c *fiber.Ctx) error {
|
||||||
utils.SetHeaders(c)
|
utils.SetHeaders(c)
|
||||||
c.Set("X-Frame-Options", "DENY")
|
c.Set("X-Frame-Options", "DENY")
|
||||||
@ -58,8 +80,12 @@ func HandlePost(c *fiber.Ctx) error {
|
|||||||
}
|
}
|
||||||
c.Set("Content-Security-Policy", csp)
|
c.Set("Content-Security-Policy", csp)
|
||||||
|
|
||||||
|
tag, sort, page, index := c.Query("tag"), c.Query("sort"), c.Query("page"), c.Query("i")
|
||||||
|
next := nextInTag(ApiClient, tag, sort, page, index)
|
||||||
|
|
||||||
return c.Render("post", fiber.Map{
|
return c.Render("post", fiber.Map{
|
||||||
"post": post,
|
"post": post,
|
||||||
|
"next": next,
|
||||||
"comments": comments,
|
"comments": comments,
|
||||||
"nonce": nonce,
|
"nonce": nonce,
|
||||||
})
|
})
|
||||||
|
@ -25,7 +25,8 @@
|
|||||||
</header>
|
</header>
|
||||||
|
|
||||||
<main>
|
<main>
|
||||||
<div class="flex flex-col gap-2 md:flex-row md:gap-4 md:items-center my-4">
|
<div class="flex flex-col sm:flex-row my-4 w-full justify-between">
|
||||||
|
<div class="flex flex-col gap-2 md:flex-row md:gap-4 md:items-center">
|
||||||
{{#if post.User.Username}}
|
{{#if post.User.Username}}
|
||||||
<a href="/user/{{post.User.Username}}" class="flex gap-2 items-center">
|
<a href="/user/{{post.User.Username}}" class="flex gap-2 items-center">
|
||||||
<img src="{{post.User.Avatar}}" class="rounded-full" width="36" height="36" />
|
<img src="{{post.User.Avatar}}" class="rounded-full" width="36" height="36" />
|
||||||
@ -51,6 +52,17 @@
|
|||||||
{{/if}}
|
{{/if}}
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
{{#noteq next ""}}
|
||||||
|
<div class="flex">
|
||||||
|
{{#noteq prev ""}}
|
||||||
|
<!-- TODO: prev -->
|
||||||
|
<a href="{{prev}}" title="Previous" class="px-3 py-2 rounded-l-lg bg-gray-400 text-gray-800"><</a>
|
||||||
|
{{/noteq}}
|
||||||
|
<a href="{{next}}" class="px-3 py-2 rounded-r-lg [&:only-child]:rounded-lg bg-green-400 text-gray-800">Next
|
||||||
|
></a>
|
||||||
|
</div>
|
||||||
|
{{/noteq}}
|
||||||
|
</div>
|
||||||
|
|
||||||
<div class="flex flex-center flex-col break-words">
|
<div class="flex flex-center flex-col break-words">
|
||||||
{{#each post.Media}}
|
{{#each post.Media}}
|
||||||
@ -101,7 +113,8 @@
|
|||||||
{{#if comments}}
|
{{#if comments}}
|
||||||
<div>
|
<div>
|
||||||
<input id="comments__expandBtn" type="checkbox" checked>
|
<input id="comments__expandBtn" type="checkbox" checked>
|
||||||
<label class="comments__expandBtn__label my-2 py-4 border-solid border-t-2 border-slate-400" for="comments__expandBtn">
|
<label class="comments__expandBtn__label my-2 py-4 border-solid border-t-2 border-slate-400"
|
||||||
|
for="comments__expandBtn">
|
||||||
<h3 class="text-xl font-bold">Comments ({{post.Comments}})</h3>
|
<h3 class="text-xl font-bold">Comments ({{post.Comments}})</h3>
|
||||||
<span class="text-xl font-bold"></span>
|
<span class="text-xl font-bold"></span>
|
||||||
</label>
|
</label>
|
||||||
|
Loading…
Reference in New Issue
Block a user