2021-05-15 14:03:22 +00:00
|
|
|
use std::{collections::BTreeMap, convert::TryFrom};
|
2021-05-12 10:56:29 +00:00
|
|
|
|
2021-04-28 09:48:20 +00:00
|
|
|
use ruma::{
|
|
|
|
api::client::r0::sync::sync_events::{
|
2021-05-12 14:21:44 +00:00
|
|
|
Ephemeral, InvitedRoom, Presence, RoomAccountData, State, ToDevice,
|
2021-04-28 09:48:20 +00:00
|
|
|
},
|
|
|
|
serde::Raw,
|
|
|
|
DeviceIdBox,
|
|
|
|
};
|
2020-11-21 21:48:27 +00:00
|
|
|
use serde::{Deserialize, Serialize};
|
|
|
|
|
2021-01-19 11:29:46 +00:00
|
|
|
use super::{
|
2021-04-12 09:09:39 +00:00
|
|
|
api::r0::{
|
|
|
|
push::get_notifications::Notification,
|
|
|
|
sync::sync_events::{
|
2021-05-12 14:21:44 +00:00
|
|
|
DeviceLists, GlobalAccountData,
|
|
|
|
UnreadNotificationsCount as RumaUnreadNotificationsCount,
|
2021-04-12 09:09:39 +00:00
|
|
|
},
|
2020-12-07 14:11:18 +00:00
|
|
|
},
|
2020-12-07 13:13:31 +00:00
|
|
|
events::{
|
2021-04-28 09:48:20 +00:00
|
|
|
room::member::MemberEventContent, AnySyncRoomEvent, StateEvent, StrippedStateEvent,
|
|
|
|
SyncStateEvent, Unsigned,
|
2020-12-07 13:13:31 +00:00
|
|
|
},
|
2020-12-12 20:44:53 +00:00
|
|
|
identifiers::{DeviceKeyAlgorithm, EventId, RoomId, UserId},
|
2021-05-15 14:03:22 +00:00
|
|
|
MilliSecondsSinceUnixEpoch,
|
2020-11-21 21:48:27 +00:00
|
|
|
};
|
|
|
|
|
2021-01-28 13:10:26 +00:00
|
|
|
/// A change in ambiguity of room members that an `m.room.member` event
|
|
|
|
/// triggers.
|
2021-01-27 10:42:13 +00:00
|
|
|
#[derive(Clone, Debug, Default, Deserialize, Serialize)]
|
|
|
|
pub struct AmbiguityChange {
|
2021-01-28 13:10:26 +00:00
|
|
|
/// Is the member that is contained in the state key of the `m.room.member`
|
|
|
|
/// event itself ambiguous because of the event.
|
2021-01-27 10:42:13 +00:00
|
|
|
pub member_ambiguous: bool,
|
2021-01-28 13:10:26 +00:00
|
|
|
/// Has another user been disambiguated because of this event.
|
2021-01-27 10:42:13 +00:00
|
|
|
pub disambiguated_member: Option<UserId>,
|
2021-01-28 13:10:26 +00:00
|
|
|
/// Has another user become ambiguous because of this event.
|
2021-01-27 10:42:13 +00:00
|
|
|
pub ambiguated_member: Option<UserId>,
|
|
|
|
}
|
|
|
|
|
2021-01-28 13:10:26 +00:00
|
|
|
/// Collection of ambiguioty changes that room member events trigger.
|
2021-01-27 10:42:13 +00:00
|
|
|
#[derive(Clone, Debug, Default, Deserialize, Serialize)]
|
|
|
|
pub struct AmbiguityChanges {
|
2021-01-28 13:10:26 +00:00
|
|
|
/// A map from room id to a map of an event id to the `AmbiguityChange` that
|
|
|
|
/// the event with the given id caused.
|
2021-01-27 10:42:13 +00:00
|
|
|
pub changes: BTreeMap<RoomId, BTreeMap<EventId, AmbiguityChange>>,
|
|
|
|
}
|
|
|
|
|
2021-04-28 09:48:20 +00:00
|
|
|
/// The verification state of the device that sent an event to us.
|
|
|
|
#[derive(Clone, Debug, Deserialize, Serialize)]
|
|
|
|
pub enum VerificationState {
|
|
|
|
/// The device is trusted.
|
|
|
|
Trusted,
|
|
|
|
/// The device is not trusted.
|
|
|
|
Untrusted,
|
|
|
|
/// The device is not known to us.
|
|
|
|
UnknownDevice,
|
|
|
|
}
|
|
|
|
|
|
|
|
/// The algorithm specific information of a decrypted event.
|
|
|
|
#[derive(Clone, Debug, Deserialize, Serialize)]
|
|
|
|
pub enum AlgorithmInfo {
|
|
|
|
/// The info if the event was encrypted using m.megolm.v1.aes-sha2
|
|
|
|
MegolmV1AesSha2 {
|
|
|
|
/// The curve25519 key of the device that created the megolm decryption
|
|
|
|
/// key originally.
|
|
|
|
curve25519_key: String,
|
|
|
|
/// The signing keys that have created the megolm key that was used to
|
|
|
|
/// decrypt this session. This map will usually contain a signle ed25519
|
|
|
|
/// key.
|
|
|
|
sender_claimed_keys: BTreeMap<DeviceKeyAlgorithm, String>,
|
|
|
|
/// Chain of curve25519 keys through which this session was forwarded,
|
|
|
|
/// via m.forwarded_room_key events.
|
|
|
|
forwarding_curve25519_key_chain: Vec<String>,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Struct containing information on how an event was decrypted.
|
|
|
|
#[derive(Clone, Debug, Deserialize, Serialize)]
|
|
|
|
pub struct EncryptionInfo {
|
|
|
|
/// The user ID of the event sender, note this is untrusted data unless the
|
|
|
|
/// `verification_state` is as well trusted.
|
|
|
|
pub sender: UserId,
|
|
|
|
/// The device ID of the device that sent us the event, note this is
|
|
|
|
/// untrusted data unless `verification_state` is as well trusted.
|
|
|
|
pub sender_device: DeviceIdBox,
|
|
|
|
/// Information about the algorithm that was used to encrypt the event.
|
|
|
|
pub algorithm_info: AlgorithmInfo,
|
|
|
|
/// The verification state of the device that sent us the event, note this
|
|
|
|
/// is the state of the device at the time of decryption. It may change in
|
|
|
|
/// the future if a device gets verified or deleted.
|
|
|
|
pub verification_state: VerificationState,
|
|
|
|
}
|
|
|
|
|
2021-04-29 13:34:04 +00:00
|
|
|
/// A customized version of a room event coming from a sync that holds optional
|
2021-04-29 13:34:39 +00:00
|
|
|
/// encryption info.
|
2021-04-28 09:48:20 +00:00
|
|
|
#[derive(Clone, Debug, Deserialize, Serialize)]
|
|
|
|
pub struct SyncRoomEvent {
|
|
|
|
/// The actual event.
|
|
|
|
pub event: Raw<AnySyncRoomEvent>,
|
|
|
|
/// The encryption info about the event. Will be `None` if the event was not
|
|
|
|
/// encrypted.
|
|
|
|
pub encryption_info: Option<EncryptionInfo>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<Raw<AnySyncRoomEvent>> for SyncRoomEvent {
|
|
|
|
fn from(inner: Raw<AnySyncRoomEvent>) -> Self {
|
2021-05-12 15:00:47 +00:00
|
|
|
Self { encryption_info: None, event: inner }
|
2021-04-28 09:48:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-21 21:48:27 +00:00
|
|
|
#[derive(Clone, Debug, Default, Deserialize, Serialize)]
|
|
|
|
pub struct SyncResponse {
|
2021-05-12 15:00:47 +00:00
|
|
|
/// The batch token to supply in the `since` param of the next `/sync`
|
|
|
|
/// request.
|
2020-11-21 21:48:27 +00:00
|
|
|
pub next_batch: String,
|
|
|
|
/// Updates to rooms.
|
|
|
|
pub rooms: Rooms,
|
2020-11-30 13:42:08 +00:00
|
|
|
/// Updates to the presence status of other users.
|
|
|
|
pub presence: Presence,
|
2020-12-07 15:35:00 +00:00
|
|
|
/// The global private data created by this user.
|
2021-05-12 14:21:44 +00:00
|
|
|
pub account_data: GlobalAccountData,
|
2020-11-30 14:24:49 +00:00
|
|
|
/// Messages sent dirrectly between devices.
|
|
|
|
pub to_device: ToDevice,
|
|
|
|
/// Information on E2E device updates.
|
|
|
|
///
|
|
|
|
/// Only present on an incremental sync.
|
|
|
|
pub device_lists: DeviceLists,
|
|
|
|
/// For each key algorithm, the number of unclaimed one-time keys
|
|
|
|
/// currently held on the server for a device.
|
|
|
|
pub device_one_time_keys_count: BTreeMap<DeviceKeyAlgorithm, u64>,
|
2021-01-27 10:42:13 +00:00
|
|
|
/// Collection of ambiguioty changes that room member events trigger.
|
|
|
|
pub ambiguity_changes: AmbiguityChanges,
|
2021-04-12 09:09:39 +00:00
|
|
|
/// New notifications per room.
|
|
|
|
pub notifications: BTreeMap<RoomId, Vec<Notification>>,
|
2020-11-21 21:48:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl SyncResponse {
|
2020-12-01 09:23:28 +00:00
|
|
|
pub fn new(next_batch: String) -> Self {
|
2021-05-12 15:00:47 +00:00
|
|
|
Self { next_batch, ..Default::default() }
|
2020-11-21 21:48:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clone, Debug, Default, Deserialize, Serialize)]
|
|
|
|
pub struct Rooms {
|
2020-12-01 09:23:28 +00:00
|
|
|
/// The rooms that the user has left or been banned from.
|
|
|
|
pub leave: BTreeMap<RoomId, LeftRoom>,
|
2020-11-21 21:48:27 +00:00
|
|
|
/// The rooms that the user has joined.
|
|
|
|
pub join: BTreeMap<RoomId, JoinedRoom>,
|
2020-12-08 08:52:27 +00:00
|
|
|
/// The rooms that the user has been invited to.
|
|
|
|
pub invite: BTreeMap<RoomId, InvitedRoom>,
|
2020-11-21 21:48:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Updates to joined rooms.
|
|
|
|
#[derive(Clone, Debug, Deserialize, Serialize)]
|
|
|
|
pub struct JoinedRoom {
|
2020-12-07 14:11:18 +00:00
|
|
|
/// Counts of unread notifications for this room.
|
|
|
|
pub unread_notifications: UnreadNotificationsCount,
|
2020-11-21 21:48:27 +00:00
|
|
|
/// The timeline of messages and state changes in the room.
|
|
|
|
pub timeline: Timeline,
|
2021-05-12 15:00:47 +00:00
|
|
|
/// Updates to the state, between the time indicated by the `since`
|
|
|
|
/// parameter, and the start of the `timeline` (or all state up to the
|
|
|
|
/// start of the `timeline`, if `since` is not given, or `full_state` is
|
|
|
|
/// true).
|
2020-11-21 21:48:27 +00:00
|
|
|
pub state: State,
|
2020-12-07 13:13:31 +00:00
|
|
|
/// The private data that this user has attached to this room.
|
2021-05-12 14:21:44 +00:00
|
|
|
pub account_data: RoomAccountData,
|
2021-05-12 15:00:47 +00:00
|
|
|
/// The ephemeral events in the room that aren't recorded in the timeline or
|
|
|
|
/// state of the room. e.g. typing.
|
2020-12-07 13:34:18 +00:00
|
|
|
pub ephemeral: Ephemeral,
|
2020-11-21 21:48:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl JoinedRoom {
|
2020-12-07 13:34:18 +00:00
|
|
|
pub fn new(
|
|
|
|
timeline: Timeline,
|
|
|
|
state: State,
|
2021-05-12 14:21:44 +00:00
|
|
|
account_data: RoomAccountData,
|
2020-12-07 13:34:18 +00:00
|
|
|
ephemeral: Ephemeral,
|
2020-12-07 14:11:18 +00:00
|
|
|
unread_notifications: UnreadNotificationsCount,
|
2020-12-07 13:34:18 +00:00
|
|
|
) -> Self {
|
2021-05-12 15:00:47 +00:00
|
|
|
Self { unread_notifications, timeline, state, account_data, ephemeral }
|
2020-12-07 14:11:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Counts of unread notifications for a room.
|
|
|
|
#[derive(Copy, Clone, Debug, Default, Deserialize, Serialize)]
|
|
|
|
pub struct UnreadNotificationsCount {
|
2021-05-12 15:00:47 +00:00
|
|
|
/// The number of unread notifications for this room with the highlight flag
|
|
|
|
/// set.
|
2021-02-02 20:11:51 +00:00
|
|
|
pub highlight_count: u64,
|
2020-12-07 14:11:18 +00:00
|
|
|
/// The total number of unread notifications for this room.
|
2021-02-02 20:11:51 +00:00
|
|
|
pub notification_count: u64,
|
2020-12-07 14:11:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl From<RumaUnreadNotificationsCount> for UnreadNotificationsCount {
|
|
|
|
fn from(notifications: RumaUnreadNotificationsCount) -> Self {
|
|
|
|
Self {
|
|
|
|
highlight_count: notifications.highlight_count.map(|c| c.into()).unwrap_or(0),
|
2021-05-12 15:00:47 +00:00
|
|
|
notification_count: notifications.notification_count.map(|c| c.into()).unwrap_or(0),
|
2020-12-07 13:13:31 +00:00
|
|
|
}
|
2020-11-21 21:48:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-01 09:23:28 +00:00
|
|
|
#[derive(Clone, Debug, Deserialize, Serialize)]
|
|
|
|
pub struct LeftRoom {
|
|
|
|
/// The timeline of messages and state changes in the room up to the point
|
|
|
|
/// when the user left.
|
|
|
|
pub timeline: Timeline,
|
2021-05-12 15:00:47 +00:00
|
|
|
/// Updates to the state, between the time indicated by the `since`
|
|
|
|
/// parameter, and the start of the `timeline` (or all state up to the
|
|
|
|
/// start of the `timeline`, if `since` is not given, or `full_state` is
|
|
|
|
/// true).
|
2020-12-01 09:23:28 +00:00
|
|
|
pub state: State,
|
2020-12-07 13:13:31 +00:00
|
|
|
/// The private data that this user has attached to this room.
|
2021-05-12 14:21:44 +00:00
|
|
|
pub account_data: RoomAccountData,
|
2020-12-01 09:23:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl LeftRoom {
|
2021-05-12 14:21:44 +00:00
|
|
|
pub fn new(timeline: Timeline, state: State, account_data: RoomAccountData) -> Self {
|
2021-05-12 15:00:47 +00:00
|
|
|
Self { timeline, state, account_data }
|
2020-12-01 09:23:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-21 21:48:27 +00:00
|
|
|
/// Events in the room.
|
|
|
|
#[derive(Clone, Debug, Default, Deserialize, Serialize)]
|
|
|
|
pub struct Timeline {
|
2021-05-12 15:00:47 +00:00
|
|
|
/// True if the number of events returned was limited by the `limit` on the
|
|
|
|
/// filter.
|
2020-11-21 21:48:27 +00:00
|
|
|
pub limited: bool,
|
|
|
|
|
|
|
|
/// A token that can be supplied to to the `from` parameter of the
|
|
|
|
/// `/rooms/{roomId}/messages` endpoint.
|
|
|
|
pub prev_batch: Option<String>,
|
|
|
|
|
|
|
|
/// A list of events.
|
2021-04-28 09:48:20 +00:00
|
|
|
pub events: Vec<SyncRoomEvent>,
|
2020-11-21 21:48:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Timeline {
|
|
|
|
pub fn new(limited: bool, prev_batch: Option<String>) -> Self {
|
2021-05-12 15:00:47 +00:00
|
|
|
Self { limited, prev_batch, ..Default::default() }
|
2020-11-21 21:48:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-12 20:44:53 +00:00
|
|
|
#[derive(Clone, Debug, Deserialize, Serialize)]
|
|
|
|
#[serde(
|
|
|
|
try_from = "SyncStateEvent<MemberEventContent>",
|
|
|
|
into = "SyncStateEvent<MemberEventContent>"
|
|
|
|
)]
|
|
|
|
pub struct MemberEvent {
|
|
|
|
pub content: MemberEventContent,
|
|
|
|
pub event_id: EventId,
|
2021-05-15 14:03:22 +00:00
|
|
|
pub origin_server_ts: MilliSecondsSinceUnixEpoch,
|
2020-12-12 20:44:53 +00:00
|
|
|
pub prev_content: Option<MemberEventContent>,
|
|
|
|
pub sender: UserId,
|
|
|
|
pub state_key: UserId,
|
|
|
|
pub unsigned: Unsigned,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl TryFrom<SyncStateEvent<MemberEventContent>> for MemberEvent {
|
2021-01-19 11:29:46 +00:00
|
|
|
type Error = super::identifiers::Error;
|
2020-12-12 20:44:53 +00:00
|
|
|
|
|
|
|
fn try_from(event: SyncStateEvent<MemberEventContent>) -> Result<Self, Self::Error> {
|
|
|
|
Ok(MemberEvent {
|
|
|
|
content: event.content,
|
|
|
|
event_id: event.event_id,
|
|
|
|
origin_server_ts: event.origin_server_ts,
|
|
|
|
prev_content: event.prev_content,
|
|
|
|
sender: event.sender,
|
|
|
|
state_key: UserId::try_from(event.state_key)?,
|
|
|
|
unsigned: event.unsigned,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl TryFrom<StateEvent<MemberEventContent>> for MemberEvent {
|
2021-01-19 11:29:46 +00:00
|
|
|
type Error = super::identifiers::Error;
|
2020-12-12 20:44:53 +00:00
|
|
|
|
|
|
|
fn try_from(event: StateEvent<MemberEventContent>) -> Result<Self, Self::Error> {
|
|
|
|
Ok(MemberEvent {
|
|
|
|
content: event.content,
|
|
|
|
event_id: event.event_id,
|
|
|
|
origin_server_ts: event.origin_server_ts,
|
|
|
|
prev_content: event.prev_content,
|
|
|
|
sender: event.sender,
|
|
|
|
state_key: UserId::try_from(event.state_key)?,
|
|
|
|
unsigned: event.unsigned,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-30 11:05:13 +00:00
|
|
|
impl From<MemberEvent> for SyncStateEvent<MemberEventContent> {
|
|
|
|
fn from(other: MemberEvent) -> SyncStateEvent<MemberEventContent> {
|
2020-12-12 20:44:53 +00:00
|
|
|
SyncStateEvent {
|
2021-03-30 11:05:13 +00:00
|
|
|
content: other.content,
|
|
|
|
event_id: other.event_id,
|
|
|
|
sender: other.sender,
|
|
|
|
origin_server_ts: other.origin_server_ts,
|
|
|
|
state_key: other.state_key.to_string(),
|
|
|
|
prev_content: other.prev_content,
|
|
|
|
unsigned: other.unsigned,
|
2020-12-12 20:44:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clone, Debug, Deserialize, Serialize)]
|
|
|
|
#[serde(
|
|
|
|
try_from = "StrippedStateEvent<MemberEventContent>",
|
|
|
|
into = "StrippedStateEvent<MemberEventContent>"
|
|
|
|
)]
|
|
|
|
pub struct StrippedMemberEvent {
|
|
|
|
pub content: MemberEventContent,
|
|
|
|
pub sender: UserId,
|
|
|
|
pub state_key: UserId,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl TryFrom<StrippedStateEvent<MemberEventContent>> for StrippedMemberEvent {
|
2021-01-19 11:29:46 +00:00
|
|
|
type Error = super::identifiers::Error;
|
2020-12-12 20:44:53 +00:00
|
|
|
|
|
|
|
fn try_from(event: StrippedStateEvent<MemberEventContent>) -> Result<Self, Self::Error> {
|
|
|
|
Ok(StrippedMemberEvent {
|
|
|
|
content: event.content,
|
|
|
|
sender: event.sender,
|
|
|
|
state_key: UserId::try_from(event.state_key)?,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-30 11:05:13 +00:00
|
|
|
impl From<StrippedMemberEvent> for StrippedStateEvent<MemberEventContent> {
|
|
|
|
fn from(other: StrippedMemberEvent) -> Self {
|
|
|
|
Self {
|
|
|
|
content: other.content,
|
|
|
|
sender: other.sender,
|
|
|
|
state_key: other.state_key.to_string(),
|
2020-12-12 20:44:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-01-27 10:42:13 +00:00
|
|
|
|
2021-01-28 13:10:26 +00:00
|
|
|
/// A deserialized response for the rooms members API call.
|
|
|
|
///
|
|
|
|
/// [GET /_matrix/client/r0/rooms/{roomId}/members](https://matrix.org/docs/spec/client_server/r0.6.0#get-matrix-client-r0-rooms-roomid-members)
|
2021-01-27 10:42:13 +00:00
|
|
|
#[derive(Clone, Debug, Default, Deserialize, Serialize)]
|
|
|
|
pub struct MembersResponse {
|
2021-01-28 13:10:26 +00:00
|
|
|
/// The list of members events.
|
2021-01-27 10:42:13 +00:00
|
|
|
pub chunk: Vec<MemberEvent>,
|
|
|
|
/// Collection of ambiguioty changes that room member events trigger.
|
|
|
|
pub ambiguity_changes: AmbiguityChanges,
|
|
|
|
}
|