2017-11-25 19:45:17 -05:00
|
|
|
import {
|
|
|
|
ACCOUNT_BLOCK_SUCCESS,
|
|
|
|
ACCOUNT_MUTE_SUCCESS,
|
|
|
|
} from '../actions/accounts';
|
2017-06-11 11:07:35 -04:00
|
|
|
import { CONTEXT_FETCH_SUCCESS } from '../actions/statuses';
|
2017-10-03 19:01:44 -04:00
|
|
|
import { TIMELINE_DELETE, TIMELINE_CONTEXT_UPDATE } from '../actions/timelines';
|
|
|
|
import { Map as ImmutableMap, List as ImmutableList } from 'immutable';
|
2017-06-11 11:07:35 -04:00
|
|
|
|
2017-07-10 19:00:14 -04:00
|
|
|
const initialState = ImmutableMap({
|
|
|
|
ancestors: ImmutableMap(),
|
|
|
|
descendants: ImmutableMap(),
|
2017-06-11 11:07:35 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
const normalizeContext = (state, id, ancestors, descendants) => {
|
2017-10-03 19:01:44 -04:00
|
|
|
const ancestorsIds = ImmutableList(ancestors.map(ancestor => ancestor.id));
|
|
|
|
const descendantsIds = ImmutableList(descendants.map(descendant => descendant.id));
|
2017-06-11 11:07:35 -04:00
|
|
|
|
|
|
|
return state.withMutations(map => {
|
|
|
|
map.setIn(['ancestors', id], ancestorsIds);
|
|
|
|
map.setIn(['descendants', id], descendantsIds);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2018-04-16 05:34:34 -04:00
|
|
|
const deleteFromContexts = (immutableState, ids) => immutableState.withMutations(state => {
|
|
|
|
state.update('ancestors', immutableAncestors => immutableAncestors.withMutations(ancestors => {
|
|
|
|
state.update('descendants', immutableDescendants => immutableDescendants.withMutations(descendants => {
|
|
|
|
ids.forEach(id => {
|
|
|
|
descendants.get(id, ImmutableList()).forEach(descendantId => {
|
|
|
|
ancestors.update(descendantId, ImmutableList(), list => list.filterNot(itemId => itemId === id));
|
|
|
|
});
|
2017-06-11 11:07:35 -04:00
|
|
|
|
2018-04-16 05:34:34 -04:00
|
|
|
ancestors.get(id, ImmutableList()).forEach(ancestorId => {
|
|
|
|
descendants.update(ancestorId, ImmutableList(), list => list.filterNot(itemId => itemId === id));
|
|
|
|
});
|
2017-06-11 11:07:35 -04:00
|
|
|
|
2018-04-16 05:34:34 -04:00
|
|
|
descendants.delete(id);
|
|
|
|
ancestors.delete(id);
|
|
|
|
});
|
|
|
|
}));
|
|
|
|
}));
|
|
|
|
});
|
2017-06-11 11:07:35 -04:00
|
|
|
|
2018-04-16 05:34:34 -04:00
|
|
|
const filterContexts = (state, relationship, statuses) => {
|
2018-04-18 07:09:06 -04:00
|
|
|
const ownedStatusIds = statuses
|
|
|
|
.filter(status => status.get('account') === relationship.id)
|
|
|
|
.map(status => status.get('id'));
|
2017-06-11 11:07:35 -04:00
|
|
|
|
2018-04-16 05:34:34 -04:00
|
|
|
return deleteFromContexts(state, ownedStatusIds);
|
2017-11-25 19:45:17 -05:00
|
|
|
};
|
|
|
|
|
2017-10-03 19:01:44 -04:00
|
|
|
const updateContext = (state, status, references) => {
|
|
|
|
return state.update('descendants', map => {
|
|
|
|
references.forEach(parentId => {
|
2017-10-07 06:16:39 -04:00
|
|
|
map = map.update(parentId, ImmutableList(), list => {
|
|
|
|
if (list.includes(status.id)) {
|
|
|
|
return list;
|
|
|
|
}
|
|
|
|
|
|
|
|
return list.push(status.id);
|
|
|
|
});
|
2017-10-03 19:01:44 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
return map;
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2017-06-11 11:07:35 -04:00
|
|
|
export default function contexts(state = initialState, action) {
|
|
|
|
switch(action.type) {
|
2017-11-25 19:45:17 -05:00
|
|
|
case ACCOUNT_BLOCK_SUCCESS:
|
|
|
|
case ACCOUNT_MUTE_SUCCESS:
|
2018-04-16 05:34:34 -04:00
|
|
|
return filterContexts(state, action.relationship, action.statuses);
|
2017-06-11 11:07:35 -04:00
|
|
|
case CONTEXT_FETCH_SUCCESS:
|
2017-10-03 19:01:44 -04:00
|
|
|
return normalizeContext(state, action.id, action.ancestors, action.descendants);
|
2017-06-11 11:07:35 -04:00
|
|
|
case TIMELINE_DELETE:
|
2018-04-16 05:34:34 -04:00
|
|
|
return deleteFromContexts(state, [action.id]);
|
2017-10-03 19:01:44 -04:00
|
|
|
case TIMELINE_CONTEXT_UPDATE:
|
|
|
|
return updateContext(state, action.status, action.references);
|
2017-06-11 11:07:35 -04:00
|
|
|
default:
|
|
|
|
return state;
|
|
|
|
}
|
|
|
|
};
|