about summary refs log tree commit diff
path: root/app/services/activitypub/fetch_remote_poll_service.rb
diff options
context:
space:
mode:
authorThibG <thib@sitedethib.com>2019-03-11 13:23:54 +0100
committerGitHub <noreply@github.com>2019-03-11 13:23:54 +0100
commit45e4c90a23ccd6ccbed389d5c1b62f830d2c3757 (patch)
tree6fbb79dda2e8a75c8b88bccd08078f346e8392ca /app/services/activitypub/fetch_remote_poll_service.rb
parentf534f4869ecb52cb48f3e68e42752d8f81d7a710 (diff)
parent9c620fc5c80ff0f2aa600069dfdf868e150b0799 (diff)
Merge pull request #953 from ThibG/glitch-soc/merge-upstream
Merge upstream changes
Diffstat (limited to 'app/services/activitypub/fetch_remote_poll_service.rb')
-rw-r--r--app/services/activitypub/fetch_remote_poll_service.rb51
1 files changed, 2 insertions, 49 deletions
diff --git a/app/services/activitypub/fetch_remote_poll_service.rb b/app/services/activitypub/fetch_remote_poll_service.rb
index 4f9814fcd..44a23712c 100644
--- a/app/services/activitypub/fetch_remote_poll_service.rb
+++ b/app/services/activitypub/fetch_remote_poll_service.rb
@@ -4,54 +4,7 @@ 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
-
-    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
-  end
-
-  private
-
-  def supported_context?
-    super(@json)
-  end
-
-  def expected_type?
-    equals_or_includes_any?(@json['type'], %w(Question))
+    json = fetch_resource(poll.status.uri, true, on_behalf_of)
+    ActivityPub::ProcessPollService.new.call(poll, json)
   end
 end