You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

122 lines
3.2 KiB

  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. has_many :voters, -> { group('accounts.id') }, through: :votes, class_name: 'Account', source: :account
  27. has_many :notifications, as: :activity, dependent: :destroy
  28. validates :options, presence: true
  29. validates :expires_at, presence: true, if: :local?
  30. validates_with PollValidator, on: :create, if: :local?
  31. scope :attached, -> { where.not(status_id: nil) }
  32. scope :unattached, -> { where(status_id: nil) }
  33. before_validation :prepare_options, if: :local?
  34. before_validation :prepare_votes_count
  35. after_initialize :prepare_cached_tallies
  36. after_commit :reset_parent_cache, on: :update
  37. def loaded_options
  38. options.map.with_index { |title, key| Option.new(self, key.to_s, title, show_totals_now? ? cached_tallies[key] : nil) }
  39. end
  40. def possibly_stale?
  41. remote? && last_fetched_before_expiration? && time_passed_since_last_fetch?
  42. end
  43. def voted?(account)
  44. account.id == account_id || votes.where(account: account).exists?
  45. end
  46. def own_votes(account)
  47. votes.where(account: account).pluck(:choice)
  48. end
  49. delegate :local?, to: :account
  50. def remote?
  51. !local?
  52. end
  53. def emojis
  54. @emojis ||= CustomEmoji.from_text(options.join(' '), account.domain)
  55. end
  56. class Option < ActiveModelSerializers::Model
  57. attributes :id, :title, :votes_count, :poll
  58. def initialize(poll, id, title, votes_count)
  59. super(
  60. poll: poll,
  61. id: id,
  62. title: title,
  63. votes_count: votes_count,
  64. )
  65. end
  66. end
  67. def reset_votes!
  68. self.cached_tallies = options.map { 0 }
  69. self.votes_count = 0
  70. votes.delete_all unless new_record?
  71. end
  72. private
  73. def prepare_cached_tallies
  74. self.cached_tallies = options.map { 0 } if cached_tallies.empty?
  75. end
  76. def prepare_votes_count
  77. self.votes_count = cached_tallies.sum unless cached_tallies.empty?
  78. end
  79. def prepare_options
  80. self.options = options.map(&:strip).reject(&:blank?)
  81. end
  82. def reset_parent_cache
  83. return if status_id.nil?
  84. Rails.cache.delete("statuses/#{status_id}")
  85. end
  86. def last_fetched_before_expiration?
  87. last_fetched_at.nil? || expires_at.nil? || last_fetched_at < expires_at
  88. end
  89. def time_passed_since_last_fetch?
  90. last_fetched_at.nil? || last_fetched_at < 1.minute.ago
  91. end
  92. def show_totals_now?
  93. expired? || !hide_totals?
  94. end
  95. end