federation-api: Improve create_invite endpoint

This commit is contained in:
Timo Kösters 2021-04-12 11:01:34 +02:00 committed by GitHub
parent eae0b58163
commit 63421dfca6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 16 additions and 98 deletions

View File

@ -1,85 +1,4 @@
//! Endpoint for inviting a remote user to a room //! Endpoint for inviting a remote user to a room
use std::time::SystemTime;
use ruma_events::{room::member::MemberEventContent, EventType};
use ruma_identifiers::{ServerName, UserId};
use serde::{Deserialize, Serialize};
use serde_json::Value as JsonValue;
pub mod v1; pub mod v1;
pub mod v2; pub mod v2;
/// A simplified event that helps the server identify a room.
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct StrippedState {
/// The `content` for the event.
pub content: JsonValue,
/// The `state_key` for the event.
pub state_key: String,
/// The `type` for the event.
#[serde(rename = "type")]
pub kind: EventType,
/// The `sender` for the event.
pub sender: UserId,
}
/// The invite event sent as a response.
#[derive(Clone, Debug, Serialize, Deserialize)]
#[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)]
pub struct InviteEvent {
/// The matrix ID of the user who sent the original `m.room.third_party_invite`.
pub sender: UserId,
/// The name of the inviting homeserver.
pub origin: Box<ServerName>,
/// A timestamp added by the inviting homeserver.
#[serde(with = "ruma_serde::time::ms_since_unix_epoch")]
pub origin_server_ts: SystemTime,
/// The event type (should always be `m.room.member`).
#[serde(rename = "type")]
pub kind: EventType,
/// The user ID of the invited member.
pub state_key: UserId,
/// The content of the event. Must include a `membership` of invite.
pub content: MemberEventContent,
}
/// Initial set of fields for `Response`.
pub struct InviteEventInit {
/// The matrix ID of the user who sent the original `m.room.third_party_invite`.
pub sender: UserId,
/// The name of the inviting homeserver.
pub origin: Box<ServerName>,
/// A timestamp added by the inviting homeserver.
pub origin_server_ts: SystemTime,
/// The user ID of the invited member.
pub state_key: UserId,
/// The content of the event. Must include a `membership` of invite.
pub content: MemberEventContent,
}
impl From<InviteEventInit> for InviteEvent {
/// Creates a new `Response` with the given inital values
fn from(init: InviteEventInit) -> Self {
InviteEvent {
sender: init.sender,
origin: init.origin,
origin_server_ts: init.origin_server_ts,
kind: EventType::RoomMember,
state_key: init.state_key,
content: init.content,
}
}
}

View File

@ -3,12 +3,11 @@
use std::time::SystemTime; use std::time::SystemTime;
use ruma_api::ruma_api; use ruma_api::ruma_api;
use ruma_events::{room::member::MemberEventContent, EventType}; use ruma_events::{pdu::Pdu, room::member::MemberEventContent, AnyStrippedStateEvent, EventType};
use ruma_identifiers::{EventId, RoomId, ServerName, UserId}; use ruma_identifiers::{EventId, RoomId, ServerName, UserId};
use ruma_serde::Raw;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use super::{InviteEvent, StrippedState};
ruma_api! { ruma_api! {
metadata: { metadata: {
description: "Invites a remote user to a room.", description: "Invites a remote user to a room.",
@ -54,10 +53,10 @@ ruma_api! {
} }
response: { response: {
/// The response invite event /// The signed invite event.
#[ruma_api(body)] #[ruma_api(body)]
#[serde(with = "crate::serde::v1_pdu")] #[serde(with = "crate::serde::v1_pdu")]
pub event: InviteEvent, pub event: Raw<Pdu>,
} }
} }
@ -69,7 +68,7 @@ pub struct UnsignedEventContent {
/// The recommended events to include are the join rules, canonical alias, avatar, and name of /// The recommended events to include are the join rules, canonical alias, avatar, and name of
/// the room. /// the room.
#[serde(skip_serializing_if = "<[_]>::is_empty")] #[serde(skip_serializing_if = "<[_]>::is_empty")]
pub invite_room_state: Vec<StrippedState>, pub invite_room_state: Vec<Raw<AnyStrippedStateEvent>>,
} }
impl UnsignedEventContent { impl UnsignedEventContent {
@ -130,7 +129,7 @@ impl<'a> From<RequestInit<'a>> for Request<'a> {
impl Response { impl Response {
/// Creates a new `Response` with the given invite event. /// Creates a new `Response` with the given invite event.
pub fn new(event: InviteEvent) -> Self { pub fn new(event: Raw<Pdu>) -> Self {
Self { event } Self { event }
} }
} }

View File

@ -1,9 +1,9 @@
//! [PUT /_matrix/federation/v2/invite/{roomId}/{eventId}](https://matrix.org/docs/spec/server_server/r0.1.4#put-matrix-federation-v2-invite-roomid-eventid) //! [PUT /_matrix/federation/v2/invite/{roomId}/{eventId}](https://matrix.org/docs/spec/server_server/r0.1.4#put-matrix-federation-v2-invite-roomid-eventid)
use ruma_api::ruma_api; use ruma_api::ruma_api;
use ruma_events::{pdu::Pdu, AnyStrippedStateEvent};
use ruma_identifiers::{EventId, RoomId, RoomVersionId}; use ruma_identifiers::{EventId, RoomId, RoomVersionId};
use ruma_serde::Raw;
use super::{InviteEvent, StrippedState};
ruma_api! { ruma_api! {
metadata: { metadata: {
@ -27,16 +27,16 @@ ruma_api! {
/// The version of the room where the user is being invited to. /// The version of the room where the user is being invited to.
pub room_version: RoomVersionId, pub room_version: RoomVersionId,
/// An invite event. /// The invite event which needs to be signed.
pub event: InviteEvent, pub event: Raw<Pdu>,
/// An optional list of simplified events to help the receiver of the invite identify the room. /// An optional list of simplified events to help the receiver of the invite identify the room.
pub invite_room_state: StrippedState, pub invite_room_state: Vec<Raw<AnyStrippedStateEvent>>,
} }
response: { response: {
/// An invite event. /// The signed invite event.
pub event: InviteEvent, pub event: Raw<Pdu>,
} }
} }
@ -46,8 +46,8 @@ impl Request {
room_id: RoomId, room_id: RoomId,
event_id: EventId, event_id: EventId,
room_version: RoomVersionId, room_version: RoomVersionId,
event: InviteEvent, event: Raw<Pdu>,
invite_room_state: StrippedState, invite_room_state: Vec<Raw<AnyStrippedStateEvent>>,
) -> Self { ) -> Self {
Self { room_id, event_id, room_version, event, invite_room_state } Self { room_id, event_id, room_version, event, invite_room_state }
} }
@ -55,7 +55,7 @@ impl Request {
impl Response { impl Response {
/// Creates a new `Response` with the given invite event. /// Creates a new `Response` with the given invite event.
pub fn new(event: InviteEvent) -> Self { pub fn new(event: Raw<Pdu>) -> Self {
Self { event } Self { event }
} }
} }