When a streaming API status arrives, sort it into conversations (#5206)

rebase/4.0.0rc2
Eugen Rochko 2017-10-04 01:01:44 +02:00 committed by GitHub
parent cdd5ef691b
commit ec13cfa4f9
2 changed files with 37 additions and 5 deletions

View File

@ -17,6 +17,8 @@ export const TIMELINE_SCROLL_TOP = 'TIMELINE_SCROLL_TOP';
export const TIMELINE_CONNECT = 'TIMELINE_CONNECT'; export const TIMELINE_CONNECT = 'TIMELINE_CONNECT';
export const TIMELINE_DISCONNECT = 'TIMELINE_DISCONNECT'; export const TIMELINE_DISCONNECT = 'TIMELINE_DISCONNECT';
export const TIMELINE_CONTEXT_UPDATE = 'CONTEXT_UPDATE';
export function refreshTimelineSuccess(timeline, statuses, skipLoading, next) { export function refreshTimelineSuccess(timeline, statuses, skipLoading, next) {
return { return {
type: TIMELINE_REFRESH_SUCCESS, type: TIMELINE_REFRESH_SUCCESS,
@ -30,6 +32,16 @@ export function refreshTimelineSuccess(timeline, statuses, skipLoading, next) {
export function updateTimeline(timeline, status) { export function updateTimeline(timeline, status) {
return (dispatch, getState) => { return (dispatch, getState) => {
const references = status.reblog ? getState().get('statuses').filter((item, itemId) => (itemId === status.reblog.id || item.get('reblog') === status.reblog.id)).map((_, itemId) => itemId) : []; const references = status.reblog ? getState().get('statuses').filter((item, itemId) => (itemId === status.reblog.id || item.get('reblog') === status.reblog.id)).map((_, itemId) => itemId) : [];
const parents = [];
if (status.in_reply_to_id) {
let parent = getState().getIn(['statuses', status.in_reply_to_id]);
while (parent.get('in_reply_to_id')) {
parents.push(parent.get('id'));
parent = getState().getIn(['statuses', parent.get('in_reply_to_id')]);
}
}
dispatch({ dispatch({
type: TIMELINE_UPDATE, type: TIMELINE_UPDATE,
@ -37,6 +49,14 @@ export function updateTimeline(timeline, status) {
status, status,
references, references,
}); });
if (parents.length > 0) {
dispatch({
type: TIMELINE_CONTEXT_UPDATE,
status,
references: parents,
});
}
}; };
}; };

View File

@ -1,6 +1,6 @@
import { CONTEXT_FETCH_SUCCESS } from '../actions/statuses'; import { CONTEXT_FETCH_SUCCESS } from '../actions/statuses';
import { TIMELINE_DELETE } from '../actions/timelines'; import { TIMELINE_DELETE, TIMELINE_CONTEXT_UPDATE } from '../actions/timelines';
import { Map as ImmutableMap, List as ImmutableList, fromJS } from 'immutable'; import { Map as ImmutableMap, List as ImmutableList } from 'immutable';
const initialState = ImmutableMap({ const initialState = ImmutableMap({
ancestors: ImmutableMap(), ancestors: ImmutableMap(),
@ -8,8 +8,8 @@ const initialState = ImmutableMap({
}); });
const normalizeContext = (state, id, ancestors, descendants) => { const normalizeContext = (state, id, ancestors, descendants) => {
const ancestorsIds = ancestors.map(ancestor => ancestor.get('id')); const ancestorsIds = ImmutableList(ancestors.map(ancestor => ancestor.id));
const descendantsIds = descendants.map(descendant => descendant.get('id')); const descendantsIds = ImmutableList(descendants.map(descendant => descendant.id));
return state.withMutations(map => { return state.withMutations(map => {
map.setIn(['ancestors', id], ancestorsIds); map.setIn(['ancestors', id], ancestorsIds);
@ -31,12 +31,24 @@ const deleteFromContexts = (state, id) => {
return state; return state;
}; };
const updateContext = (state, status, references) => {
return state.update('descendants', map => {
references.forEach(parentId => {
map = map.update(parentId, ImmutableList(), list => list.push(status.id));
});
return map;
});
};
export default function contexts(state = initialState, action) { export default function contexts(state = initialState, action) {
switch(action.type) { switch(action.type) {
case CONTEXT_FETCH_SUCCESS: case CONTEXT_FETCH_SUCCESS:
return normalizeContext(state, action.id, fromJS(action.ancestors), fromJS(action.descendants)); return normalizeContext(state, action.id, action.ancestors, action.descendants);
case TIMELINE_DELETE: case TIMELINE_DELETE:
return deleteFromContexts(state, action.id); return deleteFromContexts(state, action.id);
case TIMELINE_CONTEXT_UPDATE:
return updateContext(state, action.status, action.references);
default: default:
return state; return state;
} }