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/.
6
7use tokio_xmpp::connect::ServerConnector;
8use tokio_xmpp::parsers::{
9 message::{Message, MessageType},
10 ns,
11};
12
13use crate::{delay::message_time_info, pubsub, Agent, Event};
14
15pub mod chat;
16pub mod group_chat;
17
18pub async fn handle_message<C: ServerConnector>(
19 agent: &mut Agent<C>,
20 message: Message,
21) -> Vec<Event> {
22 let mut events = vec![];
23 let from = message.from.clone().unwrap();
24 let time_info = message_time_info(&message);
25
26 match message.type_ {
27 MessageType::Groupchat => {
28 group_chat::handle_message_group_chat(
29 agent,
30 &mut events,
31 from.clone(),
32 &message,
33 time_info,
34 )
35 .await;
36 }
37 MessageType::Chat | MessageType::Normal => {
38 chat::handle_message_chat(agent, &mut events, from.clone(), &message, time_info).await;
39 }
40 _ => {}
41 }
42
43 for child in message.payloads {
44 if child.is("event", ns::PUBSUB_EVENT) {
45 let new_events = pubsub::handle_event(&from, child, agent).await;
46 events.extend(new_events);
47 }
48 }
49
50 events
51}