feat: signed friend profile frames and TUI /profile /who
prf frames are ed25519-signed, friend-visible only (unknown pubkey is ignored). /profile edits self; F5 and /who show the selected friend's last profile.
This commit is contained in:
parent
ef78be4035
commit
d304cc4c3b
7 changed files with 732 additions and 22 deletions
104
src/store.rs
104
src/store.rs
|
|
@ -54,6 +54,13 @@ pub struct Friend {
|
|||
pub prekey: Vec<u8>,
|
||||
}
|
||||
|
||||
pub struct FriendProfile {
|
||||
pub display_name: String,
|
||||
pub bio: String,
|
||||
pub xmr_addr: String,
|
||||
pub updated_at: i64,
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct Message {
|
||||
pub dir: String,
|
||||
|
|
@ -105,6 +112,21 @@ impl Store {
|
|||
prekey_pk BLOB NOT NULL,
|
||||
hs_nickname TEXT NOT NULL DEFAULT 'ow0'
|
||||
);
|
||||
CREATE TABLE IF NOT EXISTS self_profile (
|
||||
id INTEGER PRIMARY KEY CHECK (id = 1),
|
||||
display_name TEXT NOT NULL DEFAULT '',
|
||||
bio TEXT NOT NULL DEFAULT '',
|
||||
xmr_addr TEXT NOT NULL DEFAULT '',
|
||||
updated_at INTEGER NOT NULL DEFAULT 0
|
||||
);
|
||||
CREATE TABLE IF NOT EXISTS friend_profiles (
|
||||
pubkey BLOB PRIMARY KEY,
|
||||
display_name TEXT NOT NULL,
|
||||
bio TEXT NOT NULL,
|
||||
xmr_addr TEXT NOT NULL DEFAULT '',
|
||||
updated_at INTEGER NOT NULL
|
||||
);
|
||||
INSERT OR IGNORE INTO self_profile (id) VALUES (1);
|
||||
",
|
||||
)?;
|
||||
let store = Self { conn };
|
||||
|
|
@ -304,6 +326,79 @@ impl Store {
|
|||
Ok(n > 0)
|
||||
}
|
||||
|
||||
pub fn set_self_profile(&self, display_name: &str, bio: &str, xmr_addr: &str) -> Result<()> {
|
||||
crate::profile::validate(display_name, bio, xmr_addr).map_err(|e| Error(e.to_string()))?;
|
||||
let prev: i64 = self.conn.query_row(
|
||||
"SELECT updated_at FROM self_profile WHERE id = 1",
|
||||
[],
|
||||
|row| row.get(0),
|
||||
)?;
|
||||
let now = unix_now().max(prev + 1);
|
||||
self.conn.execute(
|
||||
"UPDATE self_profile SET display_name = ?1, bio = ?2, xmr_addr = ?3, updated_at = ?4 WHERE id = 1",
|
||||
params![display_name, bio, xmr_addr, now],
|
||||
)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn self_profile(&self) -> Result<FriendProfile> {
|
||||
self.conn
|
||||
.query_row(
|
||||
"SELECT display_name, bio, xmr_addr, updated_at FROM self_profile WHERE id = 1",
|
||||
[],
|
||||
profile_from_row,
|
||||
)
|
||||
.map_err(Into::into)
|
||||
}
|
||||
|
||||
/// Apply a signed profile. Unknown friend / bad sig / stale ts → no change, no insert.
|
||||
pub fn apply_profile(&self, pubkey: &[u8], prf: &crate::profile::Profile) -> Result<bool> {
|
||||
if !crate::profile::verify(pubkey, prf) {
|
||||
return Ok(false);
|
||||
}
|
||||
let exists: i64 = self.conn.query_row(
|
||||
"SELECT COUNT(*) FROM friends WHERE pubkey = ?1",
|
||||
params![pubkey],
|
||||
|row| row.get(0),
|
||||
)?;
|
||||
if exists == 0 {
|
||||
return Ok(false);
|
||||
}
|
||||
let prev: Option<i64> = self
|
||||
.conn
|
||||
.query_row(
|
||||
"SELECT updated_at FROM friend_profiles WHERE pubkey = ?1",
|
||||
params![pubkey],
|
||||
|row| row.get(0),
|
||||
)
|
||||
.optional()?;
|
||||
if prev.is_some_and(|t| prf.ts <= t) {
|
||||
return Ok(false);
|
||||
}
|
||||
self.conn.execute(
|
||||
"INSERT INTO friend_profiles (pubkey, display_name, bio, xmr_addr, updated_at)
|
||||
VALUES (?1, ?2, ?3, ?4, ?5)
|
||||
ON CONFLICT(pubkey) DO UPDATE SET
|
||||
display_name = excluded.display_name,
|
||||
bio = excluded.bio,
|
||||
xmr_addr = excluded.xmr_addr,
|
||||
updated_at = excluded.updated_at",
|
||||
params![pubkey, prf.display_name, prf.bio, prf.xmr_addr, prf.ts],
|
||||
)?;
|
||||
Ok(true)
|
||||
}
|
||||
|
||||
pub fn friend_profile(&self, pubkey: &[u8]) -> Result<Option<FriendProfile>> {
|
||||
self.conn
|
||||
.query_row(
|
||||
"SELECT display_name, bio, xmr_addr, updated_at FROM friend_profiles WHERE pubkey = ?1",
|
||||
params![pubkey],
|
||||
profile_from_row,
|
||||
)
|
||||
.optional()
|
||||
.map_err(Into::into)
|
||||
}
|
||||
|
||||
pub fn set_friend_prekey(&self, pubkey: &[u8], prekey: &[u8]) -> Result<()> {
|
||||
let n = self.conn.execute(
|
||||
"UPDATE friends SET prekey = ?1 WHERE pubkey = ?2",
|
||||
|
|
@ -385,6 +480,15 @@ impl Store {
|
|||
}
|
||||
}
|
||||
|
||||
fn profile_from_row(row: &rusqlite::Row<'_>) -> rusqlite::Result<FriendProfile> {
|
||||
Ok(FriendProfile {
|
||||
display_name: row.get(0)?,
|
||||
bio: row.get(1)?,
|
||||
xmr_addr: row.get(2)?,
|
||||
updated_at: row.get(3)?,
|
||||
})
|
||||
}
|
||||
|
||||
fn friend_from_row(row: &rusqlite::Row<'_>) -> rusqlite::Result<Friend> {
|
||||
Ok(Friend {
|
||||
pubkey: row.get(0)?,
|
||||
|
|
|
|||
Loading…
Reference in a new issue