-
Notifications
You must be signed in to change notification settings - Fork 13.5k
Expand file tree
/
Copy pathconfigureServer.ts
More file actions
140 lines (116 loc) · 3.99 KB
/
configureServer.ts
File metadata and controls
140 lines (116 loc) · 3.99 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
import { EventEmitter } from 'events';
import { Account, Presence, MeteorService, MeteorError } from '@rocket.chat/core-services';
import { UserStatus } from '@rocket.chat/core-typings';
import { Server } from './Server';
import { DDP_EVENTS, WS_ERRORS } from './constants';
import { Autoupdate } from './lib/Autoupdate';
export const server = new Server();
export const events = new EventEmitter();
const loginServiceConfigurationCollection = 'meteor_accounts_loginServiceConfiguration';
const loginServiceConfigurationPublication = 'meteor.loginServiceConfiguration';
const loginServices = new Map<string, any>();
MeteorService.getLoginServiceConfiguration()
.then((records = []) => records.forEach((record) => loginServices.set(record._id, record)))
.catch((err) => console.error('DDPStreamer not able to retrieve login services configuration', err));
server.publish(loginServiceConfigurationPublication, async function () {
loginServices.forEach((record) => this.added(loginServiceConfigurationCollection, record._id, record));
const fn = (action: string, record: any): void => {
switch (action) {
case 'added':
case 'changed':
loginServices.set(record._id, record);
this[action](loginServiceConfigurationCollection, record._id, record);
break;
case 'removed':
loginServices.delete(record._id);
this[action](loginServiceConfigurationCollection, record._id);
}
};
events.on(loginServiceConfigurationPublication, fn);
this.onStop(() => {
events.removeListener(loginServiceConfigurationPublication, fn);
});
this.ready();
});
const autoUpdateCollection = 'meteor_autoupdate_clientVersions';
server.publish(autoUpdateCollection, function () {
Autoupdate.getVersions().forEach((version, arch) => {
this.added(autoUpdateCollection, arch, version);
});
const fn = (record: any): void => {
const { _id, ...version } = record;
this.changed(autoUpdateCollection, _id, version);
};
Autoupdate.on('update', fn);
this.onStop(() => {
Autoupdate.removeListener('update', fn);
});
this.ready();
});
server.methods({
async 'login'({ resume, user, password }: { resume: string; user: { username: string }; password: string }) {
try {
const result = await Account.login({ resume, user, password });
if (!result) {
throw new MeteorError(403, "You've been logged out by the server. Please log in again");
}
this.userId = result.uid;
this.userToken = result.hashedToken;
this.connection.loginToken = result.hashedToken;
this.emit(DDP_EVENTS.LOGGED);
server.emit(DDP_EVENTS.LOGGED, this);
return {
id: result.uid,
token: result.token,
tokenExpires: result.tokenExpires,
type: result.type,
};
} catch (error) {
throw error;
}
},
async 'logout'() {
if (this.userToken && this.userId) {
await Account.logout({ userId: this.userId, token: this.userToken });
}
this.emit(DDP_EVENTS.LOGGEDOUT);
server.emit(DDP_EVENTS.LOGGEDOUT, this);
this.userToken = undefined;
this.userId = undefined;
// Close connection after return success to the method call.
// This ensures all the subscriptions will be closed, meteor makes it manually
// here https://github.com/meteor/meteor/blob/2377ebe879d9b965d699f599392d4e8047eb7d78/packages/ddp-server/livedata_server.js#L781
// re doing the default subscriptions.
setTimeout(() => {
this.ws.close(WS_ERRORS.CLOSE_PROTOCOL_ERROR);
}, 1);
},
'UserPresence:setDefaultStatus'(status) {
const { userId } = this;
if (!userId) {
return;
}
return Presence.setStatus(userId, status);
},
'UserPresence:online'() {
const { userId, session } = this;
if (!userId) {
return;
}
return Presence.setConnectionStatus(userId, UserStatus.ONLINE, session);
},
'UserPresence:away'() {
const { userId, session } = this;
if (!userId) {
return;
}
return Presence.setConnectionStatus(userId, UserStatus.AWAY, session);
},
'setUserStatus'(status, statusText) {
const { userId } = this;
if (!userId) {
return;
}
return Presence.setStatus(userId, status, statusText);
},
});