@resolveio/server-lib 3.2.10 → 3.2.11

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (177) hide show
  1. package/package.json +26 -26
  2. package/collections/active-subscription.collection.d.ts +0 -7
  3. package/collections/active-subscription.collection.js +0 -68
  4. package/collections/app-status.collection.d.ts +0 -7
  5. package/collections/app-status.collection.js +0 -43
  6. package/collections/app-version.collection.d.ts +0 -7
  7. package/collections/app-version.collection.js +0 -56
  8. package/collections/billing-logged-in-users.collection.d.ts +0 -7
  9. package/collections/billing-logged-in-users.collection.js +0 -73
  10. package/collections/counter.collection.d.ts +0 -9
  11. package/collections/counter.collection.js +0 -73
  12. package/collections/cron-job-history.collection.d.ts +0 -7
  13. package/collections/cron-job-history.collection.js +0 -86
  14. package/collections/cron-job.collection.d.ts +0 -7
  15. package/collections/cron-job.collection.js +0 -89
  16. package/collections/file.collection.d.ts +0 -9
  17. package/collections/file.collection.js +0 -73
  18. package/collections/flag.collection.d.ts +0 -7
  19. package/collections/flag.collection.js +0 -47
  20. package/collections/log.collection.d.ts +0 -7
  21. package/collections/log.collection.js +0 -65
  22. package/collections/logged-in-users.collection.d.ts +0 -7
  23. package/collections/logged-in-users.collection.js +0 -70
  24. package/collections/method-call.collection.d.ts +0 -6
  25. package/collections/method-call.collection.js +0 -44
  26. package/collections/method-response.collection.d.ts +0 -6
  27. package/collections/method-response.collection.js +0 -48
  28. package/collections/notification.collection.d.ts +0 -7
  29. package/collections/notification.collection.js +0 -43
  30. package/collections/queue-flag.collection.d.ts +0 -7
  31. package/collections/queue-flag.collection.js +0 -41
  32. package/collections/queue-method.collection.d.ts +0 -7
  33. package/collections/queue-method.collection.js +0 -119
  34. package/collections/queue-response.collection.d.ts +0 -7
  35. package/collections/queue-response.collection.js +0 -74
  36. package/collections/support-ticket.collection.d.ts +0 -7
  37. package/collections/support-ticket.collection.js +0 -200
  38. package/collections/user.collection.d.ts +0 -9
  39. package/collections/user.collection.js +0 -153
  40. package/cron/cron.d.ts +0 -5
  41. package/cron/cron.js +0 -160
  42. package/fixtures/cron-jobs.d.ts +0 -1
  43. package/fixtures/cron-jobs.js +0 -90
  44. package/fixtures/init.d.ts +0 -1
  45. package/fixtures/init.js +0 -67
  46. package/http/auth.d.ts +0 -2
  47. package/http/auth.js +0 -468
  48. package/http/health.d.ts +0 -1
  49. package/http/health.js +0 -8
  50. package/index.d.ts +0 -16
  51. package/index.js +0 -125
  52. package/managers/cron.manager.d.ts +0 -16
  53. package/managers/cron.manager.js +0 -202
  54. package/managers/database.manager.d.ts +0 -6
  55. package/managers/database.manager.js +0 -12
  56. package/managers/method.manager.d.ts +0 -28
  57. package/managers/method.manager.js +0 -584
  58. package/managers/queue-method.manager.d.ts +0 -13
  59. package/managers/queue-method.manager.js +0 -240
  60. package/managers/subscription.manager.d.ts +0 -28
  61. package/managers/subscription.manager.js +0 -803
  62. package/managers/support.manager.d.ts +0 -22
  63. package/managers/support.manager.js +0 -320
  64. package/methods/accounts.d.ts +0 -2
  65. package/methods/accounts.js +0 -253
  66. package/methods/aws.d.ts +0 -2
  67. package/methods/aws.js +0 -387
  68. package/methods/cloudconvert.d.ts +0 -2
  69. package/methods/cloudconvert.js +0 -37
  70. package/methods/collections.d.ts +0 -2
  71. package/methods/collections.js +0 -750
  72. package/methods/counters.d.ts +0 -2
  73. package/methods/counters.js +0 -107
  74. package/methods/cron-jobs.d.ts +0 -2
  75. package/methods/cron-jobs.js +0 -132
  76. package/methods/flags.d.ts +0 -2
  77. package/methods/flags.js +0 -6
  78. package/methods/logs.d.ts +0 -2
  79. package/methods/logs.js +0 -29
  80. package/methods/pdf.d.ts +0 -2
  81. package/methods/pdf.js +0 -303
  82. package/models/active-subscription.model.d.ts +0 -13
  83. package/models/active-subscription.model.js +0 -2
  84. package/models/app-status.model.d.ts +0 -4
  85. package/models/app-status.model.js +0 -2
  86. package/models/app-version.model.d.ts +0 -4
  87. package/models/app-version.model.js +0 -2
  88. package/models/billing-logged-in-users.model.d.ts +0 -7
  89. package/models/billing-logged-in-users.model.js +0 -2
  90. package/models/collection-document.model.d.ts +0 -6
  91. package/models/collection-document.model.js +0 -2
  92. package/models/counter.model.d.ts +0 -5
  93. package/models/counter.model.js +0 -2
  94. package/models/cron-job-history.model.d.ts +0 -12
  95. package/models/cron-job-history.model.js +0 -2
  96. package/models/cron-job.model.d.ts +0 -13
  97. package/models/cron-job.model.js +0 -2
  98. package/models/dialog.model.d.ts +0 -23
  99. package/models/dialog.model.js +0 -2
  100. package/models/file.model.d.ts +0 -9
  101. package/models/file.model.js +0 -2
  102. package/models/flag.model.d.ts +0 -5
  103. package/models/flag.model.js +0 -2
  104. package/models/log.model.d.ts +0 -11
  105. package/models/log.model.js +0 -2
  106. package/models/logged-in-users.model.d.ts +0 -9
  107. package/models/logged-in-users.model.js +0 -2
  108. package/models/method-call.model.d.ts +0 -7
  109. package/models/method-call.model.js +0 -2
  110. package/models/method-response.model.d.ts +0 -8
  111. package/models/method-response.model.js +0 -2
  112. package/models/method.model.d.ts +0 -10
  113. package/models/method.model.js +0 -2
  114. package/models/notification.model.d.ts +0 -4
  115. package/models/notification.model.js +0 -2
  116. package/models/pagination.model.d.ts +0 -11
  117. package/models/pagination.model.js +0 -20
  118. package/models/permission.model.d.ts +0 -12
  119. package/models/permission.model.js +0 -2
  120. package/models/queue-method.model.d.ts +0 -10
  121. package/models/queue-method.model.js +0 -2
  122. package/models/queue-response.model.d.ts +0 -10
  123. package/models/queue-response.model.js +0 -2
  124. package/models/select-data-label.model.d.ts +0 -9
  125. package/models/select-data-label.model.js +0 -2
  126. package/models/server-response.model.d.ts +0 -5
  127. package/models/server-response.model.js +0 -2
  128. package/models/subscription.model.d.ts +0 -30
  129. package/models/subscription.model.js +0 -2
  130. package/models/support-method.model.d.ts +0 -9
  131. package/models/support-method.model.js +0 -2
  132. package/models/support-ticket.model.d.ts +0 -61
  133. package/models/support-ticket.model.js +0 -2
  134. package/models/user.model.d.ts +0 -14
  135. package/models/user.model.js +0 -2
  136. package/private/email-templates/enrollment.html +0 -12
  137. package/private/email-templates/forgot-password.html +0 -12
  138. package/private/email-templates/support-ticket-deleted.html +0 -40
  139. package/private/email-templates/support-ticket-modified.html +0 -71
  140. package/private/email-templates/support-ticket.html +0 -71
  141. package/private/images/ResolveIO.png +0 -0
  142. package/publications/app-status.d.ts +0 -2
  143. package/publications/app-status.js +0 -14
  144. package/publications/app-version.d.ts +0 -2
  145. package/publications/app-version.js +0 -14
  146. package/publications/billing-logged-in-users.d.ts +0 -1
  147. package/publications/billing-logged-in-users.js +0 -63
  148. package/publications/cron-jobs.d.ts +0 -2
  149. package/publications/cron-jobs.js +0 -14
  150. package/publications/files.d.ts +0 -2
  151. package/publications/files.js +0 -71
  152. package/publications/flags.d.ts +0 -2
  153. package/publications/flags.js +0 -23
  154. package/publications/logs.d.ts +0 -2
  155. package/publications/logs.js +0 -26
  156. package/publications/method-calls.d.ts +0 -2
  157. package/publications/method-calls.js +0 -14
  158. package/publications/method-responses.d.ts +0 -2
  159. package/publications/method-responses.js +0 -14
  160. package/publications/notifications.d.ts +0 -2
  161. package/publications/notifications.js +0 -14
  162. package/publications/super-admin.d.ts +0 -2
  163. package/publications/super-admin.js +0 -21
  164. package/publications/support-tickets.d.ts +0 -1
  165. package/publications/support-tickets.js +0 -200
  166. package/server-app.d.ts +0 -35
  167. package/server-app.js +0 -636
  168. package/support-methods/aws.d.ts +0 -2
  169. package/support-methods/aws.js +0 -276
  170. package/support-methods/collections.d.ts +0 -2
  171. package/support-methods/collections.js +0 -471
  172. package/support-methods/counters.d.ts +0 -2
  173. package/support-methods/counters.js +0 -189
  174. package/support-methods/support.d.ts +0 -2
  175. package/support-methods/support.js +0 -236
  176. package/util/common.d.ts +0 -14
  177. package/util/common.js +0 -341
@@ -1,803 +0,0 @@
1
- "use strict";
2
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
- return new (P || (P = Promise))(function (resolve, reject) {
4
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
5
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
6
- function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
7
- step((generator = generator.apply(thisArg, _arguments || [])).next());
8
- });
9
- };
10
- var __generator = (this && this.__generator) || function (thisArg, body) {
11
- var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
12
- return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
13
- function verb(n) { return function (v) { return step([n, v]); }; }
14
- function step(op) {
15
- if (f) throw new TypeError("Generator is already executing.");
16
- while (_) try {
17
- if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
18
- if (y = 0, t) op = [op[0] & 2, t.value];
19
- switch (op[0]) {
20
- case 0: case 1: t = op; break;
21
- case 4: _.label++; return { value: op[1], done: false };
22
- case 5: _.label++; y = op[1]; op = [0]; continue;
23
- case 7: op = _.ops.pop(); _.trys.pop(); continue;
24
- default:
25
- if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
26
- if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
27
- if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
28
- if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
29
- if (t[2]) _.ops.pop();
30
- _.trys.pop(); continue;
31
- }
32
- op = body.call(thisArg, _);
33
- } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
34
- if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
35
- }
36
- };
37
- Object.defineProperty(exports, "__esModule", { value: true });
38
- var MongoOplog = require("mongo-oplog");
39
- var logs_1 = require("../publications/logs");
40
- var app_status_1 = require("../publications/app-status");
41
- var app_version_1 = require("../publications/app-version");
42
- var mongoose_1 = require("mongoose");
43
- var files_1 = require("../publications/files");
44
- var super_admin_1 = require("../publications/super-admin");
45
- var active_subscription_collection_1 = require("../collections/active-subscription.collection");
46
- var logged_in_users_collection_1 = require("../collections/logged-in-users.collection");
47
- var cron_jobs_1 = require("../publications/cron-jobs");
48
- var flags_1 = require("../publications/flags");
49
- var method_calls_1 = require("../publications/method-calls");
50
- var method_responses_1 = require("../publications/method-responses");
51
- var support_tickets_1 = require("../publications/support-tickets");
52
- var index_1 = require("../index");
53
- var notifications_1 = require("../publications/notifications");
54
- var common_1 = require("../util/common");
55
- var billing_logged_in_users_collection_1 = require("../collections/billing-logged-in-users.collection");
56
- var moment = require("moment");
57
- var billing_logged_in_users_1 = require("../publications/billing-logged-in-users");
58
- var SubscriptionManager = /** @class */ (function () {
59
- function SubscriptionManager(mainServer, wss, serverConfig) {
60
- var _this = this;
61
- this._publications = {};
62
- this._subscriptions = [];
63
- this._oplogQueue = [];
64
- this._sendQueue = [];
65
- this._runningQueue = false;
66
- this._mainServer = mainServer;
67
- this.serverConfig = serverConfig;
68
- this._wss = wss;
69
- // Publications
70
- super_admin_1.loadSuperAdminPublications(this);
71
- app_version_1.loadAppVersionPublications(this);
72
- app_status_1.loadAppStatusPublications(this);
73
- logs_1.loadLogPublications(this);
74
- files_1.loadFilePublications(this);
75
- cron_jobs_1.loadCronJobPublications(this);
76
- flags_1.loadFlagsPublications(this);
77
- method_calls_1.loadMethodCallsPublications(this);
78
- method_responses_1.loadMethodResponsePublications(this);
79
- support_tickets_1.loadSupportTicketPublications(this);
80
- billing_logged_in_users_1.loadBillingLoggedInUsersPublications(this);
81
- notifications_1.loadNotificationPublications(this);
82
- this.tailOpLog();
83
- this.runOpLogQueue();
84
- if (index_1.ResolveIOServer.getSupportDB()) {
85
- this.tailOpLogSupport();
86
- }
87
- setInterval(function () {
88
- if (!_this._runningQueue) {
89
- if (_this._sendQueue.length) {
90
- _this._runningQueue = true;
91
- var _loop_1 = function (i) {
92
- var ws = _this._mainServer.getWS(_this._sendQueue[i].id_ws);
93
- if (ws && ws.readyState === ws.OPEN) {
94
- if (ws.bufferedAmount < 20480) {
95
- var sendItem = _this._sendQueue.pop();
96
- ws.send(JSON.stringify(sendItem.data), function (error) {
97
- if (error) {
98
- console.log('Error on WS: ', error);
99
- _this._mainServer.unsubscribeWS(ws);
100
- }
101
- });
102
- }
103
- }
104
- else {
105
- _this._sendQueue.splice(i, 1);
106
- }
107
- };
108
- for (var i = _this._sendQueue.length - 1; i >= 0; i--) {
109
- _loop_1(i);
110
- }
111
- _this._runningQueue = false;
112
- }
113
- }
114
- }, 25);
115
- }
116
- // Add all files to publications private object
117
- SubscriptionManager.prototype.publications = function (method) {
118
- this._publications = Object.assign(this._publications, method);
119
- };
120
- SubscriptionManager.prototype.loggedInLatency = function (ws) {
121
- return __awaiter(this, void 0, void 0, function () {
122
- var _this = this;
123
- return __generator(this, function (_a) {
124
- logged_in_users_collection_1.LoggedInUsers.findOneAndUpdate({ id_ws: ws['id_socket'] }, { $set: { latency: ws['latency'] } }).exec(function (err, res) {
125
- if (err) {
126
- _this.unsubscribeAll(ws);
127
- }
128
- });
129
- return [2 /*return*/];
130
- });
131
- });
132
- };
133
- // Subscribe to publication
134
- SubscriptionManager.prototype.subscribe = function (ws, messageId, publication) {
135
- var subscriptionData = [];
136
- for (var _i = 3; _i < arguments.length; _i++) {
137
- subscriptionData[_i - 3] = arguments[_i];
138
- }
139
- return __awaiter(this, void 0, void 0, function () {
140
- var _a, that, pub, valObj, valKeys, rootKeys, i, sub, _b, newActiveSub, begDate, endDate, billUser;
141
- return __generator(this, function (_c) {
142
- switch (_c.label) {
143
- case 0:
144
- that = this;
145
- pub = that._publications[publication];
146
- if (!!pub) return [3 /*break*/, 1];
147
- console.error('No Publication: ' + publication);
148
- return [2 /*return*/];
149
- case 1:
150
- if (subscriptionData.length > 1 || subscriptionData[0]) {
151
- if (!pub.check) {
152
- console.error('No Check Function For Pub ' + publication);
153
- return [2 /*return*/];
154
- }
155
- else if (!pub.check._schema) {
156
- console.error('No Check Schema For Pub ' + publication);
157
- return [2 /*return*/];
158
- }
159
- else {
160
- valObj = {};
161
- valKeys = Object.keys(pub.check._schema);
162
- rootKeys = valKeys.filter(function (a) { return !a.includes('.'); });
163
- for (i = 0; i < subscriptionData.length; i++) {
164
- valObj[rootKeys[i]] = subscriptionData[i];
165
- }
166
- try {
167
- pub.check.validate(valObj);
168
- }
169
- catch (errors) {
170
- if (errors) {
171
- console.error('Error in Pub Check (' + publication + ')', errors);
172
- return [2 /*return*/];
173
- }
174
- }
175
- }
176
- }
177
- sub = that._subscriptions.filter(function (a) { return a.publication === publication && JSON.stringify(a.subscriptionData) === JSON.stringify(subscriptionData); })[0];
178
- // If sub found (another user watching same data), add client to same sub
179
- if (sub) {
180
- sub.clients.push({
181
- id_user: ws['id_user'],
182
- messageId: messageId,
183
- id_socket: ws['id_socket']
184
- });
185
- }
186
- // If sub not found, create new sub
187
- else {
188
- that._subscriptions.push({
189
- publication: publication,
190
- subscriptionData: subscriptionData,
191
- collections: that.getPublicationCollections(publication),
192
- clients: [{
193
- id_user: ws['id_user'],
194
- messageId: messageId,
195
- id_socket: ws['id_socket']
196
- }]
197
- });
198
- }
199
- if (!sub) {
200
- sub = that._subscriptions.filter(function (a) { return a.publication === publication && JSON.stringify(a.subscriptionData) === JSON.stringify(subscriptionData); })[0];
201
- }
202
- // Send pub data to client who just subscribed only, don't want to send to existing clients since nothing has changed
203
- that.sendPubDataOnce(ws, messageId, sub);
204
- if (!(that._publications[sub.publication].preFunction && !sub.preSubscriptionData)) return [3 /*break*/, 3];
205
- _b = sub;
206
- return [4 /*yield*/, (_a = that._publications[sub.publication]).preFunction.apply(_a, sub.subscriptionData)];
207
- case 2:
208
- _b.preSubscriptionData = _c.sent();
209
- _c.label = 3;
210
- case 3:
211
- newActiveSub = new active_subscription_collection_1.ActiveSubscriptions({
212
- date: new Date(),
213
- publication: publication,
214
- subData: JSON.stringify(subscriptionData),
215
- collections: pub.collections,
216
- clients: sub.clients
217
- });
218
- active_subscription_collection_1.ActiveSubscriptions.findOneAndUpdate({
219
- $and: [
220
- { publication: publication },
221
- { subData: JSON.stringify(subscriptionData) }
222
- ]
223
- }, { $setOnInsert: { _id: mongoose_1.Types.ObjectId().toHexString(), __v: 0 }, $set: newActiveSub }, { upsert: true }).exec();
224
- _c.label = 4;
225
- case 4:
226
- if (!(publication === 'appversion')) return [3 /*break*/, 6];
227
- logged_in_users_collection_1.LoggedInUsers.create({
228
- _id: mongoose_1.Types.ObjectId().toHexString(),
229
- __v: 0,
230
- date: new Date(),
231
- id_user: ws['id_user'],
232
- user: ws['user'],
233
- id_ws: ws['id_socket']
234
- });
235
- if (!(ws['user'] !== 'Admin' && index_1.ResolveIOServer.getSupportDB())) return [3 /*break*/, 6];
236
- begDate = moment().startOf('month');
237
- endDate = moment().endOf('month');
238
- return [4 /*yield*/, billing_logged_in_users_collection_1.BillingLoggedInUsers.findOne({
239
- $and: [
240
- {
241
- client: index_1.ResolveIOServer.getClientName()
242
- },
243
- {
244
- id_user: ws['id_user']
245
- },
246
- {
247
- date: { $gte: begDate }
248
- },
249
- {
250
- date: { $lte: endDate }
251
- }
252
- ]
253
- })];
254
- case 5:
255
- billUser = _c.sent();
256
- if (!billUser) {
257
- billing_logged_in_users_collection_1.BillingLoggedInUsers.create({
258
- _id: mongoose_1.Types.ObjectId().toHexString(),
259
- __v: 0,
260
- id_user: ws['id_user'],
261
- user: ws['user'],
262
- date: new Date(),
263
- client: index_1.ResolveIOServer.getClientName()
264
- });
265
- }
266
- _c.label = 6;
267
- case 6: return [2 /*return*/];
268
- }
269
- });
270
- });
271
- };
272
- // Unsubscribe from publication
273
- SubscriptionManager.prototype.unsubscribe = function (ws, messageId, publication) {
274
- var subscriptionData = [];
275
- for (var _i = 3; _i < arguments.length; _i++) {
276
- subscriptionData[_i - 3] = arguments[_i];
277
- }
278
- return __awaiter(this, void 0, void 0, function () {
279
- var that, sub, i, activeSub;
280
- return __generator(this, function (_a) {
281
- switch (_a.label) {
282
- case 0:
283
- that = this;
284
- if (!!that._publications[publication]) return [3 /*break*/, 1];
285
- console.log('No Publication: ' + publication);
286
- return [2 /*return*/];
287
- case 1:
288
- sub = that._subscriptions.filter(function (a) { return a.publication === publication && JSON.stringify(a.subscriptionData) === JSON.stringify(subscriptionData); })[0];
289
- if (!sub) return [3 /*break*/, 5];
290
- for (i = sub.clients.length - 1; i >= 0; i--) {
291
- if (sub.clients[i].id_user === ws['id_user'] && sub.clients[i].messageId === messageId && sub.clients[i].id_socket === ws['id_socket']) {
292
- sub.clients.splice(i, 1);
293
- }
294
- }
295
- if (!!sub.clients.length) return [3 /*break*/, 2];
296
- that._subscriptions.splice(that._subscriptions.findIndex(function (a) { return a.publication === publication && JSON.stringify(a.subscriptionData) === JSON.stringify(subscriptionData); }), 1);
297
- active_subscription_collection_1.ActiveSubscriptions.deleteOne({
298
- $and: [
299
- { publication: publication },
300
- { subData: JSON.stringify(subscriptionData) }
301
- ]
302
- }).exec();
303
- return [3 /*break*/, 4];
304
- case 2: return [4 /*yield*/, active_subscription_collection_1.ActiveSubscriptions.findOne({
305
- $and: [
306
- { publication: publication },
307
- { subData: JSON.stringify(subscriptionData) }
308
- ]
309
- })];
310
- case 3:
311
- activeSub = _a.sent();
312
- if (activeSub) {
313
- active_subscription_collection_1.ActiveSubscriptions.updateOne({ _id: activeSub._id }, { $set: { clients: sub.clients } }).exec();
314
- }
315
- _a.label = 4;
316
- case 4: return [3 /*break*/, 6];
317
- case 5:
318
- console.log('Did not sub on unsubscribe');
319
- _a.label = 6;
320
- case 6: return [2 /*return*/];
321
- }
322
- });
323
- });
324
- };
325
- // Unsubscribe from publication
326
- SubscriptionManager.prototype.unsubscribeAll = function (ws) {
327
- return __awaiter(this, void 0, void 0, function () {
328
- var that, loggedInUser, userSubs, _loop_2, i, activeSubs;
329
- return __generator(this, function (_a) {
330
- switch (_a.label) {
331
- case 0:
332
- that = this;
333
- return [4 /*yield*/, logged_in_users_collection_1.LoggedInUsers.findOne({ id_ws: ws['id_socket'] })];
334
- case 1:
335
- loggedInUser = _a.sent();
336
- if (loggedInUser) {
337
- logged_in_users_collection_1.LoggedInUsers.deleteOne({ _id: loggedInUser._id }).exec();
338
- }
339
- userSubs = that._subscriptions.filter(function (a) { return a.clients.some(function (b) { return b.id_user === ws['id_user'] && b.id_socket === ws['id_socket']; }); });
340
- _loop_2 = function (i) {
341
- var sub = userSubs[i];
342
- for (var j = sub.clients.length - 1; j >= 0; j--) {
343
- if (sub.clients[j].id_socket === ws['id_socket']) {
344
- sub.clients.splice(j, 1);
345
- }
346
- }
347
- if (!sub.clients.length) {
348
- that._subscriptions.splice(that._subscriptions.findIndex(function (a) { return a.publication === sub.publication && JSON.stringify(a.subscriptionData) === JSON.stringify(sub.subscriptionData); }), 1);
349
- }
350
- };
351
- for (i = userSubs.length - 1; i >= 0; i--) {
352
- _loop_2(i);
353
- }
354
- return [4 /*yield*/, active_subscription_collection_1.ActiveSubscriptions.find({ 'clients.id_socket': ws['id_socket'] })];
355
- case 2:
356
- activeSubs = _a.sent();
357
- activeSubs.forEach(function (sub) {
358
- for (var j = sub.clients.length - 1; j >= 0; j--) {
359
- if (sub.clients[j].id_socket === ws['id_socket']) {
360
- sub.clients.splice(j, 1);
361
- }
362
- }
363
- if (!sub.clients.length) {
364
- active_subscription_collection_1.ActiveSubscriptions.deleteOne({ _id: sub._id }).exec();
365
- }
366
- else {
367
- for (var j = sub.clients.length - 1; j >= 0; j--) {
368
- if (sub.clients[j].id_socket === ws['id_socket']) {
369
- sub.clients.splice(j, 1);
370
- }
371
- }
372
- active_subscription_collection_1.ActiveSubscriptions.updateOne({ _id: sub._id }, { $set: { clients: sub.clients } }).exec();
373
- }
374
- });
375
- return [2 /*return*/];
376
- }
377
- });
378
- });
379
- };
380
- // Get publication collection
381
- SubscriptionManager.prototype.getPublicationCollections = function (publication) {
382
- return this._publications[publication].collections;
383
- };
384
- // Watch (tail) Mongo's operation log on the entire database (all insert/modify/delete will trigger this function)
385
- SubscriptionManager.prototype.tailOpLog = function () {
386
- var _this = this;
387
- var that = this;
388
- var oplog = MongoOplog(that.serverConfig['OPLOG_URL']);
389
- oplog.tail().then(function () {
390
- console.log('tail started');
391
- });
392
- oplog.on('insert', function (doc) {
393
- var nsArray = doc.ns.split('.');
394
- var collection = nsArray[1];
395
- if (nsArray[2]) {
396
- collection += '.' + nsArray[2];
397
- }
398
- if (collection === 'cron-jobs') {
399
- _this._mainServer.getCronManager().checkCronJobsForUpdates();
400
- }
401
- else if (collection === 'queue-methods') {
402
- _this._mainServer.getQueueManager().runMethodQueue();
403
- }
404
- else if (collection === 'queue-responses') {
405
- _this._mainServer.getQueueManager().runMethodResponse();
406
- }
407
- else if (collection === 'support-tickets') {
408
- _this._mainServer.getMethodManager().callMethodInternal('sendSupportTicketEmail', doc.o._id);
409
- }
410
- _this._oplogQueue.push({
411
- type: 'insert',
412
- collection: collection,
413
- doc: doc.o
414
- });
415
- });
416
- oplog.on('update', function (doc) {
417
- var nsArray = doc.ns.split('.');
418
- var collection = nsArray[1];
419
- if (nsArray[2]) {
420
- collection += '.' + nsArray[2];
421
- }
422
- doc.o['_id'] = doc.o2['_id'];
423
- if (collection === 'cron-jobs') {
424
- _this._mainServer.getCronManager().checkCronJobsForUpdates();
425
- }
426
- _this._oplogQueue.push({
427
- type: 'update',
428
- collection: collection,
429
- doc: doc.o
430
- });
431
- });
432
- oplog.on('delete', function (doc) {
433
- var nsArray = doc.ns.split('.');
434
- var collection = nsArray[1];
435
- if (nsArray[2]) {
436
- collection += '.' + nsArray[2];
437
- }
438
- that.resendPubsForDelete(collection);
439
- if (collection === 'cron-jobs') {
440
- _this._mainServer.getCronManager().checkCronJobsForUpdates();
441
- }
442
- });
443
- oplog.on('error', function (error) {
444
- console.log('Error', error);
445
- });
446
- oplog.on('end', function () {
447
- console.log('oplog ended');
448
- process.exit();
449
- });
450
- oplog.stop(function () {
451
- console.log('oplog stopped');
452
- });
453
- };
454
- SubscriptionManager.prototype.tailOpLogSupport = function () {
455
- var that = this;
456
- var oplog = MongoOplog(that.serverConfig['RESOLVEIO_SUPPORT_OPLOG_URL']);
457
- oplog.tail().then(function () {
458
- console.log('tail started support');
459
- });
460
- oplog.on('insert', function (doc) {
461
- var nsArray = doc.ns.split('.');
462
- var collection = nsArray[1];
463
- if (nsArray[2]) {
464
- collection += '.' + nsArray[2];
465
- }
466
- if (collection === 'support-tickets') {
467
- that.checkSupportPubsForChanges(collection, doc.o);
468
- }
469
- });
470
- oplog.on('update', function (doc) {
471
- var nsArray = doc.ns.split('.');
472
- var collection = nsArray[1];
473
- if (nsArray[2]) {
474
- collection += '.' + nsArray[2];
475
- }
476
- doc.o['_id'] = doc.o2['_id'];
477
- if (collection === 'support-tickets') {
478
- that.checkSupportPubsForChanges(collection, doc.o);
479
- }
480
- });
481
- oplog.on('delete', function (doc) {
482
- var nsArray = doc.ns.split('.');
483
- var collection = nsArray[1];
484
- if (nsArray[2]) {
485
- collection += '.' + nsArray[2];
486
- }
487
- if (collection === 'support-tickets') {
488
- that.resendPubsForDelete(collection);
489
- }
490
- });
491
- oplog.on('error', function (error) {
492
- console.log('Error support', error);
493
- });
494
- oplog.on('end', function () {
495
- console.log('oplog ended support');
496
- process.exit();
497
- });
498
- oplog.stop(function () {
499
- console.log('oplog stopped support');
500
- });
501
- };
502
- SubscriptionManager.prototype.runOpLogQueue = function () {
503
- var _this = this;
504
- setInterval(function () {
505
- var oplogQueue = common_1.deepCopy(_this._oplogQueue);
506
- var uniqueCollectionOplogQueue = oplogQueue.map(function (a) { return a.collection; }).filter(function (elem, index, self) {
507
- return index === self.indexOf(elem);
508
- });
509
- uniqueCollectionOplogQueue.forEach(function (collection) {
510
- var oplogQueueFiltered = oplogQueue.filter(function (a) { return a.collection === collection; });
511
- if (oplogQueueFiltered.length === 1) {
512
- _this.checkPubsForChanges(oplogQueueFiltered[0].collection, oplogQueueFiltered[0].doc);
513
- _this._oplogQueue.splice(_this._oplogQueue.map(function (a) { return a.collection; }).indexOf(collection), 1);
514
- }
515
- else {
516
- oplogQueueFiltered.forEach(function (item) {
517
- _this.checkPubsForChanges(item.collection, item.doc, true);
518
- });
519
- _this.checkPubsForChanges(oplogQueueFiltered[oplogQueueFiltered.length - 1].collection, oplogQueueFiltered[oplogQueueFiltered.length - 1].doc, false, true);
520
- for (var i = _this._oplogQueue.length - 1; i >= 0; i--) {
521
- var item = _this._oplogQueue[i];
522
- if (item.collection === collection) {
523
- _this._oplogQueue.splice(i, 1);
524
- }
525
- }
526
- }
527
- });
528
- }, 25);
529
- };
530
- SubscriptionManager.prototype.resendPubsForDelete = function (collection) {
531
- var that = this;
532
- that._subscriptions.filter(function (a) { return a.collections.includes(collection); }).forEach(function (sub) {
533
- if (that._publications[sub.publication].ws_specific) {
534
- sub.clients.forEach(function (client) {
535
- that._wss.clients.forEach(function (ws) {
536
- if (ws['id_socket'] === client.id_socket) {
537
- that.sendPubDataOnce(ws, client.messageId, sub);
538
- }
539
- });
540
- });
541
- }
542
- else {
543
- that.sendPubData(sub);
544
- }
545
- });
546
- };
547
- // Document: Inserted/Modified/Deleted Document
548
- // Collection: Collection Document Lives In
549
- // Check to see if any pubs need to refetch due to Mongo operation
550
- SubscriptionManager.prototype.checkPubsForChanges = function (collection, document, fetchFunctionOnly, noFetchFunction) {
551
- var _this = this;
552
- if (fetchFunctionOnly === void 0) { fetchFunctionOnly = false; }
553
- if (noFetchFunction === void 0) { noFetchFunction = false; }
554
- var that = this;
555
- that._subscriptions.filter(function (a) { return a.collections.includes(collection); }).forEach(function (sub) { return __awaiter(_this, void 0, void 0, function () {
556
- var _a, _b, schemaErrors, _c, _d;
557
- return __generator(this, function (_e) {
558
- switch (_e.label) {
559
- case 0:
560
- schemaErrors = null;
561
- try {
562
- index_1.ResolveIOServer.getMainDB().model(collection)['simplschema'].validate(document);
563
- }
564
- catch (errors) {
565
- schemaErrors = errors;
566
- }
567
- if (!schemaErrors) return [3 /*break*/, 2];
568
- return [4 /*yield*/, index_1.ResolveIOServer.getMainDB().model(collection).findById(document['_id'])];
569
- case 1:
570
- document = _e.sent();
571
- _e.label = 2;
572
- case 2:
573
- if (!sub.preSubscriptionData) return [3 /*break*/, 4];
574
- if (!that._publications[sub.publication].fetchFunction(document, sub.preSubscriptionData)) return [3 /*break*/, 4];
575
- that.sendPubData(sub);
576
- _c = sub;
577
- return [4 /*yield*/, (_a = that._publications[sub.publication]).preFunction.apply(_a, sub.subscriptionData)];
578
- case 3:
579
- _c.preSubscriptionData = _e.sent();
580
- return [2 /*return*/];
581
- case 4:
582
- if (!that._publications[sub.publication].preFunction) return [3 /*break*/, 7];
583
- if (!!noFetchFunction) return [3 /*break*/, 6];
584
- _d = sub;
585
- return [4 /*yield*/, that._publications[sub.publication].preFunction(sub.subscriptionData)];
586
- case 5:
587
- _d.preSubscriptionData = _e.sent();
588
- if (that._publications[sub.publication].fetchFunction(document, sub.preSubscriptionData)) {
589
- that.sendPubData(sub);
590
- }
591
- _e.label = 6;
592
- case 6: return [3 /*break*/, 8];
593
- case 7:
594
- if (that._publications[sub.publication].ws_specific) {
595
- sub.clients.forEach(function (client) {
596
- var _a;
597
- if (that._publications[sub.publication].fetchFunction) {
598
- if (!noFetchFunction) {
599
- if ((_a = that._publications[sub.publication]).fetchFunction.apply(_a, [document, client.id_user].concat(sub.subscriptionData))) {
600
- that._wss.clients.forEach(function (ws) {
601
- if (ws['id_socket'] === client.id_socket) {
602
- that.sendPubDataOnce(ws, client.messageId, sub);
603
- }
604
- });
605
- }
606
- }
607
- }
608
- else if (!fetchFunctionOnly) {
609
- that._wss.clients.forEach(function (ws) {
610
- if (ws['id_socket'] === client.id_socket) {
611
- that.sendPubDataOnce(ws, client.messageId, sub);
612
- }
613
- });
614
- }
615
- });
616
- }
617
- else {
618
- if (that._publications[sub.publication].fetchFunction) {
619
- if (!noFetchFunction) {
620
- if ((_b = that._publications[sub.publication]).fetchFunction.apply(_b, [document].concat(sub.subscriptionData))) {
621
- that.sendPubData(sub);
622
- }
623
- }
624
- }
625
- else if (!fetchFunctionOnly) {
626
- that.sendPubData(sub);
627
- }
628
- }
629
- _e.label = 8;
630
- case 8: return [2 /*return*/];
631
- }
632
- });
633
- }); });
634
- };
635
- SubscriptionManager.prototype.checkSupportPubsForChanges = function (collection, document) {
636
- var _this = this;
637
- var that = this;
638
- that._subscriptions.filter(function (a) { return a.collections.includes(collection); }).forEach(function (sub) { return __awaiter(_this, void 0, void 0, function () {
639
- var _a, _b, _c, schemaErrors, _d, _e;
640
- return __generator(this, function (_f) {
641
- switch (_f.label) {
642
- case 0:
643
- schemaErrors = null;
644
- try {
645
- index_1.ResolveIOServer.getSupportDB().model(collection)['simplschema'].validate(document);
646
- }
647
- catch (errors) {
648
- schemaErrors = errors;
649
- }
650
- if (!schemaErrors) return [3 /*break*/, 2];
651
- return [4 /*yield*/, index_1.ResolveIOServer.getSupportDB().model(collection).findById(document['_id'])];
652
- case 1:
653
- document = _f.sent();
654
- _f.label = 2;
655
- case 2:
656
- if (!sub.preSubscriptionData) return [3 /*break*/, 4];
657
- if (!that._publications[sub.publication].fetchFunction(document, sub.preSubscriptionData)) return [3 /*break*/, 4];
658
- that.sendPubData(sub);
659
- _d = sub;
660
- return [4 /*yield*/, (_a = that._publications[sub.publication]).preFunction.apply(_a, sub.subscriptionData)];
661
- case 3:
662
- _d.preSubscriptionData = _f.sent();
663
- return [2 /*return*/];
664
- case 4:
665
- if (!that._publications[sub.publication].preFunction) return [3 /*break*/, 6];
666
- _e = sub;
667
- return [4 /*yield*/, (_b = that._publications[sub.publication]).preFunction.apply(_b, sub.subscriptionData)];
668
- case 5:
669
- _e.preSubscriptionData = _f.sent();
670
- if (that._publications[sub.publication].fetchFunction(document, sub.preSubscriptionData)) {
671
- that.sendPubData(sub);
672
- }
673
- return [3 /*break*/, 7];
674
- case 6:
675
- if (that._publications[sub.publication].ws_specific) {
676
- sub.clients.forEach(function (client) {
677
- var _a;
678
- if (that._publications[sub.publication].fetchFunction) {
679
- if ((_a = that._publications[sub.publication]).fetchFunction.apply(_a, [document, client.id_user].concat(sub.subscriptionData))) {
680
- that._wss.clients.forEach(function (ws) {
681
- if (ws['id_socket'] === client.id_socket) {
682
- that.sendPubDataOnce(ws, client.messageId, sub);
683
- }
684
- });
685
- }
686
- }
687
- else {
688
- that._wss.clients.forEach(function (ws) {
689
- if (ws['id_socket'] === client.id_socket) {
690
- that.sendPubDataOnce(ws, client.messageId, sub);
691
- }
692
- });
693
- }
694
- });
695
- }
696
- else {
697
- if (that._publications[sub.publication].fetchFunction) {
698
- if ((_c = that._publications[sub.publication]).fetchFunction.apply(_c, [document].concat(sub.subscriptionData))) {
699
- that.sendPubData(sub);
700
- }
701
- }
702
- else {
703
- that.sendPubData(sub);
704
- }
705
- }
706
- _f.label = 7;
707
- case 7: return [2 /*return*/];
708
- }
709
- });
710
- }); });
711
- };
712
- // Fetch pub once, send to all clients linked to this pub
713
- SubscriptionManager.prototype.sendPubDataOnce = function (ws, messageId, subscription) {
714
- return __awaiter(this, void 0, void 0, function () {
715
- var _a, _b, that;
716
- var _this = this;
717
- return __generator(this, function (_c) {
718
- that = this;
719
- if (!that._publications[subscription.publication].ws_specific) { // Same pub for all users
720
- (_a = that._publications[subscription.publication].function).call.apply(_a, [Object.assign({}, that, SubscriptionManager.prototype)].concat(subscription.subscriptionData)).then(function (res) {
721
- var serverRes = {
722
- messageId: messageId,
723
- hasError: false,
724
- data: res
725
- };
726
- _this.sendWS(ws, serverRes);
727
- }, function (err) {
728
- var serverRes = {
729
- messageId: messageId,
730
- hasError: true,
731
- data: err
732
- };
733
- _this.sendWS(ws, serverRes);
734
- });
735
- }
736
- else { // Pub is different for each user (Look at users publication) [MUCH SLOWER, RE-RUNS FOR EACH USER]
737
- (_b = that._publications[subscription.publication].function).call.apply(_b, [Object.assign({}, that, SubscriptionManager.prototype), ws['id_user']].concat(subscription.subscriptionData)).then(function (res) {
738
- var serverRes = {
739
- messageId: messageId,
740
- hasError: false,
741
- data: res
742
- };
743
- _this.sendWS(ws, serverRes);
744
- }, function (err) {
745
- var serverRes = {
746
- messageId: messageId,
747
- hasError: true,
748
- data: err
749
- };
750
- _this.sendWS(ws, serverRes);
751
- });
752
- }
753
- return [2 /*return*/];
754
- });
755
- });
756
- };
757
- // Fetch pub once, send to all clients linked to this pub
758
- SubscriptionManager.prototype.sendPubData = function (subscription) {
759
- return __awaiter(this, void 0, void 0, function () {
760
- var _a, that;
761
- var _this = this;
762
- return __generator(this, function (_b) {
763
- that = this;
764
- (_a = that._publications[subscription.publication].function).call.apply(_a, [Object.assign({}, that, SubscriptionManager.prototype)].concat(subscription.subscriptionData)).then(function (res) {
765
- subscription.clients.forEach(function (client) {
766
- that._wss.clients.forEach(function (ws) {
767
- if (ws['id_socket'] === client.id_socket) {
768
- var serverRes = {
769
- messageId: client.messageId,
770
- hasError: false,
771
- data: res
772
- };
773
- _this.sendWS(ws, serverRes);
774
- }
775
- });
776
- });
777
- }, function (err) {
778
- subscription.clients.forEach(function (client) {
779
- that._wss.clients.forEach(function (ws) {
780
- if (ws['id_socket'] === client.id_socket) {
781
- var serverRes = {
782
- messageId: client.messageId,
783
- hasError: true,
784
- data: err
785
- };
786
- _this.sendWS(ws, serverRes);
787
- }
788
- });
789
- });
790
- });
791
- return [2 /*return*/];
792
- });
793
- });
794
- };
795
- SubscriptionManager.prototype.sendWS = function (ws, data) {
796
- this._sendQueue.splice(0, 0, {
797
- id_ws: ws['id_socket'],
798
- data: data
799
- });
800
- };
801
- return SubscriptionManager;
802
- }());
803
- exports.SubscriptionManager = SubscriptionManager;