Use generic types for Event, RoomEvent, and StateEvent.

This commit is contained in:
Jimmy Cuadra 2016-06-21 03:33:29 -07:00
parent f6601c89ba
commit f662f086b3
25 changed files with 110 additions and 196 deletions

View File

@ -1,17 +1,10 @@
//! Types for the *m.call.answer* event.
use events::EventType;
use events::RoomEvent;
use super::SessionDescription;
/// This event is sent by the callee when they wish to answer the call.
#[derive(Debug, Deserialize, Serialize)]
pub struct AnswerEvent {
pub content: AnswerEventContent,
pub event_id: String,
pub event_type: EventType,
pub room_id: String,
pub user_id: String,
}
pub type AnswerEvent = RoomEvent<AnswerEventContent>;
/// The payload of an `AnswerEvent`.
#[derive(Debug, Deserialize, Serialize)]

View File

@ -1,17 +1,10 @@
//! Types for the *m.call.candidates* event.
use events::EventType;
use events::RoomEvent;
/// This event is sent by callers after sending an invite and by the callee after answering.
/// Its purpose is to give the other party additional ICE candidates to try using to communicate.
#[derive(Debug, Deserialize, Serialize)]
pub struct CandidatesEvent {
pub content: CandidatesEventContent,
pub event_id: String,
pub event_type: EventType,
pub room_id: String,
pub user_id: String,
}
pub type CandidatesEvent = RoomEvent<CandidatesEventContent>;
/// The payload of a `CandidatesEvent`.
#[derive(Debug, Deserialize, Serialize)]

View File

@ -1,17 +1,10 @@
//! Types for the *m.call.hangup* event.
use events::EventType;
use events::RoomEvent;
/// Sent by either party to signal their termination of the call. This can be sent either once the
/// call has has been established or before to abort the call.
#[derive(Debug, Deserialize, Serialize)]
pub struct HangupEvent {
pub content: HangupEventContent,
pub event_id: String,
pub event_type: EventType,
pub room_id: String,
pub user_id: String,
}
pub type HangupEvent = RoomEvent<HangupEventContent>;
/// The payload of a `HangupEvent`.
#[derive(Debug, Deserialize, Serialize)]

View File

@ -1,17 +1,10 @@
//! Types for the *m.call.invite* event.
use events::EventType;
use events::RoomEvent;
use super::SessionDescription;
/// This event is sent by the caller when they wish to establish a call.
#[derive(Debug, Deserialize, Serialize)]
pub struct InviteEvent {
pub content: InviteEventContent,
pub event_id: String,
pub event_type: EventType,
pub room_id: String,
pub user_id: String,
}
pub type InviteEvent = RoomEvent<InviteEventContent>;
/// The payload of an `InviteEvent`.
#[derive(Debug, Deserialize, Serialize)]

View File

@ -2,18 +2,16 @@
use std::fmt::{Display, Formatter, Error as FmtError};
use serde::{Deserialize, Serialize};
pub mod call;
pub mod presence;
pub mod receipt;
pub mod room;
pub mod stripped;
pub mod tag;
pub mod typing;
use self::room::avatar::AvatarEventContent;
use self::room::canonical_alias::CanonicalAliasEventContent;
use self::room::join_rules::JoinRulesEventContent;
use self::room::name::NameEventContent;
/// The type of an event.
#[derive(Debug, Deserialize, Serialize)]
pub enum EventType {
@ -41,27 +39,39 @@ pub enum EventType {
Typing,
}
/// A stripped-down version of a state event that is included along with some other events.
/// A basic event.
#[derive(Debug, Deserialize, Serialize)]
pub enum StrippedState {
RoomAvatar(StrippedRoomAvatar),
RoomCanonicalAlias(StrippedRoomCanonicalAlias),
RoomJoinRules(StrippedRoomJoinRules),
RoomName(StrippedRoomName),
}
/// The general form of a `StrippedState`.
#[derive(Debug, Deserialize, Serialize)]
pub struct StrippedStateContent<T> {
pub struct Event<T> where T: Deserialize + Serialize {
pub content: T,
#[serde(rename="type")]
pub event_type: EventType,
pub state_key: String,
}
pub type StrippedRoomAvatar = StrippedStateContent<AvatarEventContent>;
pub type StrippedRoomCanonicalAlias = StrippedStateContent<CanonicalAliasEventContent>;
pub type StrippedRoomJoinRules = StrippedStateContent<JoinRulesEventContent>;
pub type StrippedRoomName = StrippedStateContent<NameEventContent>;
/// An event within the context of a room.
#[derive(Debug, Deserialize, Serialize)]
pub struct RoomEvent<T> where T: Deserialize + Serialize {
pub content: T,
pub event_id: String,
#[serde(rename="type")]
pub event_type: EventType,
pub room_id: String,
#[serde(rename="sender")]
pub user_id: String,
}
/// An event that describes persistent state about a room.
#[derive(Debug, Deserialize, Serialize)]
pub struct StateEvent<T> where T: Deserialize + Serialize {
pub content: T,
pub event_id: String,
#[serde(rename="type")]
pub event_type: EventType,
pub prev_content: Option<T>,
pub room_id: String,
pub state_key: String,
#[serde(rename="sender")]
pub user_id: String,
}
impl Display for EventType {
fn fmt(&self, f: &mut Formatter) -> Result<(), FmtError> {

View File

@ -7,6 +7,7 @@ use events::EventType;
pub struct PresenceEvent {
pub content: PresenceEventContent,
pub event_id: String,
#[serde(rename="type")]
pub event_type: EventType,
}

View File

@ -8,6 +8,7 @@ use events::EventType;
#[derive(Debug, Deserialize, Serialize)]
pub struct ReceiptEvent {
pub content: ReceiptEventContent,
#[serde(rename="type")]
pub event_type: EventType,
pub room_id: String,
}

View File

@ -1,19 +1,9 @@
//! Types for the *m.room.aliases* event.
use events::EventType;
use events::StateEvent;
/// Informs the room about what room aliases it has been given.
#[derive(Debug, Deserialize, Serialize)]
pub struct AliasesEvent {
pub content: AliasesEventContent,
pub event_id: String,
pub event_type: EventType,
pub prev_content: Option<AliasesEventContent>,
pub room_id: String,
/// The homeserver domain which owns these room aliases.
pub state_key: String,
pub user_id: String,
}
pub type AliasesEvent = StateEvent<AliasesEventContent>;
/// The payload of an `AliasesEvent`.
#[derive(Debug, Deserialize, Serialize)]

View File

@ -1,21 +1,12 @@
//! Types for the *m.room.avatar* event.
use events::EventType;
use events::StateEvent;
use super::ImageInfo;
/// A picture that is associated with the room.
///
/// This can be displayed alongside the room information.
#[derive(Debug, Deserialize, Serialize)]
pub struct AvatarEvent {
pub content: AvatarEventContent,
pub event_id: String,
pub event_type: EventType,
pub prev_content: Option<AvatarEventContent>,
pub room_id: String,
pub state_key: String,
pub user_id: String,
}
pub type AvatarEvent = StateEvent<AvatarEventContent>;
/// The payload of an `AvatarEvent`.
#[derive(Debug, Deserialize, Serialize)]

View File

@ -1,18 +1,9 @@
//! Types for the *m.room.canonical_alias* event.
use events::EventType;
use events::StateEvent;
/// Informs the room as to which alias is the canonical one.
#[derive(Debug, Deserialize, Serialize)]
pub struct CanonicalAliasEvent {
pub content: CanonicalAliasEventContent,
pub event_id: String,
pub event_type: EventType,
pub prev_content: Option<CanonicalAliasEventContent>,
pub room_id: String,
pub state_key: String,
pub user_id: String,
}
pub type CanonicalAliasEvent = StateEvent<CanonicalAliasEventContent>;
/// The payload of a `CanonicalAliasEvent`.
#[derive(Debug, Deserialize, Serialize)]

View File

@ -1,23 +1,16 @@
//! Types for the *m.room.create* event.
use events::EventType;
use events::StateEvent;
/// This is the first event in a room and cannot be changed. It acts as the root of all other
/// events.
#[derive(Debug, Deserialize, Serialize)]
pub struct CreateEvent {
pub content: CreateEventContent,
pub event_id: String,
pub event_type: EventType,
pub prev_content: Option<CreateEventContent>,
pub room_id: String,
pub state_key: String,
pub user_id: String,
}
pub type CreateEvent = StateEvent<CreateEventContent>;
/// The payload of a `CreateEvent`.
#[derive(Debug, Deserialize, Serialize)]
pub struct CreateEventContent {
/// The `user_id` of the room creator. This is set by the homeserver.
pub creator: String,
/// Whether or not this room's data should be transferred to other homeservers.
pub federate: bool,
}

View File

@ -1,21 +1,12 @@
//! Types for the *m.room.guest_access* event.
use events::EventType;
use events::StateEvent;
/// Controls whether guest users are allowed to join rooms.
///
/// This event controls whether guest users are allowed to join rooms. If this event is absent,
/// servers should act as if it is present and has the value `GuestAccess::Forbidden`.
#[derive(Debug, Deserialize, Serialize)]
pub struct GuestAccessEvent {
pub content: GuestAccessEventContent,
pub event_id: String,
pub event_type: EventType,
pub prev_content: Option<GuestAccessEventContent>,
pub room_id: String,
pub state_key: String,
pub user_id: String,
}
pub type GuestAccessEvent = StateEvent<GuestAccessEventContent>;
/// The payload of a `GuestAccessEvent`.
#[derive(Debug, Deserialize, Serialize)]

View File

@ -1,19 +1,10 @@
//! Types for the *m.room.history_visibility* event.
use events::EventType;
use events::StateEvent;
/// This event controls whether a member of a room can see the events that happened in a room from
/// before they joined.
#[derive(Debug, Deserialize, Serialize)]
pub struct HistoryVisibilityEvent {
pub content: HistoryVisibilityEventContent,
pub event_id: String,
pub event_type: EventType,
pub prev_content: Option<HistoryVisibilityEventContent>,
pub room_id: String,
pub state_key: String,
pub user_id: String,
}
pub type HistoryVisibilityEvent = StateEvent<HistoryVisibilityEventContent>;
/// The payload of a `HistoryVisibilityEvent`.
#[derive(Debug, Deserialize, Serialize)]

View File

@ -1,18 +1,9 @@
//! Types for the *m.room.join_rules* event.
use events::EventType;
use events::StateEvent;
/// Describes how users are allowed to join the room.
#[derive(Debug, Deserialize, Serialize)]
pub struct JoinRulesEvent {
pub content: JoinRulesEventContent,
pub event_id: String,
pub event_type: EventType,
pub prev_content: Option<JoinRulesEventContent>,
pub room_id: String,
pub state_key: String,
pub user_id: String,
}
pub type JoinRulesEvent = StateEvent<JoinRulesEventContent>;
/// The payload of a `JoinRulesEvent`.
#[derive(Debug, Deserialize, Serialize)]

View File

@ -1,6 +1,7 @@
//! Types for the *m.room.member* event.
use events::{EventType, StrippedState};
use events::EventType;
use events::stripped::StrippedState;
/// The current membership state of a user in the room.
///
@ -19,11 +20,13 @@ use events::{EventType, StrippedState};
pub struct MemberEvent {
pub content: MemberEventContent,
pub event_id: String,
#[serde(rename="type")]
pub event_type: EventType,
pub invite_room_state: Option<Vec<StrippedState>>,
pub prev_content: Option<MemberEventContent>,
pub room_id: String,
pub state_key: String,
#[serde(rename="sender")]
pub user_id: String,
}

View File

@ -1,17 +1,10 @@
//! Types for the *m.room.message* event.
use events::EventType;
use events::RoomEvent;
use super::ImageInfo;
/// A message sent to a room.
#[derive(Debug, Deserialize, Serialize)]
pub struct MessageEvent {
pub content: MessageEventContent,
pub event_id: String,
pub event_type: EventType,
pub room_id: String,
pub user_id: String,
}
pub type MessageEvent = RoomEvent<MessageEventContent>;
/// The message type of message event, e.g. `m.image` or `m.text`.
#[derive(Debug, Deserialize, Serialize)]

View File

@ -1,18 +1,9 @@
//! Types for the *m.room.name* event.
use events::EventType;
use events::StateEvent;
/// A human-friendly room name designed to be displayed to the end-user.
#[derive(Debug, Deserialize, Serialize)]
pub struct NameEvent {
pub content: NameEventContent,
pub event_id: String,
pub event_type: EventType,
pub prev_content: Option<NameEventContent>,
pub room_id: String,
pub state_key: String,
pub user_id: String,
}
pub type NameEvent = StateEvent<NameEventContent>;
/// The payload of a `NameEvent`.
#[derive(Debug, Deserialize, Serialize)]

View File

@ -2,19 +2,10 @@
use std::collections::HashMap;
use events::EventType;
use events::StateEvent;
/// Defines the power levels (privileges) of users in the room.
#[derive(Debug, Deserialize, Serialize)]
pub struct PowerLevelsEvent {
pub content: PowerLevelsEventContent,
pub event_id: String,
pub event_type: EventType,
pub prev_content: Option<PowerLevelsEventContent>,
pub room_id: String,
pub state_key: String,
pub user_id: String,
}
pub type PowerLevelsEvent = StateEvent<PowerLevelsEventContent>;
/// The payload of a `PowerLevelsEvent`.
#[derive(Debug, Deserialize, Serialize)]

View File

@ -7,10 +7,12 @@ use events::EventType;
pub struct RedactionEvent {
pub content: RedactionEventContent,
pub event_id: String,
#[serde(rename="type")]
pub event_type: EventType,
/// The ID of the event that was redacted.
pub redacts: String,
pub room_id: String,
#[serde(rename="sender")]
pub user_id: String,
}

View File

@ -1,22 +1,13 @@
//! Types for the *m.room.third_party_invite* event.
use events::EventType;
use events::StateEvent;
/// An invitation to a room issued to a third party identifier, rather than a matrix user ID.
///
/// Acts as an *m.room.member* invite event, where there isn't a target user_id to invite. This
/// event contains a token and a public key whose private key must be used to sign the token. Any
/// user who can present that signature may use this invitation to join the target room.
#[derive(Debug, Deserialize, Serialize)]
pub struct ThirdPartyInviteEvent {
pub content: ThirdPartyInviteEventContent,
pub event_id: String,
pub event_type: EventType,
pub prev_content: Option<ThirdPartyInviteEventContent>,
pub room_id: String,
pub state_key: String,
pub user_id: String,
}
pub type ThirdPartyInviteEvent = StateEvent<ThirdPartyInviteEventContent>;
/// The payload of a `ThirdPartyInviteEvent`.
#[derive(Debug, Deserialize, Serialize)]

View File

@ -1,18 +1,9 @@
//! Types for the *m.room.topic* event.
use events::EventType;
use events::StateEvent;
/// A topic is a short message detailing what is currently being discussed in the room.
#[derive(Debug, Deserialize, Serialize)]
pub struct TopicEvent {
pub content: TopicEventContent,
pub event_id: String,
pub event_type: EventType,
pub prev_content: Option<TopicEventContent>,
pub room_id: String,
pub state_key: String,
pub user_id: String,
}
pub type TopicEvent = StateEvent<TopicEventContent>;
/// The payload of a `TopicEvent`.
#[derive(Debug, Deserialize, Serialize)]

32
src/events/stripped.rs Normal file
View File

@ -0,0 +1,32 @@
//! Stripped-down versions of certain state events.
use serde::{Deserialize, Serialize};
use events::EventType;
use events::room::avatar::AvatarEventContent;
use events::room::canonical_alias::CanonicalAliasEventContent;
use events::room::join_rules::JoinRulesEventContent;
use events::room::name::NameEventContent;
/// A stripped-down version of a state event that is included along with some other events.
#[derive(Debug, Deserialize, Serialize)]
pub enum StrippedState {
RoomAvatar(StrippedRoomAvatar),
RoomCanonicalAlias(StrippedRoomCanonicalAlias),
RoomJoinRules(StrippedRoomJoinRules),
RoomName(StrippedRoomName),
}
/// The general form of a `StrippedState`.
#[derive(Debug, Deserialize, Serialize)]
pub struct StrippedStateContent<T> where T: Deserialize + Serialize {
pub content: T,
#[serde(rename="type")]
pub event_type: EventType,
pub state_key: String,
}
pub type StrippedRoomAvatar = StrippedStateContent<AvatarEventContent>;
pub type StrippedRoomCanonicalAlias = StrippedStateContent<CanonicalAliasEventContent>;
pub type StrippedRoomJoinRules = StrippedStateContent<JoinRulesEventContent>;
pub type StrippedRoomName = StrippedStateContent<NameEventContent>;

View File

@ -2,15 +2,10 @@
use std::collections::HashMap;
use events::EventType;
use events::Event;
/// Informs the client of tags on a room.
#[derive(Debug, Deserialize, Serialize)]
pub struct TagEvent {
/// The payload.
pub content: TagEventContent,
pub event_type: EventType,
}
pub type TagEvent = Event<TagEventContent>;
/// The payload of a `TagEvent`.
#[derive(Debug, Deserialize, Serialize)]

View File

@ -7,6 +7,7 @@ use events::EventType;
pub struct TypingEvent {
/// The payload.
pub content: TypingEventContent,
#[serde(rename="type")]
pub event_type: EventType,
/// The ID of the room associated with this event.
pub room_id: String,

View File

@ -5,4 +5,6 @@
#![feature(custom_derive, plugin)]
#![plugin(serde_macros)]
extern crate serde;
pub mod events;