forked from mastodon/mastodon
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support pushing and receiving updates to poll tallies (mastodon#10209)
* Process incoming poll tallies update * Send Update on poll vote * Do not send Updates for a poll more often than once every 3 minutes * Include voters in people to notify of results update * Schedule closing poll worker on poll creation * Add new notification type for ending polls * Add front-end support for ended poll notifications * Fix UpdatePollSerializer * Fix Updates not being triggered by local votes * Fix tests failure * Fix web push notifications for closing polls * Minor cleanup * Notify voters of both remote and local polls when those close * Fix delivery of poll updates to mentioned accounts and voters
- Loading branch information
1 parent
41977f0
commit 7d0076d
Showing
17 changed files
with
256 additions
and
62 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
# frozen_string_literal: true | ||
|
||
class ActivityPub::UpdatePollSerializer < ActiveModel::Serializer | ||
attributes :id, :type, :actor, :to | ||
|
||
has_one :object, serializer: ActivityPub::NoteSerializer | ||
|
||
def id | ||
[ActivityPub::TagManager.instance.uri_for(object), '#updates/', object.poll.updated_at.to_i].join | ||
end | ||
|
||
def type | ||
'Update' | ||
end | ||
|
||
def actor | ||
ActivityPub::TagManager.instance.uri_for(object) | ||
end | ||
|
||
def to | ||
ActivityPub::TagManager.instance.to(object) | ||
end | ||
|
||
def cc | ||
ActivityPub::TagManager.instance.cc(object) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
# frozen_string_literal: true | ||
|
||
class ActivityPub::ProcessPollService < BaseService | ||
include JsonLdHelper | ||
|
||
def call(poll, json) | ||
@json = json | ||
return unless supported_context? && expected_type? | ||
|
||
previous_expires_at = poll.expires_at | ||
|
||
expires_at = begin | ||
if @json['closed'].is_a?(String) | ||
@json['closed'] | ||
elsif !@json['closed'].nil? && !@json['closed'].is_a?(FalseClass) | ||
Time.now.utc | ||
else | ||
@json['endTime'] | ||
end | ||
end | ||
|
||
items = begin | ||
if @json['anyOf'].is_a?(Array) | ||
@json['anyOf'] | ||
else | ||
@json['oneOf'] | ||
end | ||
end | ||
|
||
latest_options = items.map { |item| item['name'].presence || item['content'] } | ||
|
||
# If for some reasons the options were changed, it invalidates all previous | ||
# votes, so we need to remove them | ||
poll.votes.delete_all if latest_options != poll.options | ||
|
||
begin | ||
poll.update!( | ||
last_fetched_at: Time.now.utc, | ||
expires_at: expires_at, | ||
options: latest_options, | ||
cached_tallies: items.map { |item| item.dig('replies', 'totalItems') || 0 } | ||
) | ||
rescue ActiveRecord::StaleObjectError | ||
poll.reload | ||
retry | ||
end | ||
|
||
# If the poll had no expiration date set but now has, and people have voted, | ||
# schedule a notification. | ||
if previous_expires_at.nil? && poll.expires_at.present? && poll.votes.exists? | ||
PollExpirationNotifyWorker.perform_at(poll.expires_at + 5.minutes, poll.id) | ||
end | ||
end | ||
|
||
private | ||
|
||
def supported_context? | ||
super(@json) | ||
end | ||
|
||
def expected_type? | ||
equals_or_includes_any?(@json['type'], %w(Question)) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.