2022-06-18 11:58:42 -07:00
|
|
|
import { Map as ImmutableMap, OrderedSet as ImmutableOrderedSet } from 'immutable';
|
|
|
|
|
|
|
|
import { getSettings } from 'soapbox/actions/settings';
|
|
|
|
import { normalizeStatus } from 'soapbox/normalizers';
|
|
|
|
import { shouldFilter } from 'soapbox/utils/timelines';
|
|
|
|
|
|
|
|
import api, { getLinks } from '../api';
|
|
|
|
|
|
|
|
import { importFetchedStatus, importFetchedStatuses } from './importer';
|
|
|
|
|
|
|
|
import type { AxiosError } from 'axios';
|
|
|
|
import type { AppDispatch, RootState } from 'soapbox/store';
|
|
|
|
import type { APIEntity, Status } from 'soapbox/types/entities';
|
|
|
|
|
2022-07-01 13:09:07 -07:00
|
|
|
const TIMELINE_UPDATE = 'TIMELINE_UPDATE';
|
|
|
|
const TIMELINE_DELETE = 'TIMELINE_DELETE';
|
|
|
|
const TIMELINE_CLEAR = 'TIMELINE_CLEAR';
|
2022-06-18 11:58:42 -07:00
|
|
|
const TIMELINE_UPDATE_QUEUE = 'TIMELINE_UPDATE_QUEUE';
|
|
|
|
const TIMELINE_DEQUEUE = 'TIMELINE_DEQUEUE';
|
|
|
|
const TIMELINE_SCROLL_TOP = 'TIMELINE_SCROLL_TOP';
|
|
|
|
|
|
|
|
const TIMELINE_EXPAND_REQUEST = 'TIMELINE_EXPAND_REQUEST';
|
|
|
|
const TIMELINE_EXPAND_SUCCESS = 'TIMELINE_EXPAND_SUCCESS';
|
2022-07-01 13:09:07 -07:00
|
|
|
const TIMELINE_EXPAND_FAIL = 'TIMELINE_EXPAND_FAIL';
|
2022-06-18 11:58:42 -07:00
|
|
|
|
2022-07-01 13:09:07 -07:00
|
|
|
const TIMELINE_CONNECT = 'TIMELINE_CONNECT';
|
2022-06-18 11:58:42 -07:00
|
|
|
const TIMELINE_DISCONNECT = 'TIMELINE_DISCONNECT';
|
|
|
|
|
2022-06-22 08:20:10 -07:00
|
|
|
const TIMELINE_REPLACE = 'TIMELINE_REPLACE';
|
2022-07-01 13:09:07 -07:00
|
|
|
const TIMELINE_INSERT = 'TIMELINE_INSERT';
|
2022-07-20 11:23:11 -07:00
|
|
|
const TIMELINE_CLEAR_FEED_ACCOUNT_ID = 'TIMELINE_CLEAR_FEED_ACCOUNT_ID';
|
2022-06-22 08:20:10 -07:00
|
|
|
|
2022-06-18 11:58:42 -07:00
|
|
|
const MAX_QUEUED_ITEMS = 40;
|
|
|
|
|
|
|
|
const processTimelineUpdate = (timeline: string, status: APIEntity, accept: ((status: APIEntity) => boolean) | null) =>
|
|
|
|
(dispatch: AppDispatch, getState: () => RootState) => {
|
|
|
|
const me = getState().me;
|
|
|
|
const ownStatus = status.account?.id === me;
|
|
|
|
const hasPendingStatuses = !getState().pending_statuses.isEmpty();
|
|
|
|
|
|
|
|
const columnSettings = getSettings(getState()).get(timeline, ImmutableMap());
|
|
|
|
const shouldSkipQueue = shouldFilter(normalizeStatus(status) as Status, columnSettings);
|
|
|
|
|
|
|
|
if (ownStatus && hasPendingStatuses) {
|
|
|
|
// WebSockets push statuses without the Idempotency-Key,
|
|
|
|
// so if we have pending statuses, don't import it from here.
|
|
|
|
// We implement optimistic non-blocking statuses.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
dispatch(importFetchedStatus(status));
|
|
|
|
|
|
|
|
if (shouldSkipQueue) {
|
|
|
|
dispatch(updateTimeline(timeline, status.id, accept));
|
|
|
|
} else {
|
|
|
|
dispatch(updateTimelineQueue(timeline, status.id, accept));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const updateTimeline = (timeline: string, statusId: string, accept: ((status: APIEntity) => boolean) | null) =>
|
|
|
|
(dispatch: AppDispatch) => {
|
|
|
|
// if (typeof accept === 'function' && !accept(status)) {
|
|
|
|
// return;
|
|
|
|
// }
|
|
|
|
|
|
|
|
dispatch({
|
|
|
|
type: TIMELINE_UPDATE,
|
|
|
|
timeline,
|
|
|
|
statusId,
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
const updateTimelineQueue = (timeline: string, statusId: string, accept: ((status: APIEntity) => boolean) | null) =>
|
|
|
|
(dispatch: AppDispatch) => {
|
|
|
|
// if (typeof accept === 'function' && !accept(status)) {
|
|
|
|
// return;
|
|
|
|
// }
|
|
|
|
|
|
|
|
dispatch({
|
|
|
|
type: TIMELINE_UPDATE_QUEUE,
|
|
|
|
timeline,
|
|
|
|
statusId,
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
const dequeueTimeline = (timelineId: string, expandFunc?: (lastStatusId: string) => void, optionalExpandArgs?: any) =>
|
|
|
|
(dispatch: AppDispatch, getState: () => RootState) => {
|
|
|
|
const state = getState();
|
2022-06-23 15:33:23 -07:00
|
|
|
const queuedCount = state.timelines.get(timelineId)?.totalQueuedItemsCount || 0;
|
2022-06-18 11:58:42 -07:00
|
|
|
|
|
|
|
if (queuedCount <= 0) return;
|
|
|
|
|
|
|
|
if (queuedCount <= MAX_QUEUED_ITEMS) {
|
|
|
|
dispatch({ type: TIMELINE_DEQUEUE, timeline: timelineId });
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (typeof expandFunc === 'function') {
|
|
|
|
dispatch(clearTimeline(timelineId));
|
|
|
|
// @ts-ignore
|
|
|
|
expandFunc();
|
|
|
|
} else {
|
|
|
|
if (timelineId === 'home') {
|
|
|
|
dispatch(clearTimeline(timelineId));
|
|
|
|
dispatch(expandHomeTimeline(optionalExpandArgs));
|
|
|
|
} else if (timelineId === 'community') {
|
|
|
|
dispatch(clearTimeline(timelineId));
|
|
|
|
dispatch(expandCommunityTimeline(optionalExpandArgs));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const deleteFromTimelines = (id: string) =>
|
|
|
|
(dispatch: AppDispatch, getState: () => RootState) => {
|
2022-07-01 13:09:07 -07:00
|
|
|
const accountId = getState().statuses.get(id)?.account;
|
2022-06-18 11:58:42 -07:00
|
|
|
const references = getState().statuses.filter(status => status.get('reblog') === id).map(status => [status.get('id'), status.get('account')]);
|
2022-07-01 13:09:07 -07:00
|
|
|
const reblogOf = getState().statuses.getIn([id, 'reblog'], null);
|
2022-06-18 11:58:42 -07:00
|
|
|
|
|
|
|
dispatch({
|
|
|
|
type: TIMELINE_DELETE,
|
|
|
|
id,
|
|
|
|
accountId,
|
|
|
|
references,
|
|
|
|
reblogOf,
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
const clearTimeline = (timeline: string) =>
|
|
|
|
(dispatch: AppDispatch) =>
|
|
|
|
dispatch({ type: TIMELINE_CLEAR, timeline });
|
|
|
|
|
2022-07-01 13:09:07 -07:00
|
|
|
const noOp = () => { };
|
2022-06-18 11:58:42 -07:00
|
|
|
const noOpAsync = () => () => new Promise(f => f(undefined));
|
|
|
|
|
|
|
|
const parseTags = (tags: Record<string, any[]> = {}, mode: 'any' | 'all' | 'none') => {
|
|
|
|
return (tags[mode] || []).map((tag) => {
|
|
|
|
return tag.value;
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2022-06-22 08:20:10 -07:00
|
|
|
const replaceHomeTimeline = (
|
|
|
|
accountId: string | null,
|
|
|
|
{ maxId }: Record<string, any> = {},
|
2022-07-14 12:46:04 -07:00
|
|
|
done?: () => void,
|
2022-06-22 08:20:10 -07:00
|
|
|
) => (dispatch: AppDispatch, _getState: () => RootState) => {
|
|
|
|
dispatch({ type: TIMELINE_REPLACE, accountId });
|
2022-07-14 12:46:04 -07:00
|
|
|
dispatch(expandHomeTimeline({ accountId, maxId }, () => {
|
|
|
|
dispatch(insertSuggestionsIntoTimeline());
|
|
|
|
if (done) {
|
|
|
|
done();
|
|
|
|
}
|
|
|
|
}));
|
2022-06-22 08:20:10 -07:00
|
|
|
};
|
|
|
|
|
2022-06-18 11:58:42 -07:00
|
|
|
const expandTimeline = (timelineId: string, path: string, params: Record<string, any> = {}, done = noOp) =>
|
|
|
|
(dispatch: AppDispatch, getState: () => RootState) => {
|
2022-06-23 15:33:23 -07:00
|
|
|
const timeline = getState().timelines.get(timelineId) || {} as Record<string, any>;
|
2022-06-18 11:58:42 -07:00
|
|
|
const isLoadingMore = !!params.max_id;
|
|
|
|
|
2022-06-23 15:33:23 -07:00
|
|
|
if (timeline.isLoading) {
|
2022-06-18 11:58:42 -07:00
|
|
|
done();
|
|
|
|
return dispatch(noOpAsync());
|
|
|
|
}
|
|
|
|
|
2022-06-23 15:33:23 -07:00
|
|
|
if (!params.max_id && !params.pinned && (timeline.items || ImmutableOrderedSet()).size > 0) {
|
2022-06-27 07:55:40 -07:00
|
|
|
params.since_id = timeline.getIn(['items', 0]);
|
2022-06-18 11:58:42 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
const isLoadingRecent = !!params.since_id;
|
|
|
|
|
|
|
|
dispatch(expandTimelineRequest(timelineId, isLoadingMore));
|
|
|
|
|
|
|
|
return api(getState).get(path, { params }).then(response => {
|
|
|
|
const next = getLinks(response).refs.find(link => link.rel === 'next');
|
|
|
|
dispatch(importFetchedStatuses(response.data));
|
|
|
|
dispatch(expandTimelineSuccess(timelineId, response.data, next ? next.uri : null, response.status === 206, isLoadingRecent, isLoadingMore));
|
|
|
|
done();
|
|
|
|
}).catch(error => {
|
|
|
|
dispatch(expandTimelineFail(timelineId, error, isLoadingMore));
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2022-06-22 08:20:10 -07:00
|
|
|
const expandHomeTimeline = ({ accountId, maxId }: Record<string, any> = {}, done = noOp) => {
|
|
|
|
const endpoint = accountId ? `/api/v1/accounts/${accountId}/statuses` : '/api/v1/timelines/home';
|
|
|
|
const params: any = { max_id: maxId };
|
|
|
|
if (accountId) {
|
|
|
|
params.exclude_replies = true;
|
|
|
|
params.with_muted = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return expandTimeline('home', endpoint, params, done);
|
|
|
|
};
|
2022-06-18 11:58:42 -07:00
|
|
|
|
|
|
|
const expandPublicTimeline = ({ maxId, onlyMedia }: Record<string, any> = {}, done = noOp) =>
|
|
|
|
expandTimeline(`public${onlyMedia ? ':media' : ''}`, '/api/v1/timelines/public', { max_id: maxId, only_media: !!onlyMedia }, done);
|
|
|
|
|
|
|
|
const expandRemoteTimeline = (instance: string, { maxId, onlyMedia }: Record<string, any> = {}, done = noOp) =>
|
|
|
|
expandTimeline(`remote${onlyMedia ? ':media' : ''}:${instance}`, '/api/v1/timelines/public', { local: false, instance: instance, max_id: maxId, only_media: !!onlyMedia }, done);
|
|
|
|
|
|
|
|
const expandCommunityTimeline = ({ maxId, onlyMedia }: Record<string, any> = {}, done = noOp) =>
|
|
|
|
expandTimeline(`community${onlyMedia ? ':media' : ''}`, '/api/v1/timelines/public', { local: true, max_id: maxId, only_media: !!onlyMedia }, done);
|
|
|
|
|
|
|
|
const expandDirectTimeline = ({ maxId }: Record<string, any> = {}, done = noOp) =>
|
|
|
|
expandTimeline('direct', '/api/v1/timelines/direct', { max_id: maxId }, done);
|
|
|
|
|
|
|
|
const expandAccountTimeline = (accountId: string, { maxId, withReplies }: Record<string, any> = {}) =>
|
|
|
|
expandTimeline(`account:${accountId}${withReplies ? ':with_replies' : ''}`, `/api/v1/accounts/${accountId}/statuses`, { exclude_replies: !withReplies, max_id: maxId, with_muted: true });
|
|
|
|
|
|
|
|
const expandAccountFeaturedTimeline = (accountId: string) =>
|
|
|
|
expandTimeline(`account:${accountId}:pinned`, `/api/v1/accounts/${accountId}/statuses`, { pinned: true, with_muted: true });
|
|
|
|
|
|
|
|
const expandAccountMediaTimeline = (accountId: string | number, { maxId }: Record<string, any> = {}) =>
|
|
|
|
expandTimeline(`account:${accountId}:media`, `/api/v1/accounts/${accountId}/statuses`, { max_id: maxId, only_media: true, limit: 40, with_muted: true });
|
|
|
|
|
|
|
|
const expandListTimeline = (id: string, { maxId }: Record<string, any> = {}, done = noOp) =>
|
|
|
|
expandTimeline(`list:${id}`, `/api/v1/timelines/list/${id}`, { max_id: maxId }, done);
|
|
|
|
|
|
|
|
const expandGroupTimeline = (id: string, { maxId }: Record<string, any> = {}, done = noOp) =>
|
|
|
|
expandTimeline(`group:${id}`, `/api/v1/timelines/group/${id}`, { max_id: maxId }, done);
|
|
|
|
|
|
|
|
const expandHashtagTimeline = (hashtag: string, { maxId, tags }: Record<string, any> = {}, done = noOp) => {
|
|
|
|
return expandTimeline(`hashtag:${hashtag}`, `/api/v1/timelines/tag/${hashtag}`, {
|
|
|
|
max_id: maxId,
|
2022-07-01 13:09:07 -07:00
|
|
|
any: parseTags(tags, 'any'),
|
|
|
|
all: parseTags(tags, 'all'),
|
|
|
|
none: parseTags(tags, 'none'),
|
2022-06-18 11:58:42 -07:00
|
|
|
}, done);
|
|
|
|
};
|
|
|
|
|
|
|
|
const expandTimelineRequest = (timeline: string, isLoadingMore: boolean) => ({
|
|
|
|
type: TIMELINE_EXPAND_REQUEST,
|
|
|
|
timeline,
|
|
|
|
skipLoading: !isLoadingMore,
|
|
|
|
});
|
|
|
|
|
|
|
|
const expandTimelineSuccess = (timeline: string, statuses: APIEntity[], next: string | null, partial: boolean, isLoadingRecent: boolean, isLoadingMore: boolean) => ({
|
|
|
|
type: TIMELINE_EXPAND_SUCCESS,
|
|
|
|
timeline,
|
|
|
|
statuses,
|
|
|
|
next,
|
|
|
|
partial,
|
|
|
|
isLoadingRecent,
|
|
|
|
skipLoading: !isLoadingMore,
|
|
|
|
});
|
|
|
|
|
|
|
|
const expandTimelineFail = (timeline: string, error: AxiosError, isLoadingMore: boolean) => ({
|
|
|
|
type: TIMELINE_EXPAND_FAIL,
|
|
|
|
timeline,
|
|
|
|
error,
|
|
|
|
skipLoading: !isLoadingMore,
|
|
|
|
});
|
|
|
|
|
|
|
|
const connectTimeline = (timeline: string) => ({
|
|
|
|
type: TIMELINE_CONNECT,
|
|
|
|
timeline,
|
|
|
|
});
|
|
|
|
|
|
|
|
const disconnectTimeline = (timeline: string) => ({
|
|
|
|
type: TIMELINE_DISCONNECT,
|
|
|
|
timeline,
|
|
|
|
});
|
|
|
|
|
|
|
|
const scrollTopTimeline = (timeline: string, top: boolean) => ({
|
|
|
|
type: TIMELINE_SCROLL_TOP,
|
|
|
|
timeline,
|
|
|
|
top,
|
|
|
|
});
|
|
|
|
|
2022-07-01 13:09:07 -07:00
|
|
|
const insertSuggestionsIntoTimeline = () => (dispatch: AppDispatch, getState: () => RootState) => {
|
|
|
|
dispatch({ type: TIMELINE_INSERT, timeline: 'home' });
|
|
|
|
};
|
|
|
|
|
2022-07-20 11:23:11 -07:00
|
|
|
const clearFeedAccountId = () => (dispatch: AppDispatch, _getState: () => RootState) => {
|
|
|
|
dispatch({ type: TIMELINE_CLEAR_FEED_ACCOUNT_ID });
|
|
|
|
};
|
|
|
|
|
2022-06-18 11:58:42 -07:00
|
|
|
export {
|
|
|
|
TIMELINE_UPDATE,
|
|
|
|
TIMELINE_DELETE,
|
|
|
|
TIMELINE_CLEAR,
|
|
|
|
TIMELINE_UPDATE_QUEUE,
|
|
|
|
TIMELINE_DEQUEUE,
|
|
|
|
TIMELINE_SCROLL_TOP,
|
|
|
|
TIMELINE_EXPAND_REQUEST,
|
|
|
|
TIMELINE_EXPAND_SUCCESS,
|
|
|
|
TIMELINE_EXPAND_FAIL,
|
|
|
|
TIMELINE_CONNECT,
|
|
|
|
TIMELINE_DISCONNECT,
|
2022-06-22 08:20:10 -07:00
|
|
|
TIMELINE_REPLACE,
|
2022-07-20 11:23:11 -07:00
|
|
|
TIMELINE_CLEAR_FEED_ACCOUNT_ID,
|
2022-07-01 13:09:07 -07:00
|
|
|
TIMELINE_INSERT,
|
2022-06-18 11:58:42 -07:00
|
|
|
MAX_QUEUED_ITEMS,
|
|
|
|
processTimelineUpdate,
|
|
|
|
updateTimeline,
|
|
|
|
updateTimelineQueue,
|
|
|
|
dequeueTimeline,
|
|
|
|
deleteFromTimelines,
|
|
|
|
clearTimeline,
|
|
|
|
expandTimeline,
|
2022-06-22 08:20:10 -07:00
|
|
|
replaceHomeTimeline,
|
2022-06-18 11:58:42 -07:00
|
|
|
expandHomeTimeline,
|
|
|
|
expandPublicTimeline,
|
|
|
|
expandRemoteTimeline,
|
|
|
|
expandCommunityTimeline,
|
|
|
|
expandDirectTimeline,
|
|
|
|
expandAccountTimeline,
|
|
|
|
expandAccountFeaturedTimeline,
|
|
|
|
expandAccountMediaTimeline,
|
|
|
|
expandListTimeline,
|
|
|
|
expandGroupTimeline,
|
|
|
|
expandHashtagTimeline,
|
|
|
|
expandTimelineRequest,
|
|
|
|
expandTimelineSuccess,
|
|
|
|
expandTimelineFail,
|
|
|
|
connectTimeline,
|
|
|
|
disconnectTimeline,
|
|
|
|
scrollTopTimeline,
|
2022-07-01 13:09:07 -07:00
|
|
|
insertSuggestionsIntoTimeline,
|
2022-07-20 11:23:11 -07:00
|
|
|
clearFeedAccountId,
|
2022-06-18 11:58:42 -07:00
|
|
|
};
|