announcement_reaction.rb 994 B

12345678910111213141516171819202122232425262728293031323334353637
  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. after_commit :queue_publish
  16. belongs_to :account
  17. belongs_to :announcement, inverse_of: :announcement_reactions
  18. belongs_to :custom_emoji, optional: true
  19. validates :name, presence: true
  20. validates_with ReactionValidator
  21. before_validation :set_custom_emoji
  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