mirror of
https://github.com/block/buzz.git
synced 2026-10-28 04:51:40 +00:00
Signed-off-by: Tyler Longwell <tlongwell@block.xyz> Co-authored-by: npub1qyvc0c5kl4gqv2fd97fsk46tu378sqgy35vc83rvgfwne90sel7s0ed67d <011987e296fd5006292d2f930b574be47c7801048d1983c46c425d3c95f0cffd@sprout-oss.stage.blox.sqprod.co> Co-authored-by: Max <d8473ee32b973aa31a21a65adddcc4b69cc2a8a4dee8121ecd51926e0cddbc02@sprout-oss.stage.blox.sqprod.co> Co-authored-by: Mari <95cae996907d7cab9f5dbf43c0f53edeac6ab0b032a6feae4abfd784e467b3f5@sprout-oss.stage.blox.sqprod.co> Co-authored-by: Quinn <96f056ad5f2305c8ddf637dc65d048aa4c12d7daeb8867690e34fca46b0ef64c@sprout-oss.stage.blox.sqprod.co> Co-authored-by: Sami <f4a42a97e594b77bdbd8ee35191c8b28a94a4cb871d96f32921558275421fb68@sprout-oss.stage.blox.sqprod.co> Co-authored-by: Perci <5a968df9a7494b4e019b9ecf739e088ba61097b4312124e9a88ae5b42e3f5f3e@sprout-oss.stage.blox.sqprod.co> Co-authored-by: Tyler Longwell <tlongwell@block.xyz>
235 lines
5.1 KiB
TypeScript
235 lines
5.1 KiB
TypeScript
import type {
|
|
ContactListResponse,
|
|
NoteReactionSummary,
|
|
PublishNoteResult,
|
|
UserNote,
|
|
UserNotesResponse,
|
|
} from "@/shared/api/socialTypes";
|
|
|
|
import { invokeTauri } from "./tauri";
|
|
|
|
type RawUserNote = {
|
|
id: string;
|
|
pubkey: string;
|
|
created_at: number;
|
|
content: string;
|
|
tags: string[][];
|
|
};
|
|
|
|
type RawNoteReactionSummary = {
|
|
note_id: string;
|
|
emoji: string;
|
|
count: number;
|
|
pubkeys: string[];
|
|
};
|
|
|
|
type RawUserNotesCursor = {
|
|
before: number;
|
|
before_id: string;
|
|
};
|
|
|
|
type RawUserNotesResponse = {
|
|
notes: RawUserNote[];
|
|
next_cursor: RawUserNotesCursor | null;
|
|
};
|
|
|
|
type RawContactListResponse = {
|
|
id: string;
|
|
pubkey: string;
|
|
created_at: number;
|
|
tags: string[][];
|
|
content: string;
|
|
};
|
|
|
|
function fromRawUserNote(note: RawUserNote): UserNote {
|
|
return {
|
|
id: note.id,
|
|
pubkey: note.pubkey,
|
|
createdAt: note.created_at,
|
|
content: note.content,
|
|
tags: note.tags,
|
|
};
|
|
}
|
|
|
|
export async function getNoteReactions(
|
|
noteIds: string[],
|
|
): Promise<NoteReactionSummary[]> {
|
|
const response = await invokeTauri<RawNoteReactionSummary[]>(
|
|
"get_note_reactions",
|
|
{ noteIds },
|
|
);
|
|
return response.map((summary) => ({
|
|
noteId: summary.note_id,
|
|
emoji: summary.emoji,
|
|
count: summary.count,
|
|
pubkeys: summary.pubkeys,
|
|
}));
|
|
}
|
|
|
|
export async function getNote(noteId: string): Promise<UserNote | null> {
|
|
const response = await invokeTauri<RawUserNote | null>("get_note", {
|
|
noteId,
|
|
});
|
|
return response ? fromRawUserNote(response) : null;
|
|
}
|
|
|
|
export async function getUserNotes(
|
|
pubkey: string,
|
|
options?: {
|
|
limit?: number;
|
|
before?: number;
|
|
beforeId?: string;
|
|
},
|
|
): Promise<UserNotesResponse> {
|
|
const response = await invokeTauri<RawUserNotesResponse>("get_user_notes", {
|
|
pubkey,
|
|
limit: options?.limit ?? null,
|
|
before: options?.before ?? null,
|
|
beforeId: options?.beforeId ?? null,
|
|
});
|
|
|
|
return {
|
|
notes: response.notes.map(fromRawUserNote),
|
|
nextCursor: response.next_cursor
|
|
? {
|
|
before: response.next_cursor.before,
|
|
beforeId: response.next_cursor.before_id,
|
|
}
|
|
: null,
|
|
};
|
|
}
|
|
|
|
type RawPublishNoteResult = {
|
|
event_id: string;
|
|
accepted: boolean;
|
|
message: string;
|
|
};
|
|
|
|
export async function publishNote(
|
|
content: string,
|
|
replyTo?: string,
|
|
mentionPubkeys?: string[],
|
|
mediaTags?: string[][],
|
|
): Promise<PublishNoteResult> {
|
|
const raw = await invokeTauri<RawPublishNoteResult>("publish_note", {
|
|
content,
|
|
replyTo: replyTo ?? null,
|
|
mentionPubkeys: mentionPubkeys ?? null,
|
|
mediaTags: mediaTags ?? null,
|
|
});
|
|
return {
|
|
eventId: raw.event_id,
|
|
accepted: raw.accepted,
|
|
message: raw.message,
|
|
};
|
|
}
|
|
|
|
export async function getContactList(
|
|
pubkey: string,
|
|
): Promise<ContactListResponse> {
|
|
const raw = await invokeTauri<RawContactListResponse>("get_contact_list", {
|
|
pubkey,
|
|
});
|
|
|
|
const contacts = raw.tags
|
|
.filter((t) => t[0] === "p" && t[1])
|
|
.map((t) => ({
|
|
pubkey: t[1],
|
|
relayUrl: t[2] || undefined,
|
|
petname: t[3] || undefined,
|
|
}));
|
|
|
|
return {
|
|
id: raw.id,
|
|
pubkey: raw.pubkey,
|
|
createdAt: raw.created_at,
|
|
contacts,
|
|
};
|
|
}
|
|
|
|
export async function setContactList(
|
|
contacts: Array<{
|
|
pubkey: string;
|
|
relayUrl?: string;
|
|
petname?: string;
|
|
}>,
|
|
): Promise<PublishNoteResult> {
|
|
const raw = await invokeTauri<RawPublishNoteResult>("set_contact_list", {
|
|
contacts: contacts.map((c) => ({
|
|
pubkey: c.pubkey,
|
|
relay_url: c.relayUrl ?? null,
|
|
petname: c.petname ?? null,
|
|
})),
|
|
});
|
|
return {
|
|
eventId: raw.event_id,
|
|
accepted: raw.accepted,
|
|
message: raw.message,
|
|
};
|
|
}
|
|
|
|
export async function getLikedNotes(
|
|
authorPubkey: string,
|
|
limit?: number,
|
|
): Promise<UserNotesResponse> {
|
|
const response = await invokeTauri<RawUserNotesResponse>("get_liked_notes", {
|
|
authorPubkey,
|
|
limit: limit ?? null,
|
|
});
|
|
|
|
return {
|
|
notes: response.notes.map(fromRawUserNote),
|
|
nextCursor: response.next_cursor
|
|
? {
|
|
before: response.next_cursor.before,
|
|
beforeId: response.next_cursor.before_id,
|
|
}
|
|
: null,
|
|
};
|
|
}
|
|
|
|
export async function getGlobalNotes(options?: {
|
|
limit?: number;
|
|
before?: number;
|
|
beforeId?: string;
|
|
}): Promise<UserNotesResponse> {
|
|
const response = await invokeTauri<RawUserNotesResponse>("get_global_notes", {
|
|
limit: options?.limit ?? null,
|
|
before: options?.before ?? null,
|
|
beforeId: options?.beforeId ?? null,
|
|
});
|
|
|
|
return {
|
|
notes: response.notes.map(fromRawUserNote),
|
|
nextCursor: response.next_cursor
|
|
? {
|
|
before: response.next_cursor.before,
|
|
beforeId: response.next_cursor.before_id,
|
|
}
|
|
: null,
|
|
};
|
|
}
|
|
|
|
export async function getNotesTimeline(
|
|
pubkeys: string[],
|
|
limitPerUser?: number,
|
|
): Promise<UserNotesResponse> {
|
|
const response = await invokeTauri<RawUserNotesResponse>(
|
|
"get_notes_timeline",
|
|
{
|
|
pubkeys,
|
|
limitPerUser: limitPerUser ?? null,
|
|
},
|
|
);
|
|
|
|
return {
|
|
notes: response.notes.map(fromRawUserNote),
|
|
nextCursor: response.next_cursor
|
|
? {
|
|
before: response.next_cursor.before,
|
|
beforeId: response.next_cursor.before_id,
|
|
}
|
|
: null,
|
|
};
|
|
}
|