2018-04-26 23:38:10 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require 'singleton'
|
|
|
|
|
|
|
|
class EntityCache
|
|
|
|
include Singleton
|
|
|
|
|
|
|
|
MAX_EXPIRATION = 7.days.freeze
|
2021-09-23 15:09:04 +00:00
|
|
|
MIN_EXPIRATION = 60.seconds.freeze
|
2018-04-26 23:38:10 +00:00
|
|
|
|
2020-04-05 10:51:22 +00:00
|
|
|
def status(url)
|
|
|
|
Rails.cache.fetch(to_key(:status, url), expires_in: MAX_EXPIRATION) { FetchRemoteStatusService.new.call(url) }
|
|
|
|
end
|
|
|
|
|
2018-04-26 23:38:10 +00:00
|
|
|
def mention(username, domain)
|
2020-07-12 19:32:45 +00:00
|
|
|
Rails.cache.fetch(to_key(:mention, username, domain), expires_in: MAX_EXPIRATION) { Account.select(:id, :username, :domain, :url, :actor_type).find_remote(username, domain) }
|
2018-04-26 23:38:10 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def emoji(shortcodes, domain)
|
2021-03-17 23:41:32 +00:00
|
|
|
shortcodes = Array(shortcodes)
|
|
|
|
return [] if shortcodes.empty?
|
|
|
|
|
2018-04-26 23:38:10 +00:00
|
|
|
cached = Rails.cache.read_multi(*shortcodes.map { |shortcode| to_key(:emoji, shortcode, domain) })
|
|
|
|
uncached_ids = []
|
|
|
|
|
|
|
|
shortcodes.each do |shortcode|
|
|
|
|
uncached_ids << shortcode unless cached.key?(to_key(:emoji, shortcode, domain))
|
|
|
|
end
|
|
|
|
|
|
|
|
unless uncached_ids.empty?
|
2021-01-12 08:27:38 +00:00
|
|
|
uncached = CustomEmoji.where(shortcode: shortcodes, domain: domain, disabled: false).index_by(&:shortcode)
|
2018-04-26 23:38:10 +00:00
|
|
|
uncached.each_value { |item| Rails.cache.write(to_key(:emoji, item.shortcode, domain), item, expires_in: MAX_EXPIRATION) }
|
|
|
|
end
|
|
|
|
|
2021-01-09 23:32:01 +00:00
|
|
|
shortcodes.filter_map { |shortcode| cached[to_key(:emoji, shortcode, domain)] || uncached[shortcode] }
|
2018-04-26 23:38:10 +00:00
|
|
|
end
|
|
|
|
|
2021-11-14 08:59:38 +00:00
|
|
|
def holding_account(url)
|
|
|
|
return Rails.cache.read(to_key(:holding_account, url)) if Rails.cache.exist?(to_key(:holding_account, url))
|
|
|
|
|
|
|
|
account = begin
|
|
|
|
if ActivityPub::TagManager.instance.local_uri?(url)
|
|
|
|
recognized_params = Rails.application.routes.recognize_path(url)
|
|
|
|
|
|
|
|
return nil unless recognized_params[:action] == 'show'
|
|
|
|
|
|
|
|
if recognized_params[:controller] == 'accounts'
|
|
|
|
Account.find_local(recognized_params[:username])
|
|
|
|
end
|
|
|
|
else
|
|
|
|
Account.where(uri: url).or(Account.where(url: url)).first
|
|
|
|
end
|
|
|
|
rescue ActiveRecord::RecordNotFound
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
|
|
|
|
Rails.cache.write(to_key(:holding_account, url), account, expires_in: account.nil? ? MIN_EXPIRATION : MAX_EXPIRATION)
|
|
|
|
|
|
|
|
account
|
|
|
|
end
|
|
|
|
|
2022-03-17 14:25:16 +00:00
|
|
|
def holding_status(url)
|
2021-09-23 15:09:04 +00:00
|
|
|
return Rails.cache.read(to_key(:holding_status, url)) if Rails.cache.exist?(to_key(:holding_status, url))
|
|
|
|
|
|
|
|
status = begin
|
|
|
|
if ActivityPub::TagManager.instance.local_uri?(url)
|
|
|
|
StatusFinder.new(url).status
|
|
|
|
else
|
|
|
|
Status.where(uri: url).or(Status.where(url: url)).first
|
|
|
|
end
|
|
|
|
rescue ActiveRecord::RecordNotFound
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
|
2022-03-17 14:25:16 +00:00
|
|
|
update_holding_status(url, status)
|
2021-09-23 15:09:04 +00:00
|
|
|
|
2022-03-17 14:25:16 +00:00
|
|
|
status
|
|
|
|
end
|
2021-09-23 15:09:04 +00:00
|
|
|
|
2022-03-17 14:25:16 +00:00
|
|
|
def update_holding_status(url, status)
|
|
|
|
Rails.cache.write(to_key(:holding_status, url), status, expires_in: status&.account.nil? ? MIN_EXPIRATION : MAX_EXPIRATION)
|
2021-09-23 15:09:04 +00:00
|
|
|
end
|
|
|
|
|
2018-04-26 23:38:10 +00:00
|
|
|
def to_key(type, *ids)
|
|
|
|
"#{type}:#{ids.compact.map(&:downcase).join(':')}"
|
|
|
|
end
|
|
|
|
end
|