Skip to content

Commit 133529b

Browse files
Gargronhiyuki2578
authored andcommitted
Fix poll update handler calling method was that was not available (mastodon#10246)
* Fix poll update handler calling method was that was not available Fix regression from mastodon#10209 * Refactor VoteService * Refactor ActivityPub::DistributePollUpdateWorker and optimize it * Fix typo * Fix typo
1 parent bdc3c2e commit 133529b

9 files changed

Lines changed: 61 additions & 53 deletions

File tree

app/helpers/jsonld_helper.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,15 @@ def unsupported_uri_scheme?(uri)
4747
!uri.start_with?('http://', 'https://')
4848
end
4949

50+
def invalid_origin?(url)
51+
return true if unsupported_uri_scheme?(url)
52+
53+
needle = Addressable::URI.parse(url).host
54+
haystack = Addressable::URI.parse(@account.uri).host
55+
56+
!haystack.casecmp(needle).zero?
57+
end
58+
5059
def canonicalize(json)
5160
graph = RDF::Graph.new << JSON::LD::API.toRdf(json, documentLoader: method(:load_jsonld_context))
5261
graph.dump(:normalize)

app/lib/activitypub/activity/create.rb

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -241,9 +241,12 @@ def process_poll
241241

242242
def poll_vote?
243243
return false if replied_to_status.nil? || replied_to_status.poll.nil? || !replied_to_status.local? || !replied_to_status.poll.options.include?(@object['name'])
244-
return true if replied_to_status.poll.expired?
245-
replied_to_status.poll.votes.create!(account: @account, choice: replied_to_status.poll.options.index(@object['name']), uri: @object['id'])
246-
ActivityPub::DistributePollUpdateWorker.perform_in(3.minutes, replied_to_status.id) unless replied_to_status.poll.hide_totals
244+
245+
unless replied_to_status.poll.expired?
246+
replied_to_status.poll.votes.create!(account: @account, choice: replied_to_status.poll.options.index(@object['name']), uri: @object['id'])
247+
ActivityPub::DistributePollUpdateWorker.perform_in(3.minutes, replied_to_status.id) unless replied_to_status.poll.hide_totals?
248+
end
249+
247250
true
248251
end
249252

@@ -371,15 +374,6 @@ def skip_download?
371374
@skip_download ||= DomainBlock.find_by(domain: @account.domain)&.reject_media?
372375
end
373376

374-
def invalid_origin?(url)
375-
return true if unsupported_uri_scheme?(url)
376-
377-
needle = Addressable::URI.parse(url).host
378-
haystack = Addressable::URI.parse(@account.uri).host
379-
380-
!haystack.casecmp(needle).zero?
381-
end
382-
383377
def reply_to_local?
384378
!replied_to_status.nil? && replied_to_status.account.local?
385379
end

app/lib/activitypub/activity/delete.rb

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -75,13 +75,4 @@ def payload
7575
def lock_options
7676
{ redis: Redis.current, key: "create:#{object_uri}" }
7777
end
78-
79-
def invalid_origin?(url)
80-
return true if unsupported_uri_scheme?(url)
81-
82-
needle = Addressable::URI.parse(url).host
83-
haystack = Addressable::URI.parse(@account.uri).host
84-
85-
!haystack.casecmp(needle).zero?
86-
end
8778
end

app/lib/activitypub/activity/update.rb

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,11 @@ class ActivityPub::Activity::Update < ActivityPub::Activity
44
SUPPORTED_TYPES = %w(Application Group Organization Person Service).freeze
55

66
def perform
7-
update_account if equals_or_includes_any?(@object['type'], SUPPORTED_TYPES)
8-
update_poll if equals_or_includes_any?(@object['type'], %w(Question))
7+
if equals_or_includes_any?(@object['type'], SUPPORTED_TYPES)
8+
update_account
9+
elsif equals_or_includes_any?(@object['type'], %w(Question))
10+
update_poll
11+
end
912
end
1013

1114
private
@@ -18,11 +21,10 @@ def update_account
1821

1922
def update_poll
2023
return reject_payload! if invalid_origin?(@object['id'])
24+
2125
status = Status.find_by(uri: object_uri, account_id: @account.id)
22-
return if status.nil? || status.poll_id.nil?
23-
poll = Poll.find(status.poll_id)
24-
return if poll.nil?
26+
return if status.nil? || status.poll.nil?
2527

26-
ActivityPub::ProcessPollService.new.call(poll, @object)
28+
ActivityPub::ProcessPollService.new.call(status.poll, @object)
2729
end
2830
end

app/models/concerns/expireable.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,11 @@ def expire!
1818
end
1919

2020
def expired?
21-
!expires_at.nil? && expires_at < Time.now.utc
21+
expires? && expires_at < Time.now.utc
22+
end
23+
24+
def expires?
25+
!expires_at.nil?
2226
end
2327
end
2428
end

app/services/activitypub/fetch_replies_service.rb

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -46,13 +46,4 @@ def filtered_replies
4646
# Also limit to 5 fetched replies to limit potential for DoS.
4747
@items.map { |item| value_or_id(item) }.reject { |uri| invalid_origin?(uri) }.take(5)
4848
end
49-
50-
def invalid_origin?(url)
51-
return true if unsupported_uri_scheme?(url)
52-
53-
needle = Addressable::URI.parse(url).host
54-
haystack = Addressable::URI.parse(@account.uri).host
55-
56-
!haystack.casecmp(needle).zero?
57-
end
5849
end

app/services/notify_service.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ def domain_blocking?
9292

9393
def blocked?
9494
blocked = @recipient.suspended? # Skip if the recipient account is suspended anyway
95-
blocked ||= from_self? unless @notification.type == :poll # Skip for interactions with self
95+
blocked ||= from_self? && @notification.type != :poll # Skip for interactions with self
9696

9797
return blocked if message? && from_staff?
9898

app/services/vote_service.rb

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,21 +20,35 @@ def call(account, poll, choices)
2020
end
2121

2222
if @poll.account.local?
23-
ActivityPub::DistributePollUpdateWorker.perform_in(3.minutes, @poll.status.id) unless @poll.hide_totals
23+
distribute_poll!
2424
else
25-
@votes.each do |vote|
26-
ActivityPub::DeliveryWorker.perform_async(
27-
build_json(vote),
28-
@account.id,
29-
@poll.account.inbox_url
30-
)
31-
end
32-
PollExpirationNotifyWorker.perform_at(@poll.expires_at + 5.minutes, @poll.id) unless @poll.expires_at.nil?
25+
deliver_votes!
26+
queue_final_poll_check!
3327
end
3428
end
3529

3630
private
3731

32+
def distribute_poll!
33+
return if @poll.hide_totals?
34+
ActivityPub::DistributePollUpdateWorker.perform_in(3.minutes, @poll.status.id)
35+
end
36+
37+
def queue_final_poll_check!
38+
return unless @poll.expires?
39+
PollExpirationNotifyWorker.perform_at(@poll.expires_at + 5.minutes, @poll.id)
40+
end
41+
42+
def deliver_votes!
43+
@votes.each do |vote|
44+
ActivityPub::DeliveryWorker.perform_async(
45+
build_json(vote),
46+
@account.id,
47+
@poll.account.inbox_url
48+
)
49+
end
50+
end
51+
3852
def build_json(vote)
3953
ActiveModelSerializers::SerializableResource.new(
4054
vote,

app/workers/activitypub/distribute_poll_update_worker.rb

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,16 @@ def relayable?
2828

2929
def inboxes
3030
return @inboxes if defined?(@inboxes)
31-
target_accounts = @status.mentions.map(&:account).reject(&:local?)
32-
target_accounts += @status.reblogs.map(&:account).reject(&:local?)
33-
target_accounts += @status.poll.votes.map(&:account).reject(&:local?)
34-
target_accounts.uniq!(&:id)
35-
@inboxes = target_accounts.select(&:activitypub?).pluck(&:inbox_url)
36-
@inboxes += @account.followers.inboxes unless @status.direct_visibility?
31+
32+
@inboxes = [@status.mentions, @status.reblogs, @status.poll.votes].flat_map do |relation|
33+
relation.includes(:account).map do |record|
34+
record.account.preferred_inbox_url if !record.account.local? && record.account.activitypub?
35+
end
36+
end
37+
38+
@inboxes.concat(@account.followers.inboxes) unless @status.direct_visibility?
3739
@inboxes.uniq!
40+
@inboxes.compact!
3841
@inboxes
3942
end
4043

0 commit comments

Comments
 (0)