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