[Glitch] Fix context building in the reducer

Port 7706ed038f to glitch-soc
This commit is contained in:
Thibaut Girka 2018-11-30 12:23:07 +01:00 committed by ThibG
parent 1624a95b2b
commit a28f5695f3
1 changed files with 14 additions and 13 deletions

View File

@ -5,6 +5,7 @@ import {
import { CONTEXT_FETCH_SUCCESS } from 'flavours/glitch/actions/statuses'; import { CONTEXT_FETCH_SUCCESS } from 'flavours/glitch/actions/statuses';
import { TIMELINE_DELETE, TIMELINE_UPDATE } from 'flavours/glitch/actions/timelines'; import { TIMELINE_DELETE, TIMELINE_UPDATE } from 'flavours/glitch/actions/timelines';
import { Map as ImmutableMap, List as ImmutableList } from 'immutable'; import { Map as ImmutableMap, List as ImmutableList } from 'immutable';
import compareId from 'flavours/glitch/util/compare_id';
const initialState = ImmutableMap({ const initialState = ImmutableMap({
inReplyTos: ImmutableMap(), inReplyTos: ImmutableMap(),
@ -15,27 +16,27 @@ const normalizeContext = (immutableState, id, ancestors, descendants) => immutab
state.update('inReplyTos', immutableAncestors => immutableAncestors.withMutations(inReplyTos => { state.update('inReplyTos', immutableAncestors => immutableAncestors.withMutations(inReplyTos => {
state.update('replies', immutableDescendants => immutableDescendants.withMutations(replies => { state.update('replies', immutableDescendants => immutableDescendants.withMutations(replies => {
function addReply({ id, in_reply_to_id }) { function addReply({ id, in_reply_to_id }) {
if (in_reply_to_id) { if (in_reply_to_id && !inReplyTos.has(id)) {
const siblings = replies.get(in_reply_to_id, ImmutableList());
if (!siblings.includes(id)) { replies.update(in_reply_to_id, ImmutableList(), siblings => {
const index = siblings.findLastIndex(sibling => sibling.id < id); const index = siblings.findLastIndex(sibling => compareId(sibling, id) < 0);
replies.set(in_reply_to_id, siblings.insert(index + 1, id)); return siblings.insert(index + 1, id);
} });
inReplyTos.set(id, in_reply_to_id); inReplyTos.set(id, in_reply_to_id);
} }
} }
// We know in_reply_to_id of statuses but `id` itself.
// So we assume that the status of the id replies to last ancestors.
ancestors.forEach(addReply);
if (ancestors[0]) { if (ancestors[0]) {
addReply({ id, in_reply_to_id: ancestors[0].id }); addReply({ id, in_reply_to_id: ancestors[ancestors.length - 1].id });
} }
if (descendants[0]) { descendants.forEach(addReply);
addReply({ id: descendants[0].id, in_reply_to_id: id });
}
[ancestors, descendants].forEach(statuses => statuses.forEach(addReply));
})); }));
})); }));
}); });
@ -79,7 +80,7 @@ const updateContext = (state, status) => {
mutable.setIn(['inReplyTos', status.id], status.in_reply_to_id); mutable.setIn(['inReplyTos', status.id], status.in_reply_to_id);
if (!replies.includes(status.id)) { if (!replies.includes(status.id)) {
mutable.setIn(['replies', status.id], replies.push(status.id)); mutable.setIn(['replies', status.in_reply_to_id], replies.push(status.id));
} }
}); });
} }