diff --git a/app/models/emoji_reaction.rb b/app/models/emoji_reaction.rb index 9786412fa3..30b83e61ec 100644 --- a/app/models/emoji_reaction.rb +++ b/app/models/emoji_reaction.rb @@ -10,35 +10,34 @@ # name :string default(""), not null # custom_emoji_id :bigint(8) # uri :string -# created_at :datetime -# updated_at :datetime +# created_at :datetime not null +# updated_at :datetime not null # class EmojiReaction < ApplicationRecord - include Paginable - - update_index('statuses', :status) - - belongs_to :account, inverse_of: :emoji_reactions - belongs_to :status, inverse_of: :emoji_reactions - belongs_to :custom_emojis, optional: true - - has_one :notification, as: :activity, dependent: :destroy - - validates :status_id, uniqueness: { scope: :account_id } - - before_validation do - self.status = status.reblog if status&.reblog? - end - - after_destroy :invalidate_cleanup_info - - private - - def invalidate_cleanup_info - return unless status&.account_id == account_id && account.local? - - account.statuses_cleanup_policy&.invalidate_last_inspected(status, :unfav) - end + include Paginable + + update_index('statuses', :status) + + belongs_to :account, inverse_of: :emoji_reactions + belongs_to :status, inverse_of: :emoji_reactions + belongs_to :custom_emojis, optional: true + + has_one :notification, as: :activity, dependent: :destroy + + after_create :refresh_cache + after_destroy :refresh_cache + after_destroy :invalidate_cleanup_info + + private + + def refresh_cache + status&.refresh_emoji_reactions_grouped_by_name! end - + + def invalidate_cleanup_info + return unless status&.account_id == account_id && account.local? + + account.statuses_cleanup_policy&.invalidate_last_inspected(status, :unfav) + end +end diff --git a/app/models/status_stat.rb b/app/models/status_stat.rb index 022c99e2f4..7e46f02769 100644 --- a/app/models/status_stat.rb +++ b/app/models/status_stat.rb @@ -9,9 +9,9 @@ # replies_count :bigint(8) default(0), not null # reblogs_count :bigint(8) default(0), not null # favourites_count :bigint(8) default(0), not null -# emoji_reactions :string # created_at :datetime not null # updated_at :datetime not null +# emoji_reactions :string # class StatusStat < ApplicationRecord diff --git a/app/serializers/rest/emoji_reactions_grouped_by_name_serializer.rb b/app/serializers/rest/emoji_reactions_grouped_by_name_serializer.rb index ac8054179a..c6d21cdaf6 100644 --- a/app/serializers/rest/emoji_reactions_grouped_by_name_serializer.rb +++ b/app/serializers/rest/emoji_reactions_grouped_by_name_serializer.rb @@ -1,31 +1,23 @@ # frozen_string_literal: true -# name: string, -#count: number, -#account_ids: Array, -#me: boolean, -#url: string, -#domain: string - class REST::EmojiReactionsGroupedByNameSerializer < ActiveModel::Serializer - attributes :name, :count - - attribute :me, if: :current_user? - attribute :url, if: :custom_emoji? - attribute :static_url, if: :custom_emoji? - attribute :domain, if: :custom_emoji? - attribute :account_ids, if: :has_account_ids? + attributes :name, :count - def current_user? - !current_user.nil? - end + attribute :me, if: :current_user? + attribute :url, if: :custom_emoji? + attribute :static_url, if: :custom_emoji? + attribute :domain, if: :custom_emoji? + attribute :account_ids, if: :account_ids? - def custom_emoji? - object.respond_to?(:custom_emoji) - end + def current_user? + !current_user.nil? + end - def has_account_ids? - object.respond_to?(:account_ids) - end + def custom_emoji? + object.respond_to?(:custom_emoji) + end + + def account_ids? + object.respond_to?(:account_ids) + end end - \ No newline at end of file diff --git a/db/migrate/20230222232121_create_emoji_reactions.rb b/db/migrate/20230222232121_create_emoji_reactions.rb index 803c48f8ae..7edd6f289e 100644 --- a/db/migrate/20230222232121_create_emoji_reactions.rb +++ b/db/migrate/20230222232121_create_emoji_reactions.rb @@ -1,13 +1,13 @@ class CreateEmojiReactions < ActiveRecord::Migration[6.1] def change create_table :emoji_reactions do |t| - - t.belongs_to :account, null: false, foreign_key: { on_delete: :cascade }, index: false + t.belongs_to :account, null: false, foreign_key: { on_delete: :cascade } t.belongs_to :status, null: false, foreign_key: { on_delete: :cascade } t.string :name, null: false, default: '' - t.belongs_to :custom_emoji, foreign_key: { on_delete: :cascade }, index: false + t.belongs_to :custom_emoji, foreign_key: { on_delete: :cascade } t.string :uri - + t.datetime :created_at, null: false + t.datetime :updated_at, null: false end end end diff --git a/db/migrate/20230222235218_add_create_at_to_emoji_reactions.rb b/db/migrate/20230222235218_add_create_at_to_emoji_reactions.rb deleted file mode 100644 index f5530c0da1..0000000000 --- a/db/migrate/20230222235218_add_create_at_to_emoji_reactions.rb +++ /dev/null @@ -1,6 +0,0 @@ -class AddCreateAtToEmojiReactions < ActiveRecord::Migration[6.1] - def change - add_column :emoji_reactions, :created_at, :timestamp - add_column :emoji_reactions, :updated_at, :timestamp - end -end diff --git a/db/schema.rb b/db/schema.rb index 6804c3af2e..cd085429f2 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -417,8 +417,10 @@ ActiveRecord::Schema.define(version: 2023_02_23_102416) do t.string "name", default: "", null: false t.bigint "custom_emoji_id" t.string "uri" - t.datetime "created_at" - t.datetime "updated_at" + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + t.index ["account_id"], name: "index_emoji_reactions_on_account_id" + t.index ["custom_emoji_id"], name: "index_emoji_reactions_on_custom_emoji_id" t.index ["status_id"], name: "index_emoji_reactions_on_status_id" end