Add emoji reaction deletion api
This commit is contained in:
parent
5f7da7bff1
commit
15bc0df759
14 changed files with 142 additions and 45 deletions
app
controllers/api/v1/statuses
javascript/mastodon
actions
components
features
notifications/containers
status
lib
models
presenters
services
workers
|
@ -6,26 +6,30 @@ class Api::V1::Statuses::EmojiReactionsController < Api::BaseController
|
||||||
before_action -> { doorkeeper_authorize! :write, :'write:emoji_reactions' }
|
before_action -> { doorkeeper_authorize! :write, :'write:emoji_reactions' }
|
||||||
before_action :require_user!
|
before_action :require_user!
|
||||||
before_action :set_status, only: [:update]
|
before_action :set_status, only: [:update]
|
||||||
|
before_action :set_status_without_authorize, only: [:destroy]
|
||||||
|
|
||||||
# For compatible with Fedibird API
|
# For compatible with Fedibird API
|
||||||
def update
|
def update
|
||||||
create_private
|
create_private
|
||||||
end
|
end
|
||||||
|
|
||||||
# TODO: destroy emoji reaction api
|
|
||||||
def destroy
|
def destroy
|
||||||
# fav = current_account.favourites.find_by(status_id: params[:status_id])
|
emoji = params[:emoji]
|
||||||
|
|
||||||
# if fav
|
if emoji
|
||||||
# @status = fav.status
|
shortcode, domain = emoji.split('@')
|
||||||
# UnfavouriteWorker.perform_async(current_account.id, @status.id)
|
emoji_reaction = EmojiReaction.where(account_id: current_account.id).where(status_id: @status.id).where(name: shortcode)
|
||||||
# else
|
.find { |reaction| domain == '' ? reaction.custom_emoji.nil? : reaction.custom_emoji&.domain == domain }
|
||||||
# @status = Status.find(params[:status_id])
|
|
||||||
# authorize @status, :show?
|
|
||||||
# end
|
|
||||||
|
|
||||||
# render json: @status, serializer: REST::StatusSerializer, relationships: StatusRelationshipsPresenter.new([@status], current_account.id, favourites_map: { @status.id => false })
|
authorize @status, :show? if emoji_reaction.nil?
|
||||||
# rescue Mastodon::NotPermittedError
|
end
|
||||||
|
|
||||||
|
UnEmojiReactWorker.perform_async(current_account.id, @status.id, emoji)
|
||||||
|
|
||||||
|
render json: @status, serializer: REST::StatusSerializer, relationships: StatusRelationshipsPresenter.new(
|
||||||
|
[@status], current_account.id, emoji_reactions_map: { @status.id => false }
|
||||||
|
)
|
||||||
|
rescue Mastodon::NotPermittedError
|
||||||
not_found
|
not_found
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -37,9 +41,13 @@ class Api::V1::Statuses::EmojiReactionsController < Api::BaseController
|
||||||
end
|
end
|
||||||
|
|
||||||
def set_status
|
def set_status
|
||||||
@status = Status.find(params[:status_id])
|
set_status_without_authorize
|
||||||
authorize @status, :show?
|
authorize @status, :show?
|
||||||
rescue Mastodon::NotPermittedError
|
rescue Mastodon::NotPermittedError
|
||||||
not_found
|
not_found
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def set_status_without_authorize
|
||||||
|
@status = Status.find(params[:status_id])
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -206,7 +206,6 @@ export function unfavouriteFail(status, error) {
|
||||||
export function emojiReact(status, emoji) {
|
export function emojiReact(status, emoji) {
|
||||||
return function (dispatch, getState) {
|
return function (dispatch, getState) {
|
||||||
dispatch(emojiReactRequest(status, emoji));
|
dispatch(emojiReactRequest(status, emoji));
|
||||||
console.dir(emoji.custom ? (emoji.name + (emoji.domain || '')) : emoji.native);
|
|
||||||
|
|
||||||
api(getState).put(`/api/v1/statuses/${status.get('id')}/emoji_reactions/${emoji.custom ? (emoji.name + (emoji.domain || '')) : emoji.native}`).then(function (response) {
|
api(getState).put(`/api/v1/statuses/${status.get('id')}/emoji_reactions/${emoji.custom ? (emoji.name + (emoji.domain || '')) : emoji.native}`).then(function (response) {
|
||||||
dispatch(importFetchedStatus(response.data));
|
dispatch(importFetchedStatus(response.data));
|
||||||
|
@ -221,7 +220,7 @@ export function unEmojiReact(status, emoji) {
|
||||||
return (dispatch, getState) => {
|
return (dispatch, getState) => {
|
||||||
dispatch(unEmojiReactRequest(status, emoji));
|
dispatch(unEmojiReactRequest(status, emoji));
|
||||||
|
|
||||||
api(getState).post(`/api/v1/statuses/${status.get('id')}/emoji_unreactions/${emoji.native}`).then(response => {
|
api(getState).post(`/api/v1/statuses/${status.get('id')}/emoji_unreaction`, { emoji }).then(response => {
|
||||||
dispatch(importFetchedStatus(response.data));
|
dispatch(importFetchedStatus(response.data));
|
||||||
dispatch(unEmojiReactSuccess(status, emoji));
|
dispatch(unEmojiReactSuccess(status, emoji));
|
||||||
}).catch(error => {
|
}).catch(error => {
|
||||||
|
|
|
@ -511,7 +511,7 @@ class Status extends ImmutablePureComponent {
|
||||||
let emojiReactionsBar = null;
|
let emojiReactionsBar = null;
|
||||||
if (status.get('emoji_reactions')) {
|
if (status.get('emoji_reactions')) {
|
||||||
const emojiReactions = status.get('emoji_reactions');
|
const emojiReactions = status.get('emoji_reactions');
|
||||||
emojiReactionsBar = <StatusEmojiReactionsBar emojiReactions={emojiReactions} statusId={status.get('id')} />;
|
emojiReactionsBar = <StatusEmojiReactionsBar emojiReactions={emojiReactions} status={status} onEmojiReact={this.props.onEmojiReact} onUnEmojiReact={this.props.onUnEmojiReact} />;
|
||||||
}
|
}
|
||||||
|
|
||||||
return (
|
return (
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
import React from 'react';
|
import React from 'react';
|
||||||
import ImmutablePropTypes from 'react-immutable-proptypes';
|
import ImmutablePropTypes from 'react-immutable-proptypes';
|
||||||
import PropTypes from 'prop-types';
|
import PropTypes from 'prop-types';
|
||||||
import { FormattedMessage, injectIntl } from 'react-intl';
|
import { injectIntl } from 'react-intl';
|
||||||
import emojify from '../features/emoji/emoji';
|
import emojify from '../features/emoji/emoji';
|
||||||
import classNames from 'classnames';
|
import classNames from 'classnames';
|
||||||
|
|
||||||
|
@ -9,11 +9,25 @@ class EmojiReactionButton extends React.PureComponent {
|
||||||
|
|
||||||
static propTypes = {
|
static propTypes = {
|
||||||
name: PropTypes.string,
|
name: PropTypes.string,
|
||||||
|
domain: PropTypes.string,
|
||||||
url: PropTypes.string,
|
url: PropTypes.string,
|
||||||
staticUrl: PropTypes.string,
|
staticUrl: PropTypes.string,
|
||||||
count: PropTypes.number.isRequired,
|
count: PropTypes.number.isRequired,
|
||||||
me: PropTypes.bool,
|
me: PropTypes.bool,
|
||||||
onClick: PropTypes.func,
|
status: PropTypes.map,
|
||||||
|
onEmojiReact: PropTypes.func,
|
||||||
|
onUnEmojiReact: PropTypes.func,
|
||||||
|
};
|
||||||
|
|
||||||
|
onClick = () => {
|
||||||
|
const { name, domain, me } = this.props;
|
||||||
|
|
||||||
|
const nameParameter = domain ? `${name}@${domain}` : name;
|
||||||
|
if (me) {
|
||||||
|
if (this.props.onUnEmojiReact) this.props.onUnEmojiReact(nameParameter);
|
||||||
|
} else {
|
||||||
|
if (this.props.onEmojiReact) this.props.onEmojiReact(nameParameter);
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
render () {
|
render () {
|
||||||
|
@ -34,7 +48,7 @@ class EmojiReactionButton extends React.PureComponent {
|
||||||
};
|
};
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<button className={classNames(classList)} type='button'>
|
<button className={classNames(classList)} type='button' onClick={this.onClick}>
|
||||||
<span className='emoji' dangerouslySetInnerHTML={{ __html: emojiHtml }} />
|
<span className='emoji' dangerouslySetInnerHTML={{ __html: emojiHtml }} />
|
||||||
<span className='count'>{count}</span>
|
<span className='count'>{count}</span>
|
||||||
</button>
|
</button>
|
||||||
|
@ -48,11 +62,23 @@ class StatusEmojiReactionsBar extends React.PureComponent {
|
||||||
|
|
||||||
static propTypes = {
|
static propTypes = {
|
||||||
emojiReactions: ImmutablePropTypes.list.isRequired,
|
emojiReactions: ImmutablePropTypes.list.isRequired,
|
||||||
statusId: PropTypes.string,
|
status: ImmutablePropTypes.map,
|
||||||
|
onEmojiReact: PropTypes.func,
|
||||||
|
onUnEmojiReact: PropTypes.func,
|
||||||
|
};
|
||||||
|
|
||||||
|
onEmojiReact = (name) => {
|
||||||
|
if (!this.props.onEmojiReact) return;
|
||||||
|
this.props.onEmojiReact(this.props.status, name);
|
||||||
|
};
|
||||||
|
|
||||||
|
onUnEmojiReact = (name) => {
|
||||||
|
if (!this.props.onUnEmojiReact) return;
|
||||||
|
this.props.onUnEmojiReact(this.props.status, name);
|
||||||
};
|
};
|
||||||
|
|
||||||
render () {
|
render () {
|
||||||
const { emojiReactions, statusId } = this.props;
|
const { emojiReactions } = this.props;
|
||||||
|
|
||||||
const emojiButtons = Array.from(emojiReactions).map((emoji, index) => (
|
const emojiButtons = Array.from(emojiReactions).map((emoji, index) => (
|
||||||
<EmojiReactionButton
|
<EmojiReactionButton
|
||||||
|
@ -62,6 +88,9 @@ class StatusEmojiReactionsBar extends React.PureComponent {
|
||||||
me={emoji.get('me')}
|
me={emoji.get('me')}
|
||||||
url={emoji.get('url')}
|
url={emoji.get('url')}
|
||||||
staticUrl={emoji.get('static_url')}
|
staticUrl={emoji.get('static_url')}
|
||||||
|
domain={emoji.get('domain')}
|
||||||
|
onEmojiReact={this.onEmojiReact}
|
||||||
|
onUnEmojiReact={this.onUnEmojiReact}
|
||||||
/>));
|
/>));
|
||||||
|
|
||||||
return (
|
return (
|
||||||
|
|
|
@ -8,6 +8,7 @@ import {
|
||||||
favourite,
|
favourite,
|
||||||
unreblog,
|
unreblog,
|
||||||
unfavourite,
|
unfavourite,
|
||||||
|
emojiReact,
|
||||||
} from '../../../actions/interactions';
|
} from '../../../actions/interactions';
|
||||||
import {
|
import {
|
||||||
hideStatus,
|
hideStatus,
|
||||||
|
@ -62,7 +63,7 @@ const mapDispatchToProps = dispatch => ({
|
||||||
},
|
},
|
||||||
|
|
||||||
onEmojiReact (status, emoji) {
|
onEmojiReact (status, emoji) {
|
||||||
|
dispatch(emojiReact(status, emoji));
|
||||||
},
|
},
|
||||||
|
|
||||||
onToggleHidden (status) {
|
onToggleHidden (status) {
|
||||||
|
|
|
@ -63,6 +63,7 @@ class ActionBar extends React.PureComponent {
|
||||||
onReply: PropTypes.func.isRequired,
|
onReply: PropTypes.func.isRequired,
|
||||||
onReblog: PropTypes.func.isRequired,
|
onReblog: PropTypes.func.isRequired,
|
||||||
onFavourite: PropTypes.func.isRequired,
|
onFavourite: PropTypes.func.isRequired,
|
||||||
|
onEmojiReact: PropTypes.func.isRequired,
|
||||||
onBookmark: PropTypes.func.isRequired,
|
onBookmark: PropTypes.func.isRequired,
|
||||||
onDelete: PropTypes.func.isRequired,
|
onDelete: PropTypes.func.isRequired,
|
||||||
onEdit: PropTypes.func.isRequired,
|
onEdit: PropTypes.func.isRequired,
|
||||||
|
@ -182,13 +183,7 @@ class ActionBar extends React.PureComponent {
|
||||||
};
|
};
|
||||||
|
|
||||||
handleEmojiPick = (data) => {
|
handleEmojiPick = (data) => {
|
||||||
/*
|
this.props.onEmojiReact(this.props.status, data);
|
||||||
const { text } = this.props;
|
|
||||||
const position = this.autosuggestTextarea.textarea.selectionStart;
|
|
||||||
const needsSpace = data.custom && position > 0 && !allowedAroundShortCode.includes(text[position - 1]);
|
|
||||||
|
|
||||||
this.props.onPickEmoji(position, data, needsSpace);
|
|
||||||
*/
|
|
||||||
};
|
};
|
||||||
|
|
||||||
render () {
|
render () {
|
||||||
|
|
|
@ -191,7 +191,7 @@ class DetailedStatus extends ImmutablePureComponent {
|
||||||
let emojiReactionsBar = null;
|
let emojiReactionsBar = null;
|
||||||
if (status.get('emoji_reactions')) {
|
if (status.get('emoji_reactions')) {
|
||||||
const emojiReactions = status.get('emoji_reactions');
|
const emojiReactions = status.get('emoji_reactions');
|
||||||
emojiReactionsBar = <StatusEmojiReactionsBar emojiReactions={emojiReactions} statusId={status.get('id')} />;
|
emojiReactionsBar = <StatusEmojiReactionsBar emojiReactions={emojiReactions} status={status} onEmojiReaction={this.props.onEmojiReaction} OnUnEmojiReaction={this.props.OnUnEmojiReaction} />;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (status.get('application')) {
|
if (status.get('application')) {
|
||||||
|
|
|
@ -13,6 +13,7 @@ import {
|
||||||
unfavourite,
|
unfavourite,
|
||||||
pin,
|
pin,
|
||||||
unpin,
|
unpin,
|
||||||
|
emojiReact,
|
||||||
} from '../../../actions/interactions';
|
} from '../../../actions/interactions';
|
||||||
import {
|
import {
|
||||||
muteStatus,
|
muteStatus,
|
||||||
|
@ -94,7 +95,7 @@ const mapDispatchToProps = (dispatch, { intl }) => ({
|
||||||
},
|
},
|
||||||
|
|
||||||
onEmojiReact (status, emoji) {
|
onEmojiReact (status, emoji) {
|
||||||
|
dispatch(emojiReact(status, emoji));
|
||||||
},
|
},
|
||||||
|
|
||||||
onPin (status) {
|
onPin (status) {
|
||||||
|
|
|
@ -6,7 +6,7 @@ class PotentialFriendshipTracker
|
||||||
|
|
||||||
WEIGHTS = {
|
WEIGHTS = {
|
||||||
reply: 1,
|
reply: 1,
|
||||||
emoji_reaction: 5,
|
emoji_reaction: 2,
|
||||||
favourite: 10,
|
favourite: 10,
|
||||||
reblog: 20,
|
reblog: 20,
|
||||||
}.freeze
|
}.freeze
|
||||||
|
|
|
@ -369,6 +369,10 @@ class Status < ApplicationRecord
|
||||||
StatusPin.select('status_id').where(status_id: status_ids).where(account_id: account_id).each_with_object({}) { |p, h| h[p.status_id] = true }
|
StatusPin.select('status_id').where(status_id: status_ids).where(account_id: account_id).each_with_object({}) { |p, h| h[p.status_id] = true }
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def emoji_reactions_map(status_ids, account_id)
|
||||||
|
EmojiReaction.select('status_id').where(status_id: status_ids).where(account_id: account_id).each_with_object({}) { |e, h| h[e.status_id] = true }
|
||||||
|
end
|
||||||
|
|
||||||
def reload_stale_associations!(cached_items)
|
def reload_stale_associations!(cached_items)
|
||||||
account_ids = []
|
account_ids = []
|
||||||
|
|
||||||
|
|
|
@ -2,7 +2,7 @@
|
||||||
|
|
||||||
class StatusRelationshipsPresenter
|
class StatusRelationshipsPresenter
|
||||||
attr_reader :reblogs_map, :favourites_map, :mutes_map, :pins_map,
|
attr_reader :reblogs_map, :favourites_map, :mutes_map, :pins_map,
|
||||||
:bookmarks_map, :filters_map
|
:bookmarks_map, :filters_map, :emoji_reactions_map
|
||||||
|
|
||||||
def initialize(statuses, current_account_id = nil, **options)
|
def initialize(statuses, current_account_id = nil, **options)
|
||||||
if current_account_id.nil?
|
if current_account_id.nil?
|
||||||
|
@ -12,6 +12,7 @@ class StatusRelationshipsPresenter
|
||||||
@mutes_map = {}
|
@mutes_map = {}
|
||||||
@pins_map = {}
|
@pins_map = {}
|
||||||
@filters_map = {}
|
@filters_map = {}
|
||||||
|
@emoji_reactions_map = {}
|
||||||
else
|
else
|
||||||
statuses = statuses.compact
|
statuses = statuses.compact
|
||||||
status_ids = statuses.flat_map { |s| [s.id, s.reblog_of_id] }.uniq.compact
|
status_ids = statuses.flat_map { |s| [s.id, s.reblog_of_id] }.uniq.compact
|
||||||
|
@ -24,6 +25,7 @@ class StatusRelationshipsPresenter
|
||||||
@bookmarks_map = Status.bookmarks_map(status_ids, current_account_id).merge(options[:bookmarks_map] || {})
|
@bookmarks_map = Status.bookmarks_map(status_ids, current_account_id).merge(options[:bookmarks_map] || {})
|
||||||
@mutes_map = Status.mutes_map(conversation_ids, current_account_id).merge(options[:mutes_map] || {})
|
@mutes_map = Status.mutes_map(conversation_ids, current_account_id).merge(options[:mutes_map] || {})
|
||||||
@pins_map = Status.pins_map(pinnable_status_ids, current_account_id).merge(options[:pins_map] || {})
|
@pins_map = Status.pins_map(pinnable_status_ids, current_account_id).merge(options[:pins_map] || {})
|
||||||
|
@emoji_reactions_map = Status.emoji_reactions_map(status_ids, current_account_id).merge(options[:emoji_reactions_map] || {})
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -49,7 +49,7 @@ class EmojiReactService < BaseService
|
||||||
|
|
||||||
return unless status.account.local?
|
return unless status.account.local?
|
||||||
|
|
||||||
ActivityPub::RawDistributionWorker.perform_async(emoji_reaction, status.account_id)
|
ActivityPub::RawDistributionWorker.perform_async(build_json(emoji_reaction), status.account_id)
|
||||||
end
|
end
|
||||||
|
|
||||||
def broadcast_updates!(emoji_reaction)
|
def broadcast_updates!(emoji_reaction)
|
||||||
|
|
39
app/services/un_emoji_react_service.rb
Normal file
39
app/services/un_emoji_react_service.rb
Normal file
|
@ -0,0 +1,39 @@
|
||||||
|
# frozen_string_literal: true
|
||||||
|
|
||||||
|
class UnEmojiReactService < BaseService
|
||||||
|
include Payloadable
|
||||||
|
|
||||||
|
def call(account, status, emoji_reaction = nil)
|
||||||
|
if emoji_reaction
|
||||||
|
emoji_reaction.destroy!
|
||||||
|
create_notification(emoji_reaction) if !status.account.local? && status.account.activitypub?
|
||||||
|
notify_to_followers(emoji_reaction) if status.account.local?
|
||||||
|
else
|
||||||
|
bulk(account, status)
|
||||||
|
end
|
||||||
|
emoji_reaction
|
||||||
|
end
|
||||||
|
|
||||||
|
private
|
||||||
|
|
||||||
|
def bulk(account, status)
|
||||||
|
EmojiReaction.where(account: account).where(status: status).tap do |emoji_reaction|
|
||||||
|
call(account, status, emoji_reaction)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def create_notification(emoji_reaction)
|
||||||
|
status = emoji_reaction.status
|
||||||
|
ActivityPub::DeliveryWorker.perform_async(build_json(emoji_reaction), status.account_id, status.account.inbox_url)
|
||||||
|
end
|
||||||
|
|
||||||
|
def notify_to_followers(emoji_reaction)
|
||||||
|
status = emoji_reaction.status
|
||||||
|
ActivityPub::RawDistributionWorker.perform_async(build_json(emoji_reaction), status.account_id)
|
||||||
|
end
|
||||||
|
|
||||||
|
def build_json(emoji_reaction)
|
||||||
|
# TODO: change to original serializer for other servers
|
||||||
|
Oj.dump(serialize_payload(emoji_reaction, ActivityPub::UndoLikeSerializer))
|
||||||
|
end
|
||||||
|
end
|
19
app/workers/un_emoji_react_worker.rb
Normal file
19
app/workers/un_emoji_react_worker.rb
Normal file
|
@ -0,0 +1,19 @@
|
||||||
|
# frozen_string_literal: true
|
||||||
|
|
||||||
|
class UnEmojiReactWorker
|
||||||
|
include Sidekiq::Worker
|
||||||
|
|
||||||
|
def perform(account_id, status_id, emoji = nil)
|
||||||
|
emoji_reaction = nil
|
||||||
|
|
||||||
|
if emoji
|
||||||
|
shortcode, domain = emoji.split('@')
|
||||||
|
emoji_reaction = EmojiReaction.where(account_id: account_id).where(status_id: status_id).where(name: shortcode)
|
||||||
|
.find { |reaction| domain == '' ? reaction.custom_emoji.nil? : reaction.custom_emoji&.domain == domain }
|
||||||
|
end
|
||||||
|
|
||||||
|
UnEmojiReactService.new.call(Account.find(account_id), Status.find(status_id), emoji_reaction)
|
||||||
|
rescue ActiveRecord::RecordNotFound
|
||||||
|
true
|
||||||
|
end
|
||||||
|
end
|
Loading…
Add table
Add a link
Reference in a new issue