This commit is contained in:
2024-10-28 08:04:19 +07:00
parent 0518389f50
commit 9ba95301db
8 changed files with 27 additions and 13 deletions

View File

@@ -32,10 +32,24 @@ pub struct Mention {
#[tauri::command] #[tauri::command]
#[specta::specta] #[specta::specta]
pub async fn get_profile(id: String, state: State<'_, Nostr>) -> Result<String, String> { pub async fn get_profile(
id: String,
cache_only: bool,
state: State<'_, Nostr>,
) -> Result<String, String> {
let client = &state.client; let client = &state.client;
let public_key = PublicKey::parse(&id).map_err(|e| e.to_string())?; let public_key = PublicKey::parse(&id).map_err(|e| e.to_string())?;
if cache_only {
let profile = client
.database()
.profile(public_key)
.await
.map_err(|e| e.to_string())?;
return Ok(profile.metadata().as_json());
};
let metadata = client let metadata = client
.fetch_metadata(public_key, Some(Duration::from_secs(3))) .fetch_metadata(public_key, Some(Duration::from_secs(3)))
.await .await

View File

@@ -128,9 +128,9 @@ async setSigner(id: string) : Promise<Result<null, string>> {
else return { status: "error", error: e as any }; else return { status: "error", error: e as any };
} }
}, },
async getProfile(id: string) : Promise<Result<string, string>> { async getProfile(id: string, cacheOnly: boolean) : Promise<Result<string, string>> {
try { try {
return { status: "ok", data: await TAURI_INVOKE("get_profile", { id }) }; return { status: "ok", data: await TAURI_INVOKE("get_profile", { id, cacheOnly }) };
} catch (e) { } catch (e) {
if(e instanceof Error) throw e; if(e instanceof Error) throw e;
else return { status: "error", error: e as any }; else return { status: "error", error: e as any };

View File

@@ -45,12 +45,12 @@ export function NoteRepost({
const list: Promise<MenuItem>[] = []; const list: Promise<MenuItem>[] = [];
for (const account of accounts) { for (const account of accounts) {
const res = await commands.getProfile(account); const res = await commands.getProfile(account, true);
let name = "unknown"; let name = "unknown";
if (res.status === "ok") { if (res.status === "ok") {
const profile: Metadata = JSON.parse(res.data); const profile: Metadata = JSON.parse(res.data);
name = profile.display_name ?? profile.name ?? "unknown"; name = profile.display_name ?? profile.name ?? "anon";
} }
list.push( list.push(

View File

@@ -199,12 +199,12 @@ function OpenLaunchpad() {
const list: Promise<MenuItem>[] = []; const list: Promise<MenuItem>[] = [];
for (const account of accounts) { for (const account of accounts) {
const res = await commands.getProfile(account); const res = await commands.getProfile(account, true);
let name = "unknown"; let name = "unknown";
if (res.status === "ok") { if (res.status === "ok") {
const profile: Metadata = JSON.parse(res.data); const profile: Metadata = JSON.parse(res.data);
name = profile.display_name ?? profile.name ?? "unknown"; name = profile.display_name ?? profile.name ?? "anon";
} }
list.push( list.push(

View File

@@ -104,12 +104,12 @@ function Screen() {
const list: Promise<MenuItem>[] = []; const list: Promise<MenuItem>[] = [];
for (const account of accounts) { for (const account of accounts) {
const res = await commands.getProfile(account); const res = await commands.getProfile(account, true);
let name = "unknown"; let name = "unknown";
if (res.status === "ok") { if (res.status === "ok") {
const profile: Metadata = JSON.parse(res.data); const profile: Metadata = JSON.parse(res.data);
name = profile.display_name ?? profile.name ?? "unknown"; name = profile.display_name ?? profile.name ?? "anon";
} }
list.push( list.push(

View File

@@ -3,7 +3,7 @@ import { createFileRoute } from "@tanstack/react-router";
export const Route = createFileRoute("/settings/$id/profile")({ export const Route = createFileRoute("/settings/$id/profile")({
beforeLoad: async ({ params }) => { beforeLoad: async ({ params }) => {
const res = await commands.getProfile(params.id); const res = await commands.getProfile(params.id, true);
if (res.status === "ok") { if (res.status === "ok") {
const profile: Profile = JSON.parse(res.data); const profile: Profile = JSON.parse(res.data);

View File

@@ -31,12 +31,12 @@ function Screen() {
const list: Promise<MenuItem>[] = []; const list: Promise<MenuItem>[] = [];
for (const account of accounts) { for (const account of accounts) {
const res = await commands.getProfile(account); const res = await commands.getProfile(account, true);
let name = "unknown"; let name = "unknown";
if (res.status === "ok") { if (res.status === "ok") {
const profile: Metadata = JSON.parse(res.data); const profile: Metadata = JSON.parse(res.data);
name = profile.display_name ?? profile.name ?? "unknown"; name = profile.display_name ?? profile.name ?? "anon";
} }
list.push( list.push(

View File

@@ -26,7 +26,7 @@ export function useProfile(pubkey: string, embed?: string) {
} }
} }
const query = await commands.getProfile(normalizedId); const query = await commands.getProfile(normalizedId, false);
if (query.status === "ok") { if (query.status === "ok") {
return JSON.parse(query.data) as Metadata; return JSON.parse(query.data) as Metadata;