2016-09-18 11:18:46 -05:00
|
|
|
import {
|
|
|
|
TIMELINE_UPDATE,
|
2016-09-21 18:08:35 -05:00
|
|
|
TIMELINE_DELETE,
|
2018-12-18 10:56:08 -06:00
|
|
|
TIMELINE_CLEAR,
|
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,
|
2019-03-07 15:17:52 -06:00
|
|
|
TIMELINE_CONNECT,
|
2017-05-20 10:31:47 -05:00
|
|
|
TIMELINE_DISCONNECT,
|
2019-07-15 23:30:47 -05:00
|
|
|
TIMELINE_LOAD_PENDING,
|
2021-05-07 07:33:57 -05:00
|
|
|
TIMELINE_MARK_AS_PARTIAL,
|
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';
|
2022-04-06 14:01:41 -05:00
|
|
|
import { Map as ImmutableMap, List as ImmutableList, OrderedSet as ImmutableOrderedSet, fromJS } from 'immutable';
|
2023-05-08 20:11:56 -05:00
|
|
|
import { compareId } from '../compare_id';
|
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,
|
2019-03-07 15:17:52 -06:00
|
|
|
online: false,
|
2017-06-11 10:07:35 -05:00
|
|
|
top: true,
|
|
|
|
isLoading: false,
|
2018-05-27 12:10:37 -05:00
|
|
|
hasMore: true,
|
2019-07-15 23:30:47 -05:00
|
|
|
pendingItems: ImmutableList(),
|
2017-07-10 18:00:14 -05:00
|
|
|
items: ImmutableList(),
|
2016-09-04 07:04:26 -05:00
|
|
|
});
|
|
|
|
|
2019-07-15 23:30:47 -05:00
|
|
|
const expandNormalizedTimeline = (state, timeline, statuses, next, isPartial, isLoadingRecent, usePendingItems) => {
|
2022-04-06 14:01:41 -05:00
|
|
|
// This method is pretty tricky because:
|
|
|
|
// - existing items in the timeline might be out of order
|
|
|
|
// - the existing timeline may have gaps, most often explicitly noted with a `null` item
|
|
|
|
// - ideally, we don't want it to reorder existing items of the timeline
|
|
|
|
// - `statuses` may include items that are already included in the timeline
|
|
|
|
// - this function can be called either to fill in a gap, or load newer items
|
|
|
|
|
2017-06-11 10:07:35 -05:00
|
|
|
return state.update(timeline, initialTimeline, map => map.withMutations(mMap => {
|
|
|
|
mMap.set('isLoading', false);
|
2019-03-01 04:11:35 -06:00
|
|
|
mMap.set('isPartial', isPartial);
|
|
|
|
|
2018-12-13 14:08:22 -06:00
|
|
|
if (!next && !isLoadingRecent) mMap.set('hasMore', false);
|
2018-05-27 12:10:37 -05:00
|
|
|
|
2019-06-07 10:05:32 -05:00
|
|
|
if (timeline.endsWith(':pinned')) {
|
|
|
|
mMap.set('items', statuses.map(status => status.get('id')));
|
|
|
|
} else if (!statuses.isEmpty()) {
|
2019-09-22 17:48:31 -05:00
|
|
|
usePendingItems = isLoadingRecent && (usePendingItems || !mMap.get('pendingItems').isEmpty());
|
|
|
|
|
2019-07-15 23:30:47 -05:00
|
|
|
mMap.update(usePendingItems ? 'pendingItems' : 'items', ImmutableList(), oldIds => {
|
2018-05-27 12:10:37 -05:00
|
|
|
const newIds = statuses.map(status => status.get('id'));
|
2022-04-06 14:01:41 -05:00
|
|
|
|
|
|
|
// Now this gets tricky, as we don't necessarily know for sure where the gap to fill is
|
|
|
|
// and some items in the timeline may not be properly ordered.
|
|
|
|
|
|
|
|
// However, we know that `newIds.last()` is the oldest item that was requested and that
|
|
|
|
// there is no “hole” between `newIds.last()` and `newIds.first()`.
|
|
|
|
|
|
|
|
// First, find the furthest (if properly sorted, oldest) item in the timeline that is
|
|
|
|
// newer than the oldest fetched one, as it's most likely that it delimits the gap.
|
|
|
|
// Start the gap *after* that item.
|
2018-05-27 12:33:28 -05:00
|
|
|
const lastIndex = oldIds.findLastIndex(id => id !== null && compareId(id, newIds.last()) >= 0) + 1;
|
2018-05-27 12:10:37 -05:00
|
|
|
|
2022-04-06 14:01:41 -05:00
|
|
|
// Then, try to find the furthest (if properly sorted, oldest) item in the timeline that
|
|
|
|
// is newer than the most recent fetched one, as it delimits a section comprised of only
|
2022-04-07 04:27:35 -05:00
|
|
|
// items older or within `newIds` (or that were deleted from the server, so should be removed
|
2022-04-06 14:01:41 -05:00
|
|
|
// anyway).
|
|
|
|
// Stop the gap *after* that item.
|
|
|
|
const firstIndex = oldIds.take(lastIndex).findLastIndex(id => id !== null && compareId(id, newIds.first()) > 0) + 1;
|
|
|
|
|
2022-04-07 04:27:35 -05:00
|
|
|
let insertedIds = ImmutableOrderedSet(newIds).withMutations(insertedIds => {
|
|
|
|
// It is possible, though unlikely, that the slice we are replacing contains items older
|
|
|
|
// than the elements we got from the API. Get them and add them back at the back of the
|
|
|
|
// slice.
|
|
|
|
const olderIds = oldIds.slice(firstIndex, lastIndex).filter(id => id !== null && compareId(id, newIds.last()) < 0);
|
|
|
|
insertedIds.union(olderIds);
|
|
|
|
|
|
|
|
// Make sure we aren't inserting duplicates
|
|
|
|
insertedIds.subtract(oldIds.take(firstIndex), oldIds.skip(lastIndex));
|
|
|
|
}).toList();
|
|
|
|
|
2022-04-06 14:01:41 -05:00
|
|
|
// Finally, insert a gap marker if the data is marked as partial by the server
|
|
|
|
if (isPartial && (firstIndex === 0 || oldIds.get(firstIndex - 1) !== null)) {
|
|
|
|
insertedIds = insertedIds.unshift(null);
|
2018-05-27 12:10:37 -05:00
|
|
|
}
|
|
|
|
|
2022-04-06 14:01:41 -05:00
|
|
|
return oldIds.take(firstIndex).concat(
|
|
|
|
insertedIds,
|
2020-03-08 10:02:36 -05:00
|
|
|
oldIds.skip(lastIndex),
|
2018-05-27 12:10:37 -05:00
|
|
|
);
|
|
|
|
});
|
|
|
|
}
|
2017-06-11 10:07:35 -05:00
|
|
|
}));
|
2017-05-19 18:28:25 -05:00
|
|
|
};
|
|
|
|
|
2019-10-24 10:47:19 -05:00
|
|
|
const updateTimeline = (state, timeline, status, usePendingItems, filtered) => {
|
2019-09-16 12:39:44 -05:00
|
|
|
const top = state.getIn([timeline, 'top']);
|
|
|
|
|
2019-09-22 17:48:31 -05:00
|
|
|
if (usePendingItems || !state.getIn([timeline, 'pendingItems']).isEmpty()) {
|
2019-07-15 23:30:47 -05:00
|
|
|
if (state.getIn([timeline, 'pendingItems'], ImmutableList()).includes(status.get('id')) || state.getIn([timeline, 'items'], ImmutableList()).includes(status.get('id'))) {
|
|
|
|
return state;
|
|
|
|
}
|
|
|
|
|
2019-10-24 10:47:19 -05:00
|
|
|
state = state.update(timeline, initialTimeline, map => map.update('pendingItems', list => list.unshift(status.get('id'))));
|
|
|
|
|
|
|
|
if (!filtered) {
|
2019-12-17 08:38:19 -06:00
|
|
|
state = state.updateIn([timeline, 'unread'], unread => unread + 1);
|
2019-10-24 10:47:19 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return state;
|
2019-07-15 23:30:47 -05:00
|
|
|
}
|
|
|
|
|
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 => {
|
2019-10-24 10:47:19 -05:00
|
|
|
if (!top && !filtered) mMap.set('unread', unread + 1);
|
2017-06-11 10:07:35 -05:00
|
|
|
if (top && ids.size > 40) newIds = newIds.take(20);
|
|
|
|
mMap.set('items', newIds.unshift(status.get('id')));
|
|
|
|
}));
|
2016-09-01 06:21:48 -05:00
|
|
|
};
|
|
|
|
|
2020-04-28 02:53:42 -05:00
|
|
|
const deleteStatus = (state, id, references, exclude_account = null) => {
|
2017-06-11 10:07:35 -05:00
|
|
|
state.keySeq().forEach(timeline => {
|
2019-07-15 23:30:47 -05: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-04 18:59:46 -05:00
|
|
|
});
|
|
|
|
|
2016-10-30 09:06:43 -05:00
|
|
|
// Remove reblogs of deleted status
|
|
|
|
references.forEach(ref => {
|
2020-04-28 02:53:42 -05:00
|
|
|
state = deleteStatus(state, ref, [], exclude_account);
|
2016-10-28 13:05:44 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
return state;
|
|
|
|
};
|
|
|
|
|
2018-12-18 10:56:08 -06:00
|
|
|
const clearTimeline = (state, timeline) => {
|
2018-12-30 05:14:41 -06:00
|
|
|
return state.set(timeline, initialTimeline);
|
2018-12-18 10:56:08 -06:00
|
|
|
};
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2020-04-28 02:53:42 -05: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-15 17:21:51 -05:00
|
|
|
});
|
2016-12-03 14:04:57 -06:00
|
|
|
|
|
|
|
return state;
|
|
|
|
};
|
|
|
|
|
2019-07-15 23:30:47 -05: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 09:44:02 -05:00
|
|
|
|
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 => {
|
2019-09-16 08:45:06 -05:00
|
|
|
if (top) mMap.set('unread', mMap.get('pendingItems').size);
|
2017-06-11 10:07:35 -05:00
|
|
|
mMap.set('top', top);
|
|
|
|
}));
|
2017-02-20 17:10:49 -06:00
|
|
|
};
|
|
|
|
|
2022-04-07 09:08:17 -05:00
|
|
|
const reconnectTimeline = (state, usePendingItems) => {
|
|
|
|
if (state.get('online')) {
|
|
|
|
return state;
|
|
|
|
}
|
|
|
|
|
|
|
|
return state.withMutations(mMap => {
|
|
|
|
mMap.update(usePendingItems ? 'pendingItems' : 'items', items => items.first() ? items.unshift(null) : items);
|
|
|
|
mMap.set('online', true);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
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) {
|
2019-07-15 23:30:47 -05: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 06:04:12 -06:00
|
|
|
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_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_EXPAND_SUCCESS:
|
2019-07-15 23:30:47 -05:00
|
|
|
return expandNormalizedTimeline(state, action.timeline, fromJS(action.statuses), action.next, action.partial, action.isLoadingRecent, action.usePendingItems);
|
2017-01-24 06:04:12 -06:00
|
|
|
case TIMELINE_UPDATE:
|
2019-10-24 10:47:19 -05:00
|
|
|
return updateTimeline(state, action.timeline, fromJS(action.status), action.usePendingItems, action.filtered);
|
2017-01-24 06:04:12 -06:00
|
|
|
case TIMELINE_DELETE:
|
2020-04-28 02:53:42 -05:00
|
|
|
return deleteStatus(state, action.id, action.references, action.reblogOf);
|
2018-12-18 10:56:08 -06:00
|
|
|
case TIMELINE_CLEAR:
|
|
|
|
return clearTimeline(state, action.timeline);
|
2017-01-24 06:04:12 -06:00
|
|
|
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);
|
2019-03-07 15:17:52 -06:00
|
|
|
case TIMELINE_CONNECT:
|
2022-04-07 09:08:17 -05:00
|
|
|
return state.update(action.timeline, initialTimeline, map => reconnectTimeline(map, action.usePendingItems));
|
2017-04-02 14:44:06 -05:00
|
|
|
case TIMELINE_DISCONNECT:
|
2018-05-27 12:10:37 -05:00
|
|
|
return state.update(
|
|
|
|
action.timeline,
|
|
|
|
initialTimeline,
|
2020-03-08 10:02:36 -05:00
|
|
|
map => map.set('online', false).update(action.usePendingItems ? 'pendingItems' : 'items', items => items.first() ? items.unshift(null) : items),
|
2018-05-27 12:10:37 -05:00
|
|
|
);
|
2021-05-07 07:33:57 -05:00
|
|
|
case TIMELINE_MARK_AS_PARTIAL:
|
|
|
|
return state.update(
|
|
|
|
action.timeline,
|
|
|
|
initialTimeline,
|
|
|
|
map => map.set('isPartial', true).set('items', ImmutableList()).set('pendingItems', ImmutableList()).set('unread', 0),
|
|
|
|
);
|
2017-01-24 06:04:12 -06:00
|
|
|
default:
|
|
|
|
return state;
|
2016-08-24 10:56:44 -05:00
|
|
|
}
|
2023-02-03 13:52:07 -06:00
|
|
|
}
|