2022-10-05 12:15:16 -07:00
|
|
|
import { InfiniteData } from '@tanstack/react-query';
|
|
|
|
|
2022-06-18 11:58:42 -07:00
|
|
|
import { getSettings } from 'soapbox/actions/settings';
|
|
|
|
import messages from 'soapbox/locales/messages';
|
2022-10-05 12:15:16 -07:00
|
|
|
import { ChatKeys } from 'soapbox/queries/chats';
|
2022-09-28 13:55:56 -07:00
|
|
|
import { queryClient } from 'soapbox/queries/client';
|
2022-10-27 07:25:37 -07:00
|
|
|
import { updatePageItem, appendPageItem, removePageItem, flattenPages, PaginatedResult } from 'soapbox/utils/queries';
|
2022-08-25 12:56:53 -07:00
|
|
|
import { play, soundCache } from 'soapbox/utils/sounds';
|
2022-06-18 11:58:42 -07:00
|
|
|
|
|
|
|
import { connectStream } from '../stream';
|
|
|
|
|
2022-07-06 14:25:19 -07:00
|
|
|
import {
|
|
|
|
deleteAnnouncement,
|
|
|
|
fetchAnnouncements,
|
|
|
|
updateAnnouncements,
|
|
|
|
updateReaction as updateAnnouncementsReaction,
|
|
|
|
} from './announcements';
|
2022-06-18 11:58:42 -07:00
|
|
|
import { updateConversations } from './conversations';
|
|
|
|
import { fetchFilters } from './filters';
|
2022-08-01 09:31:49 -07:00
|
|
|
import { MARKER_FETCH_SUCCESS } from './markers';
|
2022-06-18 11:58:42 -07:00
|
|
|
import { updateNotificationsQueue, expandNotifications } from './notifications';
|
|
|
|
import { updateStatus } from './statuses';
|
|
|
|
import {
|
2022-06-24 13:36:06 -07:00
|
|
|
// deleteFromTimelines,
|
2022-06-18 11:58:42 -07:00
|
|
|
expandHomeTimeline,
|
|
|
|
connectTimeline,
|
|
|
|
disconnectTimeline,
|
|
|
|
processTimelineUpdate,
|
|
|
|
} from './timelines';
|
|
|
|
|
|
|
|
import type { AppDispatch, RootState } from 'soapbox/store';
|
2022-09-21 15:52:20 -07:00
|
|
|
import type { APIEntity, Chat, ChatMessage } from 'soapbox/types/entities';
|
2022-06-18 11:58:42 -07:00
|
|
|
|
|
|
|
const STREAMING_CHAT_UPDATE = 'STREAMING_CHAT_UPDATE';
|
|
|
|
const STREAMING_FOLLOW_RELATIONSHIPS_UPDATE = 'STREAMING_FOLLOW_RELATIONSHIPS_UPDATE';
|
|
|
|
|
|
|
|
const validLocale = (locale: string) => Object.keys(messages).includes(locale);
|
|
|
|
|
|
|
|
const getLocale = (state: RootState) => {
|
|
|
|
const locale = getSettings(state).get('locale') as string;
|
|
|
|
return validLocale(locale) ? locale : 'en';
|
|
|
|
};
|
|
|
|
|
|
|
|
const updateFollowRelationships = (relationships: APIEntity) =>
|
|
|
|
(dispatch: AppDispatch, getState: () => RootState) => {
|
|
|
|
const me = getState().me;
|
|
|
|
return dispatch({
|
|
|
|
type: STREAMING_FOLLOW_RELATIONSHIPS_UPDATE,
|
|
|
|
me,
|
|
|
|
...relationships,
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2022-09-21 15:52:20 -07:00
|
|
|
interface ChatPayload extends Omit<Chat, 'last_message'> {
|
|
|
|
last_message: ChatMessage | null,
|
|
|
|
}
|
|
|
|
|
|
|
|
const updateChat = (payload: ChatPayload) => {
|
2022-09-28 13:55:56 -07:00
|
|
|
const { id: chatId, last_message: lastMessage } = payload;
|
2022-09-21 16:26:18 -07:00
|
|
|
|
2022-10-05 12:15:16 -07:00
|
|
|
const currentChats = flattenPages(queryClient.getQueryData<InfiniteData<PaginatedResult<unknown>>>(ChatKeys.chatSearch()));
|
|
|
|
|
|
|
|
// Update the specific Chat query data.
|
|
|
|
queryClient.setQueryData<Chat>(ChatKeys.chat(chatId), payload as any);
|
|
|
|
|
|
|
|
if (currentChats?.find((chat: any) => chat.id === chatId)) {
|
|
|
|
// If the chat exists in the client, let's update it.
|
|
|
|
updatePageItem<Chat>(ChatKeys.chatSearch(), payload as any, (o, n) => o.id === n.id);
|
|
|
|
} else {
|
|
|
|
// If this is a brand-new chat, let's invalid the queries.
|
|
|
|
queryClient.invalidateQueries(ChatKeys.chatSearch());
|
|
|
|
}
|
2022-09-21 15:52:20 -07:00
|
|
|
|
2022-09-21 16:26:18 -07:00
|
|
|
if (lastMessage) {
|
2022-10-05 12:15:16 -07:00
|
|
|
// Update the Chat Messages query data.
|
|
|
|
appendPageItem(ChatKeys.chatMessages(payload.id), lastMessage);
|
2022-09-21 16:26:18 -07:00
|
|
|
}
|
2022-09-21 15:52:20 -07:00
|
|
|
};
|
|
|
|
|
2022-10-27 07:25:37 -07:00
|
|
|
const removeChatMessage = (payload: string) => {
|
|
|
|
const chat = JSON.parse(payload);
|
|
|
|
const chatMessageId = chat.chat_message_id;
|
|
|
|
|
|
|
|
removePageItem(ChatKeys.chatMessages(chat.id), chatMessageId, (o: any, n: any) => Number(o.id) === Number(n));
|
|
|
|
};
|
|
|
|
|
2022-06-18 11:58:42 -07:00
|
|
|
const connectTimelineStream = (
|
|
|
|
timelineId: string,
|
|
|
|
path: string,
|
|
|
|
pollingRefresh: ((dispatch: AppDispatch, done?: () => void) => void) | null = null,
|
|
|
|
accept: ((status: APIEntity) => boolean) | null = null,
|
|
|
|
) => connectStream(path, pollingRefresh, (dispatch: AppDispatch, getState: () => RootState) => {
|
|
|
|
const locale = getLocale(getState());
|
|
|
|
|
|
|
|
return {
|
|
|
|
onConnect() {
|
|
|
|
dispatch(connectTimeline(timelineId));
|
|
|
|
},
|
|
|
|
|
|
|
|
onDisconnect() {
|
|
|
|
dispatch(disconnectTimeline(timelineId));
|
|
|
|
},
|
|
|
|
|
|
|
|
onReceive(data: any) {
|
|
|
|
switch (data.event) {
|
|
|
|
case 'update':
|
|
|
|
dispatch(processTimelineUpdate(timelineId, JSON.parse(data.payload), accept));
|
|
|
|
break;
|
|
|
|
case 'status.update':
|
|
|
|
dispatch(updateStatus(JSON.parse(data.payload)));
|
|
|
|
break;
|
2022-06-23 12:58:38 -07:00
|
|
|
// FIXME: We think delete & redraft is causing jumpy timelines.
|
|
|
|
// Fix that in ScrollableList then re-enable this!
|
|
|
|
//
|
|
|
|
// case 'delete':
|
|
|
|
// dispatch(deleteFromTimelines(data.payload));
|
|
|
|
// break;
|
2022-06-18 11:58:42 -07:00
|
|
|
case 'notification':
|
|
|
|
messages[locale]().then(messages => {
|
|
|
|
dispatch(updateNotificationsQueue(JSON.parse(data.payload), messages, locale, window.location.pathname));
|
|
|
|
}).catch(error => {
|
|
|
|
console.error(error);
|
|
|
|
});
|
|
|
|
break;
|
|
|
|
case 'conversation':
|
|
|
|
dispatch(updateConversations(JSON.parse(data.payload)));
|
|
|
|
break;
|
|
|
|
case 'filters_changed':
|
|
|
|
dispatch(fetchFilters());
|
|
|
|
break;
|
|
|
|
case 'pleroma:chat_update':
|
2022-10-27 07:25:37 -07:00
|
|
|
case 'chat_message.created': // TruthSocial
|
2022-09-29 08:58:58 -07:00
|
|
|
dispatch((dispatch: AppDispatch, getState: () => RootState) => {
|
|
|
|
const chat = JSON.parse(data.payload);
|
|
|
|
const me = getState().me;
|
|
|
|
const messageOwned = chat.last_message?.account_id === me;
|
|
|
|
|
|
|
|
// Don't update own messages from streaming
|
|
|
|
if (!messageOwned) {
|
|
|
|
updateChat(chat);
|
|
|
|
play(soundCache.chat);
|
|
|
|
}
|
|
|
|
});
|
2022-06-18 11:58:42 -07:00
|
|
|
break;
|
2022-10-27 07:25:37 -07:00
|
|
|
case 'chat_message.deleted': // TruthSocial
|
|
|
|
removeChatMessage(data.payload);
|
|
|
|
break;
|
2022-06-18 11:58:42 -07:00
|
|
|
case 'pleroma:follow_relationships_update':
|
|
|
|
dispatch(updateFollowRelationships(JSON.parse(data.payload)));
|
|
|
|
break;
|
2022-07-06 14:25:19 -07:00
|
|
|
case 'announcement':
|
|
|
|
dispatch(updateAnnouncements(JSON.parse(data.payload)));
|
|
|
|
break;
|
|
|
|
case 'announcement.reaction':
|
|
|
|
dispatch(updateAnnouncementsReaction(JSON.parse(data.payload)));
|
|
|
|
break;
|
|
|
|
case 'announcement.delete':
|
|
|
|
dispatch(deleteAnnouncement(data.payload));
|
|
|
|
break;
|
2022-08-01 09:31:49 -07:00
|
|
|
case 'marker':
|
|
|
|
dispatch({ type: MARKER_FETCH_SUCCESS, marker: JSON.parse(data.payload) });
|
|
|
|
break;
|
2022-06-18 11:58:42 -07:00
|
|
|
}
|
|
|
|
},
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
|
|
|
const refreshHomeTimelineAndNotification = (dispatch: AppDispatch, done?: () => void) =>
|
2022-07-06 14:25:19 -07:00
|
|
|
dispatch(expandHomeTimeline({}, () =>
|
|
|
|
dispatch(expandNotifications({}, () =>
|
|
|
|
dispatch(fetchAnnouncements(done))))));
|
2022-06-18 11:58:42 -07:00
|
|
|
|
|
|
|
const connectUserStream = () =>
|
|
|
|
connectTimelineStream('home', 'user', refreshHomeTimelineAndNotification);
|
|
|
|
|
|
|
|
const connectCommunityStream = ({ onlyMedia }: Record<string, any> = {}) =>
|
|
|
|
connectTimelineStream(`community${onlyMedia ? ':media' : ''}`, `public:local${onlyMedia ? ':media' : ''}`);
|
|
|
|
|
|
|
|
const connectPublicStream = ({ onlyMedia }: Record<string, any> = {}) =>
|
|
|
|
connectTimelineStream(`public${onlyMedia ? ':media' : ''}`, `public${onlyMedia ? ':media' : ''}`);
|
|
|
|
|
|
|
|
const connectRemoteStream = (instance: string, { onlyMedia }: Record<string, any> = {}) =>
|
|
|
|
connectTimelineStream(`remote${onlyMedia ? ':media' : ''}:${instance}`, `public:remote${onlyMedia ? ':media' : ''}&instance=${instance}`);
|
|
|
|
|
|
|
|
const connectHashtagStream = (id: string, tag: string, accept: (status: APIEntity) => boolean) =>
|
|
|
|
connectTimelineStream(`hashtag:${id}`, `hashtag&tag=${tag}`, null, accept);
|
|
|
|
|
|
|
|
const connectDirectStream = () =>
|
|
|
|
connectTimelineStream('direct', 'direct');
|
|
|
|
|
|
|
|
const connectListStream = (id: string) =>
|
|
|
|
connectTimelineStream(`list:${id}`, `list&list=${id}`);
|
|
|
|
|
|
|
|
const connectGroupStream = (id: string) =>
|
|
|
|
connectTimelineStream(`group:${id}`, `group&group=${id}`);
|
|
|
|
|
|
|
|
export {
|
|
|
|
STREAMING_CHAT_UPDATE,
|
|
|
|
STREAMING_FOLLOW_RELATIONSHIPS_UPDATE,
|
|
|
|
connectTimelineStream,
|
|
|
|
connectUserStream,
|
|
|
|
connectCommunityStream,
|
|
|
|
connectPublicStream,
|
|
|
|
connectRemoteStream,
|
|
|
|
connectHashtagStream,
|
|
|
|
connectDirectStream,
|
|
|
|
connectListStream,
|
|
|
|
connectGroupStream,
|
|
|
|
};
|