Fix tag backgrounds
This commit is contained in:
		
							parent
							
								
									e0efe6caca
								
							
						
					
					
						commit
						150df2435d
					
				
							
								
								
									
										27
									
								
								api/album.go
								
								
								
								
							
							
						
						
									
										27
									
								
								api/album.go
								
								
								
								
							| 
						 | 
					@ -19,9 +19,9 @@ type Album struct {
 | 
				
			||||||
	CreatedAt           string
 | 
						CreatedAt           string
 | 
				
			||||||
	UpdatedAt           string
 | 
						UpdatedAt           string
 | 
				
			||||||
	Comments            int64
 | 
						Comments            int64
 | 
				
			||||||
	User								User
 | 
						User                User
 | 
				
			||||||
	Media               []Media
 | 
						Media               []Media
 | 
				
			||||||
	Tags								[]Tag
 | 
						Tags                []Tag
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
type Media struct {
 | 
					type Media struct {
 | 
				
			||||||
| 
						 | 
					@ -30,8 +30,8 @@ type Media struct {
 | 
				
			||||||
	Title       string
 | 
						Title       string
 | 
				
			||||||
	Description string
 | 
						Description string
 | 
				
			||||||
	Url         string
 | 
						Url         string
 | 
				
			||||||
	Type				string
 | 
						Type        string
 | 
				
			||||||
	MimeType		string
 | 
						MimeType    string
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
var albumCache = cache.New(1*time.Hour, 15*time.Minute)
 | 
					var albumCache = cache.New(1*time.Hour, 15*time.Minute)
 | 
				
			||||||
| 
						 | 
					@ -52,7 +52,7 @@ func FetchAlbum(albumID string) (Album, error) {
 | 
				
			||||||
		return Album{}, err
 | 
							return Album{}, err
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	albumCache.Set(albumID + "-album", album, cache.DefaultExpiration)
 | 
						albumCache.Set(albumID+"-album", album, cache.DefaultExpiration)
 | 
				
			||||||
	return album, err
 | 
						return album, err
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -72,7 +72,7 @@ func FetchPosts(albumID string) (Album, error) {
 | 
				
			||||||
		return Album{}, err
 | 
							return Album{}, err
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	albumCache.Set(albumID + "-posts", album, cache.DefaultExpiration)
 | 
						albumCache.Set(albumID+"-posts", album, cache.DefaultExpiration)
 | 
				
			||||||
	return album, nil
 | 
						return album, nil
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -92,7 +92,7 @@ func FetchMedia(mediaID string) (Album, error) {
 | 
				
			||||||
		return Album{}, err
 | 
							return Album{}, err
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	albumCache.Set(mediaID + "-media", album, cache.DefaultExpiration)
 | 
						albumCache.Set(mediaID+"-media", album, cache.DefaultExpiration)
 | 
				
			||||||
	return album, nil
 | 
						return album, nil
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -121,9 +121,10 @@ func ParseAlbum(data gjson.Result) (Album, error) {
 | 
				
			||||||
	data.Get("tags").ForEach(
 | 
						data.Get("tags").ForEach(
 | 
				
			||||||
		func(key gjson.Result, value gjson.Result) bool {
 | 
							func(key gjson.Result, value gjson.Result) bool {
 | 
				
			||||||
			tags = append(tags, Tag{
 | 
								tags = append(tags, Tag{
 | 
				
			||||||
				Tag: value.Get("tag").String(),
 | 
									Tag:          value.Get("tag").String(),
 | 
				
			||||||
				Display: value.Get("display").String(),
 | 
									Display:      value.Get("display").String(),
 | 
				
			||||||
				Background: "/" + value.Get("background_id").String() + ".webp",
 | 
									Background:   "/" + value.Get("background_id").String() + ".webp",
 | 
				
			||||||
 | 
									BackgroundId: value.Get("background_id").String(),
 | 
				
			||||||
			})
 | 
								})
 | 
				
			||||||
			return true
 | 
								return true
 | 
				
			||||||
		},
 | 
							},
 | 
				
			||||||
| 
						 | 
					@ -144,15 +145,15 @@ func ParseAlbum(data gjson.Result) (Album, error) {
 | 
				
			||||||
		Comments:            data.Get("comment_count").Int(),
 | 
							Comments:            data.Get("comment_count").Int(),
 | 
				
			||||||
		CreatedAt:           createdAt,
 | 
							CreatedAt:           createdAt,
 | 
				
			||||||
		Media:               media,
 | 
							Media:               media,
 | 
				
			||||||
		Tags:								 tags,
 | 
							Tags:                tags,
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	account := data.Get("account")
 | 
						account := data.Get("account")
 | 
				
			||||||
	if account.Raw != "" {
 | 
						if account.Raw != "" {
 | 
				
			||||||
		album.User = User{
 | 
							album.User = User{
 | 
				
			||||||
			Id: account.Get("id").Int(),
 | 
								Id:       account.Get("id").Int(),
 | 
				
			||||||
			Username: account.Get("username").String(),
 | 
								Username: account.Get("username").String(),
 | 
				
			||||||
			Avatar: strings.ReplaceAll(account.Get("avatar_url").String(), "https://i.imgur.com", ""),
 | 
								Avatar:   strings.ReplaceAll(account.Get("avatar_url").String(), "https://i.imgur.com", ""),
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
							
								
								
									
										29
									
								
								api/tag.go
								
								
								
								
							
							
						
						
									
										29
									
								
								api/tag.go
								
								
								
								
							| 
						 | 
					@ -13,12 +13,13 @@ import (
 | 
				
			||||||
)
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
type Tag struct {
 | 
					type Tag struct {
 | 
				
			||||||
	Tag	        string
 | 
						Tag          string
 | 
				
			||||||
	Display			string
 | 
						Display      string
 | 
				
			||||||
	Sort				string
 | 
						Sort         string
 | 
				
			||||||
	PostCount	  int64
 | 
						PostCount    int64
 | 
				
			||||||
	Posts				[]Submission
 | 
						Posts        []Submission
 | 
				
			||||||
	Background  string
 | 
						Background   string
 | 
				
			||||||
 | 
						BackgroundId string
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
var tagCache = cache.New(15*time.Minute, 15*time.Minute)
 | 
					var tagCache = cache.New(15*time.Minute, 15*time.Minute)
 | 
				
			||||||
| 
						 | 
					@ -80,9 +81,9 @@ func FetchTag(tag string, sort string, page string) (Tag, error) {
 | 
				
			||||||
					Title: value.Get("title").String(),
 | 
										Title: value.Get("title").String(),
 | 
				
			||||||
					Link:  strings.ReplaceAll(value.Get("url").String(), "https://imgur.com", ""),
 | 
										Link:  strings.ReplaceAll(value.Get("url").String(), "https://imgur.com", ""),
 | 
				
			||||||
					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(),
 | 
				
			||||||
						Url: strings.ReplaceAll(value.Get("cover.url").String(), "https://i.imgur.com", ""),
 | 
											Url:  strings.ReplaceAll(value.Get("cover.url").String(), "https://i.imgur.com", ""),
 | 
				
			||||||
					},
 | 
										},
 | 
				
			||||||
					Points:    value.Get("point_count").Int(),
 | 
										Points:    value.Get("point_count").Int(),
 | 
				
			||||||
					Upvotes:   value.Get("upvote_count").Int(),
 | 
										Upvotes:   value.Get("upvote_count").Int(),
 | 
				
			||||||
| 
						 | 
					@ -100,12 +101,12 @@ func FetchTag(tag string, sort string, page string) (Tag, error) {
 | 
				
			||||||
	wg.Wait()
 | 
						wg.Wait()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	tagData := Tag{
 | 
						tagData := Tag{
 | 
				
			||||||
		Tag: tag,
 | 
							Tag:          tag,
 | 
				
			||||||
		Display: data.Get("display").String(),
 | 
							Display:      data.Get("display").String(),
 | 
				
			||||||
		Sort: sort,
 | 
							Sort:         sort,
 | 
				
			||||||
		PostCount: data.Get("post_count").Int(),
 | 
							PostCount:    data.Get("post_count").Int(),
 | 
				
			||||||
		Posts: posts,
 | 
							Posts:        posts,
 | 
				
			||||||
		Background: "/" + data.Get("background_id").String() + ".webp",
 | 
							Background:   "/" + data.Get("background_id").String() + ".webp",
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	tagCache.Set(tag, tagData, cache.DefaultExpiration)
 | 
						tagCache.Set(tag, tagData, cache.DefaultExpiration)
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -1,6 +1,8 @@
 | 
				
			||||||
package pages
 | 
					package pages
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import (
 | 
					import (
 | 
				
			||||||
 | 
						"crypto/rand"
 | 
				
			||||||
 | 
						"fmt"
 | 
				
			||||||
	"strings"
 | 
						"strings"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	"codeberg.org/video-prize-ranch/rimgo/api"
 | 
						"codeberg.org/video-prize-ranch/rimgo/api"
 | 
				
			||||||
| 
						 | 
					@ -11,7 +13,6 @@ import (
 | 
				
			||||||
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")
 | 
				
			||||||
	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 := api.Album{}, error(nil)
 | 
						post, err := api.Album{}, error(nil)
 | 
				
			||||||
	switch {
 | 
						switch {
 | 
				
			||||||
| 
						 | 
					@ -26,7 +27,7 @@ func HandlePost(c *fiber.Ctx) error {
 | 
				
			||||||
		c.Status(404)
 | 
							c.Status(404)
 | 
				
			||||||
		return c.Render("errors/404", nil)
 | 
							return c.Render("errors/404", nil)
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 	if err != nil {
 | 
						if err != nil {
 | 
				
			||||||
		return err
 | 
							return err
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -41,8 +42,19 @@ func HandlePost(c *fiber.Ctx) error {
 | 
				
			||||||
		c.Set("Cache-Control", "public,max-age=31557600")
 | 
							c.Set("Cache-Control", "public,max-age=31557600")
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						nonce := ""
 | 
				
			||||||
 | 
						csp := "default-src 'none'; media-src 'self'; img-src 'self'; font-src 'self'; manifest-src 'self'; block-all-mixed-content; style-src 'self'"
 | 
				
			||||||
 | 
						if len(post.Tags) != 0 {
 | 
				
			||||||
 | 
							b := make([]byte, 8)
 | 
				
			||||||
 | 
							rand.Read(b)
 | 
				
			||||||
 | 
							nonce = fmt.Sprintf("%x", b)
 | 
				
			||||||
 | 
							csp = csp + " 'nonce-" + nonce + "'"
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						c.Set("Content-Security-Policy", csp)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	return c.Render("post", fiber.Map{
 | 
						return c.Render("post", fiber.Map{
 | 
				
			||||||
		"post":    post,
 | 
							"post":     post,
 | 
				
			||||||
		"comments": comments,
 | 
							"comments": comments,
 | 
				
			||||||
 | 
							"nonce":    nonce,
 | 
				
			||||||
	})
 | 
						})
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
| 
						 | 
					@ -74,9 +74,14 @@
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    {{#if post.tags}}
 | 
					    {{#if post.tags}}
 | 
				
			||||||
    <div class="tags">
 | 
					    <div class="tags">
 | 
				
			||||||
 | 
					      <style nonce="{{nonce}}">
 | 
				
			||||||
 | 
					        {{#each post.tags}}
 | 
				
			||||||
 | 
					          .{{this.BackgroundId}} { background-image: url('{{this.Background}}') }
 | 
				
			||||||
 | 
					        {{/each}}	      
 | 
				
			||||||
 | 
					      </style>
 | 
				
			||||||
      {{#each post.tags}}
 | 
					      {{#each post.tags}}
 | 
				
			||||||
      <a href="/t/{{this.Tag}}">
 | 
					      <a href="/t/{{this.Tag}}">
 | 
				
			||||||
        <div class="tag" style="background-image: url('{{this.Background}}');">
 | 
					        <div class="tag {{this.BackgroundId}}">
 | 
				
			||||||
          <p class="tag__display">{{this.Display}}</p>
 | 
					          <p class="tag__display">{{this.Display}}</p>
 | 
				
			||||||
        </div>
 | 
					        </div>
 | 
				
			||||||
      </a>
 | 
					      </a>
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
		Reference in New Issue