Skip to content

Commit

Permalink
Store remote votes URI (#10158)
Browse files Browse the repository at this point in the history
* Store remote votes URI

* Add spec for accepting remote votes

* Make poll vote id generation work the same way as follows
  • Loading branch information
ClearlyClaire authored and Gargron committed Mar 4, 2019
1 parent 7a25bb8 commit 833ffce
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 3 deletions.
2 changes: 1 addition & 1 deletion app/lib/activitypub/activity/create.rb
Expand Up @@ -241,7 +241,7 @@ def process_poll

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)
Expand Down
3 changes: 3 additions & 0 deletions app/models/poll_vote.rb
Expand Up @@ -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
Expand All @@ -20,6 +21,8 @@ class PollVote < ApplicationRecord

after_create_commit :increment_counter_cache

delegate :local?, to: :account

private

def increment_counter_cache
Expand Down
2 changes: 1 addition & 1 deletion app/serializers/activitypub/vote_serializer.rb
Expand Up @@ -6,7 +6,7 @@ class NoteSerializer < 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
Expand Down
5 changes: 5 additions & 0 deletions db/migrate/20190304152020_add_uri_to_poll_votes.rb
@@ -0,0 +1,5 @@
class AddUriToPollVotes < ActiveRecord::Migration[5.2]
def change
add_column :poll_votes, :uri, :string
end
end
3 changes: 2 additions & 1 deletion db/schema.rb
Expand Up @@ -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"
Expand Down Expand Up @@ -447,6 +447,7 @@
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
Expand Down
21 changes: 21 additions & 0 deletions spec/lib/activitypub/activity/create_spec.rb
Expand Up @@ -447,6 +447,27 @@
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
Expand Down

0 comments on commit 833ffce

Please sign in to comment.