2022-06-05 07:17:26 -07:00
|
|
|
import {
|
|
|
|
Map as ImmutableMap,
|
|
|
|
OrderedSet as ImmutableOrderedSet,
|
|
|
|
Record as ImmutableRecord,
|
|
|
|
} from 'immutable';
|
2022-01-10 14:25:06 -08:00
|
|
|
|
2022-11-29 15:32:21 -08:00
|
|
|
import {
|
|
|
|
STATUS_QUOTES_EXPAND_FAIL,
|
|
|
|
STATUS_QUOTES_EXPAND_REQUEST,
|
|
|
|
STATUS_QUOTES_EXPAND_SUCCESS,
|
|
|
|
STATUS_QUOTES_FETCH_FAIL,
|
|
|
|
STATUS_QUOTES_FETCH_REQUEST,
|
|
|
|
STATUS_QUOTES_FETCH_SUCCESS,
|
|
|
|
} from 'soapbox/actions/status-quotes';
|
2023-01-01 14:46:47 -08:00
|
|
|
import { STATUS_CREATE_SUCCESS } from 'soapbox/actions/statuses';
|
2022-11-29 15:32:21 -08:00
|
|
|
|
2022-01-10 14:17:52 -08:00
|
|
|
import {
|
|
|
|
BOOKMARKED_STATUSES_FETCH_REQUEST,
|
|
|
|
BOOKMARKED_STATUSES_FETCH_SUCCESS,
|
|
|
|
BOOKMARKED_STATUSES_FETCH_FAIL,
|
|
|
|
BOOKMARKED_STATUSES_EXPAND_REQUEST,
|
|
|
|
BOOKMARKED_STATUSES_EXPAND_SUCCESS,
|
|
|
|
BOOKMARKED_STATUSES_EXPAND_FAIL,
|
|
|
|
} from '../actions/bookmarks';
|
2022-11-26 13:15:58 -08:00
|
|
|
import {
|
|
|
|
RECENT_EVENTS_FETCH_REQUEST,
|
|
|
|
RECENT_EVENTS_FETCH_SUCCESS,
|
|
|
|
RECENT_EVENTS_FETCH_FAIL,
|
|
|
|
JOINED_EVENTS_FETCH_REQUEST,
|
|
|
|
JOINED_EVENTS_FETCH_SUCCESS,
|
|
|
|
JOINED_EVENTS_FETCH_FAIL,
|
|
|
|
} from '../actions/events';
|
2020-03-27 13:59:38 -07:00
|
|
|
import {
|
|
|
|
FAVOURITED_STATUSES_FETCH_REQUEST,
|
|
|
|
FAVOURITED_STATUSES_FETCH_SUCCESS,
|
|
|
|
FAVOURITED_STATUSES_FETCH_FAIL,
|
|
|
|
FAVOURITED_STATUSES_EXPAND_REQUEST,
|
|
|
|
FAVOURITED_STATUSES_EXPAND_SUCCESS,
|
|
|
|
FAVOURITED_STATUSES_EXPAND_FAIL,
|
2021-09-10 03:41:42 -07:00
|
|
|
ACCOUNT_FAVOURITED_STATUSES_FETCH_REQUEST,
|
|
|
|
ACCOUNT_FAVOURITED_STATUSES_FETCH_SUCCESS,
|
|
|
|
ACCOUNT_FAVOURITED_STATUSES_FETCH_FAIL,
|
|
|
|
ACCOUNT_FAVOURITED_STATUSES_EXPAND_REQUEST,
|
|
|
|
ACCOUNT_FAVOURITED_STATUSES_EXPAND_SUCCESS,
|
|
|
|
ACCOUNT_FAVOURITED_STATUSES_EXPAND_FAIL,
|
2020-03-27 13:59:38 -07:00
|
|
|
} from '../actions/favourites';
|
|
|
|
import {
|
|
|
|
FAVOURITE_SUCCESS,
|
|
|
|
UNFAVOURITE_SUCCESS,
|
2020-07-29 14:08:36 -07:00
|
|
|
BOOKMARK_SUCCESS,
|
|
|
|
UNBOOKMARK_SUCCESS,
|
2020-03-27 13:59:38 -07:00
|
|
|
PIN_SUCCESS,
|
|
|
|
UNPIN_SUCCESS,
|
|
|
|
} from '../actions/interactions';
|
2022-01-10 14:17:52 -08:00
|
|
|
import {
|
|
|
|
PINNED_STATUSES_FETCH_SUCCESS,
|
2022-11-16 05:32:32 -08:00
|
|
|
} from '../actions/pin-statuses';
|
2021-06-27 11:59:10 -07:00
|
|
|
import {
|
|
|
|
SCHEDULED_STATUSES_FETCH_REQUEST,
|
|
|
|
SCHEDULED_STATUSES_FETCH_SUCCESS,
|
|
|
|
SCHEDULED_STATUSES_FETCH_FAIL,
|
|
|
|
SCHEDULED_STATUSES_EXPAND_REQUEST,
|
|
|
|
SCHEDULED_STATUSES_EXPAND_SUCCESS,
|
|
|
|
SCHEDULED_STATUSES_EXPAND_FAIL,
|
2021-06-27 15:04:23 -07:00
|
|
|
SCHEDULED_STATUS_CANCEL_REQUEST,
|
|
|
|
SCHEDULED_STATUS_CANCEL_SUCCESS,
|
2022-11-16 05:32:32 -08:00
|
|
|
} from '../actions/scheduled-statuses';
|
2020-03-27 13:59:38 -07:00
|
|
|
|
2022-06-05 07:17:26 -07:00
|
|
|
import type { AnyAction } from 'redux';
|
2023-01-01 14:46:47 -08:00
|
|
|
import type { APIEntity } from 'soapbox/types/entities';
|
2022-06-05 07:17:26 -07:00
|
|
|
|
2022-11-29 15:32:21 -08:00
|
|
|
export const StatusListRecord = ImmutableRecord({
|
2022-06-05 07:17:26 -07:00
|
|
|
next: null as string | null,
|
2021-07-08 14:12:04 -07:00
|
|
|
loaded: false,
|
2022-06-05 07:17:26 -07:00
|
|
|
isLoading: null as boolean | null,
|
|
|
|
items: ImmutableOrderedSet<string>(),
|
2021-07-08 14:12:04 -07:00
|
|
|
});
|
|
|
|
|
2022-06-05 07:17:26 -07:00
|
|
|
type State = ImmutableMap<string, StatusList>;
|
|
|
|
type StatusList = ReturnType<typeof StatusListRecord>;
|
|
|
|
|
|
|
|
const initialState: State = ImmutableMap({
|
|
|
|
favourites: StatusListRecord(),
|
|
|
|
bookmarks: StatusListRecord(),
|
|
|
|
pins: StatusListRecord(),
|
|
|
|
scheduled_statuses: StatusListRecord(),
|
2022-11-26 13:15:58 -08:00
|
|
|
recent_events: StatusListRecord(),
|
|
|
|
joined_events: StatusListRecord(),
|
2020-03-27 13:59:38 -07:00
|
|
|
});
|
|
|
|
|
2023-01-01 14:46:47 -08:00
|
|
|
const getStatusId = (status: string | APIEntity) => typeof status === 'string' ? status : status.id;
|
2021-07-08 14:12:04 -07:00
|
|
|
|
2023-01-01 14:46:47 -08:00
|
|
|
const getStatusIds = (statuses: APIEntity[] = []) => (
|
2022-06-05 07:17:26 -07:00
|
|
|
ImmutableOrderedSet(statuses.map(getStatusId))
|
2021-07-08 14:12:04 -07:00
|
|
|
);
|
|
|
|
|
2022-06-05 07:17:26 -07:00
|
|
|
const setLoading = (state: State, listType: string, loading: boolean) => state.setIn([listType, 'isLoading'], loading);
|
2021-07-08 14:12:04 -07:00
|
|
|
|
2023-01-01 14:46:47 -08:00
|
|
|
const normalizeList = (state: State, listType: string, statuses: APIEntity[], next: string | null) => {
|
2022-06-05 07:17:26 -07:00
|
|
|
return state.update(listType, StatusListRecord(), listMap => listMap.withMutations(map => {
|
2020-03-27 13:59:38 -07:00
|
|
|
map.set('next', next);
|
|
|
|
map.set('loaded', true);
|
|
|
|
map.set('isLoading', false);
|
2021-07-08 14:12:04 -07:00
|
|
|
map.set('items', getStatusIds(statuses));
|
2020-03-27 13:59:38 -07:00
|
|
|
}));
|
|
|
|
};
|
|
|
|
|
2023-01-01 14:46:47 -08:00
|
|
|
const appendToList = (state: State, listType: string, statuses: APIEntity[], next: string | null) => {
|
2021-07-08 14:12:04 -07:00
|
|
|
const newIds = getStatusIds(statuses);
|
|
|
|
|
2022-06-05 07:17:26 -07:00
|
|
|
return state.update(listType, StatusListRecord(), listMap => listMap.withMutations(map => {
|
2020-03-27 13:59:38 -07:00
|
|
|
map.set('next', next);
|
|
|
|
map.set('isLoading', false);
|
2022-06-05 07:17:26 -07:00
|
|
|
map.update('items', items => items.union(newIds));
|
2020-03-27 13:59:38 -07:00
|
|
|
}));
|
|
|
|
};
|
|
|
|
|
2023-01-01 14:46:47 -08:00
|
|
|
const prependOneToList = (state: State, listType: string, status: APIEntity) => {
|
2021-07-08 14:12:04 -07:00
|
|
|
const statusId = getStatusId(status);
|
|
|
|
return state.updateIn([listType, 'items'], ImmutableOrderedSet(), items => {
|
2022-06-05 07:17:26 -07:00
|
|
|
return ImmutableOrderedSet([statusId]).union(items as ImmutableOrderedSet<string>);
|
2021-07-08 14:12:04 -07:00
|
|
|
});
|
2020-03-27 13:59:38 -07:00
|
|
|
};
|
|
|
|
|
2023-01-01 14:46:47 -08:00
|
|
|
const removeOneFromList = (state: State, listType: string, status: APIEntity) => {
|
2021-07-08 14:12:04 -07:00
|
|
|
const statusId = getStatusId(status);
|
2022-06-05 07:17:26 -07:00
|
|
|
return state.updateIn([listType, 'items'], ImmutableOrderedSet(), items => (items as ImmutableOrderedSet<string>).delete(statusId));
|
2020-03-27 13:59:38 -07:00
|
|
|
};
|
|
|
|
|
2023-01-01 14:46:47 -08:00
|
|
|
const maybeAppendScheduledStatus = (state: State, status: APIEntity) => {
|
|
|
|
if (!status.scheduled_at) return state;
|
|
|
|
return prependOneToList(state, 'scheduled_statuses', getStatusId(status));
|
|
|
|
};
|
|
|
|
|
2022-06-05 07:17:26 -07:00
|
|
|
export default function statusLists(state = initialState, action: AnyAction) {
|
2022-05-11 10:40:34 -07:00
|
|
|
switch (action.type) {
|
2022-05-11 14:06:35 -07:00
|
|
|
case FAVOURITED_STATUSES_FETCH_REQUEST:
|
|
|
|
case FAVOURITED_STATUSES_EXPAND_REQUEST:
|
|
|
|
return setLoading(state, 'favourites', true);
|
|
|
|
case FAVOURITED_STATUSES_FETCH_FAIL:
|
|
|
|
case FAVOURITED_STATUSES_EXPAND_FAIL:
|
|
|
|
return setLoading(state, 'favourites', false);
|
|
|
|
case FAVOURITED_STATUSES_FETCH_SUCCESS:
|
|
|
|
return normalizeList(state, 'favourites', action.statuses, action.next);
|
|
|
|
case FAVOURITED_STATUSES_EXPAND_SUCCESS:
|
|
|
|
return appendToList(state, 'favourites', action.statuses, action.next);
|
|
|
|
case ACCOUNT_FAVOURITED_STATUSES_FETCH_REQUEST:
|
|
|
|
case ACCOUNT_FAVOURITED_STATUSES_EXPAND_REQUEST:
|
|
|
|
return setLoading(state, `favourites:${action.accountId}`, true);
|
|
|
|
case ACCOUNT_FAVOURITED_STATUSES_FETCH_FAIL:
|
|
|
|
case ACCOUNT_FAVOURITED_STATUSES_EXPAND_FAIL:
|
|
|
|
return setLoading(state, `favourites:${action.accountId}`, false);
|
|
|
|
case ACCOUNT_FAVOURITED_STATUSES_FETCH_SUCCESS:
|
|
|
|
return normalizeList(state, `favourites:${action.accountId}`, action.statuses, action.next);
|
|
|
|
case ACCOUNT_FAVOURITED_STATUSES_EXPAND_SUCCESS:
|
|
|
|
return appendToList(state, `favourites:${action.accountId}`, action.statuses, action.next);
|
|
|
|
case BOOKMARKED_STATUSES_FETCH_REQUEST:
|
|
|
|
case BOOKMARKED_STATUSES_EXPAND_REQUEST:
|
|
|
|
return setLoading(state, 'bookmarks', true);
|
|
|
|
case BOOKMARKED_STATUSES_FETCH_FAIL:
|
|
|
|
case BOOKMARKED_STATUSES_EXPAND_FAIL:
|
|
|
|
return setLoading(state, 'bookmarks', false);
|
|
|
|
case BOOKMARKED_STATUSES_FETCH_SUCCESS:
|
|
|
|
return normalizeList(state, 'bookmarks', action.statuses, action.next);
|
|
|
|
case BOOKMARKED_STATUSES_EXPAND_SUCCESS:
|
|
|
|
return appendToList(state, 'bookmarks', action.statuses, action.next);
|
|
|
|
case FAVOURITE_SUCCESS:
|
|
|
|
return prependOneToList(state, 'favourites', action.status);
|
|
|
|
case UNFAVOURITE_SUCCESS:
|
|
|
|
return removeOneFromList(state, 'favourites', action.status);
|
|
|
|
case BOOKMARK_SUCCESS:
|
|
|
|
return prependOneToList(state, 'bookmarks', action.status);
|
|
|
|
case UNBOOKMARK_SUCCESS:
|
|
|
|
return removeOneFromList(state, 'bookmarks', action.status);
|
|
|
|
case PINNED_STATUSES_FETCH_SUCCESS:
|
|
|
|
return normalizeList(state, 'pins', action.statuses, action.next);
|
|
|
|
case PIN_SUCCESS:
|
|
|
|
return prependOneToList(state, 'pins', action.status);
|
|
|
|
case UNPIN_SUCCESS:
|
|
|
|
return removeOneFromList(state, 'pins', action.status);
|
|
|
|
case SCHEDULED_STATUSES_FETCH_REQUEST:
|
|
|
|
case SCHEDULED_STATUSES_EXPAND_REQUEST:
|
|
|
|
return setLoading(state, 'scheduled_statuses', true);
|
|
|
|
case SCHEDULED_STATUSES_FETCH_FAIL:
|
|
|
|
case SCHEDULED_STATUSES_EXPAND_FAIL:
|
|
|
|
return setLoading(state, 'scheduled_statuses', false);
|
|
|
|
case SCHEDULED_STATUSES_FETCH_SUCCESS:
|
|
|
|
return normalizeList(state, 'scheduled_statuses', action.statuses, action.next);
|
|
|
|
case SCHEDULED_STATUSES_EXPAND_SUCCESS:
|
|
|
|
return appendToList(state, 'scheduled_statuses', action.statuses, action.next);
|
|
|
|
case SCHEDULED_STATUS_CANCEL_REQUEST:
|
|
|
|
case SCHEDULED_STATUS_CANCEL_SUCCESS:
|
2022-06-05 07:17:26 -07:00
|
|
|
return removeOneFromList(state, 'scheduled_statuses', action.id || action.status.id);
|
2022-11-29 15:32:21 -08:00
|
|
|
case STATUS_QUOTES_FETCH_REQUEST:
|
|
|
|
case STATUS_QUOTES_EXPAND_REQUEST:
|
|
|
|
return setLoading(state, `quotes:${action.statusId}`, true);
|
|
|
|
case STATUS_QUOTES_FETCH_FAIL:
|
|
|
|
case STATUS_QUOTES_EXPAND_FAIL:
|
|
|
|
return setLoading(state, `quotes:${action.statusId}`, false);
|
|
|
|
case STATUS_QUOTES_FETCH_SUCCESS:
|
|
|
|
return normalizeList(state, `quotes:${action.statusId}`, action.statuses, action.next);
|
|
|
|
case STATUS_QUOTES_EXPAND_SUCCESS:
|
|
|
|
return appendToList(state, `quotes:${action.statusId}`, action.statuses, action.next);
|
2022-11-26 13:15:58 -08:00
|
|
|
case RECENT_EVENTS_FETCH_REQUEST:
|
|
|
|
return setLoading(state, 'recent_events', true);
|
|
|
|
case RECENT_EVENTS_FETCH_FAIL:
|
|
|
|
return setLoading(state, 'recent_events', false);
|
|
|
|
case RECENT_EVENTS_FETCH_SUCCESS:
|
|
|
|
return normalizeList(state, 'recent_events', action.statuses, action.next);
|
|
|
|
case JOINED_EVENTS_FETCH_REQUEST:
|
|
|
|
return setLoading(state, 'joined_events', true);
|
|
|
|
case JOINED_EVENTS_FETCH_FAIL:
|
|
|
|
return setLoading(state, 'joined_events', false);
|
|
|
|
case JOINED_EVENTS_FETCH_SUCCESS:
|
|
|
|
return normalizeList(state, 'joined_events', action.statuses, action.next);
|
2023-01-01 14:46:47 -08:00
|
|
|
case STATUS_CREATE_SUCCESS:
|
|
|
|
return maybeAppendScheduledStatus(state, action.status);
|
2022-05-11 14:06:35 -07:00
|
|
|
default:
|
|
|
|
return state;
|
2020-03-27 13:59:38 -07:00
|
|
|
}
|
2021-08-03 12:22:51 -07:00
|
|
|
}
|