From bdfdb7ba479477a7647e7e7fb75ec29270c3dd58 Mon Sep 17 00:00:00 2001 From: Thibaut Girka Date: Thu, 27 Jun 2019 17:05:58 +0200 Subject: [PATCH] Add own_votes field to poll results in REST API Fixes #10679 --- app/models/poll.rb | 4 ++++ app/serializers/rest/poll_serializer.rb | 5 +++++ 2 files changed, 9 insertions(+) diff --git a/app/models/poll.rb b/app/models/poll.rb index 8f72c7b112431..55a8f13a6510e 100644 --- a/app/models/poll.rb +++ b/app/models/poll.rb @@ -54,6 +54,10 @@ def voted?(account) account.id == account_id || votes.where(account: account).exists? end + def own_votes(account) + votes.where(account: account).pluck(:choice) + end + delegate :local?, to: :account def remote? diff --git a/app/serializers/rest/poll_serializer.rb b/app/serializers/rest/poll_serializer.rb index 356c45b83868d..eb98bb2d23e66 100644 --- a/app/serializers/rest/poll_serializer.rb +++ b/app/serializers/rest/poll_serializer.rb @@ -8,6 +8,7 @@ class REST::PollSerializer < ActiveModel::Serializer has_many :emojis, serializer: REST::CustomEmojiSerializer attribute :voted, if: :current_user? + attribute :own_votes, if: :current_user? def id object.id.to_s @@ -21,6 +22,10 @@ def voted object.voted?(current_user.account) end + def own_votes + object.own_votes(current_user.account) + end + def current_user? !current_user.nil? end