[feature] Refetch emojis when they change on remote instances (#905)

* select emoji using image_static_url

* use updated on AP emojis

* allow refetch of updated emojis

* cheeky workaround for test

* clean up old files for refreshed emoji

* check error for originalPostData

* shorten GetEmojiByStaticImageURL

* delete kirby (sorry nintendo)
This commit is contained in:
tobi
2022-10-13 15:16:24 +02:00
committed by GitHub
parent 3ca7164455
commit 70d65b683f
22 changed files with 413 additions and 74 deletions

View File

@@ -37,19 +37,26 @@ func NewEmojiCache() *EmojiCache {
RegisterLookups: func(lm *cache.LookupMap[string, string]) {
lm.RegisterLookup("uri")
lm.RegisterLookup("shortcodedomain")
lm.RegisterLookup("imagestaticurl")
},
AddLookups: func(lm *cache.LookupMap[string, string], emoji *gtsmodel.Emoji) {
lm.Set("shortcodedomain", shortcodeDomainKey(emoji.Shortcode, emoji.Domain), emoji.ID)
if uri := emoji.URI; uri != "" {
lm.Set("uri", uri, emoji.URI)
lm.Set("shortcodedomain", shortcodeDomainKey(emoji.Shortcode, emoji.Domain), emoji.ID)
lm.Set("uri", uri, emoji.ID)
}
if imageStaticURL := emoji.ImageStaticURL; imageStaticURL != "" {
lm.Set("imagestaticurl", imageStaticURL, emoji.ID)
}
},
DeleteLookups: func(lm *cache.LookupMap[string, string], emoji *gtsmodel.Emoji) {
lm.Delete("shortcodedomain", shortcodeDomainKey(emoji.Shortcode, emoji.Domain))
if uri := emoji.URI; uri != "" {
lm.Delete("uri", uri)
lm.Delete("shortcodedomain", shortcodeDomainKey(emoji.Shortcode, emoji.Domain))
}
if imageStaticURL := emoji.ImageStaticURL; imageStaticURL != "" {
lm.Delete("imagestaticurl", imageStaticURL)
}
},
})
@@ -72,6 +79,10 @@ func (c *EmojiCache) GetByShortcodeDomain(shortcode string, domain string) (*gts
return c.cache.GetBy("shortcodedomain", shortcodeDomainKey(shortcode, domain))
}
func (c *EmojiCache) GetByImageStaticURL(imageStaticURL string) (*gtsmodel.Emoji, bool) {
return c.cache.GetBy("imagestaticurl", imageStaticURL)
}
// Put places an emoji in the cache, ensuring that the object place is a copy for thread-safety
func (c *EmojiCache) Put(emoji *gtsmodel.Emoji) {
if emoji == nil || emoji.ID == "" {
@@ -80,6 +91,10 @@ func (c *EmojiCache) Put(emoji *gtsmodel.Emoji) {
c.cache.Set(emoji.ID, copyEmoji(emoji))
}
func (c *EmojiCache) Invalidate(emojiID string) {
c.cache.Invalidate(emojiID)
}
// copyEmoji performs a surface-level copy of emoji, only keeping attached IDs intact, not the objects.
// due to all the data being copied being 99% primitive types or strings (which are immutable and passed by ptr)
// this should be a relatively cheap process