2022-01-19 22:37:27 +01:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
class ActivityPub::ProcessStatusUpdateService < BaseService
|
|
|
|
include JsonLdHelper
|
2022-04-28 17:47:34 +02:00
|
|
|
include Redisable
|
2022-05-13 00:02:35 +02:00
|
|
|
include Lockable
|
2022-01-19 22:37:27 +01:00
|
|
|
|
2023-09-15 19:54:32 +02:00
|
|
|
def call(status, activity_json, object_json, request_id: nil)
|
2022-03-26 00:38:44 +01:00
|
|
|
raise ArgumentError, 'Status has unsaved changes' if status.changed?
|
|
|
|
|
2023-09-15 19:54:32 +02:00
|
|
|
@activity_json = activity_json
|
|
|
|
@json = object_json
|
2022-01-19 22:37:27 +01:00
|
|
|
@status_parser = ActivityPub::Parser::StatusParser.new(@json)
|
|
|
|
@uri = @status_parser.uri
|
|
|
|
@status = status
|
|
|
|
@account = status.account
|
|
|
|
@media_attachments_changed = false
|
2022-01-26 18:05:39 +01:00
|
|
|
@poll_changed = false
|
2022-12-07 00:15:24 +01:00
|
|
|
@request_id = request_id
|
2022-01-19 22:37:27 +01:00
|
|
|
|
|
|
|
# Only native types can be updated at the moment
|
2022-03-12 19:33:10 +01:00
|
|
|
return @status if !expected_type? || already_updated_more_recently?
|
2022-01-19 22:37:27 +01:00
|
|
|
|
2022-04-06 21:01:02 +02:00
|
|
|
if @status_parser.edited_at.present? && (@status.edited_at.nil? || @status_parser.edited_at > @status.edited_at)
|
|
|
|
handle_explicit_update!
|
|
|
|
else
|
|
|
|
handle_implicit_update!
|
|
|
|
end
|
|
|
|
|
|
|
|
@status
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
2022-03-12 09:30:31 +01:00
|
|
|
|
2022-04-06 21:01:02 +02:00
|
|
|
def handle_explicit_update!
|
|
|
|
last_edit_date = @status.edited_at.presence || @status.created_at
|
2022-03-26 00:38:44 +01:00
|
|
|
|
2022-01-19 22:37:27 +01:00
|
|
|
# Only allow processing one create/update per status at a time
|
2023-05-02 18:16:07 +02:00
|
|
|
with_redis_lock("create:#{@uri}") do
|
2022-05-13 00:02:35 +02:00
|
|
|
Status.transaction do
|
|
|
|
record_previous_edit!
|
|
|
|
update_media_attachments!
|
|
|
|
update_poll!
|
|
|
|
update_immediate_attributes!
|
|
|
|
update_metadata!
|
2024-10-28 05:55:18 +01:00
|
|
|
update_counts!
|
2022-05-13 00:02:35 +02:00
|
|
|
create_edits!
|
|
|
|
end
|
2022-01-19 22:37:27 +01:00
|
|
|
|
2022-12-15 18:09:48 +01:00
|
|
|
download_media_files!
|
2022-05-13 00:02:35 +02:00
|
|
|
queue_poll_notifications!
|
2022-01-26 18:05:39 +01:00
|
|
|
|
2022-05-13 00:02:35 +02:00
|
|
|
next unless significant_changes?
|
2022-01-26 18:05:39 +01:00
|
|
|
|
2022-05-13 00:02:35 +02:00
|
|
|
reset_preview_card!
|
|
|
|
broadcast_updates!
|
2022-01-19 22:37:27 +01:00
|
|
|
end
|
2022-03-12 09:30:31 +01:00
|
|
|
|
2022-04-06 21:01:02 +02:00
|
|
|
forward_activity! if significant_changes? && @status_parser.edited_at > last_edit_date
|
2022-01-19 22:37:27 +01:00
|
|
|
end
|
|
|
|
|
2022-04-06 21:01:02 +02:00
|
|
|
def handle_implicit_update!
|
2023-05-02 18:16:07 +02:00
|
|
|
with_redis_lock("create:#{@uri}") do
|
2022-05-13 00:02:35 +02:00
|
|
|
update_poll!(allow_significant_changes: false)
|
|
|
|
queue_poll_notifications!
|
2024-10-28 05:55:18 +01:00
|
|
|
update_counts!
|
2022-04-06 21:01:02 +02:00
|
|
|
end
|
|
|
|
end
|
2022-01-19 22:37:27 +01:00
|
|
|
|
|
|
|
def update_media_attachments!
|
2022-03-12 19:33:10 +01:00
|
|
|
previous_media_attachments = @status.media_attachments.to_a
|
|
|
|
previous_media_attachments_ids = @status.ordered_media_attachment_ids || previous_media_attachments.map(&:id)
|
2022-12-15 18:09:48 +01:00
|
|
|
@next_media_attachments = []
|
2022-01-19 22:37:27 +01:00
|
|
|
|
|
|
|
as_array(@json['attachment']).each do |attachment|
|
|
|
|
media_attachment_parser = ActivityPub::Parser::MediaAttachmentParser.new(attachment)
|
|
|
|
|
2024-05-27 11:49:44 +02:00
|
|
|
next if media_attachment_parser.remote_url.blank? || @next_media_attachments.size > Status::MEDIA_ATTACHMENTS_LIMIT
|
2022-01-19 22:37:27 +01:00
|
|
|
|
|
|
|
begin
|
|
|
|
media_attachment = previous_media_attachments.find { |previous_media_attachment| previous_media_attachment.remote_url == media_attachment_parser.remote_url }
|
|
|
|
media_attachment ||= MediaAttachment.new(account: @account, remote_url: media_attachment_parser.remote_url)
|
|
|
|
|
|
|
|
# If a previously existing media attachment was significantly updated, mark
|
|
|
|
# media attachments as changed even if none were added or removed
|
2023-02-18 12:37:47 +01:00
|
|
|
@media_attachments_changed = true if media_attachment_parser.significantly_changes?(media_attachment)
|
2022-01-19 22:37:27 +01:00
|
|
|
|
|
|
|
media_attachment.description = media_attachment_parser.description
|
|
|
|
media_attachment.focus = media_attachment_parser.focus
|
|
|
|
media_attachment.thumbnail_remote_url = media_attachment_parser.thumbnail_remote_url
|
|
|
|
media_attachment.blurhash = media_attachment_parser.blurhash
|
2022-12-15 18:09:48 +01:00
|
|
|
media_attachment.status_id = @status.id
|
|
|
|
media_attachment.skip_download = unsupported_media_type?(media_attachment_parser.file_content_type) || skip_download?
|
2022-01-19 22:37:27 +01:00
|
|
|
media_attachment.save!
|
|
|
|
|
2022-12-15 18:09:48 +01:00
|
|
|
@next_media_attachments << media_attachment
|
2022-01-19 22:37:27 +01:00
|
|
|
rescue Addressable::URI::InvalidURIError => e
|
2023-02-07 03:44:36 +01:00
|
|
|
Rails.logger.debug { "Invalid URL in attachment: #{e}" }
|
2022-01-19 22:37:27 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-12-15 18:09:48 +01:00
|
|
|
@status.ordered_media_attachment_ids = @next_media_attachments.map(&:id)
|
2022-03-09 09:06:17 +01:00
|
|
|
|
2022-03-12 19:33:10 +01:00
|
|
|
@media_attachments_changed = true if @status.ordered_media_attachment_ids != previous_media_attachments_ids
|
2022-01-19 22:37:27 +01:00
|
|
|
end
|
|
|
|
|
2022-12-15 18:09:48 +01:00
|
|
|
def download_media_files!
|
|
|
|
@next_media_attachments.each do |media_attachment|
|
|
|
|
next if media_attachment.skip_download
|
|
|
|
|
|
|
|
media_attachment.download_file! if media_attachment.remote_url_previously_changed?
|
|
|
|
media_attachment.download_thumbnail! if media_attachment.thumbnail_remote_url_previously_changed?
|
|
|
|
media_attachment.save
|
2024-10-08 16:59:51 +02:00
|
|
|
rescue Mastodon::UnexpectedResponseError, *Mastodon::HTTP_CONNECTION_ERRORS
|
2022-12-15 18:09:48 +01:00
|
|
|
RedownloadMediaWorker.perform_in(rand(30..600).seconds, media_attachment.id)
|
|
|
|
rescue Seahorse::Client::NetworkingError => e
|
|
|
|
Rails.logger.warn "Error storing media attachment: #{e}"
|
|
|
|
end
|
|
|
|
|
|
|
|
@status.media_attachments.reload
|
|
|
|
end
|
|
|
|
|
2022-04-06 21:01:02 +02:00
|
|
|
def update_poll!(allow_significant_changes: true)
|
2022-01-19 22:37:27 +01:00
|
|
|
previous_poll = @status.preloadable_poll
|
|
|
|
@previous_expires_at = previous_poll&.expires_at
|
|
|
|
poll_parser = ActivityPub::Parser::PollParser.new(@json)
|
|
|
|
|
|
|
|
if poll_parser.valid?
|
|
|
|
poll = previous_poll || @account.polls.new(status: @status)
|
|
|
|
|
|
|
|
# If for some reasons the options were changed, it invalidates all previous
|
|
|
|
# votes, so we need to remove them
|
2022-02-10 14:26:54 +01:00
|
|
|
@poll_changed = true if poll_parser.significantly_changes?(poll)
|
2022-04-06 21:01:02 +02:00
|
|
|
return if @poll_changed && !allow_significant_changes
|
2022-01-19 22:37:27 +01:00
|
|
|
|
|
|
|
poll.last_fetched_at = Time.now.utc
|
|
|
|
poll.options = poll_parser.options
|
|
|
|
poll.multiple = poll_parser.multiple
|
|
|
|
poll.expires_at = poll_parser.expires_at
|
|
|
|
poll.voters_count = poll_parser.voters_count
|
|
|
|
poll.cached_tallies = poll_parser.cached_tallies
|
2022-02-10 14:26:54 +01:00
|
|
|
poll.reset_votes! if @poll_changed
|
2022-01-19 22:37:27 +01:00
|
|
|
poll.save!
|
|
|
|
|
|
|
|
@status.poll_id = poll.id
|
|
|
|
elsif previous_poll.present?
|
2022-04-06 21:01:02 +02:00
|
|
|
return unless allow_significant_changes
|
|
|
|
|
2022-01-19 22:37:27 +01:00
|
|
|
previous_poll.destroy!
|
2022-01-26 18:05:39 +01:00
|
|
|
@poll_changed = true
|
2022-01-19 22:37:27 +01:00
|
|
|
@status.poll_id = nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def update_immediate_attributes!
|
|
|
|
@status.text = @status_parser.text || ''
|
|
|
|
@status.spoiler_text = @status_parser.spoiler_text || ''
|
|
|
|
@status.sensitive = @account.sensitized? || @status_parser.sensitive || false
|
2022-02-08 02:41:17 +01:00
|
|
|
@status.language = @status_parser.language
|
2022-04-06 21:01:02 +02:00
|
|
|
|
|
|
|
@significant_changes = text_significantly_changed? || @status.spoiler_text_changed? || @media_attachments_changed || @poll_changed
|
|
|
|
|
|
|
|
@status.edited_at = @status_parser.edited_at if significant_changes?
|
2022-01-19 22:37:27 +01:00
|
|
|
|
|
|
|
@status.save!
|
|
|
|
end
|
|
|
|
|
|
|
|
def update_metadata!
|
|
|
|
@raw_tags = []
|
|
|
|
@raw_mentions = []
|
|
|
|
@raw_emojis = []
|
|
|
|
|
|
|
|
as_array(@json['tag']).each do |tag|
|
|
|
|
if equals_or_includes?(tag['type'], 'Hashtag')
|
2023-12-05 14:59:15 +01:00
|
|
|
@raw_tags << tag['name'] if tag['name'].present?
|
2022-01-19 22:37:27 +01:00
|
|
|
elsif equals_or_includes?(tag['type'], 'Mention')
|
2023-12-05 14:59:15 +01:00
|
|
|
@raw_mentions << tag['href'] if tag['href'].present?
|
2022-01-19 22:37:27 +01:00
|
|
|
elsif equals_or_includes?(tag['type'], 'Emoji')
|
|
|
|
@raw_emojis << tag
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
update_tags!
|
|
|
|
update_mentions!
|
|
|
|
update_emojis!
|
|
|
|
end
|
|
|
|
|
|
|
|
def update_tags!
|
|
|
|
@status.tags = Tag.find_or_create_by_names(@raw_tags)
|
|
|
|
end
|
|
|
|
|
|
|
|
def update_mentions!
|
2024-12-02 10:24:34 +01:00
|
|
|
unresolved_mentions = []
|
2022-01-19 22:37:27 +01:00
|
|
|
|
2024-12-11 13:59:29 +01:00
|
|
|
currently_mentioned_account_ids = @raw_mentions.filter_map do |href|
|
2022-01-19 22:37:27 +01:00
|
|
|
next if href.blank?
|
|
|
|
|
|
|
|
account = ActivityPub::TagManager.instance.uri_to_resource(href, Account)
|
2022-12-07 00:15:24 +01:00
|
|
|
account ||= ActivityPub::FetchRemoteAccountService.new.call(href, request_id: @request_id)
|
2022-01-19 22:37:27 +01:00
|
|
|
|
2024-12-11 13:59:29 +01:00
|
|
|
account&.id
|
2024-12-02 10:24:34 +01:00
|
|
|
rescue Mastodon::UnexpectedResponseError, *Mastodon::HTTP_CONNECTION_ERRORS
|
|
|
|
# Since previous mentions are about already-known accounts,
|
|
|
|
# they don't try to resolve again and won't fall into this case.
|
|
|
|
# In other words, this failure case is only for new mentions and won't
|
|
|
|
# affect `removed_mentions` so they can safely be retried asynchronously
|
|
|
|
unresolved_mentions << href
|
2024-12-11 13:59:29 +01:00
|
|
|
nil
|
2022-01-19 22:37:27 +01:00
|
|
|
end
|
|
|
|
|
2024-12-11 13:59:29 +01:00
|
|
|
@status.mentions.upsert_all(currently_mentioned_account_ids.map { |id| { account_id: id, silent: false } }, unique_by: %w(status_id account_id))
|
2022-01-19 22:37:27 +01:00
|
|
|
|
|
|
|
# If previous mentions are no longer contained in the text, convert them
|
|
|
|
# to silent mentions, since withdrawing access from someone who already
|
|
|
|
# received a notification might be more confusing
|
2024-12-11 13:59:29 +01:00
|
|
|
@status.mentions.where.not(account_id: currently_mentioned_account_ids).update_all(silent: true)
|
2024-12-02 10:24:34 +01:00
|
|
|
|
|
|
|
# Queue unresolved mentions for later
|
|
|
|
unresolved_mentions.uniq.each do |uri|
|
|
|
|
MentionResolveWorker.perform_in(rand(30...600).seconds, @status.id, uri, { 'request_id' => @request_id })
|
|
|
|
end
|
2022-01-19 22:37:27 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def update_emojis!
|
|
|
|
return if skip_download?
|
|
|
|
|
|
|
|
@raw_emojis.each do |raw_emoji|
|
|
|
|
custom_emoji_parser = ActivityPub::Parser::CustomEmojiParser.new(raw_emoji)
|
|
|
|
|
|
|
|
next if custom_emoji_parser.shortcode.blank? || custom_emoji_parser.image_remote_url.blank?
|
|
|
|
|
|
|
|
emoji = CustomEmoji.find_by(shortcode: custom_emoji_parser.shortcode, domain: @account.domain)
|
|
|
|
|
|
|
|
next unless emoji.nil? || custom_emoji_parser.image_remote_url != emoji.image_remote_url || (custom_emoji_parser.updated_at && custom_emoji_parser.updated_at >= emoji.updated_at)
|
|
|
|
|
|
|
|
begin
|
|
|
|
emoji ||= CustomEmoji.new(domain: @account.domain, shortcode: custom_emoji_parser.shortcode, uri: custom_emoji_parser.uri)
|
|
|
|
emoji.image_remote_url = custom_emoji_parser.image_remote_url
|
|
|
|
emoji.save
|
|
|
|
rescue Seahorse::Client::NetworkingError => e
|
|
|
|
Rails.logger.warn "Error storing emoji: #{e}"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2024-10-28 05:55:18 +01:00
|
|
|
def update_counts!
|
|
|
|
likes = @status_parser.favourites_count
|
|
|
|
shares = @status_parser.reblogs_count
|
|
|
|
return if likes.nil? && shares.nil?
|
|
|
|
|
|
|
|
@status.status_stat.tap do |status_stat|
|
|
|
|
status_stat.untrusted_reblogs_count = shares unless shares.nil?
|
|
|
|
status_stat.untrusted_favourites_count = likes unless likes.nil?
|
|
|
|
|
|
|
|
status_stat.save if status_stat.changed?
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-01-19 22:37:27 +01:00
|
|
|
def expected_type?
|
|
|
|
equals_or_includes_any?(@json['type'], %w(Note Question))
|
|
|
|
end
|
|
|
|
|
2022-03-26 00:38:44 +01:00
|
|
|
def record_previous_edit!
|
|
|
|
@previous_edit = @status.build_snapshot(at_time: @status.created_at, rate_limit: false) if @status.edits.empty?
|
2022-01-19 22:37:27 +01:00
|
|
|
end
|
|
|
|
|
2022-03-26 00:38:44 +01:00
|
|
|
def create_edits!
|
2022-01-26 18:05:39 +01:00
|
|
|
return unless significant_changes?
|
2022-01-19 22:37:27 +01:00
|
|
|
|
2022-03-26 00:38:44 +01:00
|
|
|
@previous_edit&.save!
|
2022-03-09 20:06:51 +01:00
|
|
|
@status.snapshot!(account_id: @account.id, rate_limit: false)
|
2022-01-19 22:37:27 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def skip_download?
|
|
|
|
return @skip_download if defined?(@skip_download)
|
|
|
|
|
|
|
|
@skip_download ||= DomainBlock.reject_media?(@account.domain)
|
|
|
|
end
|
|
|
|
|
|
|
|
def unsupported_media_type?(mime_type)
|
|
|
|
mime_type.present? && !MediaAttachment.supported_mime_types.include?(mime_type)
|
|
|
|
end
|
|
|
|
|
2022-01-26 18:05:39 +01:00
|
|
|
def significant_changes?
|
2022-04-06 21:01:02 +02:00
|
|
|
@significant_changes
|
|
|
|
end
|
|
|
|
|
|
|
|
def text_significantly_changed?
|
|
|
|
return false unless @status.text_changed?
|
|
|
|
|
|
|
|
old, new = @status.text_change
|
|
|
|
HtmlAwareFormatter.new(old, false).to_s != HtmlAwareFormatter.new(new, false).to_s
|
2022-01-26 18:05:39 +01:00
|
|
|
end
|
|
|
|
|
2022-01-19 22:37:27 +01:00
|
|
|
def already_updated_more_recently?
|
|
|
|
@status.edited_at.present? && @status_parser.edited_at.present? && @status.edited_at > @status_parser.edited_at
|
|
|
|
end
|
|
|
|
|
|
|
|
def reset_preview_card!
|
2023-11-13 10:58:28 +01:00
|
|
|
@status.reset_preview_card!
|
2022-01-26 18:05:39 +01:00
|
|
|
LinkCrawlWorker.perform_in(rand(1..59).seconds, @status.id)
|
2022-01-19 22:37:27 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def broadcast_updates!
|
2022-01-28 00:43:56 +01:00
|
|
|
::DistributionWorker.perform_async(@status.id, { 'update' => true })
|
2022-01-19 22:37:27 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def queue_poll_notifications!
|
|
|
|
poll = @status.preloadable_poll
|
|
|
|
|
|
|
|
# If the poll had no expiration date set but now has, or now has a sooner
|
|
|
|
# expiration date, and people have voted, schedule a notification
|
|
|
|
|
|
|
|
return unless poll.present? && poll.expires_at.present? && poll.votes.exists?
|
|
|
|
|
|
|
|
PollExpirationNotifyWorker.remove_from_scheduled(poll.id) if @previous_expires_at.present? && @previous_expires_at > poll.expires_at
|
|
|
|
PollExpirationNotifyWorker.perform_at(poll.expires_at + 5.minutes, poll.id)
|
|
|
|
end
|
2022-03-12 09:30:31 +01:00
|
|
|
|
|
|
|
def forward_activity!
|
|
|
|
forwarder.forward! if forwarder.forwardable?
|
|
|
|
end
|
|
|
|
|
|
|
|
def forwarder
|
2023-09-15 19:54:32 +02:00
|
|
|
@forwarder ||= ActivityPub::Forwarder.new(@account, @activity_json, @status)
|
2022-03-12 09:30:31 +01:00
|
|
|
end
|
2022-01-19 22:37:27 +01:00
|
|
|
end
|