@wirecat/cli-core 1.1.0 → 1.2.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 (63) hide show
  1. package/CHANGELOG.md +55 -0
  2. package/dist/cli/account-remove.d.ts +3 -0
  3. package/dist/cli/account-remove.js +1 -1
  4. package/dist/cli/documents/index.d.ts +2 -1
  5. package/dist/cli/documents/index.js +4 -1
  6. package/dist/cli/index.d.ts +2 -2
  7. package/dist/cli/index.js +2 -2
  8. package/dist/cli/messenger/conversation-answers.d.ts +2 -0
  9. package/dist/cli/messenger/message-answers.d.ts +2 -0
  10. package/dist/cli/messenger/message-items.js +2 -0
  11. package/dist/cli/messenger/message-writes.d.ts +4 -0
  12. package/dist/cli/messenger/search-answers.js +2 -0
  13. package/dist/cli/oauth-session.js +6 -3
  14. package/dist/cli/shared-command-exports.d.ts +2 -0
  15. package/dist/cli/shared-command-exports.js +3 -0
  16. package/dist/cli/token-session.d.ts +1 -4
  17. package/dist/cli/token-session.js +5 -1
  18. package/dist/domain/models.d.ts +5 -0
  19. package/dist/domain/person-kinds.d.ts +1 -1
  20. package/dist/entities/contact-context.d.ts +4 -0
  21. package/dist/entities/conversation-results.d.ts +4 -0
  22. package/dist/entities/evidence.d.ts +16 -0
  23. package/dist/entities/index.d.ts +36 -0
  24. package/dist/entities/listener.d.ts +8 -0
  25. package/dist/entities/message-change.d.ts +2 -0
  26. package/dist/entities/message-context.d.ts +6 -0
  27. package/dist/entities/message-write.d.ts +8 -0
  28. package/dist/entities/message.d.ts +4 -0
  29. package/dist/entities/message.js +4 -0
  30. package/dist/entities/store-records.d.ts +2 -0
  31. package/dist/errors/reasons.js +16 -0
  32. package/dist/mail/cli/index.js +1 -1
  33. package/dist/mail/headers.d.ts +2 -0
  34. package/dist/mail/headers.js +2 -1
  35. package/dist/mail/read.js +1 -0
  36. package/dist/mail/sort-rules.d.ts +58 -0
  37. package/dist/mail/sort-rules.js +79 -0
  38. package/dist/search/lucene/registry.js +24 -0
  39. package/dist/services/messages-search.js +1 -1
  40. package/dist/store/areas.d.ts +2 -0
  41. package/dist/store/areas.js +2 -0
  42. package/dist/store/public.d.ts +1 -1
  43. package/dist/store/public.js +1 -1
  44. package/dist/store/queries.js +9 -2
  45. package/dist/store/sqlite/email-types.d.ts +8 -0
  46. package/dist/store/sqlite/email-types.js +45 -10
  47. package/dist/store/sqlite/emails.d.ts +3 -0
  48. package/dist/store/sqlite/emails.js +14 -5
  49. package/dist/store/sqlite/lucene-fields.d.ts +6 -0
  50. package/dist/store/sqlite/lucene-fields.js +10 -0
  51. package/dist/store/sqlite/lucene.js +6 -0
  52. package/dist/store/sqlite/mail-sorting.d.ts +122 -0
  53. package/dist/store/sqlite/mail-sorting.js +482 -0
  54. package/dist/store/sqlite/manifest.js +2 -0
  55. package/dist/store/sqlite/migrations.generated.js +20 -0
  56. package/dist/store/sqlite/people-list.js +65 -16
  57. package/dist/store/sqlite/schema/mail-sorting.d.ts +384 -0
  58. package/dist/store/sqlite/schema/mail-sorting.js +31 -0
  59. package/dist/store/sqlite/schema.d.ts +17 -0
  60. package/dist/store/sqlite/schema.js +3 -0
  61. package/dist/store/sqlite/sender-bots.d.ts +10 -1
  62. package/dist/store/sqlite/sender-bots.js +47 -8
  63. package/package.json +2 -2
@@ -5,7 +5,8 @@ import { fold } from "../normalize.js";
5
5
  import { groupByDomain, releaseDomainHolds } from "./domain-organizations.js";
6
6
  import { currentPerson } from "./merged-persons.js";
7
7
  import { isOwner, mergePerson, move, recordOf, renameAfterLeaving } from "./person-links.js";
8
- import { senderSignalsOf, sentMailOf } from "./sender-signals.js";
8
+ import { SMALL_GROUP_MEMBERS } from "./person-ties.js";
9
+ import { LIST_ID, senderSignalsOf, sentMailOf } from "./sender-signals.js";
9
10
  import { read, write } from "./transaction.js";
10
11
  const MIN_QUERY = 3;
11
12
  const HOLDS = "EXISTS (SELECT 1 FROM sender_bot_identities h WHERE h.bot_id = b.id)";
@@ -142,24 +143,62 @@ export const ruleToBot = (database, person, reason, at) => {
142
143
  intoBot(database, identitiesOf(database, person), bot, log);
143
144
  return bot;
144
145
  };
146
+ /** The reason of a bot that only sent a newsletter: a mail written to the owner alone gives it back to a person. */
147
+ export const NEWSLETTER_ONLY = "newsletter-only";
148
+ /** Mail from the identity `s` that no list sent, to the owner and at most a small group. */
149
+ const ONE_TO_ONE = "EXISTS (SELECT 1 FROM emails e WHERE e.from_identity_id = s.identity_id AND e.outgoing IS NOT 1 " +
150
+ `AND e.deleted_at IS NULL AND ${LIST_ID} IS NULL AND json_extract(e.headers, '$."list-unsubscribe"') IS NOT 1 ` +
151
+ "AND json_extract(e.headers, '$.\"list-unsubscribe-post\"') IS NOT 1 " +
152
+ "AND json_extract(e.headers, '$.\"feedback-id\"') IS NOT 1 " +
153
+ "AND json_extract(e.headers, '$.precedence') IS NOT 'bulk' AND json_extract(e.headers, '$.precedence') IS NOT 'junk' " +
154
+ "AND NOT EXISTS (SELECT 1 FROM email_mailboxes m " +
155
+ "JOIN mailboxes x ON x.id = m.mailbox_id WHERE m.email_id = e.id AND x.kind = 'bulk') " +
156
+ "AND (SELECT count(*) FROM email_recipients r WHERE r.email_id = e.id AND r.role IN ('to', 'cc')) " +
157
+ `BETWEEN 1 AND ${SMALL_GROUP_MEMBERS})`;
145
158
  /**
146
- * A rule's bot the owner wrote to, by mail or in a private chat both wrote in, goes back to the people its identities
159
+ * A rule's bot the owner wrote to, or a newsletter's that wrote to the owner alone, by mail or in a private chat both wrote in, goes back to the people its identities
147
160
  * came from; a messenger's own bot flag holds anyway. The caller holds the write lock.
148
161
  */
149
162
  export const ruleBotsBack = (database, at) => {
150
163
  const back = database
151
- .prepare("SELECT b.id FROM sender_bots b WHERE b.author = 'rules' AND NOT EXISTS (SELECT 1 FROM sender_bot_identities s " +
152
- "JOIN identities i ON i.id = s.identity_id WHERE s.bot_id = b.id AND i.bot = 1) " +
164
+ .prepare("SELECT b.id, b.reason FROM sender_bots b WHERE b.author = 'rules' AND NOT EXISTS (SELECT 1 FROM " +
165
+ "sender_bot_identities s JOIN identities i ON i.id = s.identity_id WHERE s.bot_id = b.id AND i.bot = 1) " +
153
166
  "AND EXISTS (SELECT 1 FROM sender_bot_identities s WHERE s.bot_id = b.id AND (" +
154
167
  "EXISTS (SELECT 1 FROM email_recipients r JOIN emails e ON e.id = r.email_id WHERE r.identity_id = s.identity_id " +
155
168
  "AND e.outgoing = 1 AND e.deleted_at IS NULL AND e.draft IS NOT 1) OR " +
156
169
  "EXISTS (SELECT 1 FROM messages m JOIN chats c ON c.id = m.chat_id WHERE m.sender_identity_id = s.identity_id " +
157
170
  "AND m.outgoing IS NOT 1 AND c.kind = 'dialog' AND EXISTS (SELECT 1 FROM messages o WHERE o.chat_id = c.id " +
158
- "AND o.outgoing = 1))))")
171
+ `AND o.outgoing = 1)) OR (b.reason = '${NEWSLETTER_ONLY}' AND ${ONE_TO_ONE})))`)
159
172
  .all();
160
- const log = { author: "rules", reason: "the owner wrote to it", at };
161
- for (const row of back)
162
- outOfBot(database, botIdentitiesOf(database, Number(row.id)), undefined, log);
173
+ for (const row of back) {
174
+ const reason = row.reason === NEWSLETTER_ONLY ? "a 1-to-1 mail or the owner wrote to it" : "the owner wrote to it";
175
+ outOfBot(database, botIdentitiesOf(database, Number(row.id)), undefined, { author: "rules", reason, at });
176
+ }
177
+ };
178
+ const MAIL_CHECKED = "people.newsletter_bots_mail_checked";
179
+ /**
180
+ * Mail a newsletter's bot sent since the last check: no trigger makes a kind stale for it, so a 1-to-1 mail would wait
181
+ * for someone else's change to give the bot back. Answers the newest email id to check up to, or `undefined`.
182
+ */
183
+ export const newsletterBotsMail = (database) => {
184
+ const newest = Number(database.prepare("SELECT coalesce(max(id), 0) AS n FROM emails").get()?.n);
185
+ const checked = Number(database.prepare("SELECT value FROM store_settings WHERE key = ?").get(MAIL_CHECKED)?.value ?? 0);
186
+ return newest > checked ? newest : undefined;
187
+ };
188
+ /** Gives back the newsletter bots a 1-to-1 mail came from, and keeps how far the mail was checked. */
189
+ export const settleNewsletterBots = (database, newest, at) => {
190
+ const checked = Number(database.prepare("SELECT value FROM store_settings WHERE key = ?").get(MAIL_CHECKED)?.value ?? 0);
191
+ const sent = database
192
+ .prepare("SELECT 1 FROM sender_bots b CROSS JOIN sender_bot_identities s ON s.bot_id = b.id CROSS JOIN emails e " +
193
+ `ON e.from_identity_id = s.identity_id WHERE b.author = 'rules' AND b.reason = '${NEWSLETTER_ONLY}' ` +
194
+ "AND e.id > ? AND e.id <= ? LIMIT 1")
195
+ .get(checked, newest);
196
+ if (sent !== undefined)
197
+ ruleBotsBack(database, at);
198
+ database
199
+ .prepare("INSERT INTO store_settings (key, value, at) VALUES (?, ?, ?) " +
200
+ "ON CONFLICT (key) DO UPDATE SET value = excluded.value, at = excluded.at")
201
+ .run(MAIL_CHECKED, String(newest), at);
163
202
  };
164
203
  /** Whether a build before version 75 sorted a bot onto a person row since the last kept list. */
165
204
  export const hasLegacyBots = (database) => database.prepare("SELECT 1 FROM sender_bots WHERE person_id > 0 LIMIT 1").get() !== undefined;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@wirecat/cli-core",
3
- "version": "1.1.0",
3
+ "version": "1.2.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",
@@ -27,7 +27,7 @@
27
27
  "./update"
28
28
  ],
29
29
  "store": {
30
- "speaks": 76,
30
+ "speaks": 77,
31
31
  "minCompatible": 73
32
32
  }
33
33
  },