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