Skip to content

Commit 53cdf80

Browse files
committed
feat(desktop): refactor inbox activity
1 parent 4be5c6a commit 53cdf80

30 files changed

Lines changed: 1496 additions & 297 deletions

‎desktop/src/app/routes/reminders.tsx‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import { createFileRoute, redirect } from "@tanstack/react-router";
22

3-
// Reminders is now a filter option inside the inbox dropdown, selected via
3+
// Reminders is now a filter option inside the Activity dropdown, selected via
44
// local state rather than the URL. This redirect preserves existing history
5-
// entries and bookmarks pointing at `/reminders` so they land in the inbox
5+
// entries and bookmarks pointing at `/reminders` so they land in Activity
66
// instead of dead-ending; the user re-selects Reminders from the filter.
77
export const Route = createFileRoute("/reminders")({
88
beforeLoad: () => {

‎desktop/src/features/agents/knownAgentPubkeys.test.mjs‎

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
import assert from "node:assert/strict";
22
import test from "node:test";
33

4-
import { mergeKnownAgentPubkeys } from "./knownAgentPubkeys.ts";
4+
import {
5+
mergeKnownAgentPubkeys,
6+
mergeOwnedAgentPubkeys,
7+
} from "./knownAgentPubkeys.ts";
58

69
const MANAGED =
710
"1111111111111111111111111111111111111111111111111111111111111111";
@@ -34,3 +37,28 @@ test("normalisesCaseAndWhitespace_dedupingAcrossSources", () => {
3437

3538
assert.deepEqual([...merged], [MANAGED]);
3639
});
40+
41+
test("owned agents include locally managed and declared-owned relay agents", () => {
42+
const merged = mergeOwnedAgentPubkeys(
43+
[{ pubkey: MANAGED }],
44+
{
45+
[RELAY]: { ownerPubkey: " owner " },
46+
other: { ownerPubkey: "somebody-else" },
47+
},
48+
"OWNER",
49+
);
50+
51+
assert.deepEqual([...merged].sort(), [MANAGED, RELAY].sort());
52+
});
53+
54+
test("owned agents exclude non-owned relay agents", () => {
55+
const merged = mergeOwnedAgentPubkeys(
56+
undefined,
57+
{
58+
[RELAY]: { ownerPubkey: "somebody-else", isAgent: true },
59+
},
60+
"owner",
61+
);
62+
63+
assert.equal(merged.size, 0);
64+
});

‎desktop/src/features/agents/knownAgentPubkeys.ts‎

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,43 @@ export function mergeKnownAgentPubkeys(
2222
return pubkeys;
2323
}
2424

25+
/**
26+
* Agent identities controlled by the current user: agents managed by this
27+
* desktop plus relay agents whose NIP-OA profile declares the user as owner.
28+
* This is intentionally narrower than `mergeKnownAgentPubkeys`, which also
29+
* contains agents owned by other community members.
30+
*/
31+
export function mergeOwnedAgentPubkeys(
32+
managedAgents: readonly { pubkey: string }[] | undefined,
33+
profiles:
34+
| Readonly<
35+
Record<string, { ownerPubkey?: string | null; isAgent?: boolean }>
36+
>
37+
| undefined,
38+
currentPubkey: string | null | undefined,
39+
): ReadonlySet<string> {
40+
const pubkeys = new Set<string>();
41+
for (const agent of managedAgents ?? []) {
42+
pubkeys.add(normalizePubkey(agent.pubkey));
43+
}
44+
45+
if (!currentPubkey) {
46+
return pubkeys;
47+
}
48+
49+
const ownerPubkey = normalizePubkey(currentPubkey);
50+
for (const [pubkey, profile] of Object.entries(profiles ?? {})) {
51+
if (
52+
profile.ownerPubkey &&
53+
normalizePubkey(profile.ownerPubkey) === ownerPubkey
54+
) {
55+
pubkeys.add(normalizePubkey(pubkey));
56+
}
57+
}
58+
59+
return pubkeys;
60+
}
61+
2562
/**
2663
* Channel-scoped variant: the managed ∪ relay baseline plus this channel's
2764
* bot members (role `bot` or `isAgent`), so member-only agents are included.

‎desktop/src/features/channels/unreadReadMarker.test.mjs‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import {
1818
} from "./useUnreadChannels.ts";
1919
import {
2020
isChannelUnreadTriggerKind,
21+
isHomeActivityEvent,
2122
withChannelTagFallback,
2223
} from "./useLiveChannelUpdates.ts";
2324
import {
@@ -118,6 +119,12 @@ test("dmCatchUpFetch_includesOnlyHuddleStartInvite", () => {
118119
assert.equal(streamKinds.includes(KIND_HUDDLE_STARTED), false);
119120
});
120121

122+
test("homeActivity_includesDmMessagesAndConventionalThreadReplies", () => {
123+
assert.equal(isHomeActivityEvent(true, false), true);
124+
assert.equal(isHomeActivityEvent(false, true), true);
125+
assert.equal(isHomeActivityEvent(false, false), false);
126+
});
127+
121128
// An explicit caller timeline position must still advance the read marker. This
122129
// is the consumer (ChannelScreen) that marks the active channel read with a
123130
// real position; the fix must not regress it.

‎desktop/src/features/channels/useLiveChannelUpdates.ts‎

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,16 +34,17 @@ export type UseLiveChannelUpdatesOptions = {
3434
onLiveMention?: () => void;
3535
/**
3636
* Fired for live "new content" events in a member channel authored by
37-
* someone other than the current user. Thread replies also fire
38-
* onThreadReplyNotification so Home inbox activity stays in sync. Used to
39-
* drive the observed unread-event map that powers sidebar unread state.
37+
* someone other than the current user. Thread replies and DM messages also
38+
* fire onHomeActivity so the Home activity list stays in sync. Used to drive
39+
* the observed unread-event map that powers sidebar unread state.
4040
* See `UNREAD_TRIGGER_KINDS` for the exact kind set.
4141
*/
4242
onChannelMessage?: (channelId: string, event: RelayEvent) => void;
4343
/**
44-
* Fired for thread replies that should be surfaced as Home inbox activity.
44+
* Fired for thread replies and external DM messages that should be surfaced
45+
* as Home activity.
4546
*/
46-
onThreadReplyNotification?: (channelId: string, event: RelayEvent) => void;
47+
onHomeActivity?: (channelId: string, event: RelayEvent) => void;
4748
/**
4849
* Fired for external thread replies that do not match the locally-known
4950
* interest sets. Callers can perform an async backfill and then decide
@@ -88,6 +89,13 @@ export function isChannelUnreadTriggerKind(kind: number, isDmChannel: boolean) {
8889
: UNREAD_TRIGGER_KINDS.has(kind);
8990
}
9091

92+
export function isHomeActivityEvent(
93+
isDmChannel: boolean,
94+
isThreadedReply: boolean,
95+
) {
96+
return isDmChannel || isThreadedReply;
97+
}
98+
9199
export function withChannelTagFallback(
92100
event: RelayEvent,
93101
channelId: string,
@@ -278,8 +286,8 @@ export function useLiveChannelUpdates(
278286
}
279287
} else {
280288
options.onChannelMessage?.(channelId, event);
281-
if (isThreadedReply) {
282-
options.onThreadReplyNotification?.(channelId, event);
289+
if (isHomeActivityEvent(isDmChannel, isThreadedReply)) {
290+
options.onHomeActivity?.(channelId, event);
283291
}
284292
}
285293

‎desktop/src/features/channels/useUnreadChannels.ts‎

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import * as React from "react";
22
import {
33
EMPTY_SET,
4+
isHomeActivityEvent,
45
useLiveChannelUpdates,
56
type UseLiveChannelUpdatesOptions,
67
} from "@/features/channels/useLiveChannelUpdates";
@@ -52,7 +53,6 @@ export {
5253
readActivityFromStorage,
5354
writeActivityToStorage,
5455
} from "@/features/channels/threadActivityStorage";
55-
5656
type UseUnreadChannelsOptions = UseLiveChannelUpdatesOptions & {
5757
pubkey?: string;
5858
relayClient?: RelayClient;
@@ -239,8 +239,8 @@ export function useUnreadChannels(
239239
const mutedChannelIdsRef = React.useRef<ReadonlySet<string>>(new Set());
240240
mutedChannelIdsRef.current = mutedChannelIdsOption ?? new Set();
241241

242-
// Thread reply events that triggered notifications — surfaced in the Home
243-
// activity feed as synthetic FeedItems.
242+
// Thread replies and external DM messages that triggered notifications —
243+
// surfaced in the Home activity feed as synthetic FeedItems.
244244
const threadActivityRef = React.useRef<ThreadActivityItem[]>([]);
245245
// Tracks the (pubkey:relayUrl) scope currently loaded into threadActivityRef.
246246
// Writers guard against this before merging so in-flight writes from a prior
@@ -477,7 +477,7 @@ export function useUnreadChannels(
477477
[normalizedPubkey],
478478
);
479479

480-
const handleThreadReplyNotification = React.useCallback(
480+
const handleHomeActivity = React.useCallback(
481481
(channelId: string, event: RelayEvent) => {
482482
// Guard: don't merge into a ref whose scope has drifted from the current
483483
// identity. Also reject an empty scope — activityScopeKey() returns ""
@@ -551,7 +551,7 @@ export function useUnreadChannels(
551551
useLiveChannelUpdates(channels, activeChannelId, {
552552
...liveUpdateOptions,
553553
onChannelMessage: handleChannelMessage,
554-
onThreadReplyNotification: handleThreadReplyNotification,
554+
onHomeActivity: handleHomeActivity,
555555
onSelfChannelMessage: handleSelfChannelMessage,
556556
participatedRootIds: participatedRootIdsRef.current,
557557
followedRootIds: liveUpdateOptions.followedRootIds,
@@ -662,7 +662,7 @@ export function useUnreadChannels(
662662
// applying the notification filter to both.
663663
let maxExternal = 0;
664664
const unreadEvents: ObservedUnreadEvent[] = [];
665-
const threadReplies: ThreadActivityItem[] = [];
665+
const activityItems: ThreadActivityItem[] = [];
666666
const chType = channel?.channelType;
667667
const chName = channel?.name ?? "";
668668
for (const event of events) {
@@ -707,8 +707,8 @@ export function useUnreadChannels(
707707
isThreadedReply,
708708
}),
709709
);
710-
if (isThreadedReply) {
711-
threadReplies.push({
710+
if (isHomeActivityEvent(chType === "dm", isThreadedReply)) {
711+
activityItems.push({
712712
id: event.id,
713713
kind: event.kind,
714714
pubkey: event.pubkey,
@@ -726,7 +726,7 @@ export function useUnreadChannels(
726726
ok: true,
727727
maxExternal,
728728
unreadEvents,
729-
threadReplies,
729+
threadReplies: activityItems,
730730
};
731731
} catch {
732732
// Transient relay failure for this channel — release the claim
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
import assert from "node:assert/strict";
2+
import test from "node:test";
3+
4+
import { buildActivityListRows } from "./activityListRows.ts";
5+
6+
function draft(key, createdAt, updatedAt = createdAt) {
7+
return {
8+
key,
9+
draft: {
10+
channelId: "channel-1",
11+
content: "unfinished",
12+
createdAt,
13+
updatedAt,
14+
pendingImeta: [],
15+
selectionEnd: 0,
16+
selectionStart: 0,
17+
spoileredAttachmentUrls: [],
18+
status: "active",
19+
},
20+
};
21+
}
22+
23+
function reminder(id, createdAt, notBefore, status = "pending") {
24+
return {
25+
id,
26+
createdAt,
27+
eventId: `event-${id}`,
28+
notBefore,
29+
content: { note: id, status },
30+
};
31+
}
32+
33+
test("All orders messages, reminders, and drafts by activity time", () => {
34+
const rows = buildActivityListRows({
35+
drafts: [draft("older-draft", "2026-01-01T00:02:00.000Z")],
36+
items: [{ id: "new-agent-reply", latestActivityAt: 1767225900 }],
37+
reminders: [reminder("older-reminder", 1767225660, 1767232800)],
38+
});
39+
40+
assert.deepEqual(
41+
rows.map((row) => row.key),
42+
["inbox:new-agent-reply", "draft:older-draft", "reminder:older-reminder"],
43+
);
44+
});
45+
46+
test("All uses a draft's last edit and excludes completed reminders", () => {
47+
const rows = buildActivityListRows({
48+
drafts: [
49+
draft(
50+
"edited-draft",
51+
"2026-01-01T00:01:00.000Z",
52+
"2026-01-01T00:05:00.000Z",
53+
),
54+
],
55+
items: [{ id: "message", latestActivityAt: 1767225780 }],
56+
reminders: [reminder("done", 1767225960, 1767232800, "done")],
57+
});
58+
59+
assert.deepEqual(
60+
rows.map((row) => row.key),
61+
["draft:edited-draft", "inbox:message"],
62+
);
63+
});
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
import type { InboxItem } from "@/features/home/lib/inbox";
2+
import type { DraftState } from "@/features/messages/lib/useDrafts";
3+
import type { Reminder } from "@/features/reminders/lib/reminderTypes";
4+
5+
type DraftEntry = {
6+
draft: DraftState;
7+
key: string;
8+
};
9+
10+
export type ActivityListRow =
11+
| {
12+
key: string;
13+
kind: "inbox";
14+
item: InboxItem;
15+
sortAt: number;
16+
}
17+
| {
18+
key: string;
19+
kind: "reminder";
20+
reminder: Reminder;
21+
sortAt: number;
22+
}
23+
| {
24+
key: string;
25+
kind: "draft";
26+
entry: DraftEntry;
27+
sortAt: number;
28+
};
29+
30+
function draftActivityAt(draft: DraftState): number {
31+
for (const value of [draft.updatedAt, draft.createdAt]) {
32+
const timestamp = Date.parse(value);
33+
if (Number.isFinite(timestamp)) return timestamp / 1_000;
34+
}
35+
return 0;
36+
}
37+
38+
export function buildActivityListRows({
39+
drafts,
40+
items,
41+
reminders,
42+
}: {
43+
drafts: readonly DraftEntry[];
44+
items: readonly InboxItem[];
45+
reminders: readonly Reminder[];
46+
}): ActivityListRow[] {
47+
return [
48+
...items.map(
49+
(item): ActivityListRow => ({
50+
key: `inbox:${item.id}`,
51+
kind: "inbox",
52+
item,
53+
sortAt: item.latestActivityAt,
54+
}),
55+
),
56+
...reminders
57+
.filter((reminder) => reminder.content.status === "pending")
58+
.map(
59+
(reminder): ActivityListRow => ({
60+
key: `reminder:${reminder.id}`,
61+
kind: "reminder",
62+
reminder,
63+
sortAt: reminder.createdAt,
64+
}),
65+
),
66+
...drafts.map(
67+
(entry): ActivityListRow => ({
68+
key: `draft:${entry.key}`,
69+
kind: "draft",
70+
entry,
71+
sortAt: draftActivityAt(entry.draft),
72+
}),
73+
),
74+
].sort((left, right) => right.sortAt - left.sortAt);
75+
}

‎desktop/src/features/home/lib/inbox.ts‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,7 @@ export type InboxFilter =
2121
| "mention"
2222
| "thread"
2323
| "needs_action"
24-
| "activity"
25-
| "agent_activity"
24+
| "agents"
2625
| "reminders"
2726
| "drafts";
2827

0 commit comments

Comments
 (0)