@@ -4,6 +4,11 @@ import { MessageModel } from "@in/server/db/models/messages"
44import type { FunctionContext } from "@in/server/functions/_types"
55import { Updates } from "@in/server/modules/updates/updates"
66import { Encoders } from "@in/server/realtime/encoders/encoders"
7+ import type { UpdateGroup } from "../modules/updates"
8+ import { getUpdateGroupFromInputPeer } from "../modules/updates"
9+ import { RealtimeUpdates } from "../realtime/message"
10+ import { connectionManager } from "../ws/connections"
11+ import { Log } from "../utils/log"
712
813type Input = {
914 messageIds : bigint [ ]
@@ -18,22 +23,95 @@ export const deleteMessage = async (input: Input, context: FunctionContext): Pro
1823 const chatId = await ChatModel . getChatIdFromInputPeer ( input . peer , context )
1924 await MessageModel . deleteMessages ( input . messageIds , chatId )
2025
21- const encodingForInputPeer : InputPeer =
22- input . peer . type . oneofKind === "user" && BigInt ( context . currentUserId ) === input . peer . type . user . userId
23- ? input . peer
24- : { type : { oneofKind : "user" , user : { userId : BigInt ( context . currentUserId ) } } }
25-
26- const update : Update = {
27- update : {
28- oneofKind : "deleteMessages" ,
29- deleteMessages : {
30- messageIds : input . messageIds . map ( ( id ) => BigInt ( id ) ) ,
31- peerId : Encoders . peerFromInputPeer ( { inputPeer : encodingForInputPeer , currentUserId : context . currentUserId } ) ,
32- } ,
33- } ,
34- }
26+ const { selfUpdates, updateGroup } = await pushUpdates ( {
27+ inputPeer : input . peer ,
28+ messageIds : input . messageIds ,
29+ currentUserId : context . currentUserId ,
30+ } )
31+
32+ return { updates : selfUpdates }
33+ }
34+
35+ // ------------------------------------------------------------
36+ // Updates
37+ // ------------------------------------------------------------
38+
39+ /** Push updates for delete messages */
40+ const pushUpdates = async ( {
41+ inputPeer,
42+ messageIds,
43+ currentUserId,
44+ } : {
45+ inputPeer : InputPeer
46+ messageIds : bigint [ ]
47+ currentUserId : number
48+ } ) : Promise < { selfUpdates : Update [ ] ; updateGroup : UpdateGroup } > => {
49+ const updateGroup = await getUpdateGroupFromInputPeer ( inputPeer , { currentUserId } )
3550
36- Updates . shared . pushUpdate ( [ update ] , { peerId : input . peer , currentUserId : context . currentUserId } )
51+ let selfUpdates : Update [ ] = [ ]
52+
53+ if ( updateGroup . type === "users" ) {
54+ updateGroup . userIds . forEach ( ( userId ) => {
55+ const encodingForUserId = userId
56+ const encodingForInputPeer : InputPeer =
57+ userId === currentUserId ? inputPeer : { type : { oneofKind : "user" , user : { userId : BigInt ( currentUserId ) } } }
58+
59+ let newMessageUpdate : Update = {
60+ update : {
61+ oneofKind : "deleteMessages" ,
62+ deleteMessages : {
63+ messageIds : messageIds . map ( ( id ) => BigInt ( id ) ) ,
64+ peerId : Encoders . peerFromInputPeer ( { inputPeer : encodingForInputPeer , currentUserId } ) ,
65+ } ,
66+ } ,
67+ }
68+
69+ if ( userId === currentUserId ) {
70+ // current user gets the message id update and new message update
71+ RealtimeUpdates . pushToUser ( userId , [
72+ // order matters here
73+ newMessageUpdate ,
74+ ] )
75+ selfUpdates = [
76+ // order matters here
77+ newMessageUpdate ,
78+ ]
79+ } else {
80+ // other users get the message only
81+ RealtimeUpdates . pushToUser ( userId , [ newMessageUpdate ] )
82+ }
83+ } )
84+ } else if ( updateGroup . type === "space" ) {
85+ const userIds = connectionManager . getSpaceUserIds ( updateGroup . spaceId )
86+ Log . shared . debug ( `Sending message to space ${ updateGroup . spaceId } ` , { userIds } )
87+ userIds . forEach ( ( userId ) => {
88+ // New updates
89+ let newMessageUpdate : Update = {
90+ update : {
91+ oneofKind : "deleteMessages" ,
92+ deleteMessages : {
93+ messageIds : messageIds . map ( ( id ) => BigInt ( id ) ) ,
94+ peerId : Encoders . peerFromInputPeer ( { inputPeer, currentUserId } ) ,
95+ } ,
96+ } ,
97+ }
98+
99+ if ( userId === currentUserId ) {
100+ // current user gets the message id update and new message update
101+ RealtimeUpdates . pushToUser ( userId , [
102+ // order matters here
103+ newMessageUpdate ,
104+ ] )
105+ selfUpdates = [
106+ // order matters here
107+ newMessageUpdate ,
108+ ]
109+ } else {
110+ // other users get the message only
111+ RealtimeUpdates . pushToUser ( userId , [ newMessageUpdate ] )
112+ }
113+ } )
114+ }
37115
38- return { updates : [ update ] }
116+ return { selfUpdates , updateGroup }
39117}
0 commit comments