mirror of
https://github.com/discourse/discourse.git
synced 2026-08-11 02:59:07 +08:00
Followup bd3906ce64
Converts the new endpoints made in the original PR into
service classes, using AI to drive implementation based
on the service authoring skill:
* CustomEmoji::Export
* CustomEmoji::PreviewImport
* CustomEmoji::ConfirmImport
In addition, some supporting action, lib, and serializer
classes have been added. This fixed a couple of
bugs as well:
* Import names now go through `Emoji.sanitize_emoji_name` like
single-emoji create
* No-group imports no longer persist a literal "default" string
* Two 500s (suspicious zip entries, malformed CSV) became proper 422s
* Duplicate CSV names are flagged at preview instead of exploding the
confirm transaction
*` Emoji.clear_cache `moved outside the transaction
32 lines
624 B
Ruby
Vendored
32 lines
624 B
Ruby
Vendored
# frozen_string_literal: true
|
|
|
|
class CustomEmoji::ImportPreviewCache
|
|
TTL = 2.hours
|
|
KEY_NAMESPACE = "emoji_import_preview"
|
|
|
|
def initialize(user)
|
|
@user = user
|
|
end
|
|
|
|
def store(rows)
|
|
token = SecureRandom.hex
|
|
Discourse.redis.setex(key(token), TTL.to_i, rows.map(&:to_h).to_json)
|
|
token
|
|
end
|
|
|
|
def fetch(token)
|
|
payload = Discourse.redis.get(key(token))
|
|
return if payload.blank?
|
|
JSON.parse(payload).map { CustomEmoji::ImportRow.from_h(it) }
|
|
end
|
|
|
|
def delete(token)
|
|
Discourse.redis.del(key(token))
|
|
end
|
|
|
|
private
|
|
|
|
def key(token)
|
|
"#{KEY_NAMESPACE}:#{@user.id}:#{token}"
|
|
end
|
|
end
|