Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
39 changes: 36 additions & 3 deletions desktop/src-tauri/src/deep_link.rs
Original file line number Diff line number Diff line change
Expand Up @@ -197,11 +197,23 @@ fn parse_channel_deep_link(url: &Url) -> Option<serde_json::Value> {
}
let mut segments = url.path_segments()?;
let channel_id = segments.next()?;
let message_id = segments.next();
if segments.next().is_some() {
return None;
}
let channel_id = uuid::Uuid::parse_str(channel_id).ok()?.to_string();
Some(serde_json::json!({ "channelId": channel_id }))
if message_id.is_some_and(|value| {
Comment thread
wesbillman marked this conversation as resolved.
value.len() != 64 || !value.bytes().all(|byte| byte.is_ascii_hexdigit())
}) {
return None;
}
Some(match message_id {
Some(message_id) => serde_json::json!({
"channelId": channel_id,
"messageId": message_id.to_ascii_lowercase(),
}),
None => serde_json::json!({ "channelId": channel_id }),
})
}

/// Parse the query string of a `buzz://message?…` URL into the JSON
Expand Down Expand Up @@ -456,8 +468,13 @@ pub(crate) fn handle_deep_link_url(app: &tauri::AppHandle, url_str: &str) {
return;
};
activate_main_window(app);
queue_navigation_deep_link(app, "channel", &payload);
let _ = app.emit("deep-link-channel", payload);
if payload["messageId"].is_string() {
queue_navigation_deep_link(app, "message", &payload);
let _ = app.emit("deep-link-message", payload);
} else {
queue_navigation_deep_link(app, "channel", &payload);
let _ = app.emit("deep-link-channel", payload);
}
}
Some("message") => {
// `buzz://message?channel=<uuid>&id=<eventId>[&thread=<rootId>]`
Expand Down Expand Up @@ -689,6 +706,18 @@ mod tests {
assert_eq!(payload["channelId"], "580ca78b-9dae-46f3-8854-bd671853ba32");
}

#[test]
fn parse_channel_deep_link_accepts_message_path() {
let message_id = "8455293f0123456789abcdef0123456789abcdef0123456789abcdef01234567";
let url = Url::parse(&format!(
"buzz://channel/a372f080-5961-4535-b1a3-edffface377d/{message_id}"
))
.unwrap();
let payload = parse_channel_deep_link(&url).unwrap();
assert_eq!(payload["channelId"], "a372f080-5961-4535-b1a3-edffface377d");
assert_eq!(payload["messageId"], message_id);
}

#[test]
fn parse_channel_deep_link_accepts_v7_and_normalizes_uppercase() {
for (raw, expected) in [
Expand All @@ -712,6 +741,10 @@ mod tests {
"buzz://channel",
"buzz://channel/",
"buzz://channel/one/two",
"buzz://channel/580ca78b-9dae-46f3-8854-bd671853ba32/not-hex",
"buzz://channel/580ca78b-9dae-46f3-8854-bd671853ba32/aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
"buzz://channel/580ca78b-9dae-46f3-8854-bd671853ba32/aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/extra",
"buzz://channel/580ca78b-9dae-46f3-8854-bd671853ba32/",
"buzz://channel/one?extra=true",
"buzz://channel/one#fragment",
"buzz://channel/not-a-uuid",
Expand Down
19 changes: 17 additions & 2 deletions desktop/src/features/messages/lib/channelLink.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,23 @@ import test from "node:test";

import { isChannelLink, parseChannelLink } from "./channelLink.ts";

const CHANNEL_ID = "580ca78b-9dae-46f3-8854-bd671853ba32";
const MESSAGE_ID =
"8455293f0123456789abcdef0123456789abcdef0123456789abcdef01234567";

test("parseChannelLink accepts the canonical channel path", () => {
assert.deepEqual(parseChannelLink(`buzz://channel/${CHANNEL_ID}`), {
ok: true,
value: { channelId: CHANNEL_ID },
});
});

test("parseChannelLink accepts a channel message path", () => {
assert.deepEqual(
parseChannelLink("buzz://channel/580ca78b-9dae-46f3-8854-bd671853ba32"),
parseChannelLink(`buzz://channel/${CHANNEL_ID}/${MESSAGE_ID}`),
{
ok: true,
value: { channelId: "580ca78b-9dae-46f3-8854-bd671853ba32" },
value: { channelId: CHANNEL_ID, messageId: MESSAGE_ID },
},
);
});
Expand All @@ -35,6 +46,10 @@ test("parseChannelLink rejects malformed channel links", () => {
"buzz://channel",
"buzz://channel/",
"buzz://channel/one/two",
`buzz://channel/${CHANNEL_ID}/not-hex`,
`buzz://channel/${CHANNEL_ID}/${"a".repeat(63)}`,
`buzz://channel/${CHANNEL_ID}/${MESSAGE_ID}/extra`,
`buzz://channel/${CHANNEL_ID}/`,
"buzz://channel/one?extra=true",
"buzz://channel/one#fragment",
"https://channel/one",
Expand Down
25 changes: 20 additions & 5 deletions desktop/src/features/messages/lib/channelLink.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
/** `buzz://channel/<uuid>` link encoding and parsing. */
/** `buzz://channel/<uuid>[/<event-id>]` link encoding and parsing. */

const CHANNEL_LINK_SCHEME = "buzz:";
const CHANNEL_LINK_HOST = "channel";
const CHANNEL_UUID_PATTERN =
/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/iu;
const EVENT_ID_PATTERN = /^[0-9a-f]{64}$/iu;

export type ParsedChannelLink = { channelId: string };
export type ParsedChannelLink = {
channelId: string;
messageId?: string;
};

export type ChannelLinkParseResult =
| { ok: true; value: ParsedChannelLink }
Expand Down Expand Up @@ -34,20 +38,31 @@ export function parseChannelLink(url: string): ChannelLinkParseResult {
if (parsed.search || parsed.hash || parsed.username || parsed.password) {
return { ok: false, reason: "unexpected-components" };
}
const segments = parsed.pathname.split("/").filter(Boolean);
if (segments.length !== 1) {
const segments = parsed.pathname.split("/").slice(1);
if (segments.length < 1 || segments.length > 2 || segments.includes("")) {
return { ok: false, reason: "missing-or-extra-channel" };
}
let channelId: string;
let messageId: string | null = null;
try {
channelId = decodeURIComponent(segments[0]);
messageId = segments[1] ? decodeURIComponent(segments[1]) : null;
} catch {
return { ok: false, reason: "invalid-channel-encoding" };
}
if (!CHANNEL_UUID_PATTERN.test(channelId)) {
return { ok: false, reason: "invalid-channel-uuid" };
}
return { ok: true, value: { channelId: channelId.toLowerCase() } };
if (messageId !== null && !EVENT_ID_PATTERN.test(messageId)) {
return { ok: false, reason: "invalid-message-id" };
}
return {
ok: true,
value: {
channelId: channelId.toLowerCase(),
...(messageId ? { messageId: messageId.toLowerCase() } : {}),
},
};
}

export function isChannelLink(href: string | undefined | null): boolean {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ const CHANNEL_ID = "9a1657ac-f7aa-5db0-b632-d8bbeb6dfb50";
const MESSAGE_ID = "root-event";
const HREF = `buzz://message?channel=${CHANNEL_ID}&id=${MESSAGE_ID}`;
const CHANNEL_HREF = `buzz://channel/${CHANNEL_ID}`;
const CHANNEL_MESSAGE_ID = "a".repeat(64);
const CHANNEL_MESSAGE_HREF = `buzz://channel/${CHANNEL_ID}/${CHANNEL_MESSAGE_ID}`;
const OWNER = "a".repeat(64);
const REPO_HREF = `buzz://repo?owner=${OWNER}&d=buzz-world`;
const ISSUE_ID = "b".repeat(64);
Expand Down Expand Up @@ -47,6 +49,15 @@ test("resolves channel and entity links as composer chips", () => {
),
{ channelName: "general", href: CHANNEL_HREF },
);
assert.deepEqual(
resolveComposerMessageLinkAttributes(CHANNEL_MESSAGE_HREF, (channelId) =>
channelId === CHANNEL_ID ? "general" : undefined,
),
{
channelName: "general",
href: `buzz://message?channel=${CHANNEL_ID}&id=${CHANNEL_MESSAGE_ID}`,
},
);
assert.deepEqual(
resolveComposerMessageLinkAttributes(REPO_HREF, () => undefined),
{ channelName: "", href: REPO_HREF },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,12 @@ export function resolveComposerMessageLinkAttributes(
if (channel.ok) {
return {
channelName: resolveChannelName(channel.value.channelId) ?? "",
href: buildChannelLink(channel.value.channelId),
href: channel.value.messageId
? buildMessageLink({
channelId: channel.value.channelId,
messageId: channel.value.messageId,
})
: buildChannelLink(channel.value.channelId),
};
}

Expand Down
26 changes: 21 additions & 5 deletions desktop/src/shared/ui/markdown/ChannelDeepLink.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,18 @@ export function ChannelDeepLinkAnchor({
href,
interactive,
}: React.ComponentPropsWithoutRef<"a"> & { interactive: boolean }) {
const { channels, onOpenChannel } = useMarkdownRuntime();
const { channels, onOpenChannel, onOpenMessageLink } = useMarkdownRuntime();
if (!href) return <>{children}</>;
const parsed = parseChannelLink(href);
if (!parsed.ok) return <>{children}</>;
const openLink = () =>
parsed.value.messageId
? onOpenMessageLink({
channelId: parsed.value.channelId,
messageId: parsed.value.messageId,
threadRootId: null,
})
: onOpenChannel(parsed.value.channelId);
const authoredLabel = getReactNodeText(children);
if (authoredLabel !== href) {
return (
Expand All @@ -36,7 +44,7 @@ export function ChannelDeepLinkAnchor({
title={href}
aria-label={`Open channel: ${authoredLabel}`}
interactive={interactive}
onOpenLink={() => onOpenChannel(parsed.value.channelId)}
onOpenLink={openLink}
>
{children}
</BuzzInlineLink>
Expand All @@ -50,7 +58,7 @@ export function ChannelDeepLinkAnchor({
title={href}
aria-label={`Open channel ${label}`}
interactive={interactive}
onOpenLink={() => onOpenChannel(parsed.value.channelId)}
onOpenLink={openLink}
Comment thread
wesbillman marked this conversation as resolved.
Outdated
>
{label}
</BuzzLinkChip>
Expand All @@ -64,10 +72,18 @@ export function MarkdownChannelDeepLink({
children?: React.ReactNode;
interactive: boolean;
}) {
const { channels, onOpenChannel } = useMarkdownRuntime();
const { channels, onOpenChannel, onOpenMessageLink } = useMarkdownRuntime();
const href = String(children ?? "");
const parsed = parseChannelLink(href);
if (!parsed.ok) return <span data-channel-deep-link="">{href}</span>;
const openLink = () =>
parsed.value.messageId
? onOpenMessageLink({
channelId: parsed.value.channelId,
messageId: parsed.value.messageId,
threadRootId: null,
})
: onOpenChannel(parsed.value.channelId);
const label = channelPermalinkLabel(channels, parsed.value.channelId);
return (
<BuzzLinkChip
Expand All @@ -77,7 +93,7 @@ export function MarkdownChannelDeepLink({
title={href}
aria-label={`Open channel ${label}`}
interactive={interactive}
onOpenLink={() => onOpenChannel(parsed.value.channelId)}
onOpenLink={openLink}
>
{label}
</BuzzLinkChip>
Expand Down
Loading