announcement.rb 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. # frozen_string_literal: true
  2. # == Schema Information
  3. #
  4. # Table name: announcements
  5. #
  6. # id :bigint(8) not null, primary key
  7. # text :text default(""), not null
  8. # published :boolean default(FALSE), not null
  9. # all_day :boolean default(FALSE), not null
  10. # scheduled_at :datetime
  11. # starts_at :datetime
  12. # ends_at :datetime
  13. # created_at :datetime not null
  14. # updated_at :datetime not null
  15. # published_at :datetime
  16. # status_ids :bigint(8) is an Array
  17. #
  18. class Announcement < ApplicationRecord
  19. scope :unpublished, -> { where(published: false) }
  20. scope :published, -> { where(published: true) }
  21. scope :chronological, -> { order(coalesced_chronology_timestamps.asc) }
  22. scope :reverse_chronological, -> { order(coalesced_chronology_timestamps.desc) }
  23. has_many :announcement_mutes, dependent: :destroy
  24. has_many :announcement_reactions, dependent: :destroy
  25. validates :text, presence: true
  26. validates :starts_at, presence: true, if: :ends_at?
  27. validates :ends_at, presence: true, if: :starts_at?
  28. before_validation :set_published, on: :create
  29. class << self
  30. def coalesced_chronology_timestamps
  31. Arel.sql(
  32. <<~SQL.squish
  33. COALESCE(announcements.starts_at, announcements.scheduled_at, announcements.published_at, announcements.created_at)
  34. SQL
  35. )
  36. end
  37. end
  38. def to_log_human_identifier
  39. text
  40. end
  41. def publish!
  42. update!(published: true, published_at: Time.now.utc, scheduled_at: nil)
  43. end
  44. def unpublish!
  45. update!(published: false, scheduled_at: nil)
  46. end
  47. def mentions
  48. @mentions ||= Account.from_text(text)
  49. end
  50. def statuses
  51. @statuses ||= if status_ids.nil?
  52. []
  53. else
  54. Status.where(id: status_ids, visibility: [:public, :unlisted])
  55. end
  56. end
  57. def tags
  58. @tags ||= Tag.find_or_create_by_names(Extractor.extract_hashtags(text))
  59. end
  60. def emojis
  61. @emojis ||= CustomEmoji.from_text(text)
  62. end
  63. def reactions(account = nil)
  64. records = begin
  65. scope = announcement_reactions.group(:announcement_id, :name, :custom_emoji_id).order(Arel.sql('MIN(created_at) ASC'))
  66. if account.nil?
  67. scope.select('name, custom_emoji_id, count(*) as count, false as me')
  68. else
  69. scope.select("name, custom_emoji_id, count(*) as count, exists(select 1 from announcement_reactions r where r.account_id = #{account.id} and r.announcement_id = announcement_reactions.announcement_id and r.name = announcement_reactions.name) as me")
  70. end
  71. end.to_a
  72. ActiveRecord::Associations::Preloader.new(records: records, associations: :custom_emoji).call
  73. records
  74. end
  75. private
  76. def set_published
  77. return unless scheduled_at.blank? || scheduled_at.past?
  78. self.published = true
  79. self.published_at = Time.now.utc
  80. end
  81. end