1// Copyright (c) 2023 xmpp-rs contributors.
2//
3// This Source Code Form is subject to the terms of the Mozilla Public
4// License, v. 2.0. If a copy of the MPL was not distributed with this
5// file, You can obtain one at http://mozilla.org/MPL/2.0/.
67use tokio_xmpp::parsers::{
8 message::{Message, MessageType},
9 ns,
10};
1112use crate::{delay::message_time_info, pubsub, Agent, Event};
1314pub mod chat;
15pub mod group_chat;
1617pub async fn handle_message(agent: &mut Agent, mut message: Message) -> Vec<Event> {
18let mut events = vec![];
19let from = message.from.clone().unwrap();
20let time_info = message_time_info(&message);
2122match message.type_ {
23 MessageType::Groupchat => {
24 group_chat::handle_message_group_chat(
25 agent,
26&mut events,
27 from.clone(),
28&mut message,
29 time_info,
30 )
31 .await;
32 }
33 MessageType::Chat | MessageType::Normal => {
34 chat::handle_message_chat(agent, &mut events, from.clone(), &mut message, time_info)
35 .await;
36 }
37_ => {}
38 }
3940// TODO: should this be here or in specific branch of messagetype?
41 // We may drop some payloads in branches before (&mut message), but
42 // that's ok because pubsub only wants the pubsub payload.
43for child in message.payloads {
44if child.is("event", ns::PUBSUB_EVENT) {
45let new_events = pubsub::handle_event(&from, child, agent).await;
46 events.extend(new_events);
47 }
48 }
4950 events
51}