2022-02-19 23:27:29 -08:00
|
|
|
import escapeTextContentForBrowser from 'escape-html';
|
2022-11-19 14:14:37 -08:00
|
|
|
import { Map as ImmutableMap, List as ImmutableList } from 'immutable';
|
2022-01-10 14:25:06 -08:00
|
|
|
|
2022-07-04 13:30:35 -07:00
|
|
|
import emojify from 'soapbox/features/emoji';
|
2022-03-19 13:04:28 -07:00
|
|
|
import { normalizeStatus } from 'soapbox/normalizers';
|
2022-11-15 12:46:23 -08:00
|
|
|
import { simulateEmojiReact, simulateUnEmojiReact } from 'soapbox/utils/emoji-reacts';
|
2022-03-19 13:04:28 -07:00
|
|
|
import { stripCompatibilityFeatures, unescapeHTML } from 'soapbox/utils/html';
|
2022-03-24 12:27:27 -07:00
|
|
|
import { makeEmojiMap, normalizeId } from 'soapbox/utils/normalizers';
|
2022-01-10 14:25:06 -08:00
|
|
|
|
2022-01-10 14:17:52 -08:00
|
|
|
import {
|
|
|
|
EMOJI_REACT_REQUEST,
|
|
|
|
UNEMOJI_REACT_REQUEST,
|
2022-11-16 05:32:32 -08:00
|
|
|
} 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,
|
|
|
|
} from '../actions/interactions';
|
|
|
|
import {
|
2021-11-12 10:18:11 -08:00
|
|
|
STATUS_CREATE_REQUEST,
|
|
|
|
STATUS_CREATE_FAIL,
|
2020-03-27 13:59:38 -07:00
|
|
|
STATUS_MUTE_SUCCESS,
|
|
|
|
STATUS_UNMUTE_SUCCESS,
|
|
|
|
STATUS_REVEAL,
|
|
|
|
STATUS_HIDE,
|
2022-06-01 05:34:36 -07:00
|
|
|
STATUS_DELETE_REQUEST,
|
|
|
|
STATUS_DELETE_FAIL,
|
2022-10-27 10:46:03 -07:00
|
|
|
STATUS_TRANSLATE_SUCCESS,
|
|
|
|
STATUS_TRANSLATE_UNDO,
|
2023-03-03 13:40:39 -08:00
|
|
|
STATUS_UNFILTER,
|
2020-03-27 13:59:38 -07:00
|
|
|
} from '../actions/statuses';
|
|
|
|
import { TIMELINE_DELETE } from '../actions/timelines';
|
|
|
|
|
2022-03-26 14:20:45 -07:00
|
|
|
import type { AnyAction } from 'redux';
|
2022-12-11 12:37:00 -08:00
|
|
|
import type { APIEntity } from 'soapbox/types/entities';
|
2022-03-26 14:20:45 -07:00
|
|
|
|
2022-02-19 23:27:29 -08:00
|
|
|
const domParser = new DOMParser();
|
|
|
|
|
2022-03-19 12:41:16 -07:00
|
|
|
type StatusRecord = ReturnType<typeof normalizeStatus>;
|
|
|
|
type APIEntities = Array<APIEntity>;
|
|
|
|
|
2022-03-31 15:00:31 -07:00
|
|
|
type State = ImmutableMap<string, ReducerStatus>;
|
2022-03-19 12:41:16 -07:00
|
|
|
|
2022-03-31 15:00:31 -07:00
|
|
|
export interface ReducerStatus extends StatusRecord {
|
2023-02-15 13:26:27 -08:00
|
|
|
account: string | null
|
|
|
|
reblog: string | null
|
|
|
|
poll: string | null
|
|
|
|
quote: string | null
|
|
|
|
group: string | null
|
2022-03-31 15:00:31 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
const minifyStatus = (status: StatusRecord): ReducerStatus => {
|
2022-02-20 00:38:22 -08:00
|
|
|
return status.mergeWith((o, n) => n || o, {
|
2022-03-24 12:27:27 -07:00
|
|
|
account: normalizeId(status.getIn(['account', 'id'])),
|
|
|
|
reblog: normalizeId(status.getIn(['reblog', 'id'])),
|
|
|
|
poll: normalizeId(status.getIn(['poll', 'id'])),
|
|
|
|
quote: normalizeId(status.getIn(['quote', 'id'])),
|
2022-12-12 14:36:56 -08:00
|
|
|
group: normalizeId(status.getIn(['group', 'id'])),
|
2022-03-31 15:00:31 -07:00
|
|
|
}) as ReducerStatus;
|
2022-02-19 23:27:29 -08:00
|
|
|
};
|
|
|
|
|
2022-03-19 13:04:28 -07:00
|
|
|
// Gets titles of poll options from status
|
2022-03-24 12:27:27 -07:00
|
|
|
const getPollOptionTitles = ({ poll }: StatusRecord): ImmutableList<string> => {
|
|
|
|
if (poll && typeof poll === 'object') {
|
|
|
|
return poll.options.map(({ title }) => title);
|
|
|
|
} else {
|
|
|
|
return ImmutableList();
|
|
|
|
}
|
2022-03-19 13:04:28 -07:00
|
|
|
};
|
|
|
|
|
2022-03-26 02:55:53 -07:00
|
|
|
// Gets usernames of mentioned users from status
|
2022-03-26 08:45:45 -07:00
|
|
|
const getMentionedUsernames = (status: StatusRecord): ImmutableList<string> => {
|
2022-03-26 09:17:27 -07:00
|
|
|
return status.mentions.map(({ acct }) => `@${acct}`);
|
2022-03-26 02:55:53 -07:00
|
|
|
};
|
|
|
|
|
2022-03-19 13:04:28 -07:00
|
|
|
// Creates search text from the status
|
|
|
|
const buildSearchContent = (status: StatusRecord): string => {
|
|
|
|
const pollOptionTitles = getPollOptionTitles(status);
|
2022-03-26 02:55:53 -07:00
|
|
|
const mentionedUsernames = getMentionedUsernames(status);
|
2022-03-19 13:04:28 -07:00
|
|
|
|
|
|
|
const fields = ImmutableList([
|
|
|
|
status.spoiler_text,
|
|
|
|
status.content,
|
2022-03-26 02:55:53 -07:00
|
|
|
]).concat(pollOptionTitles).concat(mentionedUsernames);
|
2022-03-19 13:04:28 -07:00
|
|
|
|
2022-03-24 12:27:27 -07:00
|
|
|
return unescapeHTML(fields.join('\n\n')) || '';
|
2022-03-19 13:04:28 -07:00
|
|
|
};
|
|
|
|
|
2022-02-19 23:27:29 -08:00
|
|
|
// Only calculate these values when status first encountered
|
|
|
|
// Otherwise keep the ones already in the reducer
|
2022-03-19 15:29:46 -07:00
|
|
|
export const calculateStatus = (
|
2022-03-19 12:41:16 -07:00
|
|
|
status: StatusRecord,
|
2022-03-24 12:27:27 -07:00
|
|
|
oldStatus?: StatusRecord,
|
2022-03-19 12:41:16 -07:00
|
|
|
expandSpoilers: boolean = false,
|
|
|
|
): StatusRecord => {
|
2022-04-27 13:50:35 -07:00
|
|
|
if (oldStatus && oldStatus.content === status.content && oldStatus.spoiler_text === status.spoiler_text) {
|
2022-02-19 23:27:29 -08:00
|
|
|
return status.merge({
|
2022-03-19 12:41:16 -07:00
|
|
|
search_index: oldStatus.search_index,
|
|
|
|
contentHtml: oldStatus.contentHtml,
|
|
|
|
spoilerHtml: oldStatus.spoilerHtml,
|
|
|
|
hidden: oldStatus.hidden,
|
2022-02-19 23:27:29 -08:00
|
|
|
});
|
|
|
|
} else {
|
2022-03-19 12:41:16 -07:00
|
|
|
const spoilerText = status.spoiler_text;
|
2022-03-19 13:04:28 -07:00
|
|
|
const searchContent = buildSearchContent(status);
|
2022-03-19 12:41:16 -07:00
|
|
|
const emojiMap = makeEmojiMap(status.emojis);
|
2022-02-19 23:27:29 -08:00
|
|
|
|
|
|
|
return status.merge({
|
2022-03-24 12:27:27 -07:00
|
|
|
search_index: domParser.parseFromString(searchContent, 'text/html').documentElement.textContent || '',
|
2022-03-19 12:41:16 -07:00
|
|
|
contentHtml: stripCompatibilityFeatures(emojify(status.content, emojiMap)),
|
2022-02-19 23:27:29 -08:00
|
|
|
spoilerHtml: emojify(escapeTextContentForBrowser(spoilerText), emojiMap),
|
2022-03-19 12:41:16 -07:00
|
|
|
hidden: expandSpoilers ? false : spoilerText.length > 0 || status.sensitive,
|
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
|
2022-03-19 12:41:16 -07:00
|
|
|
const isQuote = (status: StatusRecord) => {
|
2022-06-04 00:22:36 -07:00
|
|
|
return Boolean(status.pleroma.get('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)) {
|
2022-01-28 08:17:38 -08:00
|
|
|
return status
|
2022-03-19 12:41:16 -07:00
|
|
|
.set('quote', oldStatus.quote)
|
2022-06-04 00:22:36 -07:00
|
|
|
.updateIn(['pleroma', 'quote_visible'], visible => visible || oldStatus.pleroma.get('quote_visible'));
|
2022-01-24 13:08:22 -08:00
|
|
|
} else {
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-03-31 15:00:31 -07:00
|
|
|
const fixStatus = (state: State, status: APIEntity, expandSpoilers: boolean): ReducerStatus => {
|
2022-03-24 12:27:27 -07:00
|
|
|
const oldStatus = state.get(status.id);
|
2022-02-19 23:27:29 -08:00
|
|
|
|
2022-03-08 21:47:30 -08:00
|
|
|
return normalizeStatus(status).withMutations(status => {
|
2022-02-20 00:38:22 -08:00
|
|
|
fixQuote(status, oldStatus);
|
2022-02-20 09:44:10 -08:00
|
|
|
calculateStatus(status, oldStatus, expandSpoilers);
|
2022-02-19 23:27:29 -08:00
|
|
|
minifyStatus(status);
|
2022-03-31 15:00:31 -07:00
|
|
|
}) as ReducerStatus;
|
2022-01-07 14:57:58 -08:00
|
|
|
};
|
|
|
|
|
2022-03-19 12:41:16 -07:00
|
|
|
const importStatus = (state: State, status: APIEntity, expandSpoilers: boolean): State =>
|
|
|
|
state.set(status.id, fixStatus(state, status, expandSpoilers));
|
2020-03-27 13:59:38 -07:00
|
|
|
|
2022-03-19 12:41:16 -07:00
|
|
|
const importStatuses = (state: State, statuses: APIEntities, expandSpoilers: boolean): State =>
|
2022-02-20 09:44:10 -08:00
|
|
|
state.withMutations(mutable => statuses.forEach(status => importStatus(mutable, status, expandSpoilers)));
|
2020-03-27 13:59:38 -07:00
|
|
|
|
2022-03-19 12:41:16 -07:00
|
|
|
const deleteStatus = (state: State, id: string, references: Array<string>) => {
|
2020-03-27 13:59:38 -07:00
|
|
|
references.forEach(ref => {
|
|
|
|
state = deleteStatus(state, ref[0], []);
|
|
|
|
});
|
|
|
|
|
|
|
|
return state.delete(id);
|
|
|
|
};
|
|
|
|
|
2022-06-01 05:34:36 -07:00
|
|
|
const incrementReplyCount = (state: State, { in_reply_to_id }: APIEntity) => {
|
2021-11-12 10:18:11 -08:00
|
|
|
if (in_reply_to_id) {
|
2022-03-24 09:53:09 -07:00
|
|
|
return state.updateIn([in_reply_to_id, 'replies_count'], 0, count => {
|
|
|
|
return typeof count === 'number' ? count + 1 : 0;
|
|
|
|
});
|
2021-11-12 10:18:11 -08:00
|
|
|
} else {
|
|
|
|
return state;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-06-01 05:34:36 -07:00
|
|
|
const decrementReplyCount = (state: State, { in_reply_to_id }: APIEntity) => {
|
2021-11-12 10:18:11 -08:00
|
|
|
if (in_reply_to_id) {
|
2022-03-24 09:53:09 -07:00
|
|
|
return state.updateIn([in_reply_to_id, 'replies_count'], 0, count => {
|
|
|
|
return typeof count === 'number' ? Math.max(0, count - 1) : 0;
|
|
|
|
});
|
2021-11-12 10:18:11 -08:00
|
|
|
} else {
|
|
|
|
return state;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
const updatedStatus = status.merge({
|
|
|
|
favourited,
|
|
|
|
favourites_count: Math.max(0, status.favourites_count + delta),
|
|
|
|
});
|
|
|
|
|
|
|
|
return state.set(statusId, updatedStatus);
|
|
|
|
};
|
|
|
|
|
2022-11-19 14:14:37 -08:00
|
|
|
interface Translation {
|
2023-02-15 13:26:27 -08:00
|
|
|
content: string
|
|
|
|
detected_source_language: string
|
|
|
|
provider: string
|
2022-11-19 14:14:37 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
/** Import translation from translation service into the store. */
|
|
|
|
const importTranslation = (state: State, statusId: string, translation: Translation) => {
|
|
|
|
const map = ImmutableMap(translation);
|
|
|
|
const result = map.set('content', stripCompatibilityFeatures(map.get('content', '')));
|
|
|
|
return state.setIn([statusId, 'translation'], result);
|
|
|
|
};
|
|
|
|
|
|
|
|
/** Delete translation from the store. */
|
|
|
|
const deleteTranslation = (state: State, statusId: string) => {
|
|
|
|
return state.deleteIn([statusId, 'translation']);
|
|
|
|
};
|
|
|
|
|
2022-03-19 12:41:16 -07:00
|
|
|
const initialState: State = ImmutableMap();
|
2020-03-27 13:59:38 -07:00
|
|
|
|
2022-03-19 12:41:16 -07:00
|
|
|
export default function 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:
|
|
|
|
return importStatus(state, action.status, action.expandSpoilers);
|
|
|
|
case STATUSES_IMPORT:
|
|
|
|
return importStatuses(state, action.statuses, action.expandSpoilers);
|
|
|
|
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:
|
|
|
|
return simulateFavourite(state, action.status.id, true);
|
|
|
|
case UNFAVOURITE_REQUEST:
|
|
|
|
return simulateFavourite(state, action.status.id, false);
|
|
|
|
case EMOJI_REACT_REQUEST:
|
|
|
|
return state
|
|
|
|
.updateIn(
|
|
|
|
[action.status.get('id'), 'pleroma', 'emoji_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(
|
|
|
|
[action.status.get('id'), 'pleroma', 'emoji_reactions'],
|
|
|
|
emojiReacts => simulateUnEmojiReact(emojiReacts as any, action.emoji),
|
|
|
|
);
|
|
|
|
case FAVOURITE_FAIL:
|
|
|
|
return state.get(action.status.get('id')) === undefined ? state : state.setIn([action.status.get('id'), 'favourited'], false);
|
|
|
|
case REBLOG_REQUEST:
|
|
|
|
return state.setIn([action.status.get('id'), 'reblogged'], true);
|
|
|
|
case REBLOG_FAIL:
|
|
|
|
return state.get(action.status.get('id')) === undefined ? state : state.setIn([action.status.get('id'), 'reblogged'], false);
|
|
|
|
case UNREBLOG_REQUEST:
|
|
|
|
return state.setIn([action.status.get('id'), 'reblogged'], false);
|
|
|
|
case UNREBLOG_FAIL:
|
|
|
|
return state.get(action.status.get('id')) === undefined ? state : state.setIn([action.status.get('id'), 'reblogged'], true);
|
|
|
|
case STATUS_MUTE_SUCCESS:
|
|
|
|
return state.setIn([action.id, 'muted'], true);
|
|
|
|
case STATUS_UNMUTE_SUCCESS:
|
|
|
|
return state.setIn([action.id, 'muted'], false);
|
|
|
|
case STATUS_REVEAL:
|
|
|
|
return state.withMutations(map => {
|
|
|
|
action.ids.forEach((id: string) => {
|
|
|
|
if (!(state.get(id) === undefined)) {
|
|
|
|
map.setIn([id, 'hidden'], false);
|
|
|
|
}
|
|
|
|
});
|
2020-03-27 13:59:38 -07:00
|
|
|
});
|
2022-05-11 14:06:35 -07:00
|
|
|
case STATUS_HIDE:
|
|
|
|
return state.withMutations(map => {
|
|
|
|
action.ids.forEach((id: string) => {
|
|
|
|
if (!(state.get(id) === undefined)) {
|
|
|
|
map.setIn([id, 'hidden'], true);
|
|
|
|
}
|
|
|
|
});
|
2020-03-27 13:59:38 -07:00
|
|
|
});
|
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);
|
2022-10-27 10:46:03 -07:00
|
|
|
case STATUS_TRANSLATE_SUCCESS:
|
2022-11-19 14:14:37 -08:00
|
|
|
return importTranslation(state, action.id, action.translation);
|
2022-10-27 10:46:03 -07:00
|
|
|
case STATUS_TRANSLATE_UNDO:
|
2022-11-19 14:14:37 -08:00
|
|
|
return deleteTranslation(state, action.id);
|
2023-03-03 13:40:39 -08:00
|
|
|
case STATUS_UNFILTER:
|
|
|
|
return state.setIn([action.id, 'showFiltered'], false);
|
2022-05-11 14:06:35 -07:00
|
|
|
case TIMELINE_DELETE:
|
|
|
|
return deleteStatus(state, action.id, action.references);
|
2022-09-07 12:24:10 -07:00
|
|
|
case EVENT_JOIN_REQUEST:
|
2022-09-21 14:27:53 -07:00
|
|
|
return state.setIn([action.id, 'event', 'join_state'], 'pending');
|
2022-09-07 12:24:10 -07:00
|
|
|
case EVENT_JOIN_FAIL:
|
|
|
|
case EVENT_LEAVE_REQUEST:
|
2022-09-21 14:27:53 -07:00
|
|
|
return state.setIn([action.id, 'event', 'join_state'], null);
|
2022-09-07 12:24:10 -07:00
|
|
|
case EVENT_LEAVE_FAIL:
|
2022-09-21 14:27:53 -07:00
|
|
|
return state.setIn([action.id, 'event', 'join_state'], action.previousState);
|
2022-05-11 14:06:35 -07:00
|
|
|
default:
|
|
|
|
return state;
|
2020-03-27 13:59:38 -07:00
|
|
|
}
|
2021-08-03 12:22:51 -07:00
|
|
|
}
|