2016-09-18 11:18:46 -05:00
|
|
|
import {
|
2016-12-03 14:04:57 -06:00
|
|
|
TIMELINE_REFRESH_REQUEST,
|
2016-09-20 16:18:00 -05:00
|
|
|
TIMELINE_REFRESH_SUCCESS,
|
2017-01-24 06:04:12 -06:00
|
|
|
TIMELINE_REFRESH_FAIL,
|
2016-09-18 11:18:46 -05:00
|
|
|
TIMELINE_UPDATE,
|
2016-09-21 18:08:35 -05:00
|
|
|
TIMELINE_DELETE,
|
2016-12-03 14:04:57 -06:00
|
|
|
TIMELINE_EXPAND_SUCCESS,
|
2017-01-23 21:12:10 -06:00
|
|
|
TIMELINE_EXPAND_REQUEST,
|
2017-01-24 06:04:12 -06:00
|
|
|
TIMELINE_EXPAND_FAIL,
|
2017-04-02 14:44:06 -05:00
|
|
|
TIMELINE_SCROLL_TOP,
|
|
|
|
TIMELINE_CONNECT,
|
2017-05-20 10:31:47 -05:00
|
|
|
TIMELINE_DISCONNECT,
|
2017-12-04 01:26:40 -06:00
|
|
|
} from 'flavours/glitch/actions/timelines';
|
2016-09-18 11:18:46 -05:00
|
|
|
import {
|
2017-03-02 11:49:32 -06:00
|
|
|
ACCOUNT_BLOCK_SUCCESS,
|
2017-05-20 10:31:47 -05:00
|
|
|
ACCOUNT_MUTE_SUCCESS,
|
2017-10-13 09:44:02 -05:00
|
|
|
ACCOUNT_UNFOLLOW_SUCCESS,
|
2017-12-04 01:26:40 -06:00
|
|
|
} from 'flavours/glitch/actions/accounts';
|
2017-07-10 18:00:14 -05:00
|
|
|
import { Map as ImmutableMap, List as ImmutableList, fromJS } from 'immutable';
|
2016-08-24 10:56:44 -05:00
|
|
|
|
2017-07-10 18:00:14 -05:00
|
|
|
const initialState = ImmutableMap();
|
2016-12-03 14:04:57 -06:00
|
|
|
|
2017-07-10 18:00:14 -05:00
|
|
|
const initialTimeline = ImmutableMap({
|
2017-06-11 10:07:35 -05:00
|
|
|
unread: 0,
|
|
|
|
online: false,
|
|
|
|
top: true,
|
|
|
|
loaded: false,
|
|
|
|
isLoading: false,
|
|
|
|
next: false,
|
2017-07-10 18:00:14 -05:00
|
|
|
items: ImmutableList(),
|
2016-09-04 07:04:26 -05:00
|
|
|
});
|
|
|
|
|
2018-01-18 10:25:37 -06:00
|
|
|
const normalizeTimeline = (state, timeline, statuses, next, isPartial) => {
|
2017-10-16 08:59:30 -05:00
|
|
|
const oldIds = state.getIn([timeline, 'items'], ImmutableList());
|
|
|
|
const ids = ImmutableList(statuses.map(status => status.get('id'))).filter(newId => !oldIds.includes(newId));
|
2017-06-11 10:07:35 -05:00
|
|
|
const wasLoaded = state.getIn([timeline, 'loaded']);
|
|
|
|
const hadNext = state.getIn([timeline, 'next']);
|
|
|
|
|
|
|
|
return state.update(timeline, initialTimeline, map => map.withMutations(mMap => {
|
|
|
|
mMap.set('loaded', true);
|
|
|
|
mMap.set('isLoading', false);
|
|
|
|
if (!hadNext) mMap.set('next', next);
|
2018-01-19 14:59:12 -06:00
|
|
|
mMap.set('items', wasLoaded ? ids.concat(oldIds) : oldIds.concat(ids));
|
2018-01-18 10:25:37 -06:00
|
|
|
mMap.set('isPartial', isPartial);
|
2017-06-11 10:07:35 -05:00
|
|
|
}));
|
2016-09-04 07:04:26 -05:00
|
|
|
};
|
|
|
|
|
2017-02-19 13:25:54 -06:00
|
|
|
const appendNormalizedTimeline = (state, timeline, statuses, next) => {
|
2017-07-10 18:00:14 -05:00
|
|
|
const oldIds = state.getIn([timeline, 'items'], ImmutableList());
|
2017-10-16 08:59:30 -05:00
|
|
|
const ids = ImmutableList(statuses.map(status => status.get('id'))).filter(newId => !oldIds.includes(newId));
|
2017-06-11 10:07:35 -05:00
|
|
|
|
|
|
|
return state.update(timeline, initialTimeline, map => map.withMutations(mMap => {
|
|
|
|
mMap.set('isLoading', false);
|
|
|
|
mMap.set('next', next);
|
|
|
|
mMap.set('items', oldIds.concat(ids));
|
|
|
|
}));
|
2017-05-19 18:28:25 -05:00
|
|
|
};
|
|
|
|
|
2016-10-30 09:06:43 -05:00
|
|
|
const updateTimeline = (state, timeline, status, references) => {
|
2017-06-11 10:07:35 -05:00
|
|
|
const top = state.getIn([timeline, 'top']);
|
2017-07-10 18:00:14 -05:00
|
|
|
const ids = state.getIn([timeline, 'items'], ImmutableList());
|
2017-06-11 10:07:35 -05:00
|
|
|
const includesId = ids.includes(status.get('id'));
|
|
|
|
const unread = state.getIn([timeline, 'unread'], 0);
|
2016-12-03 14:04:57 -06:00
|
|
|
|
2017-06-11 10:07:35 -05:00
|
|
|
if (includesId) {
|
|
|
|
return state;
|
2017-02-20 17:10:49 -06:00
|
|
|
}
|
|
|
|
|
2017-06-11 10:07:35 -05:00
|
|
|
let newIds = ids;
|
2016-10-16 18:34:16 -05:00
|
|
|
|
2017-06-11 10:07:35 -05:00
|
|
|
return state.update(timeline, initialTimeline, map => map.withMutations(mMap => {
|
|
|
|
if (!top) mMap.set('unread', unread + 1);
|
|
|
|
if (top && ids.size > 40) newIds = newIds.take(20);
|
|
|
|
if (status.getIn(['reblog', 'id'], null) !== null) newIds = newIds.filterNot(item => references.includes(item));
|
|
|
|
mMap.set('items', newIds.unshift(status.get('id')));
|
|
|
|
}));
|
2016-09-01 06:21:48 -05:00
|
|
|
};
|
|
|
|
|
2017-10-17 15:17:02 -05:00
|
|
|
const deleteStatus = (state, id, accountId, references) => {
|
2017-06-11 10:07:35 -05:00
|
|
|
state.keySeq().forEach(timeline => {
|
2017-10-17 15:17:02 -05:00
|
|
|
state = state.updateIn([timeline, 'items'], list => list.filterNot(item => item === id));
|
2016-09-04 18:59:46 -05:00
|
|
|
});
|
|
|
|
|
2016-10-30 09:06:43 -05:00
|
|
|
// Remove reblogs of deleted status
|
|
|
|
references.forEach(ref => {
|
|
|
|
state = deleteStatus(state, ref[0], ref[1], []);
|
2016-10-28 13:05:44 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
return state;
|
|
|
|
};
|
|
|
|
|
2016-11-23 15:57:57 -06: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')]);
|
2017-06-11 10:07:35 -05:00
|
|
|
state = deleteStatus(state, status.get('id'), status.get('account'), references);
|
2016-09-15 17:21:51 -05:00
|
|
|
});
|
2016-12-03 14:04:57 -06:00
|
|
|
|
|
|
|
return state;
|
|
|
|
};
|
|
|
|
|
2017-10-13 09:44:02 -05:00
|
|
|
const filterTimeline = (timeline, state, relationship, statuses) =>
|
|
|
|
state.updateIn([timeline, 'items'], ImmutableList(), list =>
|
|
|
|
list.filterNot(statusId =>
|
|
|
|
statuses.getIn([statusId, 'account']) === relationship.id
|
|
|
|
));
|
|
|
|
|
2017-02-20 17:10:49 -06:00
|
|
|
const updateTop = (state, timeline, top) => {
|
2017-06-11 10:07:35 -05:00
|
|
|
return state.update(timeline, initialTimeline, map => map.withMutations(mMap => {
|
|
|
|
if (top) mMap.set('unread', 0);
|
|
|
|
mMap.set('top', top);
|
|
|
|
}));
|
2017-02-20 17:10:49 -06:00
|
|
|
};
|
|
|
|
|
2016-08-31 09:15:12 -05:00
|
|
|
export default function timelines(state = initialState, action) {
|
2016-08-24 10:56:44 -05:00
|
|
|
switch(action.type) {
|
2017-01-24 06:04:12 -06:00
|
|
|
case TIMELINE_REFRESH_REQUEST:
|
|
|
|
case TIMELINE_EXPAND_REQUEST:
|
2017-06-11 10:07:35 -05:00
|
|
|
return state.update(action.timeline, initialTimeline, map => map.set('isLoading', true));
|
2017-01-24 06:04:12 -06:00
|
|
|
case TIMELINE_REFRESH_FAIL:
|
|
|
|
case TIMELINE_EXPAND_FAIL:
|
2017-06-11 10:07:35 -05:00
|
|
|
return state.update(action.timeline, initialTimeline, map => map.set('isLoading', false));
|
2017-01-24 06:04:12 -06:00
|
|
|
case TIMELINE_REFRESH_SUCCESS:
|
2018-01-18 10:25:37 -06:00
|
|
|
return normalizeTimeline(state, action.timeline, fromJS(action.statuses), action.next, action.partial);
|
2017-01-24 06:04:12 -06:00
|
|
|
case TIMELINE_EXPAND_SUCCESS:
|
2017-07-10 18:00:14 -05:00
|
|
|
return appendNormalizedTimeline(state, action.timeline, fromJS(action.statuses), action.next);
|
2017-01-24 06:04:12 -06:00
|
|
|
case TIMELINE_UPDATE:
|
2017-07-10 18:00:14 -05:00
|
|
|
return updateTimeline(state, action.timeline, fromJS(action.status), action.references);
|
2017-01-24 06:04:12 -06:00
|
|
|
case TIMELINE_DELETE:
|
|
|
|
return deleteStatus(state, action.id, action.accountId, action.references, action.reblogOf);
|
|
|
|
case ACCOUNT_BLOCK_SUCCESS:
|
2017-03-02 11:49:32 -06:00
|
|
|
case ACCOUNT_MUTE_SUCCESS:
|
2017-01-24 06:04:12 -06:00
|
|
|
return filterTimelines(state, action.relationship, action.statuses);
|
2017-10-13 09:44:02 -05:00
|
|
|
case ACCOUNT_UNFOLLOW_SUCCESS:
|
|
|
|
return filterTimeline('home', state, action.relationship, action.statuses);
|
2017-01-24 06:04:12 -06:00
|
|
|
case TIMELINE_SCROLL_TOP:
|
2017-02-20 17:10:49 -06:00
|
|
|
return updateTop(state, action.timeline, action.top);
|
2017-04-02 14:44:06 -05:00
|
|
|
case TIMELINE_CONNECT:
|
2017-06-11 10:07:35 -05:00
|
|
|
return state.update(action.timeline, initialTimeline, map => map.set('online', true));
|
2017-04-02 14:44:06 -05:00
|
|
|
case TIMELINE_DISCONNECT:
|
2017-06-11 10:07:35 -05:00
|
|
|
return state.update(action.timeline, initialTimeline, map => map.set('online', false));
|
2017-01-24 06:04:12 -06:00
|
|
|
default:
|
|
|
|
return state;
|
2016-08-24 10:56:44 -05:00
|
|
|
}
|
2016-09-12 12:20:55 -05:00
|
|
|
};
|