Get Messages

This commit is contained in:
2026-08-20 15:08:55 +02:00
parent 4fe471243c
commit 0bce209dd1
4 changed files with 60 additions and 7 deletions
+1
View File
@@ -27,3 +27,4 @@ target
private.key private.key
config.json config.json
messages
+11 -4
View File
@@ -87,17 +87,24 @@ impl MessageStore {
}) })
} }
/// Fetches the most recent `limit` messages, oldest-first (ready to render top-to-bottom). /// Fetches the most recent `limit` messages by the `offset`.
pub fn get_recent_messages(&self, channel_id: &str, limit: u32) -> Result<Vec<StoredMessage>> { pub fn get_recent_messages(
&self,
channel_id: &str,
limit: u32,
chunk: u32,
) -> Result<Vec<StoredMessage>> {
self.with_channel(channel_id, |conn| { self.with_channel(channel_id, |conn| {
let offset = chunk * limit;
let mut stmt = conn.prepare( let mut stmt = conn.prepare(
"SELECT id, author_pubkey, content, timestamp, signature "SELECT id, author_pubkey, content, timestamp, signature
FROM messages FROM messages
ORDER BY timestamp DESC ORDER BY timestamp DESC
LIMIT ?1", LIMIT ?1 OFFSET ?2",
)?; )?;
let rows = stmt.query_map(params![limit], |row| { let rows = stmt.query_map(params![limit, offset], |row| {
Ok(StoredMessage { Ok(StoredMessage {
id: row.get(0)?, id: row.get(0)?,
author: row.get(1)?, author: row.get(1)?,
+26
View File
@@ -1,7 +1,9 @@
use crate::data::messages::{MessageData, StoredMessage}; use crate::data::messages::{MessageData, StoredMessage};
use crate::protocol::{ClientMethod, send_socket};
use crate::server::Server; use crate::server::Server;
use axum::extract::ws::WebSocket; use axum::extract::ws::WebSocket;
use ed25519_dalek::{Verifier, VerifyingKey}; use ed25519_dalek::{Verifier, VerifyingKey};
use std::collections::HashMap;
use std::sync::Arc; use std::sync::Arc;
use std::time::{SystemTime, UNIX_EPOCH}; use std::time::{SystemTime, UNIX_EPOCH};
use tokio::sync::Mutex; use tokio::sync::Mutex;
@@ -45,3 +47,27 @@ pub async fn send_message(
Ok(()) Ok(())
} }
pub async fn get_messages(
server: &Arc<Server>,
_verifying_key: VerifyingKey,
socket: &Arc<Mutex<WebSocket>>,
channel_id: String,
chunk: u32,
) -> anyhow::Result<()> {
const CHUNK_SIZE: u32 = 16;
let messages = server
.message_store
.get_recent_messages(&channel_id, CHUNK_SIZE, chunk)?;
send_socket(
&mut *socket.lock().await,
&ClientMethod::Messages {
messages: HashMap::from([(channel_id, messages)]),
},
)
.await?;
Ok(())
}
+22 -3
View File
@@ -1,11 +1,11 @@
use std::{borrow::Cow, sync::Arc}; use std::{borrow::Cow, collections::HashMap, sync::Arc};
use axum::extract::ws::{Message, Utf8Bytes, WebSocket}; use axum::extract::ws::{Message, Utf8Bytes, WebSocket};
use ed25519_dalek::VerifyingKey; use ed25519_dalek::VerifyingKey;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use tokio::sync::Mutex; use tokio::sync::Mutex;
use crate::{server::Server, types::ClientMeta}; use crate::{data::messages::StoredMessage, server::Server, types::ClientMeta};
pub mod initialize; pub mod initialize;
pub mod message; pub mod message;
@@ -21,6 +21,10 @@ pub enum ClientMethod {
hostname: String, hostname: String,
}, },
Messages {
messages: HashMap<String, Vec<StoredMessage>>,
},
Error { Error {
error: Cow<'static, str>, error: Cow<'static, str>,
}, },
@@ -42,6 +46,11 @@ pub enum ServerMethod {
data: crate::data::messages::MessageData, data: crate::data::messages::MessageData,
}, },
GetMessages {
channel_id: String,
chunk: u32,
},
Meta(ClientMeta), Meta(ClientMeta),
Error { Error {
@@ -54,7 +63,11 @@ pub async fn read_loop(
verifying_key: VerifyingKey, verifying_key: VerifyingKey,
socket: &Arc<Mutex<WebSocket>>, socket: &Arc<Mutex<WebSocket>>,
) -> anyhow::Result<()> { ) -> anyhow::Result<()> {
while let Some(message) = read_socket(&mut *socket.lock().await).await? { let mut socket_lock = socket.lock().await;
while let Some(message) = read_socket(&mut *socket_lock).await? {
drop(socket_lock);
match message { match message {
ServerMethod::Initialize { .. } => { ServerMethod::Initialize { .. } => {
send_socket( send_socket(
@@ -76,7 +89,13 @@ pub async fn read_loop(
ServerMethod::SendMessage { channel_id, data } => { ServerMethod::SendMessage { channel_id, data } => {
message::send_message(server, verifying_key, socket, data, channel_id).await?; message::send_message(server, verifying_key, socket, data, channel_id).await?;
} }
ServerMethod::GetMessages { channel_id, chunk } => {
message::get_messages(server, verifying_key, socket, channel_id, chunk).await?;
}
} }
socket_lock = socket.lock().await;
} }
Ok(()) Ok(())