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