@wirecat/cli-core 0.618.0 → 0.620.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 +453 -1
- package/README.md +1 -1
- package/THIRD_PARTY_NOTICES +3 -0
- package/dist/background/deliver.d.ts +5 -2
- package/dist/background/deliver.js +30 -12
- package/dist/background/report/render.d.ts +3 -1
- package/dist/background/report/render.js +3 -1
- package/dist/calendar/cli/answers.d.ts +1 -0
- package/dist/calendar/cli/answers.js +6 -1
- package/dist/calendar/cli/index.js +2 -14
- package/dist/calendar/cli/people-link.d.ts +3 -0
- package/dist/calendar/cli/people-link.js +23 -0
- package/dist/calendar/cli/search-all.d.ts +1 -1
- package/dist/calendar/cli/search-all.js +9 -10
- package/dist/calendar/index.d.ts +1 -1
- package/dist/calendar/index.js +1 -1
- package/dist/calendar/mcp/index.d.ts +12 -12
- package/dist/calendar/prepare.js +1 -0
- package/dist/cli/base-stages-command.d.ts +22 -0
- package/dist/cli/base-stages-command.js +174 -0
- package/dist/cli/bot/buttons.d.ts +3 -0
- package/dist/cli/bot/buttons.js +24 -0
- package/dist/cli/bot/messages.js +14 -1
- package/dist/cli/bot/port.d.ts +8 -0
- package/dist/cli/command-runs.d.ts +2 -0
- package/dist/cli/command-runs.js +3 -1
- package/dist/cli/config-command.js +6 -1
- package/dist/cli/context.js +1 -0
- package/dist/cli/contract-templates.js +31 -2
- package/dist/cli/group-lock.d.ts +42 -0
- package/dist/cli/group-lock.js +130 -0
- package/dist/cli/index.d.ts +7 -2
- package/dist/cli/index.js +7 -2
- package/dist/cli/messenger/backfill-command.js +27 -15
- package/dist/cli/messenger/contacts-command.js +12 -4
- package/dist/cli/messenger/context.js +1 -0
- package/dist/cli/messenger/conversations-command.js +2 -1
- package/dist/cli/messenger/doctor-command.js +3 -1
- package/dist/cli/messenger/message-items.js +1 -1
- package/dist/cli/messenger/models-command.js +1 -1
- package/dist/cli/messenger/search-answers.d.ts +5 -5
- package/dist/cli/messenger/search-answers.js +9 -20
- package/dist/cli/messenger/search-command.d.ts +2 -2
- package/dist/cli/messenger/search-command.js +8 -18
- package/dist/cli/messenger/store-maintenance-command.d.ts +22 -2
- package/dist/cli/messenger/store-maintenance-command.js +9 -4
- package/dist/cli/model-catalogue-command.d.ts +3 -1
- package/dist/cli/model-catalogue-command.js +48 -2
- package/dist/cli/profile.d.ts +8 -0
- package/dist/cli/profile.js +25 -0
- package/dist/cli/program.js +2 -0
- package/dist/cli/runs/search-query.d.ts +1 -1
- package/dist/cli/runs/search-query.js +4 -0
- package/dist/cli/runs/search.d.ts +5 -1
- package/dist/cli/runs/search.js +12 -1
- package/dist/cli/search-harder-consents-command.d.ts +18 -0
- package/dist/cli/search-harder-consents-command.js +83 -0
- package/dist/cli/search-left-out.d.ts +6 -1
- package/dist/cli/search-left-out.js +9 -4
- package/dist/cli/settings.d.ts +8 -0
- package/dist/cli/settings.js +17 -2
- package/dist/cli/shared-command-exports.d.ts +16 -0
- package/dist/cli/steps.js +0 -6
- package/dist/cli/store-recover.d.ts +3 -3
- package/dist/cli/store-recover.js +26 -26
- package/dist/cli/store-settings.d.ts +2 -0
- package/dist/cli/store-settings.js +37 -9
- package/dist/cli/templates-command.js +4 -4
- package/dist/crm/cli/index.js +40 -8
- package/dist/crm/cli/search-all.js +7 -6
- package/dist/crm/group-hint.d.ts +8 -0
- package/dist/crm/group-hint.js +13 -0
- package/dist/crm/source.d.ts +12 -1
- package/dist/crm/sync.d.ts +31 -2
- package/dist/crm/sync.js +110 -3
- package/dist/crm/testing/acme-scenario.d.ts +40 -0
- package/dist/crm/testing/acme-scenario.js +170 -0
- package/dist/crm/testing/index.d.ts +1 -0
- package/dist/crm/testing/index.js +1 -0
- package/dist/crm/testing/source-contract.js +12 -0
- package/dist/crm/testing/source.d.ts +2 -0
- package/dist/crm/testing/source.js +1 -0
- package/dist/domain/addresses.d.ts +13 -0
- package/dist/domain/addresses.js +63 -0
- package/dist/domain/categories.d.ts +3 -0
- package/dist/domain/categories.js +2 -0
- package/dist/domain/crm.d.ts +23 -0
- package/dist/domain/crm.js +7 -0
- package/dist/domain/mentions.d.ts +2 -12
- package/dist/domain/mentions.js +2 -19
- package/dist/domain/models.d.ts +2 -0
- package/dist/domain/orders.d.ts +8 -1
- package/dist/domain/orders.js +6 -2
- package/dist/domain/phones.d.ts +26 -0
- package/dist/domain/phones.js +48 -0
- package/dist/domain/record-types.d.ts +1 -1
- package/dist/domain/record-types.js +1 -0
- package/dist/domain/records.d.ts +5 -0
- package/dist/domain/ref-summaries.d.ts +39 -0
- package/dist/domain/ref-summaries.js +51 -3
- package/dist/domain/relations.d.ts +12 -2
- package/dist/domain/relations.js +14 -3
- package/dist/domain/vcard.d.ts +2 -2
- package/dist/domain/vcard.js +4 -10
- package/dist/embeddings/models.d.ts +11 -3
- package/dist/embeddings/models.js +28 -4
- package/dist/entities/business-context.d.ts +149 -0
- package/dist/entities/business-context.js +192 -0
- package/dist/entities/contact-activity.d.ts +2 -0
- package/dist/entities/contact-activity.js +7 -1
- package/dist/entities/contact-context.d.ts +160 -0
- package/dist/entities/contact-context.js +6 -0
- package/dist/entities/deal.d.ts +10 -0
- package/dist/entities/deal.js +2 -0
- package/dist/entities/hit.d.ts +9 -2
- package/dist/entities/hit.js +6 -2
- package/dist/entities/index.d.ts +32 -4
- package/dist/entities/index.js +5 -0
- package/dist/entities/mention.d.ts +4 -3
- package/dist/entities/mention.js +2 -1
- package/dist/entities/message-transfer.d.ts +4 -2
- package/dist/entities/message-transfer.js +2 -2
- package/dist/entities/model.d.ts +2 -2
- package/dist/entities/model.js +1 -1
- package/dist/entities/relationship.d.ts +4 -0
- package/dist/entities/relationship.js +4 -0
- package/dist/errors/reasons.js +39 -4
- package/dist/fetch/page.js +2 -1
- package/dist/index.d.ts +3 -2
- package/dist/index.js +3 -2
- package/dist/meetings/context-store.d.ts +2 -0
- package/dist/meetings/findings-rules.d.ts +4 -0
- package/dist/meetings/findings-rules.js +5 -2
- package/dist/meetings/hit-query.d.ts +1 -1
- package/dist/meetings/hit-query.js +2 -2
- package/dist/meetings/index.d.ts +1 -0
- package/dist/meetings/index.js +1 -0
- package/dist/meetings/result-items.d.ts +1 -0
- package/dist/meetings/result-items.js +1 -0
- package/dist/meetings/store.d.ts +4 -0
- package/dist/meetings/store.js +2 -1
- package/dist/models/anthropic.js +5 -0
- package/dist/models/index.js +22 -5
- package/dist/runs/events.d.ts +11 -1
- package/dist/runs/events.js +3 -1
- package/dist/runs/log-attributes.js +4 -0
- package/dist/runs/log-events.js +2 -0
- package/dist/runs/recording.d.ts +3 -0
- package/dist/runs/recording.js +36 -12
- package/dist/runs/run.d.ts +12 -3
- package/dist/runs/run.js +32 -13
- package/dist/search/account-list.d.ts +6 -0
- package/dist/search/account-list.js +9 -3
- package/dist/search/groups.d.ts +68 -0
- package/dist/search/groups.js +172 -0
- package/dist/search/question-period.d.ts +10 -0
- package/dist/search/question-period.js +133 -0
- package/dist/sends/buttons.d.ts +2 -0
- package/dist/sends/buttons.js +6 -1
- package/dist/services/account-groups.d.ts +28 -0
- package/dist/services/account-groups.js +89 -0
- package/dist/services/archive.d.ts +7 -1
- package/dist/services/archive.js +19 -3
- package/dist/services/business-context.d.ts +113 -0
- package/dist/services/business-context.js +156 -0
- package/dist/services/communications.d.ts +50 -0
- package/dist/services/communications.js +140 -0
- package/dist/services/deal-context.d.ts +90 -0
- package/dist/services/deal-context.js +131 -0
- package/dist/services/deps.d.ts +2 -0
- package/dist/services/enrichment.d.ts +12 -2
- package/dist/services/enrichment.js +47 -6
- package/dist/services/events-search.js +2 -1
- package/dist/services/group-guard.d.ts +28 -0
- package/dist/services/group-guard.js +131 -0
- package/dist/services/index.d.ts +19 -4
- package/dist/services/index.js +12 -3
- package/dist/services/ingest.d.ts +12 -0
- package/dist/services/ingest.js +4 -0
- package/dist/services/meaning-model.d.ts +7 -0
- package/dist/services/meaning-model.js +20 -0
- package/dist/services/meeting-context.d.ts +6 -1
- package/dist/services/meeting-context.js +12 -5
- package/dist/services/message-resolution.js +2 -3
- package/dist/services/messages-meaning.js +4 -5
- package/dist/services/messages-search.d.ts +5 -1
- package/dist/services/messages-search.js +1 -1
- package/dist/services/messages.d.ts +1 -4
- package/dist/services/messages.js +0 -2
- package/dist/services/metadata.d.ts +1 -1
- package/dist/services/model-downloads.d.ts +1 -1
- package/dist/services/model-downloads.js +6 -4
- package/dist/services/money-context.d.ts +73 -0
- package/dist/services/money-context.js +77 -0
- package/dist/services/notes-search.d.ts +4 -1
- package/dist/services/notes-search.js +19 -7
- package/dist/services/organization-context.d.ts +80 -0
- package/dist/services/organization-context.js +154 -0
- package/dist/services/people.d.ts +22 -6
- package/dist/services/people.js +38 -8
- package/dist/services/person-business.d.ts +43 -0
- package/dist/services/person-business.js +67 -0
- package/dist/services/person-context.d.ts +11 -1
- package/dist/services/person-context.js +25 -10
- package/dist/services/person-preparation.d.ts +5 -1
- package/dist/services/person-preparation.js +10 -3
- package/dist/services/person-timeline.d.ts +6 -1
- package/dist/services/person-timeline.js +10 -2
- package/dist/services/proposals/kinds/identity-link.d.ts +4 -0
- package/dist/services/proposals/kinds/identity-link.js +48 -0
- package/dist/services/proposals/kinds/orders.js +1 -1
- package/dist/services/proposals/registry.js +2 -0
- package/dist/services/search-all-deprecated.d.ts +40 -0
- package/dist/services/search-all-deprecated.js +10 -0
- package/dist/services/search-all-meetings.d.ts +50 -52
- package/dist/services/search-all-meetings.js +265 -272
- package/dist/services/search-all.d.ts +34 -6
- package/dist/services/search-all.js +118 -24
- package/dist/services/search-harder.d.ts +64 -0
- package/dist/services/search-harder.js +194 -0
- package/dist/services/search-kind.d.ts +12 -5
- package/dist/services/search-kind.js +49 -10
- package/dist/services/text-model-choice.js +1 -1
- package/dist/store/accounts.d.ts +18 -0
- package/dist/store/accounts.js +18 -3
- package/dist/store/chats.d.ts +3 -1
- package/dist/store/chats.js +6 -0
- package/dist/store/damage.d.ts +43 -10
- package/dist/store/damage.js +119 -36
- package/dist/store/decisions.js +3 -1
- package/dist/store/evidence-findings.d.ts +14 -0
- package/dist/store/evidence-findings.js +2 -0
- package/dist/store/exports.d.ts +2 -1
- package/dist/store/identity-links.d.ts +92 -0
- package/dist/store/identity-links.js +6 -0
- package/dist/store/index.d.ts +11 -5
- package/dist/store/index.js +5 -2
- package/dist/store/journal.d.ts +10 -0
- package/dist/store/journal.js +33 -0
- package/dist/store/marks.d.ts +19 -1
- package/dist/store/marks.js +23 -1
- package/dist/store/people.d.ts +28 -2
- package/dist/store/people.js +15 -1
- package/dist/store/sqlite/accounts.js +2 -0
- package/dist/store/sqlite/added-sources.d.ts +15 -3
- package/dist/store/sqlite/added-sources.js +26 -5
- package/dist/store/sqlite/base-stages.d.ts +122 -0
- package/dist/store/sqlite/base-stages.js +443 -0
- package/dist/store/sqlite/base-suggest.d.ts +13 -0
- package/dist/store/sqlite/base-suggest.js +62 -0
- package/dist/store/sqlite/business-contracts.js +10 -1
- package/dist/store/sqlite/chats.d.ts +2 -0
- package/dist/store/sqlite/chats.js +6 -0
- package/dist/store/sqlite/crm-settings.d.ts +12 -2
- package/dist/store/sqlite/crm-settings.js +56 -13
- package/dist/store/sqlite/crm.d.ts +13 -1
- package/dist/store/sqlite/crm.js +20 -4
- package/dist/store/sqlite/deals.d.ts +33 -0
- package/dist/store/sqlite/deals.js +42 -4
- package/dist/store/sqlite/decision-requests.js +7 -3
- package/dist/store/sqlite/email-index.js +8 -4
- package/dist/store/sqlite/evidence-findings.js +29 -5
- package/dist/store/sqlite/findings-core.js +4 -2
- package/dist/store/sqlite/identity-link-rows.d.ts +50 -0
- package/dist/store/sqlite/identity-link-rows.js +146 -0
- package/dist/store/sqlite/identity-proposals.d.ts +53 -0
- package/dist/store/sqlite/identity-proposals.js +234 -0
- package/dist/store/sqlite/involvements.d.ts +1 -1
- package/dist/store/sqlite/involvements.js +4 -2
- package/dist/store/sqlite/knowledge.js +2 -2
- package/dist/store/sqlite/link-kinds.d.ts +1 -0
- package/dist/store/sqlite/link-kinds.js +1 -0
- package/dist/store/sqlite/link-row.d.ts +6 -0
- package/dist/store/sqlite/link-row.js +35 -0
- package/dist/store/sqlite/link-source.d.ts +10 -0
- package/dist/store/sqlite/link-source.js +16 -0
- package/dist/store/sqlite/manifest.js +1 -0
- package/dist/store/sqlite/meeting-context-links.js +9 -6
- package/dist/store/sqlite/meeting-hit-query.d.ts +5 -0
- package/dist/store/sqlite/meeting-hit-query.js +33 -0
- package/dist/store/sqlite/meeting-hit-search.js +10 -8
- package/dist/store/sqlite/meetings.js +1 -1
- package/dist/store/sqlite/memories.js +9 -7
- package/dist/store/sqlite/mentions.d.ts +4 -1
- package/dist/store/sqlite/mentions.js +14 -9
- package/dist/store/sqlite/migrations.generated.js +8 -0
- package/dist/store/sqlite/notes.d.ts +8 -2
- package/dist/store/sqlite/notes.js +17 -34
- package/dist/store/sqlite/open-store.js +3 -0
- package/dist/store/sqlite/open.js +11 -2
- package/dist/store/sqlite/orders.d.ts +2 -0
- package/dist/store/sqlite/orders.js +9 -3
- package/dist/store/sqlite/organization-categories.d.ts +18 -0
- package/dist/store/sqlite/organization-categories.js +34 -0
- package/dist/store/sqlite/organizations.d.ts +5 -1
- package/dist/store/sqlite/organizations.js +14 -4
- package/dist/store/sqlite/person-links.d.ts +19 -4
- package/dist/store/sqlite/person-links.js +49 -11
- package/dist/store/sqlite/person-match-scan.d.ts +13 -0
- package/dist/store/sqlite/person-match-scan.js +251 -0
- package/dist/store/sqlite/person-matcher.d.ts +16 -0
- package/dist/store/sqlite/person-matcher.js +32 -0
- package/dist/store/sqlite/person-preparation.d.ts +7 -3
- package/dist/store/sqlite/person-preparation.js +20 -6
- package/dist/store/sqlite/proposed-actions.js +6 -0
- package/dist/store/sqlite/record-matching.d.ts +2 -8
- package/dist/store/sqlite/record-matching.js +8 -40
- package/dist/store/sqlite/record-projectors-phone.js +2 -1
- package/dist/store/sqlite/record-relationships.d.ts +52 -0
- package/dist/store/sqlite/record-relationships.js +243 -0
- package/dist/store/sqlite/record-run.d.ts +5 -0
- package/dist/store/sqlite/record-run.js +62 -2
- package/dist/store/sqlite/record-writes.d.ts +1 -1
- package/dist/store/sqlite/record-writes.js +4 -2
- package/dist/store/sqlite/records.js +27 -2
- package/dist/store/sqlite/ref-accounts.d.ts +13 -0
- package/dist/store/sqlite/ref-accounts.js +117 -0
- package/dist/store/sqlite/ref-summaries.js +5 -1
- package/dist/store/sqlite/relation-list.d.ts +4 -1
- package/dist/store/sqlite/relation-list.js +22 -4
- package/dist/store/sqlite/relation-proposals.d.ts +6 -0
- package/dist/store/sqlite/relation-proposals.js +3 -0
- package/dist/store/sqlite/relation-roles.js +12 -6
- package/dist/store/sqlite/reported-relations.d.ts +11 -1
- package/dist/store/sqlite/reported-relations.js +59 -18
- package/dist/store/sqlite/schema.d.ts +32 -0
- package/dist/store/sqlite/schema.js +3 -0
- package/dist/store/sqlite/transaction.js +11 -1
- package/dist/store/store.d.ts +3 -0
- package/dist/store/store.js +5 -0
- package/dist/store/write-events.d.ts +18 -0
- package/dist/store/write-events.js +88 -0
- package/package.json +3 -3
- package/parity.json +37 -16
- package/schemas/records-envelope.v1.schema.json +3 -0
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
import { CliError } from "@wirecat/cli-base";
|
|
2
|
+
import { isEmailAddress, isRoleAddress } from "../../domain/addresses.js";
|
|
2
3
|
import { and, eq, inArray, isNull, sql } from "./drizzle/core.js";
|
|
3
4
|
import { identityOf } from "./identities.js";
|
|
5
|
+
import { rejectedEvidence, settleLinks } from "./identity-link-rows.js";
|
|
4
6
|
import { currentPerson } from "./merged-persons.js";
|
|
5
7
|
import { resolvePersonLinks } from "./person-resolution.js";
|
|
6
8
|
import { identitySeparated, separated } from "./person-separation.js";
|
|
@@ -140,16 +142,24 @@ export const linkIdentityToPerson = (context, identityPk, target, options) => {
|
|
|
140
142
|
export const linkIdentities = (context, person, other, options) => {
|
|
141
143
|
const target = required(context, person);
|
|
142
144
|
const moving = required(context, other);
|
|
143
|
-
mergePerson(context, moving.personPk, target.personPk, options);
|
|
144
|
-
const record = recordOf(context, target.personPk);
|
|
145
|
+
const record = recordOf(context, mergePerson(context, moving.personPk, target.personPk, options));
|
|
145
146
|
// Two identities that were two people named alike are one now, so a name that was ambiguous may not be.
|
|
146
147
|
resolvePersonLinks(context.database, record.identities.flatMap(({ name, username }) => [name, username]));
|
|
147
148
|
return record;
|
|
148
149
|
};
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
150
|
+
const isOwner = ({ database }, person) => Number(database.prepare("SELECT owner FROM persons WHERE id = ?").get(person)?.owner) === 1;
|
|
151
|
+
/** The person `mergePerson(one, other)` keeps: `other`, unless `one` is the owner's. */
|
|
152
|
+
export const survivorOf = (context, one, other) => isOwner(context, one) && !isOwner(context, other) ? one : other;
|
|
153
|
+
/**
|
|
154
|
+
* Moves every identity of `from` to `to`, as `linkIdentities` does once it has found both people, and gives the
|
|
155
|
+
* person that stays. The owner's person always stays: merging it away would leave the owner with no identity.
|
|
156
|
+
* Proposals naming either person are brought up to date.
|
|
157
|
+
*/
|
|
158
|
+
export const mergePerson = (context, one, other, options) => {
|
|
159
|
+
if (one === other)
|
|
160
|
+
return other;
|
|
161
|
+
const to = survivorOf(context, one, other);
|
|
162
|
+
const from = to === other ? one : other;
|
|
153
163
|
const together = context.orm
|
|
154
164
|
.select({ identityPk: identityLinks.identityId })
|
|
155
165
|
.from(identityLinks)
|
|
@@ -157,18 +167,41 @@ export const mergePerson = (context, from, to, options) => {
|
|
|
157
167
|
.all();
|
|
158
168
|
for (const { identityPk } of together)
|
|
159
169
|
move(context, identityPk, from, to, options);
|
|
170
|
+
settleLinks(context.database, [from, to]);
|
|
171
|
+
return to;
|
|
172
|
+
};
|
|
173
|
+
/**
|
|
174
|
+
* NEED-2446 (safe option until the owner rules): an address that two or more other people hold is a shared one, an
|
|
175
|
+
* `office@` or `info@`, and joins no one at import; the scan proposes such people instead.
|
|
176
|
+
*/
|
|
177
|
+
const sharedAddress = ({ database }, key, person) => database
|
|
178
|
+
.prepare("SELECT count(DISTINCT l.person_id) AS n FROM identities i JOIN identity_links l ON l.identity_id = i.id " +
|
|
179
|
+
"WHERE i.email = ? AND i.provider NOT IN ('email', 'org-email') AND l.person_id <> ?")
|
|
180
|
+
.get(key, person)?.n >= 2;
|
|
181
|
+
/** Keeps the address an identity's source gave it, so a later contact with it counts this one. */
|
|
182
|
+
export const noteAddress = (context, ref, address) => {
|
|
183
|
+
const key = address.trim().toLowerCase();
|
|
184
|
+
if (!isEmailAddress(key) || ref.provider === "email")
|
|
185
|
+
return;
|
|
186
|
+
context.database
|
|
187
|
+
.prepare("UPDATE identities SET email = ? WHERE id = ? AND email IS NOT ?")
|
|
188
|
+
.run(key, required(context, ref).pk, key);
|
|
160
189
|
};
|
|
161
190
|
/**
|
|
162
191
|
* Joins the identity to the person of the mail contact with this exact address, making that contact when mail has
|
|
163
192
|
* not seen it yet, so whichever import comes second lands on one person. The contact is seen by no account, so it is
|
|
164
|
-
* no one's mail correspondent. An owner's unlink wins: two people the owner separated stay apart.
|
|
193
|
+
* no one's mail correspondent. An owner's unlink wins: two people the owner separated stay apart. A role address such
|
|
194
|
+
* as `info@` joins no one; the scan proposes its people instead. A pair the owner rejected is not joined either: it is
|
|
195
|
+
* given back, so the caller proposes it when the evidence is new to the owner.
|
|
165
196
|
*/
|
|
166
197
|
export const joinByAddress = (context, ref, address, by) => {
|
|
167
198
|
const key = address.trim().toLowerCase();
|
|
168
|
-
if (
|
|
199
|
+
if (!isEmailAddress(key) || ref.provider === "email")
|
|
169
200
|
return;
|
|
170
201
|
const found = required(context, ref);
|
|
171
|
-
context
|
|
202
|
+
noteAddress(context, ref, key);
|
|
203
|
+
if (isRoleAddress(key))
|
|
204
|
+
return;
|
|
172
205
|
// The mail contact of the address, found by the address column every contact with one carries (store version 32).
|
|
173
206
|
const mail = context.database
|
|
174
207
|
.prepare("SELECT external_id FROM identities WHERE provider = 'email' AND email = ? LIMIT 1")
|
|
@@ -178,8 +211,13 @@ export const joinByAddress = (context, ref, address, by) => {
|
|
|
178
211
|
const contact = required(context, { provider: "email", id: mail ? String(mail.external_id) : key });
|
|
179
212
|
if (contact.personPk === found.personPk || separated(context, found.personPk, contact.personPk))
|
|
180
213
|
return;
|
|
181
|
-
|
|
182
|
-
|
|
214
|
+
if (sharedAddress(context, key, found.personPk))
|
|
215
|
+
return;
|
|
216
|
+
if (rejectedEvidence(context.database, found.personPk, contact.personPk) !== undefined)
|
|
217
|
+
return { identity: found.pk, with: contact.pk, from: found.personPk, to: contact.personPk };
|
|
218
|
+
const kept = mergePerson(context, found.personPk, contact.personPk, { method: "same-email", by });
|
|
219
|
+
resolvePersonLinks(context.database, recordOf(context, kept).identities.flatMap(({ name, username }) => [name, username]));
|
|
220
|
+
return undefined;
|
|
183
221
|
};
|
|
184
222
|
/** Gives the identity a person of its own again; the ones left keep theirs. */
|
|
185
223
|
export const unlinkIdentity = (context, ref, options) => {
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import type { MatchScan } from "../identity-links.js";
|
|
2
|
+
import type { Author } from "./memories.js";
|
|
3
|
+
import type { StoreContext } from "./open.js";
|
|
4
|
+
export declare const RULE: Author;
|
|
5
|
+
/**
|
|
6
|
+
* One pass of the people matcher. It reads what may match outside any transaction and takes the write lock only for
|
|
7
|
+
* each group it changes, so other writers are never kept waiting for the whole pass. After the first pass, only the
|
|
8
|
+
* values of identities changed since the last one are looked at, unless `full`. The owner's separations and
|
|
9
|
+
* rejections hold throughout.
|
|
10
|
+
*/
|
|
11
|
+
export declare const scanMatches: (context: StoreContext, { full }?: {
|
|
12
|
+
full?: boolean;
|
|
13
|
+
}) => MatchScan;
|
|
@@ -0,0 +1,251 @@
|
|
|
1
|
+
import { isEmailAddress, isNoReplyAddress, isRoleAddress } from "../../domain/addresses.js";
|
|
2
|
+
import { BARE_INTERNATIONAL_PROVIDERS, storedPhone } from "../../domain/phones.js";
|
|
3
|
+
import { linkIndex, settleLinks } from "./identity-link-rows.js";
|
|
4
|
+
import { openIdentityLink } from "./identity-proposals.js";
|
|
5
|
+
import { mergePerson } from "./person-links.js";
|
|
6
|
+
import { personsByIdentifiers } from "./person-matcher.js";
|
|
7
|
+
import { resolvePersonLinks } from "./person-resolution.js";
|
|
8
|
+
import { separated } from "./person-separation.js";
|
|
9
|
+
import { referenceOfThing } from "./things.js";
|
|
10
|
+
import { write } from "./transaction.js";
|
|
11
|
+
/** Who an automatic link is recorded as: never the owner, whose moves count as separations. */
|
|
12
|
+
const BY = "rule";
|
|
13
|
+
export const RULE = { bot: "rule", kind: "rule" };
|
|
14
|
+
/** More people than this on one number or address is a shared line or mailbox: nobody is proposed from it. */
|
|
15
|
+
const MOST_SHARING = 6;
|
|
16
|
+
const CURSOR = "people.scan";
|
|
17
|
+
/**
|
|
18
|
+
* NEED-2445, safe option until the owner rules: a number two people share joins them only when a messenger, which
|
|
19
|
+
* keeps its user's own number, holds it; a CRM's or a card's number is someone's word and waits as a proposal.
|
|
20
|
+
*/
|
|
21
|
+
const phonePairJoins = (holders) => holders.some(({ provider }) => BARE_INTERNATIONAL_PROVIDERS.includes(provider));
|
|
22
|
+
/**
|
|
23
|
+
* NEED-2446, safe option until the owner rules: an address more than two people have is shared, so it proposes. So is
|
|
24
|
+
* a role address such as `info@` or `hello@`, even when two people have it.
|
|
25
|
+
*/
|
|
26
|
+
const addressGroupJoins = (address, people) => people <= 2 && !isRoleAddress(address);
|
|
27
|
+
const maxOf = (database, sql) => Number(database.prepare(sql).get()?.n ?? 0);
|
|
28
|
+
const cursorNow = (database) => ({
|
|
29
|
+
identity: maxOf(database, "SELECT max(id) AS n FROM identities"),
|
|
30
|
+
event: maxOf(database, "SELECT max(id) AS n FROM identity_link_events"),
|
|
31
|
+
revision: maxOf(database, "SELECT max(id) AS n FROM identity_revisions"),
|
|
32
|
+
participant: maxOf(database, "SELECT max(id) AS n FROM meeting_participants"),
|
|
33
|
+
at: maxOf(database, "SELECT max(updated_at) AS n FROM identities"),
|
|
34
|
+
});
|
|
35
|
+
const savedCursor = (database) => {
|
|
36
|
+
const value = database.prepare("SELECT value FROM store_settings WHERE key = ?").get(CURSOR)?.value;
|
|
37
|
+
return value === undefined ? undefined : JSON.parse(String(value));
|
|
38
|
+
};
|
|
39
|
+
/** The identities changed since `since`: new ones, moved ones, edited ones and ones a new meeting guest row names. */
|
|
40
|
+
const changedSince = (database, since) => new Set(database
|
|
41
|
+
.prepare("SELECT id FROM identities WHERE id > ? OR updated_at > ? " +
|
|
42
|
+
"UNION SELECT identity_id FROM identity_link_events WHERE id > ? " +
|
|
43
|
+
"UNION SELECT identity_id FROM identity_revisions WHERE id > ? " +
|
|
44
|
+
"UNION SELECT identity_id FROM meeting_participants WHERE id > ?")
|
|
45
|
+
.all(since.identity, since.at, since.event, since.revision, since.participant)
|
|
46
|
+
.map(({ id }) => Number(id)));
|
|
47
|
+
const add = (found, key, holder) => {
|
|
48
|
+
if (key === undefined)
|
|
49
|
+
return;
|
|
50
|
+
const held = found.get(key) ?? [];
|
|
51
|
+
if (!held.some(({ identity }) => identity === holder.identity))
|
|
52
|
+
held.push(holder);
|
|
53
|
+
found.set(key, held);
|
|
54
|
+
};
|
|
55
|
+
const phoneHolders = (database) => {
|
|
56
|
+
const found = new Map();
|
|
57
|
+
for (const row of database
|
|
58
|
+
.prepare("SELECT i.id, i.provider, i.external_id, i.phone FROM identities i JOIN identity_links l ON l.identity_id = i.id " +
|
|
59
|
+
"WHERE i.provider = 'phone' OR i.phone IS NOT NULL ORDER BY i.id")
|
|
60
|
+
.all()) {
|
|
61
|
+
const provider = String(row.provider);
|
|
62
|
+
const raw = String(provider === "phone" ? row.external_id : row.phone);
|
|
63
|
+
add(found, storedPhone(raw, provider), { identity: Number(row.id), provider });
|
|
64
|
+
}
|
|
65
|
+
return found;
|
|
66
|
+
};
|
|
67
|
+
const addressOf = (value) => {
|
|
68
|
+
const address = typeof value === "string" ? value.trim().toLowerCase() : "";
|
|
69
|
+
return isEmailAddress(address) ? address : undefined;
|
|
70
|
+
};
|
|
71
|
+
/** A company's address is an `org-email` identity, never a person's, so it never joins people. */
|
|
72
|
+
const emailHolders = (database) => {
|
|
73
|
+
const found = new Map();
|
|
74
|
+
for (const row of database
|
|
75
|
+
.prepare("SELECT i.id, i.provider, i.external_id, i.email FROM identities i JOIN identity_links l ON l.identity_id = i.id " +
|
|
76
|
+
"WHERE i.provider <> 'org-email' AND (i.provider = 'email' OR i.email IS NOT NULL) ORDER BY i.id")
|
|
77
|
+
.all()) {
|
|
78
|
+
const holder = { identity: Number(row.id), provider: String(row.provider) };
|
|
79
|
+
add(found, addressOf(row.email), holder);
|
|
80
|
+
if (row.provider === "email")
|
|
81
|
+
add(found, addressOf(row.external_id), holder);
|
|
82
|
+
}
|
|
83
|
+
return found;
|
|
84
|
+
};
|
|
85
|
+
const personOf = ({ database }, identity) => {
|
|
86
|
+
const row = database.prepare("SELECT person_id FROM identity_links WHERE identity_id = ?").get(identity);
|
|
87
|
+
return row ? Number(row.person_id) : undefined;
|
|
88
|
+
};
|
|
89
|
+
/** The people the identities are on now, each once, with an identity of each that holds the value. */
|
|
90
|
+
const peopleOf = (context, holders) => {
|
|
91
|
+
const found = new Map();
|
|
92
|
+
for (const { identity } of holders) {
|
|
93
|
+
const person = personOf(context, identity);
|
|
94
|
+
if (person !== undefined && !found.has(person))
|
|
95
|
+
found.set(person, identity);
|
|
96
|
+
}
|
|
97
|
+
return found;
|
|
98
|
+
};
|
|
99
|
+
const resolveNames = (context, person) => resolvePersonLinks(context.database, context.database
|
|
100
|
+
.prepare("SELECT i.name, i.username FROM identities i JOIN identity_links l ON l.identity_id = i.id WHERE l.person_id = ?")
|
|
101
|
+
.all(person)
|
|
102
|
+
.flatMap((row) => [row.name, row.username]));
|
|
103
|
+
/**
|
|
104
|
+
* Joins two people unless the owner separated them, or rejected the pair (NEED-2444): then the pair is proposed when
|
|
105
|
+
* this evidence is new to the owner. Gives the person that stays.
|
|
106
|
+
*/
|
|
107
|
+
const join = (pass, sides, signal) => {
|
|
108
|
+
const { context, index, result } = pass;
|
|
109
|
+
const { from, into } = sides;
|
|
110
|
+
const method = signal === "email" ? "same-email" : "same-phone";
|
|
111
|
+
if (separated(context, from, into)) {
|
|
112
|
+
result.skipped.push({ persons: [String(from), String(into)], signal: method, reason: "identity.separated" });
|
|
113
|
+
return into;
|
|
114
|
+
}
|
|
115
|
+
if (index.rejected(from, into) !== undefined) {
|
|
116
|
+
propose(pass, { identity: sides.identity, with: sides.with, from, to: into, joins: "person", signal });
|
|
117
|
+
return into;
|
|
118
|
+
}
|
|
119
|
+
const kept = mergePerson(context, from, into, { method, by: BY });
|
|
120
|
+
index.merged(kept === into ? from : into, kept);
|
|
121
|
+
resolveNames(context, kept);
|
|
122
|
+
result.merged.push({ into: String(kept), from: String(kept === into ? from : into), signal: method });
|
|
123
|
+
return kept;
|
|
124
|
+
};
|
|
125
|
+
const propose = ({ context, index, result }, opening) => {
|
|
126
|
+
const opened = openIdentityLink(context, { ...opening, by: RULE }, index);
|
|
127
|
+
const persons = [String(opening.from), String(opening.to)];
|
|
128
|
+
if (opened.outcome === "skipped") {
|
|
129
|
+
if (opened.reason !== "identity.same")
|
|
130
|
+
result.skipped.push({ persons, signal: opening.signal, reason: opened.reason });
|
|
131
|
+
return;
|
|
132
|
+
}
|
|
133
|
+
result.proposed.push({ id: opened.action.id, persons, signal: opening.signal, opened: opened.outcome === "proposed" });
|
|
134
|
+
};
|
|
135
|
+
const proposeEachPair = (pass, people, signal) => {
|
|
136
|
+
const persons = [...people.keys()].sort((a, b) => a - b);
|
|
137
|
+
for (const [at, from] of persons.entries())
|
|
138
|
+
for (const to of persons.slice(at + 1))
|
|
139
|
+
propose(pass, {
|
|
140
|
+
identity: people.get(from),
|
|
141
|
+
with: people.get(to),
|
|
142
|
+
from,
|
|
143
|
+
to,
|
|
144
|
+
joins: "person",
|
|
145
|
+
signal,
|
|
146
|
+
candidates: persons,
|
|
147
|
+
});
|
|
148
|
+
};
|
|
149
|
+
const tooMany = (pass, people, reason, signal) => {
|
|
150
|
+
if (people.size <= MOST_SHARING)
|
|
151
|
+
return false;
|
|
152
|
+
pass.result.skipped.push({ persons: [...people.keys()].map(String), signal, reason });
|
|
153
|
+
return true;
|
|
154
|
+
};
|
|
155
|
+
/** A number two people have joins them (NEED-2445); one that more share, or that once proposed, proposes. */
|
|
156
|
+
const samePhone = (pass, holders) => {
|
|
157
|
+
const people = peopleOf(pass.context, holders);
|
|
158
|
+
if (people.size < 2 || tooMany(pass, people, "identity.shared-phone", "phone"))
|
|
159
|
+
return;
|
|
160
|
+
const [into, from] = [...people.keys()].sort((a, b) => a - b);
|
|
161
|
+
if (people.size === 2 && phonePairJoins(holders) && !pass.index.known(from, into))
|
|
162
|
+
join(pass, { from, into, identity: people.get(from), with: people.get(into) }, "phone");
|
|
163
|
+
else
|
|
164
|
+
proposeEachPair(pass, people, "phone");
|
|
165
|
+
};
|
|
166
|
+
/**
|
|
167
|
+
* One address is one person in any scope (NEED-2382), unless more than two people have it or it is a role address
|
|
168
|
+
* (NEED-2446). A no-reply address is no one's.
|
|
169
|
+
*/
|
|
170
|
+
const sameEmail = (pass, address, holders) => {
|
|
171
|
+
if (isNoReplyAddress(address))
|
|
172
|
+
return;
|
|
173
|
+
const people = peopleOf(pass.context, holders);
|
|
174
|
+
if (people.size < 2 || tooMany(pass, people, "identity.shared-email", "email"))
|
|
175
|
+
return;
|
|
176
|
+
if (!addressGroupJoins(address, people.size))
|
|
177
|
+
return proposeEachPair(pass, people, "email");
|
|
178
|
+
const contact = holders.find(({ provider }) => provider === "email");
|
|
179
|
+
const held = contact === undefined ? undefined : personOf(pass.context, contact.identity);
|
|
180
|
+
let into = held !== undefined && people.has(held) ? held : [...people.keys()].reduce((a, b) => Math.min(a, b));
|
|
181
|
+
// The mail contact stands for its person, as at import, so both paths name the same evidence to the owner.
|
|
182
|
+
const intoIdentity = contact !== undefined && held === into ? contact.identity : people.get(into);
|
|
183
|
+
for (const from of people.keys())
|
|
184
|
+
if (from !== into)
|
|
185
|
+
into = join(pass, { from, into, identity: people.get(from), with: intoIdentity }, "email");
|
|
186
|
+
};
|
|
187
|
+
/** What a meeting guest typed is their word only: it is proposed, and only when it names exactly one person. */
|
|
188
|
+
const guestEmail = (pass, row) => {
|
|
189
|
+
const { context } = pass;
|
|
190
|
+
const address = addressOf(row.email);
|
|
191
|
+
const from = personOf(context, row.identity);
|
|
192
|
+
if (address === undefined || from === undefined || isNoReplyAddress(address))
|
|
193
|
+
return;
|
|
194
|
+
const others = personsByIdentifiers(context.database, { emails: [address], phones: [] }).filter((one) => one !== from);
|
|
195
|
+
if (others.length !== 1)
|
|
196
|
+
return;
|
|
197
|
+
const meeting = referenceOfThing(context.database, { type: "meeting", id: row.meeting });
|
|
198
|
+
propose(pass, {
|
|
199
|
+
identity: row.identity,
|
|
200
|
+
with: null,
|
|
201
|
+
from,
|
|
202
|
+
to: others[0],
|
|
203
|
+
joins: "identity",
|
|
204
|
+
signal: "guest-email",
|
|
205
|
+
...(meeting === undefined ? {} : { evidence: [meeting] }),
|
|
206
|
+
});
|
|
207
|
+
};
|
|
208
|
+
const guestRows = (database) => database
|
|
209
|
+
.prepare("SELECT mp.identity_id, i.provider, i.external_id, mp.email, min(mp.meeting_id) AS meeting " +
|
|
210
|
+
"FROM meeting_participants mp JOIN identity_links l ON l.identity_id = mp.identity_id " +
|
|
211
|
+
"JOIN identities i ON i.id = mp.identity_id WHERE mp.email IS NOT NULL " +
|
|
212
|
+
"GROUP BY mp.identity_id, lower(trim(mp.email)) ORDER BY mp.identity_id")
|
|
213
|
+
.all()
|
|
214
|
+
.map((row) => ({
|
|
215
|
+
identity: Number(row.identity_id),
|
|
216
|
+
ref: `${String(row.provider)}:${String(row.external_id)}`,
|
|
217
|
+
email: row.email,
|
|
218
|
+
meeting: Number(row.meeting),
|
|
219
|
+
}));
|
|
220
|
+
/**
|
|
221
|
+
* One pass of the people matcher. It reads what may match outside any transaction and takes the write lock only for
|
|
222
|
+
* each group it changes, so other writers are never kept waiting for the whole pass. After the first pass, only the
|
|
223
|
+
* values of identities changed since the last one are looked at, unless `full`. The owner's separations and
|
|
224
|
+
* rejections hold throughout.
|
|
225
|
+
*/
|
|
226
|
+
export const scanMatches = (context, { full = false } = {}) => {
|
|
227
|
+
const { database } = context;
|
|
228
|
+
const started = cursorNow(database);
|
|
229
|
+
const since = full ? undefined : savedCursor(database);
|
|
230
|
+
const changed = since === undefined ? undefined : changedSince(database, since);
|
|
231
|
+
const touched = (holders) => changed === undefined || holders.some(({ identity }) => changed.has(identity));
|
|
232
|
+
write(database, () => settleLinks(database));
|
|
233
|
+
const pass = {
|
|
234
|
+
context,
|
|
235
|
+
index: linkIndex(database),
|
|
236
|
+
result: { full: changed === undefined, merged: [], proposed: [], skipped: [] },
|
|
237
|
+
};
|
|
238
|
+
for (const [address, holders] of emailHolders(database))
|
|
239
|
+
if (holders.length > 1 && touched(holders))
|
|
240
|
+
write(database, () => sameEmail(pass, address, holders));
|
|
241
|
+
for (const holders of phoneHolders(database).values())
|
|
242
|
+
if (holders.length > 1 && touched(holders))
|
|
243
|
+
write(database, () => samePhone(pass, holders));
|
|
244
|
+
for (const row of guestRows(database))
|
|
245
|
+
if ((changed === undefined || changed.has(row.identity)) && !pass.index.names(row.ref))
|
|
246
|
+
write(database, () => guestEmail(pass, row));
|
|
247
|
+
write(database, () => database
|
|
248
|
+
.prepare("INSERT OR REPLACE INTO store_settings (key, value, at) VALUES (?, ?, ?)")
|
|
249
|
+
.run(CURSOR, JSON.stringify(started), context.now()));
|
|
250
|
+
return pass.result;
|
|
251
|
+
};
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
export { e164Of, storedPhone } from "../../domain/phones.js";
|
|
2
|
+
import type { CacheDatabase } from "../driver.js";
|
|
3
|
+
/** What a person is matched by: lower-cased addresses, E.164 numbers and a source's own contact identity. */
|
|
4
|
+
export interface PersonIdentifiers {
|
|
5
|
+
emails: readonly string[];
|
|
6
|
+
phones: readonly string[];
|
|
7
|
+
contact?: {
|
|
8
|
+
provider: string;
|
|
9
|
+
externalId: string;
|
|
10
|
+
} | undefined;
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* The people the identifiers point at, each once, by id. The one lookup the records import and the people matcher
|
|
14
|
+
* share; it reads the store and nothing else.
|
|
15
|
+
*/
|
|
16
|
+
export declare const personsByIdentifiers: (database: CacheDatabase, ids: PersonIdentifiers) => number[];
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
export { e164Of, storedPhone } from "../../domain/phones.js";
|
|
2
|
+
/** The spellings of an E.164 number `storedPhone` reads back as it. */
|
|
3
|
+
const spellingsOf = (value) => [value, value.slice(1)];
|
|
4
|
+
const PERSON_BY = {
|
|
5
|
+
// UNION, not OR: each branch reads its own index.
|
|
6
|
+
email: "SELECT l.person_id FROM identities i JOIN identity_links l ON l.identity_id = i.id " +
|
|
7
|
+
"WHERE i.provider = 'email' AND i.external_id = ? UNION " +
|
|
8
|
+
"SELECT l.person_id FROM identities i JOIN identity_links l ON l.identity_id = i.id WHERE i.email = ?",
|
|
9
|
+
phone: "SELECT l.person_id FROM identities i JOIN identity_links l ON l.identity_id = i.id " +
|
|
10
|
+
"WHERE i.provider = 'phone' AND i.external_id = ? UNION " +
|
|
11
|
+
"SELECT l.person_id FROM identities i JOIN identity_links l ON l.identity_id = i.id WHERE i.phone IN (?, ?)",
|
|
12
|
+
contact: "SELECT l.person_id FROM identities i JOIN identity_links l ON l.identity_id = i.id " +
|
|
13
|
+
"WHERE i.provider = ? AND i.external_id = ?",
|
|
14
|
+
};
|
|
15
|
+
/**
|
|
16
|
+
* The people the identifiers point at, each once, by id. The one lookup the records import and the people matcher
|
|
17
|
+
* share; it reads the store and nothing else.
|
|
18
|
+
*/
|
|
19
|
+
export const personsByIdentifiers = (database, ids) => {
|
|
20
|
+
const found = new Set();
|
|
21
|
+
const add = (rows) => {
|
|
22
|
+
for (const row of rows)
|
|
23
|
+
found.add(Number(row.person_id));
|
|
24
|
+
};
|
|
25
|
+
for (const email of ids.emails)
|
|
26
|
+
add(database.prepare(PERSON_BY.email).all(email, email));
|
|
27
|
+
for (const value of ids.phones)
|
|
28
|
+
add(database.prepare(PERSON_BY.phone).all(value, ...spellingsOf(value)));
|
|
29
|
+
if (ids.contact)
|
|
30
|
+
add(database.prepare(PERSON_BY.contact).all(ids.contact.provider, ids.contact.externalId));
|
|
31
|
+
return [...found].sort((a, b) => a - b);
|
|
32
|
+
};
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { AccountKey } from "../types.js";
|
|
1
|
+
import type { AccountKey, StoredAccount } from "../types.js";
|
|
2
2
|
import type { StoreContext } from "./open.js";
|
|
3
3
|
export interface PreparationFact {
|
|
4
4
|
ref: string;
|
|
@@ -40,9 +40,13 @@ export interface PersonPreparationRead {
|
|
|
40
40
|
}
|
|
41
41
|
/**
|
|
42
42
|
* The accepted facts, the owner's notes and the open tasks about one person. A project bound to an account is that
|
|
43
|
-
* account's: its facts never come along, and its tasks only when `accounts` holds it.
|
|
43
|
+
* account's: its facts never come along, and its tasks only when `accounts` holds it. What belongs to no account
|
|
44
|
+
* comes only in `scopes`: a fact in its own scope, else its project's, a note only when every scope it touches is
|
|
45
|
+
* read, a global project's task in that project's. Anything else unscoped is personal, the side a work view never
|
|
46
|
+
* reads.
|
|
44
47
|
*/
|
|
45
|
-
export declare const personPreparation: ({ database, now }: Pick<StoreContext, "database" | "now">, uid: string, { accounts, limit }: {
|
|
48
|
+
export declare const personPreparation: ({ database, now }: Pick<StoreContext, "database" | "now">, uid: string, { accounts, scopes, limit, }: {
|
|
46
49
|
accounts: readonly AccountKey[];
|
|
50
|
+
scopes: readonly StoredAccount["scope"][];
|
|
47
51
|
limit: number;
|
|
48
52
|
}) => PersonPreparationRead;
|
|
@@ -5,16 +5,28 @@ import { samePersons } from "./merged-persons.js";
|
|
|
5
5
|
import { taskIdOf } from "./things.js";
|
|
6
6
|
import { read } from "./transaction.js";
|
|
7
7
|
const GLOBAL_PROJECT = "SELECT id FROM main.projects WHERE account_id IS NULL AND deleted_at IS NULL";
|
|
8
|
+
const IN_SCOPES = "IN (SELECT value FROM json_each(?))";
|
|
9
|
+
const projectScope = (id) => `(SELECT p.scope FROM main.projects p WHERE p.id=${id})`;
|
|
10
|
+
const NOTE_PROJECTS = `SELECT l.to_id FROM links l WHERE l.from_type='note' AND l.from_id=n.id AND l.to_type='project'
|
|
11
|
+
AND l.kind=${linkKind("about")}`;
|
|
12
|
+
/** A note is in its project's scope, or in those of the projects it is about; with no project, personal. */
|
|
13
|
+
const NOTE_SCOPES = `SELECT p.scope FROM main.projects p WHERE (n.notable_type='project' AND p.id=n.notable_id)
|
|
14
|
+
OR p.id IN (${NOTE_PROJECTS})
|
|
15
|
+
UNION SELECT 'personal' WHERE n.notable_type<>'project' AND NOT EXISTS(${NOTE_PROJECTS})`;
|
|
8
16
|
const iso = (value) => (value == null ? null : new Date(Number(value)).toISOString());
|
|
9
17
|
/**
|
|
10
18
|
* The accepted facts, the owner's notes and the open tasks about one person. A project bound to an account is that
|
|
11
|
-
* account's: its facts never come along, and its tasks only when `accounts` holds it.
|
|
19
|
+
* account's: its facts never come along, and its tasks only when `accounts` holds it. What belongs to no account
|
|
20
|
+
* comes only in `scopes`: a fact in its own scope, else its project's, a note only when every scope it touches is
|
|
21
|
+
* read, a global project's task in that project's. Anything else unscoped is personal, the side a work view never
|
|
22
|
+
* reads.
|
|
12
23
|
*/
|
|
13
|
-
export const personPreparation = ({ database, now }, uid, { accounts, limit }) => {
|
|
24
|
+
export const personPreparation = ({ database, now }, uid, { accounts, scopes, limit, }) => {
|
|
14
25
|
const asked = Number(uid);
|
|
15
26
|
if (!/^[1-9]\d*$/.test(uid) || !Number.isSafeInteger(asked) || !Number.isSafeInteger(limit) || limit < 1)
|
|
16
27
|
throw new CliError("validation_error", "person preparation takes a person id and a positive limit");
|
|
17
28
|
const page = (rows) => ({ items: rows.slice(0, limit), hasMore: rows.length > limit });
|
|
29
|
+
const inScopes = JSON.stringify(scopes);
|
|
18
30
|
return read(database, () => {
|
|
19
31
|
const person = JSON.stringify(samePersons(database, asked));
|
|
20
32
|
const projects = `SELECT l.to_id FROM links l WHERE l.from_type='person' AND l.from_id IN (SELECT value FROM json_each(?))
|
|
@@ -27,8 +39,9 @@ export const personPreparation = ({ database, now }, uid, { accounts, limit }) =
|
|
|
27
39
|
OR f.project_id IN (${projects}))
|
|
28
40
|
AND (f.subject_type IS NOT 'project' OR f.subject_id IN (${GLOBAL_PROJECT}))
|
|
29
41
|
AND (f.project_id IS NULL OR f.project_id IN (${GLOBAL_PROJECT}))
|
|
42
|
+
AND coalesce(f.scope,${projectScope("coalesce(f.project_id,CASE WHEN f.subject_type='project' THEN f.subject_id END)")},'personal') ${IN_SCOPES}
|
|
30
43
|
ORDER BY f.updated_at DESC, f.id DESC LIMIT ?`)
|
|
31
|
-
.all(person, person, person, limit + 1)
|
|
44
|
+
.all(person, person, person, inScopes, limit + 1)
|
|
32
45
|
.map((row) => ({
|
|
33
46
|
ref: `fact:${row.id}`,
|
|
34
47
|
type: String(row.type),
|
|
@@ -44,8 +57,9 @@ export const personPreparation = ({ database, now }, uid, { accounts, limit }) =
|
|
|
44
57
|
AND ((n.notable_type='person' AND n.notable_id IN (SELECT value FROM json_each(?))) OR EXISTS(SELECT 1 FROM links l WHERE l.from_type='note'
|
|
45
58
|
AND l.from_id=n.id AND l.to_type='person' AND l.to_id IN (SELECT value FROM json_each(?)) AND l.kind=${linkKind("about")} AND l.confirmed=1))
|
|
46
59
|
AND (n.notable_type='person' OR (n.notable_type='project' AND n.notable_id IN (${GLOBAL_PROJECT})))
|
|
60
|
+
AND NOT EXISTS(SELECT 1 FROM (${NOTE_SCOPES}) WHERE scope NOT ${IN_SCOPES})
|
|
47
61
|
ORDER BY n.updated_at DESC, n.id DESC LIMIT ?`)
|
|
48
|
-
.all(person, person, limit + 1)
|
|
62
|
+
.all(person, person, inScopes, limit + 1)
|
|
49
63
|
.map((row) => ({
|
|
50
64
|
ref: `note:${row.id}`,
|
|
51
65
|
title: row.title == null ? null : String(row.title),
|
|
@@ -57,11 +71,11 @@ export const personPreparation = ({ database, now }, uid, { accounts, limit }) =
|
|
|
57
71
|
.prepare(`SELECT t.id,t.title,t.due_at,t.project_id,t.source_locator,t.created_at,group_concat(DISTINCT ta.role) AS role
|
|
58
72
|
FROM task_assignments ta JOIN tasks t ON t.id=ta.task_id JOIN main.projects p ON p.id=t.project_id
|
|
59
73
|
WHERE ta.assignee_type='person' AND ta.assignee_id IN (SELECT value FROM json_each(?)) AND t.deleted_at IS NULL AND t.status NOT IN ('done','dismissed')
|
|
60
|
-
AND p.deleted_at IS NULL AND (p.account_id IS NULL OR p.account_id IN (SELECT a.id FROM accounts a
|
|
74
|
+
AND p.deleted_at IS NULL AND ((p.account_id IS NULL AND p.scope ${IN_SCOPES}) OR p.account_id IN (SELECT a.id FROM accounts a
|
|
61
75
|
JOIN json_each(?) k ON a.provider=json_extract(k.value,'$.provider')
|
|
62
76
|
AND a.external_id=json_extract(k.value,'$.account')))
|
|
63
77
|
GROUP BY t.id ORDER BY t.due_at IS NULL, t.due_at, t.created_at, t.id LIMIT ?`)
|
|
64
|
-
.all(person, JSON.stringify(accounts.map(({ provider, account }) => ({ provider, account }))), limit + 1)
|
|
78
|
+
.all(person, inScopes, JSON.stringify(accounts.map(({ provider, account }) => ({ provider, account }))), limit + 1)
|
|
65
79
|
.map((row) => ({
|
|
66
80
|
ref: `task:${taskIdOf(database, Number(row.id))}`,
|
|
67
81
|
title: String(row.title),
|
|
@@ -2,6 +2,7 @@ import { CliError } from "@wirecat/cli-base";
|
|
|
2
2
|
import { MEETING_FINDINGS_KIND } from "../../meetings/index.js";
|
|
3
3
|
import { assertMayDecide, deciderAuthor } from "../decisions.js";
|
|
4
4
|
import { EVIDENCE_FINDINGS_KIND } from "../evidence-findings.js";
|
|
5
|
+
import { IDENTITY_LINK_KIND } from "../identity-links.js";
|
|
5
6
|
import { MAIL_FINDINGS_KIND } from "../mail-findings.js";
|
|
6
7
|
import { SOURCE_REVIEW_KIND } from "../source-review.js";
|
|
7
8
|
import { proposedBySql } from "./actors.js";
|
|
@@ -185,6 +186,11 @@ const DEDICATED_REVIEW = {
|
|
|
185
186
|
reason: "source-review.dedicated-review",
|
|
186
187
|
through: "the review of memories and facts, which signs their sources again",
|
|
187
188
|
},
|
|
189
|
+
[IDENTITY_LINK_KIND]: {
|
|
190
|
+
what: "a proposed identity link",
|
|
191
|
+
reason: "identity-link.dedicated-review",
|
|
192
|
+
through: "the people review, which checks that neither person changed",
|
|
193
|
+
},
|
|
188
194
|
};
|
|
189
195
|
/** Refuses a dedicated kind on a generic action path: approving one as an action would skip its own checks. */
|
|
190
196
|
export const assertGenericAction = (kind) => {
|
|
@@ -37,20 +37,14 @@ export type Match = {
|
|
|
37
37
|
ids: number[];
|
|
38
38
|
reason: "record.ambiguous" | "record.separated";
|
|
39
39
|
};
|
|
40
|
-
/**
|
|
41
|
-
* A phone as E.164, `null` for a number written without `+` or `00` when the envelope names no `phone_region`, or
|
|
42
|
-
* nothing when it is no phone. Only the envelope says which country a national number is from: the store's
|
|
43
|
-
* `phone_countries` setting reads `4155550100` as Russian, so it never decides an identifier.
|
|
44
|
-
*/
|
|
45
|
-
export declare const e164Of: (phone: string, region: string | undefined) => string | null | undefined;
|
|
46
40
|
/**
|
|
47
41
|
* A record's identifiers. What does not read as an e-mail, a phone or a `{scheme, value}` of a known scheme is no
|
|
48
42
|
* identifier and stays in the fields, as v1 kept it; a free-mail domain claimed as an organization's is refused.
|
|
49
43
|
*/
|
|
50
44
|
export declare const identifiersOf: (type: IdentityType, fields: Row, source: EnvelopeSource, externalId: string, options?: EnvelopeOptions) => Identifiers;
|
|
51
45
|
/**
|
|
52
|
-
* The entities the identifiers point at, each once.
|
|
53
|
-
*
|
|
46
|
+
* The entities the identifiers point at, each once. A person is matched by the people matcher's lookup, which
|
|
47
|
+
* `people.scanMatches` reads phones and addresses with too.
|
|
54
48
|
*/
|
|
55
49
|
export declare const entitiesByIdentifiers: (database: CacheDatabase, type: IdentityType, ids: Identifiers) => number[];
|
|
56
50
|
/** Whether the owner separated `person` from an identity the record brings: linking would join what he told apart. */
|
|
@@ -1,11 +1,11 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { nationalPhone } from "../../domain/phones.js";
|
|
1
|
+
import { isEmailAddress } from "../../domain/addresses.js";
|
|
3
2
|
import { isObject } from "../../domain/record-fields.js";
|
|
4
3
|
import { RECORD_TABLES } from "../../domain/record-types.js";
|
|
5
4
|
import { FREE_MAIL } from "../../mail/score.js";
|
|
6
5
|
import { fold } from "../normalize.js";
|
|
7
6
|
import { crmIdentityId } from "./crm.js";
|
|
8
7
|
import { insertProject } from "./knowledge.js";
|
|
8
|
+
import { e164Of, personsByIdentifiers } from "./person-matcher.js";
|
|
9
9
|
import { identitySeparated } from "./person-separation.js";
|
|
10
10
|
import { refusal } from "./record-projection.js";
|
|
11
11
|
const valuesOf = (value) => (Array.isArray(value) ? value : [value]).filter((one) => typeof one === "string");
|
|
@@ -20,21 +20,6 @@ const domainOf = (value) => value
|
|
|
20
20
|
.trim()
|
|
21
21
|
.toLowerCase()
|
|
22
22
|
.replace(/^www\./, "");
|
|
23
|
-
/**
|
|
24
|
-
* A phone as E.164, `null` for a number written without `+` or `00` when the envelope names no `phone_region`, or
|
|
25
|
-
* nothing when it is no phone. Only the envelope says which country a national number is from: the store's
|
|
26
|
-
* `phone_countries` setting reads `4155550100` as Russian, so it never decides an identifier.
|
|
27
|
-
*/
|
|
28
|
-
export const e164Of = (phone, region) => {
|
|
29
|
-
if (/^\s*(\+|00)/.test(phone))
|
|
30
|
-
return typedPhone(phone)?.value;
|
|
31
|
-
if (typedPhone(phone) === undefined)
|
|
32
|
-
return undefined;
|
|
33
|
-
if (region === undefined)
|
|
34
|
-
return null;
|
|
35
|
-
const digits = phone.replace(/\(0\)/, "").replace(/\D/g, "");
|
|
36
|
-
return nationalPhone(digits, [region], { anyType: true })?.value;
|
|
37
|
-
};
|
|
38
23
|
/**
|
|
39
24
|
* A record's identifiers. What does not read as an e-mail, a phone or a `{scheme, value}` of a known scheme is no
|
|
40
25
|
* identifier and stays in the fields, as v1 kept it; a free-mail domain claimed as an organization's is refused.
|
|
@@ -45,7 +30,7 @@ export const identifiersOf = (type, fields, source, externalId, options = {}) =>
|
|
|
45
30
|
found.contact = { provider: source.provider, externalId: crmIdentityId(source, externalId) };
|
|
46
31
|
for (const email of valuesOf(fields.email)) {
|
|
47
32
|
const address = email.trim().toLowerCase();
|
|
48
|
-
if (
|
|
33
|
+
if (isEmailAddress(address) && !found.emails.includes(address))
|
|
49
34
|
found.emails.push(address);
|
|
50
35
|
}
|
|
51
36
|
const region = options.phone_region?.toLowerCase();
|
|
@@ -82,21 +67,9 @@ export const identifiersOf = (type, fields, source, externalId, options = {}) =>
|
|
|
82
67
|
found.key = fields.key.trim().toUpperCase();
|
|
83
68
|
return found;
|
|
84
69
|
};
|
|
85
|
-
const PERSON_BY = {
|
|
86
|
-
// UNION, not OR: each branch reads its own index.
|
|
87
|
-
email: "SELECT l.person_id FROM identities i JOIN identity_links l ON l.identity_id = i.id " +
|
|
88
|
-
"WHERE i.provider = 'email' AND i.external_id = ? UNION " +
|
|
89
|
-
"SELECT l.person_id FROM identities i JOIN identity_links l ON l.identity_id = i.id WHERE i.email = ?",
|
|
90
|
-
// A messenger or CRM contact keeps its number raw in identities.phone, with or without the +.
|
|
91
|
-
phone: "SELECT l.person_id FROM identities i JOIN identity_links l ON l.identity_id = i.id " +
|
|
92
|
-
"WHERE i.provider = 'phone' AND i.external_id = ? UNION " +
|
|
93
|
-
"SELECT l.person_id FROM identities i JOIN identity_links l ON l.identity_id = i.id WHERE i.phone IN (?, ?)",
|
|
94
|
-
contact: "SELECT l.person_id FROM identities i JOIN identity_links l ON l.identity_id = i.id " +
|
|
95
|
-
"WHERE i.provider = ? AND i.external_id = ?",
|
|
96
|
-
};
|
|
97
70
|
/**
|
|
98
|
-
* The entities the identifiers point at, each once.
|
|
99
|
-
*
|
|
71
|
+
* The entities the identifiers point at, each once. A person is matched by the people matcher's lookup, which
|
|
72
|
+
* `people.scanMatches` reads phones and addresses with too.
|
|
100
73
|
*/
|
|
101
74
|
export const entitiesByIdentifiers = (database, type, ids) => {
|
|
102
75
|
const found = new Set();
|
|
@@ -104,14 +77,9 @@ export const entitiesByIdentifiers = (database, type, ids) => {
|
|
|
104
77
|
for (const row of rows)
|
|
105
78
|
found.add(Number(row[column]));
|
|
106
79
|
};
|
|
107
|
-
if (type === "person")
|
|
108
|
-
for (const
|
|
109
|
-
add(
|
|
110
|
-
for (const value of ids.phones)
|
|
111
|
-
add(database.prepare(PERSON_BY.phone).all(value, value, value.slice(1)), "person_id");
|
|
112
|
-
if (ids.contact)
|
|
113
|
-
add(database.prepare(PERSON_BY.contact).all(ids.contact.provider, ids.contact.externalId), "person_id");
|
|
114
|
-
}
|
|
80
|
+
if (type === "person")
|
|
81
|
+
for (const person of personsByIdentifiers(database, ids))
|
|
82
|
+
found.add(person);
|
|
115
83
|
if (type === "organization") {
|
|
116
84
|
const organization = database.prepare("SELECT o.id FROM identities i JOIN organization_identities oi ON oi.identity_id = i.id " +
|
|
117
85
|
"JOIN organizations o ON o.id = oi.organization_id WHERE o.deleted_at IS NULL AND i.provider = ? " +
|