Files
buzz/desktop/src/features/messages/lib/messageEmbed.ts
Taylor Ho bf78dca7f5 feat(desktop): embed linked buzz messages
Co-authored-by: Taylor Ho <taylorkmho@gmail.com>
Signed-off-by: Taylor Ho <taylorkmho@gmail.com>
2026-08-10 22:54:34 -07:00

45 lines
1.3 KiB
TypeScript

import type { Channel, RelayEvent } from "@/shared/api/types";
import {
KIND_STREAM_MESSAGE,
KIND_STREAM_MESSAGE_DIFF,
KIND_STREAM_MESSAGE_V2,
} from "@/shared/constants/kinds";
import type { MessagePreviewCandidate } from "@/shared/lib/linkPreview";
import type { ParsedMessageLink } from "./messageLink";
export type MessageEmbedLink = MessagePreviewCandidate;
export function canReadMessageEmbedSource(
channel: Pick<Channel, "isMember" | "visibility"> | undefined,
): boolean {
return (
channel !== undefined && (channel.isMember || channel.visibility === "open")
);
}
/** Only user-authored timeline prose is eligible for a quoted-message card. */
export function isEmbeddableMessageEvent(event: RelayEvent): boolean {
return (
event.kind === KIND_STREAM_MESSAGE ||
event.kind === KIND_STREAM_MESSAGE_V2 ||
event.kind === KIND_STREAM_MESSAGE_DIFF
);
}
export function isMatchingMessageEmbedEvent(
event: RelayEvent,
link: ParsedMessageLink,
): boolean {
const eventChannelId = event.tags.find((tag) => tag[0] === "h")?.[1];
return (
event.id === link.messageId &&
eventChannelId === link.channelId &&
isEmbeddableMessageEvent(event)
);
}
export function messageEmbedExcerpt(content: string): string {
return content.replace(/\s+/g, " ").trim();
}