mirror of
https://github.com/block/buzz.git
synced 2026-11-03 16:01:35 +00:00
Co-authored-by: Taylor Ho <taylorkmho@gmail.com> Signed-off-by: Taylor Ho <taylorkmho@gmail.com>
45 lines
1.3 KiB
TypeScript
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();
|
|
}
|