2023-10-29 15:00:48 -07:00
|
|
|
import { List as ImmutableList } from 'immutable';
|
2022-06-19 11:38:51 -07:00
|
|
|
|
|
|
|
import { isLoggedIn } from 'soapbox/utils/auth';
|
|
|
|
|
2024-08-04 07:09:52 -07:00
|
|
|
import { getClient } from '../api';
|
2022-06-19 11:38:51 -07:00
|
|
|
|
2024-08-06 10:52:36 -07:00
|
|
|
import { importFetchedStatus } from './importer';
|
2022-06-19 11:38:51 -07:00
|
|
|
import { favourite, unfavourite } from './interactions';
|
|
|
|
|
|
|
|
import type { AppDispatch, RootState } from 'soapbox/store';
|
2024-08-06 10:52:36 -07:00
|
|
|
import type { EmojiReaction, Status } from 'soapbox/types/entities';
|
2022-06-19 11:38:51 -07:00
|
|
|
|
|
|
|
const EMOJI_REACT_REQUEST = 'EMOJI_REACT_REQUEST';
|
|
|
|
const EMOJI_REACT_SUCCESS = 'EMOJI_REACT_SUCCESS';
|
|
|
|
const EMOJI_REACT_FAIL = 'EMOJI_REACT_FAIL';
|
|
|
|
|
|
|
|
const UNEMOJI_REACT_REQUEST = 'UNEMOJI_REACT_REQUEST';
|
|
|
|
const UNEMOJI_REACT_SUCCESS = 'UNEMOJI_REACT_SUCCESS';
|
|
|
|
const UNEMOJI_REACT_FAIL = 'UNEMOJI_REACT_FAIL';
|
|
|
|
|
|
|
|
const noOp = () => () => new Promise(f => f(undefined));
|
|
|
|
|
2023-03-17 16:07:18 -07:00
|
|
|
const simpleEmojiReact = (status: Status, emoji: string, custom?: string) =>
|
2022-06-19 11:38:51 -07:00
|
|
|
(dispatch: AppDispatch) => {
|
2023-10-29 15:00:48 -07:00
|
|
|
const emojiReacts: ImmutableList<EmojiReaction> = status.reactions || ImmutableList();
|
2022-06-19 11:38:51 -07:00
|
|
|
|
|
|
|
if (emoji === '👍' && status.favourited) return dispatch(unfavourite(status));
|
|
|
|
|
2023-10-29 15:00:48 -07:00
|
|
|
const undo = emojiReacts.filter(e => e.me === true && e.name === emoji).count() > 0;
|
2022-06-19 11:38:51 -07:00
|
|
|
if (undo) return dispatch(unEmojiReact(status, emoji));
|
|
|
|
|
|
|
|
return Promise.all([
|
|
|
|
...emojiReacts
|
2023-10-29 15:00:48 -07:00
|
|
|
.filter((emojiReact) => emojiReact.me === true)
|
|
|
|
.map(emojiReact => dispatch(unEmojiReact(status, emojiReact.name))).toArray(),
|
2022-06-19 11:38:51 -07:00
|
|
|
status.favourited && dispatch(unfavourite(status)),
|
|
|
|
]).then(() => {
|
|
|
|
if (emoji === '👍') {
|
|
|
|
dispatch(favourite(status));
|
|
|
|
} else {
|
2023-03-17 16:07:18 -07:00
|
|
|
dispatch(emojiReact(status, emoji, custom));
|
2022-06-19 11:38:51 -07:00
|
|
|
}
|
|
|
|
}).catch(err => {
|
|
|
|
console.error(err);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2023-03-17 16:07:18 -07:00
|
|
|
const emojiReact = (status: Status, emoji: string, custom?: string) =>
|
2022-06-19 11:38:51 -07:00
|
|
|
(dispatch: AppDispatch, getState: () => RootState) => {
|
|
|
|
if (!isLoggedIn(getState)) return dispatch(noOp());
|
|
|
|
|
2023-03-17 16:07:18 -07:00
|
|
|
dispatch(emojiReactRequest(status, emoji, custom));
|
2022-06-19 11:38:51 -07:00
|
|
|
|
2024-08-06 10:52:36 -07:00
|
|
|
return getClient(getState).statuses.createStatusReaction(status.id, emoji).then((response) => {
|
|
|
|
dispatch(importFetchedStatus(response));
|
2024-05-11 14:37:37 -07:00
|
|
|
dispatch(emojiReactSuccess(status, emoji));
|
2024-05-13 10:00:42 -07:00
|
|
|
}).catch((error) => {
|
2024-05-11 14:37:37 -07:00
|
|
|
dispatch(emojiReactFail(status, emoji, error));
|
|
|
|
});
|
2022-06-19 11:38:51 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
const unEmojiReact = (status: Status, emoji: string) =>
|
|
|
|
(dispatch: AppDispatch, getState: () => RootState) => {
|
|
|
|
if (!isLoggedIn(getState)) return dispatch(noOp());
|
|
|
|
|
|
|
|
dispatch(unEmojiReactRequest(status, emoji));
|
|
|
|
|
2024-08-06 10:52:36 -07:00
|
|
|
return getClient(getState).statuses.deleteStatusReaction(status.id, emoji).then(response => {
|
|
|
|
dispatch(importFetchedStatus(response));
|
2024-05-11 14:37:37 -07:00
|
|
|
dispatch(unEmojiReactSuccess(status, emoji));
|
|
|
|
}).catch(error => {
|
|
|
|
dispatch(unEmojiReactFail(status, emoji, error));
|
|
|
|
});
|
2022-06-19 11:38:51 -07:00
|
|
|
};
|
|
|
|
|
2023-03-17 16:07:18 -07:00
|
|
|
const emojiReactRequest = (status: Status, emoji: string, custom?: string) => ({
|
2022-06-19 11:38:51 -07:00
|
|
|
type: EMOJI_REACT_REQUEST,
|
|
|
|
status,
|
|
|
|
emoji,
|
2023-03-17 16:07:18 -07:00
|
|
|
custom,
|
2022-06-19 11:38:51 -07:00
|
|
|
skipLoading: true,
|
|
|
|
});
|
|
|
|
|
|
|
|
const emojiReactSuccess = (status: Status, emoji: string) => ({
|
|
|
|
type: EMOJI_REACT_SUCCESS,
|
|
|
|
status,
|
|
|
|
emoji,
|
|
|
|
skipLoading: true,
|
|
|
|
});
|
|
|
|
|
2023-10-23 15:22:10 -07:00
|
|
|
const emojiReactFail = (status: Status, emoji: string, error: unknown) => ({
|
2022-06-19 11:38:51 -07:00
|
|
|
type: EMOJI_REACT_FAIL,
|
|
|
|
status,
|
|
|
|
emoji,
|
|
|
|
error,
|
|
|
|
skipLoading: true,
|
|
|
|
});
|
|
|
|
|
|
|
|
const unEmojiReactRequest = (status: Status, emoji: string) => ({
|
|
|
|
type: UNEMOJI_REACT_REQUEST,
|
|
|
|
status,
|
|
|
|
emoji,
|
|
|
|
skipLoading: true,
|
|
|
|
});
|
|
|
|
|
|
|
|
const unEmojiReactSuccess = (status: Status, emoji: string) => ({
|
|
|
|
type: UNEMOJI_REACT_SUCCESS,
|
|
|
|
status,
|
|
|
|
emoji,
|
|
|
|
skipLoading: true,
|
|
|
|
});
|
|
|
|
|
2023-10-23 15:22:10 -07:00
|
|
|
const unEmojiReactFail = (status: Status, emoji: string, error: unknown) => ({
|
2022-06-19 11:38:51 -07:00
|
|
|
type: UNEMOJI_REACT_FAIL,
|
|
|
|
status,
|
|
|
|
emoji,
|
|
|
|
error,
|
|
|
|
skipLoading: true,
|
|
|
|
});
|
|
|
|
|
|
|
|
export {
|
|
|
|
EMOJI_REACT_REQUEST,
|
|
|
|
EMOJI_REACT_SUCCESS,
|
|
|
|
EMOJI_REACT_FAIL,
|
|
|
|
UNEMOJI_REACT_REQUEST,
|
|
|
|
UNEMOJI_REACT_SUCCESS,
|
|
|
|
UNEMOJI_REACT_FAIL,
|
|
|
|
simpleEmojiReact,
|
|
|
|
emojiReact,
|
|
|
|
unEmojiReact,
|
|
|
|
emojiReactRequest,
|
|
|
|
emojiReactSuccess,
|
|
|
|
emojiReactFail,
|
|
|
|
unEmojiReactRequest,
|
|
|
|
unEmojiReactSuccess,
|
|
|
|
unEmojiReactFail,
|
|
|
|
};
|