1use crate::profile::{SlimProfile, Status};
4
5pub fn get_all_profiles() -> Result<Vec<SlimProfile>, String> {
7 let conn = super::get_db_connection_guard_static()?;
8
9 let mut stmt = conn.prepare(
10 "SELECT npub, name, display_name, nickname, lud06, lud16, banner, avatar, \
11 about, website, nip05, status_content, status_url, bot, avatar_cached, \
12 banner_cached, is_blocked FROM profiles"
13 ).map_err(|e| format!("Failed to prepare statement: {}", e))?;
14
15 let profiles = stmt.query_map([], |row| {
16 Ok(SlimProfile {
17 id: row.get(0)?,
18 name: row.get(1)?,
19 display_name: row.get(2)?,
20 nickname: row.get(3)?,
21 lud06: row.get(4)?,
22 lud16: row.get(5)?,
23 banner: row.get(6)?,
24 avatar: row.get(7)?,
25 about: row.get(8)?,
26 website: row.get(9)?,
27 nip05: row.get(10)?,
28 status: Status {
29 title: row.get(11)?,
30 purpose: String::new(),
31 url: row.get(12)?,
32 },
33 last_updated: 0,
34 mine: false,
35 bot: row.get::<_, i32>(13)? != 0,
36 avatar_cached: {
37 let p: String = row.get(14)?;
38 if !p.is_empty() && !std::path::Path::new(&p).exists() { String::new() } else { p }
39 },
40 banner_cached: {
41 let p: String = row.get(15)?;
42 if !p.is_empty() && !std::path::Path::new(&p).exists() { String::new() } else { p }
43 },
44 is_blocked: row.get::<_, i32>(16).unwrap_or(0) != 0,
45 })
46 })
47 .map_err(|e| format!("Failed to query profiles: {}", e))?
48 .collect::<Result<Vec<_>, _>>()
49 .map_err(|e| format!("Failed to collect profiles: {}", e))?;
50
51 Ok(profiles)
52}
53
54pub fn set_profile(profile: &SlimProfile) -> Result<(), String> {
56 let conn = super::get_write_connection_guard_static()?;
57
58 conn.execute(
59 "INSERT INTO profiles (npub, name, display_name, nickname, lud06, lud16, banner, avatar, \
60 about, website, nip05, status_content, status_url, bot, avatar_cached, banner_cached, is_blocked) \
61 VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8, ?9, ?10, ?11, ?12, ?13, ?14, ?15, ?16, ?17) \
62 ON CONFLICT(npub) DO UPDATE SET \
63 name = excluded.name, display_name = excluded.display_name, \
64 nickname = excluded.nickname, lud06 = excluded.lud06, lud16 = excluded.lud16, \
65 banner = excluded.banner, avatar = excluded.avatar, about = excluded.about, \
66 website = excluded.website, nip05 = excluded.nip05, \
67 status_content = excluded.status_content, status_url = excluded.status_url, \
68 bot = excluded.bot, avatar_cached = excluded.avatar_cached, \
69 banner_cached = excluded.banner_cached, is_blocked = excluded.is_blocked",
70 rusqlite::params![
71 profile.id,
72 profile.name,
73 profile.display_name,
74 profile.nickname,
75 profile.lud06,
76 profile.lud16,
77 profile.banner,
78 profile.avatar,
79 profile.about,
80 profile.website,
81 profile.nip05,
82 profile.status.title,
83 profile.status.url,
84 profile.bot as i32,
85 profile.avatar_cached,
86 profile.banner_cached,
87 profile.is_blocked as i32,
88 ],
89 ).map_err(|e| format!("Failed to insert profile: {}", e))?;
90
91 Ok(())
92}