announcement_reaction.rb 993 B

123456789101112131415161718192021222324252627282930313233343536
  1. # frozen_string_literal: true
  2. # == Schema Information
  3. #
  4. # Table name: announcement_reactions
  5. #
  6. # id :bigint(8) not null, primary key
  7. # account_id :bigint(8)
  8. # announcement_id :bigint(8)
  9. # name :string default(""), not null
  10. # custom_emoji_id :bigint(8)
  11. # created_at :datetime not null
  12. # updated_at :datetime not null
  13. #
  14. class AnnouncementReaction < ApplicationRecord
  15. before_validation :set_custom_emoji
  16. after_commit :queue_publish
  17. belongs_to :account
  18. belongs_to :announcement, inverse_of: :announcement_reactions
  19. belongs_to :custom_emoji, optional: true
  20. validates :name, presence: true
  21. validates_with ReactionValidator
  22. private
  23. def set_custom_emoji
  24. self.custom_emoji = CustomEmoji.local.find_by(disabled: false, shortcode: name) if name.present?
  25. end
  26. def queue_publish
  27. PublishAnnouncementReactionWorker.perform_async(announcement_id, name) unless announcement.destroyed?
  28. end
  29. end