mirror of
https://github.com/block/buzz.git
synced 2026-10-27 12:40:30 +00:00
Fix agent timeline identity and avatars (#46)
This commit is contained in:
@@ -33,6 +33,7 @@ pub struct ProfileInfo {
|
||||
#[derive(Serialize, Deserialize)]
|
||||
pub struct UserProfileSummaryInfo {
|
||||
pub display_name: Option<String>,
|
||||
pub avatar_url: Option<String>,
|
||||
pub nip05_handle: Option<String>,
|
||||
}
|
||||
|
||||
|
||||
@@ -6,18 +6,31 @@ import {
|
||||
type UserProfileLookup,
|
||||
} from "@/features/profile/lib/identity";
|
||||
|
||||
function getEffectiveAuthorPubkey(event: RelayEvent) {
|
||||
const [firstTag] = event.tags;
|
||||
if (
|
||||
firstTag?.[0] === "p" &&
|
||||
firstTag[1] &&
|
||||
event.tags.some((tag) => tag[0] === "h")
|
||||
) {
|
||||
return firstTag[1];
|
||||
}
|
||||
|
||||
return event.pubkey;
|
||||
}
|
||||
|
||||
function formatMessageAuthor(
|
||||
event: RelayEvent,
|
||||
channel: Channel | null,
|
||||
currentPubkey: string | undefined,
|
||||
profiles: UserProfileLookup | undefined,
|
||||
) {
|
||||
const authorPubkey = getEffectiveAuthorPubkey(event);
|
||||
const fallbackName =
|
||||
channel?.channelType === "dm"
|
||||
? (() => {
|
||||
const participantIndex = channel.participantPubkeys.indexOf(
|
||||
event.pubkey,
|
||||
);
|
||||
const participantIndex =
|
||||
channel.participantPubkeys.indexOf(authorPubkey);
|
||||
if (participantIndex < 0) {
|
||||
return null;
|
||||
}
|
||||
@@ -27,7 +40,7 @@ function formatMessageAuthor(
|
||||
: null;
|
||||
|
||||
return resolveUserLabel({
|
||||
pubkey: event.pubkey,
|
||||
pubkey: authorPubkey,
|
||||
currentPubkey,
|
||||
fallbackName,
|
||||
profiles,
|
||||
@@ -35,6 +48,21 @@ function formatMessageAuthor(
|
||||
});
|
||||
}
|
||||
|
||||
function getAuthorAvatarUrl(input: {
|
||||
authorPubkey: string;
|
||||
currentPubkey: string | undefined;
|
||||
currentUserAvatarUrl: string | null;
|
||||
profiles: UserProfileLookup | undefined;
|
||||
}) {
|
||||
const { authorPubkey, currentPubkey, currentUserAvatarUrl, profiles } = input;
|
||||
|
||||
if (currentPubkey === authorPubkey) {
|
||||
return currentUserAvatarUrl ?? null;
|
||||
}
|
||||
|
||||
return profiles?.[authorPubkey.toLowerCase()]?.avatarUrl ?? null;
|
||||
}
|
||||
|
||||
export function formatTimelineMessages(
|
||||
events: RelayEvent[],
|
||||
channel: Channel | null,
|
||||
@@ -42,24 +70,36 @@ export function formatTimelineMessages(
|
||||
currentUserAvatarUrl: string | null,
|
||||
profiles?: UserProfileLookup,
|
||||
): TimelineMessage[] {
|
||||
return events.map((event) => ({
|
||||
id: event.id,
|
||||
pubkey: event.pubkey,
|
||||
author: formatMessageAuthor(event, channel, currentPubkey, profiles),
|
||||
avatarUrl:
|
||||
currentPubkey === event.pubkey ? (currentUserAvatarUrl ?? null) : null,
|
||||
time: new Intl.DateTimeFormat("en-US", {
|
||||
hour: "numeric",
|
||||
minute: "2-digit",
|
||||
}).format(new Date(event.created_at * 1_000)),
|
||||
body: event.content,
|
||||
accent: currentPubkey === event.pubkey,
|
||||
pending: event.pending,
|
||||
kind: event.kind,
|
||||
tags: event.tags,
|
||||
}));
|
||||
return events.map((event) => {
|
||||
const authorPubkey = getEffectiveAuthorPubkey(event);
|
||||
|
||||
return {
|
||||
id: event.id,
|
||||
pubkey: authorPubkey,
|
||||
author: formatMessageAuthor(event, channel, currentPubkey, profiles),
|
||||
avatarUrl: getAuthorAvatarUrl({
|
||||
authorPubkey,
|
||||
currentPubkey,
|
||||
currentUserAvatarUrl,
|
||||
profiles,
|
||||
}),
|
||||
time: new Intl.DateTimeFormat("en-US", {
|
||||
hour: "numeric",
|
||||
minute: "2-digit",
|
||||
}).format(new Date(event.created_at * 1_000)),
|
||||
body: event.content,
|
||||
accent: currentPubkey === authorPubkey,
|
||||
pending: event.pending,
|
||||
kind: event.kind,
|
||||
tags: event.tags,
|
||||
};
|
||||
});
|
||||
}
|
||||
|
||||
export function collectMessageAuthorPubkeys(events: RelayEvent[]) {
|
||||
return [...new Set(events.map((event) => event.pubkey.toLowerCase()))];
|
||||
return [
|
||||
...new Set(
|
||||
events.map((event) => getEffectiveAuthorPubkey(event).toLowerCase()),
|
||||
),
|
||||
];
|
||||
}
|
||||
|
||||
@@ -45,6 +45,7 @@ type RawProfile = {
|
||||
|
||||
type RawUserProfileSummary = {
|
||||
display_name: string | null;
|
||||
avatar_url: string | null;
|
||||
nip05_handle: string | null;
|
||||
};
|
||||
|
||||
@@ -297,6 +298,7 @@ function fromRawUserProfileSummary(
|
||||
): UserProfileSummary {
|
||||
return {
|
||||
displayName: profile.display_name,
|
||||
avatarUrl: profile.avatar_url,
|
||||
nip05Handle: profile.nip05_handle,
|
||||
};
|
||||
}
|
||||
|
||||
@@ -90,6 +90,7 @@ export type Profile = {
|
||||
|
||||
export type UserProfileSummary = {
|
||||
displayName: string | null;
|
||||
avatarUrl: string | null;
|
||||
nip05Handle: string | null;
|
||||
};
|
||||
|
||||
|
||||
@@ -43,6 +43,7 @@ type RawProfile = {
|
||||
|
||||
type RawUserProfileSummary = {
|
||||
display_name: string | null;
|
||||
avatar_url: string | null;
|
||||
nip05_handle: string | null;
|
||||
};
|
||||
|
||||
@@ -1098,6 +1099,7 @@ async function handleGetUsersBatch(
|
||||
|
||||
profiles[normalizedPubkey] = {
|
||||
display_name: profile.display_name,
|
||||
avatar_url: profile.avatar_url,
|
||||
nip05_handle: profile.nip05_handle,
|
||||
};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user