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