2023-05-23 15:15:17 +00:00
|
|
|
import { Map as ImmutableMap, List as ImmutableList } from 'immutable';
|
|
|
|
|
2017-11-26 00:45:17 +00:00
|
|
|
import {
|
2023-11-03 15:00:03 +00:00
|
|
|
blockAccountSuccess,
|
|
|
|
muteAccountSuccess,
|
2017-11-26 00:45:17 +00:00
|
|
|
} from '../actions/accounts';
|
2017-06-11 15:07:35 +00:00
|
|
|
import { CONTEXT_FETCH_SUCCESS } from '../actions/statuses';
|
2018-05-25 16:46:28 +00:00
|
|
|
import { TIMELINE_DELETE, TIMELINE_UPDATE } from '../actions/timelines';
|
2023-05-09 01:11:56 +00:00
|
|
|
import { compareId } from '../compare_id';
|
2017-06-11 15:07:35 +00:00
|
|
|
|
2017-07-10 23:00:14 +00:00
|
|
|
const initialState = ImmutableMap({
|
2018-05-25 16:46:28 +00:00
|
|
|
inReplyTos: ImmutableMap(),
|
|
|
|
replies: ImmutableMap(),
|
2017-06-11 15:07:35 +00:00
|
|
|
});
|
|
|
|
|
2018-05-25 16:46:28 +00:00
|
|
|
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 }) {
|
2018-05-29 15:42:29 +00:00
|
|
|
if (in_reply_to_id && !inReplyTos.has(id)) {
|
2017-06-11 15:07:35 +00:00
|
|
|
|
2018-05-29 15:42:29 +00:00
|
|
|
replies.update(in_reply_to_id, ImmutableList(), siblings => {
|
|
|
|
const index = siblings.findLastIndex(sibling => compareId(sibling, id) < 0);
|
|
|
|
return siblings.insert(index + 1, id);
|
|
|
|
});
|
2018-05-25 16:46:28 +00:00
|
|
|
|
|
|
|
inReplyTos.set(id, in_reply_to_id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-29 15:42:29 +00:00
|
|
|
// We know in_reply_to_id of statuses but `id` itself.
|
|
|
|
// So we assume that the status of the id replies to last ancestors.
|
2018-05-25 16:46:28 +00:00
|
|
|
|
2018-05-29 15:42:29 +00:00
|
|
|
ancestors.forEach(addReply);
|
|
|
|
|
|
|
|
if (ancestors[0]) {
|
|
|
|
addReply({ id, in_reply_to_id: ancestors[ancestors.length - 1].id });
|
2018-05-25 16:46:28 +00:00
|
|
|
}
|
|
|
|
|
2018-05-29 15:42:29 +00:00
|
|
|
descendants.forEach(addReply);
|
2018-05-25 16:46:28 +00:00
|
|
|
}));
|
|
|
|
}));
|
|
|
|
});
|
2017-06-11 15:07:35 +00:00
|
|
|
|
2018-04-16 09:34:34 +00:00
|
|
|
const deleteFromContexts = (immutableState, ids) => immutableState.withMutations(state => {
|
2018-05-25 16:46:28 +00:00
|
|
|
state.update('inReplyTos', immutableAncestors => immutableAncestors.withMutations(inReplyTos => {
|
|
|
|
state.update('replies', immutableDescendants => immutableDescendants.withMutations(replies => {
|
2018-04-16 09:34:34 +00:00
|
|
|
ids.forEach(id => {
|
2018-05-25 16:46:28 +00:00
|
|
|
const inReplyToIdOfId = inReplyTos.get(id);
|
|
|
|
const repliesOfId = replies.get(id);
|
|
|
|
const siblings = replies.get(inReplyToIdOfId);
|
2017-06-11 15:07:35 +00:00
|
|
|
|
2018-05-25 16:46:28 +00:00
|
|
|
if (siblings) {
|
|
|
|
replies.set(inReplyToIdOfId, siblings.filterNot(sibling => sibling === id));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (repliesOfId) {
|
|
|
|
repliesOfId.forEach(reply => inReplyTos.delete(reply));
|
|
|
|
}
|
2017-06-11 15:07:35 +00:00
|
|
|
|
2018-05-25 16:46:28 +00:00
|
|
|
inReplyTos.delete(id);
|
|
|
|
replies.delete(id);
|
2018-04-16 09:34:34 +00:00
|
|
|
});
|
|
|
|
}));
|
|
|
|
}));
|
|
|
|
});
|
2017-06-11 15:07:35 +00:00
|
|
|
|
2018-04-16 09:34:34 +00:00
|
|
|
const filterContexts = (state, relationship, statuses) => {
|
2018-04-18 11:09:06 +00:00
|
|
|
const ownedStatusIds = statuses
|
|
|
|
.filter(status => status.get('account') === relationship.id)
|
|
|
|
.map(status => status.get('id'));
|
2017-06-11 15:07:35 +00:00
|
|
|
|
2018-04-16 09:34:34 +00:00
|
|
|
return deleteFromContexts(state, ownedStatusIds);
|
2017-11-26 00:45:17 +00:00
|
|
|
};
|
|
|
|
|
2018-05-25 16:46:28 +00:00
|
|
|
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());
|
2017-10-07 10:16:39 +00:00
|
|
|
|
2018-05-25 16:46:28 +00:00
|
|
|
mutable.setIn(['inReplyTos', status.id], status.in_reply_to_id);
|
|
|
|
|
|
|
|
if (!replies.includes(status.id)) {
|
2018-05-29 15:42:29 +00:00
|
|
|
mutable.setIn(['replies', status.in_reply_to_id], replies.push(status.id));
|
2018-05-25 16:46:28 +00:00
|
|
|
}
|
2017-10-03 23:01:44 +00:00
|
|
|
});
|
2018-05-25 16:46:28 +00:00
|
|
|
}
|
2017-10-03 23:01:44 +00:00
|
|
|
|
2018-05-25 16:46:28 +00:00
|
|
|
return state;
|
2017-10-03 23:01:44 +00:00
|
|
|
};
|
|
|
|
|
2018-05-25 16:46:28 +00:00
|
|
|
export default function replies(state = initialState, action) {
|
2017-06-11 15:07:35 +00:00
|
|
|
switch(action.type) {
|
2023-11-03 15:00:03 +00:00
|
|
|
case blockAccountSuccess.type:
|
|
|
|
case muteAccountSuccess.type:
|
|
|
|
return filterContexts(state, action.payload.relationship, action.payload.statuses);
|
2017-06-11 15:07:35 +00:00
|
|
|
case CONTEXT_FETCH_SUCCESS:
|
2017-10-03 23:01:44 +00:00
|
|
|
return normalizeContext(state, action.id, action.ancestors, action.descendants);
|
2017-06-11 15:07:35 +00:00
|
|
|
case TIMELINE_DELETE:
|
2018-04-16 09:34:34 +00:00
|
|
|
return deleteFromContexts(state, [action.id]);
|
2018-05-25 16:46:28 +00:00
|
|
|
case TIMELINE_UPDATE:
|
|
|
|
return updateContext(state, action.status);
|
2017-06-11 15:07:35 +00:00
|
|
|
default:
|
|
|
|
return state;
|
|
|
|
}
|
2022-12-18 15:51:37 +00:00
|
|
|
}
|