forked from lavina/lavina
1
0
Fork 0

split updates

This commit is contained in:
Nikita Vilunov 2023-10-13 00:58:16 +02:00
parent ca58ae1877
commit 6226e92438
5 changed files with 61 additions and 38 deletions

View File

@ -1,10 +1,10 @@
//! Handling of all iq stanzas //! Handling of all client2server iq stanzas
use quick_xml::events::Event; use quick_xml::events::Event;
use lavina_core::room::RoomRegistry; use lavina_core::room::RoomRegistry;
use proto_xmpp::bind::{BindResponse, Jid, Name, Resource, Server}; 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::disco::{Feature, Identity, InfoQuery, Item, ItemQuery};
use proto_xmpp::roster::RosterQuery; use proto_xmpp::roster::RosterQuery;
use proto_xmpp::session::Session; use proto_xmpp::session::Session;
@ -22,7 +22,7 @@ impl<'a> XmppConnection<'a> {
from: None, from: None,
id: iq.id, id: iq.id,
to: None, to: None,
r#type: proto_xmpp::client::IqType::Result, r#type: IqType::Result,
body: BindResponse(Jid { body: BindResponse(Jid {
name: Some(Name("darova".into())), name: Some(Name("darova".into())),
server: Server("localhost".into()), server: Server("localhost".into()),
@ -36,7 +36,7 @@ impl<'a> XmppConnection<'a> {
from: None, from: None,
id: iq.id, id: iq.id,
to: None, to: None,
r#type: proto_xmpp::client::IqType::Result, r#type: IqType::Result,
body: Session, body: Session,
}; };
req.serialize(output); req.serialize(output);
@ -46,7 +46,7 @@ impl<'a> XmppConnection<'a> {
from: None, from: None,
id: iq.id, id: iq.id,
to: None, to: None,
r#type: proto_xmpp::client::IqType::Result, r#type: IqType::Result,
body: RosterQuery, body: RosterQuery,
}; };
req.serialize(output); req.serialize(output);
@ -57,7 +57,7 @@ impl<'a> XmppConnection<'a> {
from: iq.to, from: iq.to,
id: iq.id, id: iq.id,
to: None, to: None,
r#type: proto_xmpp::client::IqType::Result, r#type: IqType::Result,
body: response, body: response,
}; };
req.serialize(output); req.serialize(output);
@ -68,7 +68,7 @@ impl<'a> XmppConnection<'a> {
from: iq.to, from: iq.to,
id: iq.id, id: iq.id,
to: None, to: None,
r#type: proto_xmpp::client::IqType::Result, r#type: IqType::Result,
body: response, body: response,
}; };
req.serialize(output); req.serialize(output);
@ -78,7 +78,7 @@ impl<'a> XmppConnection<'a> {
from: None, from: None,
id: iq.id, id: iq.id,
to: None, to: None,
r#type: proto_xmpp::client::IqType::Error, r#type: IqType::Error,
body: (), body: (),
}; };
req.serialize(output); req.serialize(output);

View File

@ -27,18 +27,17 @@ use lavina_core::prelude::*;
use lavina_core::repo::Storage; use lavina_core::repo::Storage;
use lavina_core::room::RoomRegistry; use lavina_core::room::RoomRegistry;
use lavina_core::terminator::Terminator; use lavina_core::terminator::Terminator;
use proto_xmpp::bind::{Jid, Name, Resource, Server}; use proto_xmpp::bind::{Name, Resource};
use proto_xmpp::client::Message;
use proto_xmpp::sasl::AuthBody; use proto_xmpp::sasl::AuthBody;
use proto_xmpp::stream::*; use proto_xmpp::stream::*;
use proto_xmpp::xml::{Continuation, FromXml, Parser, ToXml}; use proto_xmpp::xml::{Continuation, FromXml, Parser, ToXml};
use self::proto::ClientPacket; use self::proto::ClientPacket;
mod iq; mod iq;
mod presence;
mod message; mod message;
mod presence;
mod updates;
#[derive(Deserialize, Debug, Clone)] #[derive(Deserialize, Debug, Clone)]
pub struct ServerConfig { pub struct ServerConfig {
@ -364,30 +363,7 @@ async fn socket_final(
}, },
update = conn.user_handle.receiver.recv() => { update = conn.user_handle.receiver.recv() => {
if let Some(update) = update { if let Some(update) = update {
match update { conn.handle_update(&mut events, update).await?;
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);
}
_ => {},
}
for i in &events { for i in &events {
xml_writer.write_event_async(i).await?; xml_writer.write_event_async(i).await?;
} }

View File

@ -1,3 +1,5 @@
//! Handling of all client2server message stanzas
use quick_xml::events::Event; use quick_xml::events::Event;
use lavina_core::prelude::*; use lavina_core::prelude::*;
@ -32,7 +34,7 @@ impl<'a> XmppConnection<'a> {
resource: Some(self.user.xmpp_muc_name.clone()), resource: Some(self.user.xmpp_muc_name.clone()),
}), }),
id: m.id, id: m.id,
r#type: proto_xmpp::client::MessageType::Groupchat, r#type: MessageType::Groupchat,
lang: None, lang: None,
subject: None, subject: None,
body: m.body.clone(), body: m.body.clone(),

View File

@ -1,4 +1,4 @@
//! Handling of all presence stanzas //! Handling of all client2server presence stanzas
use quick_xml::events::Event; use quick_xml::events::Event;

View File

@ -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<Event<'static>>, 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(())
}
}