@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
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import type { AccountKey } from "../store/store.js";
|
|
2
|
+
import { type AccountGroups, type ListContext } from "./account-list.js";
|
|
3
|
+
/** The default group: every account, never saved. Reading without a group reads it. */
|
|
4
|
+
export declare const ALL = "all";
|
|
5
|
+
/** memo's own notes: the owner's, so `search notes` reads them in every group, though no group lists them. */
|
|
6
|
+
export declare const OWNER_NOTES: AccountKey;
|
|
7
|
+
/** Words a source list already gives a meaning of its own, so no group may be saved under them. */
|
|
8
|
+
export declare const NOT_GROUP_NAMES: ReadonlySet<string>;
|
|
9
|
+
/** `setting`: a saved group lists it; `all`: the default group, which holds every account. */
|
|
10
|
+
export type GroupReason = "setting" | "all";
|
|
11
|
+
export interface GroupMember extends AccountKey {
|
|
12
|
+
/** Every saved entry that names it; its `provider:account` in `all`. */
|
|
13
|
+
entries: string[];
|
|
14
|
+
reason: GroupReason;
|
|
15
|
+
/** Every profile recorded for the account, as `<app> <profile>`. */
|
|
16
|
+
profiles: string[];
|
|
17
|
+
/** The other saved groups that hold it too: allowed, and shown so the owner sees it. */
|
|
18
|
+
also_in: string[];
|
|
19
|
+
}
|
|
20
|
+
/** A profile name another app's profile or a saved group shares, for an account not in that group: never applied. */
|
|
21
|
+
export interface GroupSuggestion {
|
|
22
|
+
group: string;
|
|
23
|
+
member: string;
|
|
24
|
+
because: string;
|
|
25
|
+
}
|
|
26
|
+
export interface GroupMembership {
|
|
27
|
+
/** The saved groups, then `all`. */
|
|
28
|
+
groups: Record<string, GroupMember[]>;
|
|
29
|
+
/** Each account in more than one saved group, with those groups. */
|
|
30
|
+
duplicates: {
|
|
31
|
+
member: string;
|
|
32
|
+
groups: string[];
|
|
33
|
+
}[];
|
|
34
|
+
suggested: GroupSuggestion[];
|
|
35
|
+
/** Saved entries that name no account. */
|
|
36
|
+
unresolved: string[];
|
|
37
|
+
}
|
|
38
|
+
/** A source-list context with who is in each group and why. */
|
|
39
|
+
export interface GroupContext extends ListContext {
|
|
40
|
+
membership: GroupMembership;
|
|
41
|
+
}
|
|
42
|
+
export declare const hasGroup: (groups: object, name: string) => boolean;
|
|
43
|
+
/**
|
|
44
|
+
* Who is in each group (WC-ACCT-13): a saved `account_groups.<name>` is its whole list, and `all`, never saved, is
|
|
45
|
+
* every account. One account may be in several groups. A profile's name never puts an account in a group; it is
|
|
46
|
+
* only `suggested`.
|
|
47
|
+
*/
|
|
48
|
+
export declare const groupMembership: (context: ListContext) => GroupMembership;
|
|
49
|
+
/** Whether an account is in `group`; every account is in `all`. `context` is a `listContextOf`. */
|
|
50
|
+
export declare const inGroupOf: (context: GroupContext, group: string) => (key: AccountKey) => boolean;
|
|
51
|
+
/** The groups as a source list reads them: the saved entries as written, and `group:all` as every account. */
|
|
52
|
+
export declare const groupEntries: (saved: AccountGroups) => AccountGroups;
|
|
53
|
+
/** The group a run is scoped to, where it was named, and whether a lock pins it. */
|
|
54
|
+
export interface RunGroup {
|
|
55
|
+
name: string;
|
|
56
|
+
from: string;
|
|
57
|
+
locked: boolean;
|
|
58
|
+
}
|
|
59
|
+
export interface GroupRequest {
|
|
60
|
+
source?: string;
|
|
61
|
+
account?: string;
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* A run in a group reads only the group: no `--source` becomes `group:<name>`, a bare provider or kind keeps the
|
|
65
|
+
* group's accounts of it, and an account outside the group is refused, as is a write's `--account`. `context` is a
|
|
66
|
+
* `listContextOf`. Without a lock this is a convenience, not a boundary.
|
|
67
|
+
*/
|
|
68
|
+
export declare const scopeToGroup: (run: RunGroup, request: GroupRequest, context: GroupContext) => GroupRequest;
|
|
@@ -0,0 +1,172 @@
|
|
|
1
|
+
import { CliError } from "@wirecat/cli-base";
|
|
2
|
+
import { INTERNAL_NOTES } from "../domain/providers.js";
|
|
3
|
+
import { accountsOfList, isGroupName, keyOf, sameAs, sourceOf, } from "./account-list.js";
|
|
4
|
+
/** The default group: every account, never saved. Reading without a group reads it. */
|
|
5
|
+
export const ALL = "all";
|
|
6
|
+
/** memo's own notes: the owner's, so `search notes` reads them in every group, though no group lists them. */
|
|
7
|
+
export const OWNER_NOTES = { provider: "notes", account: INTERNAL_NOTES };
|
|
8
|
+
/** Words a source list already gives a meaning of its own, so no group may be saved under them. */
|
|
9
|
+
export const NOT_GROUP_NAMES = new Set([ALL, "bots"]);
|
|
10
|
+
export const hasGroup = (groups, name) => Object.hasOwn(groups, name);
|
|
11
|
+
const membersOf = (entries, context, unresolved) => {
|
|
12
|
+
const members = new Map();
|
|
13
|
+
for (const entry of entries)
|
|
14
|
+
try {
|
|
15
|
+
for (const { provider, account } of accountsOfList(entry, context)) {
|
|
16
|
+
const known = members.get(keyOf({ provider, account }));
|
|
17
|
+
if (known)
|
|
18
|
+
known.entries.push(entry);
|
|
19
|
+
else
|
|
20
|
+
members.set(keyOf({ provider, account }), {
|
|
21
|
+
provider,
|
|
22
|
+
account,
|
|
23
|
+
entries: [entry],
|
|
24
|
+
reason: "setting",
|
|
25
|
+
profiles: [],
|
|
26
|
+
also_in: [],
|
|
27
|
+
});
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
catch (error) {
|
|
31
|
+
// One stale entry must not break every read of every group; the group's own read still names it.
|
|
32
|
+
if (!(error instanceof CliError))
|
|
33
|
+
throw error;
|
|
34
|
+
unresolved.push(entry);
|
|
35
|
+
}
|
|
36
|
+
return [...members.values()];
|
|
37
|
+
};
|
|
38
|
+
/**
|
|
39
|
+
* Who is in each group (WC-ACCT-13): a saved `account_groups.<name>` is its whole list, and `all`, never saved, is
|
|
40
|
+
* every account. One account may be in several groups. A profile's name never puts an account in a group; it is
|
|
41
|
+
* only `suggested`.
|
|
42
|
+
*/
|
|
43
|
+
export const groupMembership = (context) => {
|
|
44
|
+
const plain = { ...context, groups: {} };
|
|
45
|
+
const unresolved = [];
|
|
46
|
+
const groups = {};
|
|
47
|
+
for (const [name, entries] of Object.entries(context.groups))
|
|
48
|
+
if (name !== ALL)
|
|
49
|
+
groups[name] = membersOf(entries, plain, unresolved);
|
|
50
|
+
// Not accounts of anyone's: memo's own notes, and the row a fresh store lacks for the account running (id 0).
|
|
51
|
+
const system = [OWNER_NOTES, ...context.held.filter(({ id }) => id === 0)].map(keyOf);
|
|
52
|
+
const every = accountsOfList(ALL, plain)
|
|
53
|
+
.filter((one) => !system.includes(keyOf(one)))
|
|
54
|
+
.map(({ provider, account }) => ({
|
|
55
|
+
provider,
|
|
56
|
+
account,
|
|
57
|
+
entries: [sourceOf({ provider, account })],
|
|
58
|
+
reason: "all",
|
|
59
|
+
profiles: [],
|
|
60
|
+
also_in: [],
|
|
61
|
+
}));
|
|
62
|
+
const recorded = Object.entries(context.profiles).flatMap(([app, named]) => Object.entries(named).map(([profile, key]) => ({ app, profile, key })));
|
|
63
|
+
const holding = new Map();
|
|
64
|
+
for (const [name, members] of Object.entries(groups))
|
|
65
|
+
for (const one of members)
|
|
66
|
+
holding.set(keyOf(one), [...(holding.get(keyOf(one)) ?? []), name]);
|
|
67
|
+
for (const [name, members] of [...Object.entries(groups), [ALL, every]])
|
|
68
|
+
for (const one of members) {
|
|
69
|
+
one.profiles = recorded.filter(({ key }) => sameAs(key)(one)).map(({ app, profile }) => `${app} ${profile}`);
|
|
70
|
+
one.also_in = (holding.get(keyOf(one)) ?? []).filter((other) => other !== name);
|
|
71
|
+
}
|
|
72
|
+
const duplicates = [...holding.entries()]
|
|
73
|
+
.filter(([, names]) => names.length > 1)
|
|
74
|
+
.map(([key, names]) => ({ member: key.replace("\u0000", ":"), groups: names }));
|
|
75
|
+
// A name only one app uses, such as `default`, says nothing about which accounts belong together.
|
|
76
|
+
const shared = (app, profile) => hasGroup(context.groups, profile) || recorded.some((other) => other.app !== app && other.profile === profile);
|
|
77
|
+
const suggested = recorded
|
|
78
|
+
.filter(({ app, profile, key }) => isGroupName(profile) &&
|
|
79
|
+
!NOT_GROUP_NAMES.has(profile.toLowerCase()) &&
|
|
80
|
+
shared(app, profile) &&
|
|
81
|
+
!holding.get(keyOf(key))?.includes(profile))
|
|
82
|
+
.map(({ app, profile, key }) => ({
|
|
83
|
+
group: profile,
|
|
84
|
+
member: sourceOf(key),
|
|
85
|
+
because: `profile named ${profile} (${app} ${profile})`,
|
|
86
|
+
}));
|
|
87
|
+
return { groups: { ...groups, [ALL]: every }, duplicates, suggested, unresolved };
|
|
88
|
+
};
|
|
89
|
+
/** Whether an account is in `group`; every account is in `all`. `context` is a `listContextOf`. */
|
|
90
|
+
export const inGroupOf = (context, group) => (key) => group === ALL ||
|
|
91
|
+
(hasGroup(context.membership.groups, group) ? (context.membership.groups[group] ?? []) : []).some(sameAs(key));
|
|
92
|
+
/** The groups as a source list reads them: the saved entries as written, and `group:all` as every account. */
|
|
93
|
+
export const groupEntries = (saved) => ({
|
|
94
|
+
...Object.fromEntries(Object.entries(saved).filter(([name]) => name !== ALL)),
|
|
95
|
+
[ALL]: [ALL],
|
|
96
|
+
});
|
|
97
|
+
const fail = (code, message, details) => {
|
|
98
|
+
throw new CliError(code, message, details);
|
|
99
|
+
};
|
|
100
|
+
const unknownGroup = (run, context) => {
|
|
101
|
+
const names = Object.keys(context.groups);
|
|
102
|
+
const unknown = run.from === "first word"
|
|
103
|
+
? `"${run.name}" is neither a command nor a group`
|
|
104
|
+
: `no group "${run.name}" (${run.from})`;
|
|
105
|
+
const suggested = context.membership.suggested.filter(({ group }) => group === run.name);
|
|
106
|
+
const offer = suggested.length
|
|
107
|
+
? `; profiles named ${run.name} suggest ${suggested.map(({ member }) => member).join(", ")}`
|
|
108
|
+
: "";
|
|
109
|
+
return fail("validation_error", `${unknown} — groups here: ${names.join(", ")}${offer}`, {
|
|
110
|
+
groups: names,
|
|
111
|
+
...(suggested.length ? { suggested } : {}),
|
|
112
|
+
});
|
|
113
|
+
};
|
|
114
|
+
/**
|
|
115
|
+
* A run in a group reads only the group: no `--source` becomes `group:<name>`, a bare provider or kind keeps the
|
|
116
|
+
* group's accounts of it, and an account outside the group is refused, as is a write's `--account`. `context` is a
|
|
117
|
+
* `listContextOf`. Without a lock this is a convenience, not a boundary.
|
|
118
|
+
*/
|
|
119
|
+
export const scopeToGroup = (run, request, context) => {
|
|
120
|
+
if (!hasGroup(context.groups, run.name))
|
|
121
|
+
unknownGroup(run, context);
|
|
122
|
+
const code = run.locked ? "permission_error" : "validation_error";
|
|
123
|
+
const why = run.locked
|
|
124
|
+
? `this process is locked to group ${run.name} (${run.from})`
|
|
125
|
+
: `this run is in group ${run.name}`;
|
|
126
|
+
if (!context.membership.groups[run.name]?.length)
|
|
127
|
+
fail("validation_error", `group ${run.name} is empty`);
|
|
128
|
+
const inGroup = new Set(accountsOfList(`group:${run.name}`, context).map(keyOf));
|
|
129
|
+
const outside = (accounts) => accounts.filter((one) => !inGroup.has(keyOf(one)));
|
|
130
|
+
// Under a lock the context holds only the group, so a name outside it fails to resolve: that is the refusal.
|
|
131
|
+
const namedBy = (item) => {
|
|
132
|
+
try {
|
|
133
|
+
return accountsOfList(item, context);
|
|
134
|
+
}
|
|
135
|
+
catch (error) {
|
|
136
|
+
if (run.locked && error instanceof CliError)
|
|
137
|
+
return [];
|
|
138
|
+
throw error;
|
|
139
|
+
}
|
|
140
|
+
};
|
|
141
|
+
const scoped = {};
|
|
142
|
+
if (request.source === undefined)
|
|
143
|
+
scoped.source = `group:${run.name}`;
|
|
144
|
+
else {
|
|
145
|
+
const items = request.source.split(",").map((item) => item.trim());
|
|
146
|
+
if (items.some((item) => item.toLowerCase() === "all"))
|
|
147
|
+
fail(code, `${why} — --source all reads outside it`);
|
|
148
|
+
const kept = [];
|
|
149
|
+
let narrowed = false;
|
|
150
|
+
for (const item of items) {
|
|
151
|
+
const accounts = namedBy(item);
|
|
152
|
+
const away = outside(accounts);
|
|
153
|
+
if (item.includes(":") && (away.length || accounts.length === 0))
|
|
154
|
+
fail(code, `${why} — ${item} is outside it${away.length ? ` (${away.map(sourceOf).join(", ")})` : ""}`);
|
|
155
|
+
if (away.length)
|
|
156
|
+
narrowed = true;
|
|
157
|
+
for (const one of accounts)
|
|
158
|
+
if (inGroup.has(keyOf(one)))
|
|
159
|
+
kept.push(sourceOf(one));
|
|
160
|
+
}
|
|
161
|
+
if (kept.length === 0)
|
|
162
|
+
fail("validation_error", `${why} — --source ${request.source} names none of its accounts`);
|
|
163
|
+
scoped.source = narrowed ? [...new Set(kept)].join(",") : request.source;
|
|
164
|
+
}
|
|
165
|
+
if (request.account !== undefined) {
|
|
166
|
+
const accounts = namedBy(request.account);
|
|
167
|
+
if (accounts.length === 0 || outside(accounts).length)
|
|
168
|
+
fail(code, `${why} — --account ${request.account} is outside it`);
|
|
169
|
+
scoped.account = request.account;
|
|
170
|
+
}
|
|
171
|
+
return scoped;
|
|
172
|
+
};
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/** The time a question asks about, as epoch ms: `from` is the start of its first local day, `to` of the day after it. */
|
|
2
|
+
export interface Period {
|
|
3
|
+
from: number;
|
|
4
|
+
to: number;
|
|
5
|
+
}
|
|
6
|
+
/**
|
|
7
|
+
* The period a question names — a date, a month, "last week", «в прошлом месяце» — from the first day any phrase names
|
|
8
|
+
* to the last; undefined when it names none. Read as `zone`'s calendar on `now`.
|
|
9
|
+
*/
|
|
10
|
+
export declare const questionPeriod: (text: string, now: number, zone: string) => Period | undefined;
|
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
import { dayBoundary } from "@wirecat/cli-base/query";
|
|
2
|
+
const DAY = 86_400_000;
|
|
3
|
+
const SPAN = { start: 0, end: 0 };
|
|
4
|
+
// `\b` knows only ASCII letters, so Russian words need their own edges.
|
|
5
|
+
const phrase = (source) => new RegExp(`(?<![\\p{L}\\p{N}])(?:${source})(?![\\p{L}\\p{N}])`, "giu");
|
|
6
|
+
const MONTHS = [
|
|
7
|
+
/january|январ\p{L}*/u,
|
|
8
|
+
/february|феврал\p{L}*/u,
|
|
9
|
+
/march|март\p{L}*/u,
|
|
10
|
+
/april|апрел\p{L}*/u,
|
|
11
|
+
/may|ма[йяе]/u,
|
|
12
|
+
/june|июн\p{L}*/u,
|
|
13
|
+
/july|июл\p{L}*/u,
|
|
14
|
+
/august|август\p{L}*/u,
|
|
15
|
+
/september|сентябр\p{L}*/u,
|
|
16
|
+
/october|октябр\p{L}*/u,
|
|
17
|
+
/november|ноябр\p{L}*/u,
|
|
18
|
+
/december|декабр\p{L}*/u,
|
|
19
|
+
];
|
|
20
|
+
const MONTH = MONTHS.map(({ source }) => source).join("|");
|
|
21
|
+
const ORDINAL = "(?:st|nd|rd|th)?";
|
|
22
|
+
const DATED_MONTH = phrase(`(?:(\\d{1,2})${ORDINAL}\\s+(?:of\\s+)?)?(${MONTH})(?:\\s+(\\d{1,2})${ORDINAL}(?!\\p{N}))?(?:,?\\s+(\\d{4}))?`);
|
|
23
|
+
// "May I…" and "it may" are not the month; "in May", "May 5" and "May 2025" are.
|
|
24
|
+
const MONTH_BEFORE_MAY = /(?:in|of|since|during|until|by|early|late|mid|from|through)\s+$/iu;
|
|
25
|
+
const monthOf = (name) => MONTHS.findIndex(({ source }) => new RegExp(`^(?:${source})$`, "iu").test(name));
|
|
26
|
+
const monthDays = (year, month) => [Date.UTC(year, month, 1), Date.UTC(year, month + 1, 0)];
|
|
27
|
+
const weekDays = (day) => {
|
|
28
|
+
const sinceMonday = (new Date(day).getUTCDay() + 6) % 7;
|
|
29
|
+
return [day - sinceMonday * DAY, day + (6 - sinceMonday) * DAY];
|
|
30
|
+
};
|
|
31
|
+
const yearDays = (year) => [Date.UTC(year, 0, 1), Date.UTC(year, 11, 31)];
|
|
32
|
+
const unitDays = (unit, day) => {
|
|
33
|
+
const at = new Date(day);
|
|
34
|
+
if (/^(?:day|дн|день)/iu.test(unit))
|
|
35
|
+
return [day, day];
|
|
36
|
+
if (/^(?:week|недел)/iu.test(unit))
|
|
37
|
+
return weekDays(day);
|
|
38
|
+
if (/^(?:month|месяц)/iu.test(unit))
|
|
39
|
+
return monthDays(at.getUTCFullYear(), at.getUTCMonth());
|
|
40
|
+
return yearDays(at.getUTCFullYear());
|
|
41
|
+
};
|
|
42
|
+
const unitsBack = (unit, count, today) => {
|
|
43
|
+
const at = new Date(today);
|
|
44
|
+
if (/^(?:day|дн|день)/iu.test(unit))
|
|
45
|
+
return today - count * DAY;
|
|
46
|
+
if (/^(?:week|недел)/iu.test(unit))
|
|
47
|
+
return today - 7 * count * DAY;
|
|
48
|
+
if (/^(?:month|месяц)/iu.test(unit))
|
|
49
|
+
return Date.UTC(at.getUTCFullYear(), at.getUTCMonth() - count, 1);
|
|
50
|
+
return Date.UTC(at.getUTCFullYear() - count, 0, 1);
|
|
51
|
+
};
|
|
52
|
+
const RULES = [
|
|
53
|
+
[
|
|
54
|
+
/(?<!\p{N})(\d{4})-(\d{2})-(\d{2})(?!\p{N})/gu,
|
|
55
|
+
(part) => {
|
|
56
|
+
const day = Date.UTC(Number(part(1)), Number(part(2)) - 1, Number(part(3)));
|
|
57
|
+
return [day, day];
|
|
58
|
+
},
|
|
59
|
+
],
|
|
60
|
+
[phrase("today|tonight|сегодня"), (_, today) => [today, today]],
|
|
61
|
+
[phrase("yesterday|вчера"), (_, today) => [today - DAY, today - DAY]],
|
|
62
|
+
[phrase("позавчера"), (_, today) => [today - 2 * DAY, today - 2 * DAY]],
|
|
63
|
+
[
|
|
64
|
+
phrase(/(\d+|an?|one)\s+(days?|weeks?|months?|years?)\s+ago|(\d+)\s+(дн\p{L}*|день|недел\p{L}*|месяц\p{L}*|год\p{L}*|лет)\s+назад/u
|
|
65
|
+
.source),
|
|
66
|
+
(part, today) => {
|
|
67
|
+
const n = Number.parseInt(part(1) || part(3), 10) || 1;
|
|
68
|
+
const word = part(2) || part(4);
|
|
69
|
+
return unitDays(word, unitsBack(word, n, today));
|
|
70
|
+
},
|
|
71
|
+
],
|
|
72
|
+
[
|
|
73
|
+
phrase("(?:last|past)\\s+(\\d+)\\s+days|(?:последние|за\\s+последние)\\s+(\\d+)\\s+дн\\p{L}*"),
|
|
74
|
+
(part, today) => [today - (Number(part(1) || part(2)) - 1) * DAY, today],
|
|
75
|
+
],
|
|
76
|
+
[
|
|
77
|
+
phrase("(last|past|previous|this)\\s+(week|weekend|month|year)"),
|
|
78
|
+
(part, today) => {
|
|
79
|
+
const last = part(1).toLowerCase() !== "this";
|
|
80
|
+
const unit = part(2);
|
|
81
|
+
if (unit.toLowerCase() === "weekend") {
|
|
82
|
+
const [monday] = weekDays(last ? today - 7 * DAY : today);
|
|
83
|
+
return [monday + 5 * DAY, monday + 6 * DAY];
|
|
84
|
+
}
|
|
85
|
+
return unitDays(unit, last ? unitsBack(unit, 1, today) : today);
|
|
86
|
+
},
|
|
87
|
+
],
|
|
88
|
+
[
|
|
89
|
+
phrase(/(?:на|в)\s+(прошл|эт)\p{L}*\s+(недел\p{L}*|месяц\p{L}*|год\p{L}*)/u.source),
|
|
90
|
+
(part, today) => unitDays(part(2), part(1).toLowerCase() === "эт" ? today : unitsBack(part(2), 1, today)),
|
|
91
|
+
],
|
|
92
|
+
[
|
|
93
|
+
DATED_MONTH,
|
|
94
|
+
(part, today, match) => {
|
|
95
|
+
const [name, year] = [part(2), part(4)];
|
|
96
|
+
const day = Number(part(1) || part(3) || 0);
|
|
97
|
+
if (/^may$/iu.test(name) && !day && !year && !MONTH_BEFORE_MAY.test(match.input.slice(0, match.index)))
|
|
98
|
+
return;
|
|
99
|
+
const month = monthOf(name);
|
|
100
|
+
const now = new Date(today);
|
|
101
|
+
// A month without a year is its last one up to today: asked in March, "December" is last December.
|
|
102
|
+
const lastYear = month > now.getUTCMonth() || (month === now.getUTCMonth() && day > now.getUTCDate());
|
|
103
|
+
const y = year ? Number(year) : now.getUTCFullYear() - (lastYear ? 1 : 0);
|
|
104
|
+
const [first, last] = monthDays(y, month);
|
|
105
|
+
if (!day || day > new Date(last).getUTCDate())
|
|
106
|
+
return [first, last];
|
|
107
|
+
return [Date.UTC(y, month, day), Date.UTC(y, month, day)];
|
|
108
|
+
},
|
|
109
|
+
],
|
|
110
|
+
];
|
|
111
|
+
/**
|
|
112
|
+
* The period a question names — a date, a month, "last week", «в прошлом месяце» — from the first day any phrase names
|
|
113
|
+
* to the last; undefined when it names none. Read as `zone`'s calendar on `now`.
|
|
114
|
+
*/
|
|
115
|
+
export const questionPeriod = (text, now, zone) => {
|
|
116
|
+
const today = Date.parse(`${new Intl.DateTimeFormat("en-CA", { timeZone: zone }).format(now)}T00:00:00Z`);
|
|
117
|
+
const found = [];
|
|
118
|
+
for (const [pattern, days] of RULES)
|
|
119
|
+
for (const match of text.matchAll(pattern)) {
|
|
120
|
+
const span = days((n) => match[n] ?? "", today, match);
|
|
121
|
+
if (span)
|
|
122
|
+
found.push(span);
|
|
123
|
+
}
|
|
124
|
+
// "In March this year" means March: a span that holds another one named beside it only gives that one its year.
|
|
125
|
+
const inner = found.filter(([first, last]) => !found.some(([f, l]) => f >= first && l <= last && l - f < last - first));
|
|
126
|
+
if (!inner.length)
|
|
127
|
+
return;
|
|
128
|
+
const iso = (day) => new Date(day).toISOString().slice(0, 10);
|
|
129
|
+
return {
|
|
130
|
+
from: dayBoundary(iso(inner.reduce((min, [first]) => Math.min(min, first), Infinity)), zone, false, SPAN),
|
|
131
|
+
to: dayBoundary(iso(inner.reduce((max, [, last]) => Math.max(max, last), -Infinity)), zone, true, SPAN),
|
|
132
|
+
};
|
|
133
|
+
};
|
package/dist/sends/buttons.d.ts
CHANGED
|
@@ -7,6 +7,8 @@ export interface Pressed {
|
|
|
7
7
|
messageId: Id;
|
|
8
8
|
button: Button;
|
|
9
9
|
}
|
|
10
|
+
/** Callback data of a button only a hand on the owner's own device may press, such as a review decision. */
|
|
11
|
+
export declare const OWNER_HAND_ONLY_PREFIX = "wcr";
|
|
10
12
|
/** The button by its number, counted from 1 across the rows as `messages show` prints them, or by its exact text. */
|
|
11
13
|
export declare const pickButton: (keyboard: Button[][], typed: string) => {
|
|
12
14
|
row: number;
|
package/dist/sends/buttons.js
CHANGED
|
@@ -2,6 +2,8 @@ import { CliError } from "@wirecat/cli-base";
|
|
|
2
2
|
import { capability } from "../ports/messenger.js";
|
|
3
3
|
import { guardedWrite } from "./guarded.js";
|
|
4
4
|
import { newOperationId, newSendId } from "./send-id.js";
|
|
5
|
+
/** Callback data of a button only a hand on the owner's own device may press, such as a review decision. */
|
|
6
|
+
export const OWNER_HAND_ONLY_PREFIX = "wcr";
|
|
5
7
|
const REFUSED = {
|
|
6
8
|
contact: "it would hand your phone number to the bot",
|
|
7
9
|
location: "it would hand your location to the bot",
|
|
@@ -40,9 +42,12 @@ export const guardedPress = async (guard, connection, { chat, message, button: t
|
|
|
40
42
|
throw new CliError("validation_error", `not pressed: "${button.text}" is a ${button.kind} button — ${refused}`);
|
|
41
43
|
if (button.kind !== "callback")
|
|
42
44
|
throw new CliError("validation_error", `not pressed: "${button.text}" is a ${button.kind} button, which this CLI cannot press`);
|
|
45
|
+
if (button.data?.startsWith(OWNER_HAND_ONLY_PREFIX))
|
|
46
|
+
throw new CliError("permission_error", `not pressed: "${button.text}" decides something for the owner — press it by hand in the messenger`, { reason: "press.owner-hand-only" });
|
|
43
47
|
const operationId = newOperationId();
|
|
44
48
|
await guardedWrite(guard, { operationId, chatId, kind: "reaction", messageId: message, key: "messages.press" }, () => press(chatId, message, row, column));
|
|
45
|
-
|
|
49
|
+
const { data: _data, ...shown } = button;
|
|
50
|
+
return { operationId, chatId, messageId: message, button: shown };
|
|
46
51
|
};
|
|
47
52
|
/** Starting a bot sends it a message — its start command — so it is guarded like one. */
|
|
48
53
|
export const guardedStart = async (guard, connection, { chat, payload, sendId }) => {
|
|
@@ -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.
|