commit
meow
diff --git a/migrations/0005_starred.sql b/migrations/0005_starred.sql
new file mode 100644
index 0000000..1e49726
--- /dev/null
+++ b/migrations/0005_starred.sql
@@ -0,0 +1,4 @@
+-- Starring bookmarks a whole conversation. It stays starred through the Bin,
+-- but the Starred view only shows what's outside it.
+ALTER TABLE threads ADD COLUMN starred_at INTEGER;
+CREATE INDEX threads_starred ON threads (owner, starred_at) WHERE starred_at IS NOT NULL;
diff --git a/src/shared/types.ts b/src/shared/types.ts
index 757e122..4dca5dd 100644
--- a/src/shared/types.ts
+++ b/src/shared/types.ts
@@ -2,8 +2,11 @@
export type Folder = "inbox" | "sent" | "drafts";
-/** What the thread list can show: a folder, or the Bin (anything deleted, from any folder). */
-export type ListFolder = Folder | "bin";
+/**
+ * What the thread list can show: a folder, the Bin (anything deleted, from
+ * any folder), or Starred (starred threads, from any folder, outside the Bin).
+ */
+export type ListFolder = Folder | "bin" | "starred";
export interface Me {
username: string;
@@ -34,6 +37,7 @@ export interface ThreadSummary {
count: number;
unread: number;
hasAttachments: boolean;
+ starred: boolean;
}
export interface AttachmentInfo {
@@ -68,6 +72,7 @@ export interface Message {
export interface Thread {
id: string;
subject: string;
+ starred: boolean;
messages: Message[];
}
diff --git a/src/web/App.svelte b/src/web/App.svelte
index 10a03f7..02ddb5c 100644
--- a/src/web/App.svelte
+++ b/src/web/App.svelte
@@ -51,7 +51,13 @@
drawer = false;
});
- const titles: Record<string, string> = { inbox: "Inbox", sent: "Sent", drafts: "Drafts", bin: "Bin" };
+ const titles: Record<string, string> = {
+ inbox: "Inbox",
+ starred: "Starred",
+ sent: "Sent",
+ drafts: "Drafts",
+ bin: "Bin",
+ };
const pageTitles: Record<string, string> = { settings: "Settings", privacy: "Privacy", terms: "Terms", admin: "Admin" };
const title = $derived(route.name === "folder" ? titles[route.folder] : pageTitles[route.name]);
</script>
diff --git a/src/web/app.css b/src/web/app.css
index dc30190..609f1b0 100644
--- a/src/web/app.css
+++ b/src/web/app.css
@@ -56,6 +56,7 @@
--accent-line: rgb(235 69 158 / 40%);
--panel: rgb(11 10 13 / 72%);
--danger: #ff5c7a;
+ --star: #f7c948;
--radius: 1rem;
--radius-sm: 0.5rem;
--font: "Comic Code", ui-monospace, monospace;
diff --git a/src/web/components/ActionMenu.svelte b/src/web/components/ActionMenu.svelte
index e7c74b2..6b35ce8 100644
--- a/src/web/components/ActionMenu.svelte
+++ b/src/web/components/ActionMenu.svelte
@@ -37,12 +37,9 @@
let menuHeight = $state(0);
let confirming = $state<MenuItem | null>(null);
- // The finger that held the row lifts over the backdrop; don't close on it.
- const openedAt = performance.now();
-
- function onbackdrop() {
- if (performance.now() - openedAt > 350) onclose();
- }
+ // The hold that opened the menu ends in stray clicks and context menus.
+ // Only a fresh press counts, so those never close it or pick an item.
+ let pressed = false;
// Keeps the popover on screen, flipping above the cursor if need be.
const left = $derived(Math.max(8, Math.min(at.x, innerWidth - menuWidth - 8)));
@@ -52,7 +49,10 @@
menu.querySelector("button")?.focus();
});
- function choose(item: MenuItem) {
+ function choose(event: MouseEvent, item: MenuItem) {
+ // detail is 0 for Enter and Space, which are always deliberate.
+ const fromKeyboard = event.detail === 0;
+ if (!pressed && !fromKeyboard) return;
if (item.confirm && confirming !== item) return void (confirming = item);
onclose();
item.run();
@@ -75,11 +75,8 @@
class="backdrop"
class:sheet-backdrop={!at.mouse}
role="presentation"
- onclick={onbackdrop}
- oncontextmenu={(event) => {
- event.preventDefault();
- onclose();
- }}
+ onpointerdown={onclose}
+ oncontextmenu={(event) => event.preventDefault()}
transition:fade={{ duration: 120 }}
></div>
@@ -92,6 +89,8 @@
bind:this={menu}
bind:clientWidth={menuWidth}
bind:clientHeight={menuHeight}
+ onpointerdown={() => (pressed = true)}
+ oncontextmenu={(event) => event.preventDefault()}
style:left={at.mouse ? `${left}px` : undefined}
style:top={at.mouse ? `${top}px` : undefined}
transition:fly={{
@@ -109,7 +108,7 @@
<button
role="menuitem"
class:danger={item.danger}
- onclick={() => choose(item)}
+ onclick={(event) => choose(event, item)}
>
<Icon size={18} />
{confirming === item ? item.confirm : item.label}
@@ -122,6 +121,8 @@
position: fixed;
inset: 0;
z-index: 30;
+ -webkit-touch-callout: none;
+ user-select: none;
}
.sheet-backdrop {
@@ -131,6 +132,8 @@
.menu {
position: fixed;
z-index: 31;
+ -webkit-touch-callout: none;
+ user-select: none;
display: flex;
flex-direction: column;
min-width: 13rem;
diff --git a/src/web/components/Sidebar.svelte b/src/web/components/Sidebar.svelte
index f836dc5..631ff21 100644
--- a/src/web/components/Sidebar.svelte
+++ b/src/web/components/Sidebar.svelte
@@ -11,6 +11,7 @@
import SettingsIcon from "@lucide/svelte/icons/settings";
import Shield from "@lucide/svelte/icons/shield";
import SquarePen from "@lucide/svelte/icons/square-pen";
+ import Star from "@lucide/svelte/icons/star";
import Trash2 from "@lucide/svelte/icons/trash-2";
import type { ListFolder } from "../../shared/types";
import { api } from "../lib/api";
@@ -22,6 +23,7 @@
const folders: { folder: ListFolder; label: string; icon: typeof InboxIcon }[] = [
{ folder: "inbox", label: "Inbox", icon: InboxIcon },
+ { folder: "starred", label: "Starred", icon: Star },
{ folder: "sent", label: "Sent", icon: Send },
{ folder: "drafts", label: "Drafts", icon: FileText },
{ folder: "bin", label: "Bin", icon: Trash2 },
@@ -79,13 +81,13 @@
<h3 class="mailboxes-title">Mailboxes</h3>
<ul class="mailboxes">
<li>
- <a class="item" class:active={!router.mailbox} href={router.href(folderHref(current ?? "inbox", null))}>
+ <a class="item" class:active={current && !router.mailbox} href={router.href(folderHref(current ?? "inbox", null))}>
<span>All</span>
</a>
</li>
{#each byDomain as [domain, addresses] (domain)}
<li>
- <a class="item domain" class:active={router.mailbox === `@${domain}`} href={router.href(folderHref(current ?? "inbox", `@${domain}`))}>
+ <a class="item domain" class:active={current && router.mailbox === `@${domain}`} href={router.href(folderHref(current ?? "inbox", `@${domain}`))}>
<Globe size={14} />
<span>{domain}</span>
{#if unreadFor(domain)}<span class="count">{unreadFor(domain)}</span>{/if}
@@ -93,7 +95,7 @@
<ul>
{#each addresses as addr (addr)}
<li>
- <a class="item address" class:active={router.mailbox === addr} href={router.href(folderHref(current ?? "inbox", addr))}>
+ <a class="item address" class:active={current && router.mailbox === addr} href={router.href(folderHref(current ?? "inbox", addr))}>
<span>{addr.slice(0, addr.lastIndexOf("@"))}</span>
{#if session.unread[addr]}<span class="count">{session.unread[addr]}</span>{/if}
</a>
diff --git a/src/web/components/ThreadList.svelte b/src/web/components/ThreadList.svelte
index b7775be..7374716 100644
--- a/src/web/components/ThreadList.svelte
+++ b/src/web/components/ThreadList.svelte
@@ -9,6 +9,8 @@
import Mail from "@lucide/svelte/icons/mail";
import MailOpen from "@lucide/svelte/icons/mail-open";
import Paperclip from "@lucide/svelte/icons/paperclip";
+ import Star from "@lucide/svelte/icons/star";
+ import StarOff from "@lucide/svelte/icons/star-off";
import Trash2 from "@lucide/svelte/icons/trash-2";
import { flip } from "svelte/animate";
import { fly, slide } from "svelte/transition";
@@ -103,6 +105,18 @@
}
}
+ async function toggleStar(thread: ThreadSummary) {
+ const star = !thread.starred;
+ thread.starred = star;
+ if (!star && folder === "starred") takeOut(thread);
+ try {
+ await api.starThread(thread.id, star);
+ changed();
+ } catch (e) {
+ fail(e);
+ }
+ }
+
function takeOut(thread: ThreadSummary) {
collapsing.add(thread.id);
items = items.filter((item) => item.id !== thread.id);
@@ -167,7 +181,7 @@
run: () => restore(thread),
};
}
- if (folder !== "inbox") return undefined;
+ if (folder !== "inbox" && folder !== "starred") return undefined;
return {
label: thread.unread ? "Read" : "Unread",
icon: thread.unread ? MailOpen : Mail,
@@ -211,7 +225,7 @@
},
];
}
- const readToggle: MenuItem[] = folder === "inbox"
+ const readToggle: MenuItem[] = folder === "inbox" || folder === "starred"
? [
{
label: thread.unread ? "Mark as read" : "Mark as unread",
@@ -222,6 +236,11 @@
: [];
return [
open,
+ {
+ label: thread.starred ? "Unstar" : "Star",
+ icon: thread.starred ? StarOff : Star,
+ run: () => toggleStar(thread),
+ },
...readToggle,
{
label: "Move to Bin",
@@ -233,13 +252,13 @@
}
function who(t: ThreadSummary) {
- // The Bin mixes folders; whoever it's from reads best there.
- return folder === "inbox" || folder === "bin" ? displayName(t.from) : `To: ${t.to.map(displayName).join(", ") || "(nobody yet)"}`;
+ // The Bin and Starred mix folders; whoever it's from reads best there.
+ return folder === "inbox" || folder === "bin" || folder === "starred" ? displayName(t.from) : `To: ${t.to.map(displayName).join(", ") || "(nobody yet)"}`;
}
/** Phones show a coloured initial beside each row, as Gmail does. */
function avatar(t: ThreadSummary) {
- const name = folder === "inbox" || folder === "bin" ? displayName(t.from) : displayName(t.to[0] ?? "?");
+ const name = folder === "inbox" || folder === "bin" || folder === "starred" ? displayName(t.from) : displayName(t.to[0] ?? "?");
const letter = name.match(/\p{L}|\p{N}/u)?.[0]?.toUpperCase() ?? "?";
let hue = 0;
for (const c of name) hue = (hue * 31 + c.charCodeAt(0)) % 360;
@@ -292,6 +311,17 @@
<div class="subject">{t.subject || "(no subject)"}</div>
<div class="snippet">{t.snippet}</div>
</a>
+ {#if folder !== "bin"}
+ <button
+ class="star"
+ class:on={t.starred}
+ aria-label={t.starred ? "Unstar" : "Star"}
+ aria-pressed={t.starred}
+ onclick={() => toggleStar(t)}
+ >
+ <Star size={18} fill={t.starred ? "currentColor" : "none"} />
+ </button>
+ {/if}
</SwipeRow>
</li>
{/each}
@@ -426,6 +456,36 @@
margin: 1rem;
}
+ /* Sits over the row's bottom-right corner, beside the snippet. */
+ .star {
+ position: absolute;
+ right: 0.6rem;
+ bottom: 0.45rem;
+ display: grid;
+ place-items: center;
+ width: 2rem;
+ height: 2rem;
+ padding: 0;
+ border: none;
+ border-radius: 50%;
+ background: none;
+ color: var(--muted);
+ cursor: pointer;
+ }
+
+ .star:hover {
+ background: var(--raised-hover);
+ color: var(--text);
+ }
+
+ .star.on {
+ color: var(--star);
+ }
+
+ .snippet {
+ padding-right: 2rem;
+ }
+
.avatar {
display: none;
}
diff --git a/src/web/components/ThreadView.svelte b/src/web/components/ThreadView.svelte
index cd13c5d..8f1d5ba 100644
--- a/src/web/components/ThreadView.svelte
+++ b/src/web/components/ThreadView.svelte
@@ -11,6 +11,7 @@
import Paperclip from "@lucide/svelte/icons/paperclip";
import Pencil from "@lucide/svelte/icons/pencil";
import Reply from "@lucide/svelte/icons/reply";
+ import Star from "@lucide/svelte/icons/star";
import Trash from "@lucide/svelte/icons/trash";
import type { Message, Thread } from "../../shared/types";
import { SvelteSet } from "svelte/reactivity";
@@ -92,6 +93,19 @@
else opened.add(m.id);
}
+ async function toggleStar() {
+ if (!thread) return;
+ const star = !thread.starred;
+ thread.starred = star;
+ try {
+ await api.starThread(id, star);
+ changed();
+ } catch (e) {
+ thread.starred = !star;
+ error = (e as Error).message;
+ }
+ }
+
async function markUnread() {
changed((await api.markThread(id, false)).unread);
router.thread(null);
@@ -169,6 +183,15 @@
<Trash size={20} />
</button>
{:else if thread && last}
+ <button
+ class="tb"
+ class:starred={thread.starred}
+ aria-label={thread.starred ? "Unstar" : "Star"}
+ aria-pressed={thread.starred}
+ onclick={toggleStar}
+ >
+ <Star size={20} fill={thread.starred ? "currentColor" : "none"} />
+ </button>
<button class="tb" aria-label="Move to Bin" title="Move to Bin" onclick={removeThread}><Trash size={20} /></button>
{#if hasInbound}
<button class="tb" aria-label="Mark unread" title="Mark unread" onclick={markUnread}><MailOpen size={20} /></button>
@@ -196,6 +219,10 @@
<div class="actions">
<button class="btn accent" title="Reply (R)" onclick={() => composeReply(last)}><Reply size={14} /> Reply</button>
<button class="btn" title="Forward (F)" onclick={() => composeForward(last)}><Forward size={14} /> Forward</button>
+ <button class="btn" class:starred={thread.starred} aria-pressed={thread.starred} onclick={toggleStar}>
+ <Star size={14} fill={thread.starred ? "currentColor" : "none"} />
+ {thread.starred ? "Starred" : "Star"}
+ </button>
{#if hasInbound}
<button class="btn" onclick={markUnread}><MailOpen size={14} /> Mark unread</button>
{/if}
@@ -551,4 +578,8 @@
font-size: 0.95rem;
}
}
+
+ .starred {
+ color: var(--star);
+ }
</style>
diff --git a/src/web/lib/api.ts b/src/web/lib/api.ts
index e66d862..21ecffe 100644
--- a/src/web/lib/api.ts
+++ b/src/web/lib/api.ts
@@ -65,6 +65,7 @@ const server = {
/** With `bin`, only the part of the thread that's in the Bin. */
thread: (id: string, bin = false) => get<Thread>(`/api/threads/${id}${bin ? "?bin=1" : ""}`),
markThread: (id: string, read: boolean) => post<Unread>(`/api/threads/${id}/read`, { read }),
+ starThread: (id: string, starred: boolean) => post<{ starred: boolean }>(`/api/threads/${id}/star`, { starred }),
/** Moves it to the Bin; `forever` deletes what of it is already in the Bin. */
deleteThread: (id: string, forever = false) => del<{ count: number }>(`/api/threads/${id}${forever ? "?forever=1" : ""}`),
restoreThread: (id: string) => post<{ count: number }>(`/api/threads/${id}/restore`),
diff --git a/src/web/lib/demo.ts b/src/web/lib/demo.ts
index 7ee3d30..85a8094 100644
--- a/src/web/lib/demo.ts
+++ b/src/web/lib/demo.ts
@@ -150,6 +150,7 @@ export function createDemoApi(): Api {
/** Uploaded, not yet part of a message. */
const uploads = new Map<string, AttachmentInfo>();
const messages: Stored[] = [];
+ const starred = new Set<string>();
function addressesOf(username: string): string[] {
const out: string[] = [];
@@ -420,7 +421,11 @@ export function createDemoApi(): Api {
function summaries(folder: ListFolder, mailbox: string | null): ThreadSummary[] {
const inFolder = messages.filter(
(m) =>
- (folder === "bin" ? !!m.trashedAt : m.folder === folder && !m.trashedAt) &&
+ (folder === "bin"
+ ? !!m.trashedAt
+ : folder === "starred"
+ ? starred.has(m.threadId) && !m.trashedAt
+ : m.folder === folder && !m.trashedAt) &&
(!mailbox || (mailbox.startsWith("@") ? m.mailbox.endsWith(mailbox.toLowerCase()) : m.mailbox === mailbox.toLowerCase())),
);
const byThread = new Map<string, Stored[]>();
@@ -440,6 +445,7 @@ export function createDemoApi(): Api {
count: list.length,
unread: list.filter((m) => !m.read).length,
hasAttachments: messages.some((m) => m.threadId === id && m.attachments.some((a) => !a.contentId)),
+ starred: starred.has(id),
};
})
.sort((a, b) => b.lastAt - a.lastAt);
@@ -480,13 +486,24 @@ export function createDemoApi(): Api {
later<Thread>(() => {
const list = messages.filter((m) => m.threadId === id && !!m.trashedAt === !!bin).sort((a, b) => a.sentAt - b.sentAt);
if (!list.length) throw new Error("Not found");
- return { id, subject: list.at(-1)!.subject, messages: list };
+ return {
+ id,
+ subject: list.at(-1)!.subject,
+ starred: starred.has(id),
+ messages: list,
+ };
}),
markThread: (id, read) =>
later(() => {
for (const m of messages) if (m.threadId === id && m.folder === "inbox") m.read = read;
return { unread: unread() };
}),
+ starThread: (id, star) =>
+ later(() => {
+ if (star) starred.add(id);
+ else starred.delete(id);
+ return { starred: star };
+ }),
deleteThread: (id, forever) =>
later(() => ({
count: forever
diff --git a/src/web/lib/router.svelte.ts b/src/web/lib/router.svelte.ts
index 364389b..e3fe8cd 100644
--- a/src/web/lib/router.svelte.ts
+++ b/src/web/lib/router.svelte.ts
@@ -25,7 +25,13 @@ function parse(path: string): Route {
if (first === "privacy") return { name: "privacy" };
if (first === "terms") return { name: "terms" };
if (first === "admin") return { name: "admin" };
- const folder: ListFolder = first === "sent" || first === "drafts" || first === "bin" ? first : "inbox";
+ const folders: ListFolder[] = [
+ "sent",
+ "drafts",
+ "bin",
+ "starred",
+ ];
+ const folder = folders.find((name) => name === first) ?? "inbox";
return { name: "folder", folder, threadId: second ?? null };
}
diff --git a/src/worker/routes/mail.ts b/src/worker/routes/mail.ts
index 33d761f..6520843 100644
--- a/src/worker/routes/mail.ts
+++ b/src/worker/routes/mail.ts
@@ -14,6 +14,7 @@ import {
getThread,
listThreads,
setThreadRead,
+ setThreadStarred,
trashMessage,
trashThread,
unreadByMailbox,
@@ -35,7 +36,7 @@ const compose = z.object({
}) satisfies z.ZodType<ComposeInput, unknown>;
const listQuery = z.object({
- folder: z.enum(["inbox", "sent", "drafts", "bin"]).default("inbox"),
+ folder: z.enum(["inbox", "sent", "drafts", "bin", "starred"]).default("inbox"),
mailbox: z.string().max(320).optional(),
cursor: z.string().max(100).optional(),
limit: z.coerce.number().int().min(1).max(100).default(50),
@@ -87,6 +88,12 @@ export const mail = new Hono<AppEnv>()
return c.json({ unread: await unreadByMailbox(c.env, user) });
})
+ .post("/threads/:id/star", zValidator("json", z.object({ starred: z.boolean() })), async (c) => {
+ const { starred } = c.req.valid("json");
+ const found = await setThreadStarred(c.env, c.get("user").username, c.req.param("id"), starred);
+ return found ? c.json({ starred }) : c.json({ error: "Not found" }, 404);
+ })
+
/** Moves the thread to the Bin; with ?forever=1, deletes what of it is already there. */
.delete("/threads/:id", zValidator("query", forever), async (c) => {
const user = c.get("user").username;
diff --git a/src/worker/services/messages.ts b/src/worker/services/messages.ts
index 7a46967..fd97d79 100644
--- a/src/worker/services/messages.ts
+++ b/src/worker/services/messages.ts
@@ -203,6 +203,12 @@ export async function listThreads(env: Env, owner: string, f: ListFilter): Promi
const params: (string | number)[] = [owner];
if (f.folder === "bin") {
where.push("trashed_at IS NOT NULL");
+ } else if (f.folder === "starred") {
+ where.push(
+ "trashed_at IS NULL",
+ "thread_id IN (SELECT id FROM threads WHERE owner = ? AND starred_at IS NOT NULL)",
+ );
+ params.push(owner);
} else {
where.push("folder = ?", "trashed_at IS NULL");
params.push(f.folder);
@@ -234,7 +240,10 @@ export async function listThreads(env: Env, owner: string, f: ListFilter): Promi
SELECT f.*, EXISTS (
SELECT 1 FROM attachments a JOIN messages m ON a.message_id = m.id
WHERE m.thread_id = f.thread_id AND a.content_id IS NULL
- ) AS has_att
+ ) AS has_att,
+ EXISTS (
+ SELECT 1 FROM threads t WHERE t.id = f.thread_id AND t.starred_at IS NOT NULL
+ ) AS starred
FROM f WHERE rn = 1 ${after}
ORDER BY sent_at DESC, thread_id DESC LIMIT ?`,
)
@@ -251,6 +260,7 @@ export async function listThreads(env: Env, owner: string, f: ListFilter): Promi
n: number;
unread: number;
has_att: number;
+ starred: number;
}>();
const page = results.slice(0, f.limit);
@@ -268,6 +278,7 @@ export async function listThreads(env: Env, owner: string, f: ListFilter): Promi
count: r.n,
unread: r.unread,
hasAttachments: r.has_att === 1,
+ starred: r.starred === 1,
})),
nextCursor: results.length > f.limit && last ? `${last.sent_at}.${last.thread_id}` : null,
};
@@ -276,7 +287,7 @@ export async function listThreads(env: Env, owner: string, f: ListFilter): Promi
/** The thread's messages outside the Bin, or with `bin`, only those in it. */
export async function getThread(env: Env, owner: string, threadId: string, bin = false): Promise<Thread | null> {
const trashed = bin ? "trashed_at IS NOT NULL" : "trashed_at IS NULL";
- const [messages, attachments] = await env.DB.batch([
+ const [messages, attachments, starred] = await env.DB.batch([
env.DB.prepare(`SELECT * FROM messages WHERE thread_id = ? AND owner = ? AND ${trashed} ORDER BY sent_at`).bind(
threadId,
owner,
@@ -285,6 +296,10 @@ export async function getThread(env: Env, owner: string, threadId: string, bin =
"SELECT a.id, a.message_id, a.filename, a.content_type, a.size, a.content_id FROM attachments a " +
`JOIN messages m ON a.message_id = m.id WHERE m.thread_id = ? AND m.owner = ? AND m.${trashed}`,
).bind(threadId, owner),
+ env.DB.prepare("SELECT 1 FROM threads WHERE id = ? AND owner = ? AND starred_at IS NOT NULL").bind(
+ threadId,
+ owner,
+ ),
]);
const rows = (messages?.results ?? []) as MessageRow[];
if (!rows.length) return null;
@@ -296,7 +311,12 @@ export async function getThread(env: Env, owner: string, threadId: string, bin =
byMessage.set(a.message_id, list);
}
const list = rows.map((r) => toMessage(r, byMessage.get(r.id) ?? []));
- return { id: threadId, subject: list[list.length - 1]!.subject, messages: list };
+ return {
+ id: threadId,
+ subject: list[list.length - 1]!.subject,
+ starred: (starred?.results.length ?? 0) > 0,
+ messages: list,
+ };
}
export async function setThreadRead(env: Env, owner: string, threadId: string, read: boolean): Promise<void> {
@@ -305,6 +325,14 @@ export async function setThreadRead(env: Env, owner: string, threadId: string, r
.run();
}
+/** Returns false when the owner has no such thread. */
+export async function setThreadStarred(env: Env, owner: string, threadId: string, starred: boolean): Promise<boolean> {
+ const res = await env.DB.prepare("UPDATE threads SET starred_at = ? WHERE id = ? AND owner = ?")
+ .bind(starred ? Date.now() : null, threadId, owner)
+ .run();
+ return res.meta.changes > 0;
+}
+
/** Unread inbox mail, per address the owner received it on. */
export async function unreadByMailbox(env: Env, owner: string): Promise<Record<string, number>> {
const { results } = await env.DB.prepare(
diff --git a/test/mailbox.test.ts b/test/mailbox.test.ts
index fca4dc7..b39c6b5 100644
--- a/test/mailbox.test.ts
+++ b/test/mailbox.test.ts
@@ -137,6 +137,46 @@ describe("delivery", () => {
});
});
+describe("starred", () => {
+ const list = async (folder: string) =>
+ ((await (await call(clove, `/api/threads?folder=${folder}`)).json()) as Page<ThreadSummary>).items;
+ const star = (token: string, threadId: string, starred: boolean) =>
+ call(token, `/api/threads/${threadId}/star`, {
+ method: "POST",
+ headers: { "content-type": "application/json" },
+ body: JSON.stringify({ starred }),
+ });
+
+ it("lists starred threads and flags them everywhere", async () => {
+ const { threadId } = await deliver("clove", "Star me");
+ expect((await list("starred")).map((t) => t.id)).not.toContain(threadId);
+
+ expect((await star(clove, threadId, true)).status).toBe(200);
+ expect((await list("starred")).map((t) => t.id)).toContain(threadId);
+ expect((await list("inbox")).find((t) => t.id === threadId)?.starred).toBe(true);
+ const thread = (await (await call(clove, `/api/threads/${threadId}`)).json()) as Thread;
+ expect(thread.starred).toBe(true);
+
+ await star(clove, threadId, false);
+ expect((await list("starred")).map((t) => t.id)).not.toContain(threadId);
+ });
+
+ it("hides binned threads from Starred but keeps the star", async () => {
+ const { threadId } = await deliver("clove", "Starred then binned");
+ await star(clove, threadId, true);
+ await call(clove, `/api/threads/${threadId}`, { method: "DELETE" });
+ expect((await list("starred")).map((t) => t.id)).not.toContain(threadId);
+
+ await call(clove, `/api/threads/${threadId}/restore`, { method: "POST" });
+ expect((await list("starred")).map((t) => t.id)).toContain(threadId);
+ });
+
+ it("can't star someone else's thread", async () => {
+ const { threadId } = await deliver("clove", "Not yours");
+ expect((await star(ari, threadId, true)).status).toBe(404);
+ });
+});
+
describe("bin", () => {
const list = async (folder: string) =>
((await (await call(clove, `/api/threads?folder=${folder}`)).json()) as Page<ThreadSummary>).items.map((t) => t.id);