2022-02-19 23:27:29 -08:00
|
|
|
import escapeTextContentForBrowser from 'escape-html';
|
2022-02-19 21:21:47 -08:00
|
|
|
import { Map as ImmutableMap, fromJS } from 'immutable';
|
2022-01-10 14:25:06 -08:00
|
|
|
|
2022-02-19 23:27:29 -08:00
|
|
|
import emojify from 'soapbox/features/emoji/emoji';
|
2022-02-19 21:21:47 -08:00
|
|
|
import { normalizeStatus } from 'soapbox/normalizers/status';
|
2022-01-10 14:01:24 -08:00
|
|
|
import { simulateEmojiReact, simulateUnEmojiReact } from 'soapbox/utils/emoji_reacts';
|
2022-02-19 23:27:29 -08:00
|
|
|
import { stripCompatibilityFeatures } from 'soapbox/utils/html';
|
2022-01-10 14:25:06 -08:00
|
|
|
|
2022-01-10 14:17:52 -08:00
|
|
|
import {
|
|
|
|
EMOJI_REACT_REQUEST,
|
|
|
|
UNEMOJI_REACT_REQUEST,
|
|
|
|
} from '../actions/emoji_reacts';
|
|
|
|
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,
|
|
|
|
} from '../actions/statuses';
|
|
|
|
import { TIMELINE_DELETE } from '../actions/timelines';
|
|
|
|
|
2022-02-19 23:27:29 -08:00
|
|
|
const domParser = new DOMParser();
|
|
|
|
|
|
|
|
const makeEmojiMap = record => record.get('emojis').reduce((obj, emoji) => {
|
|
|
|
obj[`:${emoji.get('shortcode')}:`] = emoji;
|
|
|
|
return obj;
|
|
|
|
}, {});
|
|
|
|
|
|
|
|
const minifyStatus = status => {
|
2022-02-20 00:38:22 -08:00
|
|
|
return status.mergeWith((o, n) => n || o, {
|
|
|
|
account: status.getIn(['account', 'id']),
|
|
|
|
reblog: status.getIn(['reblog', 'id']),
|
|
|
|
poll: status.getIn(['poll', 'id']),
|
|
|
|
quote: status.getIn(['quote', 'id']) || status.getIn(['pleroma', 'quote', 'id']),
|
2022-02-19 23:27:29 -08:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
// Only calculate these values when status first encountered
|
|
|
|
// Otherwise keep the ones already in the reducer
|
|
|
|
const calculateStatus = (status, oldStatus) => {
|
|
|
|
if (oldStatus) {
|
|
|
|
return status.merge({
|
|
|
|
search_index: oldStatus.get('search_index'),
|
|
|
|
contentHtml: oldStatus.get('contentHtml'),
|
|
|
|
spoilerHtml: oldStatus.get('spoilerHtml'),
|
|
|
|
hidden: oldStatus.get('hidden'),
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
const spoilerText = status.get('spoiler_text') || '';
|
|
|
|
const searchContent = ([spoilerText, status.get('content')].concat(status.getIn(['poll', 'options']) ? status.getIn(['poll', 'options']).map(option => option.get('title')) : [])).join('\n\n').replace(/<br\s*\/?>/g, '\n').replace(/<\/p><p>/g, '\n\n');
|
|
|
|
const emojiMap = makeEmojiMap(status);
|
|
|
|
|
|
|
|
return status.merge({
|
|
|
|
search_index: domParser.parseFromString(searchContent, 'text/html').documentElement.textContent,
|
|
|
|
contentHtml: stripCompatibilityFeatures(emojify(status.get('content'), emojiMap)),
|
|
|
|
spoilerHtml: emojify(escapeTextContentForBrowser(spoilerText), emojiMap),
|
|
|
|
hidden: spoilerText.length > 0 || status.get('sensitive'),
|
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-01-24 13:08:22 -08:00
|
|
|
const isQuote = status => {
|
|
|
|
return Boolean(status.get('quote_id') || status.getIn(['pleroma', 'quote_url']));
|
|
|
|
};
|
|
|
|
|
|
|
|
// Preserve quote if an existing status already has it
|
2022-02-20 00:38:22 -08:00
|
|
|
const fixQuote = (status, oldStatus) => {
|
2022-01-24 13:08:22 -08:00
|
|
|
if (oldStatus && !status.get('quote') && isQuote(status)) {
|
2022-01-28 08:17:38 -08:00
|
|
|
return status
|
|
|
|
.set('quote', oldStatus.get('quote'))
|
|
|
|
.updateIn(['pleroma', 'quote_visible'], visible => visible || oldStatus.getIn(['pleroma', 'quote_visible']));
|
2022-01-24 13:08:22 -08:00
|
|
|
} else {
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-02-19 21:21:47 -08:00
|
|
|
const fixStatus = (state, status) => {
|
2022-02-19 23:27:29 -08:00
|
|
|
const oldStatus = state.get(status.get('id'));
|
|
|
|
|
2022-01-07 14:57:58 -08:00
|
|
|
return status.withMutations(status => {
|
2022-02-19 21:21:47 -08:00
|
|
|
normalizeStatus(status);
|
2022-02-20 00:38:22 -08:00
|
|
|
fixQuote(status, oldStatus);
|
2022-02-19 23:27:29 -08:00
|
|
|
calculateStatus(status, oldStatus);
|
|
|
|
minifyStatus(status);
|
2022-01-07 14:57:58 -08:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2022-02-19 21:21:47 -08:00
|
|
|
const importStatus = (state, status) => state.set(status.id, fixStatus(state, fromJS(status)));
|
2020-03-27 13:59:38 -07:00
|
|
|
|
|
|
|
const importStatuses = (state, statuses) =>
|
|
|
|
state.withMutations(mutable => statuses.forEach(status => importStatus(mutable, status)));
|
|
|
|
|
|
|
|
const deleteStatus = (state, id, references) => {
|
|
|
|
references.forEach(ref => {
|
|
|
|
state = deleteStatus(state, ref[0], []);
|
|
|
|
});
|
|
|
|
|
|
|
|
return state.delete(id);
|
|
|
|
};
|
|
|
|
|
2021-11-12 10:18:11 -08:00
|
|
|
const importPendingStatus = (state, { in_reply_to_id }) => {
|
|
|
|
if (in_reply_to_id) {
|
|
|
|
return state.updateIn([in_reply_to_id, 'replies_count'], 0, count => count + 1);
|
|
|
|
} else {
|
|
|
|
return state;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const deletePendingStatus = (state, { in_reply_to_id }) => {
|
|
|
|
if (in_reply_to_id) {
|
|
|
|
return state.updateIn([in_reply_to_id, 'replies_count'], 0, count => Math.max(0, count - 1));
|
|
|
|
} else {
|
|
|
|
return state;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-03-27 13:59:38 -07:00
|
|
|
const initialState = ImmutableMap();
|
|
|
|
|
|
|
|
export default function statuses(state = initialState, action) {
|
|
|
|
switch(action.type) {
|
|
|
|
case STATUS_IMPORT:
|
|
|
|
return importStatus(state, action.status);
|
|
|
|
case STATUSES_IMPORT:
|
|
|
|
return importStatuses(state, action.statuses);
|
2021-11-12 10:18:11 -08:00
|
|
|
case STATUS_CREATE_REQUEST:
|
|
|
|
return importPendingStatus(state, action.params);
|
|
|
|
case STATUS_CREATE_FAIL:
|
|
|
|
return deletePendingStatus(state, action.params);
|
2020-03-27 13:59:38 -07:00
|
|
|
case FAVOURITE_REQUEST:
|
2020-09-27 15:24:55 -07:00
|
|
|
return state.update(action.status.get('id'), status =>
|
|
|
|
status
|
|
|
|
.set('favourited', true)
|
|
|
|
.update('favourites_count', count => count + 1));
|
|
|
|
case UNFAVOURITE_REQUEST:
|
|
|
|
return state.update(action.status.get('id'), status =>
|
|
|
|
status
|
|
|
|
.set('favourited', false)
|
|
|
|
.update('favourites_count', count => Math.max(0, count - 1)));
|
2020-05-23 18:29:25 -07:00
|
|
|
case EMOJI_REACT_REQUEST:
|
2020-09-27 15:24:55 -07:00
|
|
|
return state
|
|
|
|
.updateIn(
|
|
|
|
[action.status.get('id'), 'pleroma', 'emoji_reactions'],
|
2020-10-07 11:08:36 -07:00
|
|
|
emojiReacts => simulateEmojiReact(emojiReacts, action.emoji),
|
2020-09-27 15:24:55 -07:00
|
|
|
);
|
|
|
|
case UNEMOJI_REACT_REQUEST:
|
|
|
|
return state
|
|
|
|
.updateIn(
|
|
|
|
[action.status.get('id'), 'pleroma', 'emoji_reactions'],
|
2020-10-07 11:08:36 -07:00
|
|
|
emojiReacts => simulateUnEmojiReact(emojiReacts, action.emoji),
|
2020-09-27 15:24:55 -07:00
|
|
|
);
|
2020-03-27 13:59:38 -07:00
|
|
|
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);
|
2022-01-04 07:57:01 -08:00
|
|
|
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);
|
2020-03-27 13:59:38 -07:00
|
|
|
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 => {
|
|
|
|
if (!(state.get(id) === undefined)) {
|
|
|
|
map.setIn([id, 'hidden'], false);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
case STATUS_HIDE:
|
|
|
|
return state.withMutations(map => {
|
|
|
|
action.ids.forEach(id => {
|
|
|
|
if (!(state.get(id) === undefined)) {
|
|
|
|
map.setIn([id, 'hidden'], true);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
case TIMELINE_DELETE:
|
|
|
|
return deleteStatus(state, action.id, action.references);
|
|
|
|
default:
|
|
|
|
return state;
|
|
|
|
}
|
2021-08-03 12:22:51 -07:00
|
|
|
}
|