Skip to main content

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    /// Present if and only if this event is a state event.
40    #[cfg(feature = "experimental-encrypted-state-events")]
41    pub state_key: Option<String>,
42
43    /// The body of this event, as created by the client which sent it.
44    pub content: C,
45
46    /// Timestamp (in milliseconds since the unix epoch) on originating
47    /// homeserver when this event was sent.
48    pub origin_server_ts: MilliSecondsSinceUnixEpoch,
49
50    /// Contains optional extra information about the event.
51    #[serde(default)]
52    pub unsigned: BTreeMap<String, Value>,
53
54    /// Any other unknown data of the room event.
55    #[serde(flatten)]
56    pub(crate) other: BTreeMap<String, Value>,
57}
58
59impl<C> Serialize for Event<C>
60where
61    C: EventType + Debug + Sized + Serialize,
62{
63    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
64    where
65        S: serde::Serializer,
66    {
67        #[derive(Serialize)]
68        struct Helper<'a, C> {
69            sender: &'a UserId,
70            event_id: &'a EventId,
71            #[serde(rename = "type")]
72            event_type: &'a str,
73            content: &'a C,
74            origin_server_ts: MilliSecondsSinceUnixEpoch,
75            #[serde(skip_serializing_if = "BTreeMap::is_empty")]
76            unsigned: &'a BTreeMap<String, Value>,
77            #[serde(flatten)]
78            other: &'a BTreeMap<String, Value>,
79        }
80
81        let event_type = C::EVENT_TYPE;
82
83        let helper = Helper {
84            sender: &self.sender,
85            content: &self.content,
86            event_type,
87            other: &self.other,
88            event_id: &self.event_id,
89            origin_server_ts: self.origin_server_ts,
90            unsigned: &self.unsigned,
91        };
92
93        helper.serialize(serializer)
94    }
95}