2020-08-11 16:24:59 +00:00
|
|
|
// @ts-check
|
|
|
|
|
2017-11-15 15:04:15 +00:00
|
|
|
import { connectStream } from '../stream';
|
2017-08-21 13:04:34 +00:00
|
|
|
import {
|
|
|
|
updateTimeline,
|
|
|
|
deleteFromTimelines,
|
2021-07-16 06:38:50 +00:00
|
|
|
expireFromTimelines,
|
2018-03-24 14:25:15 +00:00
|
|
|
expandHomeTimeline,
|
2019-03-07 21:17:52 +00:00
|
|
|
connectTimeline,
|
2017-08-21 13:04:34 +00:00
|
|
|
disconnectTimeline,
|
|
|
|
} from './timelines';
|
2021-09-14 07:28:52 +00:00
|
|
|
import { fetchRelationships, fetchAccounts } from './accounts';
|
2021-04-12 12:43:25 +00:00
|
|
|
import { getHomeVisibilities } from 'mastodon/selectors';
|
2018-03-24 21:07:23 +00:00
|
|
|
import { updateNotifications, expandNotifications } from './notifications';
|
2018-10-07 21:44:58 +00:00
|
|
|
import { updateConversations } from './conversations';
|
2021-05-19 05:58:27 +00:00
|
|
|
import { updateEmojiReaction } from './interactions';
|
2020-01-26 19:07:26 +00:00
|
|
|
import {
|
|
|
|
fetchAnnouncements,
|
|
|
|
updateAnnouncements,
|
|
|
|
updateReaction as updateAnnouncementsReaction,
|
|
|
|
deleteAnnouncement,
|
|
|
|
} from './announcements';
|
2018-06-29 13:34:36 +00:00
|
|
|
import { fetchFilters } from './filters';
|
2017-08-21 13:04:34 +00:00
|
|
|
import { getLocale } from '../locales';
|
2023-01-08 16:07:46 +00:00
|
|
|
import { deleteScheduledStatusSuccess } from './scheduled_statuses';
|
2017-08-21 13:04:34 +00:00
|
|
|
|
|
|
|
const { messages } = getLocale();
|
|
|
|
|
2020-08-11 16:24:59 +00:00
|
|
|
/**
|
|
|
|
* @param {number} max
|
|
|
|
* @return {number}
|
|
|
|
*/
|
|
|
|
const randomUpTo = max =>
|
|
|
|
Math.floor(Math.random() * Math.floor(max));
|
2017-08-21 13:04:34 +00:00
|
|
|
|
2020-08-11 16:24:59 +00:00
|
|
|
/**
|
|
|
|
* @param {string} timelineId
|
|
|
|
* @param {string} channelName
|
|
|
|
* @param {Object.<string, string>} params
|
|
|
|
* @param {Object} options
|
|
|
|
* @param {function(Function, Function): void} [options.fallback]
|
|
|
|
* @param {function(object): boolean} [options.accept]
|
|
|
|
* @return {function(): void}
|
|
|
|
*/
|
|
|
|
export const connectTimelineStream = (timelineId, channelName, params = {}, options = {}) =>
|
|
|
|
connectStream(channelName, params, (dispatch, getState) => {
|
2017-11-15 15:04:15 +00:00
|
|
|
const locale = getState().getIn(['meta', 'locale']);
|
2019-03-07 21:17:52 +00:00
|
|
|
|
2020-08-11 16:24:59 +00:00
|
|
|
let pollingId;
|
|
|
|
|
|
|
|
/**
|
2021-04-12 12:43:25 +00:00
|
|
|
* @param {function(Function, Function, Function): void} fallback
|
2020-08-11 16:24:59 +00:00
|
|
|
*/
|
|
|
|
const useFallback = fallback => {
|
2021-04-12 12:43:25 +00:00
|
|
|
fallback(dispatch, getState, () => {
|
2020-08-11 16:24:59 +00:00
|
|
|
pollingId = setTimeout(() => useFallback(fallback), 20000 + randomUpTo(20000));
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2017-11-15 15:04:15 +00:00
|
|
|
return {
|
2019-03-07 21:17:52 +00:00
|
|
|
onConnect() {
|
|
|
|
dispatch(connectTimeline(timelineId));
|
2020-08-11 16:24:59 +00:00
|
|
|
|
|
|
|
if (pollingId) {
|
|
|
|
clearTimeout(pollingId);
|
|
|
|
pollingId = null;
|
|
|
|
}
|
2019-03-07 21:17:52 +00:00
|
|
|
},
|
|
|
|
|
2017-11-15 15:04:15 +00:00
|
|
|
onDisconnect() {
|
2017-08-21 13:04:34 +00:00
|
|
|
dispatch(disconnectTimeline(timelineId));
|
2020-08-11 16:24:59 +00:00
|
|
|
|
|
|
|
if (options.fallback) {
|
|
|
|
pollingId = setTimeout(() => useFallback(options.fallback), randomUpTo(40000));
|
|
|
|
}
|
2017-08-21 13:04:34 +00:00
|
|
|
},
|
|
|
|
|
2017-11-15 15:04:15 +00:00
|
|
|
onReceive (data) {
|
2017-08-21 13:04:34 +00:00
|
|
|
switch(data.event) {
|
|
|
|
case 'update':
|
2020-08-11 16:24:59 +00:00
|
|
|
dispatch(updateTimeline(timelineId, JSON.parse(data.payload), options.accept));
|
2017-08-21 13:04:34 +00:00
|
|
|
break;
|
|
|
|
case 'delete':
|
|
|
|
dispatch(deleteFromTimelines(data.payload));
|
|
|
|
break;
|
2021-07-16 06:38:50 +00:00
|
|
|
case 'expire':
|
|
|
|
dispatch(expireFromTimelines(data.payload));
|
|
|
|
break;
|
2023-01-08 16:07:46 +00:00
|
|
|
case 'scheduled_status':
|
|
|
|
dispatch(deleteScheduledStatusSuccess(data.payload));
|
|
|
|
break;
|
2017-08-21 13:04:34 +00:00
|
|
|
case 'notification':
|
|
|
|
dispatch(updateNotifications(JSON.parse(data.payload), messages, locale));
|
|
|
|
break;
|
2018-10-07 21:44:58 +00:00
|
|
|
case 'conversation':
|
|
|
|
dispatch(updateConversations(JSON.parse(data.payload)));
|
|
|
|
break;
|
2018-06-29 13:34:36 +00:00
|
|
|
case 'filters_changed':
|
|
|
|
dispatch(fetchFilters());
|
|
|
|
break;
|
2021-05-19 05:58:27 +00:00
|
|
|
case 'emoji_reaction':
|
2021-09-14 07:28:52 +00:00
|
|
|
const emojiReaction = JSON.parse(data.payload);
|
|
|
|
dispatch(fetchRelationships(emojiReaction.account_ids));
|
|
|
|
dispatch(fetchAccounts(emojiReaction.account_ids));
|
|
|
|
dispatch(updateEmojiReaction(emojiReaction));
|
2021-05-19 05:58:27 +00:00
|
|
|
break;
|
2020-01-23 21:00:13 +00:00
|
|
|
case 'announcement':
|
|
|
|
dispatch(updateAnnouncements(JSON.parse(data.payload)));
|
|
|
|
break;
|
|
|
|
case 'announcement.reaction':
|
|
|
|
dispatch(updateAnnouncementsReaction(JSON.parse(data.payload)));
|
2020-01-26 19:07:26 +00:00
|
|
|
break;
|
|
|
|
case 'announcement.delete':
|
|
|
|
dispatch(deleteAnnouncement(data.payload));
|
2020-01-23 21:00:13 +00:00
|
|
|
break;
|
2017-08-21 13:04:34 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
};
|
2017-11-15 15:04:15 +00:00
|
|
|
});
|
2017-08-21 13:04:34 +00:00
|
|
|
|
2020-08-11 16:24:59 +00:00
|
|
|
/**
|
|
|
|
* @param {Function} dispatch
|
|
|
|
* @param {function(): void} done
|
|
|
|
*/
|
2021-04-12 12:43:25 +00:00
|
|
|
const refreshHomeTimelineAndNotification = (dispatch, getState, done) => {
|
|
|
|
const visibilities = getHomeVisibilities(getState());
|
|
|
|
|
|
|
|
dispatch(expandHomeTimeline({ visibilities }, () =>
|
2020-01-23 21:00:13 +00:00
|
|
|
dispatch(expandNotifications({}, () =>
|
|
|
|
dispatch(fetchAnnouncements(done))))));
|
2018-05-18 00:32:35 +00:00
|
|
|
};
|
2017-08-21 13:04:34 +00:00
|
|
|
|
2020-08-11 16:24:59 +00:00
|
|
|
/**
|
|
|
|
* @return {function(): void}
|
|
|
|
*/
|
|
|
|
export const connectUserStream = () =>
|
|
|
|
connectTimelineStream('home', 'user', {}, { fallback: refreshHomeTimelineAndNotification });
|
|
|
|
|
|
|
|
/**
|
2019-11-13 22:42:56 +00:00
|
|
|
* @param {string} domain
|
2020-08-11 16:24:59 +00:00
|
|
|
* @param {Object} options
|
|
|
|
* @param {boolean} [options.onlyMedia]
|
2022-07-03 05:54:03 +00:00
|
|
|
* @param {boolean} [options.withoutMedia]
|
|
|
|
* @param {boolean} [options.withoutBot]
|
2019-11-13 22:42:56 +00:00
|
|
|
* @return {function(): void}
|
|
|
|
*/
|
2022-07-03 05:54:03 +00:00
|
|
|
export const connectDomainStream = (domain, { onlyMedia, withoutMedia, withoutBot } = {}) =>
|
|
|
|
connectTimelineStream(`domain${withoutBot ? ':nobot' : ':bot'}${withoutMedia ? ':nomedia' : ''}${onlyMedia ? ':media' : ''}:${domain}`, `public:domain${withoutBot ? ':nobot' : ':bot'}${withoutMedia ? ':nomedia' : ''}${onlyMedia ? ':media' : ''}`, { domain: domain });
|
2019-11-13 22:42:56 +00:00
|
|
|
|
2020-07-12 13:00:23 +00:00
|
|
|
/**
|
|
|
|
* @param {string} id
|
|
|
|
* @param {Object} options
|
|
|
|
* @param {boolean} [options.onlyMedia]
|
2022-07-03 05:54:03 +00:00
|
|
|
* @param {boolean} [options.withoutMedia]
|
2020-07-12 13:00:23 +00:00
|
|
|
* @param {string} [options.tagged]
|
|
|
|
* @return {function(): void}
|
|
|
|
*/
|
2022-07-03 05:54:03 +00:00
|
|
|
export const connectGroupStream = (id, { onlyMedia, withoutMedia, tagged } = {}) =>
|
|
|
|
connectTimelineStream(`group:${id}${withoutMedia ? ':nomedia' : ''}${onlyMedia ? ':media' : ''}${tagged ? `:${tagged}` : ''}`, `group${withoutMedia ? ':nomedia' : ''}${onlyMedia ? ':media' : ''}`, { id: id, tagged: tagged });
|
2020-07-12 13:00:23 +00:00
|
|
|
|
2019-11-13 22:42:56 +00:00
|
|
|
/**
|
|
|
|
* @param {Object} options
|
2020-08-11 16:24:59 +00:00
|
|
|
* @param {boolean} [options.onlyRemote]
|
2019-11-13 22:42:56 +00:00
|
|
|
* @param {boolean} [options.onlyMedia]
|
2022-07-03 05:54:03 +00:00
|
|
|
* @param {boolean} [options.withoutMedia]
|
|
|
|
* @param {boolean} [options.withoutBot]
|
2020-08-11 16:24:59 +00:00
|
|
|
* @return {function(): void}
|
|
|
|
*/
|
2022-07-03 05:54:03 +00:00
|
|
|
export const connectPublicStream = ({ onlyMedia, withoutMedia, withoutBot, onlyRemote } = {}) =>
|
|
|
|
connectTimelineStream(`public${onlyRemote ? ':remote' : ''}${withoutBot ? ':nobot' : ':bot'}${withoutMedia ? ':nomedia' : ''}${onlyMedia ? ':media' : ''}`, `public${onlyRemote ? ':remote' : ''}${withoutBot ? ':nobot' : ':bot'}${withoutMedia ? ':nomedia' : ''}${onlyMedia ? ':media' : ''}`);
|
2020-08-11 16:24:59 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {string} columnId
|
|
|
|
* @param {string} tagName
|
|
|
|
* @param {function(object): boolean} accept
|
|
|
|
* @return {function(): void}
|
|
|
|
*/
|
2019-08-15 14:45:25 +00:00
|
|
|
export const connectHashtagStream = (columnId, tagName, accept) =>
|
2022-07-03 05:54:03 +00:00
|
|
|
connectTimelineStream(`hashtag:${columnId}`, 'hashtag', { tag: tagName }, { accept });
|
2020-08-11 16:24:59 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @return {function(): void}
|
|
|
|
*/
|
|
|
|
export const connectDirectStream = () =>
|
|
|
|
connectTimelineStream('direct', 'direct');
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {string} listId
|
|
|
|
* @return {function(): void}
|
|
|
|
*/
|
|
|
|
export const connectListStream = listId =>
|
|
|
|
connectTimelineStream(`list:${listId}`, 'list', { list: listId });
|