2023-05-28 09:38:10 -05:00
|
|
|
import { Map as ImmutableMap, List as ImmutableList } from 'immutable';
|
|
|
|
|
2018-04-19 09:42:27 -05:00
|
|
|
import {
|
|
|
|
ACCOUNT_BLOCK_SUCCESS,
|
|
|
|
ACCOUNT_MUTE_SUCCESS,
|
|
|
|
} from 'flavours/glitch/actions/accounts';
|
2017-12-04 01:26:40 -06:00
|
|
|
import { CONTEXT_FETCH_SUCCESS } from 'flavours/glitch/actions/statuses';
|
2018-11-30 05:17:56 -06:00
|
|
|
import { TIMELINE_DELETE, TIMELINE_UPDATE } from 'flavours/glitch/actions/timelines';
|
2023-05-28 09:38:10 -05:00
|
|
|
|
2023-05-08 20:11:56 -05:00
|
|
|
import { compareId } from '../compare_id';
|
2017-06-11 10:07:35 -05:00
|
|
|
|
2017-07-10 18:00:14 -05:00
|
|
|
const initialState = ImmutableMap({
|
2018-11-30 05:17:56 -06:00
|
|
|
inReplyTos: ImmutableMap(),
|
|
|
|
replies: ImmutableMap(),
|
2017-06-11 10:07:35 -05:00
|
|
|
});
|
|
|
|
|
2018-11-30 05:17:56 -06: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-11-30 05:23:07 -06:00
|
|
|
if (in_reply_to_id && !inReplyTos.has(id)) {
|
2017-06-11 10:07:35 -05:00
|
|
|
|
2018-11-30 05:23:07 -06: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-11-30 05:17:56 -06:00
|
|
|
|
|
|
|
inReplyTos.set(id, in_reply_to_id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-30 05:23:07 -06: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-11-30 05:17:56 -06:00
|
|
|
|
2018-11-30 05:23:07 -06:00
|
|
|
ancestors.forEach(addReply);
|
|
|
|
|
|
|
|
if (ancestors[0]) {
|
|
|
|
addReply({ id, in_reply_to_id: ancestors[ancestors.length - 1].id });
|
2018-11-30 05:17:56 -06:00
|
|
|
}
|
|
|
|
|
2018-11-30 05:23:07 -06:00
|
|
|
descendants.forEach(addReply);
|
2018-11-30 05:17:56 -06:00
|
|
|
}));
|
|
|
|
}));
|
|
|
|
});
|
2017-06-11 10:07:35 -05:00
|
|
|
|
2018-04-19 09:43:02 -05:00
|
|
|
const deleteFromContexts = (immutableState, ids) => immutableState.withMutations(state => {
|
2018-11-30 05:17:56 -06:00
|
|
|
state.update('inReplyTos', immutableAncestors => immutableAncestors.withMutations(inReplyTos => {
|
|
|
|
state.update('replies', immutableDescendants => immutableDescendants.withMutations(replies => {
|
2018-04-19 09:43:02 -05:00
|
|
|
ids.forEach(id => {
|
2018-11-30 05:17:56 -06:00
|
|
|
const inReplyToIdOfId = inReplyTos.get(id);
|
|
|
|
const repliesOfId = replies.get(id);
|
|
|
|
const siblings = replies.get(inReplyToIdOfId);
|
2017-06-11 10:07:35 -05:00
|
|
|
|
2018-11-30 05:17:56 -06:00
|
|
|
if (siblings) {
|
|
|
|
replies.set(inReplyToIdOfId, siblings.filterNot(sibling => sibling === id));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (repliesOfId) {
|
|
|
|
repliesOfId.forEach(reply => inReplyTos.delete(reply));
|
|
|
|
}
|
2017-06-11 10:07:35 -05:00
|
|
|
|
2018-11-30 05:17:56 -06:00
|
|
|
inReplyTos.delete(id);
|
|
|
|
replies.delete(id);
|
2018-04-19 09:43:02 -05:00
|
|
|
});
|
|
|
|
}));
|
|
|
|
}));
|
|
|
|
});
|
2017-06-11 10:07:35 -05:00
|
|
|
|
2018-04-19 09:43:02 -05:00
|
|
|
const filterContexts = (state, relationship, statuses) => {
|
|
|
|
const ownedStatusIds = statuses.filter(status => status.get('account') === relationship.id)
|
2023-02-03 13:52:07 -06:00
|
|
|
.map(status => status.get('id'));
|
2017-06-11 10:07:35 -05:00
|
|
|
|
2018-04-19 09:43:02 -05:00
|
|
|
return deleteFromContexts(state, ownedStatusIds);
|
2018-04-19 09:42:27 -05:00
|
|
|
};
|
|
|
|
|
2018-11-30 05:17:56 -06: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 05:16:39 -05:00
|
|
|
|
2018-11-30 05:17:56 -06:00
|
|
|
mutable.setIn(['inReplyTos', status.id], status.in_reply_to_id);
|
|
|
|
|
|
|
|
if (!replies.includes(status.id)) {
|
2018-11-30 05:23:07 -06:00
|
|
|
mutable.setIn(['replies', status.in_reply_to_id], replies.push(status.id));
|
2018-11-30 05:17:56 -06:00
|
|
|
}
|
2017-10-03 18:01:44 -05:00
|
|
|
});
|
2018-11-30 05:17:56 -06:00
|
|
|
}
|
2017-10-03 18:01:44 -05:00
|
|
|
|
2018-11-30 05:17:56 -06:00
|
|
|
return state;
|
2017-10-03 18:01:44 -05:00
|
|
|
};
|
|
|
|
|
2018-11-30 05:17:56 -06:00
|
|
|
export default function replies(state = initialState, action) {
|
2017-06-11 10:07:35 -05:00
|
|
|
switch(action.type) {
|
2018-04-19 09:42:27 -05:00
|
|
|
case ACCOUNT_BLOCK_SUCCESS:
|
|
|
|
case ACCOUNT_MUTE_SUCCESS:
|
2018-04-19 09:43:02 -05:00
|
|
|
return filterContexts(state, action.relationship, action.statuses);
|
2017-06-11 10:07:35 -05:00
|
|
|
case CONTEXT_FETCH_SUCCESS:
|
2017-10-03 18:01:44 -05:00
|
|
|
return normalizeContext(state, action.id, action.ancestors, action.descendants);
|
2017-06-11 10:07:35 -05:00
|
|
|
case TIMELINE_DELETE:
|
2018-04-19 09:43:02 -05:00
|
|
|
return deleteFromContexts(state, [action.id]);
|
2018-11-30 05:17:56 -06:00
|
|
|
case TIMELINE_UPDATE:
|
|
|
|
return updateContext(state, action.status);
|
2017-06-11 10:07:35 -05:00
|
|
|
default:
|
|
|
|
return state;
|
|
|
|
}
|
2023-02-03 13:52:07 -06:00
|
|
|
}
|