1
0

poll.rb 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. # frozen_string_literal: true
  2. # == Schema Information
  3. #
  4. # Table name: polls
  5. #
  6. # id :bigint(8) not null, primary key
  7. # account_id :bigint(8)
  8. # status_id :bigint(8)
  9. # expires_at :datetime
  10. # options :string default([]), not null, is an Array
  11. # cached_tallies :bigint(8) default([]), not null, is an Array
  12. # multiple :boolean default(FALSE), not null
  13. # hide_totals :boolean default(FALSE), not null
  14. # votes_count :bigint(8) default(0), not null
  15. # last_fetched_at :datetime
  16. # created_at :datetime not null
  17. # updated_at :datetime not null
  18. # lock_version :integer default(0), not null
  19. # voters_count :bigint(8)
  20. #
  21. class Poll < ApplicationRecord
  22. include Expireable
  23. belongs_to :account
  24. belongs_to :status
  25. has_many :votes, class_name: 'PollVote', inverse_of: :poll, dependent: :delete_all
  26. with_options class_name: 'Account', source: :account, through: :votes do
  27. has_many :voters, -> { group(accounts: [:id]) }
  28. has_many :local_voters, -> { group(accounts: [:id]).merge(Account.local) }
  29. end
  30. has_many :notifications, as: :activity, dependent: :destroy
  31. validates :options, presence: true
  32. validates :expires_at, presence: true, if: :local?
  33. validates_with PollValidator, on: :create, if: :local?
  34. scope :attached, -> { where.not(status_id: nil) }
  35. scope :unattached, -> { where(status_id: nil) }
  36. before_validation :prepare_options, if: :local?
  37. before_validation :prepare_votes_count
  38. before_validation :prepare_cached_tallies
  39. after_commit :reset_parent_cache, on: :update
  40. def loaded_options
  41. options.map.with_index { |title, key| Option.new(self, key.to_s, title, show_totals_now? ? (cached_tallies[key] || 0) : nil) }
  42. end
  43. def possibly_stale?
  44. remote? && last_fetched_before_expiration? && time_passed_since_last_fetch?
  45. end
  46. def voted?(account)
  47. account.id == account_id || votes.exists?(account: account)
  48. end
  49. def own_votes(account)
  50. votes.where(account: account).pluck(:choice)
  51. end
  52. delegate :local?, to: :account
  53. def remote?
  54. !local?
  55. end
  56. def emojis
  57. @emojis ||= CustomEmoji.from_text(options.join(' '), account.domain)
  58. end
  59. class Option < ActiveModelSerializers::Model
  60. attributes :id, :title, :votes_count, :poll
  61. def initialize(poll, id, title, votes_count)
  62. super(
  63. poll: poll,
  64. id: id,
  65. title: title,
  66. votes_count: votes_count,
  67. )
  68. end
  69. end
  70. def reset_votes!
  71. self.cached_tallies = options.map { 0 }
  72. self.votes_count = 0
  73. self.voters_count = 0
  74. votes.delete_all unless new_record?
  75. end
  76. private
  77. def prepare_cached_tallies
  78. self.cached_tallies = options.map { 0 } if cached_tallies.empty?
  79. end
  80. def prepare_votes_count
  81. self.votes_count = cached_tallies.sum unless cached_tallies.empty?
  82. end
  83. def prepare_options
  84. self.options = options.map(&:strip).compact_blank
  85. end
  86. def reset_parent_cache
  87. return if status_id.nil?
  88. Rails.cache.delete("v3:statuses/#{status_id}")
  89. end
  90. def last_fetched_before_expiration?
  91. last_fetched_at.nil? || expires_at.nil? || last_fetched_at < expires_at
  92. end
  93. def time_passed_since_last_fetch?
  94. last_fetched_at.nil? || last_fetched_at < 1.minute.ago
  95. end
  96. def show_totals_now?
  97. expired? || !hide_totals?
  98. end
  99. end