Typescript: reducers/statuses.ts
This commit is contained in:
parent
b71a592f95
commit
9d79b60134
|
@ -15,7 +15,6 @@ import { normalizeCard } from 'soapbox/normalizers/card';
|
||||||
import { normalizeEmoji } from 'soapbox/normalizers/emoji';
|
import { normalizeEmoji } from 'soapbox/normalizers/emoji';
|
||||||
import { normalizeMention } from 'soapbox/normalizers/mention';
|
import { normalizeMention } from 'soapbox/normalizers/mention';
|
||||||
import { normalizePoll } from 'soapbox/normalizers/poll';
|
import { normalizePoll } from 'soapbox/normalizers/poll';
|
||||||
import { IStatus } from 'soapbox/types';
|
|
||||||
|
|
||||||
// https://docs.joinmastodon.org/entities/status/
|
// https://docs.joinmastodon.org/entities/status/
|
||||||
export const StatusRecord = ImmutableRecord({
|
export const StatusRecord = ImmutableRecord({
|
||||||
|
@ -136,7 +135,7 @@ const fixQuote = (status: ImmutableMap<string, any>) => {
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
export const normalizeStatus = (status: Record<string, any>): IStatus => {
|
export const normalizeStatus = (status: Record<string, any>) => {
|
||||||
return StatusRecord(
|
return StatusRecord(
|
||||||
ImmutableMap(fromJS(status)).withMutations(status => {
|
ImmutableMap(fromJS(status)).withMutations(status => {
|
||||||
normalizeAttachments(status);
|
normalizeAttachments(status);
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
import escapeTextContentForBrowser from 'escape-html';
|
import escapeTextContentForBrowser from 'escape-html';
|
||||||
import { Map as ImmutableMap, List as ImmutableList, fromJS } from 'immutable';
|
import { Map as ImmutableMap, List as ImmutableList } from 'immutable';
|
||||||
|
import { AnyAction } from 'redux';
|
||||||
|
|
||||||
import emojify from 'soapbox/features/emoji/emoji';
|
import emojify from 'soapbox/features/emoji/emoji';
|
||||||
import { normalizeStatus } from 'soapbox/normalizers/status';
|
import { normalizeStatus } from 'soapbox/normalizers/status';
|
||||||
|
@ -33,7 +34,13 @@ import { TIMELINE_DELETE } from '../actions/timelines';
|
||||||
|
|
||||||
const domParser = new DOMParser();
|
const domParser = new DOMParser();
|
||||||
|
|
||||||
const minifyStatus = status => {
|
type StatusRecord = ReturnType<typeof normalizeStatus>;
|
||||||
|
type APIEntity = Record<string, any>;
|
||||||
|
type APIEntities = Array<APIEntity>;
|
||||||
|
|
||||||
|
type State = ImmutableMap<string, StatusRecord>;
|
||||||
|
|
||||||
|
const minifyStatus = (status: StatusRecord): StatusRecord => {
|
||||||
return status.mergeWith((o, n) => n || o, {
|
return status.mergeWith((o, n) => n || o, {
|
||||||
account: status.getIn(['account', 'id']),
|
account: status.getIn(['account', 'id']),
|
||||||
reblog: status.getIn(['reblog', 'id']),
|
reblog: status.getIn(['reblog', 'id']),
|
||||||
|
@ -44,46 +51,50 @@ const minifyStatus = status => {
|
||||||
|
|
||||||
// Only calculate these values when status first encountered
|
// Only calculate these values when status first encountered
|
||||||
// Otherwise keep the ones already in the reducer
|
// Otherwise keep the ones already in the reducer
|
||||||
export const calculateStatus = (status, oldStatus, expandSpoilers = false) => {
|
export const calculateStatus = (
|
||||||
|
status: StatusRecord,
|
||||||
|
oldStatus: StatusRecord,
|
||||||
|
expandSpoilers: boolean = false,
|
||||||
|
): StatusRecord => {
|
||||||
if (oldStatus) {
|
if (oldStatus) {
|
||||||
return status.merge({
|
return status.merge({
|
||||||
search_index: oldStatus.get('search_index'),
|
search_index: oldStatus.search_index,
|
||||||
contentHtml: oldStatus.get('contentHtml'),
|
contentHtml: oldStatus.contentHtml,
|
||||||
spoilerHtml: oldStatus.get('spoilerHtml'),
|
spoilerHtml: oldStatus.spoilerHtml,
|
||||||
hidden: oldStatus.get('hidden'),
|
hidden: oldStatus.hidden,
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
const spoilerText = status.get('spoiler_text') || '';
|
const spoilerText = status.spoiler_text;
|
||||||
const searchContent = (ImmutableList([spoilerText, status.get('content')]).concat(status.getIn(['poll', 'options'], ImmutableList()).map(option => option.get('title')))).join('\n\n').replace(/<br\s*\/?>/g, '\n').replace(/<\/p><p>/g, '\n\n');
|
const searchContent = (ImmutableList([spoilerText, status.content]).concat(status.poll?.options).map(option => option.title)).join('\n\n').replace(/<br\s*\/?>/g, '\n').replace(/<\/p><p>/g, '\n\n');
|
||||||
const emojiMap = makeEmojiMap(status.get('emojis'));
|
const emojiMap = makeEmojiMap(status.emojis);
|
||||||
|
|
||||||
return status.merge({
|
return status.merge({
|
||||||
search_index: domParser.parseFromString(searchContent, 'text/html').documentElement.textContent,
|
search_index: domParser.parseFromString(searchContent, 'text/html').documentElement.textContent,
|
||||||
contentHtml: stripCompatibilityFeatures(emojify(status.get('content'), emojiMap)),
|
contentHtml: stripCompatibilityFeatures(emojify(status.content, emojiMap)),
|
||||||
spoilerHtml: emojify(escapeTextContentForBrowser(spoilerText), emojiMap),
|
spoilerHtml: emojify(escapeTextContentForBrowser(spoilerText), emojiMap),
|
||||||
hidden: expandSpoilers ? false : spoilerText.length > 0 || status.get('sensitive'),
|
hidden: expandSpoilers ? false : spoilerText.length > 0 || status.sensitive,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
// Check whether a status is a quote by secondary characteristics
|
// Check whether a status is a quote by secondary characteristics
|
||||||
const isQuote = status => {
|
const isQuote = (status: StatusRecord) => {
|
||||||
return Boolean(status.get('quote_id') || status.getIn(['pleroma', 'quote_url']));
|
return Boolean(status.getIn(['pleroma', 'quote_url']));
|
||||||
};
|
};
|
||||||
|
|
||||||
// Preserve quote if an existing status already has it
|
// Preserve quote if an existing status already has it
|
||||||
const fixQuote = (status, oldStatus) => {
|
const fixQuote = (status: StatusRecord, oldStatus: StatusRecord): StatusRecord => {
|
||||||
if (oldStatus && !status.get('quote') && isQuote(status)) {
|
if (oldStatus && !status.quote && isQuote(status)) {
|
||||||
return status
|
return status
|
||||||
.set('quote', oldStatus.get('quote'))
|
.set('quote', oldStatus.quote)
|
||||||
.updateIn(['pleroma', 'quote_visible'], visible => visible || oldStatus.getIn(['pleroma', 'quote_visible']));
|
.updateIn(['pleroma', 'quote_visible'], visible => visible || oldStatus.getIn(['pleroma', 'quote_visible']));
|
||||||
} else {
|
} else {
|
||||||
return status;
|
return status;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
const fixStatus = (state, status, expandSpoilers) => {
|
const fixStatus = (state: State, status: APIEntity, expandSpoilers: boolean): StatusRecord => {
|
||||||
const oldStatus = state.get(status.get('id'));
|
const oldStatus: StatusRecord = state.get(status.id);
|
||||||
|
|
||||||
return normalizeStatus(status).withMutations(status => {
|
return normalizeStatus(status).withMutations(status => {
|
||||||
fixQuote(status, oldStatus);
|
fixQuote(status, oldStatus);
|
||||||
|
@ -92,13 +103,13 @@ const fixStatus = (state, status, expandSpoilers) => {
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
const importStatus = (state, status, expandSpoilers) =>
|
const importStatus = (state: State, status: APIEntity, expandSpoilers: boolean): State =>
|
||||||
state.set(status.id, fixStatus(state, fromJS(status), expandSpoilers));
|
state.set(status.id, fixStatus(state, status, expandSpoilers));
|
||||||
|
|
||||||
const importStatuses = (state, statuses, expandSpoilers) =>
|
const importStatuses = (state: State, statuses: APIEntities, expandSpoilers: boolean): State =>
|
||||||
state.withMutations(mutable => statuses.forEach(status => importStatus(mutable, status, expandSpoilers)));
|
state.withMutations(mutable => statuses.forEach(status => importStatus(mutable, status, expandSpoilers)));
|
||||||
|
|
||||||
const deleteStatus = (state, id, references) => {
|
const deleteStatus = (state: State, id: string, references: Array<string>) => {
|
||||||
references.forEach(ref => {
|
references.forEach(ref => {
|
||||||
state = deleteStatus(state, ref[0], []);
|
state = deleteStatus(state, ref[0], []);
|
||||||
});
|
});
|
||||||
|
@ -106,25 +117,25 @@ const deleteStatus = (state, id, references) => {
|
||||||
return state.delete(id);
|
return state.delete(id);
|
||||||
};
|
};
|
||||||
|
|
||||||
const importPendingStatus = (state, { in_reply_to_id }) => {
|
const importPendingStatus = (state: State, { in_reply_to_id }: APIEntity) => {
|
||||||
if (in_reply_to_id) {
|
if (in_reply_to_id) {
|
||||||
return state.updateIn([in_reply_to_id, 'replies_count'], 0, count => count + 1);
|
return state.updateIn([in_reply_to_id, 'replies_count'], 0, (count: number) => count + 1);
|
||||||
} else {
|
} else {
|
||||||
return state;
|
return state;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
const deletePendingStatus = (state, { in_reply_to_id }) => {
|
const deletePendingStatus = (state: State, { in_reply_to_id }: APIEntity) => {
|
||||||
if (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));
|
return state.updateIn([in_reply_to_id, 'replies_count'], 0, (count: number) => Math.max(0, count - 1));
|
||||||
} else {
|
} else {
|
||||||
return state;
|
return state;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
const initialState = ImmutableMap();
|
const initialState: State = ImmutableMap();
|
||||||
|
|
||||||
export default function statuses(state = initialState, action) {
|
export default function statuses(state = initialState, action: AnyAction): State {
|
||||||
switch(action.type) {
|
switch(action.type) {
|
||||||
case STATUS_IMPORT:
|
case STATUS_IMPORT:
|
||||||
return importStatus(state, action.status, action.expandSpoilers);
|
return importStatus(state, action.status, action.expandSpoilers);
|
||||||
|
@ -172,7 +183,7 @@ export default function statuses(state = initialState, action) {
|
||||||
return state.setIn([action.id, 'muted'], false);
|
return state.setIn([action.id, 'muted'], false);
|
||||||
case STATUS_REVEAL:
|
case STATUS_REVEAL:
|
||||||
return state.withMutations(map => {
|
return state.withMutations(map => {
|
||||||
action.ids.forEach(id => {
|
action.ids.forEach((id: string) => {
|
||||||
if (!(state.get(id) === undefined)) {
|
if (!(state.get(id) === undefined)) {
|
||||||
map.setIn([id, 'hidden'], false);
|
map.setIn([id, 'hidden'], false);
|
||||||
}
|
}
|
||||||
|
@ -180,7 +191,7 @@ export default function statuses(state = initialState, action) {
|
||||||
});
|
});
|
||||||
case STATUS_HIDE:
|
case STATUS_HIDE:
|
||||||
return state.withMutations(map => {
|
return state.withMutations(map => {
|
||||||
action.ids.forEach(id => {
|
action.ids.forEach((id: string) => {
|
||||||
if (!(state.get(id) === undefined)) {
|
if (!(state.get(id) === undefined)) {
|
||||||
map.setIn([id, 'hidden'], true);
|
map.setIn([id, 'hidden'], true);
|
||||||
}
|
}
|
Loading…
Reference in New Issue