logo

mastofe

My custom branche(s) on git.pleroma.social/pleroma/mastofe git clone https://hacktivis.me/git/mastofe.git

create.rb (7696B)


  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. end
  12. end
  13. @status
  14. end
  15. private
  16. def process_status
  17. status_params = process_status_params
  18. ApplicationRecord.transaction do
  19. @status = Status.create!(status_params)
  20. process_tags(@status)
  21. end
  22. resolve_thread(@status)
  23. distribute(@status)
  24. forward_for_reply if @status.public_visibility? || @status.unlisted_visibility?
  25. end
  26. def find_existing_status
  27. status = status_from_uri(object_uri)
  28. status ||= Status.find_by(uri: @object['atomUri']) if @object['atomUri'].present?
  29. status
  30. end
  31. def process_status_params
  32. {
  33. uri: @object['id'],
  34. url: object_url || @object['id'],
  35. account: @account,
  36. text: text_from_content || '',
  37. language: detected_language,
  38. spoiler_text: @object['summary'] || '',
  39. created_at: @options[:override_timestamps] ? nil : @object['published'],
  40. reply: @object['inReplyTo'].present?,
  41. sensitive: @object['sensitive'] || false,
  42. visibility: visibility_from_audience,
  43. thread: replied_to_status,
  44. conversation: conversation_from_uri(@object['conversation']),
  45. media_attachment_ids: process_attachments.take(4).map(&:id),
  46. }
  47. end
  48. def process_tags(status)
  49. return if @object['tag'].nil?
  50. as_array(@object['tag']).each do |tag|
  51. case tag['type']
  52. when 'Hashtag'
  53. process_hashtag tag, status
  54. when 'Mention'
  55. process_mention tag, status
  56. when 'Emoji'
  57. process_emoji tag, status
  58. end
  59. end
  60. end
  61. def process_hashtag(tag, status)
  62. return if tag['name'].blank?
  63. hashtag = tag['name'].gsub(/\A#/, '').mb_chars.downcase
  64. hashtag = Tag.where(name: hashtag).first_or_initialize(name: hashtag)
  65. status.tags << hashtag
  66. rescue ActiveRecord::RecordInvalid
  67. nil
  68. end
  69. def process_mention(tag, status)
  70. return if tag['href'].blank?
  71. account = account_from_uri(tag['href'])
  72. account = FetchRemoteAccountService.new.call(tag['href'], id: false) if account.nil?
  73. return if account.nil?
  74. account.mentions.create(status: status)
  75. end
  76. def process_emoji(tag, _status)
  77. return if skip_download?
  78. return if tag['name'].blank? || tag['icon'].blank? || tag['icon']['url'].blank?
  79. shortcode = tag['name'].delete(':')
  80. image_url = tag['icon']['url']
  81. uri = tag['id']
  82. updated = tag['updated']
  83. emoji = CustomEmoji.find_by(shortcode: shortcode, domain: @account.domain)
  84. return unless emoji.nil? || emoji.updated_at >= updated
  85. emoji ||= CustomEmoji.new(domain: @account.domain, shortcode: shortcode, uri: uri)
  86. emoji.image_remote_url = image_url
  87. emoji.save
  88. end
  89. def process_attachments
  90. return [] if @object['attachment'].nil?
  91. media_attachments = []
  92. as_array(@object['attachment']).each do |attachment|
  93. next if attachment['url'].blank?
  94. href = Addressable::URI.parse(attachment['url']).normalize.to_s
  95. media_attachment = MediaAttachment.create(account: @account, remote_url: href, description: attachment['name'].presence, focus: attachment['focalPoint'])
  96. media_attachments << media_attachment
  97. next if unsupported_media_type?(attachment['mediaType']) || skip_download?
  98. media_attachment.file_remote_url = href
  99. media_attachment.save
  100. end
  101. media_attachments
  102. rescue Addressable::URI::InvalidURIError => e
  103. Rails.logger.debug e
  104. media_attachments
  105. end
  106. def resolve_thread(status)
  107. return unless status.reply? && status.thread.nil?
  108. ThreadResolveWorker.perform_async(status.id, in_reply_to_uri)
  109. end
  110. def conversation_from_uri(uri)
  111. return nil if uri.nil?
  112. return Conversation.find_by(id: OStatus::TagManager.instance.unique_tag_to_local_id(uri, 'Conversation')) if OStatus::TagManager.instance.local_id?(uri)
  113. Conversation.find_by(uri: uri) || Conversation.create(uri: uri)
  114. end
  115. def visibility_from_audience
  116. if equals_or_includes?(@object['to'], ActivityPub::TagManager::COLLECTIONS[:public])
  117. :public
  118. elsif equals_or_includes?(@object['cc'], ActivityPub::TagManager::COLLECTIONS[:public])
  119. :unlisted
  120. elsif equals_or_includes?(@object['to'], @account.followers_url)
  121. :private
  122. else
  123. :direct
  124. end
  125. end
  126. def audience_includes?(account)
  127. uri = ActivityPub::TagManager.instance.uri_for(account)
  128. equals_or_includes?(@object['to'], uri) || equals_or_includes?(@object['cc'], uri)
  129. end
  130. def replied_to_status
  131. return @replied_to_status if defined?(@replied_to_status)
  132. if in_reply_to_uri.blank?
  133. @replied_to_status = nil
  134. else
  135. @replied_to_status = status_from_uri(in_reply_to_uri)
  136. @replied_to_status ||= status_from_uri(@object['inReplyToAtomUri']) if @object['inReplyToAtomUri'].present?
  137. @replied_to_status
  138. end
  139. end
  140. def in_reply_to_uri
  141. value_or_id(@object['inReplyTo'])
  142. end
  143. def text_from_content
  144. return Formatter.instance.linkify([text_from_name, object_url || @object['id']].join(' ')) if converted_object_type?
  145. if @object['content'].present?
  146. @object['content']
  147. elsif content_language_map?
  148. @object['contentMap'].values.first
  149. end
  150. end
  151. def text_from_name
  152. if @object['name'].present?
  153. @object['name']
  154. elsif name_language_map?
  155. @object['nameMap'].values.first
  156. end
  157. end
  158. def detected_language
  159. if content_language_map?
  160. @object['contentMap'].keys.first
  161. elsif name_language_map?
  162. @object['nameMap'].keys.first
  163. elsif supported_object_type?
  164. LanguageDetector.instance.detect(text_from_content, @account)
  165. end
  166. end
  167. def object_url
  168. return if @object['url'].blank?
  169. url_candidate = url_to_href(@object['url'], 'text/html')
  170. if invalid_origin?(url_candidate)
  171. nil
  172. else
  173. url_candidate
  174. end
  175. end
  176. def content_language_map?
  177. @object['contentMap'].is_a?(Hash) && !@object['contentMap'].empty?
  178. end
  179. def name_language_map?
  180. @object['nameMap'].is_a?(Hash) && !@object['nameMap'].empty?
  181. end
  182. def unsupported_object_type?
  183. @object.is_a?(String) || !(supported_object_type? || converted_object_type?)
  184. end
  185. def unsupported_media_type?(mime_type)
  186. mime_type.present? && !(MediaAttachment::IMAGE_MIME_TYPES + MediaAttachment::VIDEO_MIME_TYPES).include?(mime_type)
  187. end
  188. def supported_object_type?
  189. SUPPORTED_TYPES.include?(@object['type'])
  190. end
  191. def converted_object_type?
  192. CONVERTED_TYPES.include?(@object['type'])
  193. end
  194. def skip_download?
  195. return @skip_download if defined?(@skip_download)
  196. @skip_download ||= DomainBlock.find_by(domain: @account.domain)&.reject_media?
  197. end
  198. def invalid_origin?(url)
  199. return true if unsupported_uri_scheme?(url)
  200. needle = Addressable::URI.parse(url).host
  201. haystack = Addressable::URI.parse(@account.uri).host
  202. !haystack.casecmp(needle).zero?
  203. end
  204. def reply_to_local?
  205. !replied_to_status.nil? && replied_to_status.account.local?
  206. end
  207. def forward_for_reply
  208. return unless @json['signature'].present? && reply_to_local?
  209. ActivityPub::RawDistributionWorker.perform_async(Oj.dump(@json), replied_to_status.account_id, [@account.preferred_inbox_url])
  210. end
  211. def lock_options
  212. { redis: Redis.current, key: "create:#{@object['id']}" }
  213. end
  214. end