2019-03-03 23:18:23 +02:00
|
|
|
# frozen_string_literal: true
|
2023-02-20 07:58:28 +02:00
|
|
|
|
2019-03-03 23:18:23 +02:00
|
|
|
# == Schema Information
|
|
|
|
#
|
|
|
|
# Table name: polls
|
|
|
|
#
|
|
|
|
# id :bigint(8) not null, primary key
|
|
|
|
# account_id :bigint(8)
|
|
|
|
# status_id :bigint(8)
|
|
|
|
# expires_at :datetime
|
|
|
|
# options :string default([]), not null, is an Array
|
|
|
|
# cached_tallies :bigint(8) default([]), not null, is an Array
|
|
|
|
# multiple :boolean default(FALSE), not null
|
|
|
|
# hide_totals :boolean default(FALSE), not null
|
|
|
|
# votes_count :bigint(8) default(0), not null
|
|
|
|
# last_fetched_at :datetime
|
|
|
|
# created_at :datetime not null
|
|
|
|
# updated_at :datetime not null
|
2019-03-06 20:53:57 +02:00
|
|
|
# lock_version :integer default(0), not null
|
2019-09-29 23:58:01 +03:00
|
|
|
# voters_count :bigint(8)
|
2019-03-03 23:18:23 +02:00
|
|
|
#
|
|
|
|
|
|
|
|
class Poll < ApplicationRecord
|
|
|
|
include Expireable
|
|
|
|
|
|
|
|
belongs_to :account
|
|
|
|
belongs_to :status
|
|
|
|
|
2020-12-20 19:25:00 +02:00
|
|
|
has_many :votes, class_name: 'PollVote', inverse_of: :poll, dependent: :delete_all
|
2024-01-18 14:29:41 +02:00
|
|
|
|
|
|
|
with_options class_name: 'Account', source: :account, through: :votes do
|
|
|
|
has_many :voters, -> { group('accounts.id') }
|
|
|
|
has_many :local_voters, -> { group('accounts.id').merge(Account.local) }
|
|
|
|
end
|
2019-03-03 23:18:23 +02:00
|
|
|
|
2019-03-13 20:29:54 +02:00
|
|
|
has_many :notifications, as: :activity, dependent: :destroy
|
|
|
|
|
2019-03-03 23:18:23 +02:00
|
|
|
validates :options, presence: true
|
|
|
|
validates :expires_at, presence: true, if: :local?
|
2019-03-08 01:54:50 +02:00
|
|
|
validates_with PollValidator, on: :create, if: :local?
|
2019-03-03 23:18:23 +02:00
|
|
|
|
|
|
|
scope :attached, -> { where.not(status_id: nil) }
|
|
|
|
scope :unattached, -> { where(status_id: nil) }
|
|
|
|
|
2019-12-01 18:24:33 +02:00
|
|
|
before_validation :prepare_options, if: :local?
|
2019-03-03 23:18:23 +02:00
|
|
|
before_validation :prepare_votes_count
|
2022-05-26 19:05:47 +03:00
|
|
|
before_validation :prepare_cached_tallies
|
2019-03-05 04:45:56 +02:00
|
|
|
|
2019-03-03 23:18:23 +02:00
|
|
|
after_commit :reset_parent_cache, on: :update
|
|
|
|
|
|
|
|
def loaded_options
|
2022-05-26 19:05:47 +03:00
|
|
|
options.map.with_index { |title, key| Option.new(self, key.to_s, title, show_totals_now? ? (cached_tallies[key] || 0) : nil) }
|
2019-03-03 23:18:23 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
def possibly_stale?
|
|
|
|
remote? && last_fetched_before_expiration? && time_passed_since_last_fetch?
|
|
|
|
end
|
|
|
|
|
2019-03-07 23:53:47 +02:00
|
|
|
def voted?(account)
|
2024-01-24 11:57:49 +02:00
|
|
|
account.id == account_id || votes.exists?(account: account)
|
2019-03-07 23:53:47 +02:00
|
|
|
end
|
|
|
|
|
2019-09-22 15:15:18 +03:00
|
|
|
def own_votes(account)
|
|
|
|
votes.where(account: account).pluck(:choice)
|
|
|
|
end
|
|
|
|
|
2019-03-03 23:18:23 +02:00
|
|
|
delegate :local?, to: :account
|
|
|
|
|
|
|
|
def remote?
|
|
|
|
!local?
|
|
|
|
end
|
|
|
|
|
2019-03-20 18:29:12 +02:00
|
|
|
def emojis
|
|
|
|
@emojis ||= CustomEmoji.from_text(options.join(' '), account.domain)
|
|
|
|
end
|
|
|
|
|
2019-03-03 23:18:23 +02:00
|
|
|
class Option < ActiveModelSerializers::Model
|
|
|
|
attributes :id, :title, :votes_count, :poll
|
|
|
|
|
|
|
|
def initialize(poll, id, title, votes_count)
|
2021-01-07 10:40:55 +02:00
|
|
|
super(
|
2023-02-20 07:58:28 +02:00
|
|
|
poll: poll,
|
|
|
|
id: id,
|
|
|
|
title: title,
|
2021-01-07 10:40:55 +02:00
|
|
|
votes_count: votes_count,
|
|
|
|
)
|
2019-03-03 23:18:23 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-02-10 15:26:54 +02:00
|
|
|
def reset_votes!
|
|
|
|
self.cached_tallies = options.map { 0 }
|
|
|
|
self.votes_count = 0
|
2022-11-27 00:08:25 +02:00
|
|
|
self.voters_count = 0
|
2022-02-10 15:26:54 +02:00
|
|
|
votes.delete_all unless new_record?
|
|
|
|
end
|
|
|
|
|
2019-03-03 23:18:23 +02:00
|
|
|
private
|
|
|
|
|
|
|
|
def prepare_cached_tallies
|
|
|
|
self.cached_tallies = options.map { 0 } if cached_tallies.empty?
|
|
|
|
end
|
|
|
|
|
|
|
|
def prepare_votes_count
|
|
|
|
self.votes_count = cached_tallies.sum unless cached_tallies.empty?
|
|
|
|
end
|
|
|
|
|
2019-03-05 04:45:56 +02:00
|
|
|
def prepare_options
|
2023-04-30 15:07:21 +03:00
|
|
|
self.options = options.map(&:strip).compact_blank
|
2019-03-05 04:45:56 +02:00
|
|
|
end
|
|
|
|
|
2019-03-03 23:18:23 +02:00
|
|
|
def reset_parent_cache
|
|
|
|
return if status_id.nil?
|
2023-02-20 07:58:28 +02:00
|
|
|
|
2023-09-15 20:52:28 +03:00
|
|
|
Rails.cache.delete("v3:statuses/#{status_id}")
|
2019-03-03 23:18:23 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
def last_fetched_before_expiration?
|
|
|
|
last_fetched_at.nil? || expires_at.nil? || last_fetched_at < expires_at
|
|
|
|
end
|
|
|
|
|
|
|
|
def time_passed_since_last_fetch?
|
|
|
|
last_fetched_at.nil? || last_fetched_at < 1.minute.ago
|
|
|
|
end
|
2019-03-07 23:53:47 +02:00
|
|
|
|
|
|
|
def show_totals_now?
|
|
|
|
expired? || !hide_totals?
|
|
|
|
end
|
2019-03-03 23:18:23 +02:00
|
|
|
end
|