GetUser
This commit is contained in:
@@ -28,3 +28,4 @@ target
|
|||||||
private.key
|
private.key
|
||||||
config.json
|
config.json
|
||||||
messages
|
messages
|
||||||
|
users.db
|
||||||
|
|||||||
@@ -1,2 +1,3 @@
|
|||||||
pub mod config;
|
pub mod config;
|
||||||
pub mod messages;
|
pub mod messages;
|
||||||
|
pub mod users;
|
||||||
|
|||||||
@@ -0,0 +1,62 @@
|
|||||||
|
use std::{collections::HashMap, path::PathBuf};
|
||||||
|
|
||||||
|
use rusqlite::{Connection, params};
|
||||||
|
use tokio::sync::Mutex;
|
||||||
|
|
||||||
|
use crate::types::ClientMeta;
|
||||||
|
|
||||||
|
pub struct UserMetaStore {
|
||||||
|
conn: Mutex<Connection>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl UserMetaStore {
|
||||||
|
pub fn new(path: PathBuf) -> anyhow::Result<Self> {
|
||||||
|
let conn = Connection::open(&path)?;
|
||||||
|
conn.execute(
|
||||||
|
"CREATE TABLE IF NOT EXISTS users (
|
||||||
|
pubkey TEXT PRIMARY KEY,
|
||||||
|
display_name TEXT NOT NULL,
|
||||||
|
avatar TEXT
|
||||||
|
)",
|
||||||
|
[],
|
||||||
|
)?;
|
||||||
|
Ok(Self {
|
||||||
|
conn: Mutex::new(conn),
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn get_users(
|
||||||
|
&self,
|
||||||
|
pubkeys: &[String],
|
||||||
|
) -> anyhow::Result<HashMap<String, ClientMeta>> {
|
||||||
|
let conn = self.conn.lock().await;
|
||||||
|
let placeholders = pubkeys.iter().map(|_| "?").collect::<Vec<_>>().join(",");
|
||||||
|
let query = format!(
|
||||||
|
"SELECT pubkey, display_name, avatar FROM users WHERE pubkey IN ({placeholders})"
|
||||||
|
);
|
||||||
|
|
||||||
|
let mut stmt = conn.prepare(&query)?;
|
||||||
|
let rows = stmt.query_map(rusqlite::params_from_iter(pubkeys), |row| {
|
||||||
|
Ok((
|
||||||
|
row.get::<_, String>(0)?,
|
||||||
|
ClientMeta {
|
||||||
|
display_name: row.get(1)?,
|
||||||
|
avatar: row.get(2)?,
|
||||||
|
},
|
||||||
|
))
|
||||||
|
})?;
|
||||||
|
|
||||||
|
rows.collect::<Result<HashMap<_, _>, _>>()
|
||||||
|
.map_err(Into::into)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn upsert_user(&self, pubkey: &str, meta: &ClientMeta) -> anyhow::Result<()> {
|
||||||
|
let conn = self.conn.lock().await;
|
||||||
|
conn.execute(
|
||||||
|
"INSERT INTO users (pubkey, display_name, avatar) VALUES (?1, ?2, ?3)
|
||||||
|
ON CONFLICT(pubkey) DO UPDATE SET display_name = ?2, avatar = ?3",
|
||||||
|
params![pubkey, meta.display_name, meta.avatar],
|
||||||
|
)?;
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -9,6 +9,7 @@ use crate::{data::messages::StoredMessage, server::Server, types::ClientMeta};
|
|||||||
|
|
||||||
pub mod initialize;
|
pub mod initialize;
|
||||||
pub mod message;
|
pub mod message;
|
||||||
|
pub mod user;
|
||||||
|
|
||||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||||
#[serde(tag = "method")]
|
#[serde(tag = "method")]
|
||||||
@@ -25,6 +26,10 @@ pub enum ClientMethod {
|
|||||||
messages: HashMap<String, Vec<StoredMessage>>,
|
messages: HashMap<String, Vec<StoredMessage>>,
|
||||||
},
|
},
|
||||||
|
|
||||||
|
Users {
|
||||||
|
users: HashMap<String, ClientMeta>,
|
||||||
|
},
|
||||||
|
|
||||||
Error {
|
Error {
|
||||||
error: Cow<'static, str>,
|
error: Cow<'static, str>,
|
||||||
},
|
},
|
||||||
@@ -53,6 +58,10 @@ pub enum ServerMethod {
|
|||||||
|
|
||||||
Meta(ClientMeta),
|
Meta(ClientMeta),
|
||||||
|
|
||||||
|
GetUsers {
|
||||||
|
pubkeys: Vec<String>,
|
||||||
|
},
|
||||||
|
|
||||||
Error {
|
Error {
|
||||||
error: String,
|
error: String,
|
||||||
},
|
},
|
||||||
@@ -93,6 +102,10 @@ pub async fn read_loop(
|
|||||||
ServerMethod::GetMessages { channel_id, chunk } => {
|
ServerMethod::GetMessages { channel_id, chunk } => {
|
||||||
message::get_messages(server, verifying_key, socket, channel_id, chunk).await?;
|
message::get_messages(server, verifying_key, socket, channel_id, chunk).await?;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
ServerMethod::GetUsers { pubkeys } => {
|
||||||
|
user::get_users(server, verifying_key, socket, pubkeys).await?;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
socket_lock = socket.lock().await;
|
socket_lock = socket.lock().await;
|
||||||
|
|||||||
@@ -0,0 +1,23 @@
|
|||||||
|
use std::sync::Arc;
|
||||||
|
|
||||||
|
use axum::extract::ws::WebSocket;
|
||||||
|
use ed25519_dalek::VerifyingKey;
|
||||||
|
use tokio::sync::Mutex;
|
||||||
|
|
||||||
|
use crate::{
|
||||||
|
protocol::{ClientMethod, send_socket},
|
||||||
|
server::Server,
|
||||||
|
};
|
||||||
|
|
||||||
|
pub async fn get_users(
|
||||||
|
server: &Arc<Server>,
|
||||||
|
_verifying_key: VerifyingKey,
|
||||||
|
socket: &Arc<Mutex<WebSocket>>,
|
||||||
|
pubkeys: Vec<String>,
|
||||||
|
) -> anyhow::Result<()> {
|
||||||
|
let users = server.user_store.get_users(&pubkeys).await?;
|
||||||
|
|
||||||
|
send_socket(&mut *socket.lock().await, &ClientMethod::Users { users }).await?;
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
+11
-1
@@ -12,7 +12,7 @@ use ed25519_dalek::{SigningKey, VerifyingKey};
|
|||||||
use tokio::{sync::Mutex, task::JoinSet};
|
use tokio::{sync::Mutex, task::JoinSet};
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
data::{config::Config, messages::MessageStore},
|
data::{config::Config, messages::MessageStore, users::UserMetaStore},
|
||||||
protocol::{ClientMethod, read_loop, send_socket},
|
protocol::{ClientMethod, read_loop, send_socket},
|
||||||
types::ClientMeta,
|
types::ClientMeta,
|
||||||
};
|
};
|
||||||
@@ -29,6 +29,7 @@ pub struct Server {
|
|||||||
pub config: Config,
|
pub config: Config,
|
||||||
pub clients: Mutex<HashMap<VerifyingKey, Arc<UserConnections>>>,
|
pub clients: Mutex<HashMap<VerifyingKey, Arc<UserConnections>>>,
|
||||||
pub message_store: MessageStore,
|
pub message_store: MessageStore,
|
||||||
|
pub user_store: UserMetaStore,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Server {
|
impl Server {
|
||||||
@@ -38,6 +39,7 @@ impl Server {
|
|||||||
config: Config::get().await?,
|
config: Config::get().await?,
|
||||||
clients: Mutex::new(HashMap::new()),
|
clients: Mutex::new(HashMap::new()),
|
||||||
message_store: MessageStore::new(PathBuf::from("messages"))?,
|
message_store: MessageStore::new(PathBuf::from("messages"))?,
|
||||||
|
user_store: UserMetaStore::new(PathBuf::from("users.db"))?,
|
||||||
}))
|
}))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -49,6 +51,14 @@ impl Server {
|
|||||||
ws.on_upgrade(move |socket: WebSocket| async move {
|
ws.on_upgrade(move |socket: WebSocket| async move {
|
||||||
match UserConnections::initialize(&s, socket).await {
|
match UserConnections::initialize(&s, socket).await {
|
||||||
Ok((client, public_key, meta)) => {
|
Ok((client, public_key, meta)) => {
|
||||||
|
if let Err(e) = s
|
||||||
|
.user_store
|
||||||
|
.upsert_user(&crate::signature::to_string(&public_key), &meta)
|
||||||
|
.await
|
||||||
|
{
|
||||||
|
eprintln!("Failed to upsert client: {e}");
|
||||||
|
}
|
||||||
|
|
||||||
let client = Arc::new(Mutex::new(client));
|
let client = Arc::new(Mutex::new(client));
|
||||||
|
|
||||||
let mut clients_meta = s.clients.lock().await;
|
let mut clients_meta = s.clients.lock().await;
|
||||||
|
|||||||
Reference in New Issue
Block a user