feat(luz): initial message timestamp support

This commit is contained in:
cel 🌸 2025-03-03 06:10:46 +00:00
parent 542a8e299c
commit 9baf682466
7 changed files with 16 additions and 7 deletions

View File

@ -8,7 +8,7 @@ futures = "0.3.31"
jabber = { version = "0.1.0", path = "../jabber" }
peanuts = { version = "0.1.0", path = "../../peanuts" }
jid = { version = "0.1.0", path = "../jid", features = ["sqlx"] }
sqlx = { version = "0.8.3", features = ["sqlite", "runtime-tokio", "uuid"] }
sqlx = { version = "0.8.3", features = ["sqlite", "runtime-tokio", "uuid", "chrono"] }
stanza = { version = "0.1.0", path = "../stanza" }
tokio = "1.42.0"
tokio-stream = "0.1.17"
@ -17,3 +17,4 @@ tracing = "0.1.41"
tracing-subscriber = "0.3.19"
uuid = { version = "1.13.1", features = ["v4"] }
thiserror = "2.0.11"
chrono = "0.4.40"

View File

@ -83,6 +83,7 @@ create table messages (
-- user is the current "owner" of the message
-- TODO: queued messages offline
-- TODO: timestamp
timestamp text not null,
-- TODO: icky
-- the user to show it coming from (not necessarily the original sender)

View File

@ -1,3 +1,4 @@
use chrono::{DateTime, Utc};
use jid::JID;
use uuid::Uuid;
@ -7,6 +8,7 @@ pub struct Message {
// does not contain full user information
#[sqlx(rename = "from_jid")]
pub from: JID,
pub timestamp: DateTime<Utc>,
// TODO: originally_from
// TODO: message edits
// TODO: message timestamp

View File

@ -6,6 +6,7 @@ use std::{
time::Duration,
};
use chrono::{DateTime, Utc};
use jabber::{connection::Tls, jabber_stream::bound_stream::BoundJabberReader};
use stanza::client::Stanza;
use tokio::{
@ -188,6 +189,7 @@ async fn handle_stanza(
.map(|id| Uuid::from_str(&id).unwrap_or_else(|_| Uuid::new_v4()))
.unwrap_or_else(|| Uuid::new_v4()),
from: from.clone(),
timestamp: Utc::now(),
body: Body {
// TODO: should this be an option?
body: stanza_message

View File

@ -358,7 +358,7 @@ impl Db {
let bare_jid = message.from.as_bare();
let resource = message.from.resourcepart;
let chat_id = self.read_chat_id(chat).await?;
sqlx::query!("insert into messages (id, body, chat_id, from_jid, from_resource) values (?, ?, ?, ?, ?)", message.id, message.body.body, chat_id, bare_jid, resource).execute(&self.db).await?;
sqlx::query!("insert into messages (id, body, chat_id, from_jid, from_resource, timestamp) values (?, ?, ?, ?, ?, ?)", message.id, message.body.body, chat_id, bare_jid, resource, message.timestamp).execute(&self.db).await?;
Ok(())
}
@ -452,10 +452,11 @@ impl Db {
// TODO: paging
pub(crate) async fn read_message_history(&self, chat: JID) -> Result<Vec<Message>, Error> {
let chat_id = self.read_chat_id(chat).await?;
let messages: Vec<Message> = sqlx::query_as("select * from messages where chat_id = ?")
.bind(chat_id)
.fetch_all(&self.db)
.await?;
let messages: Vec<Message> =
sqlx::query_as("select * from messages where chat_id = ? order by timestamp asc")
.bind(chat_id)
.fetch_all(&self.db)
.await?;
Ok(messages)
}

View File

@ -6,6 +6,7 @@ use std::{
};
use chat::{Body, Chat, Message};
use chrono::Utc;
use connection::{write::WriteMessage, SupervisorSender};
use db::Db;
use error::{
@ -996,6 +997,7 @@ impl CommandMessage {
id,
from: owned_jid,
body,
timestamp: Utc::now(),
};
info!("send message {:?}", message);
if let Err(e) = db

View File

@ -16,7 +16,7 @@ async fn main() {
.await
.unwrap();
let (luz, mut recv) =
LuzHandle::new("test@blos.sm".try_into().unwrap(), "slayed".to_string(), db).unwrap();
LuzHandle::new("test@blos.sm".try_into().unwrap(), "slayed".to_string(), db);
tokio::spawn(async move {
while let Some(msg) = recv.recv().await {