@wirecat/cli-core 0.638.0 → 0.640.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 +48 -0
- package/dist/domain/crm.d.ts +3 -0
- package/dist/domain/name-variants.d.ts +2 -0
- package/dist/domain/name-variants.js +107 -0
- package/dist/domain/person-kinds.d.ts +6 -3
- package/dist/domain/person-kinds.js +5 -2
- package/dist/entities/contact-activity.d.ts +3 -0
- package/dist/entities/contact-activity.js +3 -0
- package/dist/entities/contact-context.d.ts +6 -0
- package/dist/entities/index.d.ts +6 -0
- package/dist/errors/reasons.js +2 -1
- package/dist/resolve.d.ts +8 -0
- package/dist/resolve.js +48 -6
- package/dist/search/lucene/registry.js +17 -15
- package/dist/services/agenda.js +8 -1
- package/dist/services/message-resolution.d.ts +12 -0
- package/dist/services/message-resolution.js +42 -1
- package/dist/services/messages-search.js +29 -15
- package/dist/services/metadata.d.ts +1 -1
- package/dist/services/task-rules.js +1 -1
- package/dist/store/index.d.ts +2 -2
- package/dist/store/people.d.ts +10 -3
- package/dist/store/people.js +2 -1
- package/dist/store/sqlite/accounts.js +2 -0
- package/dist/store/sqlite/crm.js +5 -0
- package/dist/store/sqlite/hit-places.js +19 -6
- package/dist/store/sqlite/identities.js +21 -1
- package/dist/store/sqlite/knowledge.js +5 -2
- package/dist/store/sqlite/lucene-fields.d.ts +2 -1
- package/dist/store/sqlite/lucene-fields.js +76 -16
- package/dist/store/sqlite/lucene.js +8 -8
- package/dist/store/sqlite/note-search.js +9 -0
- package/dist/store/sqlite/people-list.js +14 -8
- package/dist/store/sqlite/person-doubts.d.ts +13 -3
- package/dist/store/sqlite/person-doubts.js +92 -48
- package/dist/store/sqlite/person-links.js +15 -2
- package/dist/store/sqlite/task-board.d.ts +4 -2
- package/dist/store/sqlite/task-board.js +5 -3
- package/dist/store/sqlite/task-stats.js +2 -2
- package/dist/store/sqlite/tasks.js +20 -3
- package/dist/store/sqlite/test-people.d.ts +24 -0
- package/dist/store/sqlite/test-people.js +92 -0
- package/dist/store/sqlite/work-tasks.d.ts +7 -3
- package/dist/store/sqlite/work-tasks.js +9 -7
- package/dist/store/types.d.ts +6 -0
- package/docs/search/recipes.json +3 -0
- package/package.json +1 -1
|
@@ -8,6 +8,7 @@ import { fold } from "../normalize.js";
|
|
|
8
8
|
import { mergedAwaySql } from "./merged-persons.js";
|
|
9
9
|
import { ALWAYS_SHOWN, shownReasons } from "./person-ties.js";
|
|
10
10
|
import { mergedAssignees } from "./task-board.js";
|
|
11
|
+
import { hiddenTestPeople } from "./test-people.js";
|
|
11
12
|
import { read, writeBatch } from "./transaction.js";
|
|
12
13
|
export const PEOPLE_SORTS = ["interactions", "recent", "name", "tasks"];
|
|
13
14
|
const MIN_QUERY = 3;
|
|
@@ -110,7 +111,9 @@ const lastContacts = (database, scope) => new Map(database
|
|
|
110
111
|
/** One misfiled letter is no newsletter. */
|
|
111
112
|
const BULK_MIN = 3;
|
|
112
113
|
/** The first rule that holds; the list's contract in the people-clients-digests plan names the same order. */
|
|
113
|
-
const kindOf = (shown, facts, ties, mail) => {
|
|
114
|
+
const kindOf = (shown, facts, ties, mail, test) => {
|
|
115
|
+
if (test)
|
|
116
|
+
return { kind: "test", signals: ["test-data"] };
|
|
114
117
|
const always = ties.filter((tie) => ALWAYS_SHOWN.includes(tie));
|
|
115
118
|
if (always.length > 0)
|
|
116
119
|
return { kind: "person", signals: ties };
|
|
@@ -140,6 +143,7 @@ const kindOf = (shown, facts, ties, mail) => {
|
|
|
140
143
|
/** Each person with their kind, contact and counts; the last contact is added for the page alone. */
|
|
141
144
|
const classified = (database, people, scope, reasons = shownReasons(database, scope === "all" ? undefined : scope)) => {
|
|
142
145
|
const facts = factsOf(database, scope);
|
|
146
|
+
const test = hiddenTestPeople(database);
|
|
143
147
|
const tasks = countsOf(database, TASK_COUNTS, mergedAssignees(database));
|
|
144
148
|
const messages = countsOf(database, MESSAGE_COUNTS(scope), ...scopeParams(scope));
|
|
145
149
|
const mail = new Map(database
|
|
@@ -151,7 +155,7 @@ const classified = (database, people, scope, reasons = shownReasons(database, sc
|
|
|
151
155
|
const contact = { username: own?.username ?? null, phone: own?.phone ?? null, email: own?.email ?? null };
|
|
152
156
|
const shown = displayName(name, { ...contact, name: own?.name ?? null });
|
|
153
157
|
const ties = reasons.get(id) ?? [];
|
|
154
|
-
const { kind, signals } = kindOf(shown, own, ties, mail.get(id));
|
|
158
|
+
const { kind, signals } = kindOf(shown, own, ties, mail.get(id), test.has(id));
|
|
155
159
|
return {
|
|
156
160
|
id,
|
|
157
161
|
nameOrder: nameOrderOf(shown),
|
|
@@ -185,7 +189,7 @@ const byName = (a, b) => a.nameOrder < b.nameOrder ? -1 : a.nameOrder > b.nameOr
|
|
|
185
189
|
* Bump when what decides a kind, a name's order or a count changes in code (the word lists, `BULK_MIN`, the ties):
|
|
186
190
|
* every stored one is worked out again on the next list.
|
|
187
191
|
*/
|
|
188
|
-
const KIND_RULES = "
|
|
192
|
+
const KIND_RULES = "4";
|
|
189
193
|
const RULES_KEY = "people.kind_rules";
|
|
190
194
|
/** Past this many people to work out, one pass over everyone beats a lookup per person. */
|
|
191
195
|
const WHOLE_PASS = 20_000;
|
|
@@ -288,6 +292,8 @@ const storedCounts = (database, filter) => {
|
|
|
288
292
|
counts[row.kind] = Number(row.n);
|
|
289
293
|
return counts;
|
|
290
294
|
};
|
|
295
|
+
/** Everyone but test data, which `all` leaves out as every list does. */
|
|
296
|
+
const allOf = (counts) => Object.entries(counts).reduce((sum, [kind, n]) => (kind === "test" ? sum : sum + n), 0);
|
|
291
297
|
const checked = (options) => {
|
|
292
298
|
const { kind = "person", sort = "interactions" } = options;
|
|
293
299
|
if (kind !== "all" && !PERSON_KINDS.includes(kind))
|
|
@@ -309,11 +315,11 @@ export const listPeople = (database, options) => {
|
|
|
309
315
|
const { limit, offset = 0 } = options;
|
|
310
316
|
return read(database, () => {
|
|
311
317
|
const counts = storedCounts(database, filter);
|
|
312
|
-
const total = kind === "all" ?
|
|
318
|
+
const total = kind === "all" ? allOf(counts) : counts[kind];
|
|
313
319
|
const rows = database
|
|
314
|
-
.prepare(`SELECT p.id, p.name, p.owner, p.last_contact FROM persons p WHERE p.kind ${kind === "all" ? "
|
|
320
|
+
.prepare(`SELECT p.id, p.name, p.owner, p.last_contact FROM persons p WHERE p.kind ${kind === "all" ? "NOT IN (?, ?)" : "= ?"}` +
|
|
315
321
|
`${filter.sql} ORDER BY p.owner DESC, ${STORED_ORDER[sort]}p.name_order, p.id LIMIT ? OFFSET ?`)
|
|
316
|
-
.all(kind === "all" ? MERGED : kind, ...filter.params, limit, offset);
|
|
322
|
+
.all(...(kind === "all" ? [MERGED, "test"] : [kind]), ...filter.params, limit, offset);
|
|
317
323
|
const page = rows.map(candidateOf);
|
|
318
324
|
const contacts = new Map(rows.filter((row) => row.last_contact != null).map((row) => [Number(row.id), Number(row.last_contact)]));
|
|
319
325
|
return {
|
|
@@ -337,7 +343,7 @@ const listInMemory = (database, options) => {
|
|
|
337
343
|
const counts = noCounts();
|
|
338
344
|
for (const person of everyone)
|
|
339
345
|
counts[person.kind] += 1;
|
|
340
|
-
const chosen = kind === "all" ?
|
|
346
|
+
const chosen = everyone.filter((person) => (kind === "all" ? person.kind !== "test" : person.kind === kind));
|
|
341
347
|
// The newest contact of every person reads every message; only this order needs it.
|
|
342
348
|
const recent = sort === "recent" ? lastContacts(database, "all") : undefined;
|
|
343
349
|
const order = sort === "name"
|
|
@@ -425,7 +431,7 @@ export const similarPeople = (database, typed, limit) => {
|
|
|
425
431
|
}));
|
|
426
432
|
const byId = new Map(people.map((person) => [person.id, person]));
|
|
427
433
|
const ordered = [...sameName.keys()].flatMap((id) => byId.get(id) ?? []);
|
|
428
|
-
const rows = classified(database, ordered, [...sameName.keys()], ties);
|
|
434
|
+
const rows = classified(database, ordered, [...sameName.keys()], ties).filter(({ kind }) => kind !== "test");
|
|
429
435
|
const same = rows.map(({ id }) => sameName.get(id) === true);
|
|
430
436
|
return withLastContact(database, rows).map((person, at) => ({ ...person, sameName: same[at] === true }));
|
|
431
437
|
};
|
|
@@ -27,7 +27,7 @@ export interface PersonDoubt {
|
|
|
27
27
|
/** The same moves as `fix`, one each, for an app that runs them without a command line. */
|
|
28
28
|
moves: DoubtMove[];
|
|
29
29
|
}
|
|
30
|
-
/** Undo a merge,
|
|
30
|
+
/** Undo a merge, move identities to one new person, or call the person test data, which hides them. */
|
|
31
31
|
export type DoubtMove = {
|
|
32
32
|
op: "unmerge";
|
|
33
33
|
merge: string;
|
|
@@ -37,6 +37,9 @@ export type DoubtMove = {
|
|
|
37
37
|
op: "unlink";
|
|
38
38
|
identities: string[];
|
|
39
39
|
from: string;
|
|
40
|
+
} | {
|
|
41
|
+
op: "test-data";
|
|
42
|
+
person: string;
|
|
40
43
|
};
|
|
41
44
|
export interface PersonDoubts {
|
|
42
45
|
items: PersonDoubt[];
|
|
@@ -53,8 +56,15 @@ export interface KeptPerson {
|
|
|
53
56
|
/** The identities the owner said belong here; a doubt about them comes back once one of them moves. */
|
|
54
57
|
kept: string[];
|
|
55
58
|
}
|
|
59
|
+
export interface TestDataPerson {
|
|
60
|
+
person: string;
|
|
61
|
+
/** The identities the owner called test data: the person is hidden from every list until called real. */
|
|
62
|
+
identities: string[];
|
|
63
|
+
}
|
|
56
64
|
/**
|
|
57
|
-
* The owner's answer to a doubt: this person is right as they are
|
|
58
|
-
* when nothing about the person is in doubt.
|
|
65
|
+
* The owner's answer to a doubt: this person is right as they are, and what looked like test data is a real contact,
|
|
66
|
+
* which also brings back a person memo hid as test data. Refused when nothing about the person is in doubt or hidden.
|
|
59
67
|
*/
|
|
60
68
|
export declare const keepPerson: (context: StoreContext, uid: string, by: Decider) => KeptPerson;
|
|
69
|
+
/** The owner's answer to a doubt: the person is test data, hidden from every list until the owner calls them real. */
|
|
70
|
+
export declare const markTestData: (context: StoreContext, uid: string, by: Decider) => TestDataPerson;
|
|
@@ -1,14 +1,16 @@
|
|
|
1
1
|
import { CliError } from "@wirecat/cli-base";
|
|
2
2
|
import { displayName } from "../../domain/display-names.js";
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
3
|
+
import { FIRST_NAMES } from "../../domain/first-names.js";
|
|
4
|
+
import { fullNames, nameSkeletons, wordSkeleton } from "../../domain/name-variants.js";
|
|
5
|
+
import { foldName } from "../../resolve.js";
|
|
5
6
|
import { keepTogether, keptTogether } from "./identity-split-proposals.js";
|
|
6
7
|
import { currentPerson } from "./merged-persons.js";
|
|
7
8
|
import { merges, ownerOnly } from "./person-merges.js";
|
|
9
|
+
import { hiddenTestPeople, judgeTestData, testIdentitySql, testVerdicts } from "./test-people.js";
|
|
8
10
|
/** Why a person may be wrong, most urgent first: the owner's own person holding someone else matters most. */
|
|
9
11
|
export const DOUBT_REASONS = ["owner-other-name", "test-data", "shared-address", "mixed-names"];
|
|
10
12
|
const COLUMNS = "l.person_id AS person, p.owner, p.name AS person_name, i.id AS pk, i.provider, i.external_id, i.name, i.phone, " +
|
|
11
|
-
"i.email, l.method, l.author, l.created_at, EXISTS (SELECT 1 FROM accounts a WHERE a.provider = i.provider " +
|
|
13
|
+
"i.email, i.username, l.method, l.author, l.created_at, EXISTS (SELECT 1 FROM accounts a WHERE a.provider = i.provider " +
|
|
12
14
|
"AND a.external_id = i.external_id) AS account FROM identity_links l JOIN identities i ON i.id = l.identity_id " +
|
|
13
15
|
"JOIN persons p ON p.id = l.person_id";
|
|
14
16
|
const text = (value) => (typeof value === "string" && value.trim() !== "" ? value : null);
|
|
@@ -21,6 +23,7 @@ const heldOf = (row) => ({
|
|
|
21
23
|
name: text(row.name),
|
|
22
24
|
phone: text(row.phone)?.replace(/\D/gu, "") || null,
|
|
23
25
|
email: text(row.email)?.toLowerCase() ?? null,
|
|
26
|
+
username: text(row.username),
|
|
24
27
|
account: Number(row.account) === 1,
|
|
25
28
|
method: String(row.method),
|
|
26
29
|
by: String(row.author),
|
|
@@ -32,36 +35,41 @@ const sharedPeople = (database) => database
|
|
|
32
35
|
"ORDER BY l.person_id, i.id")
|
|
33
36
|
.all()
|
|
34
37
|
.map(heldOf);
|
|
35
|
-
/**
|
|
36
|
-
* Identities a test run left: the seed's word ids, an address at a reserved domain, or a seed name with its username
|
|
37
|
-
* or beside another seed name in one provider, since one name alone may be a real person's.
|
|
38
|
-
*/
|
|
38
|
+
/** Identities a test run left, the owner's word aside. */
|
|
39
39
|
const testData = (database) => {
|
|
40
|
-
const
|
|
41
|
-
const usernames = JSON.stringify(SEED_PEOPLE.flatMap(({ username }) => (username === null ? [] : [username])));
|
|
42
|
-
const globs = (column, patterns) => patterns.map((pattern) => `${column} GLOB '${pattern}'`).join(" OR ");
|
|
40
|
+
const { where, sure, params } = testIdentitySql();
|
|
43
41
|
return database
|
|
44
|
-
.prepare(`SELECT ${COLUMNS} WHERE ${
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
"AND j.name IN (SELECT value FROM json_each(?))) >= 2)) ORDER BY l.person_id, i.id")
|
|
48
|
-
.all(names, usernames, names)
|
|
49
|
-
.map(heldOf);
|
|
42
|
+
.prepare(`SELECT ${sure}, ${COLUMNS} WHERE ${where} ORDER BY l.person_id, i.id`)
|
|
43
|
+
.all(...params)
|
|
44
|
+
.map((row) => ({ ...heldOf(row), sure: Number(row.sure) === 1 }));
|
|
50
45
|
};
|
|
51
|
-
|
|
52
|
-
|
|
46
|
+
const FIRST = new Set(FIRST_NAMES.map(wordSkeleton));
|
|
47
|
+
/** «Ivan» at ivan@… is still a name; «msx04x» at msx04x@… is not. */
|
|
48
|
+
const isFirstName = (name) => nameSkeletons(name).some((word) => FIRST.has(word));
|
|
49
|
+
const bare = (text) => foldName(text).replace(/[^\p{L}\p{N}]/gu, "");
|
|
50
|
+
/**
|
|
51
|
+
* A name's words, script, spelling and short forms aside; none for an address or a placeholder, which say nothing of a
|
|
52
|
+
* name, nor for a name that only repeats the identity's own mailbox or username and is no first name, as Drive and
|
|
53
|
+
* mail show «msx04x» for msx04x@….
|
|
54
|
+
*/
|
|
55
|
+
const wordsOf = (name, handles = []) => {
|
|
53
56
|
const shown = displayName(name);
|
|
54
57
|
if (shown === null || shown.includes("@"))
|
|
55
58
|
return new Set();
|
|
56
|
-
|
|
59
|
+
if (handles.some((handle) => handle !== null && bare(handle) === bare(shown)) && !isFirstName(shown))
|
|
60
|
+
return new Set();
|
|
61
|
+
return new Set(nameSkeletons(shown)
|
|
62
|
+
.filter((word) => word.length >= 2 && !/^\d+$/u.test(word))
|
|
63
|
+
.flatMap(fullNames));
|
|
57
64
|
};
|
|
65
|
+
const heldWords = ({ name, email, username }) => wordsOf(name, [email?.split("@")[0] ?? null, username]);
|
|
58
66
|
const sharesWord = (one, other) => [...one].some((word) => other.has(word));
|
|
59
67
|
/**
|
|
60
68
|
* The identities grouped by names that share a word, the person's own first: the most identities, counting double what
|
|
61
69
|
* the owner linked by hand, an account the owner connected and the person's own name.
|
|
62
70
|
*/
|
|
63
71
|
const nameGroups = (held) => {
|
|
64
|
-
const named = held.map((one) => ({ one, words:
|
|
72
|
+
const named = held.map((one) => ({ one, words: heldWords(one) })).filter(({ words }) => words.size > 0);
|
|
65
73
|
const group = named.map((_, index) => index);
|
|
66
74
|
const root = (at) => {
|
|
67
75
|
let top = at;
|
|
@@ -94,7 +102,7 @@ const nameGroups = (held) => {
|
|
|
94
102
|
}
|
|
95
103
|
const own = wordsOf(held[0]?.personName ?? null);
|
|
96
104
|
const weight = (members) => members.reduce((sum, one) => sum + 1 + (one.by === "owner" ? 2 : 0) + (one.account ? 2 : 0), 0) +
|
|
97
|
-
(members.some((
|
|
105
|
+
(members.some((one) => sharesWord(heldWords(one), own)) ? 2 : 0);
|
|
98
106
|
return [...groups.values()].sort((a, b) => weight(b) - weight(a) || a[0].pk - b[0].pk);
|
|
99
107
|
};
|
|
100
108
|
const quoted = (held) => [...new Set(held.map(({ name, ref }) => name ?? ref))].map((name) => `«${name}»`).join(", ");
|
|
@@ -154,24 +162,36 @@ const byPerson = (held) => {
|
|
|
154
162
|
return found;
|
|
155
163
|
};
|
|
156
164
|
const ORDER = new Map(DOUBT_REASONS.map((reason, index) => [reason, index]));
|
|
165
|
+
/** The identities a test run left that the owner has not called real, and the people memo hides as test data. */
|
|
166
|
+
const testState = (database) => {
|
|
167
|
+
const verdicts = testVerdicts(database);
|
|
168
|
+
const test = testData(database).filter(({ ref }) => verdicts.get(ref) !== false);
|
|
169
|
+
return { verdicts, test, pks: new Set(test.map(({ pk }) => pk)), hidden: hiddenTestPeople(database) };
|
|
170
|
+
};
|
|
157
171
|
/** Everything in doubt, the owner first, then by reason and person. A few thousand people are read, never every row. */
|
|
158
172
|
const everyDoubt = (database) => {
|
|
159
|
-
const test =
|
|
160
|
-
const testPks = new Set(test.map(({ pk }) => pk));
|
|
173
|
+
const { verdicts, test, pks, hidden } = testState(database);
|
|
161
174
|
const kept = keptTogether(database);
|
|
162
175
|
const found = [];
|
|
163
176
|
for (const [person, held] of byPerson(sharedPeople(database)))
|
|
164
|
-
for (const one of doubtsOf(held, kept.get(person) ?? new Set(),
|
|
177
|
+
for (const one of doubtsOf(held, kept.get(person) ?? new Set(), pks))
|
|
165
178
|
found.push(one);
|
|
166
|
-
|
|
167
|
-
|
|
179
|
+
const holds = database.prepare("SELECT count(*) AS n FROM identity_links WHERE person_id = ?");
|
|
180
|
+
for (const [person, held] of byPerson(test)) {
|
|
181
|
+
if (hidden.has(person))
|
|
182
|
+
continue;
|
|
183
|
+
const open = held.filter(({ ref }) => !verdicts.has(ref));
|
|
184
|
+
const [first] = open;
|
|
185
|
+
if (first === undefined)
|
|
186
|
+
continue;
|
|
168
187
|
found.push({
|
|
169
|
-
person
|
|
188
|
+
person,
|
|
170
189
|
owner: first.owner,
|
|
171
190
|
name: first.personName,
|
|
172
191
|
reason: "test-data",
|
|
173
|
-
held,
|
|
192
|
+
held: open,
|
|
174
193
|
doubted: [],
|
|
194
|
+
whole: !first.owner && Number(holds.get(person)?.n) === held.length,
|
|
175
195
|
});
|
|
176
196
|
}
|
|
177
197
|
return found.sort((a, b) => Number(b.owner) - Number(a.owner) ||
|
|
@@ -183,8 +203,12 @@ const everyDoubt = (database) => {
|
|
|
183
203
|
* unlink per name group of the rest, so two strangers on one person do not land on one new person together.
|
|
184
204
|
*/
|
|
185
205
|
const movesOf = (context, found) => {
|
|
186
|
-
if (found.reason === "
|
|
206
|
+
if (found.reason === "mixed-names")
|
|
187
207
|
return [];
|
|
208
|
+
if (found.reason === "test-data")
|
|
209
|
+
return found.whole
|
|
210
|
+
? [{ op: "test-data", person: `person:${found.person}` }]
|
|
211
|
+
: [{ op: "unlink", identities: found.held.map(({ ref }) => ref), from: `person:${found.person}` }];
|
|
188
212
|
const doubted = new Set(found.held.map(({ ref }) => ref));
|
|
189
213
|
const moves = [];
|
|
190
214
|
const unmerged = new Set();
|
|
@@ -203,7 +227,9 @@ const movesOf = (context, found) => {
|
|
|
203
227
|
};
|
|
204
228
|
const commandOf = (move) => move.op === "unmerge"
|
|
205
229
|
? `people unmerge ${move.merge}`
|
|
206
|
-
:
|
|
230
|
+
: move.op === "test-data"
|
|
231
|
+
? `people links resolve ${move.person} test`
|
|
232
|
+
: `people unlink ${move.identities.map(shellWord).join(" ")} --from ${move.from}`;
|
|
207
233
|
/** A ref as one shell word: an id may hold a space or a quote, and the fix is pasted into a terminal. */
|
|
208
234
|
const shellWord = (ref) => (/^[\w@.:+/=-]+$/u.test(ref) ? ref : `'${ref.replaceAll("'", "'\\''")}'`);
|
|
209
235
|
const shown = (context, found) => {
|
|
@@ -243,31 +269,49 @@ export const personDoubts = (context, { limit, offset }) => {
|
|
|
243
269
|
hasMore: found.length > offset + limit,
|
|
244
270
|
};
|
|
245
271
|
};
|
|
246
|
-
|
|
247
|
-
* The owner's answer to a doubt: this person is right as they are. Test data is not a link and stays listed. Refused
|
|
248
|
-
* when nothing about the person is in doubt.
|
|
249
|
-
*/
|
|
250
|
-
export const keepPerson = (context, uid, by) => {
|
|
251
|
-
ownerOnly(by, "says a person's links are right");
|
|
272
|
+
const personFor = (context, uid) => {
|
|
252
273
|
const asked = /^(?:person:)?([1-9]\d*)$/u.exec(uid.trim())?.[1];
|
|
253
274
|
const known = asked === undefined || !Number.isSafeInteger(Number(asked))
|
|
254
275
|
? undefined
|
|
255
276
|
: context.database.prepare("SELECT id FROM persons WHERE id = ?").get(Number(asked));
|
|
256
277
|
if (known === undefined)
|
|
257
278
|
throw new CliError("not_found", `the store has no person for "${uid}"`, { reason: "person.unknown" });
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
279
|
+
return currentPerson(context.database, Number(known.id));
|
|
280
|
+
};
|
|
281
|
+
const heldOn = (context, person) => context.database.prepare(`SELECT ${COLUMNS} WHERE l.person_id = ? ORDER BY i.id`).all(person).map(heldOf);
|
|
282
|
+
/**
|
|
283
|
+
* The owner's answer to a doubt: this person is right as they are, and what looked like test data is a real contact,
|
|
284
|
+
* which also brings back a person memo hid as test data. Refused when nothing about the person is in doubt or hidden.
|
|
285
|
+
*/
|
|
286
|
+
export const keepPerson = (context, uid, by) => {
|
|
287
|
+
ownerOnly(by, "says a person's links are right");
|
|
288
|
+
const person = personFor(context, uid);
|
|
289
|
+
const held = heldOn(context, person);
|
|
290
|
+
const { verdicts, pks } = testState(context.database);
|
|
261
291
|
const doubted = [
|
|
262
|
-
...new Set(doubtsOf(held, keptTogether(context.database).get(person) ?? new Set(),
|
|
292
|
+
...new Set(doubtsOf(held, keptTogether(context.database).get(person) ?? new Set(), pks).flatMap((found) => found.doubted.map(({ ref }) => ref))),
|
|
263
293
|
];
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
throw new CliError("validation_error", `person:${person} is in doubt only as test data, which is not kept`, {
|
|
267
|
-
reason: "person.test-data",
|
|
268
|
-
});
|
|
294
|
+
const real = held.filter(({ pk, ref }) => pks.has(pk) || verdicts.get(ref) === true).map(({ ref }) => ref);
|
|
295
|
+
if (doubted.length === 0 && real.length === 0)
|
|
269
296
|
throw new CliError("not_found", `nothing about person:${person} is in doubt`, { reason: "person.no-doubt" });
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
297
|
+
if (doubted.length > 0)
|
|
298
|
+
keepTogether(context, person, doubted);
|
|
299
|
+
if (real.length > 0)
|
|
300
|
+
judgeTestData(context, person, real, false);
|
|
301
|
+
return { person: `person:${person}`, kept: [...new Set([...doubted, ...real])] };
|
|
302
|
+
};
|
|
303
|
+
/** The owner's answer to a doubt: the person is test data, hidden from every list until the owner calls them real. */
|
|
304
|
+
export const markTestData = (context, uid, by) => {
|
|
305
|
+
ownerOnly(by, "calls a person test data");
|
|
306
|
+
const person = personFor(context, uid);
|
|
307
|
+
const held = heldOn(context, person);
|
|
308
|
+
if (held[0]?.owner === true)
|
|
309
|
+
throw new CliError("validation_error", "your own person is not test data", { reason: "person.owner" });
|
|
310
|
+
if (held.length === 0)
|
|
311
|
+
throw new CliError("validation_error", `person:${person} has no identity to call test data`, {
|
|
312
|
+
reason: "person.no-identity",
|
|
313
|
+
});
|
|
314
|
+
const identities = held.map(({ ref }) => ref);
|
|
315
|
+
judgeTestData(context, person, identities, true);
|
|
316
|
+
return { person: `person:${person}`, identities };
|
|
273
317
|
};
|
|
@@ -21,6 +21,14 @@ export const required = (context, ref) => {
|
|
|
21
21
|
throw new CliError("not_found", `no ${ref.provider} person ${ref.id} in the store`);
|
|
22
22
|
return found;
|
|
23
23
|
};
|
|
24
|
+
/** Every phone and address an identity holds: the source's lists, else the one `phone` and `email`. */
|
|
25
|
+
const contactListsOf = ({ phone, email }, metadata) => {
|
|
26
|
+
const stored = metadata === null ? {} : JSON.parse(metadata);
|
|
27
|
+
const list = (values, one) => [
|
|
28
|
+
...new Set([...(one === null ? [] : [one]), ...(Array.isArray(values) ? values.map(String) : [])]),
|
|
29
|
+
];
|
|
30
|
+
return { phones: list(stored.phones, phone), emails: list(stored.emails, email) };
|
|
31
|
+
};
|
|
24
32
|
export const recordOf = ({ orm }, personPk) => {
|
|
25
33
|
const person = orm.select().from(persons).where(eq(persons.id, personPk)).get();
|
|
26
34
|
if (!person)
|
|
@@ -34,6 +42,7 @@ export const recordOf = ({ orm }, personPk) => {
|
|
|
34
42
|
username: identities.username,
|
|
35
43
|
phone: identities.phone,
|
|
36
44
|
email: identities.email,
|
|
45
|
+
metadata: identities.metadata,
|
|
37
46
|
isBot: identities.bot,
|
|
38
47
|
method: identityLinks.method,
|
|
39
48
|
linkedBy: identityLinks.author,
|
|
@@ -50,19 +59,23 @@ export const recordOf = ({ orm }, personPk) => {
|
|
|
50
59
|
.innerJoin(accounts, eq(accounts.id, accountIdentities.accountId))
|
|
51
60
|
.where(inArray(accountIdentities.identityId, rows.map((row) => row.pk)))
|
|
52
61
|
.all();
|
|
53
|
-
const identitiesOf = rows.map(({ pk, isBot, linkedAt, ...row }) => {
|
|
62
|
+
const identitiesOf = rows.map(({ pk, isBot, linkedAt, metadata, ...row }) => {
|
|
54
63
|
const seenIn = seen
|
|
55
64
|
.filter((one) => one.identityPk === pk)
|
|
56
65
|
.map(({ account, scope }) => ({ account, scope: scope }));
|
|
57
66
|
return {
|
|
58
67
|
...row,
|
|
68
|
+
...contactListsOf(row, metadata),
|
|
59
69
|
isBot: isBot === null ? null : isBot === 1,
|
|
60
70
|
accounts: seenIn.map((one) => one.account),
|
|
61
71
|
seenIn,
|
|
62
72
|
linkedAt: new Date(linkedAt).toISOString(),
|
|
63
73
|
};
|
|
64
74
|
});
|
|
65
|
-
const fallbacks = [...rows]
|
|
75
|
+
const fallbacks = [...rows]
|
|
76
|
+
.map(({ metadata: _, ...row }) => row)
|
|
77
|
+
.sort((a, b) => a.pk - b.pk)
|
|
78
|
+
.reduce(takeFallbacks, noFallbacks());
|
|
66
79
|
return {
|
|
67
80
|
uid: String(person.id),
|
|
68
81
|
name: person.name,
|
|
@@ -18,8 +18,10 @@ export declare const dueGroups: (now: number, zone: string) => {
|
|
|
18
18
|
export type DueGroup = keyof ReturnType<typeof dueGroups>;
|
|
19
19
|
/** `now` and `zone` place today; one value per answer, so the list and its counts agree. */
|
|
20
20
|
export declare const projectTaskColumns: (now: number, zone: string, database: CacheDatabase) => ColumnMap;
|
|
21
|
-
/** Live tasks of the owner's projects
|
|
22
|
-
export declare const
|
|
21
|
+
/** Live tasks of the owner's projects, without the tasks found in messages. */
|
|
22
|
+
export declare const PROJECT_SCOPE = "t.deleted_at IS NULL AND t.project_id IN (SELECT id FROM projects WHERE deleted_at IS NULL AND length(key) <> 10)";
|
|
23
|
+
/** Every live task: the owner's projects and the tasks found in messages, which wait in each account's inbox. */
|
|
24
|
+
export declare const BOARD_SCOPE = "t.deleted_at IS NULL AND t.project_id IN (SELECT id FROM projects WHERE deleted_at IS NULL)";
|
|
23
25
|
/** The chosen order first, then priority, age and id, so a page boundary never splits a tie. */
|
|
24
26
|
export declare const boardOrder: (sort: TaskSort, descending: boolean) => string;
|
|
25
27
|
/** The query without its required terms on `name`: a picker counts the choices its own filter hides. */
|
|
@@ -62,7 +62,7 @@ const wordsMatch = (node) => {
|
|
|
62
62
|
};
|
|
63
63
|
const projectWhere = (node) => {
|
|
64
64
|
if (node.operator === "wildcard" && node.value === "*")
|
|
65
|
-
return { sql:
|
|
65
|
+
return { sql: `t.project_id IN (${OWNER_PROJECTS})`, params: [] };
|
|
66
66
|
const id = /^project:(\d+)$/.exec(node.value.trim())?.[1];
|
|
67
67
|
if (id !== undefined)
|
|
68
68
|
return { sql: "t.project_id = ?", params: [Number(id)] };
|
|
@@ -213,8 +213,10 @@ export const projectTaskColumns = (now, zone, database) => ({
|
|
|
213
213
|
},
|
|
214
214
|
},
|
|
215
215
|
});
|
|
216
|
-
/** Live tasks of the owner's projects
|
|
217
|
-
export const
|
|
216
|
+
/** Live tasks of the owner's projects, without the tasks found in messages. */
|
|
217
|
+
export const PROJECT_SCOPE = `t.deleted_at IS NULL AND t.project_id IN (${OWNER_PROJECTS})`;
|
|
218
|
+
/** Every live task: the owner's projects and the tasks found in messages, which wait in each account's inbox. */
|
|
219
|
+
export const BOARD_SCOPE = "t.deleted_at IS NULL AND t.project_id IN (SELECT id FROM projects WHERE deleted_at IS NULL)";
|
|
218
220
|
const STATE_ORDER = `CASE t.status ${TASK_STATES.map((state, at) => `WHEN '${state}' THEN ${at}`).join(" ")} END`;
|
|
219
221
|
/** The chosen order first, then priority, age and id, so a page boundary never splits a tie. */
|
|
220
222
|
export const boardOrder = (sort, descending) => {
|
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { PROJECT_SCOPE } from "./task-board.js";
|
|
2
2
|
const ACCOUNT_SCOPE = "t.package_id IS NOT NULL AND t.deleted_at IS NULL AND t.project_id = ?";
|
|
3
3
|
/** Every statement `stats tasks show` runs, for the query-plan test. */
|
|
4
4
|
export const TASK_STATS_SQL = {
|
|
5
5
|
rows: (board, filter) => "SELECT t.source_group, t.source_kind, t.project_id, t.status, t.created_at, t.closed_at FROM tasks t " +
|
|
6
|
-
`WHERE ${board ?
|
|
6
|
+
`WHERE ${board ? PROJECT_SCOPE : ACCOUNT_SCOPE}${filter} ORDER BY t.created_at, t.id`,
|
|
7
7
|
chat: "SELECT c.external_id AS id, c.title AS name FROM chats c JOIN accounts a ON a.id = c.account_id " +
|
|
8
8
|
"WHERE a.provider = ? AND a.external_id = ? AND c.external_id IN (SELECT value FROM json_each(?))",
|
|
9
9
|
meeting: "SELECT CAST(id AS TEXT) AS id, title AS name FROM meetings WHERE id IN (SELECT value FROM json_each(?))",
|
|
@@ -9,7 +9,7 @@ import { taskSimilarityOver } from "./task-candidates.js";
|
|
|
9
9
|
import { savedTaskCategories, saveTaskCategories, storeTaskCategories, unlistedTaskCategories, } from "./task-category-settings.js";
|
|
10
10
|
import { CLEAR_SUGGESTION, closeSuggestionOf, SUGGESTION_COLUMNS } from "./task-close.js";
|
|
11
11
|
import { taskCloseOver } from "./task-close-store.js";
|
|
12
|
-
import { inboxKey, typedKey } from "./task-keys.js";
|
|
12
|
+
import { INBOX_KEY_LENGTH, inboxKey, typedKey } from "./task-keys.js";
|
|
13
13
|
import { taskPairsOver } from "./task-pairs.js";
|
|
14
14
|
import { taskStatsOver } from "./task-stats.js";
|
|
15
15
|
import { displayTitleOf } from "./task-titles.js";
|
|
@@ -48,8 +48,17 @@ export const inboxProject = ({ database, now }, account, create) => {
|
|
|
48
48
|
export const taskStoreOver = (context) => {
|
|
49
49
|
const { database, now } = context;
|
|
50
50
|
const inbox = (account, create) => inboxProject(context, account, create);
|
|
51
|
-
// A task made here has no package id and may be a type the package does not know; the package never sees it
|
|
52
|
-
|
|
51
|
+
// A task made here has no package id and may be a type the package does not know; the package never sees it,
|
|
52
|
+
// nor a found task the owner moved into a project.
|
|
53
|
+
const select = (where) => database.prepare(`SELECT ${COLUMNS} FROM tasks t JOIN projects p ON p.id = t.project_id WHERE t.package_id IS NOT NULL ` +
|
|
54
|
+
`AND length(p.key) = ${INBOX_KEY_LENGTH} AND ${where}`);
|
|
55
|
+
/** The message kind a task was found as, though it may be a bug or a chore in its project now. */
|
|
56
|
+
const kindFound = (row) => TASK_KINDS.includes(row.type)
|
|
57
|
+
? row.type
|
|
58
|
+
: database
|
|
59
|
+
.prepare("SELECT json_extract(changes, '$.type[0]') AS kind FROM task_events WHERE task_id = ? AND kind = 'type' " +
|
|
60
|
+
"ORDER BY id LIMIT 1")
|
|
61
|
+
.get(Number(row.row_id))?.kind;
|
|
53
62
|
const toTask = (row) => {
|
|
54
63
|
const task = {
|
|
55
64
|
id: String(row.package_id),
|
|
@@ -138,6 +147,14 @@ export const taskStoreOver = (context) => {
|
|
|
138
147
|
.map(toTask);
|
|
139
148
|
if (!linked || own.length > 0)
|
|
140
149
|
return own;
|
|
150
|
+
// A rule must not find a message again once its task moved into a project, so that task still counts as seen.
|
|
151
|
+
const moved = database
|
|
152
|
+
.prepare(`SELECT ${COLUMNS} FROM tasks t JOIN projects p ON p.id = t.project_id WHERE t.package_id IS NOT NULL ` +
|
|
153
|
+
"AND t.source_locator = ? AND EXISTS (SELECT 1 FROM task_events e WHERE e.task_id = t.id " +
|
|
154
|
+
"AND e.kind = 'project' AND json_extract(e.changes, '$.project[0]') = ?) ORDER BY t.created_at, t.id")
|
|
155
|
+
.all(source, `project:${project}`);
|
|
156
|
+
if (moved.length > 0)
|
|
157
|
+
return moved.map((row) => toTask({ ...row, account, type: kindFound(row) }));
|
|
141
158
|
let message;
|
|
142
159
|
try {
|
|
143
160
|
message = thingOf(database, source);
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import type { CacheDatabase } from "../driver.js";
|
|
2
|
+
import type { StoreContext } from "./open.js";
|
|
3
|
+
/** The owner's word on identities: approved, they are test data; rejected, a real contact. */
|
|
4
|
+
export declare const TEST_DATA_KIND = "test_data";
|
|
5
|
+
/** The SQL that picks a test run's identities, aliased `i`, and its parameters; `sure` is a column of it. */
|
|
6
|
+
export declare const testIdentitySql: () => {
|
|
7
|
+
where: string;
|
|
8
|
+
sure: string;
|
|
9
|
+
params: string[];
|
|
10
|
+
};
|
|
11
|
+
/** The owner's newest word on each identity: `true` test data, `false` a real contact. */
|
|
12
|
+
export declare const testVerdicts: (database: CacheDatabase) => Map<string, boolean>;
|
|
13
|
+
/**
|
|
14
|
+
* People memo keeps out of every list as test data: each identity is a sure test run's or one the owner called test
|
|
15
|
+
* data, and the owner called none a real contact. Never the owner.
|
|
16
|
+
*/
|
|
17
|
+
export declare const hiddenTestPeople: (database: CacheDatabase) => Set<number>;
|
|
18
|
+
/**
|
|
19
|
+
* Records the owner's word on the identities: test data, or a real contact. The next people list works the person's
|
|
20
|
+
* kind out again, since no trigger watches a decision.
|
|
21
|
+
*/
|
|
22
|
+
export declare const judgeTestData: (context: StoreContext, person: number, identities: readonly string[], test: boolean) => void;
|
|
23
|
+
/** Deletes the owner's words on identities no longer in the store: a payload names the address. */
|
|
24
|
+
export declare const forgetErasedTestData: (database: CacheDatabase) => void;
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
import { RESERVED_ADDRESS_GLOBS, SEED_ID_GLOBS, SEED_PEOPLE } from "../../domain/test-data.js";
|
|
2
|
+
import { personOf, pkOf } from "./identity-link-rows.js";
|
|
3
|
+
import { proposalOperationsOver } from "./proposed-actions.js";
|
|
4
|
+
/** The owner's word on identities: approved, they are test data; rejected, a real contact. */
|
|
5
|
+
export const TEST_DATA_KIND = "test_data";
|
|
6
|
+
const globs = (column, patterns) => patterns.map((pattern) => `${column} GLOB '${pattern}'`).join(" OR ");
|
|
7
|
+
/** A seed id or an address at a reserved domain: nothing real has one, so memo hides it without asking. */
|
|
8
|
+
const SURE = `(${globs("i.external_id", SEED_ID_GLOBS)} OR ${globs("lower(i.email)", RESERVED_ADDRESS_GLOBS)})`;
|
|
9
|
+
/** A seed name with its username or beside another seed name in one provider: one name alone may be a real person's. */
|
|
10
|
+
const SEED_NAMED = "(i.name IN (SELECT value FROM json_each(?)) AND (i.username IN (SELECT value FROM json_each(?)) " +
|
|
11
|
+
"OR (SELECT count(DISTINCT j.name) FROM identities j WHERE j.provider = i.provider " +
|
|
12
|
+
"AND j.name IN (SELECT value FROM json_each(?))) >= 2))";
|
|
13
|
+
const seedParams = () => {
|
|
14
|
+
const names = JSON.stringify(SEED_PEOPLE.map(({ name }) => name));
|
|
15
|
+
const usernames = JSON.stringify(SEED_PEOPLE.flatMap(({ username }) => (username === null ? [] : [username])));
|
|
16
|
+
return [names, usernames, names];
|
|
17
|
+
};
|
|
18
|
+
/** The SQL that picks a test run's identities, aliased `i`, and its parameters; `sure` is a column of it. */
|
|
19
|
+
export const testIdentitySql = () => ({
|
|
20
|
+
where: `(${SURE} OR ${SEED_NAMED})`,
|
|
21
|
+
sure: `${SURE} AS sure`,
|
|
22
|
+
params: seedParams(),
|
|
23
|
+
});
|
|
24
|
+
/** The owner's newest word on each identity: `true` test data, `false` a real contact. */
|
|
25
|
+
export const testVerdicts = (database) => {
|
|
26
|
+
const verdicts = new Map();
|
|
27
|
+
for (const row of database
|
|
28
|
+
.prepare("SELECT status, payload FROM proposed_actions WHERE kind = ? AND status IN ('approved', 'rejected') ORDER BY id")
|
|
29
|
+
.all(TEST_DATA_KIND))
|
|
30
|
+
for (const ref of JSON.parse(String(row.payload)).identities)
|
|
31
|
+
verdicts.set(ref, row.status === "approved");
|
|
32
|
+
return verdicts;
|
|
33
|
+
};
|
|
34
|
+
/**
|
|
35
|
+
* People memo keeps out of every list as test data: each identity is a sure test run's or one the owner called test
|
|
36
|
+
* data, and the owner called none a real contact. Never the owner.
|
|
37
|
+
*/
|
|
38
|
+
export const hiddenTestPeople = (database) => {
|
|
39
|
+
const { where, sure, params } = testIdentitySql();
|
|
40
|
+
const verdicts = testVerdicts(database);
|
|
41
|
+
const test = new Map();
|
|
42
|
+
const note = (person, ref, isSure) => {
|
|
43
|
+
const refs = test.get(person) ?? new Map();
|
|
44
|
+
refs.set(ref, isSure || refs.get(ref) === true);
|
|
45
|
+
test.set(person, refs);
|
|
46
|
+
};
|
|
47
|
+
for (const row of database
|
|
48
|
+
.prepare(`SELECT l.person_id AS person, i.provider || ':' || i.external_id AS ref, ${sure} FROM identity_links l ` +
|
|
49
|
+
`JOIN identities i ON i.id = l.identity_id WHERE ${where}`)
|
|
50
|
+
.all(...params))
|
|
51
|
+
note(Number(row.person), String(row.ref), Number(row.sure) === 1);
|
|
52
|
+
for (const [ref, isTest] of verdicts) {
|
|
53
|
+
if (!isTest)
|
|
54
|
+
continue;
|
|
55
|
+
const person = personOf(database, pkOf(database, ref));
|
|
56
|
+
if (person !== undefined)
|
|
57
|
+
note(person, ref, false);
|
|
58
|
+
}
|
|
59
|
+
const hidden = new Set();
|
|
60
|
+
const held = database.prepare("SELECT count(*) AS n, max(p.owner) AS owner FROM identity_links l JOIN persons p ON p.id = l.person_id " +
|
|
61
|
+
"WHERE l.person_id = ?");
|
|
62
|
+
for (const [person, refs] of test) {
|
|
63
|
+
const told = [...refs].every(([ref, isSure]) => verdicts.get(ref) ?? isSure);
|
|
64
|
+
if (!told)
|
|
65
|
+
continue;
|
|
66
|
+
const row = held.get(person);
|
|
67
|
+
if (Number(row?.owner) !== 1 && Number(row?.n) === refs.size)
|
|
68
|
+
hidden.add(person);
|
|
69
|
+
}
|
|
70
|
+
return hidden;
|
|
71
|
+
};
|
|
72
|
+
/**
|
|
73
|
+
* Records the owner's word on the identities: test data, or a real contact. The next people list works the person's
|
|
74
|
+
* kind out again, since no trigger watches a decision.
|
|
75
|
+
*/
|
|
76
|
+
export const judgeTestData = (context, person, identities, test) => {
|
|
77
|
+
const operations = proposalOperationsOver(context);
|
|
78
|
+
const payload = { person: String(person), identities: [...identities] };
|
|
79
|
+
const reason = test ? "the owner called it test data" : "the owner called it a real contact";
|
|
80
|
+
const proposed = operations.propose({ kind: TEST_DATA_KIND, by: "owner", payload, reason });
|
|
81
|
+
if (test)
|
|
82
|
+
operations.approve(proposed.id, { reason });
|
|
83
|
+
else
|
|
84
|
+
operations.reject(proposed.id, "owner", { reason });
|
|
85
|
+
context.database.prepare("UPDATE persons SET kind = NULL WHERE id = ?").run(person);
|
|
86
|
+
};
|
|
87
|
+
/** Deletes the owner's words on identities no longer in the store: a payload names the address. */
|
|
88
|
+
export const forgetErasedTestData = (database) => {
|
|
89
|
+
for (const row of database.prepare("SELECT id, payload FROM proposed_actions WHERE kind = ?").all(TEST_DATA_KIND))
|
|
90
|
+
if (JSON.parse(String(row.payload)).identities.some((ref) => pkOf(database, ref) === undefined))
|
|
91
|
+
database.prepare("DELETE FROM proposed_actions WHERE id = ?").run(Number(row.id));
|
|
92
|
+
};
|