Skip to content

Commit 7422e6c

Browse files
committed
Convert EmailInbox from model to raw
1 parent d994c66 commit 7422e6c

File tree

6 files changed

+111
-128
lines changed

6 files changed

+111
-128
lines changed

app/api/server/v1/email-inbox.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { check, Match } from 'meteor/check';
33
import { API } from '../api';
44
import { findEmailInboxes, findOneEmailInbox, insertOneOrUpdateEmailInbox } from '../lib/emailInbox';
55
import { hasPermission } from '../../../authorization/server/functions/hasPermission';
6-
import { EmailInbox } from '../../../models';
6+
import { EmailInbox } from '../../../models/server/raw';
77
import Users from '../../../models/server/models/Users';
88
import { sendTestEmailToInbox } from '../../../../server/features/EmailInbox/EmailInbox_Outgoing';
99

@@ -79,12 +79,12 @@ API.v1.addRoute('email-inbox/:_id', { authRequired: true }, {
7979
const { _id } = this.urlParams;
8080
if (!_id) { throw new Error('error-invalid-param'); }
8181

82-
const emailInboxes = EmailInbox.findOneById(_id);
82+
const emailInboxes = Promise.await(EmailInbox.findOneById(_id));
8383

8484
if (!emailInboxes) {
8585
return API.v1.notFound();
8686
}
87-
EmailInbox.removeById(_id);
87+
Promise.await(EmailInbox.removeById(_id));
8888
return API.v1.success({ _id });
8989
},
9090
});

app/models/server/index.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ import ReadReceipts from './models/ReadReceipts';
3535
import LivechatExternalMessage from './models/LivechatExternalMessages';
3636
import OmnichannelQueue from './models/OmnichannelQueue';
3737
import Analytics from './models/Analytics';
38-
import EmailInbox from './models/EmailInbox';
3938
import ImportData from './models/ImportData';
4039

4140
export { AppsLogsModel } from './models/apps-logs-model';
@@ -84,6 +83,5 @@ export {
8483
LivechatInquiry,
8584
Analytics,
8685
OmnichannelQueue,
87-
EmailInbox,
8886
ImportData,
8987
};

app/models/server/models/EmailInbox.js

Lines changed: 0 additions & 27 deletions
This file was deleted.

app/models/server/models/_BaseDb.js

Lines changed: 101 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -30,31 +30,14 @@ const actions = {
3030
d: 'remove',
3131
};
3232

33-
export class BaseDb extends EventEmitter {
34-
constructor(model, baseModel, options = {}) {
33+
export class BaseDbWatch extends EventEmitter {
34+
constructor(collectionName) {
3535
super();
36-
37-
if (Match.test(model, String)) {
38-
this.name = model;
39-
this.collectionName = this.baseName + this.name;
40-
this.model = new Mongo.Collection(this.collectionName);
41-
} else {
42-
this.name = model._name;
43-
this.collectionName = this.name;
44-
this.model = model;
45-
}
46-
47-
this.baseModel = baseModel;
48-
49-
this.preventSetUpdatedAt = !!options.preventSetUpdatedAt;
50-
51-
this.wrapModel();
36+
this.collectionName = collectionName;
5237

5338
if (!process.env.DISABLE_DB_WATCH) {
5439
this.initDbWatch();
5540
}
56-
57-
this.tryEnsureIndex({ _updatedAt: 1 }, options._updatedAtIndexOptions);
5841
}
5942

6043
initDbWatch() {
@@ -97,6 +80,104 @@ export class BaseDb extends EventEmitter {
9780
}
9881
}
9982

83+
processOplogRecord({ id, op }) {
84+
const action = actions[op.op];
85+
metrics.oplog.inc({
86+
collection: this.collectionName,
87+
op: action,
88+
});
89+
90+
if (action === 'insert') {
91+
this.emit('change', {
92+
action,
93+
clientAction: 'inserted',
94+
id: op.o._id,
95+
data: op.o,
96+
oplog: true,
97+
});
98+
return;
99+
}
100+
101+
if (action === 'update') {
102+
if (!op.o.$set && !op.o.$unset) {
103+
this.emit('change', {
104+
action,
105+
clientAction: 'updated',
106+
id,
107+
data: op.o,
108+
oplog: true,
109+
});
110+
return;
111+
}
112+
113+
const diff = {};
114+
if (op.o.$set) {
115+
for (const key in op.o.$set) {
116+
if (op.o.$set.hasOwnProperty(key)) {
117+
diff[key] = op.o.$set[key];
118+
}
119+
}
120+
}
121+
const unset = {};
122+
if (op.o.$unset) {
123+
for (const key in op.o.$unset) {
124+
if (op.o.$unset.hasOwnProperty(key)) {
125+
diff[key] = undefined;
126+
unset[key] = 1;
127+
}
128+
}
129+
}
130+
131+
this.emit('change', {
132+
action,
133+
clientAction: 'updated',
134+
id,
135+
diff,
136+
unset,
137+
oplog: true,
138+
});
139+
return;
140+
}
141+
142+
if (action === 'remove') {
143+
this.emit('change', {
144+
action,
145+
clientAction: 'removed',
146+
id,
147+
oplog: true,
148+
});
149+
}
150+
}
151+
}
152+
153+
154+
export class BaseDb extends BaseDbWatch {
155+
constructor(model, baseModel, options = {}) {
156+
const collectionName = Match.test(model, String) ? baseName + model : model._name;
157+
158+
super(collectionName);
159+
160+
this.collectionName = collectionName;
161+
162+
if (Match.test(model, String)) {
163+
this.name = model;
164+
this.collectionName = this.baseName + this.name;
165+
this.model = new Mongo.Collection(this.collectionName);
166+
} else {
167+
this.name = model._name;
168+
this.collectionName = this.name;
169+
this.model = model;
170+
}
171+
172+
this.baseModel = baseModel;
173+
174+
this.preventSetUpdatedAt = !!options.preventSetUpdatedAt;
175+
176+
this.wrapModel();
177+
178+
this.tryEnsureIndex({ _updatedAt: 1 }, options._updatedAtIndexOptions);
179+
}
180+
100181
get baseName() {
101182
return baseName;
102183
}
@@ -204,75 +285,6 @@ export class BaseDb extends EventEmitter {
204285
);
205286
}
206287

207-
processOplogRecord({ id, op }) {
208-
const action = actions[op.op];
209-
metrics.oplog.inc({
210-
collection: this.collectionName,
211-
op: action,
212-
});
213-
214-
if (action === 'insert') {
215-
this.emit('change', {
216-
action,
217-
clientAction: 'inserted',
218-
id: op.o._id,
219-
data: op.o,
220-
oplog: true,
221-
});
222-
return;
223-
}
224-
225-
if (action === 'update') {
226-
if (!op.o.$set && !op.o.$unset) {
227-
this.emit('change', {
228-
action,
229-
clientAction: 'updated',
230-
id,
231-
data: op.o,
232-
oplog: true,
233-
});
234-
return;
235-
}
236-
237-
const diff = {};
238-
if (op.o.$set) {
239-
for (const key in op.o.$set) {
240-
if (op.o.$set.hasOwnProperty(key)) {
241-
diff[key] = op.o.$set[key];
242-
}
243-
}
244-
}
245-
const unset = {};
246-
if (op.o.$unset) {
247-
for (const key in op.o.$unset) {
248-
if (op.o.$unset.hasOwnProperty(key)) {
249-
diff[key] = undefined;
250-
unset[key] = 1;
251-
}
252-
}
253-
}
254-
255-
this.emit('change', {
256-
action,
257-
clientAction: 'updated',
258-
id,
259-
diff,
260-
unset,
261-
oplog: true,
262-
});
263-
return;
264-
}
265-
266-
if (action === 'remove') {
267-
this.emit('change', {
268-
action,
269-
clientAction: 'removed',
270-
id,
271-
oplog: true,
272-
});
273-
}
274-
}
275-
276288
insert(record, ...args) {
277289
this.setUpdatedAt(record);
278290

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1-
import { BaseRaw } from './BaseRaw';
1+
import { BaseRaw, IndexSpecification } from './BaseRaw';
22
import { IEmailInbox } from '../../../../definition/IEmailInbox';
33

44
export class EmailInboxRaw extends BaseRaw<IEmailInbox> {
5-
//
5+
protected indexes: IndexSpecification[] = [
6+
{ key: { email: 1 }, unique: true },
7+
]
68
}

app/models/server/raw/index.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ import { LivechatBusinessHoursRaw } from './LivechatBusinessHours';
5252
import { UsersSessionsRaw } from './UsersSessions';
5353
import UsersSessionsModel from '../models/UsersSessions';
5454
import { ServerEventsRaw } from './ServerEvents';
55-
import { trash } from '../models/_BaseDb';
55+
import { BaseDbWatch, trash } from '../models/_BaseDb';
5656
import LoginServiceConfigurationModel from '../models/LoginServiceConfiguration';
5757
import { LoginServiceConfigurationRaw } from './LoginServiceConfiguration';
5858
import { InstanceStatusRaw } from './InstanceStatus';
@@ -61,7 +61,6 @@ import { IntegrationHistoryRaw } from './IntegrationHistory';
6161
import IntegrationHistoryModel from '../models/IntegrationHistory';
6262
import OmnichannelQueueModel from '../models/OmnichannelQueue';
6363
import { OmnichannelQueueRaw } from './OmnichannelQueue';
64-
import EmailInboxModel from '../models/EmailInbox';
6564
import { EmailInboxRaw } from './EmailInbox';
6665
import EmailMessageHistoryModel from '../models/EmailMessageHistory';
6766
import { EmailMessageHistoryRaw } from './EmailMessageHistory';
@@ -102,7 +101,6 @@ export const InstanceStatus = new InstanceStatusRaw(InstanceStatusModel.model.ra
102101
export const IntegrationHistory = new IntegrationHistoryRaw(IntegrationHistoryModel.model.rawCollection(), trashCollection);
103102
export const Sessions = new SessionsRaw(SessionsModel.model.rawCollection(), trashCollection);
104103
export const OmnichannelQueue = new OmnichannelQueueRaw(OmnichannelQueueModel.model.rawCollection(), trashCollection);
105-
export const EmailInbox = new EmailInboxRaw(EmailInboxModel.model.rawCollection(), trashCollection);
106104
export const EmailMessageHistory = new EmailMessageHistoryRaw(EmailMessageHistoryModel.model.rawCollection(), trashCollection);
107105
export const ImportData = new ImportDataRaw(ImportDataModel.model.rawCollection(), trashCollection);
108106

@@ -115,6 +113,7 @@ export const OEmbedCache = new OEmbedCacheRaw(db.collection(`${ prefix }oembed_c
115113
export const NotificationQueue = new NotificationQueueRaw(db.collection(`${ prefix }notification_queue`), trashCollection);
116114
export const Invites = new InvitesRaw(db.collection(`${ prefix }invites`), trashCollection);
117115
export const ServerEvents = new ServerEventsRaw(db.collection(`${ prefix }server_events`), trashCollection);
116+
export const EmailInbox = new EmailInboxRaw(db.collection(`${ prefix }email_inbox`), trashCollection);
118117

119118
const map = {
120119
[Messages.col.collectionName]: MessagesModel,
@@ -131,7 +130,6 @@ const map = {
131130
[InstanceStatus.col.collectionName]: InstanceStatusModel,
132131
[IntegrationHistory.col.collectionName]: IntegrationHistoryModel,
133132
[Integrations.col.collectionName]: IntegrationsModel,
134-
[EmailInbox.col.collectionName]: EmailInboxModel,
135133
};
136134

137135
if (!process.env.DISABLE_DB_WATCH) {
@@ -154,7 +152,7 @@ if (!process.env.DISABLE_DB_WATCH) {
154152
};
155153

156154
initWatchers(models, api.broadcastLocal.bind(api), (model, fn) => {
157-
const meteorModel = map[model.col.collectionName];
155+
const meteorModel = map[model.col.collectionName] || new BaseDbWatch(model.col.collectionName);
158156
if (!meteorModel) {
159157
return;
160158
}

0 commit comments

Comments
 (0)