gab-social/app/javascript/gabsocial/reducers/statuses.js

85 lines
3.3 KiB
JavaScript
Raw Normal View History

2019-07-02 08:10:25 +01:00
import {
2020-03-14 17:31:29 +00:00
REPOST_REQUEST,
2020-11-25 21:22:37 +00:00
UNREPOST_REQUEST,
2020-03-14 17:31:29 +00:00
REPOST_FAIL,
2020-03-02 22:26:25 +00:00
FAVORITE_REQUEST,
FAVORITE_FAIL,
2020-03-13 18:44:44 +00:00
UNFAVORITE_REQUEST,
2019-07-02 08:10:25 +01:00
} from '../actions/interactions';
import {
STATUS_REVEAL,
STATUS_HIDE,
2020-11-15 18:48:32 +00:00
UPDATE_STATUS_STATS,
2019-07-02 08:10:25 +01:00
} from '../actions/statuses';
import { TIMELINE_DELETE } from '../actions/timelines';
import { STATUS_IMPORT, STATUSES_IMPORT } from '../actions/importer';
import { Map as ImmutableMap, fromJS } from 'immutable';
const importStatus = (state, status) => state.set(status.id, fromJS(status));
const importStatuses = (state, statuses) =>
state.withMutations(mutable => statuses.forEach(status => importStatus(mutable, status)));
const deleteStatus = (state, id, references) => {
references.forEach(ref => {
state = deleteStatus(state, ref[0], []);
});
return state.delete(id);
};
const initialState = ImmutableMap();
export default function statuses(state = initialState, action) {
switch(action.type) {
case STATUS_IMPORT:
return importStatus(state, action.status);
case STATUSES_IMPORT:
return importStatuses(state, action.statuses);
2020-03-02 22:26:25 +00:00
case FAVORITE_REQUEST:
2019-07-02 08:10:25 +01:00
return state.setIn([action.status.get('id'), 'favourited'], true);
2020-03-02 22:26:25 +00:00
case FAVORITE_FAIL:
2019-07-02 08:10:25 +01:00
return state.get(action.status.get('id')) === undefined ? state : state.setIn([action.status.get('id'), 'favourited'], false);
2020-03-13 18:44:44 +00:00
case UNFAVORITE_REQUEST:
return state.setIn([action.status.get('id'), 'favourited'], false);
2020-03-14 17:31:29 +00:00
case REPOST_REQUEST:
2019-07-02 08:10:25 +01:00
return state.setIn([action.status.get('id'), 'reblogged'], true);
2020-11-25 21:22:37 +00:00
case UNREPOST_REQUEST:
return state.setIn([action.status.get('id'), 'reblogged'], false);
2020-03-14 17:31:29 +00:00
case REPOST_FAIL:
2019-07-02 08:10:25 +01:00
return state.get(action.status.get('id')) === undefined ? state : state.setIn([action.status.get('id'), 'reblogged'], false);
case STATUS_REVEAL:
2020-11-25 21:22:37 +00:00
return state.withMutations((map) => {
2019-07-02 08:10:25 +01:00
action.ids.forEach(id => {
if (!(state.get(id) === undefined)) {
map.setIn([id, 'hidden'], false);
}
});
});
case STATUS_HIDE:
2020-11-25 21:22:37 +00:00
return state.withMutations((map) => {
2019-07-02 08:10:25 +01:00
action.ids.forEach(id => {
if (!(state.get(id) === undefined)) {
map.setIn([id, 'hidden'], true);
}
});
});
case TIMELINE_DELETE:
return deleteStatus(state, action.id, action.references);
2020-11-15 18:48:32 +00:00
case UPDATE_STATUS_STATS:
2020-11-25 21:22:37 +00:00
const { status_id } = action.data
return state.withMutations((map) => {
if (action.data.favourited !== undefined) map.setIn([status_id, 'favourited'], action.data.favourited)
if (action.data.favourites_count !== undefined) map.setIn([status_id, 'favourites_count'], action.data.favourites_count)
if (action.data.reblogged !== undefined) map.setIn([status_id, 'reblogged'], action.data.reblogged)
if (action.data.reblogs_count !== undefined) map.setIn([status_id, 'reblogs_count'], action.data.reblogs_count)
if (action.data.replies_count !== undefined) map.setIn([status_id, 'replies_count'], action.data.replies_count)
if (action.data.pinned !== undefined) map.setIn([status_id, 'pinned'], action.data.pinned)
if (action.data.pinned_by_group !== undefined) map.setIn([status_id, 'pinned_by_group'], action.data.pinned_by_group)
if (action.data.bookmarked !== undefined) map.setIn([status_id, 'bookmarked'], action.data.bookmarked)
})
2019-07-02 08:10:25 +01:00
default:
return state;
}
};