From 9ad51e001e0be26a31d3c87a82c084f4a72fffee Mon Sep 17 00:00:00 2001 From: KMY Date: Mon, 27 Feb 2023 08:45:11 +0900 Subject: [PATCH] Add emoji_reactioned_by_slim api (not work) --- ..._reactioned_by_accounts_slim_controller.rb | 28 +++++++++++++++++++ .../features/emoji_reactions/index.jsx | 1 - .../mastodon/reducers/user_lists.js | 2 -- app/models/status.rb | 7 +++++ ...oji_reactions_grouped_by_account_object.rb | 14 ++++++++++ ...oji_reacted_by_slim_reaction_serializer.rb | 27 ++++++++++++++++++ .../rest/emoji_reacted_by_slim_serializer.rb | 21 ++++++++++++++ config/routes.rb | 1 + 8 files changed, 98 insertions(+), 3 deletions(-) create mode 100644 app/controllers/api/v1/statuses/emoji_reactioned_by_accounts_slim_controller.rb create mode 100644 app/models/status_emoji_reactions_grouped_by_account_object.rb create mode 100644 app/serializers/rest/emoji_reacted_by_slim_reaction_serializer.rb create mode 100644 app/serializers/rest/emoji_reacted_by_slim_serializer.rb diff --git a/app/controllers/api/v1/statuses/emoji_reactioned_by_accounts_slim_controller.rb b/app/controllers/api/v1/statuses/emoji_reactioned_by_accounts_slim_controller.rb new file mode 100644 index 0000000000..2220174527 --- /dev/null +++ b/app/controllers/api/v1/statuses/emoji_reactioned_by_accounts_slim_controller.rb @@ -0,0 +1,28 @@ +# frozen_string_literal: true + +class Api::V1::Statuses::EmojiReactionedByAccountsSlimController < Api::BaseController + include Authorization + + before_action -> { authorize_if_got_token! :read, :'read:accounts' } + before_action :set_status + + def index + @accounts = load_emoji_reactions + + # TODO for serialize hash object + render json: @accounts, each_serializer: REST::EmojiReactedBySlimSerializer + end + + private + + def load_emoji_reactions + @status.generate_emoji_reactions_grouped_by_account + end + + def set_status + @status = Status.find(params[:status_id]) + authorize @status, :show? + rescue Mastodon::NotPermittedError + not_found + end +end diff --git a/app/javascript/mastodon/features/emoji_reactions/index.jsx b/app/javascript/mastodon/features/emoji_reactions/index.jsx index 9ea4c11f52..821bc601ef 100644 --- a/app/javascript/mastodon/features/emoji_reactions/index.jsx +++ b/app/javascript/mastodon/features/emoji_reactions/index.jsx @@ -69,7 +69,6 @@ class EmojiReactions extends ImmutablePureComponent { if (!groups[key]) groups[key] = [value]; else groups[key].push(value); } - console.dir(groups) const emptyMessage = ; diff --git a/app/javascript/mastodon/reducers/user_lists.js b/app/javascript/mastodon/reducers/user_lists.js index b57cbe3823..86f95bed1b 100644 --- a/app/javascript/mastodon/reducers/user_lists.js +++ b/app/javascript/mastodon/reducers/user_lists.js @@ -136,8 +136,6 @@ export default function userLists(state = initialState, action) { case FAVOURITES_FETCH_SUCCESS: return state.setIn(['favourited_by', action.id], ImmutableList(action.accounts.map(item => item.id))); case EMOJI_REACTIONS_FETCH_SUCCESS: - console.log('===================') - console.dir(state); return state.setIn(['emoji_reactioned_by', action.id], ImmutableList(action.accounts)); case NOTIFICATIONS_UPDATE: return action.notification.type === 'follow_request' ? normalizeFollowRequest(state, action.notification) : state; diff --git a/app/models/status.rb b/app/models/status.rb index 5e1606e2a1..3bccb5faa6 100644 --- a/app/models/status.rb +++ b/app/models/status.rb @@ -29,6 +29,8 @@ # ordered_media_attachment_ids :bigint(8) is an Array # +require 'ostruct' + class Status < ApplicationRecord before_destroy :unlink_from_conversations! @@ -312,6 +314,11 @@ class Status < ApplicationRecord end end + def generate_emoji_reactions_grouped_by_account + # TODO for serializer + EmojiReaction.where(status_id: id).group_by(&:account) + end + def trendable? if attributes['trendable'].nil? account.trendable? diff --git a/app/models/status_emoji_reactions_grouped_by_account_object.rb b/app/models/status_emoji_reactions_grouped_by_account_object.rb new file mode 100644 index 0000000000..4dd33d805a --- /dev/null +++ b/app/models/status_emoji_reactions_grouped_by_account_object.rb @@ -0,0 +1,14 @@ +class StatusEmojiReactionsGroupedByAccountObject + def initialize(account, emoji_reactions) + @account = account + @emoji_reactions = emoji_reactions + end + + def read_attribute_for_serialization(_) + REST::EmojiReactedBySlimSerializer + end + + def model_name + 'EmojiReaction' + end +end diff --git a/app/serializers/rest/emoji_reacted_by_slim_reaction_serializer.rb b/app/serializers/rest/emoji_reacted_by_slim_reaction_serializer.rb new file mode 100644 index 0000000000..234218f2e0 --- /dev/null +++ b/app/serializers/rest/emoji_reacted_by_slim_reaction_serializer.rb @@ -0,0 +1,27 @@ +# frozen_string_literal: true + +class REST::EmojiReactedBySlimReactionSerializer < ActiveModel::Serializer + include RoutingHelper + + attributes :name + + attribute :url, if: :custom_emoji? + attribute :static_url, if: :custom_emoji? + attribute :domain, if: :custom_emoji? + + def url + full_asset_url(object.custom_emoji.image.url) + end + + def static_url + full_asset_url(object.custom_emoji.image.url(:static)) + end + + def domain + object.custom_emoji.domain + end + + def custom_emoji? + object.custom_emoji.present? + end +end diff --git a/app/serializers/rest/emoji_reacted_by_slim_serializer.rb b/app/serializers/rest/emoji_reacted_by_slim_serializer.rb new file mode 100644 index 0000000000..6a0888d116 --- /dev/null +++ b/app/serializers/rest/emoji_reacted_by_slim_serializer.rb @@ -0,0 +1,21 @@ +# frozen_string_literal: true + +class REST::EmojiReactedBySlimSerializer < ActiveModel::Serializer + + belongs_to :account, serializer: REST::AccountSerializer + has_many :emoji_reactions, serializer: REST::EmojiReactedBySlimReactionSerializer + + def attributes + { account => emoji_reactions } + end + + private + + def account + object.first + end + + def emoji_reactions + object.last + end +end diff --git a/config/routes.rb b/config/routes.rb index 55654695d0..51cf7f72e1 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -441,6 +441,7 @@ Rails.application.routes.draw do resources :reblogged_by, controller: :reblogged_by_accounts, only: :index resources :favourited_by, controller: :favourited_by_accounts, only: :index resources :emoji_reactioned_by, controller: :emoji_reactioned_by_accounts, only: :index + resources :emoji_reactioned_by_slim, controller: :emoji_reactioned_by_accounts_slim, only: :index resource :reblog, only: :create post :unreblog, to: 'reblogs#destroy'