From 99a49feff9f9bbb5e3e767331e2ae2b73ef3205a Mon Sep 17 00:00:00 2001 From: Mikhail Date: Sat, 4 May 2024 16:33:53 +0200 Subject: [PATCH] Describe response with count 0 --- crates/projection-xmpp/src/iq.rs | 23 +++++++------ crates/proto-xmpp/src/mam.rs | 56 ++++++++++++++++++++++++++++++-- 2 files changed, 66 insertions(+), 13 deletions(-) diff --git a/crates/projection-xmpp/src/iq.rs b/crates/projection-xmpp/src/iq.rs index fd0f98b..0232fbc 100644 --- a/crates/projection-xmpp/src/iq.rs +++ b/crates/projection-xmpp/src/iq.rs @@ -1,19 +1,20 @@ //! Handling of all client2server iq stanzas use quick_xml::events::Event; +use serde::de::Unexpected::Option; use lavina_core::room::{RoomId, RoomRegistry}; use proto_xmpp::bind::{BindResponse, Jid, Name, Server}; use proto_xmpp::client::{Iq, IqError, IqErrorType, IqType, Message, MessageType}; use proto_xmpp::disco::{Feature, Identity, InfoQuery, Item, ItemQuery}; +use proto_xmpp::mam::{Fin, Set}; use proto_xmpp::roster::RosterQuery; use proto_xmpp::session::Session; +use proto_xmpp::xml::ToXml; use crate::proto::IqClientBody; use crate::XmppConnection; -use proto_xmpp::xml::{Ignore, ToXml}; - impl<'a> XmppConnection<'a> { pub async fn handle_iq(&self, output: &mut Vec>, iq: Iq) { match iq.body { @@ -88,14 +89,16 @@ impl<'a> XmppConnection<'a> { req.serialize(output); } IqClientBody::MessageArchiveRequest(request) => { - let response = self.mam().await; - // let req = Iq { - // from: iq.to, - // id: iq.id, - // to: None, - // r#type: IqType::Result, - // body: response, - // }; + // let response = self.mam().await; + let response = Iq { + from: iq.to, + id: iq.id, + to: None, + r#type: IqType::Result, + body: Fin { + set: Set { count: Some(0) }, + }, + }; response.serialize(output); } _ => { diff --git a/crates/proto-xmpp/src/mam.rs b/crates/proto-xmpp/src/mam.rs index c134096..5e083c6 100644 --- a/crates/proto-xmpp/src/mam.rs +++ b/crates/proto-xmpp/src/mam.rs @@ -1,5 +1,5 @@ use anyhow::{anyhow, Result}; -use quick_xml::events::Event; +use quick_xml::events::{BytesStart, Event}; use quick_xml::name::{Namespace, ResolveResult}; use std::io::Read; @@ -7,6 +7,7 @@ use crate::xml::*; pub const MAM_XMLNS: &'static str = "urn:xmpp:mam:2"; pub const DATA_XMLNS: &'static str = "jabber:x:data"; +pub const RESULT_SET_XMLNS: &'static str = "http://jabber.org/protocol/rsm"; #[derive(PartialEq, Eq, Debug, Clone)] pub struct MessageArchiveRequest { @@ -23,6 +24,55 @@ pub struct Field { pub values: Vec, } +// Message archive response styled as a result set. +pub struct Fin { + pub set: Set, +} + +pub struct Set { + pub count: Option, +} + +impl FromXml for Set { + type P = impl Parser>; + + fn parse() -> Self::P { + todo!() + } +} + +impl FromXmlTag for Set { + const NAME: &'static str = "set"; + const NS: &'static str = "http://jabber.org/protocol/rsm"; +} + +impl FromXml for Fin { + type P = impl Parser>; + + fn parse() -> Self::P { + todo!() + } +} + +impl FromXmlTag for Fin { + const NAME: &'static str = "fin"; + const NS: &'static str = RESULT_SET_XMLNS; +} + +impl ToXml for Fin { + fn serialize(&self, events: &mut Vec>) { + events.push(Event::Start(format!(r#"fin xmlns="{}" complete=True"#, Fin::NS))); + events.push(Event::Start(format!(r#"set xmlns="{}""#, Set::NS))); + if let Some(count) = &self.set.count { + events.push(Event::Start("count".into())); + events.push(Event::Text("0".into())); + events.push(Event::End("count".into())); + } + events.push(Event::End("set".into())); + events.push(Event::End("fin".into())); + } +} + impl FromXmlTag for X { const NAME: &'static str = "x"; const NS: &'static str = DATA_XMLNS; @@ -140,7 +190,7 @@ mod tests { #[test] fn test_parse_archive_query() { - let input = r#""#; + let input = r#""; let result: Iq = parse(input).unwrap(); assert_eq!( @@ -161,7 +211,7 @@ mod tests { #[test] fn test_parse_query_messages_from_jid() { - let input = r#"value1juliet@capulet.lit"#; + let input = r#"value1juliet@capulet.lit"#; let result: Iq = parse(input).unwrap(); assert_eq!(