From 70d65b683fa963d2a8761182a2ddd2f4f9a86bb4 Mon Sep 17 00:00:00 2001 From: tobi <31960611+tsmethurst@users.noreply.github.com> Date: Thu, 13 Oct 2022 15:16:24 +0200 Subject: [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) --- internal/cache/emoji.go | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) (limited to 'internal/cache') diff --git a/internal/cache/emoji.go b/internal/cache/emoji.go index eda7583ea..117f5475e 100644 --- a/internal/cache/emoji.go +++ b/internal/cache/emoji.go @@ -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 -- cgit v1.2.3