diff --git a/luz/src/chat.rs b/luz/src/chat.rs index 7bb99e1..8bb81db 100644 --- a/luz/src/chat.rs +++ b/luz/src/chat.rs @@ -26,7 +26,7 @@ pub struct Body { pub body: String, } -#[derive(sqlx::FromRow)] +#[derive(sqlx::FromRow, Debug, Clone)] pub struct Chat { pub correspondent: JID, // message history is not stored in chat, retreived separately. diff --git a/luz/src/connection/read.rs b/luz/src/connection/read.rs index 0590ce3..aadf476 100644 --- a/luz/src/connection/read.rs +++ b/luz/src/connection/read.rs @@ -179,9 +179,9 @@ async fn handle_stanza( ) { match stanza { Stanza::Message(stanza_message) => { - if let Some(from) = stanza_message.from { + if let Some(mut from) = stanza_message.from { // TODO: group chat messages - let message = Message { + let mut message = Message { id: stanza_message .id // TODO: proper id storage @@ -209,6 +209,8 @@ async fn handle_stanza( ))) .await; } + message.from = message.from.as_bare(); + from = from.as_bare(); let _ = update_sender .send(UpdateMessage::Message { to: from, message }) .await; diff --git a/luz/src/db/mod.rs b/luz/src/db/mod.rs index 3a1d73d..dd111bd 100644 --- a/luz/src/db/mod.rs +++ b/luz/src/db/mod.rs @@ -335,7 +335,42 @@ impl Db { Ok(()) } - // create direct message + pub async fn create_message_with_self_resource_and_chat( + &self, + message: Message, + chat: JID, + ) -> Result<(), Error> { + let from_jid = message.from.as_bare(); + let resource = &message.from.resourcepart; + let bare_chat = chat.as_bare(); + sqlx::query!( + "insert into users (jid) values (?) on conflict do nothing", + from_jid + ) + .execute(&self.db) + .await?; + let id = Uuid::new_v4(); + sqlx::query!( + "insert into chats (id, correspondent) values (?, ?) on conflict do nothing", + id, + bare_chat + ) + .execute(&self.db) + .await?; + if let Some(resource) = resource { + sqlx::query!( + "insert into resources (bare_jid, resource) values (?, ?) on conflict do nothing", + from_jid, + resource + ) + .execute(&self.db) + .await?; + } + self.create_message(message, chat).await?; + Ok(()) + } + + // create direct message from incoming pub async fn create_message_with_user_resource_and_chat( &self, message: Message, diff --git a/luz/src/lib.rs b/luz/src/lib.rs index 399fec2..4ae3578 100644 --- a/luz/src/lib.rs +++ b/luz/src/lib.rs @@ -122,9 +122,11 @@ impl Luz { ) .await; debug!("connected and logged in as {}", jid); + debug!("test"); } match streams_result { Ok(s) => { + debug!("ok stream result"); let (shutdown_send, shutdown_recv) = oneshot::channel::<()>(); let (writer, supervisor) = SupervisorHandle::new( s, @@ -242,6 +244,7 @@ impl Luz { } } Err(e) => { + tracing::error!("error: {}", e); let _ = self.sender.send(UpdateMessage::Error(Error::Connecting( ConnectionError::ConnectionFailed(e.into()), @@ -984,24 +987,38 @@ impl CommandMessage { }), thread: None, }); + let _ = sender.send(Ok(())); + // let _ = sender.send(Ok(message.clone())); let result = write_handle.write(message).await; match result { Ok(_) => { - let message = Message { + let mut message = Message { id, from: owned_jid, body, }; - if let Err(e) = db.create_message(message, jid).await.map_err(|e| e.into()) + info!("send message {:?}", message); + if let Err(e) = db + .create_message_with_self_resource_and_chat( + message.clone(), + jid.clone(), + ) + .await + .map_err(|e| e.into()) { + tracing::error!("{}", e); let _ = update_sender.send(UpdateMessage::Error(Error::MessageSend( error::MessageSendError::MessageHistory(e), ))); } - let _ = sender.send(Ok(())); + // TODO: don't do this, have separate from from details + message.from = message.from.as_bare(); + let _ = update_sender + .send(UpdateMessage::Message { to: jid, message }) + .await; } Err(_) => { - let _ = sender.send(result); + // let _ = sender.send(result); } } }