Skip to content

Commit 05862cb

Browse files
ClearlyClaireGargron
authored andcommitted
Store remote votes URI (mastodon#10158)
* Store remote votes URI * Add spec for accepting remote votes * Make poll vote id generation work the same way as follows
1 parent a8d8343 commit 05862cb

6 files changed

Lines changed: 33 additions & 3 deletions

File tree

app/lib/activitypub/activity/create.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,7 @@ 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-
replied_to_status.poll.votes.create!(account: @account, choice: replied_to_status.poll.options.index(@object['name']))
244+
replied_to_status.poll.votes.create!(account: @account, choice: replied_to_status.poll.options.index(@object['name']), uri: @object['id'])
245245
end
246246

247247
def resolve_thread(status)

app/models/poll_vote.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
# choice :integer default(0), not null
1010
# created_at :datetime not null
1111
# updated_at :datetime not null
12+
# uri :string
1213
#
1314

1415
class PollVote < ApplicationRecord
@@ -20,6 +21,8 @@ class PollVote < ApplicationRecord
2021

2122
after_create_commit :increment_counter_cache
2223

24+
delegate :local?, to: :account
25+
2326
private
2427

2528
def increment_counter_cache

app/serializers/activitypub/vote_serializer.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ class NoteSerializer < ActiveModel::Serializer
66
:in_reply_to, :to
77

88
def id
9-
[ActivityPub::TagManager.instance.uri_for(object.account), '#votes/', object.id].join
9+
ActivityPub::TagManager.instance.uri_for(object) || [ActivityPub::TagManager.instance.uri_for(object.account), '#votes/', object.id].join
1010
end
1111

1212
def type
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
class AddUriToPollVotes < ActiveRecord::Migration[5.2]
2+
def change
3+
add_column :poll_votes, :uri, :string
4+
end
5+
end

db/schema.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
#
1111
# It's strongly recommended that you check this file into your version control system.
1212

13-
ActiveRecord::Schema.define(version: 2019_02_26_003449) do
13+
ActiveRecord::Schema.define(version: 2019_03_04_152020) do
1414

1515
# These are extensions that must be enabled in order to support this database
1616
enable_extension "plpgsql"
@@ -447,6 +447,7 @@
447447
t.integer "choice", default: 0, null: false
448448
t.datetime "created_at", null: false
449449
t.datetime "updated_at", null: false
450+
t.string "uri"
450451
t.index ["account_id"], name: "index_poll_votes_on_account_id"
451452
t.index ["poll_id"], name: "index_poll_votes_on_poll_id"
452453
end

spec/lib/activitypub/activity/create_spec.rb

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -447,6 +447,27 @@
447447
expect(poll.cached_tallies).to eq [10, 3]
448448
end
449449
end
450+
451+
context 'when a vote to a local poll' do
452+
let(:poll) { Fabricate(:poll, options: %w(Yellow Blue)) }
453+
let!(:local_status) { Fabricate(:status, owned_poll: poll) }
454+
455+
let(:object_json) do
456+
{
457+
id: [ActivityPub::TagManager.instance.uri_for(sender), '#bar'].join,
458+
type: 'Note',
459+
name: 'Yellow',
460+
inReplyTo: ActivityPub::TagManager.instance.uri_for(local_status)
461+
}
462+
end
463+
464+
it 'adds a vote to the poll with correct uri' do
465+
vote = poll.votes.first
466+
expect(vote).to_not be_nil
467+
expect(vote.uri).to eq object_json[:id]
468+
expect(poll.reload.cached_tallies).to eq [1, 0]
469+
end
470+
end
450471
end
451472

452473
context 'when sender is followed by local users' do

0 commit comments

Comments
 (0)