The code powering m.abunchtell.com https://m.abunchtell.com
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

332 lines
9.3 KiB

  1. # frozen_string_literal: true
  2. class ActivityPub::Activity::Create < ActivityPub::Activity
  3. SUPPORTED_TYPES = %w(Note).freeze
  4. CONVERTED_TYPES = %w(Image Video Article).freeze
  5. def perform
  6. return if delete_arrived_first?(object_uri) || unsupported_object_type? || invalid_origin?(@object['id'])
  7. RedisLock.acquire(lock_options) do |lock|
  8. if lock.acquired?
  9. @status = find_existing_status
  10. process_status if @status.nil?
  11. else
  12. raise Mastodon::RaceConditionError
  13. end
  14. end
  15. @status
  16. end
  17. private
  18. def process_status
  19. @tags = []
  20. @mentions = []
  21. @params = {}
  22. process_status_params
  23. process_tags
  24. process_audience
  25. ApplicationRecord.transaction do
  26. @status = Status.create!(@params)
  27. attach_tags(@status)
  28. end
  29. resolve_thread(@status)
  30. distribute(@status)
  31. forward_for_reply if @status.public_visibility? || @status.unlisted_visibility?
  32. end
  33. def find_existing_status
  34. status = status_from_uri(object_uri)
  35. status ||= Status.find_by(uri: @object['atomUri']) if @object['atomUri'].present?
  36. status
  37. end
  38. def process_status_params
  39. @params = begin
  40. {
  41. uri: @object['id'],
  42. url: object_url || @object['id'],
  43. account: @account,
  44. text: text_from_content || '',
  45. language: detected_language,
  46. spoiler_text: text_from_summary || '',
  47. created_at: @object['published'],
  48. override_timestamps: @options[:override_timestamps],
  49. reply: @object['inReplyTo'].present?,
  50. sensitive: @object['sensitive'] || false,
  51. visibility: visibility_from_audience,
  52. thread: replied_to_status,
  53. conversation: conversation_from_uri(@object['conversation']),
  54. media_attachment_ids: process_attachments.take(4).map(&:id),
  55. }
  56. end
  57. end
  58. def process_audience
  59. (as_array(@object['to']) + as_array(@object['cc'])).uniq.each do |audience|
  60. next if audience == ActivityPub::TagManager::COLLECTIONS[:public]
  61. # Unlike with tags, there is no point in resolving accounts we don't already
  62. # know here, because silent mentions would only be used for local access
  63. # control anyway
  64. account = account_from_uri(audience)
  65. next if account.nil? || @mentions.any? { |mention| mention.account_id == account.id }
  66. @mentions << Mention.new(account: account, silent: true)
  67. # If there is at least one silent mention, then the status can be considered
  68. # as a limited-audience status, and not strictly a direct message
  69. next unless @params[:visibility] == :direct
  70. @params[:visibility] = :limited
  71. end
  72. end
  73. def attach_tags(status)
  74. @tags.each do |tag|
  75. status.tags << tag
  76. TrendingTags.record_use!(tag, status.account, status.created_at) if status.public_visibility?
  77. end
  78. @mentions.each do |mention|
  79. mention.status = status
  80. mention.save
  81. end
  82. end
  83. def process_tags
  84. return if @object['tag'].nil?
  85. as_array(@object['tag']).each do |tag|
  86. if equals_or_includes?(tag['type'], 'Hashtag')
  87. process_hashtag tag
  88. elsif equals_or_includes?(tag['type'], 'Mention')
  89. process_mention tag
  90. elsif equals_or_includes?(tag['type'], 'Emoji')
  91. process_emoji tag
  92. end
  93. end
  94. end
  95. def process_hashtag(tag)
  96. return if tag['name'].blank?
  97. hashtag = tag['name'].gsub(/\A#/, '').mb_chars.downcase
  98. hashtag = Tag.where(name: hashtag).first_or_create(name: hashtag)
  99. return if @tags.include?(hashtag)
  100. @tags << hashtag
  101. rescue ActiveRecord::RecordInvalid
  102. nil
  103. end
  104. def process_mention(tag)
  105. return if tag['href'].blank?
  106. account = account_from_uri(tag['href'])
  107. account = ::FetchRemoteAccountService.new.call(tag['href'], id: false) if account.nil?
  108. return if account.nil?
  109. @mentions << Mention.new(account: account, silent: false)
  110. end
  111. def process_emoji(tag)
  112. return if skip_download?
  113. return if tag['name'].blank? || tag['icon'].blank? || tag['icon']['url'].blank?
  114. shortcode = tag['name'].delete(':')
  115. image_url = tag['icon']['url']
  116. uri = tag['id']
  117. updated = tag['updated']
  118. emoji = CustomEmoji.find_by(shortcode: shortcode, domain: @account.domain)
  119. return unless emoji.nil? || image_url != emoji.image_remote_url || (updated && emoji.updated_at >= updated)
  120. emoji ||= CustomEmoji.new(domain: @account.domain, shortcode: shortcode, uri: uri)
  121. emoji.image_remote_url = image_url
  122. emoji.save
  123. end
  124. def process_attachments
  125. return [] if @object['attachment'].nil?
  126. media_attachments = []
  127. as_array(@object['attachment']).each do |attachment|
  128. next if attachment['url'].blank?
  129. href = Addressable::URI.parse(attachment['url']).normalize.to_s
  130. media_attachment = MediaAttachment.create(account: @account, remote_url: href, description: attachment['name'].presence, focus: attachment['focalPoint'])
  131. media_attachments << media_attachment
  132. next if unsupported_media_type?(attachment['mediaType']) || skip_download?
  133. media_attachment.file_remote_url = href
  134. media_attachment.save
  135. end
  136. media_attachments
  137. rescue Addressable::URI::InvalidURIError => e
  138. Rails.logger.debug e
  139. media_attachments
  140. end
  141. def resolve_thread(status)
  142. return unless status.reply? && status.thread.nil?
  143. ThreadResolveWorker.perform_async(status.id, in_reply_to_uri)
  144. end
  145. def conversation_from_uri(uri)
  146. return nil if uri.nil?
  147. return Conversation.find_by(id: OStatus::TagManager.instance.unique_tag_to_local_id(uri, 'Conversation')) if OStatus::TagManager.instance.local_id?(uri)
  148. Conversation.find_by(uri: uri) || Conversation.create(uri: uri)
  149. end
  150. def visibility_from_audience
  151. if equals_or_includes?(@object['to'], ActivityPub::TagManager::COLLECTIONS[:public])
  152. :public
  153. elsif equals_or_includes?(@object['cc'], ActivityPub::TagManager::COLLECTIONS[:public])
  154. :unlisted
  155. elsif equals_or_includes?(@object['to'], @account.followers_url)
  156. :private
  157. else
  158. :direct
  159. end
  160. end
  161. def audience_includes?(account)
  162. uri = ActivityPub::TagManager.instance.uri_for(account)
  163. equals_or_includes?(@object['to'], uri) || equals_or_includes?(@object['cc'], uri)
  164. end
  165. def replied_to_status
  166. return @replied_to_status if defined?(@replied_to_status)
  167. if in_reply_to_uri.blank?
  168. @replied_to_status = nil
  169. else
  170. @replied_to_status = status_from_uri(in_reply_to_uri)
  171. @replied_to_status ||= status_from_uri(@object['inReplyToAtomUri']) if @object['inReplyToAtomUri'].present?
  172. @replied_to_status
  173. end
  174. end
  175. def in_reply_to_uri
  176. value_or_id(@object['inReplyTo'])
  177. end
  178. def text_from_content
  179. return Formatter.instance.linkify([text_from_name, object_url || @object['id']].join(' ')) if converted_object_type?
  180. if @object['content'].present?
  181. @object['content']
  182. elsif content_language_map?
  183. @object['contentMap'].values.first
  184. end
  185. end
  186. def text_from_summary
  187. if @object['summary'].present?
  188. @object['summary']
  189. elsif summary_language_map?
  190. @object['summaryMap'].values.first
  191. end
  192. end
  193. def text_from_name
  194. if @object['name'].present?
  195. @object['name']
  196. elsif name_language_map?
  197. @object['nameMap'].values.first
  198. end
  199. end
  200. def detected_language
  201. if content_language_map?
  202. @object['contentMap'].keys.first
  203. elsif name_language_map?
  204. @object['nameMap'].keys.first
  205. elsif summary_language_map?
  206. @object['summaryMap'].keys.first
  207. elsif supported_object_type?
  208. LanguageDetector.instance.detect(text_from_content, @account)
  209. end
  210. end
  211. def object_url
  212. return if @object['url'].blank?
  213. url_candidate = url_to_href(@object['url'], 'text/html')
  214. if invalid_origin?(url_candidate)
  215. nil
  216. else
  217. url_candidate
  218. end
  219. end
  220. def summary_language_map?
  221. @object['summaryMap'].is_a?(Hash) && !@object['summaryMap'].empty?
  222. end
  223. def content_language_map?
  224. @object['contentMap'].is_a?(Hash) && !@object['contentMap'].empty?
  225. end
  226. def name_language_map?
  227. @object['nameMap'].is_a?(Hash) && !@object['nameMap'].empty?
  228. end
  229. def unsupported_object_type?
  230. @object.is_a?(String) || !(supported_object_type? || converted_object_type?)
  231. end
  232. def unsupported_media_type?(mime_type)
  233. mime_type.present? && !(MediaAttachment::IMAGE_MIME_TYPES + MediaAttachment::VIDEO_MIME_TYPES).include?(mime_type)
  234. end
  235. def supported_object_type?
  236. equals_or_includes_any?(@object['type'], SUPPORTED_TYPES)
  237. end
  238. def converted_object_type?
  239. equals_or_includes_any?(@object['type'], CONVERTED_TYPES)
  240. end
  241. def skip_download?
  242. return @skip_download if defined?(@skip_download)
  243. @skip_download ||= DomainBlock.find_by(domain: @account.domain)&.reject_media?
  244. end
  245. def invalid_origin?(url)
  246. return true if unsupported_uri_scheme?(url)
  247. needle = Addressable::URI.parse(url).host
  248. haystack = Addressable::URI.parse(@account.uri).host
  249. !haystack.casecmp(needle).zero?
  250. end
  251. def reply_to_local?
  252. !replied_to_status.nil? && replied_to_status.account.local?
  253. end
  254. def forward_for_reply
  255. return unless @json['signature'].present? && reply_to_local?
  256. ActivityPub::RawDistributionWorker.perform_async(Oj.dump(@json), replied_to_status.account_id, [@account.preferred_inbox_url])
  257. end
  258. def lock_options
  259. { redis: Redis.current, key: "create:#{@object['id']}" }
  260. end
  261. end