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