Skip to content

Commit 3c26ca0

Browse files
committed
fix: d
1 parent 8a6cae6 commit 3c26ca0

File tree

8 files changed

+86
-3
lines changed

8 files changed

+86
-3
lines changed

ee/apps/ddp-streamer/src/DDPStreamer.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,11 @@ export class DDPStreamer extends ServiceClass {
215215
server.on(DDP_EVENTS.CONNECTED, ({ connection }) => {
216216
this.api?.broadcast('socket.connected', connection);
217217
});
218+
219+
server.on(DDP_EVENTS.PING, (client: Client): void => {
220+
const { connection } = client;
221+
Presence.renewConnection(connection.id);
222+
});
218223
}
219224

220225
async started(): Promise<void> {

ee/packages/presence/src/Presence.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@ export class Presence extends ServiceClass implements IPresence {
128128
id: session,
129129
instanceId: nodeId,
130130
status: UserStatus.ONLINE,
131+
expiresAt: new Date(Date.now() + 5 * 60 * 1000),
131132
});
132133

133134
await this.updateUserPresence(uid);
@@ -180,6 +181,13 @@ export class Presence extends ServiceClass implements IPresence {
180181
return affectedUsers.map(({ _id }) => _id);
181182
}
182183

184+
async renewConnection(connectionId: string): Promise<void> {
185+
const result = await UsersSessions.renewConnectionByConnectionId(connectionId, new Date(Date.now() + 5 * 60 * 1000));
186+
if (result.modifiedCount === 0) {
187+
throw new Error('Connection not found');
188+
}
189+
}
190+
183191
async setStatus(uid: string, statusDefault: UserStatus, statusText?: string): Promise<boolean> {
184192
const userSessions = (await UsersSessions.findOneById(uid)) || { connections: [] };
185193

ee/packages/presence/src/lib/processConnectionStatus.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,10 @@ export const processPresenceAndStatus = (
3636
userSessions: IUserSessionConnection[] = [],
3737
statusDefault = UserStatus.ONLINE,
3838
): { status: UserStatus; statusConnection: UserStatus } => {
39-
const statusConnection = userSessions.map((s) => s.status).reduce(processConnectionStatus, UserStatus.OFFLINE);
39+
const statusConnection = userSessions
40+
.filter((s) => s.expiresAt > new Date())
41+
.map((s) => s.status)
42+
.reduce(processConnectionStatus, UserStatus.OFFLINE);
4043

4144
const status = processStatus(statusConnection, statusDefault);
4245

ee/packages/presence/tests/lib/processConnectionStatus.test.ts

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ describe('Presence micro service', () => {
4949
id: 'random',
5050
instanceId: 'random',
5151
status: UserStatus.ONLINE,
52+
expiresAt: new Date(Date.now() + 1000 * 60),
5253
_createdAt: new Date(),
5354
_updatedAt: new Date(),
5455
},
@@ -64,6 +65,7 @@ describe('Presence micro service', () => {
6465
id: 'random',
6566
instanceId: 'random',
6667
status: UserStatus.AWAY,
68+
expiresAt: new Date(Date.now() + 1000 * 60),
6769
_createdAt: new Date(),
6870
_updatedAt: new Date(),
6971
},
@@ -79,6 +81,7 @@ describe('Presence micro service', () => {
7981
id: 'random',
8082
instanceId: 'random',
8183
status: UserStatus.ONLINE,
84+
expiresAt: new Date(Date.now() + 1000 * 60),
8285
_createdAt: new Date(),
8386
_updatedAt: new Date(),
8487
},
@@ -94,6 +97,7 @@ describe('Presence micro service', () => {
9497
id: 'random',
9598
instanceId: 'random',
9699
status: UserStatus.ONLINE,
100+
expiresAt: new Date(Date.now() + 1000 * 60),
97101
_createdAt: new Date(),
98102
_updatedAt: new Date(),
99103
},
@@ -109,6 +113,7 @@ describe('Presence micro service', () => {
109113
id: 'random',
110114
instanceId: 'random',
111115
status: UserStatus.AWAY,
116+
expiresAt: new Date(Date.now() + 1000 * 60),
112117
_createdAt: new Date(),
113118
_updatedAt: new Date(),
114119
},
@@ -124,6 +129,7 @@ describe('Presence micro service', () => {
124129
id: 'random',
125130
instanceId: 'random',
126131
status: UserStatus.ONLINE,
132+
expiresAt: new Date(Date.now() + 1000 * 60),
127133
_createdAt: new Date(),
128134
_updatedAt: new Date(),
129135
},
@@ -139,6 +145,7 @@ describe('Presence micro service', () => {
139145
id: 'random',
140146
instanceId: 'random',
141147
status: UserStatus.AWAY,
148+
expiresAt: new Date(Date.now() + 1000 * 60),
142149
_createdAt: new Date(),
143150
_updatedAt: new Date(),
144151
},
@@ -156,13 +163,15 @@ describe('Presence micro service', () => {
156163
id: 'random',
157164
instanceId: 'random',
158165
status: UserStatus.ONLINE,
166+
expiresAt: new Date(Date.now() + 1000 * 60),
159167
_createdAt: new Date(),
160168
_updatedAt: new Date(),
161169
},
162170
{
163171
id: 'random',
164172
instanceId: 'random',
165173
status: UserStatus.AWAY,
174+
expiresAt: new Date(Date.now() + 1000 * 60),
166175
_createdAt: new Date(),
167176
_updatedAt: new Date(),
168177
},
@@ -178,13 +187,15 @@ describe('Presence micro service', () => {
178187
id: 'random',
179188
instanceId: 'random',
180189
status: UserStatus.AWAY,
190+
expiresAt: new Date(Date.now() + 1000 * 60),
181191
_createdAt: new Date(),
182192
_updatedAt: new Date(),
183193
},
184194
{
185195
id: 'random',
186196
instanceId: 'random',
187197
status: UserStatus.ONLINE,
198+
expiresAt: new Date(Date.now() + 1000 * 60),
188199
_createdAt: new Date(),
189200
_updatedAt: new Date(),
190201
},
@@ -200,13 +211,15 @@ describe('Presence micro service', () => {
200211
id: 'random',
201212
instanceId: 'random',
202213
status: UserStatus.AWAY,
214+
expiresAt: new Date(Date.now() + 1000 * 60),
203215
_createdAt: new Date(),
204216
_updatedAt: new Date(),
205217
},
206218
{
207219
id: 'random',
208220
instanceId: 'random',
209221
status: UserStatus.AWAY,
222+
expiresAt: new Date(Date.now() + 1000 * 60),
210223
_createdAt: new Date(),
211224
_updatedAt: new Date(),
212225
},
@@ -237,4 +250,38 @@ describe('Presence micro service', () => {
237250
statusConnection: UserStatus.OFFLINE,
238251
});
239252
});
253+
254+
test('should return correct status and statusConnection when there are expired connections', () => {
255+
expect(
256+
processPresenceAndStatus(
257+
[
258+
{
259+
id: 'random',
260+
instanceId: 'random',
261+
status: UserStatus.ONLINE,
262+
expiresAt: new Date(Date.now() - 1000 * 60),
263+
_createdAt: new Date(),
264+
_updatedAt: new Date(),
265+
},
266+
],
267+
UserStatus.ONLINE,
268+
),
269+
).toStrictEqual({ status: UserStatus.OFFLINE, statusConnection: UserStatus.OFFLINE });
270+
271+
expect(
272+
processPresenceAndStatus(
273+
[
274+
{
275+
id: 'random',
276+
instanceId: 'random',
277+
status: UserStatus.AWAY,
278+
expiresAt: new Date(Date.now() - 1000 * 60),
279+
_createdAt: new Date(),
280+
_updatedAt: new Date(),
281+
},
282+
],
283+
UserStatus.ONLINE,
284+
),
285+
).toStrictEqual({ status: UserStatus.OFFLINE, statusConnection: UserStatus.OFFLINE });
286+
});
240287
});

packages/core-services/src/types/IPresence.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ export interface IPresence extends IServiceClass {
1313
session: string | undefined,
1414
nodeId: string,
1515
): Promise<{ uid: string; session: string } | undefined>;
16+
renewConnection(connectionId: string): Promise<void>;
1617
removeLostConnections(nodeID: string): Promise<string[]>;
1718
setStatus(uid: string, status: UserStatus, statusText?: string): Promise<boolean>;
1819
setConnectionStatus(uid: string, status: UserStatus, session: string): Promise<boolean>;

packages/core-typings/src/IUserSession.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ export interface IUserSessionConnection {
44
id: string;
55
instanceId: string;
66
status: UserStatus;
7+
expiresAt: Date;
78
_createdAt: Date;
89
_updatedAt: Date;
910
}

packages/model-typings/src/models/IUsersSessionsModel.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,11 @@ export interface IUsersSessionsModel extends IBaseModel<IUserSession> {
88
updateConnectionStatusById(uid: string, connectionId: string, status: string): ReturnType<IBaseModel<IUserSession>['updateOne']>;
99
removeConnectionsFromInstanceId(instanceId: string): ReturnType<IBaseModel<IUserSession>['updateMany']>;
1010
removeConnectionByConnectionId(connectionId: string): ReturnType<IBaseModel<IUserSession>['updateMany']>;
11+
renewConnectionByConnectionId(connectionId: string, expiresAt: Date): ReturnType<IBaseModel<IUserSession>['updateMany']>;
1112
findByInstanceId(instanceId: string): FindCursor<IUserSession>;
1213
addConnectionById(
1314
userId: string,
14-
{ id, instanceId, status }: Pick<IUserSessionConnection, 'id' | 'instanceId' | 'status'>,
15+
{ id, instanceId, status, expiresAt }: Pick<IUserSessionConnection, 'id' | 'instanceId' | 'status' | 'expiresAt'>,
1516
): ReturnType<IBaseModel<IUserSession>['updateOne']>;
1617
findByOtherInstanceIds(instanceIds: string[], options?: FindOptions<IUserSession>): FindCursor<IUserSession>;
1718
removeConnectionsFromOtherInstanceIds(instanceIds: string[]): ReturnType<IBaseModel<IUserSession>['updateMany']>;

packages/models/src/models/UsersSessions.ts

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,22 @@ export class UsersSessionsRaw extends BaseRaw<IUserSession> implements IUsersSes
9090
);
9191
}
9292

93+
async renewConnectionByConnectionId(connectionId: string, expiresAt: Date): ReturnType<BaseRaw<IUserSession>['updateMany']> {
94+
const result = await this.updateMany(
95+
{
96+
'connections.id': connectionId,
97+
},
98+
{
99+
$set: {
100+
'connections.$.expiresAt': expiresAt,
101+
'connections.$._updatedAt': new Date(),
102+
},
103+
},
104+
);
105+
106+
return result;
107+
}
108+
93109
findByInstanceId(instanceId: string): FindCursor<IUserSession> {
94110
return this.find({
95111
'connections.instanceId': instanceId,
@@ -98,7 +114,7 @@ export class UsersSessionsRaw extends BaseRaw<IUserSession> implements IUsersSes
98114

99115
addConnectionById(
100116
userId: string,
101-
{ id, instanceId, status }: Pick<IUserSessionConnection, 'id' | 'instanceId' | 'status'>,
117+
{ id, instanceId, status, expiresAt }: Pick<IUserSessionConnection, 'id' | 'instanceId' | 'status' | 'expiresAt'>,
102118
): ReturnType<BaseRaw<IUserSession>['updateOne']> {
103119
const now = new Date();
104120

@@ -108,6 +124,7 @@ export class UsersSessionsRaw extends BaseRaw<IUserSession> implements IUsersSes
108124
id,
109125
instanceId,
110126
status,
127+
expiresAt,
111128
_createdAt: now,
112129
_updatedAt: now,
113130
},

0 commit comments

Comments
 (0)