fix: send UpdateMessage for Message sent by client user
This commit is contained in:
parent
6b44ac8ea4
commit
357311cb93
|
@ -26,7 +26,7 @@ pub struct Body {
|
||||||
pub body: String,
|
pub body: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(sqlx::FromRow)]
|
#[derive(sqlx::FromRow, Debug, Clone)]
|
||||||
pub struct Chat {
|
pub struct Chat {
|
||||||
pub correspondent: JID,
|
pub correspondent: JID,
|
||||||
// message history is not stored in chat, retreived separately.
|
// message history is not stored in chat, retreived separately.
|
||||||
|
|
|
@ -179,9 +179,9 @@ async fn handle_stanza(
|
||||||
) {
|
) {
|
||||||
match stanza {
|
match stanza {
|
||||||
Stanza::Message(stanza_message) => {
|
Stanza::Message(stanza_message) => {
|
||||||
if let Some(from) = stanza_message.from {
|
if let Some(mut from) = stanza_message.from {
|
||||||
// TODO: group chat messages
|
// TODO: group chat messages
|
||||||
let message = Message {
|
let mut message = Message {
|
||||||
id: stanza_message
|
id: stanza_message
|
||||||
.id
|
.id
|
||||||
// TODO: proper id storage
|
// TODO: proper id storage
|
||||||
|
@ -209,6 +209,8 @@ async fn handle_stanza(
|
||||||
)))
|
)))
|
||||||
.await;
|
.await;
|
||||||
}
|
}
|
||||||
|
message.from = message.from.as_bare();
|
||||||
|
from = from.as_bare();
|
||||||
let _ = update_sender
|
let _ = update_sender
|
||||||
.send(UpdateMessage::Message { to: from, message })
|
.send(UpdateMessage::Message { to: from, message })
|
||||||
.await;
|
.await;
|
||||||
|
|
|
@ -335,7 +335,42 @@ impl Db {
|
||||||
Ok(())
|
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(
|
pub async fn create_message_with_user_resource_and_chat(
|
||||||
&self,
|
&self,
|
||||||
message: Message,
|
message: Message,
|
||||||
|
|
|
@ -122,9 +122,11 @@ impl Luz {
|
||||||
)
|
)
|
||||||
.await;
|
.await;
|
||||||
debug!("connected and logged in as {}", jid);
|
debug!("connected and logged in as {}", jid);
|
||||||
|
debug!("test");
|
||||||
}
|
}
|
||||||
match streams_result {
|
match streams_result {
|
||||||
Ok(s) => {
|
Ok(s) => {
|
||||||
|
debug!("ok stream result");
|
||||||
let (shutdown_send, shutdown_recv) = oneshot::channel::<()>();
|
let (shutdown_send, shutdown_recv) = oneshot::channel::<()>();
|
||||||
let (writer, supervisor) = SupervisorHandle::new(
|
let (writer, supervisor) = SupervisorHandle::new(
|
||||||
s,
|
s,
|
||||||
|
@ -242,6 +244,7 @@ impl Luz {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
|
tracing::error!("error: {}", e);
|
||||||
let _ =
|
let _ =
|
||||||
self.sender.send(UpdateMessage::Error(Error::Connecting(
|
self.sender.send(UpdateMessage::Error(Error::Connecting(
|
||||||
ConnectionError::ConnectionFailed(e.into()),
|
ConnectionError::ConnectionFailed(e.into()),
|
||||||
|
@ -984,24 +987,38 @@ impl CommandMessage {
|
||||||
}),
|
}),
|
||||||
thread: None,
|
thread: None,
|
||||||
});
|
});
|
||||||
|
let _ = sender.send(Ok(()));
|
||||||
|
// let _ = sender.send(Ok(message.clone()));
|
||||||
let result = write_handle.write(message).await;
|
let result = write_handle.write(message).await;
|
||||||
match result {
|
match result {
|
||||||
Ok(_) => {
|
Ok(_) => {
|
||||||
let message = Message {
|
let mut message = Message {
|
||||||
id,
|
id,
|
||||||
from: owned_jid,
|
from: owned_jid,
|
||||||
body,
|
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(
|
let _ = update_sender.send(UpdateMessage::Error(Error::MessageSend(
|
||||||
error::MessageSendError::MessageHistory(e),
|
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(_) => {
|
Err(_) => {
|
||||||
let _ = sender.send(result);
|
// let _ = sender.send(result);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue