From 3ce259b14384b9fb6f8274383ce52b51f8e54ee9 Mon Sep 17 00:00:00 2001 From: "xmppftw@kl.netlib.re" Date: Sat, 30 Dec 2023 13:09:01 +0100 Subject: [PATCH] Move handling of MessageType::GroupChat to message::receive::group_chat module --- xmpp/src/message/receive/group_chat.rs | 30 +++++++++++++++++++ .../message/{receive.rs => receive/mod.rs} | 28 ++++++++--------- 2 files changed, 43 insertions(+), 15 deletions(-) create mode 100644 xmpp/src/message/receive/group_chat.rs rename xmpp/src/message/{receive.rs => receive/mod.rs} (78%) diff --git a/xmpp/src/message/receive/group_chat.rs b/xmpp/src/message/receive/group_chat.rs new file mode 100644 index 0000000..7cf81f8 --- /dev/null +++ b/xmpp/src/message/receive/group_chat.rs @@ -0,0 +1,30 @@ +// Copyright (c) 2023 xmpp-rs contributors. +// +// This Source Code Form is subject to the terms of the Mozilla Public +// License, v. 2.0. If a copy of the MPL was not distributed with this +// file, You can obtain one at http://mozilla.org/MPL/2.0/. + +use tokio_xmpp::{parsers::message::Message, Jid}; + +use crate::{Agent, Event}; + +pub async fn handle_message_group_chat( + agent: &mut Agent, + events: &mut Vec, + from: Jid, + message: &Message, +) { + let langs: Vec<&str> = agent.lang.iter().map(String::as_str).collect(); + if let Some((_lang, body)) = message.get_best_body(langs) { + let event = match from.clone() { + Jid::Full(full) => Event::RoomMessage( + message.id.clone(), + from.to_bare(), + full.resource_str().to_owned(), + body.clone(), + ), + Jid::Bare(bare) => Event::ServiceMessage(message.id.clone(), bare, body.clone()), + }; + events.push(event) + } +} diff --git a/xmpp/src/message/receive.rs b/xmpp/src/message/receive/mod.rs similarity index 78% rename from xmpp/src/message/receive.rs rename to xmpp/src/message/receive/mod.rs index 2a4032e..521513a 100644 --- a/xmpp/src/message/receive.rs +++ b/xmpp/src/message/receive/mod.rs @@ -15,26 +15,24 @@ use tokio_xmpp::{ use crate::{pubsub, Agent, Event}; +pub mod group_chat; + pub async fn handle_message(agent: &mut Agent, message: Message) -> Vec { let mut events = vec![]; let from = message.from.clone().unwrap(); - let langs: Vec<&str> = agent.lang.iter().map(String::as_str).collect(); + //let langs: Vec<&str> = agent.lang.iter().map(String::as_str).collect(); + let langs = agent.lang.to_vec(); + let langs = langs.iter().map(|x| x.as_str()).collect(); + + match message.type_ { + MessageType::Groupchat => { + group_chat::handle_message_group_chat(agent, &mut events, from.clone(), &message).await; + } + _ => {} + } + match message.get_best_body(langs) { Some((_lang, body)) => match message.type_ { - MessageType::Groupchat => { - let event = match from.clone() { - Jid::Full(full) => Event::RoomMessage( - message.id.clone(), - from.to_bare(), - full.resource_str().to_owned(), - body.clone(), - ), - Jid::Bare(bare) => { - Event::ServiceMessage(message.id.clone(), bare, body.clone()) - } - }; - events.push(event) - } MessageType::Chat | MessageType::Normal => { let mut found_special_message = false;