From 6a251a7dd845a0363f690ae3bc7118b01d009985 Mon Sep 17 00:00:00 2001 From: fef Date: Thu, 24 Nov 2022 11:50:32 +0000 Subject: [PATCH] add backend support for status emoji reactions turns out we can just reuse the code for announcement reactions. --- .../api/v1/statuses/reactions_controller.rb | 29 ++++++++++++++++++ app/models/status.rb | 16 ++++++++++ app/models/status_reaction.rb | 29 ++++++++++++++++++ app/serializers/rest/status_serializer.rb | 5 ++++ app/validators/status_reaction_validator.rb | 28 +++++++++++++++++ config/routes.rb | 1 + .../20221124114030_create_status_reactions.rb | 14 +++++++++ db/schema.rb | 30 ++++++++++++++++++- .../fabricators/status_reaction_fabricator.rb | 6 ++++ spec/models/status_reaction_spec.rb | 5 ++++ 10 files changed, 162 insertions(+), 1 deletion(-) create mode 100644 app/controllers/api/v1/statuses/reactions_controller.rb create mode 100644 app/models/status_reaction.rb create mode 100644 app/validators/status_reaction_validator.rb create mode 100644 db/migrate/20221124114030_create_status_reactions.rb create mode 100644 spec/fabricators/status_reaction_fabricator.rb create mode 100644 spec/models/status_reaction_spec.rb diff --git a/app/controllers/api/v1/statuses/reactions_controller.rb b/app/controllers/api/v1/statuses/reactions_controller.rb new file mode 100644 index 0000000000..9a1bf57079 --- /dev/null +++ b/app/controllers/api/v1/statuses/reactions_controller.rb @@ -0,0 +1,29 @@ +# frozen_string_literal: true + +class Api::V1::Statuses::ReactionsController < Api::BaseController + before_action -> { doorkeeper_authorize! :write, :'write:favourites' } + before_action :require_user! + + before_action :set_status + before_action :set_reaction, except: :update + + def update + @status.status_reactions.create!(account: current_account, name: params[:id]) + render_empty + end + + def destroy + @reaction.destroy! + render_empty + end + + private + + def set_reaction + @reaction = @status.status_reactions.where(account: current_account).find_by!(name: params[:id]) + end + + def set_status + @status = Status.find(params[:status_id]) + end +end diff --git a/app/models/status.rb b/app/models/status.rb index 044816be76..f8eb988c2c 100644 --- a/app/models/status.rb +++ b/app/models/status.rb @@ -70,6 +70,7 @@ class Status < ApplicationRecord has_many :mentions, dependent: :destroy, inverse_of: :status has_many :active_mentions, -> { active }, class_name: 'Mention', inverse_of: :status has_many :media_attachments, dependent: :nullify + has_many :status_reactions, dependent: :destroy has_and_belongs_to_many :tags has_and_belongs_to_many :preview_cards @@ -262,6 +263,21 @@ class Status < ApplicationRecord @emojis = CustomEmoji.from_text(fields.join(' '), account.domain) end + def reactions(account = nil) + records = begin + scope = status_reactions.group(:status_id, :name, :custom_emoji_id).order(Arel.sql('MIN(created_at) ASC')) + + if account.nil? + scope.select('name, custom_emoji_id, count(*) as count, false as me') + else + scope.select("name, custom_emoji_id, count(*) as count, exists(select 1 from status_reactions r where r.account_id = #{account.id} and r.status_id = status_reactions.status_id and r.name = status_reactions.name) as me") + end + end + + ActiveRecord::Associations::Preloader.new.preload(records, :custom_emoji) + records + end + def ordered_media_attachments if ordered_media_attachment_ids.nil? media_attachments diff --git a/app/models/status_reaction.rb b/app/models/status_reaction.rb new file mode 100644 index 0000000000..32cb9edd4a --- /dev/null +++ b/app/models/status_reaction.rb @@ -0,0 +1,29 @@ +# frozen_string_literal: true +# == Schema Information +# +# Table name: status_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) +# created_at :datetime not null +# updated_at :datetime not null +# +class StatusReaction < ApplicationRecord + belongs_to :account + belongs_to :status, inverse_of: :status_reactions + belongs_to :custom_emoji, optional: true + + validates :name, presence: true + validates_with StatusReactionValidator + + before_validation :set_custom_emoji + + private + + def set_custom_emoji + self.custom_emoji = CustomEmoji.local.find_by(disabled: false, shortcode: name) if name.present? + end +end diff --git a/app/serializers/rest/status_serializer.rb b/app/serializers/rest/status_serializer.rb index 659c45b835..43c1e86aff 100644 --- a/app/serializers/rest/status_serializer.rb +++ b/app/serializers/rest/status_serializer.rb @@ -28,6 +28,7 @@ class REST::StatusSerializer < ActiveModel::Serializer has_many :ordered_mentions, key: :mentions has_many :tags has_many :emojis, serializer: REST::CustomEmojiSerializer + has_many :reactions, serializer: REST::ReactionSerializer has_one :preview_card, key: :card, serializer: REST::PreviewCardSerializer has_one :preloadable_poll, key: :poll, serializer: REST::PollSerializer @@ -146,6 +147,10 @@ class REST::StatusSerializer < ActiveModel::Serializer object.active_mentions.to_a.sort_by(&:id) end + def reactions + object.reactions(current_user&.account) + end + class ApplicationSerializer < ActiveModel::Serializer attributes :name, :website diff --git a/app/validators/status_reaction_validator.rb b/app/validators/status_reaction_validator.rb new file mode 100644 index 0000000000..7c1c6983bb --- /dev/null +++ b/app/validators/status_reaction_validator.rb @@ -0,0 +1,28 @@ +# frozen_string_literal: true + +class StatusReactionValidator < ActiveModel::Validator + SUPPORTED_EMOJIS = Oj.load_file(Rails.root.join('app', 'javascript', 'mastodon', 'features', 'emoji', 'emoji_map.json').to_s).keys.freeze + + LIMIT = 8 + + def validate(reaction) + return if reaction.name.blank? + + reaction.errors.add(:name, I18n.t('reactions.errors.unrecognized_emoji')) if reaction.custom_emoji_id.blank? && !unicode_emoji?(reaction.name) + reaction.errors.add(:base, I18n.t('reactions.errors.limit_reached')) if new_reaction?(reaction) && limit_reached?(reaction) + end + + private + + def unicode_emoji?(name) + SUPPORTED_EMOJIS.include?(name) + end + + def new_reaction?(reaction) + !reaction.status.status_reactions.where(name: reaction.name).exists? + end + + def limit_reached?(reaction) + reaction.status.status_reactions.where.not(name: reaction.name).count('distinct name') >= LIMIT + end +end diff --git a/config/routes.rb b/config/routes.rb index 8639f0ef53..da4ddfa200 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -453,6 +453,7 @@ Rails.application.routes.draw do resource :history, only: :show resource :source, only: :show + resources :reactions, only: [:update, :destroy] post :translate, to: 'translations#create' end diff --git a/db/migrate/20221124114030_create_status_reactions.rb b/db/migrate/20221124114030_create_status_reactions.rb new file mode 100644 index 0000000000..bbf1f3376a --- /dev/null +++ b/db/migrate/20221124114030_create_status_reactions.rb @@ -0,0 +1,14 @@ +class CreateStatusReactions < ActiveRecord::Migration[6.1] + def change + create_table :status_reactions do |t| + t.references :account, null: false, foreign_key: true + t.references :status, null: false, foreign_key: true + t.string :name, null: false, default: '' + t.references :custom_emoji, null: true, foreign_key: true + + t.timestamps + end + + add_index :status_reactions, [:account_id, :status_id, :name], unique: true, name: :index_status_reactions_on_account_id_and_status_id + end +end diff --git a/db/schema.rb b/db/schema.rb index 01a95c0f29..b80ce713ed 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_11_04_133904) do +ActiveRecord::Schema.define(version: 2022_11_24_114030) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -781,6 +781,16 @@ ActiveRecord::Schema.define(version: 2022_11_04_133904) do t.index ["status_id", "preview_card_id"], name: "index_preview_cards_statuses_on_status_id_and_preview_card_id" end + create_table "reactions", force: :cascade do |t| + t.string "emoji" + t.bigint "status_id", null: false + t.bigint "account_id", null: false + t.datetime "created_at", precision: 6, null: false + t.datetime "updated_at", precision: 6, null: false + t.index ["account_id"], name: "index_reactions_on_account_id" + t.index ["status_id"], name: "index_reactions_on_status_id" + end + create_table "relays", force: :cascade do |t| t.string "inbox_url", default: "", null: false t.string "follow_activity_id" @@ -897,6 +907,19 @@ ActiveRecord::Schema.define(version: 2022_11_04_133904) do t.index ["status_id"], name: "index_status_pins_on_status_id" end + create_table "status_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.datetime "created_at", precision: 6, null: false + t.datetime "updated_at", precision: 6, null: false + t.index ["account_id", "status_id", "name"], name: "index_status_reactions_on_account_id_and_status_id", unique: true + t.index ["account_id"], name: "index_status_reactions_on_account_id" + t.index ["custom_emoji_id"], name: "index_status_reactions_on_custom_emoji_id" + t.index ["status_id"], name: "index_status_reactions_on_status_id" + end + create_table "status_stats", force: :cascade do |t| t.bigint "status_id", null: false t.bigint "replies_count", default: 0, null: false @@ -1198,6 +1221,8 @@ ActiveRecord::Schema.define(version: 2022_11_04_133904) do add_foreign_key "polls", "accounts", on_delete: :cascade add_foreign_key "polls", "statuses", on_delete: :cascade add_foreign_key "preview_card_trends", "preview_cards", on_delete: :cascade + add_foreign_key "reactions", "accounts" + add_foreign_key "reactions", "statuses" add_foreign_key "report_notes", "accounts", on_delete: :cascade add_foreign_key "report_notes", "reports", on_delete: :cascade add_foreign_key "reports", "accounts", column: "action_taken_by_account_id", name: "fk_bca45b75fd", on_delete: :nullify @@ -1211,6 +1236,9 @@ ActiveRecord::Schema.define(version: 2022_11_04_133904) do add_foreign_key "status_edits", "statuses", on_delete: :cascade add_foreign_key "status_pins", "accounts", name: "fk_d4cb435b62", on_delete: :cascade add_foreign_key "status_pins", "statuses", on_delete: :cascade + add_foreign_key "status_reactions", "accounts" + add_foreign_key "status_reactions", "custom_emojis" + add_foreign_key "status_reactions", "statuses" add_foreign_key "status_stats", "statuses", on_delete: :cascade add_foreign_key "status_trends", "accounts", on_delete: :cascade add_foreign_key "status_trends", "statuses", on_delete: :cascade diff --git a/spec/fabricators/status_reaction_fabricator.rb b/spec/fabricators/status_reaction_fabricator.rb new file mode 100644 index 0000000000..3d4b93efe0 --- /dev/null +++ b/spec/fabricators/status_reaction_fabricator.rb @@ -0,0 +1,6 @@ +Fabricator(:status_reaction) do + account nil + status nil + name "MyString" + custom_emoji nil +end \ No newline at end of file diff --git a/spec/models/status_reaction_spec.rb b/spec/models/status_reaction_spec.rb new file mode 100644 index 0000000000..18860318cc --- /dev/null +++ b/spec/models/status_reaction_spec.rb @@ -0,0 +1,5 @@ +require 'rails_helper' + +RSpec.describe StatusReaction, type: :model do + pending "add some examples to (or delete) #{__FILE__}" +end