diff options
author | Claire <claire.github-309c@sitedethib.com> | 2022-05-16 09:42:32 +0200 |
---|---|---|
committer | Claire <claire.github-309c@sitedethib.com> | 2022-05-16 09:42:32 +0200 |
commit | 040b7d37a460e5f023f7654b1b619368cbbc24ea (patch) | |
tree | 251a14cd362a6d98c1721e8f3f54fc386165e464 /app/services | |
parent | 94e98864e39c010635e839fea984f2b4893bef1a (diff) | |
parent | c3fac61f56b3ad63534961f3d3c426cdf8ac6213 (diff) |
Merge branch 'main' into glitch-soc/merge-upstream
Conflicts: - `app/services/remove_status_service.rb`: Conflict due to glitch-soc having extra code for a proper direct visibility timeline, in a part of the code upstream refactored. Restored glitch-soc's extra code in the refactored bit.
Diffstat (limited to 'app/services')
-rw-r--r-- | app/services/activitypub/process_account_service.rb | 33 | ||||
-rw-r--r-- | app/services/activitypub/process_status_update_service.rb | 46 | ||||
-rw-r--r-- | app/services/fetch_link_card_service.rb | 15 | ||||
-rw-r--r-- | app/services/remove_status_service.rb | 63 | ||||
-rw-r--r-- | app/services/resolve_account_service.rb | 13 | ||||
-rw-r--r-- | app/services/vote_service.rb | 19 |
6 files changed, 71 insertions, 118 deletions
diff --git a/app/services/activitypub/process_account_service.rb b/app/services/activitypub/process_account_service.rb index 5649153ee..4449a5427 100644 --- a/app/services/activitypub/process_account_service.rb +++ b/app/services/activitypub/process_account_service.rb @@ -4,6 +4,7 @@ class ActivityPub::ProcessAccountService < BaseService include JsonLdHelper include DomainControlHelper include Redisable + include Lockable # Should be called with confirmed valid JSON # and WebFinger-resolved username and domain @@ -17,22 +18,18 @@ class ActivityPub::ProcessAccountService < BaseService @domain = domain @collections = {} - RedisLock.acquire(lock_options) do |lock| - if lock.acquired? - @account = Account.remote.find_by(uri: @uri) if @options[:only_key] - @account ||= Account.find_remote(@username, @domain) - @old_public_key = @account&.public_key - @old_protocol = @account&.protocol - @suspension_changed = false - - create_account if @account.nil? - update_account - process_tags - - process_duplicate_accounts! if @options[:verified_webfinger] - else - raise Mastodon::RaceConditionError - end + with_lock("process_account:#{@uri}") do + @account = Account.remote.find_by(uri: @uri) if @options[:only_key] + @account ||= Account.find_remote(@username, @domain) + @old_public_key = @account&.public_key + @old_protocol = @account&.protocol + @suspension_changed = false + + create_account if @account.nil? + update_account + process_tags + + process_duplicate_accounts! if @options[:verified_webfinger] end return if @account.nil? @@ -289,10 +286,6 @@ class ActivityPub::ProcessAccountService < BaseService !@old_protocol.nil? && @old_protocol != @account.protocol end - def lock_options - { redis: redis, key: "process_account:#{@uri}", autorelease: 15.minutes.seconds } - end - def process_tags return if @json['tag'].blank? diff --git a/app/services/activitypub/process_status_update_service.rb b/app/services/activitypub/process_status_update_service.rb index fb6e44c6d..addd5fc27 100644 --- a/app/services/activitypub/process_status_update_service.rb +++ b/app/services/activitypub/process_status_update_service.rb @@ -3,6 +3,7 @@ class ActivityPub::ProcessStatusUpdateService < BaseService include JsonLdHelper include Redisable + include Lockable def call(status, json) raise ArgumentError, 'Status has unsaved changes' if status.changed? @@ -33,41 +34,32 @@ class ActivityPub::ProcessStatusUpdateService < BaseService last_edit_date = @status.edited_at.presence || @status.created_at # Only allow processing one create/update per status at a time - RedisLock.acquire(lock_options) do |lock| - if lock.acquired? - Status.transaction do - record_previous_edit! - update_media_attachments! - update_poll! - update_immediate_attributes! - update_metadata! - create_edits! - end + with_lock("create:#{@uri}") do + Status.transaction do + record_previous_edit! + update_media_attachments! + update_poll! + update_immediate_attributes! + update_metadata! + create_edits! + end - queue_poll_notifications! + queue_poll_notifications! - next unless significant_changes? + next unless significant_changes? - reset_preview_card! - broadcast_updates! - else - raise Mastodon::RaceConditionError - end + reset_preview_card! + broadcast_updates! end forward_activity! if significant_changes? && @status_parser.edited_at > last_edit_date end def handle_implicit_update! - RedisLock.acquire(lock_options) do |lock| - if lock.acquired? - update_poll!(allow_significant_changes: false) - else - raise Mastodon::RaceConditionError - end + with_lock("create:#{@uri}") do + update_poll!(allow_significant_changes: false) + queue_poll_notifications! end - - queue_poll_notifications! end def update_media_attachments! @@ -241,10 +233,6 @@ class ActivityPub::ProcessStatusUpdateService < BaseService equals_or_includes_any?(@json['type'], %w(Note Question)) end - def lock_options - { redis: redis, key: "create:#{@uri}", autorelease: 15.minutes.seconds } - end - def record_previous_edit! @previous_edit = @status.build_snapshot(at_time: @status.created_at, rate_limit: false) if @status.edits.empty? end diff --git a/app/services/fetch_link_card_service.rb b/app/services/fetch_link_card_service.rb index 868796a6b..e5b5b730e 100644 --- a/app/services/fetch_link_card_service.rb +++ b/app/services/fetch_link_card_service.rb @@ -2,6 +2,7 @@ class FetchLinkCardService < BaseService include Redisable + include Lockable URL_PATTERN = %r{ (#{Twitter::TwitterText::Regex[:valid_url_preceding_chars]}) # $1 preceding chars @@ -22,13 +23,9 @@ class FetchLinkCardService < BaseService @url = @original_url.to_s - RedisLock.acquire(lock_options) do |lock| - if lock.acquired? - @card = PreviewCard.find_by(url: @url) - process_url if @card.nil? || @card.updated_at <= 2.weeks.ago || @card.missing_image? - else - raise Mastodon::RaceConditionError - end + with_lock("fetch:#{@original_url}") do + @card = PreviewCard.find_by(url: @url) + process_url if @card.nil? || @card.updated_at <= 2.weeks.ago || @card.missing_image? end attach_card if @card&.persisted? @@ -155,8 +152,4 @@ class FetchLinkCardService < BaseService @card.assign_attributes(link_details_extractor.to_preview_card_attributes) @card.save_with_optional_image! unless @card.title.blank? && @card.html.blank? end - - def lock_options - { redis: redis, key: "fetch:#{@original_url}", autorelease: 15.minutes.seconds } - end end diff --git a/app/services/remove_status_service.rb b/app/services/remove_status_service.rb index c31d89776..97afc3f61 100644 --- a/app/services/remove_status_service.rb +++ b/app/services/remove_status_service.rb @@ -3,6 +3,7 @@ class RemoveStatusService < BaseService include Redisable include Payloadable + include Lockable # Delete a status # @param [Status] status @@ -17,38 +18,34 @@ class RemoveStatusService < BaseService @account = status.account @options = options - RedisLock.acquire(lock_options) do |lock| - if lock.acquired? - @status.discard - - remove_from_self if @account.local? - remove_from_followers - remove_from_lists - - # There is no reason to send out Undo activities when the - # cause is that the original object has been removed, since - # original object being removed implicitly removes reblogs - # of it. The Delete activity of the original is forwarded - # separately. - remove_from_remote_reach if @account.local? && !@options[:original_removed] - - # Since reblogs don't mention anyone, don't get reblogged, - # favourited and don't contain their own media attachments - # or hashtags, this can be skipped - unless @status.reblog? - remove_from_mentions - remove_reblogs - remove_from_hashtags - remove_from_public - remove_from_media if @status.with_media? - remove_from_direct if status.direct_visibility? - remove_media - end - - @status.destroy! if permanently? - else - raise Mastodon::RaceConditionError + with_lock("distribute:#{@status.id}") do + @status.discard + + remove_from_self if @account.local? + remove_from_followers + remove_from_lists + + # There is no reason to send out Undo activities when the + # cause is that the original object has been removed, since + # original object being removed implicitly removes reblogs + # of it. The Delete activity of the original is forwarded + # separately. + remove_from_remote_reach if @account.local? && !@options[:original_removed] + + # Since reblogs don't mention anyone, don't get reblogged, + # favourited and don't contain their own media attachments + # or hashtags, this can be skipped + unless @status.reblog? + remove_from_mentions + remove_reblogs + remove_from_hashtags + remove_from_public + remove_from_media if @status.with_media? + remove_from_direct if status.direct_visibility? + remove_media end + + @status.destroy! if permanently? end end @@ -152,8 +149,4 @@ class RemoveStatusService < BaseService def permanently? @options[:immediate] || !(@options[:preserve] || @status.reported?) end - - def lock_options - { redis: redis, key: "distribute:#{@status.id}", autorelease: 5.minutes.seconds } - end end diff --git a/app/services/resolve_account_service.rb b/app/services/resolve_account_service.rb index 387e2e09b..b55e45409 100644 --- a/app/services/resolve_account_service.rb +++ b/app/services/resolve_account_service.rb @@ -5,6 +5,7 @@ class ResolveAccountService < BaseService include DomainControlHelper include WebfingerHelper include Redisable + include Lockable # Find or create an account record for a remote user. When creating, # look up the user's webfinger and fetch ActivityPub data @@ -108,12 +109,8 @@ class ResolveAccountService < BaseService def fetch_account! return unless activitypub_ready? - RedisLock.acquire(lock_options) do |lock| - if lock.acquired? - @account = ActivityPub::FetchRemoteAccountService.new.call(actor_url) - else - raise Mastodon::RaceConditionError - end + with_lock("resolve:#{@username}@#{@domain}") do + @account = ActivityPub::FetchRemoteAccountService.new.call(actor_url) end @account @@ -146,8 +143,4 @@ class ResolveAccountService < BaseService @account.suspend!(origin: :remote) AccountDeletionWorker.perform_async(@account.id, { 'reserve_username' => false, 'skip_activitypub' => true }) end - - def lock_options - { redis: redis, key: "resolve:#{@username}@#{@domain}", autorelease: 15.minutes.seconds } - end end diff --git a/app/services/vote_service.rb b/app/services/vote_service.rb index b77812970..ccd04dbfc 100644 --- a/app/services/vote_service.rb +++ b/app/services/vote_service.rb @@ -4,6 +4,7 @@ class VoteService < BaseService include Authorization include Payloadable include Redisable + include Lockable def call(account, poll, choices) authorize_with account, poll, :vote? @@ -15,17 +16,13 @@ class VoteService < BaseService already_voted = true - RedisLock.acquire(lock_options) do |lock| - if lock.acquired? - already_voted = @poll.votes.where(account: @account).exists? + with_lock("vote:#{@poll.id}:#{@account.id}") do + already_voted = @poll.votes.where(account: @account).exists? - ApplicationRecord.transaction do - @choices.each do |choice| - @votes << @poll.votes.create!(account: @account, choice: Integer(choice)) - end + ApplicationRecord.transaction do + @choices.each do |choice| + @votes << @poll.votes.create!(account: @account, choice: Integer(choice)) end - else - raise Mastodon::RaceConditionError end end @@ -76,8 +73,4 @@ class VoteService < BaseService @poll.reload retry end - - def lock_options - { redis: redis, key: "vote:#{@poll.id}:#{@account.id}" } - end end |