2024-08-11 01:48:58 -07:00
|
|
|
import { Map as ImmutableMap } from 'immutable';
|
2024-08-18 08:50:56 -07:00
|
|
|
import omit from 'lodash/omit';
|
2022-01-10 14:25:06 -08:00
|
|
|
|
2024-08-18 08:50:56 -07:00
|
|
|
import { normalizeStatus, normalizeTranslation, Status as StatusRecord } from 'soapbox/normalizers';
|
2022-11-15 12:46:23 -08:00
|
|
|
import { simulateEmojiReact, simulateUnEmojiReact } from 'soapbox/utils/emoji-reacts';
|
2022-01-10 14:25:06 -08:00
|
|
|
|
2024-08-24 06:01:17 -07:00
|
|
|
import { EMOJI_REACT_REQUEST, UNEMOJI_REACT_REQUEST } from '../actions/emoji-reacts';
|
2022-01-10 14:17:52 -08:00
|
|
|
import {
|
2022-09-07 12:24:10 -07:00
|
|
|
EVENT_JOIN_REQUEST,
|
|
|
|
EVENT_JOIN_FAIL,
|
|
|
|
EVENT_LEAVE_REQUEST,
|
|
|
|
EVENT_LEAVE_FAIL,
|
|
|
|
} from '../actions/events';
|
2022-01-10 14:17:52 -08:00
|
|
|
import { STATUS_IMPORT, STATUSES_IMPORT } from '../actions/importer';
|
2020-03-27 13:59:38 -07:00
|
|
|
import {
|
|
|
|
REBLOG_REQUEST,
|
|
|
|
REBLOG_FAIL,
|
2022-01-04 07:57:01 -08:00
|
|
|
UNREBLOG_REQUEST,
|
|
|
|
UNREBLOG_FAIL,
|
2020-03-27 13:59:38 -07:00
|
|
|
FAVOURITE_REQUEST,
|
2020-09-27 15:24:55 -07:00
|
|
|
UNFAVOURITE_REQUEST,
|
2020-03-27 13:59:38 -07:00
|
|
|
FAVOURITE_FAIL,
|
2023-03-25 15:16:40 -07:00
|
|
|
DISLIKE_REQUEST,
|
|
|
|
UNDISLIKE_REQUEST,
|
|
|
|
DISLIKE_FAIL,
|
2020-03-27 13:59:38 -07:00
|
|
|
} from '../actions/interactions';
|
|
|
|
import {
|
2021-11-12 10:18:11 -08:00
|
|
|
STATUS_CREATE_REQUEST,
|
|
|
|
STATUS_CREATE_FAIL,
|
2022-06-01 05:34:36 -07:00
|
|
|
STATUS_DELETE_REQUEST,
|
|
|
|
STATUS_DELETE_FAIL,
|
2024-08-23 15:27:40 -07:00
|
|
|
STATUS_HIDE_MEDIA,
|
2024-07-11 05:22:14 -07:00
|
|
|
STATUS_MUTE_SUCCESS,
|
2024-08-23 15:27:40 -07:00
|
|
|
STATUS_REVEAL_MEDIA,
|
2024-07-11 05:22:14 -07:00
|
|
|
STATUS_TRANSLATE_FAIL,
|
|
|
|
STATUS_TRANSLATE_REQUEST,
|
2022-10-27 10:46:03 -07:00
|
|
|
STATUS_TRANSLATE_SUCCESS,
|
|
|
|
STATUS_TRANSLATE_UNDO,
|
2023-03-03 13:40:39 -08:00
|
|
|
STATUS_UNFILTER,
|
2024-07-11 05:22:14 -07:00
|
|
|
STATUS_UNMUTE_SUCCESS,
|
2024-05-27 15:11:28 -07:00
|
|
|
STATUS_LANGUAGE_CHANGE,
|
2024-08-23 15:27:40 -07:00
|
|
|
STATUS_COLLAPSE_SPOILER,
|
|
|
|
STATUS_EXPAND_SPOILER,
|
2020-03-27 13:59:38 -07:00
|
|
|
} from '../actions/statuses';
|
|
|
|
import { TIMELINE_DELETE } from '../actions/timelines';
|
|
|
|
|
2024-08-11 01:48:58 -07:00
|
|
|
import type { Status as BaseStatus, Translation } from 'pl-api';
|
2022-03-26 14:20:45 -07:00
|
|
|
import type { AnyAction } from 'redux';
|
2022-02-19 23:27:29 -08:00
|
|
|
|
2024-08-18 08:50:56 -07:00
|
|
|
type State = ImmutableMap<string, MinifiedStatus>;
|
2022-03-19 12:41:16 -07:00
|
|
|
|
2024-08-18 08:50:56 -07:00
|
|
|
type MinifiedStatus = ReturnType<typeof minifyStatus>;
|
2022-03-19 12:41:16 -07:00
|
|
|
|
2024-08-18 08:50:56 -07:00
|
|
|
const minifyStatus = (status: StatusRecord) => omit(status, ['reblog', 'poll', 'quote', 'group']);
|
2022-02-19 23:27:29 -08:00
|
|
|
|
2022-02-23 15:25:38 -08:00
|
|
|
// Check whether a status is a quote by secondary characteristics
|
2024-08-06 10:52:36 -07:00
|
|
|
const isQuote = (status: StatusRecord) => Boolean(status.quote_url);
|
2022-01-24 13:08:22 -08:00
|
|
|
|
|
|
|
// Preserve quote if an existing status already has it
|
2022-03-24 12:27:27 -07:00
|
|
|
const fixQuote = (status: StatusRecord, oldStatus?: StatusRecord): StatusRecord => {
|
2022-03-19 12:41:16 -07:00
|
|
|
if (oldStatus && !status.quote && isQuote(status)) {
|
2024-08-11 01:48:58 -07:00
|
|
|
return {
|
|
|
|
...status,
|
|
|
|
quote: oldStatus.quote,
|
|
|
|
quote_visible: status.quote_visible || oldStatus.quote_visible,
|
|
|
|
};
|
2022-01-24 13:08:22 -08:00
|
|
|
} else {
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2024-08-18 08:50:56 -07:00
|
|
|
const fixStatus = (state: State, status: BaseStatus): MinifiedStatus => {
|
2022-03-24 12:27:27 -07:00
|
|
|
const oldStatus = state.get(status.id);
|
2022-02-19 23:27:29 -08:00
|
|
|
|
2024-08-11 01:48:58 -07:00
|
|
|
return minifyStatus(fixQuote(normalizeStatus(status, oldStatus)));
|
2022-01-07 14:57:58 -08:00
|
|
|
};
|
|
|
|
|
2024-08-11 01:48:58 -07:00
|
|
|
const importStatus = (state: State, status: BaseStatus): State =>
|
2024-04-21 10:07:14 -07:00
|
|
|
state.set(status.id, fixStatus(state, status));
|
2020-03-27 13:59:38 -07:00
|
|
|
|
2024-08-11 01:48:58 -07:00
|
|
|
const importStatuses = (state: State, statuses: Array<BaseStatus>): State =>
|
2024-04-21 10:07:14 -07:00
|
|
|
state.withMutations(mutable => statuses.forEach(status => importStatus(mutable, status)));
|
2020-03-27 13:59:38 -07:00
|
|
|
|
2024-08-11 01:48:58 -07:00
|
|
|
const deleteStatus = (state: State, statusId: string, references: Array<string>) => {
|
2020-03-27 13:59:38 -07:00
|
|
|
references.forEach(ref => {
|
|
|
|
state = deleteStatus(state, ref[0], []);
|
|
|
|
});
|
|
|
|
|
2024-08-11 01:48:58 -07:00
|
|
|
return state.delete(statusId);
|
2020-03-27 13:59:38 -07:00
|
|
|
};
|
|
|
|
|
2024-08-18 08:50:56 -07:00
|
|
|
const incrementReplyCount = (state: State, { in_reply_to_id, quote }: BaseStatus) => {
|
2024-08-11 01:48:58 -07:00
|
|
|
if (in_reply_to_id && state.has(in_reply_to_id)) {
|
|
|
|
const parent = state.get(in_reply_to_id)!;
|
|
|
|
state = state.set(in_reply_to_id, {
|
|
|
|
...parent,
|
|
|
|
replies_count: (typeof parent.replies_count === 'number' ? parent.replies_count : 0) + 1,
|
|
|
|
});
|
2024-06-05 15:39:36 -07:00
|
|
|
}
|
|
|
|
|
2024-08-18 08:50:56 -07:00
|
|
|
if (quote?.id && state.has(quote.id)) {
|
|
|
|
const parent = state.get(quote.id)!;
|
|
|
|
state = state.set(quote.id, {
|
2024-08-11 01:48:58 -07:00
|
|
|
...parent,
|
|
|
|
quotes_count: (typeof parent.quotes_count === 'number' ? parent.quotes_count : 0) + 1,
|
|
|
|
});
|
2021-11-12 10:18:11 -08:00
|
|
|
}
|
2024-06-05 15:39:36 -07:00
|
|
|
|
|
|
|
return state;
|
2021-11-12 10:18:11 -08:00
|
|
|
};
|
|
|
|
|
2024-08-18 08:50:56 -07:00
|
|
|
const decrementReplyCount = (state: State, { in_reply_to_id, quote }: BaseStatus) => {
|
2021-11-12 10:18:11 -08:00
|
|
|
if (in_reply_to_id) {
|
2024-06-05 15:39:36 -07:00
|
|
|
state = state.updateIn([in_reply_to_id, 'replies_count'], 0, count =>
|
|
|
|
typeof count === 'number' ? Math.max(0, count - 1) : 0,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2024-08-18 08:50:56 -07:00
|
|
|
if (quote?.id) {
|
|
|
|
state = state.updateIn([quote.id, 'quotes_count'], 0, count =>
|
2024-05-12 16:18:04 -07:00
|
|
|
typeof count === 'number' ? Math.max(0, count - 1) : 0,
|
|
|
|
);
|
2021-11-12 10:18:11 -08:00
|
|
|
}
|
2024-06-05 15:39:36 -07:00
|
|
|
|
|
|
|
return state;
|
2021-11-12 10:18:11 -08:00
|
|
|
};
|
|
|
|
|
2022-03-24 12:27:27 -07:00
|
|
|
/** Simulate favourite/unfavourite of status for optimistic interactions */
|
|
|
|
const simulateFavourite = (
|
|
|
|
state: State,
|
|
|
|
statusId: string,
|
|
|
|
favourited: boolean,
|
|
|
|
): State => {
|
|
|
|
const status = state.get(statusId);
|
|
|
|
if (!status) return state;
|
|
|
|
|
|
|
|
const delta = favourited ? +1 : -1;
|
|
|
|
|
2024-08-11 01:48:58 -07:00
|
|
|
const updatedStatus = {
|
|
|
|
...status,
|
2022-03-24 12:27:27 -07:00
|
|
|
favourited,
|
|
|
|
favourites_count: Math.max(0, status.favourites_count + delta),
|
2024-08-11 01:48:58 -07:00
|
|
|
};
|
2022-03-24 12:27:27 -07:00
|
|
|
|
|
|
|
return state.set(statusId, updatedStatus);
|
|
|
|
};
|
|
|
|
|
2023-03-25 15:16:40 -07:00
|
|
|
/** Simulate dislike/undislike of status for optimistic interactions */
|
|
|
|
const simulateDislike = (
|
|
|
|
state: State,
|
|
|
|
statusId: string,
|
|
|
|
disliked: boolean,
|
|
|
|
): State => {
|
|
|
|
const status = state.get(statusId);
|
|
|
|
if (!status) return state;
|
|
|
|
|
|
|
|
const delta = disliked ? +1 : -1;
|
|
|
|
|
2024-08-11 01:48:58 -07:00
|
|
|
const updatedStatus = ({
|
|
|
|
...status,
|
2023-03-25 15:16:40 -07:00
|
|
|
disliked,
|
|
|
|
dislikes_count: Math.max(0, status.dislikes_count + delta),
|
|
|
|
});
|
|
|
|
|
|
|
|
return state.set(statusId, updatedStatus);
|
|
|
|
};
|
|
|
|
|
2022-11-19 14:14:37 -08:00
|
|
|
/** Import translation from translation service into the store. */
|
|
|
|
const importTranslation = (state: State, statusId: string, translation: Translation) => {
|
2024-08-11 01:48:58 -07:00
|
|
|
const result = normalizeTranslation(translation, state.get(statusId)!);
|
2024-07-11 05:22:14 -07:00
|
|
|
|
2024-08-11 01:48:58 -07:00
|
|
|
return state.update(statusId, undefined as any, (status) => ({
|
|
|
|
...status,
|
|
|
|
translation: result,
|
|
|
|
translating: false,
|
|
|
|
}));
|
2022-11-19 14:14:37 -08:00
|
|
|
};
|
|
|
|
|
|
|
|
/** Delete translation from the store. */
|
2024-05-12 16:18:04 -07:00
|
|
|
const deleteTranslation = (state: State, statusId: string) => state.deleteIn([statusId, 'translation']);
|
2022-11-19 14:14:37 -08:00
|
|
|
|
2022-03-19 12:41:16 -07:00
|
|
|
const initialState: State = ImmutableMap();
|
2020-03-27 13:59:38 -07:00
|
|
|
|
2024-05-12 16:18:04 -07:00
|
|
|
const statuses = (state = initialState, action: AnyAction): State => {
|
2022-05-11 10:40:34 -07:00
|
|
|
switch (action.type) {
|
2022-05-11 14:06:35 -07:00
|
|
|
case STATUS_IMPORT:
|
2024-04-21 10:07:14 -07:00
|
|
|
return importStatus(state, action.status);
|
2022-05-11 14:06:35 -07:00
|
|
|
case STATUSES_IMPORT:
|
2024-04-21 10:07:14 -07:00
|
|
|
return importStatuses(state, action.statuses);
|
2022-05-11 14:06:35 -07:00
|
|
|
case STATUS_CREATE_REQUEST:
|
2022-08-06 05:39:05 -07:00
|
|
|
return action.editing ? state : incrementReplyCount(state, action.params);
|
2022-05-11 14:06:35 -07:00
|
|
|
case STATUS_CREATE_FAIL:
|
2022-08-06 05:39:05 -07:00
|
|
|
return action.editing ? state : decrementReplyCount(state, action.params);
|
2022-05-11 14:06:35 -07:00
|
|
|
case FAVOURITE_REQUEST:
|
2024-08-11 01:48:58 -07:00
|
|
|
return simulateFavourite(state, action.statusId, true);
|
2022-05-11 14:06:35 -07:00
|
|
|
case UNFAVOURITE_REQUEST:
|
2024-08-11 01:48:58 -07:00
|
|
|
return simulateFavourite(state, action.statusId, false);
|
2023-03-25 15:16:40 -07:00
|
|
|
case DISLIKE_REQUEST:
|
2024-08-11 01:48:58 -07:00
|
|
|
return simulateDislike(state, action.statusId, true);
|
2023-03-25 15:16:40 -07:00
|
|
|
case UNDISLIKE_REQUEST:
|
2024-08-11 01:48:58 -07:00
|
|
|
return simulateDislike(state, action.statusId, false);
|
2022-05-11 14:06:35 -07:00
|
|
|
case EMOJI_REACT_REQUEST:
|
|
|
|
return state
|
|
|
|
.updateIn(
|
2024-08-11 01:48:58 -07:00
|
|
|
[action.statusId, 'reactions'],
|
2023-03-17 16:07:18 -07:00
|
|
|
emojiReacts => simulateEmojiReact(emojiReacts as any, action.emoji, action.custom),
|
2022-05-11 14:06:35 -07:00
|
|
|
);
|
|
|
|
case UNEMOJI_REACT_REQUEST:
|
|
|
|
return state
|
|
|
|
.updateIn(
|
2024-08-11 01:48:58 -07:00
|
|
|
[action.statusId, 'reactions'],
|
2022-05-11 14:06:35 -07:00
|
|
|
emojiReacts => simulateUnEmojiReact(emojiReacts as any, action.emoji),
|
|
|
|
);
|
|
|
|
case FAVOURITE_FAIL:
|
2024-08-11 01:48:58 -07:00
|
|
|
return state.get(action.statusId) === undefined ? state : state.setIn([action.statusId, 'favourited'], false);
|
2023-03-25 15:16:40 -07:00
|
|
|
case DISLIKE_FAIL:
|
2024-08-11 01:48:58 -07:00
|
|
|
return state.get(action.statusId) === undefined ? state : state.setIn([action.statusId, 'disliked'], false);
|
2022-05-11 14:06:35 -07:00
|
|
|
case REBLOG_REQUEST:
|
2024-08-20 03:54:41 -07:00
|
|
|
return state
|
|
|
|
.updateIn([action.statusId, 'reblogs_count'], 0, (count) => typeof count === 'number' ? count + 1 : 1)
|
|
|
|
.setIn([action.statusId, 'reblogged'], true);
|
2022-05-11 14:06:35 -07:00
|
|
|
case REBLOG_FAIL:
|
2024-08-11 01:48:58 -07:00
|
|
|
return state.get(action.statusId) === undefined ? state : state.setIn([action.statusId, 'reblogged'], false);
|
2022-05-11 14:06:35 -07:00
|
|
|
case UNREBLOG_REQUEST:
|
2024-08-20 03:54:41 -07:00
|
|
|
return state
|
|
|
|
.updateIn([action.statusId, 'reblogs_count'], 0, (count) => typeof count === 'number' ? Math.max(0, count - 1) : 0)
|
|
|
|
.setIn([action.statusId, 'reblogged'], false);
|
2022-05-11 14:06:35 -07:00
|
|
|
case UNREBLOG_FAIL:
|
2024-08-11 01:48:58 -07:00
|
|
|
return state.get(action.statusId) === undefined ? state : state.setIn([action.statusId, 'reblogged'], true);
|
2022-05-11 14:06:35 -07:00
|
|
|
case STATUS_MUTE_SUCCESS:
|
2024-08-11 01:48:58 -07:00
|
|
|
return state.setIn([action.statusId, 'muted'], true);
|
2022-05-11 14:06:35 -07:00
|
|
|
case STATUS_UNMUTE_SUCCESS:
|
2024-08-11 01:48:58 -07:00
|
|
|
return state.setIn([action.statusId, 'muted'], false);
|
2024-08-23 15:27:40 -07:00
|
|
|
case STATUS_REVEAL_MEDIA:
|
2022-05-11 14:06:35 -07:00
|
|
|
return state.withMutations(map => {
|
2024-08-11 01:48:58 -07:00
|
|
|
action.statusIds.forEach((id: string) => {
|
2022-05-11 14:06:35 -07:00
|
|
|
if (!(state.get(id) === undefined)) {
|
|
|
|
map.setIn([id, 'hidden'], false);
|
|
|
|
}
|
|
|
|
});
|
2020-03-27 13:59:38 -07:00
|
|
|
});
|
2024-08-23 15:27:40 -07:00
|
|
|
case STATUS_HIDE_MEDIA:
|
2022-05-11 14:06:35 -07:00
|
|
|
return state.withMutations(map => {
|
2024-08-11 01:48:58 -07:00
|
|
|
action.statusIds.forEach((id: string) => {
|
2022-05-11 14:06:35 -07:00
|
|
|
if (!(state.get(id) === undefined)) {
|
|
|
|
map.setIn([id, 'hidden'], true);
|
|
|
|
}
|
|
|
|
});
|
2020-03-27 13:59:38 -07:00
|
|
|
});
|
2024-08-23 15:27:40 -07:00
|
|
|
case STATUS_EXPAND_SPOILER:
|
|
|
|
return state.withMutations(map => {
|
|
|
|
action.statusIds.forEach((id: string) => {
|
|
|
|
if (!(state.get(id) === undefined)) {
|
|
|
|
map.setIn([id, 'expanded'], true);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
case STATUS_COLLAPSE_SPOILER:
|
|
|
|
return state.withMutations(map => {
|
|
|
|
action.statusIds.forEach((id: string) => {
|
|
|
|
if (!(state.get(id) === undefined)) {
|
|
|
|
map.setIn([id, 'expanded'], false);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
2022-06-01 05:34:36 -07:00
|
|
|
case STATUS_DELETE_REQUEST:
|
|
|
|
return decrementReplyCount(state, action.params);
|
|
|
|
case STATUS_DELETE_FAIL:
|
|
|
|
return incrementReplyCount(state, action.params);
|
2024-07-11 05:22:14 -07:00
|
|
|
case STATUS_TRANSLATE_REQUEST:
|
2024-08-11 01:48:58 -07:00
|
|
|
return state.setIn([action.statusId, 'translating'], true);
|
2022-10-27 10:46:03 -07:00
|
|
|
case STATUS_TRANSLATE_SUCCESS:
|
2024-08-11 01:48:58 -07:00
|
|
|
return importTranslation(state, action.statusId, action.translation);
|
2024-07-11 05:22:14 -07:00
|
|
|
case STATUS_TRANSLATE_FAIL:
|
|
|
|
return state
|
2024-08-11 01:48:58 -07:00
|
|
|
.setIn([action.statusId, 'translating'], false)
|
|
|
|
.setIn([action.statusId, 'translation'], false);
|
2022-10-27 10:46:03 -07:00
|
|
|
case STATUS_TRANSLATE_UNDO:
|
2024-08-11 01:48:58 -07:00
|
|
|
return deleteTranslation(state, action.statusId);
|
2023-03-03 13:40:39 -08:00
|
|
|
case STATUS_UNFILTER:
|
2024-08-11 01:48:58 -07:00
|
|
|
return state.setIn([action.statusId, 'showFiltered'], false);
|
2024-05-27 15:11:28 -07:00
|
|
|
case STATUS_LANGUAGE_CHANGE:
|
2024-08-11 01:48:58 -07:00
|
|
|
return state.setIn([action.statusId, 'currentLanguage'], action.language);
|
2022-05-11 14:06:35 -07:00
|
|
|
case TIMELINE_DELETE:
|
2024-08-11 01:48:58 -07:00
|
|
|
return deleteStatus(state, action.statusId, action.references);
|
2022-09-07 12:24:10 -07:00
|
|
|
case EVENT_JOIN_REQUEST:
|
2024-08-11 01:48:58 -07:00
|
|
|
return state.setIn([action.statusId, 'event', 'join_state'], 'pending');
|
2022-09-07 12:24:10 -07:00
|
|
|
case EVENT_JOIN_FAIL:
|
|
|
|
case EVENT_LEAVE_REQUEST:
|
2024-08-11 01:48:58 -07:00
|
|
|
return state.setIn([action.statusId, 'event', 'join_state'], null);
|
2022-09-07 12:24:10 -07:00
|
|
|
case EVENT_LEAVE_FAIL:
|
2024-08-11 01:48:58 -07:00
|
|
|
return state.setIn([action.statusId, 'event', 'join_state'], action.previousState);
|
2022-05-11 14:06:35 -07:00
|
|
|
default:
|
|
|
|
return state;
|
2020-03-27 13:59:38 -07:00
|
|
|
}
|
2024-05-12 16:18:04 -07:00
|
|
|
};
|
|
|
|
|
2024-05-13 10:00:42 -07:00
|
|
|
export {
|
2024-08-18 08:50:56 -07:00
|
|
|
type MinifiedStatus,
|
2024-05-13 10:00:42 -07:00
|
|
|
statuses as default,
|
|
|
|
};
|