Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions changes/feat-newsletter-bots.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
### Changed — experimental

- **A sender of a newsletter only is a bot, a human author too.** The owner's ruling of 25.11.2026, in place of the
earlier one that kept a human-written newsletter a person. A person all of whose mail came from a list (List-Id,
List-Unsubscribe, a one-click unsubscribe, a Feedback-ID, bulk `Precedence` or the bulk box), with no tie to the
owner, no messenger identity and no discussion list among their mail, moves to a bot with the reason
`newsletter-only` on the next people list; `kindSignals` names `newsletter-only`. Its mail stays typed `newsletter`:
a newsletter's bot is not a program for the mail types. A rule's newsletter bot goes back to a person once the owner
writes to it, as before, and also once a mail written to the owner alone (no list headers, at most a small group in
To and Cc) comes from it; the newsletter's own mail keeps reach `broadcast`. The owner's `moveToPerson` and
`moveToBot` and the sorting rules still win. The people list works out every kind again once after the upgrade.
1 change: 1 addition & 0 deletions src/domain/person-kinds.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export type KindSignal =
| "null-return-path"
| "bulk-mail"
| "bulk-never-replied"
| "newsletter-only"
| "automated-name"
| "organization"
| "role-address"
Expand Down
142 changes: 142 additions & 0 deletions src/store/newsletter-bots.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
import { mkdtempSync } from "node:fs"
import { tmpdir } from "node:os"
import { join } from "node:path"
import { afterEach, expect, it } from "vitest"
import { syncMail } from "../mail/sync.js"
import { type FakeMail, type FakeMailbox, fakeMailSource } from "../mail/testing/index.js"
import { type MessageStore, openStore } from "./store.js"

const ME = "owner@home.example.com"
const NOW = Date.parse("2026-10-04T12:00:00Z")
const SINCE = new Date("2026-09-01T00:00:00Z")

let n = 0
const mail = (from: [string, string], subject: string, overrides: Partial<FakeMail> = {}): FakeMail => {
n += 1
return {
uid: String(100 + n),
messageId: `<m${n}@test.example.com>`,
threadId: String(9000 + n),
receivedAt: `2026-09-${String(10 + (n % 15)).padStart(2, "0")}T08:00:00Z`,
subject,
from,
to: [[null, ME]],
text: `Synthetic body ${n}`,
...overrides,
}
}

const AUTHOR: [string, string] = ["Ada Quill", "ada@quill.substack.example.com"]

const issue = (subject: string) =>
mail(AUTHOR, subject, {
extraHeaders: [
"List-Unsubscribe: <https://quill.substack.example.com/unsubscribe>",
"List-Unsubscribe-Post: List-Unsubscribe=One-Click",
"DKIM-Signature: v=1; a=rsa-sha256; d=quill.substack.example.com; s=s1; h=from:to:subject:list-unsubscribe-post; b=abc",
"Authentication-Results: mx.example.com; dkim=pass header.i=@quill.substack.example.com header.s=s1 header.b=abc",
],
listId: "<quill.substack.example.com>",
})

const handles: { close(): unknown }[] = []
afterEach(async () => {
for (const handle of handles.splice(0)) await handle.close()
})

const opened = async (messages: FakeMail[]) => {
const path = join(mkdtempSync(join(tmpdir(), "newsletter-bots-")), "store.db")
const store = await openStore({ path })
handles.push(store)
const mailbox: FakeMailbox = { messages }
const sync = () =>
syncMail(fakeMailSource(mailbox), store, { account: { address: ME }, since: SINCE, now: () => NOW })
await sync()
return { store, mailbox, sync }
}

const sorted = async (store: MessageStore) => {
await store.people.list({ limit: 0 })
return (await store.people.listBots({ limit: 50 })).items
}

const botNames = async (store: MessageStore) =>
(await sorted(store)).map(({ displayName, reason }) => ({ displayName, reason }))

const listed = async (store: MessageStore) =>
(await store.people.list({ limit: 50, kind: "all" })).items.map(({ displayName }) => displayName)

const reaches = async (store: MessageStore) =>
Object.fromEntries(
(await store.mail.emails({ limit: 50 })).flatMap((email) =>
email.outgoing ? [] : [[email.subject, { type: email.mailType, reach: email.reach }]],
),
)

it("makes a sender of a newsletter only a bot, a human author too, and keeps its mail typed a newsletter", async () => {
const { store } = await opened([issue("Notes from the road"), issue("Notes from the sea")])

expect(await botNames(store)).toEqual([{ displayName: "Ada Quill", reason: "newsletter-only" }])
expect(await listed(store)).not.toContain("Ada Quill")
expect(await reaches(store)).toEqual({
"Notes from the road": { type: "newsletter", reach: "broadcast" },
"Notes from the sea": { type: "newsletter", reach: "broadcast" },
})
})

it("gives the bot back once the owner replies, and only its newsletter mail keeps reach broadcast", async () => {
const { store, mailbox, sync } = await opened([issue("Notes from the road")])
expect(await botNames(store)).toHaveLength(1)

mailbox.messages.push(
mail(["Me", ME], "Re: Notes from the road", { to: [[null, AUTHOR[1]]], sent: true }),
mail(AUTHOR, "Re: Re: Notes from the road"),
)
await sync()

expect(await botNames(store)).toEqual([])
const ada = (await store.people.list({ limit: 50, kind: "all" })).items.find(
({ displayName }) => displayName === "Ada Quill",
)
expect(ada).toMatchObject({ displayName: "Ada Quill", reach: "direct", ties: ["mail"] })
expect(await reaches(store)).toEqual({
"Notes from the road": { type: "newsletter", reach: "broadcast" },
"Re: Re: Notes from the road": { type: "personal", reach: "direct" },
})
})

it("gives the bot back after a 1-to-1 mail from its address, though the owner never wrote", async () => {
const { store, mailbox, sync } = await opened([issue("Notes from the road")])
expect(await botNames(store)).toHaveLength(1)

mailbox.messages.push(mail(AUTHOR, "A question for you"))
await sync()

expect(await botNames(store)).toEqual([])
expect(await listed(store)).toContain("Ada Quill")
})

it("keeps a person the owner writes to who also sends a newsletter, and a discussion list's members", async () => {
const list = "<dev.lists.example.org>"
const { store } = await opened([
mail(["Me", ME], "Lunch?", { to: [[null, AUTHOR[1]]], sent: true }),
issue("Notes from the road"),
mail(["Bo Lind", "bo@dev.example.org"], "[dev] release plan", { listId: list }),
mail(["Cy Moor", "cy@dev.example.org"], "[dev] re: release plan", { listId: list }),
mail(["Di Park", "di@dev.example.org"], "[dev] re: re: release plan", { listId: list }),
])

expect(await botNames(store)).toEqual([])
expect(await listed(store)).toEqual(expect.arrayContaining(["Ada Quill", "Bo Lind", "Cy Moor", "Di Park"]))
})

it("keeps the owner's choice over the newsletter rule", async () => {
const { store } = await opened([issue("Notes from the road")])
const [bot] = await sorted(store)

await store.people.moveToPerson(String(bot?.bot), "owner")
await store.people.list({ limit: 0, kind: "all" })

expect(await botNames(store)).toEqual([])
expect(await listed(store)).toContain("Ada Quill")
})
18 changes: 10 additions & 8 deletions src/store/people-list.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -355,7 +355,8 @@ describe("the kind of a person", () => {
)
.get(account, NOW, NOW)?.id,
)
for (const n of [1, 2, 3]) {
// One mail outside the bulk box: all of it there would make her a newsletter's bot.
for (const n of [1, 2, 3, 4]) {
const email = Number(
db
.prepare(
Expand All @@ -364,18 +365,19 @@ describe("the kind of a person", () => {
)
.get(account, thread, `m${n}`, identityPk(db, "email", "lena@acme.ru"), NOW + n, NOW, NOW)?.id,
)
db.prepare("INSERT INTO email_mailboxes (email_id, mailbox_id, created_at) VALUES (?, ?, ?)").run(
email,
bulk,
NOW,
)
if (n < 4)
db.prepare("INSERT INTO email_mailboxes (email_id, mailbox_id, created_at) VALUES (?, ?, ?)").run(
email,
bulk,
NOW,
)
}

const [lena] = (await store.people.list({ limit: 10, kind: "automated" })).items
expect(lena).toMatchObject({
kindSignals: ["bulk-mail"],
messagesFrom: 3,
lastContact: new Date(NOW + 3).toISOString(),
messagesFrom: 4,
lastContact: new Date(NOW + 4).toISOString(),
contact: "lena@acme.ru",
})
})
Expand Down
4 changes: 3 additions & 1 deletion src/store/sqlite/email-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { MAIL_TYPES, type MailType, type MailTypeOf, mailTypeOf, schemaTypesOf }
import { sortRuleFor } from "../../mail/sort-rules.js"
import { type CacheDatabase, isQueryOnly } from "../driver.js"
import { typeRulesOf } from "./mail-sorting.js"
import { NEWSLETTER_ONLY } from "./sender-bots.js"
import { LIST_ID, LIST_SENDERS, REACHES, type Reach, senderKey } from "./sender-signals.js"
import { read, writeBatch } from "./transaction.js"

Expand All @@ -27,7 +28,8 @@ const INPUTS =
"WHERE a.attachable_type = 'email' AND a.attachable_id = e.id AND a.mime IS NOT NULL) AS parts, " +
"e.outgoing IS 1 OR EXISTS (SELECT 1 FROM email_recipients r JOIN emails o ON o.id = r.email_id " +
"WHERE r.identity_id = e.from_identity_id AND o.outgoing = 1) AS owner_wrote, " +
"EXISTS (SELECT 1 FROM sender_bot_identities b WHERE b.identity_id = e.from_identity_id) AS bot, " +
"EXISTS (SELECT 1 FROM sender_bot_identities b JOIN sender_bots s ON s.id = b.bot_id " +
`WHERE b.identity_id = e.from_identity_id AND s.reason IS NOT '${NEWSLETTER_ONLY}') AS bot, ` +
"(SELECT json_extract(g.metadata, '$.sector') FROM organization_identities oi " +
"JOIN organizations g ON g.id = oi.organization_id " +
"WHERE oi.identity_id = e.from_identity_id AND g.deleted_at IS NULL ORDER BY g.id LIMIT 1) AS sector " +
Expand Down
Loading
Loading