2016-09-18 17:18:46 +01:00
|
|
|
import {
|
2016-09-20 22:18:00 +01:00
|
|
|
TIMELINE_REFRESH_SUCCESS,
|
2016-09-18 17:18:46 +01:00
|
|
|
TIMELINE_UPDATE,
|
2016-09-22 00:08:35 +01:00
|
|
|
TIMELINE_DELETE,
|
|
|
|
TIMELINE_EXPAND_SUCCESS
|
2016-10-30 14:06:43 +00:00
|
|
|
} from '../actions/timelines';
|
2016-09-18 17:18:46 +01:00
|
|
|
import {
|
|
|
|
REBLOG_SUCCESS,
|
2016-10-02 14:14:26 +01:00
|
|
|
UNREBLOG_SUCCESS,
|
|
|
|
FAVOURITE_SUCCESS,
|
|
|
|
UNFAVOURITE_SUCCESS
|
2016-10-30 14:06:43 +00:00
|
|
|
} from '../actions/interactions';
|
2016-09-18 17:18:46 +01:00
|
|
|
import {
|
|
|
|
ACCOUNT_FETCH_SUCCESS,
|
2016-09-22 19:58:35 +01:00
|
|
|
ACCOUNT_TIMELINE_FETCH_SUCCESS,
|
2016-11-23 21:57:57 +00:00
|
|
|
ACCOUNT_TIMELINE_EXPAND_SUCCESS,
|
|
|
|
ACCOUNT_BLOCK_SUCCESS
|
2016-10-30 14:06:43 +00:00
|
|
|
} from '../actions/accounts';
|
2016-09-29 23:00:45 +01:00
|
|
|
import {
|
|
|
|
STATUS_FETCH_SUCCESS,
|
2016-10-30 14:06:43 +00:00
|
|
|
CONTEXT_FETCH_SUCCESS
|
|
|
|
} from '../actions/statuses';
|
|
|
|
import Immutable from 'immutable';
|
2016-08-24 16:56:44 +01:00
|
|
|
|
2016-09-04 13:04:26 +01:00
|
|
|
const initialState = Immutable.Map({
|
2016-10-30 14:06:43 +00:00
|
|
|
home: Immutable.List(),
|
|
|
|
mentions: Immutable.List(),
|
|
|
|
public: Immutable.List(),
|
2016-11-05 14:20:05 +00:00
|
|
|
tag: Immutable.List(),
|
2016-09-18 17:18:46 +01:00
|
|
|
accounts_timelines: Immutable.Map(),
|
2016-09-15 23:21:51 +01:00
|
|
|
ancestors: Immutable.Map(),
|
2016-10-30 14:06:43 +00:00
|
|
|
descendants: Immutable.Map()
|
2016-09-04 13:04:26 +01:00
|
|
|
});
|
|
|
|
|
2016-10-30 14:06:43 +00:00
|
|
|
const normalizeStatus = (state, status) => {
|
|
|
|
const replyToId = status.get('in_reply_to_id');
|
|
|
|
const id = status.get('id');
|
2016-09-04 13:04:26 +01:00
|
|
|
|
2016-10-30 14:06:43 +00:00
|
|
|
if (replyToId) {
|
|
|
|
if (!state.getIn(['descendants', replyToId], Immutable.List()).includes(id)) {
|
|
|
|
state = state.updateIn(['descendants', replyToId], Immutable.List(), set => set.push(id));
|
|
|
|
}
|
2016-09-18 11:51:09 +01:00
|
|
|
|
2016-10-30 14:06:43 +00:00
|
|
|
if (!state.getIn(['ancestors', id], Immutable.List()).includes(replyToId)) {
|
|
|
|
state = state.updateIn(['ancestors', id], Immutable.List(), set => set.push(replyToId));
|
2016-09-18 11:51:09 +01:00
|
|
|
}
|
2016-10-30 14:06:43 +00:00
|
|
|
}
|
2016-09-18 11:51:09 +01:00
|
|
|
|
2016-10-30 14:06:43 +00:00
|
|
|
return state;
|
2016-09-04 13:04:26 +01:00
|
|
|
};
|
|
|
|
|
2016-10-30 14:06:43 +00:00
|
|
|
const normalizeTimeline = (state, timeline, statuses, replace = false) => {
|
|
|
|
let ids = Immutable.List();
|
2016-10-18 00:48:46 +01:00
|
|
|
|
2016-09-04 13:04:26 +01:00
|
|
|
statuses.forEach((status, i) => {
|
2016-09-19 22:25:59 +01:00
|
|
|
state = normalizeStatus(state, status);
|
2016-10-18 00:48:46 +01:00
|
|
|
ids = ids.set(i, status.get('id'));
|
2016-09-01 12:21:48 +01:00
|
|
|
});
|
2016-09-04 13:04:26 +01:00
|
|
|
|
2016-11-05 14:20:05 +00:00
|
|
|
return state.update(timeline, Immutable.List(), list => (replace ? ids : list.unshift(...ids)));
|
2016-09-04 13:04:26 +01:00
|
|
|
};
|
|
|
|
|
2016-10-30 14:06:43 +00:00
|
|
|
const appendNormalizedTimeline = (state, timeline, statuses) => {
|
|
|
|
let moreIds = Immutable.List();
|
2016-09-22 00:08:35 +01:00
|
|
|
|
|
|
|
statuses.forEach((status, i) => {
|
|
|
|
state = normalizeStatus(state, status);
|
|
|
|
moreIds = moreIds.set(i, status.get('id'));
|
|
|
|
});
|
|
|
|
|
2016-11-05 14:20:05 +00:00
|
|
|
return state.update(timeline, Immutable.List(), list => list.push(...moreIds));
|
2016-09-22 00:08:35 +01:00
|
|
|
};
|
|
|
|
|
2016-10-30 14:06:43 +00:00
|
|
|
const normalizeAccountTimeline = (state, accountId, statuses, replace = false) => {
|
|
|
|
let ids = Immutable.List();
|
2016-10-07 15:00:11 +01:00
|
|
|
|
2016-09-18 17:18:46 +01:00
|
|
|
statuses.forEach((status, i) => {
|
2016-09-19 22:25:59 +01:00
|
|
|
state = normalizeStatus(state, status);
|
2016-10-18 22:06:28 +01:00
|
|
|
ids = ids.set(i, status.get('id'));
|
2016-09-18 17:18:46 +01:00
|
|
|
});
|
|
|
|
|
2016-10-19 17:20:19 +01:00
|
|
|
return state.updateIn(['accounts_timelines', accountId], Immutable.List([]), list => (replace ? ids : list.unshift(...ids)));
|
2016-09-18 17:18:46 +01:00
|
|
|
};
|
|
|
|
|
2016-10-30 14:06:43 +00:00
|
|
|
const appendNormalizedAccountTimeline = (state, accountId, statuses) => {
|
2016-10-07 15:00:11 +01:00
|
|
|
let moreIds = Immutable.List([]);
|
2016-09-22 19:58:35 +01:00
|
|
|
|
|
|
|
statuses.forEach((status, i) => {
|
|
|
|
state = normalizeStatus(state, status);
|
|
|
|
moreIds = moreIds.set(i, status.get('id'));
|
|
|
|
});
|
|
|
|
|
2016-10-07 15:00:11 +01:00
|
|
|
return state.updateIn(['accounts_timelines', accountId], Immutable.List([]), list => list.push(...moreIds));
|
2016-09-22 19:58:35 +01:00
|
|
|
};
|
|
|
|
|
2016-10-30 14:06:43 +00:00
|
|
|
const updateTimeline = (state, timeline, status, references) => {
|
2016-09-19 22:25:59 +01:00
|
|
|
state = normalizeStatus(state, status);
|
2016-10-17 00:34:16 +01:00
|
|
|
|
2016-11-05 14:20:05 +00:00
|
|
|
state = state.update(timeline, Immutable.List(), list => {
|
2016-11-03 10:06:55 +00:00
|
|
|
if (list.includes(status.get('id'))) {
|
|
|
|
return list;
|
|
|
|
}
|
|
|
|
|
2016-10-17 00:34:16 +01:00
|
|
|
const reblogOfId = status.getIn(['reblog', 'id'], null);
|
|
|
|
|
|
|
|
if (reblogOfId !== null) {
|
2016-10-30 14:06:43 +00:00
|
|
|
list = list.filterNot(itemId => references.includes(itemId));
|
2016-10-17 00:34:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return list.unshift(status.get('id'));
|
|
|
|
});
|
|
|
|
|
2016-09-04 13:04:26 +01:00
|
|
|
return state;
|
2016-09-01 12:21:48 +01:00
|
|
|
};
|
|
|
|
|
2016-10-30 14:06:43 +00:00
|
|
|
const deleteStatus = (state, id, accountId, references) => {
|
2016-09-29 23:00:45 +01:00
|
|
|
// Remove references from timelines
|
2016-11-05 14:20:05 +00:00
|
|
|
['home', 'mentions', 'public', 'tag'].forEach(function (timeline) {
|
2016-09-05 00:59:46 +01:00
|
|
|
state = state.update(timeline, list => list.filterNot(item => item === id));
|
|
|
|
});
|
|
|
|
|
2016-09-29 23:00:45 +01:00
|
|
|
// Remove references from account timelines
|
2016-10-30 14:06:43 +00:00
|
|
|
state = state.updateIn(['accounts_timelines', accountId], Immutable.List([]), list => list.filterNot(item => item === id));
|
2016-09-29 23:00:45 +01:00
|
|
|
|
2016-10-30 14:06:43 +00:00
|
|
|
// Remove references from context
|
|
|
|
state.getIn(['descendants', id], Immutable.List()).forEach(descendantId => {
|
|
|
|
state = state.updateIn(['ancestors', descendantId], Immutable.List(), list => list.filterNot(itemId => itemId === id));
|
2016-09-29 23:00:45 +01:00
|
|
|
});
|
|
|
|
|
2016-10-30 14:06:43 +00:00
|
|
|
state.getIn(['ancestors', id], Immutable.List()).forEach(ancestorId => {
|
|
|
|
state = state.updateIn(['descendants', ancestorId], Immutable.List(), list => list.filterNot(itemId => itemId === id));
|
|
|
|
});
|
2016-10-15 12:48:38 +01:00
|
|
|
|
2016-10-30 14:06:43 +00:00
|
|
|
state = state.deleteIn(['descendants', id]).deleteIn(['ancestors', id]);
|
2016-09-23 19:23:26 +01:00
|
|
|
|
2016-10-30 14:06:43 +00:00
|
|
|
// Remove reblogs of deleted status
|
|
|
|
references.forEach(ref => {
|
|
|
|
state = deleteStatus(state, ref[0], ref[1], []);
|
2016-10-28 19:05:44 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
return state;
|
|
|
|
};
|
|
|
|
|
2016-11-23 21:57:57 +00:00
|
|
|
const filterTimelines = (state, relationship, statuses) => {
|
|
|
|
let references;
|
|
|
|
|
|
|
|
statuses.forEach(status => {
|
|
|
|
if (status.get('account') !== relationship.id) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
references = statuses.filter(item => item.get('reblog') === status.get('id')).map(item => [item.get('id'), item.get('account')]);
|
|
|
|
state = deleteStatus(state, status.get('id'), status.get('account'), references);
|
|
|
|
});
|
|
|
|
|
|
|
|
return state;
|
|
|
|
};
|
|
|
|
|
2016-10-30 14:06:43 +00:00
|
|
|
const normalizeContext = (state, id, ancestors, descendants) => {
|
|
|
|
const ancestorsIds = ancestors.map(ancestor => ancestor.get('id'));
|
|
|
|
const descendantsIds = descendants.map(descendant => descendant.get('id'));
|
2016-09-15 23:21:51 +01:00
|
|
|
|
|
|
|
return state.withMutations(map => {
|
2016-10-30 14:06:43 +00:00
|
|
|
map.setIn(['ancestors', id], ancestorsIds);
|
|
|
|
map.setIn(['descendants', id], descendantsIds);
|
2016-09-15 23:21:51 +01:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2016-08-31 15:15:12 +01:00
|
|
|
export default function timelines(state = initialState, action) {
|
2016-08-24 16:56:44 +01:00
|
|
|
switch(action.type) {
|
2016-09-20 22:18:00 +01:00
|
|
|
case TIMELINE_REFRESH_SUCCESS:
|
2016-10-19 17:20:19 +01:00
|
|
|
return normalizeTimeline(state, action.timeline, Immutable.fromJS(action.statuses), action.replace);
|
2016-09-22 00:08:35 +01:00
|
|
|
case TIMELINE_EXPAND_SUCCESS:
|
|
|
|
return appendNormalizedTimeline(state, action.timeline, Immutable.fromJS(action.statuses));
|
2016-08-31 15:15:12 +01:00
|
|
|
case TIMELINE_UPDATE:
|
2016-10-30 14:06:43 +00:00
|
|
|
return updateTimeline(state, action.timeline, Immutable.fromJS(action.status), action.references);
|
2016-09-05 00:59:46 +01:00
|
|
|
case TIMELINE_DELETE:
|
2016-10-30 14:06:43 +00:00
|
|
|
return deleteStatus(state, action.id, action.accountId, action.references);
|
|
|
|
case CONTEXT_FETCH_SUCCESS:
|
|
|
|
return normalizeContext(state, action.id, Immutable.fromJS(action.ancestors), Immutable.fromJS(action.descendants));
|
2016-09-18 17:18:46 +01:00
|
|
|
case ACCOUNT_TIMELINE_FETCH_SUCCESS:
|
2016-10-19 17:20:19 +01:00
|
|
|
return normalizeAccountTimeline(state, action.id, Immutable.fromJS(action.statuses), action.replace);
|
2016-09-22 19:58:35 +01:00
|
|
|
case ACCOUNT_TIMELINE_EXPAND_SUCCESS:
|
|
|
|
return appendNormalizedAccountTimeline(state, action.id, Immutable.fromJS(action.statuses));
|
2016-11-23 21:57:57 +00:00
|
|
|
case ACCOUNT_BLOCK_SUCCESS:
|
|
|
|
return filterTimelines(state, action.relationship, action.statuses);
|
2016-08-24 16:56:44 +01:00
|
|
|
default:
|
|
|
|
return state;
|
|
|
|
}
|
2016-09-12 18:20:55 +01:00
|
|
|
};
|