matrix_sdk_crypto/types/events/room/
mod.rs

1// Copyright 2022 The Matrix.org Foundation C.I.C.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15//! Types for room events.
16
17use std::{collections::BTreeMap, fmt::Debug};
18
19use ruma::{EventId, MilliSecondsSinceUnixEpoch, OwnedEventId, OwnedUserId, UserId};
20use serde::{Deserialize, Serialize};
21use serde_json::Value;
22
23use super::EventType;
24
25pub mod encrypted;
26
27/// Generic room event with a known type and content.
28#[derive(Debug, Deserialize)]
29pub struct Event<C>
30where
31    C: EventType + Debug + Sized + Serialize,
32{
33    /// Contains the fully-qualified ID of the user who sent this event.
34    pub sender: OwnedUserId,
35
36    /// The globally unique identifier for this event.
37    pub event_id: OwnedEventId,
38
39    /// The body of this event, as created by the client which sent it.
40    pub content: C,
41
42    /// Timestamp (in milliseconds since the unix epoch) on originating
43    /// homeserver when this event was sent.
44    pub origin_server_ts: MilliSecondsSinceUnixEpoch,
45
46    /// Contains optional extra information about the event.
47    #[serde(default)]
48    pub unsigned: BTreeMap<String, Value>,
49
50    /// Any other unknown data of the room event.
51    #[serde(flatten)]
52    pub(crate) other: BTreeMap<String, Value>,
53}
54
55impl<C> Serialize for Event<C>
56where
57    C: EventType + Debug + Sized + Serialize,
58{
59    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
60    where
61        S: serde::Serializer,
62    {
63        #[derive(Serialize)]
64        struct Helper<'a, C> {
65            sender: &'a UserId,
66            event_id: &'a EventId,
67            #[serde(rename = "type")]
68            event_type: &'a str,
69            content: &'a C,
70            origin_server_ts: MilliSecondsSinceUnixEpoch,
71            #[serde(skip_serializing_if = "BTreeMap::is_empty")]
72            unsigned: &'a BTreeMap<String, Value>,
73            #[serde(flatten)]
74            other: &'a BTreeMap<String, Value>,
75        }
76
77        let event_type = C::EVENT_TYPE;
78
79        let helper = Helper {
80            sender: &self.sender,
81            content: &self.content,
82            event_type,
83            other: &self.other,
84            event_id: &self.event_id,
85            origin_server_ts: self.origin_server_ts,
86            unsigned: &self.unsigned,
87        };
88
89        helper.serialize(serializer)
90    }
91}