turns out we can just reuse the code for announcement reactions.main
parent
ba15694003
commit
7facf831e1
@ -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
|
@ -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
|
@ -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
|
@ -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
|
@ -0,0 +1,6 @@
|
|||||||
|
Fabricator(:status_reaction) do
|
||||||
|
account nil
|
||||||
|
status nil
|
||||||
|
name "MyString"
|
||||||
|
custom_emoji nil
|
||||||
|
end
|
@ -0,0 +1,5 @@
|
|||||||
|
require 'rails_helper'
|
||||||
|
|
||||||
|
RSpec.describe StatusReaction, type: :model do
|
||||||
|
pending "add some examples to (or delete) #{__FILE__}"
|
||||||
|
end
|
Loading…
Reference in new issue