Skip to content

Commit 564ead3

Browse files
klopez4212npub13fn4ahfnvaa2qwylvegdgeajqs0mph6v4qsw4jcqnw4mjh3hzh2quuucm5
andauthored
feat(desktop): add 10-minute message grouping window (#1578)
Signed-off-by: npub13fn4ahfnvaa2qwylvegdgeajqs0mph6v4qsw4jcqnw4mjh3hzh2quuucm5 <8a675edd33677aa0389f6650d467b2041fb0df4ca820eacb009babb95e3715d4@sprout-oss.stage.blox.sqprod.co> Co-authored-by: npub13fn4ahfnvaa2qwylvegdgeajqs0mph6v4qsw4jcqnw4mjh3hzh2quuucm5 <8a675edd33677aa0389f6650d467b2041fb0df4ca820eacb009babb95e3715d4@sprout-oss.stage.blox.sqprod.co>
1 parent d945617 commit 564ead3

8 files changed

Lines changed: 182 additions & 14 deletions

File tree

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ export type InboxReply = {
5454
authorPubkey: string;
5555
avatarUrl: string | null;
5656
content: string;
57+
createdAt: number;
5758
depth?: number;
5859
fullTimestampLabel: string;
5960
id: string;

‎desktop/src/features/home/ui/HomeView.tsx‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -371,6 +371,7 @@ export function HomeView({
371371
authorPubkey,
372372
avatarUrl: message.avatarUrl ?? null,
373373
content: message.body,
374+
createdAt: message.createdAt,
374375
depth: event ? getContextMessageDepth(event, eventById) : message.depth,
375376
fullTimestampLabel: formatInboxFullTimestamp(message.createdAt),
376377
isSelected: message.id === selectedItem.id,
@@ -704,6 +705,7 @@ export function HomeView({
704705
?.avatarUrl ?? null)
705706
: null,
706707
content,
708+
createdAt: result.createdAt,
707709
depth: result.depth,
708710
fullTimestampLabel: formatInboxFullTimestamp(
709711
result.createdAt,

‎desktop/src/features/home/ui/InboxDetailPane.tsx‎

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,10 @@ import {
1414
} from "@/features/home/ui/InboxMessageRow";
1515
import type { TimelineMessage } from "@/features/messages/types";
1616
import { formatTime } from "@/features/messages/lib/dateFormatters";
17-
import { hasSameMessageAuthor } from "@/features/messages/lib/messageGrouping";
17+
import {
18+
hasSameMessageAuthor,
19+
isWithinGroupingWindow,
20+
} from "@/features/messages/lib/messageGrouping";
1821
import { MessageComposer } from "@/features/messages/ui/MessageComposer";
1922
import { UpdateIndicator } from "@/features/settings/UpdateIndicator";
2023
import type { Channel } from "@/shared/api/types";
@@ -192,6 +195,7 @@ export function InboxDetailPane({
192195
authorPubkey: item.item.pubkey,
193196
avatarUrl: item.avatarUrl,
194197
content: item.preview,
198+
createdAt: item.item.createdAt,
195199
depth: 0,
196200
fullTimestampLabel: item.fullTimestampLabel,
197201
id: item.id,
@@ -325,11 +329,16 @@ export function InboxDetailPane({
325329
<div>
326330
{displayMessages.map((message, index) => {
327331
const isAfterSeparator = index === 1;
332+
const previousMessage = displayMessages[index - 1];
328333
const isContinuation =
329334
!isAfterSeparator &&
330335
hasSameMessageAuthor(
331-
{ pubkey: displayMessages[index - 1]?.authorPubkey },
336+
{ pubkey: previousMessage?.authorPubkey },
332337
{ pubkey: message.authorPubkey },
338+
) &&
339+
isWithinGroupingWindow(
340+
previousMessage?.createdAt,
341+
message.createdAt,
333342
);
334343

335344
return (
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import assert from "node:assert/strict";
2+
import test from "node:test";
3+
4+
import {
5+
MESSAGE_GROUPING_WINDOW_SECONDS,
6+
hasSameMessageAuthor,
7+
isWithinGroupingWindow,
8+
} from "./messageGrouping.ts";
9+
10+
test("hasSameMessageAuthor: matches case-insensitively and trims", () => {
11+
assert.equal(
12+
hasSameMessageAuthor({ pubkey: " ABC " }, { pubkey: "abc" }),
13+
true,
14+
);
15+
assert.equal(
16+
hasSameMessageAuthor({ pubkey: "abc" }, { pubkey: "def" }),
17+
false,
18+
);
19+
});
20+
21+
test("hasSameMessageAuthor: missing pubkeys never match", () => {
22+
assert.equal(hasSameMessageAuthor(null, { pubkey: "abc" }), false);
23+
assert.equal(hasSameMessageAuthor({ pubkey: "abc" }, undefined), false);
24+
assert.equal(hasSameMessageAuthor({ pubkey: "" }, { pubkey: "" }), false);
25+
});
26+
27+
test("isWithinGroupingWindow: at or under the boundary is in window", () => {
28+
const base = 1_000_000;
29+
assert.equal(isWithinGroupingWindow(base, base), true);
30+
assert.equal(
31+
isWithinGroupingWindow(base, base + MESSAGE_GROUPING_WINDOW_SECONDS),
32+
true,
33+
);
34+
});
35+
36+
test("isWithinGroupingWindow: past the boundary is out of window", () => {
37+
const base = 1_000_000;
38+
assert.equal(
39+
isWithinGroupingWindow(base, base + MESSAGE_GROUPING_WINDOW_SECONDS + 1),
40+
false,
41+
);
42+
});
43+
44+
test("isWithinGroupingWindow: out-of-order (negative gap) is out of window", () => {
45+
const base = 1_000_000;
46+
assert.equal(isWithinGroupingWindow(base + 60, base), false);
47+
});
48+
49+
test("isWithinGroupingWindow: missing timestamps are out of window", () => {
50+
assert.equal(isWithinGroupingWindow(null, 1_000_000), false);
51+
assert.equal(isWithinGroupingWindow(1_000_000, undefined), false);
52+
assert.equal(isWithinGroupingWindow(undefined, undefined), false);
53+
});

‎desktop/src/features/messages/lib/messageGrouping.ts‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,15 @@ type MessageAuthorCandidate = {
22
pubkey?: string | null;
33
};
44

5+
/**
6+
* Max gap (seconds) between two same-author messages for the later one to still
7+
* render as a continuation (time-only, no avatar). Beyond this the message
8+
* reads as a new thought and gets the traditional avatar + header treatment,
9+
* even from the same author. Applied consistently across the channel timeline,
10+
* the threaded reply panel, and the home inbox detail view.
11+
*/
12+
export const MESSAGE_GROUPING_WINDOW_SECONDS = 10 * 60;
13+
514
export function hasSameMessageAuthor(
615
previous: MessageAuthorCandidate | null | undefined,
716
current: MessageAuthorCandidate | null | undefined,
@@ -13,3 +22,24 @@ export function hasSameMessageAuthor(
1322
previousPubkey && currentPubkey && previousPubkey === currentPubkey,
1423
);
1524
}
25+
26+
/**
27+
* Whether `current` falls within {@link MESSAGE_GROUPING_WINDOW_SECONDS} of
28+
* `previous`. Both timestamps are Unix seconds. A missing previous timestamp
29+
* (or one in the future) is treated as out of window. Callers combine this
30+
* with {@link hasSameMessageAuthor} to decide continuation grouping.
31+
*/
32+
export function isWithinGroupingWindow(
33+
previousCreatedAt: number | null | undefined,
34+
currentCreatedAt: number | null | undefined,
35+
) {
36+
if (
37+
typeof previousCreatedAt !== "number" ||
38+
typeof currentCreatedAt !== "number"
39+
) {
40+
return false;
41+
}
42+
43+
const gap = currentCreatedAt - previousCreatedAt;
44+
return gap >= 0 && gap <= MESSAGE_GROUPING_WINDOW_SECONDS;
45+
}

‎desktop/src/features/messages/lib/timelineItems.test.mjs‎

Lines changed: 67 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@ import {
88
getTimelineItemKey,
99
} from "./timelineItems.ts";
1010

11-
function dayAt(year, month, day, hour = 12) {
11+
function dayAt(year, month, day, hour = 12, minute = 0) {
1212
return Math.floor(
13-
new Date(year, month - 1, day, hour, 0, 0).getTime() / 1_000,
13+
new Date(year, month - 1, day, hour, minute, 0).getTime() / 1_000,
1414
);
1515
}
1616

@@ -90,11 +90,19 @@ test("buildTimelineItems: system messages flatten to a 'system' item", () => {
9090
assert.deepEqual(kinds(items), ["day-divider", "message", "system"]);
9191
});
9292

93-
test("buildTimelineItems: consecutive messages from the same author are grouped", () => {
93+
test("buildTimelineItems: consecutive same-author messages within the window are grouped", () => {
9494
const entries = [
9595
entry({ id: "a", pubkey: "author-a", createdAt: dayAt(2026, 6, 14) }),
96-
entry({ id: "b", pubkey: "AUTHOR-A", createdAt: dayAt(2026, 6, 14, 13) }),
97-
entry({ id: "c", pubkey: "author-b", createdAt: dayAt(2026, 6, 14, 14) }),
96+
entry({
97+
id: "b",
98+
pubkey: "AUTHOR-A",
99+
createdAt: dayAt(2026, 6, 14, 12, 2),
100+
}),
101+
entry({
102+
id: "c",
103+
pubkey: "author-b",
104+
createdAt: dayAt(2026, 6, 14, 12, 3),
105+
}),
98106
];
99107

100108
const messageItems = buildTimelineItems(entries, null).items.filter(
@@ -111,6 +119,53 @@ test("buildTimelineItems: consecutive messages from the same author are grouped"
111119
);
112120
});
113121

122+
test("buildTimelineItems: same-author messages past the window start a new group", () => {
123+
const author = "author-a";
124+
const entries = [
125+
entry({ id: "a", pubkey: author, createdAt: dayAt(2026, 6, 14, 12, 0) }),
126+
// 8 min later — within the 10-min window, groups as a continuation.
127+
entry({ id: "b", pubkey: author, createdAt: dayAt(2026, 6, 14, 12, 8) }),
128+
// 12 min after "b" — past the window, breaks into a new thought.
129+
entry({ id: "c", pubkey: author, createdAt: dayAt(2026, 6, 14, 12, 20) }),
130+
// 5 min after "c" — within the window again, groups onto "c".
131+
entry({ id: "d", pubkey: author, createdAt: dayAt(2026, 6, 14, 12, 25) }),
132+
];
133+
134+
const messageItems = buildTimelineItems(entries, null).items.filter(
135+
(item) => item.kind === "message",
136+
);
137+
138+
assert.deepEqual(
139+
messageItems.map((item) => item.isContinuation),
140+
[false, true, false, true],
141+
);
142+
assert.deepEqual(
143+
messageItems.map((item) => item.isFollowedByContinuation),
144+
[true, false, true, false],
145+
);
146+
});
147+
148+
test("buildTimelineItems: window is measured against the previous message, not the group start", () => {
149+
const author = "author-a";
150+
// Each message is 8 min after the one above it — a steady stream that never
151+
// gaps out, so grouping continues even though the span (16 min) exceeds the
152+
// 10-min window.
153+
const entries = [
154+
entry({ id: "a", pubkey: author, createdAt: dayAt(2026, 6, 14, 12, 0) }),
155+
entry({ id: "b", pubkey: author, createdAt: dayAt(2026, 6, 14, 12, 8) }),
156+
entry({ id: "c", pubkey: author, createdAt: dayAt(2026, 6, 14, 12, 16) }),
157+
];
158+
159+
const messageItems = buildTimelineItems(entries, null).items.filter(
160+
(item) => item.kind === "message",
161+
);
162+
163+
assert.deepEqual(
164+
messageItems.map((item) => item.isContinuation),
165+
[false, true, true],
166+
);
167+
});
168+
114169
test("buildTimelineItems: dividers break grouping while thread summaries do not", () => {
115170
const sameAuthor = "author-a";
116171
const entries = [
@@ -119,16 +174,20 @@ test("buildTimelineItems: dividers break grouping while thread summaries do not"
119174
...entry({
120175
id: "b",
121176
pubkey: sameAuthor,
122-
createdAt: dayAt(2026, 6, 14, 13),
177+
createdAt: dayAt(2026, 6, 14, 12, 1),
123178
}),
124179
summary: {
125180
threadHeadId: "b",
126181
replyCount: 1,
127-
lastReplyAt: dayAt(2026, 6, 14, 13),
182+
lastReplyAt: dayAt(2026, 6, 14, 12, 1),
128183
participants: [],
129184
},
130185
},
131-
entry({ id: "c", pubkey: sameAuthor, createdAt: dayAt(2026, 6, 14, 14) }),
186+
entry({
187+
id: "c",
188+
pubkey: sameAuthor,
189+
createdAt: dayAt(2026, 6, 14, 12, 2),
190+
}),
132191
entry({ id: "d", pubkey: sameAuthor, createdAt: dayAt(2026, 6, 15) }),
133192
];
134193

‎desktop/src/features/messages/lib/timelineItems.ts‎

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,10 @@ import {
1212
} from "@/features/messages/lib/timelineSnapshot";
1313
import { shouldRenderUnreadDivider } from "@/features/messages/lib/threadPanel";
1414
import type { MainTimelineEntry } from "@/features/messages/lib/threadPanel";
15-
import { hasSameMessageAuthor } from "@/features/messages/lib/messageGrouping";
15+
import {
16+
hasSameMessageAuthor,
17+
isWithinGroupingWindow,
18+
} from "@/features/messages/lib/messageGrouping";
1619
import { KIND_SYSTEM_MESSAGE } from "@/shared/constants/kinds";
1720

1821
/**
@@ -109,7 +112,11 @@ export function buildTimelineItems(
109112

110113
const isContinuation =
111114
previousGroupEntry !== null &&
112-
hasSameMessageAuthor(previousGroupEntry.message, message);
115+
hasSameMessageAuthor(previousGroupEntry.message, message) &&
116+
isWithinGroupingWindow(
117+
previousGroupEntry.message.createdAt,
118+
message.createdAt,
119+
);
113120

114121
if (isContinuation && previousMessageItemIndex !== null) {
115122
const previousItem = items[previousMessageItemIndex];

‎desktop/src/features/messages/ui/MessageThreadPanel.tsx‎

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,10 @@ import {
66
hasNestedThreadBranches,
77
type MainTimelineEntry,
88
} from "@/features/messages/lib/threadPanel";
9-
import { hasSameMessageAuthor } from "@/features/messages/lib/messageGrouping";
9+
import {
10+
hasSameMessageAuthor,
11+
isWithinGroupingWindow,
12+
} from "@/features/messages/lib/messageGrouping";
1013
import type { ImetaMedia } from "@/features/messages/lib/imetaMediaMarkdown";
1114
import { canManageMessageForCurrentUser } from "@/features/messages/lib/canManageMessage";
1215
import type { TimelineMessage } from "@/features/messages/types";
@@ -543,7 +546,11 @@ export function MessageThreadPanel({
543546
const isContinuation =
544547
!startsUnreadSection &&
545548
entry.summary === null &&
546-
hasSameMessageAuthor(previousGroupMessage, entry.message);
549+
hasSameMessageAuthor(previousGroupMessage, entry.message) &&
550+
isWithinGroupingWindow(
551+
previousGroupMessage?.createdAt,
552+
entry.message.createdAt,
553+
);
547554

548555
if (connectsToVisibleChild && !entry.summary) {
549556
ancestorStack.push({ index, message: entry.message });

0 commit comments

Comments
 (0)