@wirecat/cli-core 1.0.0 → 1.1.0
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.
- package/CHANGELOG.md +89 -0
- package/THIRD_PARTY_NOTICES +43 -0
- package/dist/background/daily-archive-timer.d.ts +1 -1
- package/dist/cli/documents/words.d.ts +1 -1
- package/dist/cli/domain-lists-command.d.ts +16 -0
- package/dist/cli/domain-lists-command.js +88 -0
- package/dist/cli/index.d.ts +4 -3
- package/dist/cli/index.js +3 -2
- package/dist/cli/model-catalogue-command.d.ts +6 -0
- package/dist/cli/model-catalogue-command.js +1 -1
- package/dist/cli/oauth-session.d.ts +38 -4
- package/dist/cli/oauth-session.js +66 -22
- package/dist/cli/shared-command-exports.d.ts +2 -0
- package/dist/cli/shared-command-exports.js +3 -0
- package/dist/cli/token-session.d.ts +9 -0
- package/dist/cli/token-session.js +29 -2
- package/dist/domain/domain-lists.d.ts +86 -0
- package/dist/domain/domain-lists.js +110 -0
- package/dist/domain/free-mail-domains.generated.d.ts +1 -0
- package/dist/domain/free-mail-domains.generated.js +4 -0
- package/dist/domain/organization-sectors.d.ts +9 -3
- package/dist/domain/organization-sectors.generated.d.ts +1 -0
- package/dist/domain/organization-sectors.generated.js +1 -0
- package/dist/domain/organization-sectors.js +16 -3
- package/dist/domain/person-kinds.d.ts +6 -1
- package/dist/domain/person-kinds.js +5 -0
- package/dist/entities/domain-list.d.ts +27 -0
- package/dist/entities/domain-list.js +28 -0
- package/dist/entities/index.d.ts +20 -2
- package/dist/entities/index.js +3 -1
- package/dist/entities/knowledge-lists.d.ts +1 -1
- package/dist/errors/reasons.js +4 -2
- package/dist/mail/mail-types.d.ts +48 -0
- package/dist/mail/mail-types.js +218 -0
- package/dist/oauth/index.d.ts +1 -1
- package/dist/oauth/storage.d.ts +10 -2
- package/dist/oauth/storage.js +23 -9
- package/dist/services/domain-lists-download.d.ts +26 -0
- package/dist/services/domain-lists-download.js +155 -0
- package/dist/services/metadata.d.ts +1 -1
- package/dist/store/areas.d.ts +1 -1
- package/dist/store/index.d.ts +2 -2
- package/dist/store/index.js +1 -1
- package/dist/store/people.d.ts +18 -9
- package/dist/store/people.js +8 -7
- package/dist/store/sqlite/accounts.js +3 -1
- package/dist/store/sqlite/domain-organizations.d.ts +7 -4
- package/dist/store/sqlite/domain-organizations.js +94 -36
- package/dist/store/sqlite/email-types.d.ts +79 -0
- package/dist/store/sqlite/email-types.js +261 -0
- package/dist/store/sqlite/emails.d.ts +11 -2
- package/dist/store/sqlite/emails.js +18 -7
- package/dist/store/sqlite/identities.d.ts +4 -1
- package/dist/store/sqlite/identities.js +11 -2
- package/dist/store/sqlite/identity-rekey.js +2 -0
- package/dist/store/sqlite/manifest.js +4 -0
- package/dist/store/sqlite/merged-persons.d.ts +3 -2
- package/dist/store/sqlite/merged-persons.js +5 -3
- package/dist/store/sqlite/migrations.generated.js +56 -0
- package/dist/store/sqlite/people-list.d.ts +1 -6
- package/dist/store/sqlite/people-list.js +38 -32
- package/dist/store/sqlite/person-links.js +13 -3
- package/dist/store/sqlite/person-match-scan.js +5 -2
- package/dist/store/sqlite/schema/sender-bots.d.ts +56 -0
- package/dist/store/sqlite/schema/sender-bots.js +9 -1
- package/dist/store/sqlite/schema.d.ts +49 -1
- package/dist/store/sqlite/schema.js +6 -1
- package/dist/store/sqlite/sender-bots.d.ts +80 -15
- package/dist/store/sqlite/sender-bots.js +378 -136
- package/dist/store/sqlite/sender-signals.d.ts +11 -4
- package/dist/store/sqlite/sender-signals.js +14 -10
- package/package.json +2 -2
|
@@ -0,0 +1,261 @@
|
|
|
1
|
+
import { SENDER_BOT_PREFIX } from "../../domain/person-kinds.js";
|
|
2
|
+
import { MAIL_TYPES, mailTypeOf, schemaTypesOf } from "../../mail/mail-types.js";
|
|
3
|
+
import { isQueryOnly } from "../driver.js";
|
|
4
|
+
import { LIST_ID, LIST_SENDERS, REACHES, senderKey } from "./sender-signals.js";
|
|
5
|
+
import { read, writeBatch } from "./transaction.js";
|
|
6
|
+
/** Bump when what decides a mail type changes in code: every stored one is worked out again on the next read. */
|
|
7
|
+
const TYPE_RULES = "1";
|
|
8
|
+
const RULES_KEY = "mail.type_rules";
|
|
9
|
+
/**
|
|
10
|
+
* Working out 5,000 types takes about 40 ms; the commit's sync to disk costs more, so fewer and larger batches finish
|
|
11
|
+
* sooner and still hold the lock well inside another writer's 5 s wait.
|
|
12
|
+
*/
|
|
13
|
+
const BATCH = 5_000;
|
|
14
|
+
const INPUTS = "SELECT e.id, e.from_address, e.subject, e.headers, e.body_html, " +
|
|
15
|
+
`${LIST_ID} AS list, ` +
|
|
16
|
+
"CASE WHEN json_extract(e.metadata, '$.rule') IS NOT NULL THEN NULL ELSE " +
|
|
17
|
+
"(SELECT count(*) FROM email_recipients r WHERE r.email_id = e.id AND r.role IN ('to', 'cc')) END AS recipients, " +
|
|
18
|
+
"(SELECT json_group_array(lower(a.mime)) FROM attachments a " +
|
|
19
|
+
"WHERE a.attachable_type = 'email' AND a.attachable_id = e.id AND a.mime IS NOT NULL) AS parts, " +
|
|
20
|
+
"e.outgoing IS 1 OR EXISTS (SELECT 1 FROM email_recipients r JOIN emails o ON o.id = r.email_id " +
|
|
21
|
+
"WHERE r.identity_id = e.from_identity_id AND o.outgoing = 1) AS owner_wrote, " +
|
|
22
|
+
"EXISTS (SELECT 1 FROM sender_bot_identities b WHERE b.identity_id = e.from_identity_id) AS bot, " +
|
|
23
|
+
"(SELECT json_extract(g.metadata, '$.sector') FROM organization_identities oi " +
|
|
24
|
+
"JOIN organizations g ON g.id = oi.organization_id " +
|
|
25
|
+
"WHERE oi.identity_id = e.from_identity_id AND g.deleted_at IS NULL ORDER BY g.id LIMIT 1) AS sector " +
|
|
26
|
+
"FROM emails e WHERE e.id IN (SELECT value FROM json_each(?))";
|
|
27
|
+
const parsed = (value) => {
|
|
28
|
+
if (typeof value !== "string")
|
|
29
|
+
return {};
|
|
30
|
+
try {
|
|
31
|
+
const found = JSON.parse(value);
|
|
32
|
+
return found !== null && typeof found === "object" && !Array.isArray(found)
|
|
33
|
+
? found
|
|
34
|
+
: {};
|
|
35
|
+
}
|
|
36
|
+
catch {
|
|
37
|
+
return {};
|
|
38
|
+
}
|
|
39
|
+
};
|
|
40
|
+
/** The type, reach and signal of each of these emails, worked out from the store; nothing is written. */
|
|
41
|
+
/**
|
|
42
|
+
* `senders` carries each List-Id's sender count from batch to batch: a newsletter's every issue would read all its
|
|
43
|
+
* issues again.
|
|
44
|
+
*/
|
|
45
|
+
export const typesOf = (database, ids, senders = new Map()) => {
|
|
46
|
+
const found = new Map();
|
|
47
|
+
if (ids.length === 0)
|
|
48
|
+
return found;
|
|
49
|
+
const rows = database.prepare(INPUTS).all(JSON.stringify(ids));
|
|
50
|
+
const lists = [
|
|
51
|
+
...new Set(rows.flatMap((row) => (typeof row.list === "string" && !senders.has(row.list) ? [row.list] : []))),
|
|
52
|
+
];
|
|
53
|
+
if (lists.length > 0) {
|
|
54
|
+
const held = new Map(lists.map((list) => [list, new Set()]));
|
|
55
|
+
for (const row of database.prepare(LIST_SENDERS).all(JSON.stringify(lists)))
|
|
56
|
+
held.get(String(row.list))?.add(senderKey(String(row.address)));
|
|
57
|
+
for (const [list, addresses] of held)
|
|
58
|
+
senders.set(list, addresses.size);
|
|
59
|
+
}
|
|
60
|
+
for (const row of rows) {
|
|
61
|
+
const headers = parsed(row.headers);
|
|
62
|
+
found.set(Number(row.id), mailTypeOf({
|
|
63
|
+
from: typeof row.from_address === "string" ? row.from_address.toLowerCase() : null,
|
|
64
|
+
subject: typeof row.subject === "string" ? row.subject : null,
|
|
65
|
+
headers: typeof row.list === "string" ? { ...headers, "list-id": row.list } : headers,
|
|
66
|
+
recipients: row.recipients == null ? null : Number(row.recipients),
|
|
67
|
+
parts: JSON.parse(String(row.parts ?? "[]")),
|
|
68
|
+
schemaTypes: schemaTypesOf(typeof row.body_html === "string" ? row.body_html : null),
|
|
69
|
+
listSenders: typeof row.list === "string" ? (senders.get(row.list) ?? 0) : 0,
|
|
70
|
+
ownerWrote: Number(row.owner_wrote) === 1,
|
|
71
|
+
bot: Number(row.bot) === 1,
|
|
72
|
+
sector: typeof row.sector === "string" ? row.sector : null,
|
|
73
|
+
}));
|
|
74
|
+
}
|
|
75
|
+
return found;
|
|
76
|
+
};
|
|
77
|
+
const rulesOf = (database) => {
|
|
78
|
+
const row = database.prepare("SELECT value FROM store_settings WHERE key = ?").get(RULES_KEY);
|
|
79
|
+
return row === undefined ? undefined : String(row.value);
|
|
80
|
+
};
|
|
81
|
+
const anyLeft = (database) => database.prepare("SELECT 1 FROM emails WHERE mail_type IS NULL LIMIT 1").get() !== undefined;
|
|
82
|
+
/** Whether every email's stored type is current. */
|
|
83
|
+
export const typesCurrent = (database) => rulesOf(database) === TYPE_RULES && !anyLeft(database);
|
|
84
|
+
/**
|
|
85
|
+
* Works out and keeps the type of every email whose stored one the triggers cleared, or every email after the rules
|
|
86
|
+
* changed, a batch per write so another process never waits long for the lock. A query-only open keeps nothing.
|
|
87
|
+
* Answers whether every stored type is current.
|
|
88
|
+
*/
|
|
89
|
+
export const storeMailTypes = (database) => {
|
|
90
|
+
if (isQueryOnly(database))
|
|
91
|
+
return typesCurrent(database);
|
|
92
|
+
if (rulesOf(database) !== TYPE_RULES)
|
|
93
|
+
writeBatch(database, () => {
|
|
94
|
+
if (rulesOf(database) === TYPE_RULES)
|
|
95
|
+
return;
|
|
96
|
+
database
|
|
97
|
+
.prepare("UPDATE emails SET mail_type = NULL, reach = NULL, type_signal = NULL WHERE mail_type IS NOT NULL")
|
|
98
|
+
.run();
|
|
99
|
+
database
|
|
100
|
+
.prepare("INSERT INTO store_settings (key, value, at) VALUES (?, ?, ?) " +
|
|
101
|
+
"ON CONFLICT (key) DO UPDATE SET value = excluded.value, at = excluded.at")
|
|
102
|
+
.run(RULES_KEY, TYPE_RULES, Date.now());
|
|
103
|
+
});
|
|
104
|
+
const keep = database.prepare("UPDATE emails SET mail_type = ?, reach = ?, type_signal = ? WHERE id = ?");
|
|
105
|
+
const senders = new Map();
|
|
106
|
+
for (;;) {
|
|
107
|
+
const more = writeBatch(database, () => {
|
|
108
|
+
const ids = database
|
|
109
|
+
.prepare("SELECT id FROM emails WHERE mail_type IS NULL LIMIT ?")
|
|
110
|
+
.all(BATCH)
|
|
111
|
+
.map((row) => Number(row.id));
|
|
112
|
+
for (const [id, { type, reach, signal }] of typesOf(database, ids, senders))
|
|
113
|
+
keep.run(type, reach, signal, id);
|
|
114
|
+
return ids.length === BATCH;
|
|
115
|
+
});
|
|
116
|
+
if (!more)
|
|
117
|
+
return true;
|
|
118
|
+
}
|
|
119
|
+
};
|
|
120
|
+
/** Each email's stored type, or for one the store has not typed yet, the type worked out for this read. */
|
|
121
|
+
export const withTypes = (database, emails) => {
|
|
122
|
+
const missing = emails.filter((email) => email.mailType === null).map((email) => email.id);
|
|
123
|
+
if (missing.length === 0)
|
|
124
|
+
return emails;
|
|
125
|
+
const found = read(database, () => typesOf(database, missing));
|
|
126
|
+
return emails.map((email) => {
|
|
127
|
+
const typed = found.get(email.id);
|
|
128
|
+
return typed === undefined
|
|
129
|
+
? email
|
|
130
|
+
: { ...email, mailType: typed.type, reach: typed.reach, typeSignal: typed.signal };
|
|
131
|
+
});
|
|
132
|
+
};
|
|
133
|
+
const passes = (filter, type, reach) => (filter.types === undefined || filter.types.includes(type)) &&
|
|
134
|
+
(filter.reaches === undefined || filter.reaches.includes(reach));
|
|
135
|
+
/**
|
|
136
|
+
* The SQL of a filter by type and reach over `e`. Types are worked out and kept first; a query-only open that finds
|
|
137
|
+
* some not worked out works those out in memory, so a filtered list is never short.
|
|
138
|
+
*/
|
|
139
|
+
export const typeWhere = (database, filter) => {
|
|
140
|
+
if (filter.types === undefined && filter.reaches === undefined)
|
|
141
|
+
return { sql: [], params: [] };
|
|
142
|
+
const stored = [
|
|
143
|
+
...(filter.types === undefined ? [] : ["e.mail_type IN (SELECT value FROM json_each(?))"]),
|
|
144
|
+
...(filter.reaches === undefined ? [] : ["e.reach IN (SELECT value FROM json_each(?))"]),
|
|
145
|
+
].join(" AND ");
|
|
146
|
+
const params = [
|
|
147
|
+
...(filter.types === undefined ? [] : [JSON.stringify(filter.types)]),
|
|
148
|
+
...(filter.reaches === undefined ? [] : [JSON.stringify(filter.reaches)]),
|
|
149
|
+
];
|
|
150
|
+
if (storeMailTypes(database))
|
|
151
|
+
return { sql: [stored], params };
|
|
152
|
+
const pending = read(database, () => {
|
|
153
|
+
const ids = database
|
|
154
|
+
.prepare("SELECT id FROM emails WHERE mail_type IS NULL")
|
|
155
|
+
.all()
|
|
156
|
+
.map((row) => Number(row.id));
|
|
157
|
+
const kept = [];
|
|
158
|
+
const senders = new Map();
|
|
159
|
+
for (let at = 0; at < ids.length; at += BATCH)
|
|
160
|
+
for (const [id, typed] of typesOf(database, ids.slice(at, at + BATCH), senders))
|
|
161
|
+
if (passes(filter, typed.type, typed.reach))
|
|
162
|
+
kept.push(id);
|
|
163
|
+
return kept;
|
|
164
|
+
});
|
|
165
|
+
return {
|
|
166
|
+
sql: [`((${stored}) OR e.id IN (SELECT value FROM json_each(?)))`],
|
|
167
|
+
params: [...params, JSON.stringify(pending)],
|
|
168
|
+
};
|
|
169
|
+
};
|
|
170
|
+
const SENDER_ROWS = "SELECT e.id, lower(e.from_address) AS address, e.from_name AS name, e.mail_type, e.reach, " +
|
|
171
|
+
"coalesce(e.received_at, e.sent_at) AS at, b.bot_id, l.person_id " +
|
|
172
|
+
"FROM emails e LEFT JOIN sender_bot_identities b ON b.identity_id = e.from_identity_id " +
|
|
173
|
+
"LEFT JOIN identity_links l ON l.identity_id = e.from_identity_id AND b.bot_id IS NULL " +
|
|
174
|
+
"WHERE e.deleted_at IS NULL AND e.outgoing IS NOT 1 AND e.from_address IS NOT NULL";
|
|
175
|
+
/**
|
|
176
|
+
* Every sender of incoming mail with its mail counted by type and reach, most mail first. A sender is listed when
|
|
177
|
+
* any of its emails passes `types` and `reaches`, and its counts cover all its mail: Subscriptions shows a shop by
|
|
178
|
+
* its sales and still says it also sends receipts.
|
|
179
|
+
*/
|
|
180
|
+
export const mailSenders = (database, filter) => {
|
|
181
|
+
const complete = storeMailTypes(database);
|
|
182
|
+
const where = [];
|
|
183
|
+
const params = [];
|
|
184
|
+
const add = (sql, value) => {
|
|
185
|
+
if (value === undefined)
|
|
186
|
+
return;
|
|
187
|
+
where.push(sql);
|
|
188
|
+
params.push(value);
|
|
189
|
+
};
|
|
190
|
+
add("e.account_id = ?", filter.accountId);
|
|
191
|
+
add("coalesce(e.received_at, e.sent_at) >= ?", filter.since);
|
|
192
|
+
add("coalesce(e.received_at, e.sent_at) < ?", filter.until);
|
|
193
|
+
const rows = read(database, () => {
|
|
194
|
+
const all = database.prepare([SENDER_ROWS, ...where].join(" AND ")).all(...params);
|
|
195
|
+
if (complete)
|
|
196
|
+
return all;
|
|
197
|
+
const missing = all.filter((row) => row.mail_type == null).map((row) => Number(row.id));
|
|
198
|
+
const found = new Map();
|
|
199
|
+
const senders = new Map();
|
|
200
|
+
for (let at = 0; at < missing.length; at += BATCH)
|
|
201
|
+
for (const [id, typed] of typesOf(database, missing.slice(at, at + BATCH), senders))
|
|
202
|
+
found.set(id, typed);
|
|
203
|
+
return all.map((row) => {
|
|
204
|
+
const typed = found.get(Number(row.id));
|
|
205
|
+
return typed === undefined ? row : { ...row, mail_type: typed.type, reach: typed.reach };
|
|
206
|
+
});
|
|
207
|
+
});
|
|
208
|
+
const tallies = new Map();
|
|
209
|
+
for (const row of rows) {
|
|
210
|
+
const address = String(row.address);
|
|
211
|
+
const ref = row.bot_id != null
|
|
212
|
+
? `${SENDER_BOT_PREFIX}${row.bot_id}`
|
|
213
|
+
: row.person_id != null
|
|
214
|
+
? `person:${row.person_id}`
|
|
215
|
+
: null;
|
|
216
|
+
const key = ref ?? `address:${address}`;
|
|
217
|
+
const at = row.at == null ? null : Number(row.at);
|
|
218
|
+
const tally = tallies.get(key) ?? {
|
|
219
|
+
ref,
|
|
220
|
+
addresses: [],
|
|
221
|
+
name: null,
|
|
222
|
+
emails: 0,
|
|
223
|
+
types: {},
|
|
224
|
+
reaches: {},
|
|
225
|
+
lastAt: null,
|
|
226
|
+
matched: false,
|
|
227
|
+
};
|
|
228
|
+
const type = String(row.mail_type);
|
|
229
|
+
const reach = String(row.reach);
|
|
230
|
+
if (!tally.addresses.includes(address))
|
|
231
|
+
tally.addresses.push(address);
|
|
232
|
+
if (at !== null && (tally.lastAt === null || at >= tally.lastAt)) {
|
|
233
|
+
tally.lastAt = at;
|
|
234
|
+
tally.name = typeof row.name === "string" ? row.name : tally.name;
|
|
235
|
+
}
|
|
236
|
+
tally.emails += 1;
|
|
237
|
+
tally.types[type] = (tally.types[type] ?? 0) + 1;
|
|
238
|
+
tally.reaches[reach] = (tally.reaches[reach] ?? 0) + 1;
|
|
239
|
+
if (passes(filter, type, reach))
|
|
240
|
+
tally.matched = true;
|
|
241
|
+
tallies.set(key, tally);
|
|
242
|
+
}
|
|
243
|
+
const listed = [...tallies.values()]
|
|
244
|
+
.filter((tally) => tally.matched)
|
|
245
|
+
.sort((a, b) => b.emails - a.emails || (b.lastAt ?? 0) - (a.lastAt ?? 0))
|
|
246
|
+
.map(({ matched: _, ...tally }) => ({
|
|
247
|
+
...tally,
|
|
248
|
+
addresses: tally.addresses.sort(),
|
|
249
|
+
types: ordered(tally.types, MAIL_TYPES),
|
|
250
|
+
reaches: ordered(tally.reaches, REACHES),
|
|
251
|
+
}));
|
|
252
|
+
const offset = filter.offset ?? 0;
|
|
253
|
+
return {
|
|
254
|
+
items: listed.slice(offset, filter.limit === undefined ? undefined : offset + filter.limit),
|
|
255
|
+
total: listed.length,
|
|
256
|
+
};
|
|
257
|
+
};
|
|
258
|
+
const ordered = (counts, order) => Object.fromEntries(order.flatMap((key) => (counts[key] === undefined ? [] : [[key, counts[key]]])));
|
|
259
|
+
export const mailTypeStore = (database) => ({
|
|
260
|
+
mailSenders: async (filter = {}) => mailSenders(database, filter),
|
|
261
|
+
});
|
|
@@ -1,13 +1,16 @@
|
|
|
1
1
|
import type { Message } from "../../domain/models.js";
|
|
2
|
+
import type { MailType } from "../../mail/mail-types.js";
|
|
2
3
|
import type { Attachment, Metadata } from "../../meetings/index.js";
|
|
3
4
|
import type { Stemmer, Stemmers } from "../../search/stem.js";
|
|
4
5
|
import type { CacheDatabase } from "../driver.js";
|
|
5
6
|
import type { ConversationSummary, StoredHit } from "../types.js";
|
|
6
7
|
import { type MailActivityStore } from "./email-activity.js";
|
|
7
8
|
import { type EmailLocationInput, type FolderEmail, type PartialMailStore } from "./email-locations.js";
|
|
9
|
+
import { type MailTypeStore, type TypeFilter } from "./email-types.js";
|
|
8
10
|
import { type MailRulesStore } from "./mail-rules.js";
|
|
9
11
|
import { type AttachmentInput } from "./meetings.js";
|
|
10
12
|
import type { StoreContext } from "./open.js";
|
|
13
|
+
import type { Reach } from "./sender-signals.js";
|
|
11
14
|
import { type SyncStateSave } from "./sync.js";
|
|
12
15
|
import { type ThreadFilter } from "./thread-columns.js";
|
|
13
16
|
export type { EmailLocationInput, FolderEmail, PartialEmail } from "./email-locations.js";
|
|
@@ -115,6 +118,12 @@ export interface Email {
|
|
|
115
118
|
headers: Metadata;
|
|
116
119
|
metadata: Metadata;
|
|
117
120
|
mailRuleId: number | null;
|
|
121
|
+
/** What it is, apart from who sent it (docs/dev/GLOSSARY.md, "mail type"). @experimental */
|
|
122
|
+
mailType: MailType | null;
|
|
123
|
+
/** How it reached the owner. @experimental */
|
|
124
|
+
reach: Reach | null;
|
|
125
|
+
/** What decided `mailType`, such as `list-id`. @experimental */
|
|
126
|
+
typeSignal: string | null;
|
|
118
127
|
recipients: EmailRecipient[];
|
|
119
128
|
mailboxes: Mailbox[];
|
|
120
129
|
attachments: EmailAttachment[];
|
|
@@ -150,7 +159,7 @@ export interface MailFilter {
|
|
|
150
159
|
limit?: number;
|
|
151
160
|
offset?: number;
|
|
152
161
|
}
|
|
153
|
-
export interface EmailFilter extends MailFilter {
|
|
162
|
+
export interface EmailFilter extends MailFilter, TypeFilter {
|
|
154
163
|
threadExternalId?: string;
|
|
155
164
|
/** An address in From, To, Cc, Bcc or Reply-To. */
|
|
156
165
|
participant?: string;
|
|
@@ -206,7 +215,7 @@ export interface SenderScore {
|
|
|
206
215
|
demoted?: boolean;
|
|
207
216
|
}
|
|
208
217
|
/** Mail in tables of its own: threads, emails, recipients and mailboxes, keyed by account and Message-ID. */
|
|
209
|
-
export interface MailStore extends PartialMailStore, MailActivityStore {
|
|
218
|
+
export interface MailStore extends PartialMailStore, MailActivityStore, MailTypeStore {
|
|
210
219
|
saveThread(input: ThreadSave): Promise<ThreadDetails>;
|
|
211
220
|
/** Several threads and sync state in one transaction: a slice of mail commits with its resume point, or not at all. */
|
|
212
221
|
saveThreads(inputs: ThreadSave[], states?: SyncStateSave[]): Promise<ThreadDetails[]>;
|
|
@@ -5,6 +5,7 @@ import { mailActivity, threadEmailIds } from "./email-activity.js";
|
|
|
5
5
|
import { chunkAttachments, drainEmailIndex, dropAttachmentChunks, MAIL_PIECES, queueThreads, threadPieces, titleOf, } from "./email-index.js";
|
|
6
6
|
import { backfillSentAt, folderEmails, forgetLocations, locate, partialMail, setLocations, storedWhole, } from "./email-locations.js";
|
|
7
7
|
import { markEmailsDeleted, markEmailsRestored, markOwnMail, recountEmailThread as recount, setEmailFlags, } from "./email-state.js";
|
|
8
|
+
import { mailTypeStore, typeWhere, withTypes } from "./email-types.js";
|
|
8
9
|
import { EMAIL_ENRICHMENT, enrichmentText } from "./enrichments.js";
|
|
9
10
|
import { bool, flag, fromJson, int, page, str, toJson } from "./events.js";
|
|
10
11
|
import { folded } from "./folded.js";
|
|
@@ -93,6 +94,9 @@ const emailOf = (database, row) => ({
|
|
|
93
94
|
headers: fromJson(row.headers),
|
|
94
95
|
metadata: fromJson(row.metadata),
|
|
95
96
|
mailRuleId: int(row.mail_rule_id),
|
|
97
|
+
mailType: str(row.mail_type),
|
|
98
|
+
reach: str(row.reach),
|
|
99
|
+
typeSignal: str(row.type_signal),
|
|
96
100
|
recipients: database
|
|
97
101
|
.prepare("SELECT * FROM email_recipients WHERE email_id = ? ORDER BY position")
|
|
98
102
|
.all(Number(row.id))
|
|
@@ -290,7 +294,7 @@ const saveThread = (database, orm, input) => {
|
|
|
290
294
|
queueThreads(database, moved);
|
|
291
295
|
return threadId;
|
|
292
296
|
};
|
|
293
|
-
const emailWhere = (filter) => {
|
|
297
|
+
const emailWhere = (database, filter) => {
|
|
294
298
|
const where = ["(? OR e.deleted_at IS NULL)"];
|
|
295
299
|
const params = [filter.includeDeleted ? 1 : 0];
|
|
296
300
|
const add = (sql, ...values) => {
|
|
@@ -321,6 +325,11 @@ const emailWhere = (filter) => {
|
|
|
321
325
|
AND extraction_error IN (${filter.attachmentErrors.map(() => "?").join(",") || "NULL"}))`, ...filter.attachmentErrors);
|
|
322
326
|
if (filter.externalIds !== undefined)
|
|
323
327
|
add("e.external_id IN (SELECT value FROM json_each(?))", JSON.stringify(filter.externalIds));
|
|
328
|
+
const typed = typeWhere(database, filter);
|
|
329
|
+
for (const sql of typed.sql)
|
|
330
|
+
add(sql);
|
|
331
|
+
for (const value of typed.params)
|
|
332
|
+
params.push(value);
|
|
324
333
|
return [where.join(" AND "), params];
|
|
325
334
|
};
|
|
326
335
|
/** An email as a message of its thread, the shape mail had when it was stored as messages. */
|
|
@@ -450,19 +459,20 @@ export const mailStoreOver = ({ database, orm, now }, stemmerFor = stemmerCache(
|
|
|
450
459
|
const row = database
|
|
451
460
|
.prepare("SELECT * FROM emails WHERE account_id = ? AND external_id = ?")
|
|
452
461
|
.get(accountId, externalId);
|
|
453
|
-
return row ? emailOf(database, row) : null;
|
|
462
|
+
return row ? withTypes(database, [emailOf(database, row)])[0] : null;
|
|
454
463
|
},
|
|
455
464
|
async emails(filter = {}) {
|
|
456
465
|
const [limit, offset] = page(filter);
|
|
457
|
-
const [where, params] = emailWhere(filter);
|
|
458
|
-
|
|
466
|
+
const [where, params] = emailWhere(database, filter);
|
|
467
|
+
const emails = database
|
|
459
468
|
.prepare(`SELECT e.* FROM emails e WHERE ${where}
|
|
460
469
|
ORDER BY coalesce(e.sent_at, e.received_at) DESC, e.id DESC LIMIT ? OFFSET ?`)
|
|
461
470
|
.all(...params, limit, offset)
|
|
462
471
|
.map((row) => emailOf(database, row));
|
|
472
|
+
return withTypes(database, emails);
|
|
463
473
|
},
|
|
464
474
|
async emailRefs(filter = {}) {
|
|
465
|
-
const [where, params] = emailWhere(filter);
|
|
475
|
+
const [where, params] = emailWhere(database, filter);
|
|
466
476
|
return database
|
|
467
477
|
.prepare(`SELECT e.external_id, e.deleted_at IS NOT NULL AS deleted,
|
|
468
478
|
(SELECT json_group_array(json_object('externalId', b.external_id, 'kind', b.kind))
|
|
@@ -477,8 +487,9 @@ export const mailStoreOver = ({ database, orm, now }, stemmerFor = stemmerCache(
|
|
|
477
487
|
},
|
|
478
488
|
...partialMail(database),
|
|
479
489
|
...mailActivity(database),
|
|
490
|
+
...mailTypeStore(database),
|
|
480
491
|
async emailFacts(filter = {}) {
|
|
481
|
-
const [where, params] = emailWhere(filter);
|
|
492
|
+
const [where, params] = emailWhere(database, filter);
|
|
482
493
|
return database
|
|
483
494
|
.prepare(`SELECT e.external_id, e.from_address, e.from_name, e.subject, coalesce(e.received_at, e.sent_at) AS at, e.size,
|
|
484
495
|
e.read, e.flagged,
|
|
@@ -641,7 +652,7 @@ export const mailStoreOver = ({ database, orm, now }, stemmerFor = stemmerCache(
|
|
|
641
652
|
return [];
|
|
642
653
|
const [limit, offset] = page(filter);
|
|
643
654
|
drainEmailIndex(database, stemmerFor);
|
|
644
|
-
const [where, params] = emailWhere({ ...filter, includeDeleted: false });
|
|
655
|
+
const [where, params] = emailWhere(database, { ...filter, includeDeleted: false });
|
|
645
656
|
return database
|
|
646
657
|
.prepare(`SELECT e.* FROM email_words w JOIN emails e ON e.id = w.rowid
|
|
647
658
|
WHERE email_words MATCH ? AND ${where}
|
|
@@ -13,7 +13,10 @@ export interface SavedIdentity {
|
|
|
13
13
|
/** The profile differs from an earlier one written. */
|
|
14
14
|
revised: boolean;
|
|
15
15
|
}
|
|
16
|
-
/**
|
|
16
|
+
/**
|
|
17
|
+
* A new identity's own person, as ingest gives every identity; one already linked keeps its person, and a sender
|
|
18
|
+
* bot's has none.
|
|
19
|
+
*/
|
|
17
20
|
export declare const ensurePerson: (orm: Orm, identity: number, name: string | null, at: number) => void;
|
|
18
21
|
/** Every new identity gets its own person; linking two is a later, recorded act. */
|
|
19
22
|
export declare const identityOf: (context: StoreContext, provider: Provider, nativeId: Id, name: string | null, facts?: Facts) => number;
|
|
@@ -6,7 +6,7 @@ import { folded } from "./folded.js";
|
|
|
6
6
|
import { holdingText } from "./note-index.js";
|
|
7
7
|
import { resolvePersonLinks } from "./person-resolution.js";
|
|
8
8
|
import { bound } from "./query-filter.js";
|
|
9
|
-
import { accountIdentities, accounts, aliases, chatMembers, chats, identities, identityLinkEvents, identityLinks, identityRevisions, messages, persons, } from "./schema.js";
|
|
9
|
+
import { accountIdentities, accounts, aliases, chatMembers, chats, identities, identityLinkEvents, identityLinks, identityRevisions, messages, persons, senderBotIdentities, } from "./schema.js";
|
|
10
10
|
import { trigramPart } from "./trigram-match.js";
|
|
11
11
|
import { toIso } from "./values.js";
|
|
12
12
|
/** The source's every phone and address as a JSON patch of `metadata`; `undefined` when it gave no list. */
|
|
@@ -94,10 +94,19 @@ const revise = (orm, identity, next, at, before) => {
|
|
|
94
94
|
writeRevision(orm, identity, { ...next, marks }, at);
|
|
95
95
|
return last !== undefined;
|
|
96
96
|
};
|
|
97
|
-
/**
|
|
97
|
+
/**
|
|
98
|
+
* A new identity's own person, as ingest gives every identity; one already linked keeps its person, and a sender
|
|
99
|
+
* bot's has none.
|
|
100
|
+
*/
|
|
98
101
|
export const ensurePerson = (orm, identity, name, at) => {
|
|
99
102
|
if (orm.select({ id: identityLinks.identityId }).from(identityLinks).where(eq(identityLinks.identityId, identity)).get())
|
|
100
103
|
return;
|
|
104
|
+
if (orm
|
|
105
|
+
.select({ id: senderBotIdentities.identityId })
|
|
106
|
+
.from(senderBotIdentities)
|
|
107
|
+
.where(eq(senderBotIdentities.identityId, identity))
|
|
108
|
+
.get())
|
|
109
|
+
return;
|
|
101
110
|
const person = Number(orm.insert(persons).values({ name, createdAt: at, updatedAt: at }).returning({ pk: persons.id }).get()?.pk);
|
|
102
111
|
orm
|
|
103
112
|
.insert(identityLinks)
|
|
@@ -2,6 +2,7 @@ import { CliError } from "@wirecat/cli-base";
|
|
|
2
2
|
import { pointerPairs } from "./entity-types.js";
|
|
3
3
|
import { settleLinks } from "./identity-link-rows.js";
|
|
4
4
|
import { mergePerson } from "./person-links.js";
|
|
5
|
+
import { rekeyIntoBot } from "./sender-bots.js";
|
|
5
6
|
import { write } from "./transaction.js";
|
|
6
7
|
const quoted = (name) => `"${name.replace(/"/g, '""')}"`;
|
|
7
8
|
const columnsOf = (database, table) => database.prepare("SELECT name, pk, type FROM pragma_table_info(?)").all(table);
|
|
@@ -144,6 +145,7 @@ export const rekeyIdentity = (context, provider, { from, to, phone }, by) => {
|
|
|
144
145
|
return "renamed";
|
|
145
146
|
}
|
|
146
147
|
const keptId = Number(kept.id);
|
|
148
|
+
rekeyIntoBot(database, oldId, keptId, context.now());
|
|
147
149
|
const [mine, theirs] = [personOfIdentity(database, oldId), personOfIdentity(database, keptId)];
|
|
148
150
|
if (mine !== undefined && theirs !== undefined && mine !== theirs)
|
|
149
151
|
persons.push(mergePerson(context, mine, theirs, { method: "identity-rekey", by }));
|
|
@@ -249,6 +249,10 @@ export const MANIFEST = [
|
|
|
249
249
|
{ name: "20261123230507_version-73-autoincrement", version: 73, minCompatible: 73, tables: V73_TABLES },
|
|
250
250
|
{ name: "20261125025700_version-74-sender-bots", version: 74, minCompatible: 73 },
|
|
251
251
|
{ name: "20261125025709_version-74-sender-bot-triggers", version: 74, minCompatible: 73 },
|
|
252
|
+
{ name: "20261125111113_version-75-bot-identities", version: 75, minCompatible: 73 },
|
|
253
|
+
{ name: "20261125111118_version-75-bot-identities-moved", version: 75, minCompatible: 73 },
|
|
254
|
+
{ name: "20261125183530_version-76-mail-types", version: 76, minCompatible: 73 },
|
|
255
|
+
{ name: "20261125183555_version-76-mail-type-triggers", version: 76, minCompatible: 73 },
|
|
252
256
|
];
|
|
253
257
|
export const generatedMigrations = (manifest = MANIFEST, generated = GENERATED) => {
|
|
254
258
|
const migrations = [];
|
|
@@ -9,8 +9,9 @@ export declare const SEPARATIONS: readonly ["unlink", "split", "undo-merge"];
|
|
|
9
9
|
*/
|
|
10
10
|
export declare const mergeSql: (event: string) => string;
|
|
11
11
|
/**
|
|
12
|
-
* A person every identity left by a merge, so another person holds them now. The
|
|
13
|
-
* gives the uid back; a person added by name has no identity and no
|
|
12
|
+
* A person every identity left by a merge or a move to a sender bot, so another person or a bot holds them now. The
|
|
13
|
+
* row stays so that undoing the merge or the move gives the uid back; a person added by name has no identity and no
|
|
14
|
+
* move, and stays visible.
|
|
14
15
|
*/
|
|
15
16
|
export declare const mergedAwaySql: (person: string) => string;
|
|
16
17
|
/** The person `pk` is now: itself, or where its identities went, by the latest merge of each merged-away person. */
|
|
@@ -17,11 +17,13 @@ export const mergeSql = (event) => `(${event}.from_person_id IS NOT NULL AND ${e
|
|
|
17
17
|
`AND went.id = (SELECT min(w.id) FROM identity_link_events w WHERE w.identity_id = stay.identity_id ` +
|
|
18
18
|
`AND w.id > ${event}.id))))`;
|
|
19
19
|
/**
|
|
20
|
-
* A person every identity left by a merge, so another person holds them now. The
|
|
21
|
-
* gives the uid back; a person added by name has no identity and no
|
|
20
|
+
* A person every identity left by a merge or a move to a sender bot, so another person or a bot holds them now. The
|
|
21
|
+
* row stays so that undoing the merge or the move gives the uid back; a person added by name has no identity and no
|
|
22
|
+
* move, and stays visible.
|
|
22
23
|
*/
|
|
23
24
|
export const mergedAwaySql = (person) => `(${person}.owner = 0 AND NOT EXISTS (SELECT 1 FROM identity_links ml WHERE ml.person_id = ${person}.id) ` +
|
|
24
|
-
`AND EXISTS (SELECT 1 FROM identity_link_events me WHERE me.from_person_id = ${person}.id AND ${mergeSql("me")})
|
|
25
|
+
`AND (EXISTS (SELECT 1 FROM identity_link_events me WHERE me.from_person_id = ${person}.id AND ${mergeSql("me")}) ` +
|
|
26
|
+
`OR EXISTS (SELECT 1 FROM sender_bot_moves mb WHERE mb.from_person_id = ${person}.id AND mb.to_bot_id IS NOT NULL)))`;
|
|
25
27
|
const LATEST_MOVE = (person) => `(SELECT e.to_person_id FROM identity_link_events e WHERE e.from_person_id = ${person}.id AND ${mergeSql("e")}
|
|
26
28
|
ORDER BY e.id DESC LIMIT 1)`;
|
|
27
29
|
/** The person `pk` is now: itself, or where its identities went, by the latest merge of each merged-away person. */
|
|
@@ -2114,5 +2114,61 @@ export const GENERATED = [
|
|
|
2114
2114
|
"CREATE TRIGGER person_kind_sender_bot_au AFTER UPDATE OF person_id, author, name ON sender_bots BEGIN\n UPDATE persons SET kind = NULL WHERE kind IS NOT NULL AND id IN (OLD.person_id, NEW.person_id);\nEND;",
|
|
2115
2115
|
"CREATE TRIGGER person_kind_sender_bot_move_ai AFTER INSERT ON sender_bot_moves BEGIN\n UPDATE persons SET kind = NULL WHERE kind IS NOT NULL AND id IN (SELECT person_id FROM identity_links WHERE identity_id = NEW.identity_id);\nEND;"
|
|
2116
2116
|
]
|
|
2117
|
+
},
|
|
2118
|
+
{
|
|
2119
|
+
"name": "20261125111113_version-75-bot-identities",
|
|
2120
|
+
"statements": [
|
|
2121
|
+
"CREATE TABLE `sender_bot_identities` (\n\t`identity_id` integer PRIMARY KEY,\n\t`bot_id` integer NOT NULL,\n\t`created_at` integer NOT NULL\n);",
|
|
2122
|
+
"CREATE INDEX `sender_bot_identities_by_bot_id` ON `sender_bot_identities` (`bot_id`);",
|
|
2123
|
+
"CREATE INDEX `sender_bot_moves_by_from_person_id` ON `sender_bot_moves` (`from_person_id`);"
|
|
2124
|
+
]
|
|
2125
|
+
},
|
|
2126
|
+
{
|
|
2127
|
+
"name": "20261125111118_version-75-bot-identities-moved",
|
|
2128
|
+
"statements": [
|
|
2129
|
+
"-- A bot of version 74 is a person row holding the bot's identities. Each identity moves to the bot, the move is logged\n-- with the bot's author, and the person row stays, emptied and hidden, so a move back gives its uid back.\nINSERT INTO sender_bot_identities (identity_id, bot_id, created_at)\nSELECT l.identity_id, b.id, CAST(unixepoch('subsec') * 1000 AS INTEGER)\nFROM sender_bots b JOIN identity_links l ON l.person_id = b.person_id;",
|
|
2130
|
+
"INSERT INTO sender_bot_moves (identity_id, from_person_id, from_bot_id, to_person_id, to_bot_id, author, reason, created_at)\nSELECT s.identity_id, b.person_id, NULL, NULL, b.id, b.author, 'store version 75: a bot holds its own identities',\n CAST(unixepoch('subsec') * 1000 AS INTEGER)\nFROM sender_bot_identities s JOIN sender_bots b ON b.id = s.bot_id;",
|
|
2131
|
+
"DELETE FROM identity_links WHERE identity_id IN (SELECT identity_id FROM sender_bot_identities);",
|
|
2132
|
+
"-- Negative: a build before 75 reads a bot as its person row, and this names none.\nUPDATE sender_bots SET person_id = -id;",
|
|
2133
|
+
"-- Stored as a merge's emptied person is, so every build's people list leaves it out.\nCREATE TRIGGER person_kind_sender_bot_origin_au AFTER UPDATE OF kind ON persons\nWHEN NEW.kind IS NOT 'merged' AND NEW.owner = 0\n AND NOT EXISTS (SELECT 1 FROM identity_links WHERE person_id = NEW.id)\n AND EXISTS (SELECT 1 FROM sender_bot_moves WHERE from_person_id = NEW.id AND to_bot_id IS NOT NULL) BEGIN\n UPDATE persons SET kind = 'merged' WHERE id = NEW.id;\nEND;",
|
|
2134
|
+
"UPDATE persons SET kind = 'merged' WHERE owner = 0\n AND NOT EXISTS (SELECT 1 FROM identity_links WHERE person_id = persons.id)\n AND EXISTS (SELECT 1 FROM sender_bot_moves WHERE from_person_id = persons.id AND to_bot_id IS NOT NULL);",
|
|
2135
|
+
"-- An older build's sync links an identity with no person to a new one: a bot's identity gets none.\nCREATE TRIGGER sender_bot_identity_link_bi BEFORE INSERT ON identity_links\nWHEN EXISTS (SELECT 1 FROM sender_bot_identities WHERE identity_id = NEW.identity_id) BEGIN\n SELECT RAISE(IGNORE);\nEND;",
|
|
2136
|
+
"-- A re-key moves a person's link onto an identity a bot holds: the bot keeps it, and the move is logged.\nCREATE TRIGGER sender_bot_identity_link_au AFTER UPDATE OF identity_id ON identity_links\nWHEN EXISTS (SELECT 1 FROM sender_bot_identities WHERE identity_id = NEW.identity_id) BEGIN\n INSERT INTO sender_bot_moves (identity_id, from_person_id, from_bot_id, to_person_id, to_bot_id, author, reason, created_at)\n SELECT NEW.identity_id, NEW.person_id, NULL, NULL, s.bot_id, 'rules', 'the source re-keyed the identity',\n CAST(unixepoch('subsec') * 1000 AS INTEGER)\n FROM sender_bot_identities s WHERE s.identity_id = NEW.identity_id;\n DELETE FROM identity_links WHERE identity_id = NEW.identity_id;\nEND;",
|
|
2137
|
+
"-- ...and the person row it made for it, with its log row, goes again.\nCREATE TRIGGER sender_bot_identity_person_ai AFTER INSERT ON identity_link_events\nWHEN NEW.from_person_id IS NULL\n AND EXISTS (SELECT 1 FROM sender_bot_identities WHERE identity_id = NEW.identity_id)\n AND NOT EXISTS (SELECT 1 FROM identity_links WHERE person_id = NEW.to_person_id) BEGIN\n DELETE FROM identity_link_events WHERE id = NEW.id;\n DELETE FROM persons WHERE id = NEW.to_person_id AND owner = 0\n AND NOT EXISTS (SELECT 1 FROM identity_link_events WHERE from_person_id = NEW.to_person_id OR to_person_id = NEW.to_person_id);\nEND;",
|
|
2138
|
+
"CREATE TRIGGER sender_bot_identities_linked_bi BEFORE INSERT ON sender_bot_identities\nWHEN EXISTS (SELECT 1 FROM identity_links WHERE identity_id = NEW.identity_id) BEGIN\n SELECT RAISE(ABORT, 'sender_bot_identities: the identity is linked to a person; unlink it first');\nEND;",
|
|
2139
|
+
"-- An older build gives a bot back by deleting its row; one that still holds identities stays.\nCREATE TRIGGER sender_bots_holding_bd BEFORE DELETE ON sender_bots\nWHEN EXISTS (SELECT 1 FROM sender_bot_identities WHERE bot_id = OLD.id) BEGIN\n SELECT RAISE(IGNORE);\nEND;",
|
|
2140
|
+
"CREATE TRIGGER sender_bot_identities_identity_ad AFTER DELETE ON identities BEGIN\n DELETE FROM sender_bot_identities WHERE identity_id = OLD.id;\nEND;",
|
|
2141
|
+
"CREATE TRIGGER sender_bots_emptied_ad AFTER DELETE ON sender_bot_identities\nWHEN NOT EXISTS (SELECT 1 FROM sender_bot_identities WHERE bot_id = OLD.bot_id) BEGIN\n DELETE FROM sender_bots WHERE id = OLD.bot_id;\nEND;",
|
|
2142
|
+
"CREATE TRIGGER sender_bots_emptied_au AFTER UPDATE OF bot_id ON sender_bot_identities\nWHEN NOT EXISTS (SELECT 1 FROM sender_bot_identities WHERE bot_id = OLD.bot_id) BEGIN\n DELETE FROM sender_bots WHERE id = OLD.bot_id;\nEND;"
|
|
2143
|
+
]
|
|
2144
|
+
},
|
|
2145
|
+
{
|
|
2146
|
+
"name": "20261125183530_version-76-mail-types",
|
|
2147
|
+
"statements": [
|
|
2148
|
+
"ALTER TABLE `emails` ADD `mail_type` text;",
|
|
2149
|
+
"ALTER TABLE `emails` ADD `reach` text;",
|
|
2150
|
+
"ALTER TABLE `emails` ADD `type_signal` text;",
|
|
2151
|
+
"CREATE INDEX `emails_by_mail_type` ON `emails` (`mail_type`);",
|
|
2152
|
+
"CREATE INDEX `emails_type_to_work_out` ON `emails` (`id`) WHERE mail_type IS NULL;"
|
|
2153
|
+
]
|
|
2154
|
+
},
|
|
2155
|
+
{
|
|
2156
|
+
"name": "20261125183555_version-76-mail-type-triggers",
|
|
2157
|
+
"statements": [
|
|
2158
|
+
"CREATE TRIGGER mail_type_email_au AFTER UPDATE OF from_address, from_identity_id, subject, headers, metadata, outgoing, body_html ON emails\nWHEN OLD.from_address IS NOT NEW.from_address OR OLD.from_identity_id IS NOT NEW.from_identity_id OR OLD.subject IS NOT NEW.subject OR OLD.headers IS NOT NEW.headers OR OLD.metadata IS NOT NEW.metadata OR OLD.outgoing IS NOT NEW.outgoing OR OLD.body_html IS NOT NEW.body_html BEGIN\n UPDATE emails SET mail_type = NULL, reach = NULL, type_signal = NULL WHERE mail_type IS NOT NULL AND id = NEW.id;\nEND;",
|
|
2159
|
+
"CREATE TRIGGER mail_type_outgoing_au AFTER UPDATE OF outgoing ON emails\nWHEN NEW.outgoing IS 1 AND OLD.outgoing IS NOT 1 BEGIN\n UPDATE emails SET mail_type = NULL, reach = NULL, type_signal = NULL WHERE mail_type IS NOT NULL AND from_identity_id IN (SELECT identity_id FROM email_recipients WHERE email_id = NEW.id AND identity_id IS NOT NULL);\nEND;",
|
|
2160
|
+
"CREATE TRIGGER mail_type_list_ai AFTER INSERT ON emails\nWHEN coalesce(json_extract(NEW.headers, '$.\"list-id\"'), json_extract(NEW.metadata, '$.listId')) IS NOT NULL AND NOT EXISTS (SELECT 1 FROM emails o WHERE coalesce(json_extract(o.headers, '$.\"list-id\"'), json_extract(o.metadata, '$.listId')) = coalesce(json_extract(NEW.headers, '$.\"list-id\"'), json_extract(NEW.metadata, '$.listId')) AND o.from_address IS NEW.from_address AND o.id <> NEW.id) BEGIN\n UPDATE emails SET mail_type = NULL, reach = NULL, type_signal = NULL WHERE mail_type IS NOT NULL AND coalesce(json_extract(headers, '$.\"list-id\"'), json_extract(metadata, '$.listId')) = coalesce(json_extract(NEW.headers, '$.\"list-id\"'), json_extract(NEW.metadata, '$.listId'));\nEND;",
|
|
2161
|
+
"CREATE TRIGGER mail_type_recipient_ai AFTER INSERT ON email_recipients BEGIN\n UPDATE emails SET mail_type = NULL, reach = NULL, type_signal = NULL WHERE mail_type IS NOT NULL AND id = NEW.email_id;\n UPDATE emails SET mail_type = NULL, reach = NULL, type_signal = NULL WHERE mail_type IS NOT NULL AND NEW.identity_id IS NOT NULL AND from_identity_id = NEW.identity_id\n AND (SELECT outgoing FROM emails WHERE id = NEW.email_id) IS 1\n AND NOT EXISTS (SELECT 1 FROM email_recipients r JOIN emails o ON o.id = r.email_id WHERE r.identity_id = NEW.identity_id AND o.outgoing = 1 AND r.email_id <> NEW.email_id);\nEND;",
|
|
2162
|
+
"CREATE TRIGGER mail_type_recipient_ad AFTER DELETE ON email_recipients BEGIN\n UPDATE emails SET mail_type = NULL, reach = NULL, type_signal = NULL WHERE mail_type IS NOT NULL AND id = OLD.email_id;\nEND;",
|
|
2163
|
+
"CREATE TRIGGER mail_type_recipient_au AFTER UPDATE OF email_id, identity_id, address, role ON email_recipients\nWHEN OLD.email_id IS NOT NEW.email_id OR OLD.identity_id IS NOT NEW.identity_id OR OLD.address IS NOT NEW.address OR OLD.role IS NOT NEW.role BEGIN\n UPDATE emails SET mail_type = NULL, reach = NULL, type_signal = NULL WHERE mail_type IS NOT NULL AND id IN (OLD.email_id, NEW.email_id);\nEND;",
|
|
2164
|
+
"CREATE TRIGGER mail_type_attachment_ai AFTER INSERT ON attachments\nWHEN NEW.attachable_type = 'email' AND lower(NEW.mime) IN ('text/calendar', 'application/ics', 'message/delivery-status', 'message/disposition-notification') BEGIN\n UPDATE emails SET mail_type = NULL, reach = NULL, type_signal = NULL WHERE mail_type IS NOT NULL AND id = NEW.attachable_id;\nEND;",
|
|
2165
|
+
"CREATE TRIGGER mail_type_attachment_ad AFTER DELETE ON attachments\nWHEN OLD.attachable_type = 'email' AND lower(OLD.mime) IN ('text/calendar', 'application/ics', 'message/delivery-status', 'message/disposition-notification') BEGIN\n UPDATE emails SET mail_type = NULL, reach = NULL, type_signal = NULL WHERE mail_type IS NOT NULL AND id = OLD.attachable_id;\nEND;",
|
|
2166
|
+
"CREATE TRIGGER mail_type_attachment_au AFTER UPDATE OF mime ON attachments\nWHEN NEW.attachable_type = 'email' AND OLD.mime IS NOT NEW.mime BEGIN\n UPDATE emails SET mail_type = NULL, reach = NULL, type_signal = NULL WHERE mail_type IS NOT NULL AND id = NEW.attachable_id;\nEND;",
|
|
2167
|
+
"CREATE TRIGGER mail_type_bot_ai AFTER INSERT ON sender_bot_identities BEGIN\n UPDATE emails SET mail_type = NULL, reach = NULL, type_signal = NULL WHERE mail_type IS NOT NULL AND from_identity_id = NEW.identity_id;\nEND;",
|
|
2168
|
+
"CREATE TRIGGER mail_type_bot_ad AFTER DELETE ON sender_bot_identities BEGIN\n UPDATE emails SET mail_type = NULL, reach = NULL, type_signal = NULL WHERE mail_type IS NOT NULL AND from_identity_id = OLD.identity_id;\nEND;",
|
|
2169
|
+
"CREATE TRIGGER mail_type_organization_ai AFTER INSERT ON organization_identities BEGIN\n UPDATE emails SET mail_type = NULL, reach = NULL, type_signal = NULL WHERE mail_type IS NOT NULL AND from_identity_id = NEW.identity_id;\nEND;",
|
|
2170
|
+
"CREATE TRIGGER mail_type_organization_ad AFTER DELETE ON organization_identities BEGIN\n UPDATE emails SET mail_type = NULL, reach = NULL, type_signal = NULL WHERE mail_type IS NOT NULL AND from_identity_id = OLD.identity_id;\nEND;",
|
|
2171
|
+
"CREATE TRIGGER mail_type_sector_au AFTER UPDATE OF metadata, deleted_at ON organizations\nWHEN json_extract(OLD.metadata, '$.sector') IS NOT json_extract(NEW.metadata, '$.sector') OR OLD.deleted_at IS NOT NEW.deleted_at BEGIN\n UPDATE emails SET mail_type = NULL, reach = NULL, type_signal = NULL WHERE mail_type IS NOT NULL AND from_identity_id IN (SELECT identity_id FROM organization_identities WHERE organization_id = NEW.id);\nEND;"
|
|
2172
|
+
]
|
|
2117
2173
|
}
|
|
2118
2174
|
];
|
|
@@ -38,13 +38,8 @@ export interface ListedPerson {
|
|
|
38
38
|
messagesFrom: number;
|
|
39
39
|
/** The newest of their messages, their mail and their one-to-one chat's last message, as ISO time. */
|
|
40
40
|
lastContact: string | null;
|
|
41
|
-
/** Why they are shown; empty for someone hidden by default
|
|
41
|
+
/** Why they are shown; empty for someone hidden by default. */
|
|
42
42
|
ties: ShownBecause[];
|
|
43
|
-
/**
|
|
44
|
-
* The sender bot this row is (`sender-bot:<id>`), listed as `automated` under its person row's ref; `null` for a
|
|
45
|
-
* person.
|
|
46
|
-
*/
|
|
47
|
-
bot: string | null;
|
|
48
43
|
/** How their messages reached the owner (`senderSignalsOf`); `null` when nothing says. Worked out for the page. */
|
|
49
44
|
reach: Reach | null;
|
|
50
45
|
/** What their mail and addresses show about who writes and how it reached the owner. Worked out for the page. */
|