@wirecat/cli-core 0.633.0 → 0.635.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.
Files changed (71) hide show
  1. package/CHANGELOG.md +86 -0
  2. package/dist/background/daily-archive-timer.d.ts +1 -1
  3. package/dist/domain/display-names.d.ts +4 -2
  4. package/dist/domain/display-names.js +5 -2
  5. package/dist/domain/relation-role-labels.d.ts +10 -0
  6. package/dist/domain/relation-role-labels.js +40 -0
  7. package/dist/domain/relations.d.ts +7 -0
  8. package/dist/domain/relations.js +10 -0
  9. package/dist/errors/reasons.js +5 -0
  10. package/dist/index.d.ts +2 -1
  11. package/dist/index.js +2 -1
  12. package/dist/search/groups.d.ts +2 -0
  13. package/dist/search/groups.js +1 -0
  14. package/dist/services/account-groups.d.ts +30 -1
  15. package/dist/services/account-groups.js +27 -0
  16. package/dist/services/index.d.ts +2 -1
  17. package/dist/services/index.js +2 -1
  18. package/dist/services/project-activity.d.ts +45 -0
  19. package/dist/services/project-activity.js +76 -0
  20. package/dist/services/project-status/index.d.ts +11 -1
  21. package/dist/services/project-status/index.js +3 -3
  22. package/dist/store/accounts.d.ts +10 -1
  23. package/dist/store/accounts.js +3 -2
  24. package/dist/store/chats.d.ts +5 -0
  25. package/dist/store/chats.js +6 -0
  26. package/dist/store/exports.d.ts +2 -1
  27. package/dist/store/identity-links.d.ts +21 -0
  28. package/dist/store/identity-splits.d.ts +2 -0
  29. package/dist/store/index.d.ts +3 -3
  30. package/dist/store/keys.d.ts +1 -1
  31. package/dist/store/keys.js +1 -1
  32. package/dist/store/messages.d.ts +13 -0
  33. package/dist/store/messages.js +7 -0
  34. package/dist/store/people.d.ts +10 -4
  35. package/dist/store/sqlite/accounts.d.ts +12 -2
  36. package/dist/store/sqlite/accounts.js +42 -3
  37. package/dist/store/sqlite/address-organization.d.ts +18 -0
  38. package/dist/store/sqlite/address-organization.js +48 -0
  39. package/dist/store/sqlite/chat-activity-counts.d.ts +19 -0
  40. package/dist/store/sqlite/chat-activity-counts.js +26 -0
  41. package/dist/store/sqlite/chats.d.ts +3 -1
  42. package/dist/store/sqlite/chats.js +8 -0
  43. package/dist/store/sqlite/identity-link-rows.d.ts +5 -1
  44. package/dist/store/sqlite/identity-link-rows.js +9 -3
  45. package/dist/store/sqlite/identity-proposals.d.ts +5 -3
  46. package/dist/store/sqlite/identity-proposals.js +66 -7
  47. package/dist/store/sqlite/identity-split-proposals.js +3 -1
  48. package/dist/store/sqlite/mail-findings.js +10 -44
  49. package/dist/store/sqlite/manifest.js +1 -0
  50. package/dist/store/sqlite/migrations.generated.js +6 -0
  51. package/dist/store/sqlite/name-traces.d.ts +5 -2
  52. package/dist/store/sqlite/name-traces.js +8 -5
  53. package/dist/store/sqlite/open-store.js +2 -0
  54. package/dist/store/sqlite/people-list.js +7 -3
  55. package/dist/store/sqlite/person-doubts.d.ts +13 -0
  56. package/dist/store/sqlite/person-doubts.js +12 -8
  57. package/dist/store/sqlite/person-links.d.ts +7 -2
  58. package/dist/store/sqlite/person-links.js +26 -1
  59. package/dist/store/sqlite/person-match-scan.js +74 -4
  60. package/dist/store/sqlite/person-merges.js +3 -7
  61. package/dist/store/sqlite/relation-list.d.ts +3 -1
  62. package/dist/store/sqlite/relation-list.js +16 -9
  63. package/dist/store/sqlite/relation-roles.d.ts +4 -0
  64. package/dist/store/sqlite/relation-roles.js +19 -5
  65. package/dist/store/sqlite/reply-times.d.ts +2 -0
  66. package/dist/store/sqlite/reply-times.js +2 -1
  67. package/dist/store/sqlite/task-titles.d.ts +12 -1
  68. package/dist/store/sqlite/task-titles.js +42 -1
  69. package/dist/store/sqlite/tasks.js +6 -1
  70. package/dist/store/types.d.ts +4 -0
  71. package/package.json +2 -2
@@ -1,6 +1,6 @@
1
1
  import { CliError } from "@wirecat/cli-base";
2
2
  import { currentPerson, mergeSql, SEPARATIONS } from "./merged-persons.js";
3
- import { move, recordOf, renameAfterLeaving, required } from "./person-links.js";
3
+ import { heldBy, move, recordOf, renameAfterLeaving, required } from "./person-links.js";
4
4
  import { persons } from "./schema.js";
5
5
  export const mergeId = (to, from) => `merge:${to}-${from}`;
6
6
  /** The two uids a `merge:<to>-<from>` id names. */
@@ -116,12 +116,8 @@ export const split = (context, uid, refs, by) => {
116
116
  const chosen = [...new Map(refs.map((ref) => [`${ref.provider}:${ref.id}`, ref])).values()];
117
117
  if (chosen.length === 0)
118
118
  throw new CliError("validation_error", "name at least one identity to split off");
119
- const found = chosen.map((ref) => {
120
- const row = required(context, ref);
121
- if (row.personPk !== person)
122
- throw new CliError("validation_error", `${ref.provider}:${ref.id} is not an identity of person:${person}`);
123
- return row;
124
- });
119
+ heldBy(context, uid, chosen);
120
+ const found = chosen.map((ref) => required(context, ref));
125
121
  if (found.length === recordOf(context, person).identities.length)
126
122
  throw new CliError("validation_error", "a split leaves at least one identity with the person");
127
123
  const at = context.now();
@@ -1,4 +1,4 @@
1
- import { type RelationKind, type Standing } from "../../domain/relations.js";
1
+ import { type RelationKind, type RoleList, type Standing } from "../../domain/relations.js";
2
2
  import type { AccountKey } from "../types.js";
3
3
  import type { StoreContext } from "./open.js";
4
4
  import { type KnowledgeRelation } from "./relation-proposals.js";
@@ -35,6 +35,8 @@ export interface RelationListItem extends KnowledgeRelation {
35
35
  inverse?: string | null;
36
36
  /** A stored role off the list its ends take, such as one written before the lists. */
37
37
  custom: boolean;
38
+ /** The list `relationships roles list` offers for this role; `null` where it is free text. */
39
+ roleList: RoleList | null;
38
40
  }
39
41
  export interface RelationList {
40
42
  /** Relationships and pending relationship proposals, filtered and paged in one query (WC-CMD-20). */
@@ -1,11 +1,11 @@
1
1
  import { CliError } from "@wirecat/cli-base";
2
- import { inverseOf, notOverriddenSql, RELATION_KINDS, rolesOf, STANDINGS, } from "../../domain/relations.js";
2
+ import { inverseOf, notOverriddenSql, RELATION_KINDS, ROLE_LISTS, roleListOf, STANDINGS, } from "../../domain/relations.js";
3
3
  import { linkKind } from "./link-kinds.js";
4
4
  import { idsOf } from "./merged-persons.js";
5
5
  import { linkOf } from "./notes.js";
6
6
  import { proposalOperationsOver } from "./proposed-actions.js";
7
7
  import { relationOf } from "./relation-proposals.js";
8
- import { extraRolesOf } from "./relation-roles.js";
8
+ import { ownerRolesOf } from "./relation-roles.js";
9
9
  import { thingOf } from "./things.js";
10
10
  const KINDS = RELATION_KINDS.map(linkKind).join(", ");
11
11
  /** The ends a relationship may have that belong to one account. */
@@ -113,17 +113,24 @@ export const relationListOver = (context) => {
113
113
  return { items, hasMore: rows.length > limit };
114
114
  },
115
115
  };
116
- function custom(kind, from, to, role) {
116
+ /** The list a relationship's role is offered from, and whether its role is off that list and the owner's own. */
117
+ function roleOf(kind, from, to, role) {
117
118
  const fromThing = thingOf(database, from);
118
119
  const toThing = thingOf(database, to);
119
- const roles = fromThing && toThing ? rolesOf(kind, fromThing.type, toThing.type) : undefined;
120
- if (role === null || roles === undefined || roles.includes(role) || !fromThing || !toThing)
121
- return false;
122
- return !extraRolesOf(database, kind, fromThing.type, toThing.type).includes(role);
120
+ const roleList = (fromThing && toThing && roleListOf(kind, fromThing.type, toThing.type)) || null;
121
+ const custom = role !== null &&
122
+ roleList !== null &&
123
+ !ROLE_LISTS[roleList].includes(role) &&
124
+ !ownerRolesOf(database, roleList).some(({ id }) => id === role);
125
+ return { roleList, custom };
123
126
  }
124
127
  function linkItem(id, at) {
125
128
  const relation = relationOf(linkOf(database, database.prepare("SELECT * FROM links WHERE id = ?").get(id)), at);
126
- return { ...relation, source: "link", custom: custom(relation.kind, relation.from, relation.to, relation.role) };
129
+ return {
130
+ ...relation,
131
+ source: "link",
132
+ ...roleOf(relation.kind, relation.from, relation.to, relation.role),
133
+ };
127
134
  }
128
135
  function proposalItem(id) {
129
136
  const action = operations.get(id);
@@ -148,7 +155,7 @@ export const relationListOver = (context) => {
148
155
  endsAt: iso(payload.endsAt),
149
156
  createdAt: action.createdAt,
150
157
  source: "proposal",
151
- custom: custom(payload.kind, payload.from, payload.to, payload.role ?? null),
158
+ ...roleOf(payload.kind, payload.from, payload.to, payload.role ?? null),
152
159
  };
153
160
  }
154
161
  };
@@ -1,9 +1,13 @@
1
+ import { type RoleLabels } from "../../domain/relation-role-labels.js";
1
2
  import { type RoleList } from "../../domain/relations.js";
2
3
  import type { CacheDatabase } from "../driver.js";
3
4
  import type { StoreContext } from "./open.js";
4
5
  export interface RelationRole {
5
6
  id: string;
7
+ /** The owner's own word for his role; a built-in one has none and reads by `labels`. */
6
8
  label: string | null;
9
+ /** A built-in role in English, Russian and Spanish; `null` for the owner's own. */
10
+ labels: RoleLabels | null;
7
11
  builtIn: boolean;
8
12
  }
9
13
  export interface RelationRoles {
@@ -1,4 +1,5 @@
1
1
  import { CliError } from "@wirecat/cli-base";
2
+ import { ROLE_LABELS } from "../../domain/relation-role-labels.js";
2
3
  import { checkRole, ROLE_LISTS, roleListOf } from "../../domain/relations.js";
3
4
  import { write } from "./transaction.js";
4
5
  const settingOf = (list) => `relationships.roles.${list}`;
@@ -29,19 +30,27 @@ export const extraRolesOf = (database, kind, from, to) => {
29
30
  /** `checkRole` with the owner's own roles for the list the ends take. */
30
31
  export const checkStoredRole = (database, kind, from, to, role) => checkRole(kind, from, to, role, extraRolesOf(database, kind, from, to));
31
32
  /** The `related-to` links whose role comes from a list, by the types that name it. */
32
- const RELATED_FROM = { deal: "'deal', 'lead'", contract: "'contract'" };
33
+ const RELATED_FROM = {
34
+ deal: "'deal', 'lead'",
35
+ contract: "'contract'",
36
+ related: "'person', 'organization'",
37
+ };
33
38
  /**
34
39
  * The links whose role comes from this list: a person's on an organization or a project, a deal's or a lead's people,
35
40
  * a contract's parties.
36
41
  */
37
42
  const usedBy = (database, list, role) => {
38
43
  const related = RELATED_FROM[list];
44
+ const parties = list === "related";
39
45
  const links = related
40
- ? `kind = 'related-to' AND from_type IN (${related})`
46
+ ? `kind = 'related-to' AND from_type IN (${related})${parties ? ` AND to_type IN (${related})` : ""}`
41
47
  : `kind = 'member-of' AND from_type = 'person' AND to_type = '${list}'`;
42
48
  // A pending proposal would make the role a relationship's once approved: it counts as a use.
43
49
  const proposals = related
44
- ? `target_type IN (${related}) AND json_extract(payload, '$.kind') = 'related-to'`
50
+ ? `target_type IN (${related}) AND json_extract(payload, '$.kind') = 'related-to'` +
51
+ (parties
52
+ ? " AND (json_extract(payload, '$.to') LIKE 'person:%' OR json_extract(payload, '$.to') LIKE 'organization:%')"
53
+ : "")
45
54
  : `target_type = 'person' AND json_extract(payload, '$.kind') = 'member-of' AND json_extract(payload, '$.to') LIKE '${list}:%'`;
46
55
  return Number(database
47
56
  .prepare(`SELECT (SELECT count(*) FROM links WHERE role = ? AND deleted_at IS NULL AND ${links}) + (SELECT count(*) FROM proposed_actions ` +
@@ -51,8 +60,13 @@ const usedBy = (database, list, role) => {
51
60
  };
52
61
  export const relationRolesOver = ({ database, now }) => {
53
62
  const all = (list) => [
54
- ...ROLE_LISTS[list].map((id) => ({ id, label: null, builtIn: true })),
55
- ...ownerRolesOf(database, list).map((role) => ({ ...role, builtIn: false })),
63
+ ...ROLE_LISTS[list].map((id) => ({
64
+ id,
65
+ label: null,
66
+ labels: ROLE_LABELS[list][id],
67
+ builtIn: true,
68
+ })),
69
+ ...ownerRolesOf(database, list).map((role) => ({ ...role, labels: null, builtIn: false })),
56
70
  ];
57
71
  const save = (list, roles) => database
58
72
  .prepare("INSERT OR REPLACE INTO store_settings (key, value, at) VALUES (?, ?, ?)")
@@ -31,6 +31,8 @@ export interface NewestMessage {
31
31
  /** Muted or archived, and not marked unread: what `inbox` leaves out. */
32
32
  quiet: boolean;
33
33
  }
34
+ /** Muted now, until the mute runs out; one bound parameter, now. */
35
+ export declare const MUTED_NOW = "(coalesce(c.muted,0)=1 AND (c.muted_until IS NULL OR c.muted_until>?))";
34
36
  /**
35
37
  * One row per reply in one-to-one chats. Each chat is read from the start of the run before the first message in the
36
38
  * period, so a reply early in the period is timed from the first message it answers, however old.
@@ -1,7 +1,8 @@
1
1
  import { PEER_KIND } from "./lucene-fields.js";
2
2
  import { read } from "./transaction.js";
3
3
  const FAR = Number.MAX_SAFE_INTEGER;
4
- const MUTED_NOW = "(coalesce(c.muted,0)=1 AND (c.muted_until IS NULL OR c.muted_until>?))";
4
+ /** Muted now, until the mute runs out; one bound parameter, now. */
5
+ export const MUTED_NOW = "(coalesce(c.muted,0)=1 AND (c.muted_until IS NULL OR c.muted_until>?))";
5
6
  const chatsIn = (scope, kinds, now) => {
6
7
  const params = [...scope.accounts.flatMap(({ provider, account }) => [provider, account]), ...kinds];
7
8
  const parts = [
@@ -6,7 +6,18 @@ export declare const placeholderTitleOf: (row: Row) => string;
6
6
  export declare const PLACEHOLDER_TITLE_SQL = "title = type || ' \u2014 ' || coalesce(source_locator, '')";
7
7
  /**
8
8
  * What to show as a task's title: the first line of its source message while the stored title is still the
9
- * placeholder, the stored title otherwise. Read time only, so a deleted message leaves nothing behind.
9
+ * placeholder, the stored title otherwise. A found task stores it when it is found, and store version 65 stored it for
10
+ * the tasks found before; read time covers a task found before its message was saved.
10
11
  */
11
12
  export declare const displayTitleOf: (database: CacheDatabase, row: Row) => string;
13
+ /** Set by store version 65 and deleted once every found task's title is stored. */
14
+ export declare const FOUND_TASK_TITLES_TO_FILL = "found_task_titles_to_fill";
15
+ /**
16
+ * Stores the title `displayTitleOf` shows for each live found task still titled `<kind> — <source>`, so it stays
17
+ * when the message is deleted. One short write per batch; a task whose message is missing, deleted or empty keeps
18
+ * its title. A failure keeps the key for the next open and only warns.
19
+ */
20
+ export declare const fillFoundTaskTitles: (database: CacheDatabase, warn: (line: string) => void, { batch }?: {
21
+ batch?: number;
22
+ }) => number;
12
23
  export {};
@@ -1,5 +1,7 @@
1
1
  import { isLocator } from "../../domain/locator.js";
2
+ import { folded } from "./folded.js";
2
3
  import { thingOf } from "./things.js";
4
+ import { writeBatch } from "./transaction.js";
3
5
  const MAX_TITLE = 120;
4
6
  /** The title a rule gives a task it finds in a message: no message text is stored, so it is only a reference. */
5
7
  export const placeholderTitleOf = (row) => `${String(row.type)} — ${String(row.source_locator ?? "")}`;
@@ -20,7 +22,8 @@ const firstLine = (text) => {
20
22
  };
21
23
  /**
22
24
  * What to show as a task's title: the first line of its source message while the stored title is still the
23
- * placeholder, the stored title otherwise. Read time only, so a deleted message leaves nothing behind.
25
+ * placeholder, the stored title otherwise. A found task stores it when it is found, and store version 65 stored it for
26
+ * the tasks found before; read time covers a task found before its message was saved.
24
27
  */
25
28
  export const displayTitleOf = (database, row) => {
26
29
  const stored = String(row.title);
@@ -40,3 +43,41 @@ export const displayTitleOf = (database, row) => {
40
43
  return stored;
41
44
  }
42
45
  };
46
+ /** Set by store version 65 and deleted once every found task's title is stored. */
47
+ export const FOUND_TASK_TITLES_TO_FILL = "found_task_titles_to_fill";
48
+ /**
49
+ * Stores the title `displayTitleOf` shows for each live found task still titled `<kind> — <source>`, so it stays
50
+ * when the message is deleted. One short write per batch; a task whose message is missing, deleted or empty keeps
51
+ * its title. A failure keeps the key for the next open and only warns.
52
+ */
53
+ export const fillFoundTaskTitles = (database, warn, { batch = 500 } = {}) => {
54
+ if (!database.prepare("SELECT 1 AS found FROM store_settings WHERE key = ?").get(FOUND_TASK_TITLES_TO_FILL))
55
+ return 0;
56
+ const next = database.prepare("SELECT id, type, title, source_locator FROM tasks WHERE id > ? AND package_id IS NOT NULL AND deleted_at IS NULL " +
57
+ `AND source_locator LIKE 'msg:%' AND ${PLACEHOLDER_TITLE_SQL} ORDER BY id LIMIT ?`);
58
+ const store = database.prepare(`UPDATE tasks SET title = ?, title_folded = ? WHERE id = ? AND ${PLACEHOLDER_TITLE_SQL}`);
59
+ let filled = 0;
60
+ let after = 0;
61
+ try {
62
+ for (;;) {
63
+ const rows = writeBatch(database, () => {
64
+ const page = next.all(after, batch);
65
+ for (const row of page) {
66
+ const title = displayTitleOf(database, row);
67
+ if (title !== String(row.title))
68
+ filled += store.run(title, folded(title), Number(row.id)).changes;
69
+ }
70
+ if (page.length < batch)
71
+ database.prepare("DELETE FROM store_settings WHERE key = ?").run(FOUND_TASK_TITLES_TO_FILL);
72
+ return page;
73
+ });
74
+ if (rows.length < batch)
75
+ return filled;
76
+ after = Number(rows.at(-1)?.id);
77
+ }
78
+ }
79
+ catch (error) {
80
+ warn(`could not store the titles of found tasks; the next open tries again: ${String(error)}`);
81
+ return filled;
82
+ }
83
+ };
@@ -161,12 +161,17 @@ export const taskStoreOver = (context) => {
161
161
  const key = typedKey(database, task.kind);
162
162
  const author = actorOfOrigin(database, task.origin);
163
163
  const closedBy = task.closedBy ? actorOfOrigin(database, task.closedBy) : undefined;
164
+ const title = displayTitleOf(database, {
165
+ type: task.kind,
166
+ title: `${task.kind} — ${task.source}`,
167
+ source_locator: task.source,
168
+ });
164
169
  const made = database
165
170
  .prepare("INSERT INTO tasks (project_id, number, key, title, type, status, due_at, closed_at, closed_by_type, closed_by_id, " +
166
171
  "close_reason, author_type, author_id, source, package_id, source_locator, source_kind, source_group, created_at, updated_at, " +
167
172
  "title_folded, close_reason_folded, category) " +
168
173
  "VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) RETURNING id")
169
- .get(project, number, key, `${task.kind} — ${task.source}`, task.kind, task.state, time(task.dueAt), time(task.closedAt), closedBy?.type ?? null, closedBy?.id ?? null, task.reason ?? null, author.type, author.id, task.origin, task.id, task.source, task.sourceKind, task.group, task.createdAt.getTime(), now(), folded(`${task.kind} — ${task.source}`), folded(task.reason), task.category ?? null);
174
+ .get(project, number, key, title, task.kind, task.state, time(task.dueAt), time(task.closedAt), closedBy?.type ?? null, closedBy?.id ?? null, task.reason ?? null, author.type, author.id, task.origin, task.id, task.source, task.sourceKind, task.group, task.createdAt.getTime(), now(), folded(title), folded(task.reason), task.category ?? null);
170
175
  recordTaskEvent(database, Number(made?.id), author, "created", null, task.createdAt.getTime());
171
176
  });
172
177
  },
@@ -47,6 +47,10 @@ export interface LinkOptions {
47
47
  method: string;
48
48
  by: string;
49
49
  }
50
+ export interface UnlinkOptions extends LinkOptions {
51
+ /** The uid of the person the identity is taken from; refused when it sits on someone else. */
52
+ from?: string;
53
+ }
50
54
  export interface LinkedIdentity extends IdentityRef {
51
55
  name: string | null;
52
56
  username: string | null;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@wirecat/cli-core",
3
- "version": "0.633.0",
3
+ "version": "0.635.0",
4
4
  "description": "The messenger-neutral half of a messaging CLI: domain model, message locators, rendering, name resolution, the SQLite seam, the command skeleton and shared read commands, the send guard, run records and the message store",
5
5
  "license": "Apache-2.0",
6
6
  "author": "Viacheslav Ptsarev",
@@ -15,7 +15,7 @@
15
15
  "type": "module",
16
16
  "wirecat": {
17
17
  "store": {
18
- "speaks": 64,
18
+ "speaks": 65,
19
19
  "minCompatible": 56
20
20
  }
21
21
  },