mirror of
https://github.com/block/buzz.git
synced 2026-08-18 06:50:31 +02:00
Signed-off-by: Logan Johnson <loganj@squareup.com> Signed-off-by: npub1dpf98sl35hm9k65t8h6cvh5n6knn2msugh5k5nmysxwnw5wlh7uqn2wgp4 <685253c3f1a5f65b6a8b3df5865e93d5a7356e1c45e96a4f64819d3751dfbfb8@sprout-oss.stage.blox.sqprod.co> Signed-off-by: npub13n66s06epmqf2kc3v373ez8hj65cuzyvxzjf93vwpervxqn2u7jq2qd9je <8cf5a83f590ec0955b11647d1c88f796a98e088c30a492c58e0e46c3026ae7a4@buzz.block.builderlab.xyz> Co-authored-by: npub1dpf98sl35hm9k65t8h6cvh5n6knn2msugh5k5nmysxwnw5wlh7uqn2wgp4 <685253c3f1a5f65b6a8b3df5865e93d5a7356e1c45e96a4f64819d3751dfbfb8@sprout-oss.stage.blox.sqprod.co> Co-authored-by: npub13n66s06epmqf2kc3v373ez8hj65cuzyvxzjf93vwpervxqn2u7jq2qd9je <8cf5a83f590ec0955b11647d1c88f796a98e088c30a492c58e0e46c3026ae7a4@buzz.block.builderlab.xyz>
137 lines
3.4 KiB
TypeScript
137 lines
3.4 KiB
TypeScript
import { invokeTauri } from "@/shared/api/tauri";
|
|
import type {
|
|
Profile,
|
|
UpdateProfileInput,
|
|
UserProfileSummary,
|
|
UserSearchPage,
|
|
UserSearchResult,
|
|
UsersBatchResponse,
|
|
} from "@/shared/api/types";
|
|
|
|
type RawProfile = {
|
|
pubkey: string;
|
|
display_name: string | null;
|
|
avatar_url: string | null;
|
|
about: string | null;
|
|
nip05_handle: string | null;
|
|
owner_pubkey: string | null;
|
|
has_profile_event?: boolean;
|
|
};
|
|
|
|
type RawUserProfileSummary = Omit<RawProfile, "pubkey" | "about"> & {
|
|
name?: string | null;
|
|
is_agent?: boolean;
|
|
};
|
|
|
|
type RawUsersBatchResponse = {
|
|
profiles: Record<string, RawUserProfileSummary>;
|
|
missing: string[];
|
|
};
|
|
|
|
type RawUserSearchResult = RawUserProfileSummary & { pubkey: string };
|
|
|
|
type RawSearchUsersResponse = {
|
|
users: RawUserSearchResult[];
|
|
next_cursor?: string | null;
|
|
};
|
|
|
|
function fromRawProfile(profile: RawProfile): Profile {
|
|
return {
|
|
pubkey: profile.pubkey,
|
|
displayName: profile.display_name,
|
|
avatarUrl: profile.avatar_url,
|
|
about: profile.about,
|
|
nip05Handle: profile.nip05_handle,
|
|
ownerPubkey: profile.owner_pubkey,
|
|
hasProfileEvent: profile.has_profile_event ?? false,
|
|
};
|
|
}
|
|
|
|
function fromRawUserProfileSummary(
|
|
profile: RawUserProfileSummary,
|
|
): UserProfileSummary {
|
|
return {
|
|
displayName: profile.display_name,
|
|
name: profile.name ?? null,
|
|
avatarUrl: profile.avatar_url,
|
|
nip05Handle: profile.nip05_handle,
|
|
ownerPubkey: profile.owner_pubkey,
|
|
isAgent: profile.is_agent ?? false,
|
|
};
|
|
}
|
|
|
|
function fromRawUserSearchResult(user: RawUserSearchResult): UserSearchResult {
|
|
return {
|
|
pubkey: user.pubkey,
|
|
displayName: user.display_name,
|
|
avatarUrl: user.avatar_url,
|
|
nip05Handle: user.nip05_handle,
|
|
ownerPubkey: user.owner_pubkey,
|
|
isAgent: user.is_agent ?? false,
|
|
};
|
|
}
|
|
|
|
export async function getProfile(): Promise<Profile> {
|
|
const profile = await invokeTauri<RawProfile>("get_profile");
|
|
return fromRawProfile(profile);
|
|
}
|
|
|
|
export async function updateProfile(
|
|
input: UpdateProfileInput,
|
|
): Promise<Profile> {
|
|
const profile = await invokeTauri<RawProfile>("update_profile", input);
|
|
return fromRawProfile(profile);
|
|
}
|
|
|
|
export async function updateProfileAtRelay(input: {
|
|
relayUrl: string;
|
|
expectedPubkey: string;
|
|
expectedAvatarUrl: string | null;
|
|
avatarUrl: string;
|
|
}): Promise<Profile> {
|
|
const profile = await invokeTauri<RawProfile>(
|
|
"update_profile_at_relay",
|
|
input,
|
|
);
|
|
return fromRawProfile(profile);
|
|
}
|
|
|
|
export async function getUserProfile(pubkey?: string): Promise<Profile> {
|
|
const profile = await invokeTauri<RawProfile>("get_user_profile", { pubkey });
|
|
return fromRawProfile(profile);
|
|
}
|
|
|
|
export async function getUsersBatch(
|
|
pubkeys: string[],
|
|
): Promise<UsersBatchResponse> {
|
|
const response = await invokeTauri<RawUsersBatchResponse>("get_users_batch", {
|
|
pubkeys,
|
|
});
|
|
|
|
return {
|
|
profiles: Object.fromEntries(
|
|
Object.entries(response.profiles).map(([pubkey, profile]) => [
|
|
pubkey,
|
|
fromRawUserProfileSummary(profile),
|
|
]),
|
|
),
|
|
missing: response.missing,
|
|
};
|
|
}
|
|
|
|
export async function searchUsers(
|
|
query: string,
|
|
limit = 8,
|
|
cursor?: string | null,
|
|
): Promise<UserSearchPage> {
|
|
const response = await invokeTauri<RawSearchUsersResponse>("search_users", {
|
|
query,
|
|
limit,
|
|
cursor: cursor ?? null,
|
|
});
|
|
return {
|
|
users: response.users.map(fromRawUserSearchResult),
|
|
nextCursor: response.next_cursor ?? null,
|
|
};
|
|
}
|