-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathLiveLeaveServerCommand.ts
More file actions
149 lines (132 loc) · 4.57 KB
/
LiveLeaveServerCommand.ts
File metadata and controls
149 lines (132 loc) · 4.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
/**
* Live Leave Command - Server Implementation
*
* Removes user from call. Ends call if last participant.
*/
import { DATA_COMMANDS } from '@commands/data/shared/DataCommandConstants';
import { transformPayload } from '@system/core/types/JTAGTypes';
import { LiveLeaveCommand } from '../shared/LiveLeaveCommand';
import type { LiveLeaveParams, LiveLeaveResult } from '../shared/LiveLeaveTypes';
import { CallEntity } from '@system/data/entities/CallEntity';
import { UserEntity } from '@system/data/entities/UserEntity';
import type { UUID } from '@system/core/types/CrossPlatformUUID';
import { Commands } from '@system/core/shared/Commands';
import { Events } from '@system/core/shared/Events';
import type { DataListParams, DataListResult } from '@commands/data/list/shared/DataListTypes';
import type { DataUpdateParams, DataUpdateResult } from '@commands/data/update/shared/DataUpdateTypes';
import { getVoiceOrchestrator } from '@system/voice/server';
import { DataList } from '../../../../data/list/shared/DataListTypes';
import { DataUpdate } from '../../../../data/update/shared/DataUpdateTypes';
export class LiveLeaveServerCommand extends LiveLeaveCommand {
protected async executeLeave(params: LiveLeaveParams): Promise<LiveLeaveResult> {
// 1. Get current user from params.userId (auto-injected by infrastructure)
const user = await this.findUserById(params.userId, params);
if (!user) {
return transformPayload(params, {
success: false,
message: 'Could not identify current user',
sessionEnded: false,
remainingParticipants: 0
});
}
// 2. Find the call
const call = await this.findCall(params.sessionId, params);
if (!call) {
return transformPayload(params, {
success: false,
message: `Call not found: ${params.sessionId}`,
sessionEnded: false,
remainingParticipants: 0
});
}
// 3. Remove user from participants
const removed = call.removeParticipant(user.id);
if (!removed) {
return transformPayload(params, {
success: false,
message: 'User was not in call',
sessionEnded: false,
remainingParticipants: call.getActiveParticipants().length
});
}
// 4. Save updated call
await this.saveCall(call, params);
// 5. Emit leave event for other clients
Events.emit(`live:left:${call.id}`, {
sessionId: call.id,
userId: user.id
});
const remainingParticipants = call.getActiveParticipants().length;
const callEnded = call.status === 'ended';
// 6. Unregister voice session if call ended
if (callEnded) {
try {
getVoiceOrchestrator().unregisterSession(call.id);
} catch (error) {
console.warn('Failed to unregister voice session:', error);
}
}
return transformPayload(params, {
success: true,
message: callEnded
? 'Left call (call ended - no participants remaining)'
: `Left call (${remainingParticipants} remaining)`,
sessionEnded: callEnded,
remainingParticipants
});
}
/**
* Find user by ID from database
*/
private async findUserById(userId: UUID, params: LiveLeaveParams): Promise<UserEntity | null> {
const result = await DataList.execute<UserEntity>({
collection: UserEntity.collection,
filter: { id: userId },
limit: 1,
context: params.context,
sessionId: params.sessionId
});
if (result.success && result.items && result.items.length > 0) {
return result.items[0];
}
return null;
}
/**
* Find call by ID
*/
private async findCall(callId: string, params: LiveLeaveParams): Promise<CallEntity | null> {
const result = await DataList.execute<CallEntity>({
collection: CallEntity.collection,
filter: { id: callId },
limit: 1,
context: params.context,
sessionId: params.sessionId
}
);
if (result.success && result.items && result.items.length > 0) {
// Reconstruct entity with methods
const data = result.items[0];
const call = new CallEntity();
Object.assign(call, data);
return call;
}
return null;
}
/**
* Save updated call
*/
private async saveCall(call: CallEntity, params: LiveLeaveParams): Promise<void> {
await DataUpdate.execute<CallEntity>({
collection: CallEntity.collection,
id: call.id,
data: {
participants: call.participants,
status: call.status,
endedAt: call.endedAt
},
context: params.context,
sessionId: params.sessionId
}
);
}
}