diff options
author | 2022-11-14 23:47:27 +0100 | |
---|---|---|
committer | 2022-11-14 22:47:27 +0000 | |
commit | 4cd00d546c495b085487d11f2fe2c4928600dc10 (patch) | |
tree | 6605424baafddf020a4a6e0a0ddcde9293c1cb56 /internal/cache | |
parent | [chore] Remove unused `admin account suspend` action (#1047) (diff) | |
download | gotosocial-4cd00d546c495b085487d11f2fe2c4928600dc10.tar.xz |
[feature] Allow newly uploaded emojis to be placed in categories (#939)
* [feature] Add emoji categories GET
Serialize emojis in appropriate categories; make it possible to get categories via the admin API
* [feature] Create (or use existing) category for new emoji uploads
* fix lint issue
* update misleading line in swagger docs
Diffstat (limited to 'internal/cache')
-rw-r--r-- | internal/cache/emojicategory.go | 84 |
1 files changed, 84 insertions, 0 deletions
diff --git a/internal/cache/emojicategory.go b/internal/cache/emojicategory.go new file mode 100644 index 000000000..17df5591a --- /dev/null +++ b/internal/cache/emojicategory.go @@ -0,0 +1,84 @@ +/* + GoToSocial + Copyright (C) 2021-2022 GoToSocial Authors admin@gotosocial.org + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU Affero General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Affero General Public License for more details. + + You should have received a copy of the GNU Affero General Public License + along with this program. If not, see <http://www.gnu.org/licenses/>. +*/ + +package cache + +import ( + "strings" + "time" + + "codeberg.org/gruf/go-cache/v2" + "github.com/superseriousbusiness/gotosocial/internal/gtsmodel" +) + +// EmojiCategoryCache is a cache wrapper to provide ID lookups for gtsmodel.EmojiCategory +type EmojiCategoryCache struct { + cache cache.LookupCache[string, string, *gtsmodel.EmojiCategory] +} + +// NewEmojiCategoryCache returns a new instantiated EmojiCategoryCache object +func NewEmojiCategoryCache() *EmojiCategoryCache { + c := &EmojiCategoryCache{} + c.cache = cache.NewLookup(cache.LookupCfg[string, string, *gtsmodel.EmojiCategory]{ + RegisterLookups: func(lm *cache.LookupMap[string, string]) { + lm.RegisterLookup("name") + }, + + AddLookups: func(lm *cache.LookupMap[string, string], emojiCategory *gtsmodel.EmojiCategory) { + lm.Set(("name"), strings.ToLower(emojiCategory.Name), emojiCategory.ID) + }, + + DeleteLookups: func(lm *cache.LookupMap[string, string], emojiCategory *gtsmodel.EmojiCategory) { + lm.Delete("name", strings.ToLower(emojiCategory.Name)) + }, + }) + c.cache.SetTTL(time.Minute*5, false) + c.cache.Start(time.Second * 10) + return c +} + +// GetByID attempts to fetch an emojiCategory from the cache by its ID, you will receive a copy for thread-safety +func (c *EmojiCategoryCache) GetByID(id string) (*gtsmodel.EmojiCategory, bool) { + return c.cache.Get(id) +} + +// GetByName attempts to fetch an emojiCategory from the cache by its name, you will receive a copy for thread-safety +func (c *EmojiCategoryCache) GetByName(name string) (*gtsmodel.EmojiCategory, bool) { + return c.cache.GetBy("name", strings.ToLower(name)) +} + +// Put places an emojiCategory in the cache, ensuring that the object place is a copy for thread-safety +func (c *EmojiCategoryCache) Put(emoji *gtsmodel.EmojiCategory) { + if emoji == nil || emoji.ID == "" { + panic("invalid emoji") + } + c.cache.Set(emoji.ID, copyEmojiCategory(emoji)) +} + +func (c *EmojiCategoryCache) Invalidate(emojiID string) { + c.cache.Invalidate(emojiID) +} + +func copyEmojiCategory(emojiCategory *gtsmodel.EmojiCategory) *gtsmodel.EmojiCategory { + return >smodel.EmojiCategory{ + ID: emojiCategory.ID, + CreatedAt: emojiCategory.CreatedAt, + UpdatedAt: emojiCategory.UpdatedAt, + Name: emojiCategory.Name, + } +} |