@ -5,6 +5,7 @@ import {
import { CONTEXT _FETCH _SUCCESS } from '../actions/statuses' ;
import { TIMELINE _DELETE , TIMELINE _UPDATE } from '../actions/timelines' ;
import { Map as ImmutableMap , List as ImmutableList } from 'immutable' ;
import compareId from '../compare_id' ;
const initialState = ImmutableMap ( {
inReplyTos : ImmutableMap ( ) ,
@ -15,27 +16,27 @@ const normalizeContext = (immutableState, id, ancestors, descendants) => immutab
state . update ( 'inReplyTos' , immutableAncestors => immutableAncestors . withMutations ( inReplyTos => {
state . update ( 'replies' , immutableDescendants => immutableDescendants . withMutations ( replies => {
function addReply ( { id , in _reply _to _id } ) {
if ( in _reply _to _id ) {
const siblings = replies . get ( in _reply _to _id , ImmutableList ( ) ) ;
if ( in _reply _to _id && ! inReplyTos . has ( id ) ) {
if ( ! siblings . includes ( id ) ) {
const index = siblings . findLastIndex ( sibling => sibling. id < id ) ;
replies . set ( in _reply _to _id , siblings . insert ( index + 1 , id ) ) ;
}
replies . update ( in _reply _to _id , ImmutableList ( ) , siblings => {
const index = siblings . findLastIndex ( sibling => compareId( sibling , id ) < 0 ) ;
return siblings . insert ( index + 1 , id ) ;
} ) ;
inReplyTos . set ( id , in _reply _to _id ) ;
}
}
if ( ancestors [ 0 ] ) {
addReply ( { id , in _reply _to _id : ancestors [ 0 ] . 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.
if ( descendants [ 0 ] ) {
addReply ( { id : descendants [ 0 ] . id , in _reply _to _id : id } ) ;
ancestors . forEach ( addReply ) ;
if ( ancestors [ 0 ] ) {
addReply ( { id , in _reply _to _id : ancestors [ ancestors . length - 1 ] . id } ) ;
}
[ ancestors , descendants ] . forEach ( statuses => statuses . forEach ( addReply ) ) ;
descendants . forEach ( addReply ) ;
} ) ) ;
} ) ) ;
} ) ;
@ -80,7 +81,7 @@ const updateContext = (state, status) => {
mutable . setIn ( [ 'inReplyTos' , status . id ] , status . in _reply _to _id ) ;
if ( ! replies . includes ( status . id ) ) {
mutable . setIn ( [ 'replies' , status . i d] , replies . push ( status . id ) ) ;
mutable . setIn ( [ 'replies' , status . i n_reply _to _i d] , replies . push ( status . id ) ) ;
}
} ) ;
}