@wirecat/cli-core 0.617.0 → 0.619.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 +406 -1
- package/README.md +1 -1
- package/THIRD_PARTY_NOTICES +3 -0
- package/dist/background/backfill.d.ts +21 -0
- package/dist/background/backfill.js +41 -0
- package/dist/background/daily-archive-timer.d.ts +1 -1
- package/dist/background/deliver.d.ts +5 -2
- package/dist/background/deliver.js +30 -12
- package/dist/background/report/messages.js +4 -1
- package/dist/background/report/render.d.ts +3 -1
- package/dist/background/report/render.js +3 -1
- package/dist/background/report/view.js +4 -1
- package/dist/background/schedules.d.ts +17 -0
- package/dist/background/schedules.js +21 -8
- 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/index.d.ts +1 -1
- package/dist/calendar/index.js +1 -1
- package/dist/calendar/mcp/index.d.ts +12 -12
- package/dist/cli/base-stages-command.d.ts +22 -0
- package/dist/cli/base-stages-command.js +174 -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.d.ts +5 -0
- package/dist/cli/context.js +1 -0
- package/dist/cli/group-lock.d.ts +42 -0
- package/dist/cli/group-lock.js +130 -0
- package/dist/cli/index.d.ts +10 -4
- package/dist/cli/index.js +10 -4
- package/dist/cli/messenger/backfill-command.js +27 -15
- package/dist/cli/messenger/contacts-command.js +10 -3
- package/dist/cli/messenger/context.js +1 -0
- package/dist/cli/messenger/doctor-command.js +3 -1
- package/dist/cli/messenger/models-command.js +1 -1
- package/dist/cli/messenger/serve-command.js +24 -2
- package/dist/cli/messenger/serve-outdated.d.ts +22 -0
- package/dist/cli/messenger/serve-outdated.js +100 -0
- 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 +46 -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/schedule/schedule-command.d.ts +9 -0
- package/dist/cli/schedule/schedule-command.js +13 -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/store-recover.d.ts +3 -3
- package/dist/cli/store-recover.js +47 -23
- package/dist/cli/store-settings.d.ts +2 -0
- package/dist/cli/store-settings.js +37 -9
- package/dist/crm/cli/index.js +38 -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/links.d.ts +17 -4
- package/dist/domain/links.js +20 -7
- package/dist/domain/mentions.d.ts +2 -12
- package/dist/domain/mentions.js +2 -19
- 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 +27 -2
- package/dist/domain/relations.js +37 -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 +157 -0
- package/dist/entities/contact-context.js +2 -0
- package/dist/entities/deal.d.ts +10 -0
- package/dist/entities/deal.js +2 -0
- package/dist/entities/index.d.ts +23 -3
- 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 +44 -4
- package/dist/fetch/page.js +2 -1
- package/dist/index.d.ts +4 -3
- package/dist/index.js +4 -3
- 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/mcp/index.d.ts +15 -15
- package/dist/meetings/result-items.d.ts +1 -0
- package/dist/meetings/result-items.js +1 -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/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 +7 -2
- package/dist/services/enrichment.js +46 -5
- 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 +15 -2
- package/dist/services/index.js +9 -2
- package/dist/services/ingest.d.ts +12 -0
- package/dist/services/ingest.js +4 -0
- package/dist/services/link-scorers/chat-senders.js +39 -29
- package/dist/services/link-scorers/member-chats.js +4 -3
- package/dist/services/link-scorers/member-dialog.js +5 -7
- package/dist/services/link-scorers/scan.js +17 -8
- package/dist/services/link-scorers/shared.d.ts +6 -2
- package/dist/services/link-scorers/shared.js +6 -2
- package/dist/services/link-scorers/types.d.ts +2 -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/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 +9 -1
- package/dist/services/person-context.js +5 -3
- 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-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/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/migrations.d.ts +1 -1
- package/dist/store/migrations.js +35 -12
- package/dist/store/people.d.ts +22 -0
- package/dist/store/people.js +15 -1
- package/dist/store/recover.d.ts +34 -0
- package/dist/store/recover.js +188 -6
- package/dist/store/refill.d.ts +28 -0
- package/dist/store/refill.js +120 -0
- package/dist/store/sqlite/accounts.js +2 -0
- 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/clients.d.ts +7 -3
- package/dist/store/sqlite/clients.js +11 -7
- 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 +17 -15
- 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/knowledge.js +5 -3
- 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/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/note-index.js +9 -7
- package/dist/store/sqlite/notes.d.ts +2 -3
- package/dist/store/sqlite/notes.js +6 -29
- 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/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 +20 -2
- package/dist/store/sqlite/relation-proposals.d.ts +18 -1
- package/dist/store/sqlite/relation-proposals.js +53 -11
- 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.d.ts +13 -0
- package/dist/store/sqlite/transaction.js +33 -15
- 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 +11 -4
- package/schemas/records-envelope.v1.schema.json +3 -0
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import type { AppIdentity } from "../ports/app.js";
|
|
2
|
+
import { type GroupMembership } from "../search/groups.js";
|
|
3
|
+
import type { AccountKey, MessageStore } from "../store/store.js";
|
|
4
|
+
/** The store, the account running and its app: what every group edit resolves members against. */
|
|
5
|
+
export interface GroupStore {
|
|
6
|
+
store: MessageStore;
|
|
7
|
+
account: AccountKey;
|
|
8
|
+
app?: AppIdentity;
|
|
9
|
+
/** The run's environment: under a group lock only that group is listed and changed. */
|
|
10
|
+
env?: NodeJS.ProcessEnv;
|
|
11
|
+
}
|
|
12
|
+
export interface GroupMemberChange {
|
|
13
|
+
group: string;
|
|
14
|
+
/** One account, as `--source` names it: `telegram:102`, `tg:work`, `email:me@work.example`. */
|
|
15
|
+
member: string;
|
|
16
|
+
/** Words that may not name a group, such as memo's command words. */
|
|
17
|
+
reserved?: ReadonlySet<string>;
|
|
18
|
+
}
|
|
19
|
+
export interface GroupMemberMove extends Omit<GroupMemberChange, "group"> {
|
|
20
|
+
from: string;
|
|
21
|
+
to: string;
|
|
22
|
+
}
|
|
23
|
+
/** Every group with its members, why each is in, its profiles and other groups, the duplicates and the suggestions. */
|
|
24
|
+
export declare const listAccountGroups: (at: GroupStore) => Promise<GroupMembership>;
|
|
25
|
+
export declare const addGroupMember: (at: GroupStore, change: GroupMemberChange) => Promise<GroupMembership>;
|
|
26
|
+
export declare const removeGroupMember: (at: GroupStore, change: GroupMemberChange) => Promise<GroupMembership>;
|
|
27
|
+
/** Out of one group and into another in one write: what a drag between groups does. */
|
|
28
|
+
export declare const moveGroupMember: (at: GroupStore, move: GroupMemberMove) => Promise<GroupMembership>;
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
import { CliError } from "@wirecat/cli-base";
|
|
2
|
+
import { accountsOfList, groupName, sameAs, sourceOf } from "../search/account-list.js";
|
|
3
|
+
import { ALL, hasGroup, NOT_GROUP_NAMES } from "../search/groups.js";
|
|
4
|
+
import { groupLockOf, listContextOf } from "./search-kind.js";
|
|
5
|
+
const fail = (code, message) => {
|
|
6
|
+
throw new CliError(code, message);
|
|
7
|
+
};
|
|
8
|
+
const contextOf = ({ store, account, app, env }) => listContextOf(store, account, app, undefined, env);
|
|
9
|
+
/** Under a lock the groups are the owner's to change, never the locked process's. */
|
|
10
|
+
const unlocked = (at) => {
|
|
11
|
+
const lock = groupLockOf(at.app, at.env);
|
|
12
|
+
if (lock !== undefined)
|
|
13
|
+
throw new CliError("permission_error", `this process is locked to group ${lock} — groups are changed without the lock`);
|
|
14
|
+
};
|
|
15
|
+
const usableGroup = (name, reserved = new Set()) => {
|
|
16
|
+
groupName(name);
|
|
17
|
+
if (NOT_GROUP_NAMES.has(name.toLowerCase()) || reserved.has(name))
|
|
18
|
+
fail("validation_error", `"${name}" cannot name a group — it already means something on the command line`);
|
|
19
|
+
return name;
|
|
20
|
+
};
|
|
21
|
+
/** A group holds accounts, so a member is saved as the one account it names, under its stable key. */
|
|
22
|
+
const accountOf = (context, member) => {
|
|
23
|
+
if (member.toLowerCase().startsWith("group:"))
|
|
24
|
+
fail("validation_error", "a group lists accounts, not other groups");
|
|
25
|
+
const accounts = accountsOfList(member, { ...context, groups: {} });
|
|
26
|
+
if (accounts.length !== 1)
|
|
27
|
+
fail("validation_error", `${member} names ${accounts.length} accounts — name one: ${accounts.map(sourceOf).join(", ")}`);
|
|
28
|
+
return accounts[0];
|
|
29
|
+
};
|
|
30
|
+
/**
|
|
31
|
+
* Whether a saved entry is to go when `key` leaves its group: every entry naming only this account does. One that
|
|
32
|
+
* names more, such as a bare `email`, would take the others with it, so the change is refused instead.
|
|
33
|
+
*/
|
|
34
|
+
const namesOnly = (context, key) => (entry) => {
|
|
35
|
+
let accounts;
|
|
36
|
+
try {
|
|
37
|
+
accounts = accountsOfList(entry, { ...context, groups: {} });
|
|
38
|
+
}
|
|
39
|
+
catch {
|
|
40
|
+
return false;
|
|
41
|
+
}
|
|
42
|
+
if (!accounts.some(sameAs(key)))
|
|
43
|
+
return false;
|
|
44
|
+
if (accounts.length > 1)
|
|
45
|
+
fail("validation_error", `${entry} names more than this account — change the group with config set`);
|
|
46
|
+
return true;
|
|
47
|
+
};
|
|
48
|
+
const isIn = (context, group, key) => {
|
|
49
|
+
if (!hasGroup(context.membership.groups, group))
|
|
50
|
+
fail("not_found", `no group "${group}" — groups here: ${Object.keys(context.membership.groups).join(", ")}`);
|
|
51
|
+
return (context.membership.groups[group] ?? []).some(sameAs(key));
|
|
52
|
+
};
|
|
53
|
+
/** Every group with its members, why each is in, its profiles and other groups, the duplicates and the suggestions. */
|
|
54
|
+
export const listAccountGroups = async (at) => (await contextOf(at)).membership;
|
|
55
|
+
export const addGroupMember = async (at, change) => {
|
|
56
|
+
unlocked(at);
|
|
57
|
+
const group = usableGroup(change.group, change.reserved);
|
|
58
|
+
const key = accountOf(await contextOf(at), change.member);
|
|
59
|
+
await at.store.accounts.changeAccountGroups({ add: { group, entry: sourceOf(key) } });
|
|
60
|
+
return listAccountGroups(at);
|
|
61
|
+
};
|
|
62
|
+
export const removeGroupMember = async (at, change) => {
|
|
63
|
+
unlocked(at);
|
|
64
|
+
const group = usableGroup(change.group);
|
|
65
|
+
const context = await contextOf(at);
|
|
66
|
+
const key = accountOf(context, change.member);
|
|
67
|
+
isIn(context, group, key);
|
|
68
|
+
const { removed } = await at.store.accounts.changeAccountGroups({
|
|
69
|
+
remove: { group, drop: namesOnly(context, key) },
|
|
70
|
+
});
|
|
71
|
+
if (removed.length === 0)
|
|
72
|
+
fail("not_found", `${change.member} is not in group ${group}`);
|
|
73
|
+
return listAccountGroups(at);
|
|
74
|
+
};
|
|
75
|
+
/** Out of one group and into another in one write: what a drag between groups does. */
|
|
76
|
+
export const moveGroupMember = async (at, move) => {
|
|
77
|
+
unlocked(at);
|
|
78
|
+
const to = usableGroup(move.to, move.reserved);
|
|
79
|
+
const context = await contextOf(at);
|
|
80
|
+
const key = accountOf(context, move.member);
|
|
81
|
+
if (!isIn(context, move.from, key))
|
|
82
|
+
fail("not_found", `${move.member} is not in group ${move.from}`);
|
|
83
|
+
// Out of `all` nothing can go: a move from it puts the account in `to` and leaves it in every account.
|
|
84
|
+
await at.store.accounts.changeAccountGroups({
|
|
85
|
+
...(move.from === ALL ? {} : { remove: { group: move.from, drop: namesOnly(context, key) } }),
|
|
86
|
+
add: { group: to, entry: sourceOf(key) },
|
|
87
|
+
});
|
|
88
|
+
return listAccountGroups(at);
|
|
89
|
+
};
|
|
@@ -48,6 +48,8 @@ export interface FetchOptions {
|
|
|
48
48
|
}) => void;
|
|
49
49
|
/** Past the history limits for this chat (`--full-history`). */
|
|
50
50
|
fullHistory?: boolean;
|
|
51
|
+
/** `fetchAll` only: read muted chats too (`--with-muted`); `history_muted` decides when unset. */
|
|
52
|
+
withMuted?: boolean;
|
|
51
53
|
/** The chat's kind and size as the messenger lists it, for a chat the store does not hold yet. */
|
|
52
54
|
shape?: ChatShape;
|
|
53
55
|
}
|
|
@@ -89,10 +91,14 @@ export type FetchedAll = {
|
|
|
89
91
|
issue?: ActionableError;
|
|
90
92
|
limited?: "group_too_large" | "older_than_window";
|
|
91
93
|
}[];
|
|
92
|
-
/**
|
|
94
|
+
/**
|
|
95
|
+
* How many chats the history limits left unread (big groups) or read only to the window's start, and how many muted
|
|
96
|
+
* chats were not read (`history_muted`); those are not in `chats` or `items`.
|
|
97
|
+
*/
|
|
93
98
|
limited?: {
|
|
94
99
|
groupTooLarge: number;
|
|
95
100
|
olderThanWindow: number;
|
|
101
|
+
muted?: number;
|
|
96
102
|
};
|
|
97
103
|
};
|
|
98
104
|
/** The local store of messages: what it holds, filling it from the messenger, and reading it out. */
|
package/dist/services/archive.js
CHANGED
|
@@ -147,17 +147,21 @@ export const archiveService = (deps) => {
|
|
|
147
147
|
pushed(deps, "`messages sync --all`");
|
|
148
148
|
const connection = await deps.connection();
|
|
149
149
|
const list = capability(connection, "chats", "list its chats");
|
|
150
|
-
const
|
|
150
|
+
const listed = [];
|
|
151
151
|
for (let offset = 0;;) {
|
|
152
152
|
const page = await patiently(() => list({ offset, limit: 100 }), options.note, options.stop);
|
|
153
|
-
|
|
153
|
+
listed.push(...page.items);
|
|
154
154
|
offset += page.items.length;
|
|
155
155
|
if (!page.hasMore || page.items.length === 0)
|
|
156
156
|
break;
|
|
157
157
|
}
|
|
158
|
-
|
|
158
|
+
listed.sort((one, other) => (other.lastMessageAt ?? "").localeCompare(one.lastMessageAt ?? ""));
|
|
159
|
+
const muted = await mutedSkips(deps, listed, options.withMuted ?? deps.historyMuted ?? false);
|
|
160
|
+
const chats = listed.filter((chat) => !muted.has(chat.id));
|
|
159
161
|
const own = archiveService({ ...deps, withConnection: undefined, connection: async () => connection });
|
|
160
162
|
const answer = { chats: chats.length, fetched: 0, complete: false, items: [] };
|
|
163
|
+
if (muted.size > 0)
|
|
164
|
+
answer.limited = { groupTooLarge: 0, olderThanWindow: 0, muted: muted.size };
|
|
161
165
|
const batch = batchProgress(deps.env);
|
|
162
166
|
const catchingUp = Boolean(options.catchUp ?? deps.searchCatchUp);
|
|
163
167
|
const upToDate = heldToNewest(await deps.store(), await deps.account(), deps.messenger.fetching ?? FETCHING, {
|
|
@@ -222,6 +226,18 @@ export const archiveService = (deps) => {
|
|
|
222
226
|
},
|
|
223
227
|
};
|
|
224
228
|
};
|
|
229
|
+
const mutedNow = (chat, now) => chat.muted === true && (chat.mutedUntil == null || Date.parse(chat.mutedUntil) > now);
|
|
230
|
+
/** The muted chats `--all` leaves out unless asked to read them; a chat kept in full (`--full-history`) is read. */
|
|
231
|
+
const mutedSkips = async (deps, chats, readMuted) => {
|
|
232
|
+
if (readMuted)
|
|
233
|
+
return new Set();
|
|
234
|
+
const now = Date.now();
|
|
235
|
+
const muted = chats.filter((chat) => mutedNow(chat, now)).map((chat) => chat.id);
|
|
236
|
+
if (muted.length === 0)
|
|
237
|
+
return new Set();
|
|
238
|
+
const shapes = await (await deps.store()).chats.historyShapes(await deps.account(), muted);
|
|
239
|
+
return new Set(muted.filter((id) => shapes.get(id)?.included !== true));
|
|
240
|
+
};
|
|
225
241
|
/**
|
|
226
242
|
* Where the history limits stop this chat's read: not at all for a group above the cap, else at the window's start.
|
|
227
243
|
* None for a lifted chat or without limits. A chat the store does not hold yet is judged by the shape the list gave.
|
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
import { CATEGORIES, type Category } from "../domain/categories.js";
|
|
2
|
+
import type { DealKind } from "../domain/deals.js";
|
|
3
|
+
import { type Standing } from "../domain/relations.js";
|
|
4
|
+
import type { AccountKey, KnowledgeRelation, MessageStore } from "../store/index.js";
|
|
5
|
+
/** Items a context section lists before it says `hasMore`. */
|
|
6
|
+
export declare const SECTION_LIMIT = 20;
|
|
7
|
+
/** What one store list answers at most: a read that fills it may hold more. */
|
|
8
|
+
export declare const STORE_PAGE = 500;
|
|
9
|
+
/**
|
|
10
|
+
* A bounded list: the first `limit`, how many there are, and the memo command that lists the rest; `null` when
|
|
11
|
+
* nothing was left out or no one command lists exactly the rest.
|
|
12
|
+
*/
|
|
13
|
+
export interface Section<T> {
|
|
14
|
+
items: T[];
|
|
15
|
+
total: number;
|
|
16
|
+
hasMore: boolean;
|
|
17
|
+
next: string | null;
|
|
18
|
+
}
|
|
19
|
+
export declare const sectionOf: <T>(all: T[], limit: number, next: string | null, { more, total, newest }?: {
|
|
20
|
+
more?: boolean;
|
|
21
|
+
total?: number;
|
|
22
|
+
newest?: boolean;
|
|
23
|
+
}) => Section<T>;
|
|
24
|
+
export declare const keyOf: ({ provider, account }: AccountKey) => string;
|
|
25
|
+
/**
|
|
26
|
+
* The accounts a context reads, the caller's `--source` or group already resolved; every account when none is
|
|
27
|
+
* given. A row no account reported — the owner's, an agent's — is always in it. `left` collects every account whose
|
|
28
|
+
* rows were kept out, so `coverage` can say so. `denied` holds the kinds the profile denies (`deals.list`,
|
|
29
|
+
* `leads.list`): no row, title or count of them is answered, and `coverage` says they were denied.
|
|
30
|
+
*/
|
|
31
|
+
export interface Scope {
|
|
32
|
+
accounts: AccountKey[] | undefined;
|
|
33
|
+
has(account: AccountKey | string | null | undefined): boolean;
|
|
34
|
+
left: Set<string>;
|
|
35
|
+
denied: ReadonlySet<DealKind>;
|
|
36
|
+
/** A ref the answer may name: none of a denied kind. */
|
|
37
|
+
allows(ref: string): boolean;
|
|
38
|
+
}
|
|
39
|
+
export declare const scopeOf: (accounts: AccountKey[] | undefined, denied?: readonly DealKind[]) => Scope;
|
|
40
|
+
export declare const notRejected: <T extends {
|
|
41
|
+
review: string;
|
|
42
|
+
}>(rows: T[]) => T[];
|
|
43
|
+
/** One relationship, as seen from the entity a context is about: a link, or a proposal waiting for the owner. */
|
|
44
|
+
export interface RelationRole {
|
|
45
|
+
/** `link:<id>`; absent for a proposal. */
|
|
46
|
+
link?: string;
|
|
47
|
+
/** The pending `relationship` proposal's id, when it is one (`standing: proposed`). */
|
|
48
|
+
proposal?: string;
|
|
49
|
+
kind: KnowledgeRelation["kind"];
|
|
50
|
+
role: string | null;
|
|
51
|
+
standing: Standing;
|
|
52
|
+
/** The source account that reported it, `<provider>:<account>`. */
|
|
53
|
+
account?: string;
|
|
54
|
+
position?: string;
|
|
55
|
+
derived?: string;
|
|
56
|
+
startsAt?: string;
|
|
57
|
+
endsAt?: string;
|
|
58
|
+
}
|
|
59
|
+
/** Another entity and every relationship joining it to the one asked about: counted once however many. */
|
|
60
|
+
export interface Related {
|
|
61
|
+
ref: string;
|
|
62
|
+
name: string | null;
|
|
63
|
+
roles: RelationRole[];
|
|
64
|
+
}
|
|
65
|
+
/** What a context read, and what it did not and why: `notRead` names a missing source, never fills the gap. */
|
|
66
|
+
export interface Coverage {
|
|
67
|
+
/** `<provider>:<account>` of every account read. */
|
|
68
|
+
accounts: string[];
|
|
69
|
+
notRead: {
|
|
70
|
+
source: string;
|
|
71
|
+
reason: string;
|
|
72
|
+
}[];
|
|
73
|
+
}
|
|
74
|
+
/** A relationship's two ends and what it is, whether a link or a proposal. */
|
|
75
|
+
export interface Edge {
|
|
76
|
+
from: string;
|
|
77
|
+
to: string;
|
|
78
|
+
role: RelationRole;
|
|
79
|
+
}
|
|
80
|
+
/**
|
|
81
|
+
* Every relationship touching `ref`, both ways, in scope and of no denied kind: its links with their standing, ended
|
|
82
|
+
* ones too, and the `relationship` proposals waiting for the owner (plan §2.4), read through `listRelations`. `full`
|
|
83
|
+
* when more than a store page holds.
|
|
84
|
+
*/
|
|
85
|
+
export declare const edgesOf: (store: MessageStore, ref: string, scope: Scope) => Promise<{
|
|
86
|
+
edges: Edge[];
|
|
87
|
+
full: boolean;
|
|
88
|
+
}>;
|
|
89
|
+
/** A title for each ref, read in one batch. */
|
|
90
|
+
export declare const namesOf: (store: MessageStore, refs: Iterable<string>) => Promise<Map<string, string | null>>;
|
|
91
|
+
/**
|
|
92
|
+
* The other ends of `edges` that `pick` keeps, each once with all its roles: the usable ones first, then those
|
|
93
|
+
* waiting for the owner, then the ended ones.
|
|
94
|
+
*/
|
|
95
|
+
export declare const grouped: (edges: Edge[], pick: (edge: Edge) => string | undefined, names: Map<string, string | null>) => Related[];
|
|
96
|
+
export declare const usable: (edge: Edge) => boolean;
|
|
97
|
+
export { CATEGORIES, type Category };
|
|
98
|
+
/**
|
|
99
|
+
* Each organization's categories, derived from usable relationships in scope and never stored (NEED-2377): `client`
|
|
100
|
+
* is `customer-of` an own organization, `supplier` one an own organization is `customer-of`, `partner` `partner-of`
|
|
101
|
+
* one, `employer` one the owner's person is `member-of` as employee, contractor or founder. Read once per answer.
|
|
102
|
+
*/
|
|
103
|
+
export declare const organizationCategories: (store: MessageStore, scope: Scope) => Promise<Map<string, Category[]>>;
|
|
104
|
+
/**
|
|
105
|
+
* The answer's `coverage`: what was read, each account kept out by the scope, and what each CRM account in scope
|
|
106
|
+
* said its last sync could not read, such as reporting lines with no department list. Named, never filled in.
|
|
107
|
+
*/
|
|
108
|
+
export declare const coverageFor: (store: MessageStore, base: Coverage, scope: Scope) => Promise<Coverage>;
|
|
109
|
+
/** Newest first; one with no date last, in a fixed order. */
|
|
110
|
+
export declare const newestFirst: <T extends {
|
|
111
|
+
at: string | null;
|
|
112
|
+
ref: string;
|
|
113
|
+
}>(a: T, b: T) => number;
|
|
@@ -0,0 +1,156 @@
|
|
|
1
|
+
import { crmGapsOf } from "../crm/index.js";
|
|
2
|
+
import { CATEGORIES } from "../domain/categories.js";
|
|
3
|
+
import { isUsable } from "../domain/relations.js";
|
|
4
|
+
/** Items a context section lists before it says `hasMore`. */
|
|
5
|
+
export const SECTION_LIMIT = 20;
|
|
6
|
+
/** What one store list answers at most: a read that fills it may hold more. */
|
|
7
|
+
export const STORE_PAGE = 500;
|
|
8
|
+
export const sectionOf = (all, limit, next, { more = false, total = all.length, newest = false } = {}) => {
|
|
9
|
+
const cut = more || total > limit || all.length > limit;
|
|
10
|
+
return {
|
|
11
|
+
items: newest ? all.slice(Math.max(0, all.length - limit)) : all.slice(0, limit),
|
|
12
|
+
total,
|
|
13
|
+
hasMore: cut,
|
|
14
|
+
next: cut ? next : null,
|
|
15
|
+
};
|
|
16
|
+
};
|
|
17
|
+
export const keyOf = ({ provider, account }) => `${provider}:${account}`;
|
|
18
|
+
export const scopeOf = (accounts, denied = []) => {
|
|
19
|
+
const asked = accounts && new Set(accounts.map(keyOf));
|
|
20
|
+
const left = new Set();
|
|
21
|
+
const kinds = new Set(denied);
|
|
22
|
+
return {
|
|
23
|
+
accounts,
|
|
24
|
+
left,
|
|
25
|
+
denied: kinds,
|
|
26
|
+
has: (account) => {
|
|
27
|
+
if (account == null)
|
|
28
|
+
return true;
|
|
29
|
+
const key = typeof account === "string" ? account : keyOf(account);
|
|
30
|
+
if (asked === undefined || asked.has(key))
|
|
31
|
+
return true;
|
|
32
|
+
left.add(key);
|
|
33
|
+
return false;
|
|
34
|
+
},
|
|
35
|
+
allows: (ref) => !kinds.has(ref.slice(0, ref.indexOf(":"))),
|
|
36
|
+
};
|
|
37
|
+
};
|
|
38
|
+
export const notRejected = (rows) => rows.filter((row) => row.review !== "rejected");
|
|
39
|
+
const roleOf = (relation) => ({
|
|
40
|
+
link: `link:${relation.id}`,
|
|
41
|
+
kind: relation.kind,
|
|
42
|
+
role: relation.role,
|
|
43
|
+
standing: relation.standing,
|
|
44
|
+
...(relation.account === undefined ? {} : { account: relation.account }),
|
|
45
|
+
...(relation.position == null ? {} : { position: relation.position }),
|
|
46
|
+
...(relation.derived === undefined ? {} : { derived: relation.derived }),
|
|
47
|
+
...(relation.startsAt == null ? {} : { startsAt: relation.startsAt }),
|
|
48
|
+
...(relation.endsAt == null ? {} : { endsAt: relation.endsAt }),
|
|
49
|
+
});
|
|
50
|
+
/**
|
|
51
|
+
* Every relationship touching `ref`, both ways, in scope and of no denied kind: its links with their standing, ended
|
|
52
|
+
* ones too, and the `relationship` proposals waiting for the owner (plan §2.4), read through `listRelations`. `full`
|
|
53
|
+
* when more than a store page holds.
|
|
54
|
+
*/
|
|
55
|
+
export const edgesOf = async (store, ref, scope) => {
|
|
56
|
+
const { items, hasMore } = await store.knowledge.listRelations({
|
|
57
|
+
about: ref,
|
|
58
|
+
includeEnded: true,
|
|
59
|
+
deniedKinds: [...scope.denied],
|
|
60
|
+
limit: STORE_PAGE,
|
|
61
|
+
...(scope.accounts === undefined ? {} : { accounts: scope.accounts }),
|
|
62
|
+
});
|
|
63
|
+
const edges = items.map((relation) => ({
|
|
64
|
+
from: relation.from,
|
|
65
|
+
to: relation.to,
|
|
66
|
+
role: relation.source === "proposal"
|
|
67
|
+
? { proposal: relation.id, kind: relation.kind, role: relation.role, standing: relation.standing }
|
|
68
|
+
: roleOf(relation),
|
|
69
|
+
}));
|
|
70
|
+
return { edges, full: hasMore };
|
|
71
|
+
};
|
|
72
|
+
/** A title for each ref, read in one batch. */
|
|
73
|
+
export const namesOf = async (store, refs) => {
|
|
74
|
+
const summaries = await store.refs.summaries([...new Set(refs)]);
|
|
75
|
+
return new Map([...summaries].map(([ref, summary]) => [ref, summary.title]));
|
|
76
|
+
};
|
|
77
|
+
/**
|
|
78
|
+
* The other ends of `edges` that `pick` keeps, each once with all its roles: the usable ones first, then those
|
|
79
|
+
* waiting for the owner, then the ended ones.
|
|
80
|
+
*/
|
|
81
|
+
export const grouped = (edges, pick, names) => {
|
|
82
|
+
const byRef = new Map();
|
|
83
|
+
for (const edge of edges) {
|
|
84
|
+
const ref = pick(edge);
|
|
85
|
+
if (ref === undefined)
|
|
86
|
+
continue;
|
|
87
|
+
const held = byRef.get(ref) ?? { ref, name: names.get(ref) ?? null, roles: [] };
|
|
88
|
+
held.roles.push(edge.role);
|
|
89
|
+
byRef.set(ref, held);
|
|
90
|
+
}
|
|
91
|
+
const rank = (one) => one.roles.some((role) => isUsable(role.standing))
|
|
92
|
+
? 0
|
|
93
|
+
: one.roles.some((role) => role.standing === "proposed")
|
|
94
|
+
? 1
|
|
95
|
+
: 2;
|
|
96
|
+
return [...byRef.values()].sort((a, b) => rank(a) - rank(b));
|
|
97
|
+
};
|
|
98
|
+
export const usable = (edge) => isUsable(edge.role.standing);
|
|
99
|
+
export { CATEGORIES };
|
|
100
|
+
/** The roles that make an organization the owner's employer. */
|
|
101
|
+
const EMPLOYED = new Set(["employee", "contractor", "founder"]);
|
|
102
|
+
/**
|
|
103
|
+
* Each organization's categories, derived from usable relationships in scope and never stored (NEED-2377): `client`
|
|
104
|
+
* is `customer-of` an own organization, `supplier` one an own organization is `customer-of`, `partner` `partner-of`
|
|
105
|
+
* one, `employer` one the owner's person is `member-of` as employee, contractor or founder. Read once per answer.
|
|
106
|
+
*/
|
|
107
|
+
export const organizationCategories = async (store, scope) => {
|
|
108
|
+
const found = new Map();
|
|
109
|
+
const add = (ref, category) => found.set(ref, (found.get(ref) ?? new Set()).add(category));
|
|
110
|
+
for (const own of await store.knowledge.ownOrganizations())
|
|
111
|
+
for (const edge of (await edgesOf(store, own.ref, scope)).edges) {
|
|
112
|
+
if (!usable(edge))
|
|
113
|
+
continue;
|
|
114
|
+
const other = edge.from === own.ref ? edge.to : edge.to === own.ref ? edge.from : undefined;
|
|
115
|
+
if (!other?.startsWith("organization:"))
|
|
116
|
+
continue;
|
|
117
|
+
if (edge.role.kind === "customer-of")
|
|
118
|
+
add(other, edge.to === own.ref ? "client" : "supplier");
|
|
119
|
+
if (edge.role.kind === "partner-of")
|
|
120
|
+
add(other, "partner");
|
|
121
|
+
}
|
|
122
|
+
const owner = await store.people.owner();
|
|
123
|
+
if (owner)
|
|
124
|
+
for (const edge of (await edgesOf(store, `person:${owner.uid}`, scope)).edges)
|
|
125
|
+
if (usable(edge) &&
|
|
126
|
+
edge.role.kind === "member-of" &&
|
|
127
|
+
edge.to.startsWith("organization:") &&
|
|
128
|
+
EMPLOYED.has(edge.role.role ?? ""))
|
|
129
|
+
add(edge.to, "employer");
|
|
130
|
+
return new Map([...found].map(([ref, categories]) => [ref, CATEGORIES.filter((one) => categories.has(one))]));
|
|
131
|
+
};
|
|
132
|
+
/**
|
|
133
|
+
* The answer's `coverage`: what was read, each account kept out by the scope, and what each CRM account in scope
|
|
134
|
+
* said its last sync could not read, such as reporting lines with no department list. Named, never filled in.
|
|
135
|
+
*/
|
|
136
|
+
export const coverageFor = async (store, base, scope) => {
|
|
137
|
+
const read = new Set(base.accounts);
|
|
138
|
+
const notRead = [...base.notRead];
|
|
139
|
+
const named = new Set(notRead.map((one) => one.source));
|
|
140
|
+
for (const kind of scope.denied)
|
|
141
|
+
notRead.push({ source: `${kind}s`, reason: "denied" });
|
|
142
|
+
if (scope.accounts !== undefined)
|
|
143
|
+
for (const key of await store.accounts.accounts())
|
|
144
|
+
if (!scope.has(key))
|
|
145
|
+
scope.left.add(keyOf(key));
|
|
146
|
+
for (const key of [...scope.left].sort())
|
|
147
|
+
if (!read.has(key) && !named.has(key))
|
|
148
|
+
notRead.push({ source: key, reason: "outside the accounts asked for" });
|
|
149
|
+
for (const key of await store.accounts.accounts())
|
|
150
|
+
if (scope.accounts === undefined || scope.accounts.some((one) => keyOf(one) === keyOf(key)))
|
|
151
|
+
for (const gap of await crmGapsOf(store, key))
|
|
152
|
+
notRead.push({ source: keyOf(key), reason: gap.message });
|
|
153
|
+
return { accounts: [...read].sort(), notRead };
|
|
154
|
+
};
|
|
155
|
+
/** Newest first; one with no date last, in a fixed order. */
|
|
156
|
+
export const newestFirst = (a, b) => (b.at ?? "").localeCompare(a.at ?? "") || a.ref.localeCompare(b.ref);
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import type { AccountKey, LinkedIdentity, MessageStore, PersonRecord, StoredAccount } from "../store/store.js";
|
|
2
|
+
import { type Coverage, type Scope } from "./business-context.js";
|
|
3
|
+
/** A one-to-one chat, a mail or a calendar event with some of the people asked about. */
|
|
4
|
+
export interface Touch {
|
|
5
|
+
kind: "message" | "mail" | "meeting";
|
|
6
|
+
/** A message's or mail's locator, an `event:<id>`. */
|
|
7
|
+
ref: string;
|
|
8
|
+
/** The chat's title, the mail's subject, the event's title. */
|
|
9
|
+
title: string | null;
|
|
10
|
+
at: string | null;
|
|
11
|
+
/** `person:<uid>` of each one asked about it involves. */
|
|
12
|
+
people: string[];
|
|
13
|
+
}
|
|
14
|
+
export interface Communications {
|
|
15
|
+
/** Each message, mail and event once, however many of the people it involves. */
|
|
16
|
+
counts: {
|
|
17
|
+
messages: number;
|
|
18
|
+
mail: number;
|
|
19
|
+
meetings: number;
|
|
20
|
+
};
|
|
21
|
+
/** Newest first: every mail and meeting counted, and each chat's newest `limit` messages. */
|
|
22
|
+
items: Touch[];
|
|
23
|
+
/** A read stopped at its cap: there may be more than `counts` says. */
|
|
24
|
+
hasMore: boolean;
|
|
25
|
+
/** The accounts read, and the sources the people have nothing to read by; the scope's own gaps come after. */
|
|
26
|
+
coverage: Coverage;
|
|
27
|
+
}
|
|
28
|
+
export interface CommunicationsOptions {
|
|
29
|
+
/** ms, inclusive. */
|
|
30
|
+
since?: number;
|
|
31
|
+
/** ms, inclusive. */
|
|
32
|
+
until?: number;
|
|
33
|
+
/** Messages listed per chat; 0 counts only. */
|
|
34
|
+
limit?: number;
|
|
35
|
+
}
|
|
36
|
+
/** The stored accounts that hold a calendar: what a person's events are read from. */
|
|
37
|
+
export declare const calendarsOf: (store: MessageStore, stored: StoredAccount[]) => Promise<StoredAccount[]>;
|
|
38
|
+
/** The accounts read, and each source the people have nothing to read by. */
|
|
39
|
+
export declare const coverageOf: (people: PersonRecord[], read: Iterable<string>, calendars: boolean) => Coverage;
|
|
40
|
+
/** Each identity in each stored account of its provider that saw it, inside the scope. */
|
|
41
|
+
export declare const readsIn: (stored: AccountKey[], record: PersonRecord, scope: Scope) => {
|
|
42
|
+
key: AccountKey;
|
|
43
|
+
identity: LinkedIdentity;
|
|
44
|
+
}[];
|
|
45
|
+
/**
|
|
46
|
+
* What the owner's accounts in scope hold with these people: their one-to-one chats, mail with their address and
|
|
47
|
+
* calendar events they are invited to. A group chat is left out: what another member wrote there is not theirs. The
|
|
48
|
+
* owner is never one of the people read: through him every chat and mail of his would count.
|
|
49
|
+
*/
|
|
50
|
+
export declare const communicationsOf: (store: MessageStore, asked: PersonRecord[], scope: Scope, { since, until, limit }?: CommunicationsOptions) => Promise<Communications>;
|
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
import { formatLocator } from "../domain/locator.js";
|
|
2
|
+
import { kindIn } from "../domain/providers.js";
|
|
3
|
+
import { keyOf, newestFirst } from "./business-context.js";
|
|
4
|
+
import { eventsOnce } from "./person-context.js";
|
|
5
|
+
/** Mail read per address when counting: past it the count says `hasMore`. */
|
|
6
|
+
const MAIL_ROWS = 500;
|
|
7
|
+
const EVENT_ROWS = 1000;
|
|
8
|
+
/** The stored accounts that hold a calendar: what a person's events are read from. */
|
|
9
|
+
export const calendarsOf = async (store, stored) => {
|
|
10
|
+
const held = [];
|
|
11
|
+
for (const account of stored)
|
|
12
|
+
if ((await store.calendar.calendars(account.id)).length > 0)
|
|
13
|
+
held.push(account);
|
|
14
|
+
return held;
|
|
15
|
+
};
|
|
16
|
+
/** The accounts read, and each source the people have nothing to read by. */
|
|
17
|
+
export const coverageOf = (people, read, calendars) => {
|
|
18
|
+
const notRead = [];
|
|
19
|
+
if (!people.some((person) => person.identities.some((one) => kindIn(one.provider) === "messages")))
|
|
20
|
+
notRead.push({ source: "messages", reason: "no messenger identity is linked to them" });
|
|
21
|
+
if (!people.some((person) => person.identities.some((one) => one.provider === "email")))
|
|
22
|
+
notRead.push({ source: "mail", reason: "no mail address is linked to them" });
|
|
23
|
+
if (!calendars)
|
|
24
|
+
notRead.push({ source: "events", reason: "no calendar account in the accounts read" });
|
|
25
|
+
return { accounts: [...new Set(read)].sort(), notRead };
|
|
26
|
+
};
|
|
27
|
+
/** Each identity in each stored account of its provider that saw it, inside the scope. */
|
|
28
|
+
export const readsIn = (stored, record, scope) => record.identities.flatMap((identity) => stored
|
|
29
|
+
.filter((one) => one.provider === identity.provider && identity.accounts.includes(one.account))
|
|
30
|
+
.map((one) => ({ key: { provider: one.provider, account: one.account }, identity }))
|
|
31
|
+
.filter(({ key }) => scope.has(key)));
|
|
32
|
+
/**
|
|
33
|
+
* What the owner's accounts in scope hold with these people: their one-to-one chats, mail with their address and
|
|
34
|
+
* calendar events they are invited to. A group chat is left out: what another member wrote there is not theirs. The
|
|
35
|
+
* owner is never one of the people read: through him every chat and mail of his would count.
|
|
36
|
+
*/
|
|
37
|
+
export const communicationsOf = async (store, asked, scope, { since, until, limit = 0 } = {}) => {
|
|
38
|
+
const owner = (await store.people.owner())?.uid;
|
|
39
|
+
const people = asked.filter((person) => person.uid !== owner);
|
|
40
|
+
const stored = await store.accounts.storedAccounts();
|
|
41
|
+
const ids = new Map(stored.map((one) => [keyOf(one), one.id]));
|
|
42
|
+
const read = new Set();
|
|
43
|
+
const dialogs = new Set();
|
|
44
|
+
const threads = new Map();
|
|
45
|
+
const messages = new Map();
|
|
46
|
+
const mail = new Map();
|
|
47
|
+
const meetings = new Map();
|
|
48
|
+
let messageCount = 0;
|
|
49
|
+
let cut = false;
|
|
50
|
+
const sinceIso = since === undefined ? undefined : new Date(since).toISOString();
|
|
51
|
+
const untilIso = until === undefined ? undefined : new Date(until).toISOString();
|
|
52
|
+
const touch = (into, id, made, uid) => {
|
|
53
|
+
const held = into.get(id) ?? { ...made, people: [] };
|
|
54
|
+
if (!held.people.includes(`person:${uid}`))
|
|
55
|
+
held.people.push(`person:${uid}`);
|
|
56
|
+
into.set(id, held);
|
|
57
|
+
};
|
|
58
|
+
for (const person of people)
|
|
59
|
+
for (const { key, identity } of readsIn(stored, person, scope)) {
|
|
60
|
+
read.add(keyOf(key));
|
|
61
|
+
if (key.provider === "email") {
|
|
62
|
+
const emails = await store.mail.emails({
|
|
63
|
+
accountId: ids.get(keyOf(key)),
|
|
64
|
+
participant: identity.id,
|
|
65
|
+
limit: MAIL_ROWS + 1,
|
|
66
|
+
...(since === undefined ? {} : { since }),
|
|
67
|
+
...(until === undefined ? {} : { until: until + 1 }),
|
|
68
|
+
});
|
|
69
|
+
cut ||= emails.length > MAIL_ROWS;
|
|
70
|
+
for (const email of emails.slice(0, MAIL_ROWS)) {
|
|
71
|
+
const held = mail.get(email.id);
|
|
72
|
+
if (held) {
|
|
73
|
+
touch(mail, email.id, held, person.uid);
|
|
74
|
+
continue;
|
|
75
|
+
}
|
|
76
|
+
if (!threads.has(email.emailThreadId))
|
|
77
|
+
threads.set(email.emailThreadId, (await store.mail.thread(email.emailThreadId))?.thread.externalId ?? null);
|
|
78
|
+
const thread = threads.get(email.emailThreadId);
|
|
79
|
+
const at = email.receivedAt ?? email.sentAt;
|
|
80
|
+
touch(mail, email.id, {
|
|
81
|
+
kind: "mail",
|
|
82
|
+
ref: formatLocator({ ...key, chat: thread ?? String(email.emailThreadId), message: email.externalId }),
|
|
83
|
+
title: email.subject,
|
|
84
|
+
at: at === null ? null : new Date(at).toISOString(),
|
|
85
|
+
}, person.uid);
|
|
86
|
+
}
|
|
87
|
+
continue;
|
|
88
|
+
}
|
|
89
|
+
for (const chat of await store.chats.chatsWith(key, identity.id)) {
|
|
90
|
+
const id = `${keyOf(key)}/${chat.id}`;
|
|
91
|
+
if (chat.kind !== "dialog" || dialogs.has(id))
|
|
92
|
+
continue;
|
|
93
|
+
dialogs.add(id);
|
|
94
|
+
messageCount += await store.messages.countMessages(key, chat.id, {
|
|
95
|
+
...(sinceIso === undefined ? {} : { since: sinceIso }),
|
|
96
|
+
...(untilIso === undefined ? {} : { until: untilIso }),
|
|
97
|
+
});
|
|
98
|
+
if (limit === 0)
|
|
99
|
+
continue;
|
|
100
|
+
const page = await store.messages.messages(key, chat.id, {
|
|
101
|
+
limit,
|
|
102
|
+
...(sinceIso === undefined ? {} : { since: sinceIso }),
|
|
103
|
+
...(until === undefined ? {} : { until }),
|
|
104
|
+
});
|
|
105
|
+
for (const message of page.items)
|
|
106
|
+
touch(messages, `${id}/${message.id}`, {
|
|
107
|
+
kind: "message",
|
|
108
|
+
ref: formatLocator({ ...key, chat: chat.id, message: message.id }),
|
|
109
|
+
title: chat.title,
|
|
110
|
+
at: message.timestamp,
|
|
111
|
+
}, person.uid);
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
const calendars = (await calendarsOf(store, stored)).filter((one) => scope.has(one));
|
|
115
|
+
for (const person of calendars.length === 0 ? [] : people) {
|
|
116
|
+
const found = await eventsOnce(store, {
|
|
117
|
+
personId: Number(person.uid),
|
|
118
|
+
...(scope.accounts === undefined ? {} : { accountIds: calendars.map(({ id }) => id) }),
|
|
119
|
+
...(since === undefined ? {} : { from: since }),
|
|
120
|
+
...(until === undefined ? {} : { to: until }),
|
|
121
|
+
newest: true,
|
|
122
|
+
}, EVENT_ROWS);
|
|
123
|
+
cut ||= found.cut;
|
|
124
|
+
for (const event of found.items)
|
|
125
|
+
touch(meetings, event.id, {
|
|
126
|
+
kind: "meeting",
|
|
127
|
+
ref: `event:${event.id}`,
|
|
128
|
+
title: event.title,
|
|
129
|
+
at: event.startsAt === null ? null : new Date(event.startsAt).toISOString(),
|
|
130
|
+
}, person.uid);
|
|
131
|
+
}
|
|
132
|
+
for (const key of calendars)
|
|
133
|
+
read.add(keyOf(key));
|
|
134
|
+
return {
|
|
135
|
+
counts: { messages: messageCount, mail: mail.size, meetings: meetings.size },
|
|
136
|
+
items: [...messages.values(), ...mail.values(), ...meetings.values()].sort(newestFirst),
|
|
137
|
+
hasMore: cut,
|
|
138
|
+
coverage: coverageOf(people, read, calendars.length > 0),
|
|
139
|
+
};
|
|
140
|
+
};
|