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