From 424c0f335d10188f3960155190f5f80dffda6e03 Mon Sep 17 00:00:00 2001 From: Jimmy Cuadra Date: Sun, 29 Nov 2015 02:58:57 -0800 Subject: [PATCH] ruma-events --- .gitignore | 2 + Cargo.toml | 11 +++ LICENSE | 20 +++++ README.md | 11 +++ src/call.rs | 201 ++++++++++++++++++++++++++++++++++++++++++++ src/core.rs | 27 ++++++ src/lib.rs | 9 ++ src/presence.rs | 1 + src/receipt.rs | 1 + src/room/message.rs | 1 + src/room/mod.rs | 3 + src/typing.rs | 27 ++++++ 12 files changed, 314 insertions(+) create mode 100644 .gitignore create mode 100644 Cargo.toml create mode 100644 LICENSE create mode 100644 README.md create mode 100644 src/call.rs create mode 100644 src/core.rs create mode 100644 src/lib.rs create mode 100644 src/presence.rs create mode 100644 src/receipt.rs create mode 100644 src/room/message.rs create mode 100644 src/room/mod.rs create mode 100644 src/typing.rs diff --git a/.gitignore b/.gitignore new file mode 100644 index 00000000..fa8d85ac --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +Cargo.lock +target diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 00000000..54314208 --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,11 @@ +[package] +name = "ruma-events" +version = "0.1.0" +authors = ["Jimmy Cuadra "] +description = "Serializable types for the events in the Matrix specification." +license = "MIT" +readme = "README.md" +keywords = ["matrix", "matrix.org", "chat", "messaging", "ruma"] +repository = "https://github.com/ruma/ruma-events" +homepage = "https://github.com/ruma/ruma-events" +documentation = "http://ruma.github.io/ruma-events/ruma-events" diff --git a/LICENSE b/LICENSE new file mode 100644 index 00000000..6225a6dc --- /dev/null +++ b/LICENSE @@ -0,0 +1,20 @@ +Copyright (c) 2015 Jimmy Cuadra + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. + diff --git a/README.md b/README.md new file mode 100644 index 00000000..07a96e50 --- /dev/null +++ b/README.md @@ -0,0 +1,11 @@ +# ruma-events + +ruma-events contains serializable types for the events in the [Matrix](https://matrix.org/) specification that can be shared by client and server code. + +## Status + +This project is currently experimental and is very likely to change drastically. + +## License + +[MIT](http://opensource.org/licenses/MIT) diff --git a/src/call.rs b/src/call.rs new file mode 100644 index 00000000..6ca6028b --- /dev/null +++ b/src/call.rs @@ -0,0 +1,201 @@ +//! Events within the *m.call* namespace. + +use core::{Event, RoomEvent}; + +/// This event is sent by the callee when they wish to answer the call. +pub struct Answer<'a> { + content: AnswerContent<'a>, + event_id: String, + room_id: String, + user_id: String, +} + +impl<'a> Event<'a, AnswerContent<'a>> for Answer<'a> { + fn content(&'a self) -> &'a AnswerContent { + &self.content + } + + fn event_type(&self) -> &'static str { + "m.call.answer" + } +} + +impl<'a> RoomEvent<'a, AnswerContent<'a>> for Answer<'a> { + fn event_id(&'a self) -> &'a str { + &self.event_id + } + + fn room_id(&'a self) -> &'a str { + &self.room_id + } + + fn user_id(&'a self) -> &'a str { + &self.user_id + } +} + +/// The payload of an `Answer` event. +pub struct AnswerContent<'a> { + /// The VoIP session description. + answer: SessionDescription<'a>, + /// The ID of the call this event relates to. + call_id: String, + /// The version of the VoIP specification this messages adheres to. + version: u64, +} + +/// A VoIP session description. +pub struct SessionDescription<'a> { + /// The type of session description. + session_type: SessionDescriptionType, + /// The SDP text of the session description. + sdp: &'a str, +} + +/// The types of VoIP session descriptions. +pub enum SessionDescriptionType { + /// An answer. + Answer, + /// An offer. + Offer, +} + +/// 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. +pub struct Candidates<'a> { + content: CandidatesContent<'a>, + event_id: &'a str, + room_id: &'a str, + user_id: &'a str, +} + +impl<'a> Event<'a, CandidatesContent<'a>> for Candidates<'a> { + fn content(&'a self) -> &'a CandidatesContent { + &self.content + } + + fn event_type(&self) -> &'static str { + "m.call.candidates" + } +} + +impl<'a> RoomEvent<'a, CandidatesContent<'a>> for Candidates<'a> { + fn event_id(&'a self) -> &'a str { + &self.event_id + } + + fn room_id(&'a self) -> &'a str { + &self.room_id + } + + fn user_id(&'a self) -> &'a str { + &self.user_id + } +} + +/// The payload of a `Candidates` event. +pub struct CandidatesContent<'a> { + /// The ID of the call this event relates to. + call_id: &'a str, + /// A list of candidates. + candidates: &'a[Candidate<'a>], + /// The version of the VoIP specification this messages adheres to. + version: u64, +} + +/// An ICE (Interactive Connectivity Establishment) candidate. +pub struct Candidate<'a> { + /// The SDP "a" line of the candidate. + candidate: &'a str, + /// The SDP media type this candidate is intended for. + sdp_mid: &'a str, + /// The index of the SDP "m" line this candidate is intended for. + sdp_m_line_index: u64, +} + +/// 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. +pub struct Hangup<'a> { + content: HangupContent<'a>, + event_id: &'a str, + room_id: &'a str, + user_id: &'a str, +} + +impl<'a> Event<'a, HangupContent<'a>> for Hangup<'a> { + fn content(&'a self) -> &'a HangupContent { + &self.content + } + + fn event_type(&self) -> &'static str { + "m.call.hangup" + } +} + +impl<'a> RoomEvent<'a, HangupContent<'a>> for Hangup<'a> { + fn event_id(&'a self) -> &'a str { + &self.event_id + } + + fn room_id(&'a self) -> &'a str { + &self.room_id + } + + fn user_id(&'a self) -> &'a str { + &self.user_id + } +} + +/// The payload of a `Hangup` event. +pub struct HangupContent<'a> { + /// The ID of the call this event relates to. + call_id: &'a str, + /// The version of the VoIP specification this messages adheres to. + version: u64, +} + +/// This event is sent by the caller when they wish to establish a call. +pub struct Invite<'a> { + content: InviteContent<'a>, + event_id: &'a str, + room_id: &'a str, + user_id: &'a str, +} + +impl<'a> Event<'a, InviteContent<'a>> for Invite<'a> { + fn content(&'a self) -> &'a InviteContent { + &self.content + } + + fn event_type(&self) -> &'static str { + "m.call.invite" + } +} + +impl<'a> RoomEvent<'a, InviteContent<'a>> for Invite<'a> { + fn event_id(&'a self) -> &'a str { + &self.event_id + } + + fn room_id(&'a self) -> &'a str { + &self.room_id + } + + fn user_id(&'a self) -> &'a str { + &self.user_id + } +} + +/// The payload of an `Invite` event. +pub struct InviteContent<'a> { + /// A unique identifer for the call. + call_id: &'a str, + /// The time in milliseconds that the invite is valid for. Once the invite age exceeds this + /// value, clients should discard it. They should also no longer show the call as awaiting an + /// answer in the UI. + lifetime: u64, + /// The session description object. + offer: SessionDescription<'a>, + /// The version of the VoIP specification this messages adheres to. + version: u64, +} diff --git a/src/core.rs b/src/core.rs new file mode 100644 index 00000000..0dba2add --- /dev/null +++ b/src/core.rs @@ -0,0 +1,27 @@ +//! Traits for types of events that share common fields. + +/// The basic set of fields all events must have. +pub trait Event<'a, T> { + /// The primary event payload. + fn content(&'a self) -> &'a T; + /// The type of event, e.g. *com.example.subdomain.event.type*. + fn event_type(&self) -> &'static str; +} + +/// An event emitted within the context of a room. +pub trait RoomEvent<'a, T>: Event<'a, T> { + /// The globally unique event identifier. + fn event_id(&'a self) -> &'a str; + /// The ID of the room associated with this event. + fn room_id(&'a self) -> &'a str; + /// The fully-qualified ID of the user who sent the event. + fn user_id(&'a self) -> &'a str; +} + +/// An event that represents some aspect of a room's state. +pub trait StateEvent<'a, T>: RoomEvent<'a, T> { + /// Previous content for this aspect of room state. + fn prev_content(&self) -> Option<&'a T>; + /// A unique key which defines the overwriting semantics for this aspect of room state. + fn state_key() -> &'a str; +} diff --git a/src/lib.rs b/src/lib.rs new file mode 100644 index 00000000..73816ee6 --- /dev/null +++ b/src/lib.rs @@ -0,0 +1,9 @@ +//! Crate ruma_events contains serializable types for the events in the [Matrix](https://matrix.org) +//! specification that can be shared by client and server code. + +pub mod call; +pub mod core; +pub mod presence; +pub mod receipt; +pub mod room; +pub mod typing; diff --git a/src/presence.rs b/src/presence.rs new file mode 100644 index 00000000..30b869db --- /dev/null +++ b/src/presence.rs @@ -0,0 +1 @@ +//! Events within the *m.presence* namespace. diff --git a/src/receipt.rs b/src/receipt.rs new file mode 100644 index 00000000..f7349a54 --- /dev/null +++ b/src/receipt.rs @@ -0,0 +1 @@ +//! Events within the *m.receipt* namespace. diff --git a/src/room/message.rs b/src/room/message.rs new file mode 100644 index 00000000..d5fdfe47 --- /dev/null +++ b/src/room/message.rs @@ -0,0 +1 @@ +//! Events within the *m.room.message* namespace. diff --git a/src/room/mod.rs b/src/room/mod.rs new file mode 100644 index 00000000..1fa07cf3 --- /dev/null +++ b/src/room/mod.rs @@ -0,0 +1,3 @@ +//! Events within the *m.room* namespace. + +pub mod message; diff --git a/src/typing.rs b/src/typing.rs new file mode 100644 index 00000000..53a2c9fe --- /dev/null +++ b/src/typing.rs @@ -0,0 +1,27 @@ +//! Events within the *m.typing* namespace. + +use core::Event; + +/// Informs the client of the list of users currently typing. +pub struct Typing<'a> { + /// The payload. + content: TypingContent<'a>, + /// The ID of the room associated with this event. + room_id: &'a str, +} + +impl<'a> Event<'a, TypingContent<'a>> for Typing<'a> { + fn content(&'a self) -> &'a TypingContent<'a> { + &self.content + } + + fn event_type(&self) -> &'static str { + "m.typing" + } +} + +/// The payload of a `Typing` event. +pub struct TypingContent<'a> { + /// The list of user IDs typing in this room, if any. + user_ids: &'a[&'a str], +}