2016-09-18 18:18:46 +02:00
|
|
|
import {
|
|
|
|
TIMELINE_UPDATE,
|
2016-09-22 01:08:35 +02:00
|
|
|
TIMELINE_DELETE,
|
2018-11-05 18:53:25 +01:00
|
|
|
TIMELINE_CLEAR,
|
2016-12-03 21:04:57 +01:00
|
|
|
TIMELINE_EXPAND_SUCCESS,
|
2017-01-24 04:12:10 +01:00
|
|
|
TIMELINE_EXPAND_REQUEST,
|
2017-01-24 13:04:12 +01:00
|
|
|
TIMELINE_EXPAND_FAIL,
|
2017-04-02 21:44:06 +02:00
|
|
|
TIMELINE_SCROLL_TOP,
|
2019-03-07 22:17:52 +01:00
|
|
|
TIMELINE_CONNECT,
|
2017-05-20 17:31:47 +02:00
|
|
|
TIMELINE_DISCONNECT,
|
2019-07-16 06:30:47 +02:00
|
|
|
TIMELINE_LOAD_PENDING,
|
2016-10-30 15:06:43 +01:00
|
|
|
} from '../actions/timelines';
|
2016-09-18 18:18:46 +02:00
|
|
|
import {
|
2017-03-02 18:49:32 +01:00
|
|
|
ACCOUNT_BLOCK_SUCCESS,
|
2017-05-20 17:31:47 +02:00
|
|
|
ACCOUNT_MUTE_SUCCESS,
|
2017-10-13 16:44:02 +02:00
|
|
|
ACCOUNT_UNFOLLOW_SUCCESS,
|
2016-10-30 15:06:43 +01:00
|
|
|
} from '../actions/accounts';
|
2017-07-11 01:00:14 +02:00
|
|
|
import { Map as ImmutableMap, List as ImmutableList, fromJS } from 'immutable';
|
2018-04-18 16:50:19 +02:00
|
|
|
import compareId from '../compare_id';
|
2016-08-24 17:56:44 +02:00
|
|
|
|
2017-07-11 01:00:14 +02:00
|
|
|
const initialState = ImmutableMap();
|
2016-12-03 21:04:57 +01:00
|
|
|
|
2017-07-11 01:00:14 +02:00
|
|
|
const initialTimeline = ImmutableMap({
|
2017-06-11 17:07:35 +02:00
|
|
|
unread: 0,
|
2019-03-07 22:17:52 +01:00
|
|
|
online: false,
|
2017-06-11 17:07:35 +02:00
|
|
|
top: true,
|
|
|
|
isLoading: false,
|
2018-03-24 15:25:15 +01:00
|
|
|
hasMore: true,
|
2019-07-16 06:30:47 +02:00
|
|
|
pendingItems: ImmutableList(),
|
2017-07-11 01:00:14 +02:00
|
|
|
items: ImmutableList(),
|
2016-09-04 14:04:26 +02:00
|
|
|
});
|
|
|
|
|
2019-07-16 06:30:47 +02:00
|
|
|
const expandNormalizedTimeline = (state, timeline, statuses, next, isPartial, isLoadingRecent, usePendingItems) => {
|
2017-06-11 17:07:35 +02:00
|
|
|
return state.update(timeline, initialTimeline, map => map.withMutations(mMap => {
|
|
|
|
mMap.set('isLoading', false);
|
2019-03-01 11:11:35 +01:00
|
|
|
mMap.set('isPartial', isPartial);
|
|
|
|
|
2018-12-14 01:41:38 +01:00
|
|
|
if (!next && !isLoadingRecent) mMap.set('hasMore', false);
|
2018-03-24 15:25:15 +01:00
|
|
|
|
2019-06-05 13:39:59 +02:00
|
|
|
if (timeline.endsWith(':pinned')) {
|
|
|
|
mMap.set('items', statuses.map(status => status.get('id')));
|
|
|
|
} else if (!statuses.isEmpty()) {
|
2019-09-23 00:48:31 +02:00
|
|
|
usePendingItems = isLoadingRecent && (usePendingItems || !mMap.get('pendingItems').isEmpty());
|
|
|
|
|
2019-07-16 06:30:47 +02:00
|
|
|
mMap.update(usePendingItems ? 'pendingItems' : 'items', ImmutableList(), oldIds => {
|
2018-03-24 15:25:15 +01:00
|
|
|
const newIds = statuses.map(status => status.get('id'));
|
2018-05-19 02:51:29 +02:00
|
|
|
|
2018-04-18 16:50:19 +02:00
|
|
|
const lastIndex = oldIds.findLastIndex(id => id !== null && compareId(id, newIds.last()) >= 0) + 1;
|
2018-04-24 11:47:27 +02:00
|
|
|
const firstIndex = oldIds.take(lastIndex).findLastIndex(id => id !== null && compareId(id, newIds.first()) > 0);
|
2018-03-24 15:25:15 +01:00
|
|
|
|
|
|
|
if (firstIndex < 0) {
|
|
|
|
return (isPartial ? newIds.unshift(null) : newIds).concat(oldIds.skip(lastIndex));
|
|
|
|
}
|
|
|
|
|
|
|
|
return oldIds.take(firstIndex + 1).concat(
|
|
|
|
isPartial && oldIds.get(firstIndex) !== null ? newIds.unshift(null) : newIds,
|
2020-03-08 16:02:36 +01:00
|
|
|
oldIds.skip(lastIndex),
|
2018-03-24 15:25:15 +01:00
|
|
|
);
|
|
|
|
});
|
|
|
|
}
|
2017-06-11 17:07:35 +02:00
|
|
|
}));
|
2017-05-20 01:28:25 +02:00
|
|
|
};
|
|
|
|
|
2019-07-16 06:30:47 +02:00
|
|
|
const updateTimeline = (state, timeline, status, usePendingItems) => {
|
2019-09-16 19:39:44 +02:00
|
|
|
const top = state.getIn([timeline, 'top']);
|
|
|
|
|
2019-09-23 00:48:31 +02:00
|
|
|
if (usePendingItems || !state.getIn([timeline, 'pendingItems']).isEmpty()) {
|
2019-07-16 06:30:47 +02:00
|
|
|
if (state.getIn([timeline, 'pendingItems'], ImmutableList()).includes(status.get('id')) || state.getIn([timeline, 'items'], ImmutableList()).includes(status.get('id'))) {
|
|
|
|
return state;
|
|
|
|
}
|
|
|
|
|
2019-09-16 15:45:06 +02:00
|
|
|
return state.update(timeline, initialTimeline, map => map.update('pendingItems', list => list.unshift(status.get('id'))).update('unread', unread => unread + 1));
|
2019-07-16 06:30:47 +02:00
|
|
|
}
|
|
|
|
|
2017-07-11 01:00:14 +02:00
|
|
|
const ids = state.getIn([timeline, 'items'], ImmutableList());
|
2017-06-11 17:07:35 +02:00
|
|
|
const includesId = ids.includes(status.get('id'));
|
|
|
|
const unread = state.getIn([timeline, 'unread'], 0);
|
2016-12-03 21:04:57 +01:00
|
|
|
|
2017-06-11 17:07:35 +02:00
|
|
|
if (includesId) {
|
|
|
|
return state;
|
2017-02-21 00:10:49 +01:00
|
|
|
}
|
|
|
|
|
2017-06-11 17:07:35 +02:00
|
|
|
let newIds = ids;
|
2016-10-17 01:34:16 +02:00
|
|
|
|
2017-06-11 17:07:35 +02: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);
|
|
|
|
mMap.set('items', newIds.unshift(status.get('id')));
|
|
|
|
}));
|
2016-09-01 13:21:48 +02:00
|
|
|
};
|
|
|
|
|
2020-04-28 09:53:42 +02:00
|
|
|
const deleteStatus = (state, id, references, exclude_account = null) => {
|
2017-06-11 17:07:35 +02:00
|
|
|
state.keySeq().forEach(timeline => {
|
2019-07-16 06:30:47 +02:00
|
|
|
if (exclude_account === null || (timeline !== `account:${exclude_account}` && !timeline.startsWith(`account:${exclude_account}:`))) {
|
|
|
|
const helper = list => list.filterNot(item => item === id);
|
|
|
|
state = state.updateIn([timeline, 'items'], helper).updateIn([timeline, 'pendingItems'], helper);
|
|
|
|
}
|
2016-09-05 01:59:46 +02:00
|
|
|
});
|
|
|
|
|
2016-10-30 15:06:43 +01:00
|
|
|
// Remove reblogs of deleted status
|
|
|
|
references.forEach(ref => {
|
2020-04-28 09:53:42 +02:00
|
|
|
state = deleteStatus(state, ref, [], exclude_account);
|
2016-10-28 20:05:44 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
return state;
|
|
|
|
};
|
|
|
|
|
2018-11-05 18:53:25 +01:00
|
|
|
const clearTimeline = (state, timeline) => {
|
2018-12-30 18:57:39 +01:00
|
|
|
return state.set(timeline, initialTimeline);
|
2018-11-05 18:53:25 +01:00
|
|
|
};
|
|
|
|
|
2016-11-23 22:57:57 +01:00
|
|
|
const filterTimelines = (state, relationship, statuses) => {
|
|
|
|
let references;
|
|
|
|
|
|
|
|
statuses.forEach(status => {
|
|
|
|
if (status.get('account') !== relationship.id) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-04-28 09:53:42 +02:00
|
|
|
references = statuses.filter(item => item.get('reblog') === status.get('id')).map(item => item.get('id'));
|
|
|
|
state = deleteStatus(state, status.get('id'), references, relationship.id);
|
2016-09-16 00:21:51 +02:00
|
|
|
});
|
2016-12-03 21:04:57 +01:00
|
|
|
|
|
|
|
return state;
|
|
|
|
};
|
|
|
|
|
2019-07-16 06:30:47 +02:00
|
|
|
const filterTimeline = (timeline, state, relationship, statuses) => {
|
|
|
|
const helper = list => list.filterNot(statusId => statuses.getIn([statusId, 'account']) === relationship.id);
|
|
|
|
return state.updateIn([timeline, 'items'], ImmutableList(), helper).updateIn([timeline, 'pendingItems'], ImmutableList(), helper);
|
|
|
|
};
|
2017-10-13 16:44:02 +02:00
|
|
|
|
2017-02-21 00:10:49 +01:00
|
|
|
const updateTop = (state, timeline, top) => {
|
2017-06-11 17:07:35 +02:00
|
|
|
return state.update(timeline, initialTimeline, map => map.withMutations(mMap => {
|
2019-09-16 15:45:06 +02:00
|
|
|
if (top) mMap.set('unread', mMap.get('pendingItems').size);
|
2017-06-11 17:07:35 +02:00
|
|
|
mMap.set('top', top);
|
|
|
|
}));
|
2017-02-21 00:10:49 +01:00
|
|
|
};
|
|
|
|
|
2016-08-31 16:15:12 +02:00
|
|
|
export default function timelines(state = initialState, action) {
|
2016-08-24 17:56:44 +02:00
|
|
|
switch(action.type) {
|
2019-07-16 06:30:47 +02:00
|
|
|
case TIMELINE_LOAD_PENDING:
|
|
|
|
return state.update(action.timeline, initialTimeline, map =>
|
|
|
|
map.update('items', list => map.get('pendingItems').concat(list.take(40))).set('pendingItems', ImmutableList()).set('unread', 0));
|
2017-01-24 13:04:12 +01:00
|
|
|
case TIMELINE_EXPAND_REQUEST:
|
2017-06-11 17:07:35 +02:00
|
|
|
return state.update(action.timeline, initialTimeline, map => map.set('isLoading', true));
|
2017-01-24 13:04:12 +01:00
|
|
|
case TIMELINE_EXPAND_FAIL:
|
2017-06-11 17:07:35 +02:00
|
|
|
return state.update(action.timeline, initialTimeline, map => map.set('isLoading', false));
|
2017-01-24 13:04:12 +01:00
|
|
|
case TIMELINE_EXPAND_SUCCESS:
|
2019-07-16 06:30:47 +02:00
|
|
|
return expandNormalizedTimeline(state, action.timeline, fromJS(action.statuses), action.next, action.partial, action.isLoadingRecent, action.usePendingItems);
|
2017-01-24 13:04:12 +01:00
|
|
|
case TIMELINE_UPDATE:
|
2019-07-16 06:30:47 +02:00
|
|
|
return updateTimeline(state, action.timeline, fromJS(action.status), action.usePendingItems);
|
2017-01-24 13:04:12 +01:00
|
|
|
case TIMELINE_DELETE:
|
2020-04-28 09:53:42 +02:00
|
|
|
return deleteStatus(state, action.id, action.references, action.reblogOf);
|
2018-11-05 18:53:25 +01:00
|
|
|
case TIMELINE_CLEAR:
|
|
|
|
return clearTimeline(state, action.timeline);
|
2017-01-24 13:04:12 +01:00
|
|
|
case ACCOUNT_BLOCK_SUCCESS:
|
2017-03-02 18:49:32 +01:00
|
|
|
case ACCOUNT_MUTE_SUCCESS:
|
2017-01-24 13:04:12 +01:00
|
|
|
return filterTimelines(state, action.relationship, action.statuses);
|
2017-10-13 16:44:02 +02:00
|
|
|
case ACCOUNT_UNFOLLOW_SUCCESS:
|
|
|
|
return filterTimeline('home', state, action.relationship, action.statuses);
|
2017-01-24 13:04:12 +01:00
|
|
|
case TIMELINE_SCROLL_TOP:
|
2017-02-21 00:10:49 +01:00
|
|
|
return updateTop(state, action.timeline, action.top);
|
2019-03-07 22:17:52 +01:00
|
|
|
case TIMELINE_CONNECT:
|
|
|
|
return state.update(action.timeline, initialTimeline, map => map.set('online', true));
|
2017-04-02 21:44:06 +02:00
|
|
|
case TIMELINE_DISCONNECT:
|
2018-03-24 15:25:15 +01:00
|
|
|
return state.update(
|
|
|
|
action.timeline,
|
|
|
|
initialTimeline,
|
2020-03-08 16:02:36 +01:00
|
|
|
map => map.set('online', false).update(action.usePendingItems ? 'pendingItems' : 'items', items => items.first() ? items.unshift(null) : items),
|
2018-03-24 15:25:15 +01:00
|
|
|
);
|
2017-01-24 13:04:12 +01:00
|
|
|
default:
|
|
|
|
return state;
|
2016-08-24 17:56:44 +02:00
|
|
|
}
|
2016-09-12 19:20:55 +02:00
|
|
|
};
|