2020-01-23 22:00:13 +01:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
# == Schema Information
|
|
|
|
#
|
|
|
|
# Table name: announcements
|
|
|
|
#
|
|
|
|
# id :bigint(8) not null, primary key
|
|
|
|
# text :text default(""), not null
|
|
|
|
# published :boolean default(FALSE), not null
|
|
|
|
# all_day :boolean default(FALSE), not null
|
|
|
|
# scheduled_at :datetime
|
|
|
|
# starts_at :datetime
|
|
|
|
# ends_at :datetime
|
|
|
|
# created_at :datetime not null
|
|
|
|
# updated_at :datetime not null
|
2020-01-26 22:43:18 +01:00
|
|
|
# published_at :datetime
|
2020-04-15 20:33:24 +02:00
|
|
|
# status_ids :bigint(8) is an Array
|
2020-01-23 22:00:13 +01:00
|
|
|
#
|
|
|
|
|
|
|
|
class Announcement < ApplicationRecord
|
|
|
|
scope :unpublished, -> { where(published: false) }
|
|
|
|
scope :published, -> { where(published: true) }
|
2024-01-08 12:22:16 +01:00
|
|
|
scope :chronological, -> { order(coalesced_chronology_timestamps.asc) }
|
|
|
|
scope :reverse_chronological, -> { order(coalesced_chronology_timestamps.desc) }
|
2020-01-23 22:00:13 +01:00
|
|
|
|
|
|
|
has_many :announcement_mutes, dependent: :destroy
|
|
|
|
has_many :announcement_reactions, dependent: :destroy
|
|
|
|
|
|
|
|
validates :text, presence: true
|
2023-12-29 11:55:50 +01:00
|
|
|
validates :starts_at, presence: true, if: :ends_at?
|
|
|
|
validates :ends_at, presence: true, if: :starts_at?
|
2020-01-23 22:00:13 +01:00
|
|
|
|
2020-01-26 20:07:26 +01:00
|
|
|
before_validation :set_published, on: :create
|
|
|
|
|
2024-01-08 12:22:16 +01:00
|
|
|
class << self
|
|
|
|
def coalesced_chronology_timestamps
|
|
|
|
Arel.sql(
|
|
|
|
<<~SQL.squish
|
|
|
|
COALESCE(announcements.starts_at, announcements.scheduled_at, announcements.published_at, announcements.created_at)
|
|
|
|
SQL
|
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-08-25 20:39:40 +02:00
|
|
|
def to_log_human_identifier
|
|
|
|
text
|
|
|
|
end
|
|
|
|
|
2020-01-27 11:05:33 +01:00
|
|
|
def publish!
|
|
|
|
update!(published: true, published_at: Time.now.utc, scheduled_at: nil)
|
|
|
|
end
|
|
|
|
|
|
|
|
def unpublish!
|
|
|
|
update!(published: false, scheduled_at: nil)
|
|
|
|
end
|
|
|
|
|
2020-01-23 22:00:13 +01:00
|
|
|
def mentions
|
|
|
|
@mentions ||= Account.from_text(text)
|
|
|
|
end
|
|
|
|
|
2020-03-08 16:10:48 +01:00
|
|
|
def statuses
|
2023-02-18 23:09:40 +01:00
|
|
|
@statuses ||= if status_ids.nil?
|
|
|
|
[]
|
|
|
|
else
|
|
|
|
Status.where(id: status_ids, visibility: [:public, :unlisted])
|
|
|
|
end
|
2020-03-08 16:10:48 +01:00
|
|
|
end
|
|
|
|
|
2020-01-23 22:00:13 +01:00
|
|
|
def tags
|
|
|
|
@tags ||= Tag.find_or_create_by_names(Extractor.extract_hashtags(text))
|
|
|
|
end
|
|
|
|
|
|
|
|
def emojis
|
|
|
|
@emojis ||= CustomEmoji.from_text(text)
|
|
|
|
end
|
|
|
|
|
|
|
|
def reactions(account = nil)
|
2024-01-17 10:18:13 +01:00
|
|
|
grouped_ordered_announcement_reactions.select(
|
|
|
|
[:name, :custom_emoji_id, 'COUNT(*) as count'].tap do |values|
|
|
|
|
values << value_for_reaction_me_column(account)
|
2020-01-23 22:00:13 +01:00
|
|
|
end
|
2024-01-17 10:18:13 +01:00
|
|
|
).to_a.tap do |records|
|
|
|
|
ActiveRecord::Associations::Preloader.new(records: records, associations: :custom_emoji).call
|
|
|
|
end
|
2020-01-23 22:00:13 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2024-01-17 10:18:13 +01:00
|
|
|
def grouped_ordered_announcement_reactions
|
|
|
|
announcement_reactions
|
|
|
|
.group(:announcement_id, :name, :custom_emoji_id)
|
|
|
|
.order(
|
|
|
|
Arel.sql('MIN(created_at)').asc
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
def value_for_reaction_me_column(account)
|
|
|
|
if account.nil?
|
|
|
|
'FALSE AS me'
|
|
|
|
else
|
|
|
|
<<~SQL.squish
|
|
|
|
EXISTS(
|
|
|
|
SELECT 1
|
|
|
|
FROM announcement_reactions inner_reactions
|
|
|
|
WHERE inner_reactions.account_id = #{account.id}
|
|
|
|
AND inner_reactions.announcement_id = announcement_reactions.announcement_id
|
|
|
|
AND inner_reactions.name = announcement_reactions.name
|
|
|
|
) AS me
|
|
|
|
SQL
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-01-26 20:07:26 +01:00
|
|
|
def set_published
|
2020-01-26 22:43:18 +01:00
|
|
|
return unless scheduled_at.blank? || scheduled_at.past?
|
|
|
|
|
|
|
|
self.published = true
|
|
|
|
self.published_at = Time.now.utc
|
2020-01-23 22:00:13 +01:00
|
|
|
end
|
|
|
|
end
|