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