From 6226e924386cc13039ad908557c770c72c778524 Mon Sep 17 00:00:00 2001 From: Nikita Vilunov Date: Fri, 13 Oct 2023 00:58:16 +0200 Subject: [PATCH] split updates --- crates/projection-xmpp/src/iq.rs | 16 ++++----- crates/projection-xmpp/src/lib.rs | 32 +++--------------- crates/projection-xmpp/src/message.rs | 4 ++- crates/projection-xmpp/src/presence.rs | 2 +- crates/projection-xmpp/src/updates.rs | 45 ++++++++++++++++++++++++++ 5 files changed, 61 insertions(+), 38 deletions(-) create mode 100644 crates/projection-xmpp/src/updates.rs diff --git a/crates/projection-xmpp/src/iq.rs b/crates/projection-xmpp/src/iq.rs index e66e0da..3ad0ae5 100644 --- a/crates/projection-xmpp/src/iq.rs +++ b/crates/projection-xmpp/src/iq.rs @@ -1,10 +1,10 @@ -//! Handling of all iq stanzas +//! Handling of all client2server iq stanzas use quick_xml::events::Event; use lavina_core::room::RoomRegistry; use proto_xmpp::bind::{BindResponse, Jid, Name, Resource, Server}; -use proto_xmpp::client::Iq; +use proto_xmpp::client::{Iq, IqType}; use proto_xmpp::disco::{Feature, Identity, InfoQuery, Item, ItemQuery}; use proto_xmpp::roster::RosterQuery; use proto_xmpp::session::Session; @@ -22,7 +22,7 @@ impl<'a> XmppConnection<'a> { from: None, id: iq.id, to: None, - r#type: proto_xmpp::client::IqType::Result, + r#type: IqType::Result, body: BindResponse(Jid { name: Some(Name("darova".into())), server: Server("localhost".into()), @@ -36,7 +36,7 @@ impl<'a> XmppConnection<'a> { from: None, id: iq.id, to: None, - r#type: proto_xmpp::client::IqType::Result, + r#type: IqType::Result, body: Session, }; req.serialize(output); @@ -46,7 +46,7 @@ impl<'a> XmppConnection<'a> { from: None, id: iq.id, to: None, - r#type: proto_xmpp::client::IqType::Result, + r#type: IqType::Result, body: RosterQuery, }; req.serialize(output); @@ -57,7 +57,7 @@ impl<'a> XmppConnection<'a> { from: iq.to, id: iq.id, to: None, - r#type: proto_xmpp::client::IqType::Result, + r#type: IqType::Result, body: response, }; req.serialize(output); @@ -68,7 +68,7 @@ impl<'a> XmppConnection<'a> { from: iq.to, id: iq.id, to: None, - r#type: proto_xmpp::client::IqType::Result, + r#type: IqType::Result, body: response, }; req.serialize(output); @@ -78,7 +78,7 @@ impl<'a> XmppConnection<'a> { from: None, id: iq.id, to: None, - r#type: proto_xmpp::client::IqType::Error, + r#type: IqType::Error, body: (), }; req.serialize(output); diff --git a/crates/projection-xmpp/src/lib.rs b/crates/projection-xmpp/src/lib.rs index 371d89d..9865d24 100644 --- a/crates/projection-xmpp/src/lib.rs +++ b/crates/projection-xmpp/src/lib.rs @@ -27,18 +27,17 @@ use lavina_core::prelude::*; use lavina_core::repo::Storage; use lavina_core::room::RoomRegistry; use lavina_core::terminator::Terminator; -use proto_xmpp::bind::{Jid, Name, Resource, Server}; -use proto_xmpp::client::Message; +use proto_xmpp::bind::{Name, Resource}; use proto_xmpp::sasl::AuthBody; - use proto_xmpp::stream::*; use proto_xmpp::xml::{Continuation, FromXml, Parser, ToXml}; use self::proto::ClientPacket; mod iq; -mod presence; mod message; +mod presence; +mod updates; #[derive(Deserialize, Debug, Clone)] pub struct ServerConfig { @@ -364,30 +363,7 @@ async fn socket_final( }, update = conn.user_handle.receiver.recv() => { if let Some(update) = update { - match update { - lavina_core::player::Updates::NewMessage { room_id, author_id, body } => { - Message::<()> { - to: Some(Jid { - name: Some(authenticated.xmpp_name.clone()), - server: Server("localhost".into()), - resource: Some(authenticated.xmpp_resource.clone()), - }), - from: Some(Jid { - name: Some(Name(room_id.into_inner().into())), - server: Server("rooms.localhost".into()), - resource: Some(Resource(author_id.into_inner().into())), - }), - id: None, - r#type: proto_xmpp::client::MessageType::Groupchat, - lang: None, - subject: None, - body: body.into(), - custom: vec![], - } - .serialize(&mut events); - } - _ => {}, - } + conn.handle_update(&mut events, update).await?; for i in &events { xml_writer.write_event_async(i).await?; } diff --git a/crates/projection-xmpp/src/message.rs b/crates/projection-xmpp/src/message.rs index 7128ed6..9369076 100644 --- a/crates/projection-xmpp/src/message.rs +++ b/crates/projection-xmpp/src/message.rs @@ -1,3 +1,5 @@ +//! Handling of all client2server message stanzas + use quick_xml::events::Event; use lavina_core::prelude::*; @@ -32,7 +34,7 @@ impl<'a> XmppConnection<'a> { resource: Some(self.user.xmpp_muc_name.clone()), }), id: m.id, - r#type: proto_xmpp::client::MessageType::Groupchat, + r#type: MessageType::Groupchat, lang: None, subject: None, body: m.body.clone(), diff --git a/crates/projection-xmpp/src/presence.rs b/crates/projection-xmpp/src/presence.rs index e348a16..eabf0fd 100644 --- a/crates/projection-xmpp/src/presence.rs +++ b/crates/projection-xmpp/src/presence.rs @@ -1,4 +1,4 @@ -//! Handling of all presence stanzas +//! Handling of all client2server presence stanzas use quick_xml::events::Event; diff --git a/crates/projection-xmpp/src/updates.rs b/crates/projection-xmpp/src/updates.rs new file mode 100644 index 0000000..c211be8 --- /dev/null +++ b/crates/projection-xmpp/src/updates.rs @@ -0,0 +1,45 @@ +//! Handling of updates and converting them into server2client stanzas + +use anyhow::Result; +use quick_xml::events::Event; + +use lavina_core::player::Updates; +use proto_xmpp::bind::{Jid, Name, Resource, Server}; +use proto_xmpp::client::{Message, MessageType}; +use proto_xmpp::xml::ToXml; + +use crate::XmppConnection; + +impl<'a> XmppConnection<'a> { + pub async fn handle_update(&mut self, output: &mut Vec>, update: Updates) -> Result<()> { + match update { + Updates::NewMessage { + room_id, + author_id, + body, + } => { + Message::<()> { + to: Some(Jid { + name: Some(self.user.xmpp_name.clone()), + server: Server("localhost".into()), + resource: Some(self.user.xmpp_resource.clone()), + }), + from: Some(Jid { + name: Some(Name(room_id.into_inner().into())), + server: Server("rooms.localhost".into()), + resource: Some(Resource(author_id.into_inner().into())), + }), + id: None, + r#type: MessageType::Groupchat, + lang: None, + subject: None, + body: body.into(), + custom: vec![], + } + .serialize(output); + } + _ => {} + } + Ok(()) + } +}