Compare commits

...

3 Commits

Author SHA1 Message Date
HolgerHuo
983810813f
Merge 6eeb9215b6 into 94bceb8683 2025-07-11 14:36:39 +01:00
HolgerHuo
6eeb9215b6
add: entity_cache_spec.rb 2024-11-23 08:47:28 +08:00
Holger Huo
07e6df0d40
fix: custom emoji shortcode case not respected 2024-11-23 08:45:12 +08:00
2 changed files with 18 additions and 4 deletions

View File

@ -19,24 +19,30 @@ class EntityCache
shortcodes = Array(shortcodes)
return [] if shortcodes.empty?
cached = Rails.cache.read_multi(*shortcodes.map { |shortcode| to_key(:emoji, shortcode, domain) })
domain = domain.downcase if domain
cached = Rails.cache.read_multi(*shortcodes.map { |shortcode| to_emoji(:emoji, shortcode, domain) })
uncached_ids = []
shortcodes.each do |shortcode|
uncached_ids << shortcode unless cached.key?(to_key(:emoji, shortcode, domain))
uncached_ids << shortcode unless cached.key?(to_emoji(:emoji, shortcode, domain))
end
if uncached_ids.empty?
uncached = {}
else
uncached = CustomEmoji.enabled.where(shortcode: shortcodes, domain: domain).index_by(&:shortcode)
uncached.each_value { |item| Rails.cache.write(to_key(:emoji, item.shortcode, domain), item, expires_in: MAX_EXPIRATION) }
uncached.each_value { |item| Rails.cache.write(to_emoji(:emoji, item.shortcode, domain), item, expires_in: MAX_EXPIRATION) }
end
shortcodes.filter_map { |shortcode| cached[to_key(:emoji, shortcode, domain)] || uncached[shortcode] }
shortcodes.filter_map { |shortcode| cached[to_emoji(:emoji, shortcode, domain)] || uncached[shortcode] }
end
def to_key(type, *ids)
"#{type}:#{ids.compact.map(&:downcase).join(':')}"
end
def to_emoji(type, *ids)
"#{type}:#{ids.compact.join(':')}"
end
end

View File

@ -18,4 +18,12 @@ RSpec.describe EntityCache do
end
end
end
describe '#to_emoji' do
context 'when input shortcode has cases' do
it 'returns emoji with cases preserved' do
expect(described_class.instance.to_emoji(:emoji, 'FooBar', 'example.org')).to eq 'emoji:FooBar:example.org'
end
end
end
end