Skip to content

Commit d765549

Browse files
committed
fix: 🐛 Emit user presence when accepting a friend request or joining a server.
1 parent 19f4a63 commit d765549

File tree

3 files changed

+18
-0
lines changed

3 files changed

+18
-0
lines changed

src/emits/User.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ export const emitUserPresenceUpdate = (userId: string, presence: Presence, socke
1313
});
1414
};
1515

16+
export const emitUserPresenceUpdateTo = (to: string | string[], presence: Presence) => {
17+
getIO().to(to).emit(USER_PRESENCE_UPDATE, presence);
18+
};
19+
1620

1721
export const emitInboxOpened = (userId: string, inbox: Inbox) => {
1822
getIO().to(userId).emit(INBOX_OPENED, inbox);

src/services/Friend.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1+
import { getUserPresences } from '../cache/UserCache';
12
import { exists, prisma } from '../common/database';
23
import { generateError } from '../common/errorHandler';
34
import { generateId } from '../common/flakeId';
45
import { emitFriendRemoved, emitFriendRequestAccept, emitFriendRequestSent } from '../emits/Friend';
6+
import { emitUserPresenceUpdateTo } from '../emits/User';
57
import { FriendStatus } from '../types/Friend';
68

79

@@ -84,6 +86,12 @@ export const acceptFriend = async (userId: string, friendId: string) => {
8486

8587

8688
emitFriendRequestAccept(userId, friendId);
89+
90+
const [userPresence, friendPresence] = await getUserPresences([userId, friendId]);
91+
userPresence && emitUserPresenceUpdateTo(userId, friendPresence);
92+
friendPresence && emitUserPresenceUpdateTo(friendId, userPresence);
93+
94+
8795
return [{message: 'Accepted!'}, null];
8896

8997
};

src/services/Server.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import { emitServerJoined, emitServerLeft, emitServerUpdated } from '../emits/Se
1111
import { ChannelType } from '../types/Channel';
1212
import { createMessage } from './Message';
1313
import { MessageType } from '../types/Message';
14+
import { emitUserPresenceUpdateTo } from '../emits/User';
1415

1516
interface CreateServerOptions {
1617
name: string;
@@ -172,6 +173,11 @@ export const joinServer = async (userId: string, serverId: string): Promise<Cust
172173
memberPresences,
173174
});
174175

176+
177+
const [userPresence] = await getUserPresences([userId]);
178+
userPresence && emitUserPresenceUpdateTo(serverId, userPresence);
179+
180+
175181
return [server, null];
176182
};
177183

0 commit comments

Comments
 (0)