-
Notifications
You must be signed in to change notification settings - Fork 103
/
threads.js
222 lines (190 loc) · 5.78 KB
/
threads.js
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
/*
* Copyright (c) Forward Email LLC
* SPDX-License-Identifier: MPL-2.0
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*
* This file incorporates work covered by the following copyright and
* permission notice:
*
* WildDuck Mail Agent is licensed under the European Union Public License 1.2 or later.
* https://github.com/nodemailer/wildduck
*/
const crypto = require('node:crypto');
const Database = require('better-sqlite3-multiple-ciphers');
const MessageHandler = require('wildduck/lib/message-handler');
const _ = require('lodash');
const mongoose = require('mongoose');
const safeStringify = require('fast-safe-stringify');
const validationErrorTransform = require('mongoose-validation-error-transform');
const { Builder } = require('json-sql');
// <https://github.com/Automattic/mongoose/issues/5534>
mongoose.Error.messages = require('@ladjs/mongoose-error-messages');
const {
dummyProofModel,
dummySchemaOptions,
convertResult,
sqliteVirtualDB
} = require('#helpers/mongoose-to-sqlite');
const builder = new Builder();
const Threads = new mongoose.Schema(
{
ids: [
{
type: String,
index: true
}
],
subject: {
type: String,
index: true
}
},
dummySchemaOptions
);
Threads.pre('validate', function (next) {
this.ids = _.uniq(this.ids);
next();
});
Threads.plugin(sqliteVirtualDB);
Threads.plugin(validationErrorTransform);
// code is inspired from wildduck (rewrite necessary for async/await and different db structure)
// eslint-disable-next-line complexity
async function getThreadId(instance, session, subject, mimeTree) {
if (!session?.db || (!(session.db instanceof Database) && !session.db.wsp))
throw new TypeError('Database is missing');
if (
instance?.wsp?.constructor?.name !== 'WebSocketAsPromised' &&
(!instance?.wsp || !instance.wsp[Symbol.for('isWSP')]) &&
!instance[Symbol.for('isWSP')]
)
throw new TypeError('WebSocketAsPromised instance required');
if (typeof session?.user?.password !== 'string')
throw new TypeError('Session user and password missing');
let referenceIds = new Set(
[
[mimeTree.parsedHeader['message-id'] || []].flat().pop() || '',
[mimeTree.parsedHeader['in-reply-to'] || []].flat().pop() || '',
([mimeTree.parsedHeader['thread-index'] || []].flat().pop() || '').slice(
0,
22
),
[mimeTree.parsedHeader.references || []].flat().pop() || ''
]
.join(' ')
.split(/\s+/)
.map((id) => id.replace(/[<>]/g, '').trim())
.filter(Boolean)
.map((id) =>
crypto
.createHash('sha1')
.update(id)
.digest('base64')
.replace(/=+$/g, '')
)
);
subject = MessageHandler.prototype.normalizeSubject(subject, {
removePrefix: true
});
referenceIds = [...referenceIds].slice(0, 10);
let thread;
if (referenceIds.length > 0) {
{
// <https://stackoverflow.com/questions/63651913/is-there-a-method-to-check-if-an-array-includes-one-value-in-sqlite>
const sql = `select * from "Threads" where "subject" = ? and exists (select 1 from json_each("ids") where (${referenceIds
.map(() => `"value" = ?`)
.join(' or ')})) limit 1;`;
const values = [subject, ...referenceIds];
// reading so no need to lock
if (session.db.wsp) {
thread = await instance.wsp.request({
action: 'stmt',
session: { user: session.user },
stmt: [
['prepare', sql],
['get', values]
]
});
} else {
thread = session.db.prepare(sql).get(values);
}
}
if (thread) {
thread = await convertResult(this, thread);
for (const id of referenceIds) {
thread.ids.push(id);
}
// make it unique (similar to $addToSet in mongo)
thread.ids = _.uniq(thread.ids);
// save the doc here
{
const sql = builder.build({
type: 'update',
table: 'Threads',
condition: {
_id: thread._id.toString()
},
modifier: {
ids: safeStringify(thread.ids)
}
});
// result of this will be like:
// `{ changes: 1, lastInsertRowid: 11 }`
// use websockets if readonly
if (session.db.readonly) {
await instance.wsp.request({
action: 'stmt',
session: { user: session.user },
stmt: [
['prepare', sql.query],
['run', sql.values]
]
});
} else {
session.db.prepare(sql.query).run(sql.values);
}
}
{
const sql = builder.build({
type: 'select',
table: 'Threads',
condition: {
_id: thread._id.toString()
}
});
if (session.db.wsp) {
thread = await instance.wsp.request({
action: 'stmt',
session: { user: session.user },
stmt: [
['prepare', sql.query],
['get', sql.values]
]
});
} else {
thread = session.db.prepare(sql.query).get(sql.values);
}
if (!thread) throw new TypeError('Thread does not exist');
thread = await convertResult(this, thread);
}
}
}
if (thread) {
// virtual for rollback
thread.isNew = false;
return thread;
}
thread = await this.create({
instance,
session,
ids: referenceIds,
subject
});
// virtual for rollback
thread.isNew = true;
return thread;
}
Threads.statics.getThreadId = getThreadId;
module.exports = dummyProofModel(mongoose.model('Threads', Threads));