]> cat aescling's git repositories - mastodon.git/commitdiff
Store remote votes URI (#10158)
authorThibG <thib@sitedethib.com>
Mon, 4 Mar 2019 21:51:23 +0000 (22:51 +0100)
committerEugen Rochko <eugen@zeonfederated.com>
Mon, 4 Mar 2019 21:51:23 +0000 (22:51 +0100)
* Store remote votes URI

* Add spec for accepting remote votes

* Make poll vote id generation work the same way as follows

app/lib/activitypub/activity/create.rb
app/models/poll_vote.rb
app/serializers/activitypub/vote_serializer.rb
db/migrate/20190304152020_add_uri_to_poll_votes.rb [new file with mode: 0644]
db/schema.rb
spec/lib/activitypub/activity/create_spec.rb

index fc4c4569282da774bb94b60df7f7cd84058eb7cb..07ef16bf3f964088181b1bb47d6d16dc920f2991 100644 (file)
@@ -241,7 +241,7 @@ class ActivityPub::Activity::Create < ActivityPub::Activity
 
   def poll_vote?
     return false if replied_to_status.nil? || replied_to_status.poll.nil? || !replied_to_status.local? || !replied_to_status.poll.options.include?(@object['name'])
-    replied_to_status.poll.votes.create!(account: @account, choice: replied_to_status.poll.options.index(@object['name']))
+    replied_to_status.poll.votes.create!(account: @account, choice: replied_to_status.poll.options.index(@object['name']), uri: @object['id'])
   end
 
   def resolve_thread(status)
index 57781d61676bcbafd2e121e67bba6c4e1faef021..aec67896861c792325471734198a77bb432d2253 100644 (file)
@@ -9,6 +9,7 @@
 #  choice     :integer          default(0), not null
 #  created_at :datetime         not null
 #  updated_at :datetime         not null
+#  uri        :string
 #
 
 class PollVote < ApplicationRecord
@@ -20,6 +21,8 @@ class PollVote < ApplicationRecord
 
   after_create_commit :increment_counter_cache
 
+  delegate :local?, to: :account
+
   private
 
   def increment_counter_cache
index 655d04d22ee60c19c13d55f1824954f95a7fcf86..248190404e840f4f71a16e787a9feffca8021503 100644 (file)
@@ -6,7 +6,7 @@ class ActivityPub::VoteSerializer < ActiveModel::Serializer
                :in_reply_to, :to
 
     def id
-      [ActivityPub::TagManager.instance.uri_for(object.account), '#votes/', object.id].join
+      ActivityPub::TagManager.instance.uri_for(object) || [ActivityPub::TagManager.instance.uri_for(object.account), '#votes/', object.id].join
     end
 
     def type
diff --git a/db/migrate/20190304152020_add_uri_to_poll_votes.rb b/db/migrate/20190304152020_add_uri_to_poll_votes.rb
new file mode 100644 (file)
index 0000000..f6b81f1
--- /dev/null
@@ -0,0 +1,5 @@
+class AddUriToPollVotes < ActiveRecord::Migration[5.2]
+  def change
+    add_column :poll_votes, :uri, :string
+  end
+end
index d5d516e066dc771481ff32253474c2d0344c4ddc..161619dcf5993c81d571429296cb6c1e2236306c 100644 (file)
@@ -10,7 +10,7 @@
 #
 # It's strongly recommended that you check this file into your version control system.
 
-ActiveRecord::Schema.define(version: 2019_02_26_003449) do
+ActiveRecord::Schema.define(version: 2019_03_04_152020) do
 
   # These are extensions that must be enabled in order to support this database
   enable_extension "plpgsql"
@@ -447,6 +447,7 @@ ActiveRecord::Schema.define(version: 2019_02_26_003449) do
     t.integer "choice", default: 0, null: false
     t.datetime "created_at", null: false
     t.datetime "updated_at", null: false
+    t.string "uri"
     t.index ["account_id"], name: "index_poll_votes_on_account_id"
     t.index ["poll_id"], name: "index_poll_votes_on_poll_id"
   end
index ac6237c865cdebc47cd2db961c43810eb3c6fe7e..4780c29c88da7d0c9a42c9330e9d5cc4f79b0234 100644 (file)
@@ -447,6 +447,27 @@ RSpec.describe ActivityPub::Activity::Create do
           expect(poll.cached_tallies).to eq [10, 3]
         end
       end
+
+      context 'when a vote to a local poll' do
+        let(:poll) { Fabricate(:poll, options: %w(Yellow Blue)) }
+        let!(:local_status) { Fabricate(:status, owned_poll: poll) }
+
+        let(:object_json) do
+          {
+            id: [ActivityPub::TagManager.instance.uri_for(sender), '#bar'].join,
+            type: 'Note',
+            name: 'Yellow',
+            inReplyTo: ActivityPub::TagManager.instance.uri_for(local_status)
+          }
+        end
+
+        it 'adds a vote to the poll with correct uri' do
+          vote = poll.votes.first
+          expect(vote).to_not be_nil
+          expect(vote.uri).to eq object_json[:id]
+          expect(poll.reload.cached_tallies).to eq [1, 0]
+        end
+      end
     end
 
     context 'when sender is followed by local users' do