Refactor context reducer to TypeScript (#34506)

This commit is contained in:
Eugen Rochko 2025-04-28 15:38:40 +02:00 committed by GitHub
parent bd9223f0b9
commit 17d8e2b6e3
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
13 changed files with 308 additions and 234 deletions

View file

@ -1,109 +0,0 @@
import { Map as ImmutableMap, List as ImmutableList } from 'immutable';
import { timelineDelete } from 'mastodon/actions/timelines_typed';
import {
blockAccountSuccess,
muteAccountSuccess,
} from '../actions/accounts';
import { CONTEXT_FETCH_SUCCESS } from '../actions/statuses';
import { TIMELINE_UPDATE } from '../actions/timelines';
import { compareId } from '../compare_id';
const initialState = ImmutableMap({
inReplyTos: ImmutableMap(),
replies: ImmutableMap(),
});
const normalizeContext = (immutableState, id, ancestors, descendants) => immutableState.withMutations(state => {
state.update('inReplyTos', immutableAncestors => immutableAncestors.withMutations(inReplyTos => {
state.update('replies', immutableDescendants => immutableDescendants.withMutations(replies => {
function addReply({ id, in_reply_to_id }) {
if (in_reply_to_id && !inReplyTos.has(id)) {
replies.update(in_reply_to_id, ImmutableList(), siblings => {
const index = siblings.findLastIndex(sibling => compareId(sibling, id) < 0);
return siblings.insert(index + 1, id);
});
inReplyTos.set(id, in_reply_to_id);
}
}
// We know in_reply_to_id of statuses but `id` itself.
// So we assume that the status of the id replies to last ancestors.
ancestors.forEach(addReply);
if (ancestors[0]) {
addReply({ id, in_reply_to_id: ancestors[ancestors.length - 1].id });
}
descendants.forEach(addReply);
}));
}));
});
const deleteFromContexts = (immutableState, ids) => immutableState.withMutations(state => {
state.update('inReplyTos', immutableAncestors => immutableAncestors.withMutations(inReplyTos => {
state.update('replies', immutableDescendants => immutableDescendants.withMutations(replies => {
ids.forEach(id => {
const inReplyToIdOfId = inReplyTos.get(id);
const repliesOfId = replies.get(id);
const siblings = replies.get(inReplyToIdOfId);
if (siblings) {
replies.set(inReplyToIdOfId, siblings.filterNot(sibling => sibling === id));
}
if (repliesOfId) {
repliesOfId.forEach(reply => inReplyTos.delete(reply));
}
inReplyTos.delete(id);
replies.delete(id);
});
}));
}));
});
const filterContexts = (state, relationship, statuses) => {
const ownedStatusIds = statuses
.filter(status => status.get('account') === relationship.id)
.map(status => status.get('id'));
return deleteFromContexts(state, ownedStatusIds);
};
const updateContext = (state, status) => {
if (status.in_reply_to_id) {
return state.withMutations(mutable => {
const replies = mutable.getIn(['replies', status.in_reply_to_id], ImmutableList());
mutable.setIn(['inReplyTos', status.id], status.in_reply_to_id);
if (!replies.includes(status.id)) {
mutable.setIn(['replies', status.in_reply_to_id], replies.push(status.id));
}
});
}
return state;
};
export default function replies(state = initialState, action) {
switch(action.type) {
case blockAccountSuccess.type:
case muteAccountSuccess.type:
return filterContexts(state, action.payload.relationship, action.payload.statuses);
case CONTEXT_FETCH_SUCCESS:
return normalizeContext(state, action.id, action.ancestors, action.descendants);
case timelineDelete.type:
return deleteFromContexts(state, [action.payload.statusId]);
case TIMELINE_UPDATE:
return updateContext(state, action.status);
default:
return state;
}
}

View file

@ -0,0 +1,155 @@
/* eslint-disable @typescript-eslint/no-dynamic-delete */
import { createReducer } from '@reduxjs/toolkit';
import type { Draft, UnknownAction } from '@reduxjs/toolkit';
import type { List as ImmutableList } from 'immutable';
import { timelineDelete } from 'mastodon/actions/timelines_typed';
import type { ApiRelationshipJSON } from 'mastodon/api_types/relationships';
import type {
ApiStatusJSON,
ApiContextJSON,
} from 'mastodon/api_types/statuses';
import type { Status } from 'mastodon/models/status';
import { blockAccountSuccess, muteAccountSuccess } from '../actions/accounts';
import { fetchContext } from '../actions/statuses';
import { TIMELINE_UPDATE } from '../actions/timelines';
import { compareId } from '../compare_id';
interface TimelineUpdateAction extends UnknownAction {
timeline: string;
status: ApiStatusJSON;
usePendingItems: boolean;
}
interface State {
inReplyTos: Record<string, string>;
replies: Record<string, string[]>;
}
const initialState: State = {
inReplyTos: {},
replies: {},
};
const normalizeContext = (
state: Draft<State>,
id: string,
{ ancestors, descendants }: ApiContextJSON,
): void => {
const addReply = ({
id,
in_reply_to_id,
}: {
id: string;
in_reply_to_id?: string;
}) => {
if (!in_reply_to_id) {
return;
}
if (!state.inReplyTos[id]) {
const siblings = (state.replies[in_reply_to_id] ??= []);
const index = siblings.findIndex((sibling) => compareId(sibling, id) < 0);
siblings.splice(index + 1, 0, id);
state.inReplyTos[id] = in_reply_to_id;
}
};
// We know in_reply_to_id of statuses but `id` itself.
// So we assume that the status of the id replies to last ancestors.
ancestors.forEach(addReply);
if (ancestors[0]) {
addReply({
id,
in_reply_to_id: ancestors[ancestors.length - 1]?.id,
});
}
descendants.forEach(addReply);
};
const deleteFromContexts = (state: Draft<State>, ids: string[]): void => {
ids.forEach((id) => {
const inReplyToIdOfId = state.inReplyTos[id];
const repliesOfId = state.replies[id];
if (inReplyToIdOfId) {
const siblings = state.replies[inReplyToIdOfId];
if (siblings) {
state.replies[inReplyToIdOfId] = siblings.filter(
(sibling) => sibling !== id,
);
}
}
if (repliesOfId) {
repliesOfId.forEach((reply) => {
delete state.inReplyTos[reply];
});
}
delete state.inReplyTos[id];
delete state.replies[id];
});
};
const filterContexts = (
state: Draft<State>,
relationship: ApiRelationshipJSON,
statuses: ImmutableList<Status>,
): void => {
const ownedStatusIds = statuses
.filter((status) => (status.get('account') as string) === relationship.id)
.map((status) => status.get('id') as string);
deleteFromContexts(state, ownedStatusIds.toArray());
};
const updateContext = (state: Draft<State>, status: ApiStatusJSON): void => {
if (!status.in_reply_to_id) {
return;
}
const siblings = (state.replies[status.in_reply_to_id] ??= []);
state.inReplyTos[status.id] = status.in_reply_to_id;
if (!siblings.includes(status.id)) {
siblings.push(status.id);
}
};
export const contextsReducer = createReducer(initialState, (builder) => {
builder
.addCase(fetchContext.fulfilled, (state, action) => {
normalizeContext(state, action.meta.arg.statusId, action.payload.context);
})
.addCase(blockAccountSuccess, (state, action) => {
filterContexts(
state,
action.payload.relationship,
action.payload.statuses as ImmutableList<Status>,
);
})
.addCase(muteAccountSuccess, (state, action) => {
filterContexts(
state,
action.payload.relationship,
action.payload.statuses as ImmutableList<Status>,
);
})
.addCase(timelineDelete, (state, action) => {
deleteFromContexts(state, [action.payload.statusId]);
})
.addMatcher(
(action: UnknownAction): action is TimelineUpdateAction =>
action.type === TIMELINE_UPDATE,
(state, action) => {
updateContext(state, action.status);
},
);
});

View file

@ -8,7 +8,7 @@ import accounts_map from './accounts_map';
import { alertsReducer } from './alerts';
import announcements from './announcements';
import { composeReducer } from './compose';
import contexts from './contexts';
import { contextsReducer } from './contexts';
import conversations from './conversations';
import custom_emojis from './custom_emojis';
import { dropdownMenuReducer } from './dropdown_menu';
@ -55,7 +55,7 @@ const reducers = {
settings,
push_notifications,
server,
contexts,
contexts: contextsReducer,
compose: composeReducer,
search: searchReducer,
media_attachments,

View file

@ -64,6 +64,7 @@ const statusTranslateUndo = (state, id) => {
});
};
/** @type {ImmutableMap<string, ImmutableMap<string, any>>} */
const initialState = ImmutableMap();
/** @type {import('@reduxjs/toolkit').Reducer<typeof initialState>} */