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 { const response = await invokeTauri( "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 { const response = await invokeTauri("get_note", { noteId, }); return response ? fromRawUserNote(response) : null; } export async function getUserNotes( pubkey: string, options?: { limit?: number; before?: number; beforeId?: string; }, ): Promise { const response = await invokeTauri("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 { const raw = await invokeTauri("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 { const raw = await invokeTauri("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 { const raw = await invokeTauri("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 { const response = await invokeTauri("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 { const response = await invokeTauri("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 { const response = await invokeTauri( "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, }; }