diff options
author | Thibaut Girka <thib@sitedethib.com> | 2019-03-05 19:23:16 +0100 |
---|---|---|
committer | Thibaut Girka <thib@sitedethib.com> | 2019-03-05 19:23:16 +0100 |
commit | f513317ba262d9f5d7aa7dd66f8b61690297e107 (patch) | |
tree | 6732fcf1178fc3e27404c6830bc2bc1c832659ae /app/services/activitypub/fetch_remote_poll_service.rb | |
parent | 2a4ce7458a16c64029842fde210089453be2ede1 (diff) | |
parent | 7d5e2dda78414316f9cf09fcf6096d6a158da312 (diff) |
Merge branch 'master' into glitch-soc/merge-upstream
Conflicts: - app/models/status.rb - db/schema.rb Both conflicts are caused by us having extra database columns.
Diffstat (limited to 'app/services/activitypub/fetch_remote_poll_service.rb')
-rw-r--r-- | app/services/activitypub/fetch_remote_poll_service.rb | 52 |
1 files changed, 52 insertions, 0 deletions
diff --git a/app/services/activitypub/fetch_remote_poll_service.rb b/app/services/activitypub/fetch_remote_poll_service.rb new file mode 100644 index 000000000..1dd587d73 --- /dev/null +++ b/app/services/activitypub/fetch_remote_poll_service.rb @@ -0,0 +1,52 @@ +# frozen_string_literal: true + +class ActivityPub::FetchRemotePollService < BaseService + include JsonLdHelper + + def call(poll, on_behalf_of = nil) + @json = fetch_resource(poll.status.uri, true, on_behalf_of) + + return unless supported_context? && expected_type? + + 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 + + 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 } + ) + end + + private + + def supported_context? + super(@json) + end + + def expected_type? + equals_or_includes_any?(@json['type'], %w(Question)) + end +end |