From 0ebf8b302dfdda86325716dcd3d49f00918fd0d9 Mon Sep 17 00:00:00 2001 From: KMY Date: Thu, 23 Feb 2023 09:14:38 +0900 Subject: [PATCH] Create emoji reactions table --- app/models/emoji_reaction.rb | 44 +++++++++++++++++++ .../20230222232121_create_emoji_reactions.rb | 13 ++++++ ...235218_add_create_at_to_emoji_reactions.rb | 6 +++ db/schema.rb | 16 ++++++- 4 files changed, 78 insertions(+), 1 deletion(-) create mode 100644 app/models/emoji_reaction.rb create mode 100644 db/migrate/20230222232121_create_emoji_reactions.rb create mode 100644 db/migrate/20230222235218_add_create_at_to_emoji_reactions.rb diff --git a/app/models/emoji_reaction.rb b/app/models/emoji_reaction.rb new file mode 100644 index 0000000000..9786412fa3 --- /dev/null +++ b/app/models/emoji_reaction.rb @@ -0,0 +1,44 @@ +# frozen_string_literal: true + +# == Schema Information +# +# Table name: emoji_reactions +# +# id :bigint(8) not null, primary key +# account_id :bigint(8) not null +# status_id :bigint(8) not null +# name :string default(""), not null +# custom_emoji_id :bigint(8) +# uri :string +# created_at :datetime +# updated_at :datetime +# + +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 + end + diff --git a/db/migrate/20230222232121_create_emoji_reactions.rb b/db/migrate/20230222232121_create_emoji_reactions.rb new file mode 100644 index 0000000000..803c48f8ae --- /dev/null +++ b/db/migrate/20230222232121_create_emoji_reactions.rb @@ -0,0 +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 :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.string :uri + + 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 new file mode 100644 index 0000000000..f5530c0da1 --- /dev/null +++ b/db/migrate/20230222235218_add_create_at_to_emoji_reactions.rb @@ -0,0 +1,6 @@ +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 704cef1228..64fa61c855 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 2022_12_06_114142) do +ActiveRecord::Schema.define(version: 2023_02_22_235218) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -411,6 +411,17 @@ ActiveRecord::Schema.define(version: 2022_12_06_114142) do t.index ["domain"], name: "index_email_domain_blocks_on_domain", unique: true end + create_table "emoji_reactions", force: :cascade do |t| + t.bigint "account_id", null: false + t.bigint "status_id", null: false + t.string "name", default: "", null: false + t.bigint "custom_emoji_id" + t.string "uri" + t.datetime "created_at" + t.datetime "updated_at" + t.index ["status_id"], name: "index_emoji_reactions_on_status_id" + end + create_table "encrypted_messages", id: :bigint, default: -> { "timestamp_id('encrypted_messages'::text)" }, force: :cascade do |t| t.bigint "device_id" t.bigint "from_account_id" @@ -1155,6 +1166,9 @@ ActiveRecord::Schema.define(version: 2022_12_06_114142) do add_foreign_key "devices", "accounts", on_delete: :cascade add_foreign_key "devices", "oauth_access_tokens", column: "access_token_id", on_delete: :cascade add_foreign_key "email_domain_blocks", "email_domain_blocks", column: "parent_id", on_delete: :cascade + add_foreign_key "emoji_reactions", "accounts", on_delete: :cascade + add_foreign_key "emoji_reactions", "custom_emojis", on_delete: :cascade + add_foreign_key "emoji_reactions", "statuses", on_delete: :cascade add_foreign_key "encrypted_messages", "accounts", column: "from_account_id", on_delete: :cascade add_foreign_key "encrypted_messages", "devices", on_delete: :cascade add_foreign_key "favourites", "accounts", name: "fk_5eb6c2b873", on_delete: :cascade