2023-03-13 12:44:09 -07:00
|
|
|
import { getLocale, getSettings } from 'soapbox/actions/settings';
|
2023-07-22 14:38:21 -07:00
|
|
|
import { importEntities } from 'soapbox/entity-store/actions';
|
|
|
|
import { Entities } from 'soapbox/entity-store/entities';
|
|
|
|
import { selectEntity } from 'soapbox/entity-store/selectors';
|
2022-06-18 11:58:42 -07:00
|
|
|
import messages from 'soapbox/locales/messages';
|
2022-11-02 12:28:16 -07:00
|
|
|
import { ChatKeys, IChat, isLastMessage } from 'soapbox/queries/chats';
|
2022-09-28 13:55:56 -07:00
|
|
|
import { queryClient } from 'soapbox/queries/client';
|
2023-02-08 09:58:01 -08:00
|
|
|
import { getUnreadChatsCount, updateChatListItem, updateChatMessage } from 'soapbox/utils/chats';
|
2022-12-02 12:50:54 -08:00
|
|
|
import { removePageItem } from 'soapbox/utils/queries';
|
2022-12-06 15:18:47 -08: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,
|
|
|
|
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';
|
2023-07-22 10:53:06 -07:00
|
|
|
import { updateNotificationsQueue } from './notifications';
|
2022-06-18 11:58:42 -07:00
|
|
|
import { updateStatus } from './statuses';
|
|
|
|
import {
|
2022-06-24 13:36:06 -07:00
|
|
|
// deleteFromTimelines,
|
2022-06-18 11:58:42 -07:00
|
|
|
connectTimeline,
|
|
|
|
disconnectTimeline,
|
|
|
|
processTimelineUpdate,
|
|
|
|
} from './timelines';
|
|
|
|
|
2022-12-06 22:45:10 -08:00
|
|
|
import type { IStatContext } from 'soapbox/contexts/stat-context';
|
2023-07-22 14:38:21 -07:00
|
|
|
import type { Relationship } from 'soapbox/schemas';
|
2022-06-18 11:58:42 -07:00
|
|
|
import type { AppDispatch, RootState } from 'soapbox/store';
|
2022-12-02 12:50:54 -08:00
|
|
|
import type { APIEntity, Chat } from 'soapbox/types/entities';
|
2022-06-18 11:58:42 -07:00
|
|
|
|
|
|
|
const STREAMING_CHAT_UPDATE = 'STREAMING_CHAT_UPDATE';
|
|
|
|
|
2022-10-27 07:25:37 -07:00
|
|
|
const removeChatMessage = (payload: string) => {
|
2022-10-27 10:59:54 -07:00
|
|
|
const data = JSON.parse(payload);
|
2022-11-01 06:44:21 -07:00
|
|
|
const chatId = data.chat_id;
|
2022-10-27 10:59:54 -07:00
|
|
|
const chatMessageId = data.deleted_message_id;
|
2022-10-27 07:25:37 -07:00
|
|
|
|
2022-11-01 06:44:21 -07:00
|
|
|
// If the user just deleted the "last_message", then let's invalidate
|
|
|
|
// the Chat Search query so the Chat List will show the new "last_message".
|
|
|
|
if (isLastMessage(chatMessageId)) {
|
|
|
|
queryClient.invalidateQueries(ChatKeys.chatSearch());
|
|
|
|
}
|
|
|
|
|
|
|
|
removePageItem(ChatKeys.chatMessages(chatId), chatMessageId, (o: any, n: any) => String(o.id) === String(n));
|
2022-10-27 07:25:37 -07:00
|
|
|
};
|
|
|
|
|
2022-11-02 12:28:16 -07:00
|
|
|
// Update the specific Chat query data.
|
|
|
|
const updateChatQuery = (chat: IChat) => {
|
|
|
|
const cachedChat = queryClient.getQueryData<IChat>(ChatKeys.chat(chat.id));
|
|
|
|
if (!cachedChat) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const newChat = {
|
|
|
|
...cachedChat,
|
|
|
|
latest_read_message_by_account: chat.latest_read_message_by_account,
|
|
|
|
latest_read_message_created_at: chat.latest_read_message_created_at,
|
|
|
|
};
|
|
|
|
queryClient.setQueryData<Chat>(ChatKeys.chat(chat.id), newChat as any);
|
|
|
|
};
|
|
|
|
|
2023-07-22 10:49:02 -07:00
|
|
|
interface TimelineStreamOpts {
|
2023-02-15 13:26:27 -08:00
|
|
|
statContext?: IStatContext
|
2023-07-22 10:49:02 -07:00
|
|
|
enabled?: boolean
|
2022-12-06 22:45:10 -08:00
|
|
|
}
|
|
|
|
|
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,
|
2023-07-22 10:49:02 -07:00
|
|
|
opts?: TimelineStreamOpts,
|
2022-06-18 11:58:42 -07:00
|
|
|
) => connectStream(path, pollingRefresh, (dispatch: AppDispatch, getState: () => RootState) => {
|
|
|
|
const locale = getLocale(getState());
|
|
|
|
|
|
|
|
return {
|
|
|
|
onConnect() {
|
|
|
|
dispatch(connectTimeline(timelineId));
|
|
|
|
},
|
|
|
|
|
|
|
|
onDisconnect() {
|
|
|
|
dispatch(disconnectTimeline(timelineId));
|
|
|
|
},
|
|
|
|
|
2023-04-02 17:54:08 -07:00
|
|
|
onReceive(websocket, data: any) {
|
2022-06-18 11:58:42 -07:00
|
|
|
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 => {
|
2022-11-11 04:22:21 -08:00
|
|
|
dispatch(
|
|
|
|
updateNotificationsQueue(
|
|
|
|
JSON.parse(data.payload),
|
|
|
|
messages,
|
|
|
|
locale,
|
|
|
|
window.location.pathname,
|
|
|
|
),
|
|
|
|
);
|
2022-06-18 11:58:42 -07:00
|
|
|
}).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-11-09 10:17:10 -08:00
|
|
|
dispatch((_dispatch: AppDispatch, getState: () => RootState) => {
|
2022-09-29 08:58:58 -07:00
|
|
|
const chat = JSON.parse(data.payload);
|
|
|
|
const me = getState().me;
|
|
|
|
const messageOwned = chat.last_message?.account_id === me;
|
2022-12-06 15:18:47 -08:00
|
|
|
const settings = getSettings(getState());
|
2022-09-29 08:58:58 -07:00
|
|
|
|
|
|
|
// Don't update own messages from streaming
|
|
|
|
if (!messageOwned) {
|
2022-12-02 12:50:54 -08:00
|
|
|
updateChatListItem(chat);
|
2022-12-06 15:18:47 -08:00
|
|
|
|
|
|
|
if (settings.getIn(['chats', 'sound'])) {
|
|
|
|
play(soundCache.chat);
|
|
|
|
}
|
2022-12-06 22:45:10 -08:00
|
|
|
|
|
|
|
// Increment unread counter
|
|
|
|
opts?.statContext?.setUnreadChatsCount(getUnreadChatsCount());
|
2022-09-29 08:58:58 -07:00
|
|
|
}
|
|
|
|
});
|
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-11-02 12:28:16 -07:00
|
|
|
case 'chat_message.read': // TruthSocial
|
2022-11-03 12:16:07 -07:00
|
|
|
dispatch((_dispatch: AppDispatch, getState: () => RootState) => {
|
|
|
|
const chat = JSON.parse(data.payload);
|
|
|
|
const me = getState().me;
|
|
|
|
const isFromOtherUser = chat.account.id !== me;
|
|
|
|
if (isFromOtherUser) {
|
|
|
|
updateChatQuery(JSON.parse(data.payload));
|
|
|
|
}
|
|
|
|
});
|
2022-11-02 12:28:16 -07:00
|
|
|
break;
|
2023-02-08 09:58:01 -08:00
|
|
|
case 'chat_message.reaction': // TruthSocial
|
|
|
|
updateChatMessage(JSON.parse(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
|
|
|
}
|
|
|
|
},
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
2023-07-22 14:38:21 -07:00
|
|
|
function followStateToRelationship(followState: string) {
|
|
|
|
switch (followState) {
|
|
|
|
case 'follow_pending':
|
|
|
|
return { following: false, requested: true };
|
|
|
|
case 'follow_accept':
|
|
|
|
return { following: true, requested: false };
|
|
|
|
case 'follow_reject':
|
|
|
|
return { following: false, requested: false };
|
|
|
|
default:
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
interface FollowUpdate {
|
|
|
|
state: 'follow_pending' | 'follow_accept' | 'follow_reject'
|
|
|
|
follower: {
|
|
|
|
id: string
|
|
|
|
follower_count: number
|
|
|
|
following_count: number
|
|
|
|
}
|
|
|
|
following: {
|
|
|
|
id: string
|
|
|
|
follower_count: number
|
|
|
|
following_count: number
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function updateFollowRelationships(update: FollowUpdate) {
|
|
|
|
return (dispatch: AppDispatch, getState: () => RootState) => {
|
|
|
|
const me = getState().me;
|
|
|
|
const relationship = selectEntity<Relationship>(getState(), Entities.RELATIONSHIPS, update.following.id);
|
|
|
|
|
|
|
|
if (update.follower.id === me && relationship) {
|
|
|
|
const updated = {
|
|
|
|
...relationship,
|
|
|
|
...followStateToRelationship(update.state),
|
|
|
|
};
|
|
|
|
|
|
|
|
// Add a small delay to deal with API race conditions.
|
|
|
|
setTimeout(() => dispatch(importEntities([updated], Entities.RELATIONSHIPS)), 300);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2022-06-18 11:58:42 -07:00
|
|
|
export {
|
|
|
|
STREAMING_CHAT_UPDATE,
|
|
|
|
connectTimelineStream,
|
2023-07-22 10:49:02 -07:00
|
|
|
type TimelineStreamOpts,
|
2022-06-18 11:58:42 -07:00
|
|
|
};
|