Skip to content

Commit c7d7bb6

Browse files
committed
Add hooks on monolith
1 parent 29dc89f commit c7d7bb6

File tree

8 files changed

+69
-19
lines changed

8 files changed

+69
-19
lines changed

app/statistics/server/lib/SAUMonitor.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,12 +71,12 @@ export class SAUMonitorClass {
7171
return;
7272
}
7373

74-
sauEvents.on('socket.disconnected', async ({ connection }) => {
74+
sauEvents.on('socket.disconnected', async ({ id, instanceId }) => {
7575
if (!this.isRunning()) {
7676
return;
7777
}
7878

79-
await Sessions.closeByInstanceIdAndSessionId(connection.instanceId, connection.id);
79+
await Sessions.closeByInstanceIdAndSessionId(instanceId, id);
8080
});
8181
}
8282

@@ -89,7 +89,6 @@ export class SAUMonitorClass {
8989
if (!this.isRunning()) {
9090
return;
9191
}
92-
console.log('accounts.login', userId, connection);
9392

9493
// TODO need to perform a find on user to get his roles
9594
// const { roles = ['user'], _id: userId } = info.user;
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
declare module 'meteor/konecty:multiple-instances-status' {
2+
namespace InstanceStatus {
3+
function id(): string;
4+
}
5+
}

ee/server/services/ddp-streamer/configureServer.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -155,27 +155,27 @@ server.methods({
155155

156156
server.on(DDP_EVENTS.LOGGED, (info) => {
157157
// console.log('DDP_EVENTS.LOGGED ->', info);
158-
const { userId, session, connection } = info;
159-
Presence.newConnection(userId, session);
160-
api.broadcast('accounts.login', { userId, session, connection });
158+
const { userId, connection } = info;
159+
Presence.newConnection(userId, connection.id);
160+
api.broadcast('accounts.login', { userId, connection });
161161
});
162162

163163
server.on(DDP_EVENTS.LOGGEDOUT, (info) => {
164164
// console.log('DDP_EVENTS.LOGGEDOUT ->', info);
165-
const { userId, session, connection } = info;
166-
api.broadcast('accounts.logout', { userId, session, connection });
165+
const { userId, connection } = info;
166+
api.broadcast('accounts.logout', { userId, connection });
167167
});
168168

169169
server.on(DDP_EVENTS.DISCONNECTED, (info) => {
170-
const { userId, session, connection } = info;
170+
const { userId, connection } = info;
171171

172172
// console.log('DDP_EVENTS.DISCONNECTED ->', info);
173-
api.broadcast('socket.disconnected', { userId, session, connection });
173+
api.broadcast('socket.disconnected', connection);
174174

175175
if (!userId) {
176176
return;
177177
}
178-
Presence.removeConnection(userId, session);
178+
Presence.removeConnection(userId, connection.id);
179179
});
180180

181181
server.on(DDP_EVENTS.CONNECTED, ({ connection }) => {

server/hooks/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
import './sauMonitorHooks';

server/hooks/sauMonitorHooks.ts

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import type { IncomingHttpHeaders } from 'http';
2+
3+
import { InstanceStatus } from 'meteor/konecty:multiple-instances-status';
4+
import { Accounts } from 'meteor/accounts-base';
5+
import { Meteor } from 'meteor/meteor';
6+
7+
import { sauEvents } from '../services/sauMonitor/events';
8+
9+
Accounts.onLogin((info: { user: Meteor.User; connection: Meteor.Connection }) => {
10+
const { httpHeaders } = info.connection;
11+
12+
sauEvents.emit('accounts.login', {
13+
userId: info.user._id,
14+
connection: { instanceId: InstanceStatus.id(), ...info.connection, httpHeaders: httpHeaders as IncomingHttpHeaders },
15+
});
16+
});
17+
18+
Accounts.onLogout((info: { user: Meteor.User; connection: Meteor.Connection }) => {
19+
const { httpHeaders } = info.connection;
20+
21+
sauEvents.emit('accounts.logout', {
22+
userId: info.user._id,
23+
connection: { instanceId: InstanceStatus.id(), ...info.connection, httpHeaders: httpHeaders as IncomingHttpHeaders },
24+
});
25+
});
26+
27+
Meteor.onConnection((connection) => {
28+
connection.onClose(async () => {
29+
const { httpHeaders } = connection;
30+
sauEvents.emit('socket.disconnected', {
31+
instanceId: InstanceStatus.id(),
32+
...connection,
33+
httpHeaders: httpHeaders as IncomingHttpHeaders,
34+
});
35+
});
36+
});
37+
38+
Meteor.onConnection((connection) => {
39+
const { httpHeaders } = connection;
40+
41+
sauEvents.emit('socket.connected', { instanceId: InstanceStatus.id(), ...connection, httpHeaders: httpHeaders as IncomingHttpHeaders });
42+
});

server/sdk/lib/Events.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,10 @@ import { ISocketConnection } from '../../../definition/ISocketConnection';
2020
type ClientAction = 'inserted' | 'updated' | 'removed' | 'changed';
2121

2222
export type EventSignatures = {
23-
'accounts.login': (info: { userId: string; session: string; connection: ISocketConnection }) => void;
24-
'accounts.logout': (info: { userId: string; session: string; connection: ISocketConnection }) => void;
25-
'socket.connected': (info: any) => void;
26-
'socket.disconnected': (info: any) => void;
23+
'accounts.login': (info: { userId: string; connection: ISocketConnection }) => void;
24+
'accounts.logout': (info: { userId: string; connection: ISocketConnection }) => void;
25+
'socket.connected': (connection: ISocketConnection) => void;
26+
'socket.disconnected': (connection: ISocketConnection) => void;
2727
'banner.new'(bannerId: string): void;
2828
'banner.enabled'(bannerId: string): void;
2929
'banner.disabled'(bannerId: string): void;
Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
import { Emitter } from '@rocket.chat/emitter';
22

3+
import { ISocketConnection } from '../../../definition/ISocketConnection';
4+
35
export const sauEvents = new Emitter<{
4-
'accounts.login': any;
5-
'accounts.logout': any;
6-
'socket.connected': any;
7-
'socket.disconnected': any;
6+
'accounts.login': { userId: string; connection: ISocketConnection };
7+
'accounts.logout': { userId: string; connection: ISocketConnection };
8+
'socket.connected': ISocketConnection;
9+
'socket.disconnected': ISocketConnection;
810
}>();

server/startup/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,4 @@ import './instance';
77
import './presence';
88
import './serverRunning';
99
import './coreApps';
10+
import '../hooks';

0 commit comments

Comments
 (0)