use ruma_common::{
api::ruma_api,
events::{room::member::RoomMemberEventContent, AnyStrippedStateEvent, StateEventType},
serde::Raw,
EventId, MilliSecondsSinceUnixEpoch, RoomId, ServerName, UserId,
};
use serde::{Deserialize, Serialize};
use serde_json::value::RawValue as RawJsonValue;
ruma_api! {
metadata: {
description: "Invites a remote user to a room.",
method: PUT,
name: "create_invite",
stable_path: "/_matrix/federation/v1/invite/:room_id/:event_id",
rate_limited: false,
authentication: ServerSignatures,
added: 1.0,
}
request: {
#[ruma_api(path)]
pub room_id: &'a RoomId,
#[ruma_api(path)]
pub event_id: &'a EventId,
pub sender: &'a UserId,
pub origin: &'a ServerName,
pub origin_server_ts: MilliSecondsSinceUnixEpoch,
#[serde(rename = "type")]
pub kind: StateEventType,
pub state_key: &'a UserId,
pub content: RoomMemberEventContent,
#[serde(default, skip_serializing_if = "UnsignedEventContent::is_empty")]
pub unsigned: UnsignedEventContent,
}
response: {
#[ruma_api(body)]
#[serde(with = "crate::serde::v1_pdu")]
pub event: Box<RawJsonValue>,
}
}
#[derive(Clone, Debug, Default, Serialize, Deserialize)]
#[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)]
pub struct UnsignedEventContent {
#[serde(skip_serializing_if = "<[_]>::is_empty")]
pub invite_room_state: Vec<Raw<AnyStrippedStateEvent>>,
}
impl UnsignedEventContent {
pub fn new() -> Self {
Default::default()
}
pub fn is_empty(&self) -> bool {
self.invite_room_state.is_empty()
}
}
#[derive(Debug)]
#[allow(clippy::exhaustive_structs)]
pub struct RequestInit<'a> {
pub room_id: &'a RoomId,
pub event_id: &'a EventId,
pub sender: &'a UserId,
pub origin: &'a ServerName,
pub origin_server_ts: MilliSecondsSinceUnixEpoch,
pub state_key: &'a UserId,
pub content: RoomMemberEventContent,
pub unsigned: UnsignedEventContent,
}
impl<'a> From<RequestInit<'a>> for Request<'a> {
fn from(init: RequestInit<'a>) -> Self {
Self {
room_id: init.room_id,
event_id: init.event_id,
sender: init.sender,
origin: init.origin,
origin_server_ts: init.origin_server_ts,
kind: StateEventType::RoomMember,
state_key: init.state_key,
content: init.content,
unsigned: init.unsigned,
}
}
}
impl Response {
pub fn new(event: Box<RawJsonValue>) -> Self {
Self { event }
}
}