use std::collections::BTreeMap;
use salvo::prelude::*;
use serde::{Deserialize, Serialize};
use crate::events::room::member::RoomMemberEvent;
use crate::serde::{RawJson, StringEnum};
use crate::{
OwnedMxcUri, OwnedRoomId, OwnedServerName, OwnedServerSigningKeyId, OwnedUserId, PrivOwnedStr, third_party::Medium,
};
#[derive(ToSchema, Deserialize, Serialize, Clone, Debug)]
pub struct ThirdPartySigned {
pub sender: OwnedUserId,
pub mxid: OwnedUserId,
pub token: String,
pub signatures: BTreeMap<OwnedServerName, BTreeMap<OwnedServerSigningKeyId, String>>,
}
impl ThirdPartySigned {
pub fn new(
sender: OwnedUserId,
mxid: OwnedUserId,
token: String,
signatures: BTreeMap<OwnedServerName, BTreeMap<OwnedServerSigningKeyId, String>>,
) -> Self {
Self {
sender,
mxid,
token,
signatures,
}
}
}
#[derive(ToSchema, Deserialize, Serialize, Clone, Debug)]
pub struct InviteThreepid {
pub id_server: String,
pub id_access_token: String,
pub medium: Medium,
pub address: String,
}
#[derive(Debug)]
#[allow(clippy::exhaustive_structs)]
pub struct InviteThreepidInit {
pub id_server: String,
pub id_access_token: String,
pub medium: Medium,
pub address: String,
}
impl From<InviteThreepidInit> for InviteThreepid {
fn from(init: InviteThreepidInit) -> Self {
let InviteThreepidInit {
id_server,
id_access_token,
medium,
address,
} = init;
Self {
id_server,
id_access_token,
medium,
address,
}
}
}
#[derive(ToSchema, Deserialize, Debug)]
pub struct BanUserReqBody {
pub user_id: OwnedUserId,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub reason: Option<String>,
}
#[derive(ToSchema, Deserialize, Debug)]
pub struct UnbanUserReqBody {
pub user_id: OwnedUserId,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub reason: Option<String>,
}
#[derive(ToSchema, Deserialize, Serialize, Clone, Debug)]
#[serde(untagged)]
pub enum InvitationRecipient {
UserId {
user_id: OwnedUserId,
},
ThirdPartyId(InviteThreepid),
}
#[derive(ToSchema, Deserialize, Debug)]
pub struct KickUserReqBody {
pub user_id: OwnedUserId,
#[serde(skip_serializing_if = "Option::is_none")]
pub reason: Option<String>,
}
#[derive(ToSchema, Deserialize, Debug)]
pub struct InviteUserReqBody {
#[serde(flatten)]
pub recipient: InvitationRecipient,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub reason: Option<String>,
}
#[derive(ToSchema, Deserialize, Debug)]
pub struct LeaveRoomReqBody {
#[serde(default, skip_serializing_if = "Option::is_none")]
pub reason: Option<String>,
}
#[derive(ToSchema, Deserialize, Debug)]
pub struct MutualRoomsReqBody {
#[salvo(parameter(parameter_in = Query))]
pub user_id: OwnedUserId,
#[serde(default, skip_serializing_if = "Option::is_none")]
#[salvo(parameter(parameter_in = Query))]
pub batch_token: Option<String>,
}
#[derive(ToSchema, Serialize, Debug)]
pub struct MutualRoomsResBody {
pub joined: Vec<OwnedRoomId>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub next_batch_token: Option<String>,
}
impl MutualRoomsResBody {
pub fn new(joined: Vec<OwnedRoomId>) -> Self {
Self {
joined,
next_batch_token: None,
}
}
pub fn with_token(joined: Vec<OwnedRoomId>, token: String) -> Self {
Self {
joined,
next_batch_token: Some(token),
}
}
}
#[derive(ToSchema, Serialize, Debug)]
pub struct JoinedRoomsResBody {
pub joined_rooms: Vec<OwnedRoomId>,
}
impl JoinedRoomsResBody {
pub fn new(joined_rooms: Vec<OwnedRoomId>) -> Self {
Self { joined_rooms }
}
}
#[derive(ToSchema, Deserialize, Default, Debug)]
pub struct JoinRoomByIdReqBody {
#[serde(default, skip_serializing_if = "Option::is_none")]
pub third_party_signed: Option<ThirdPartySigned>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub reason: Option<String>,
}
#[derive(ToSchema, Default, Deserialize, Debug)]
pub struct JoinRoomByIdOrAliasReqBody {
pub third_party_signed: Option<ThirdPartySigned>,
#[serde(skip_serializing_if = "Option::is_none")]
pub reason: Option<String>,
}
#[derive(ToSchema, Serialize, Debug)]
pub struct JoinRoomResBody {
pub room_id: OwnedRoomId,
}
impl JoinRoomResBody {
pub fn new(room_id: OwnedRoomId) -> Self {
Self { room_id }
}
}
#[derive(ToParameters, Deserialize, Debug)]
pub struct MembersReqArgs {
#[salvo(parameter(parameter_in = Path))]
pub room_id: OwnedRoomId,
#[serde(skip_serializing_if = "Option::is_none")]
#[salvo(parameter(parameter_in = Query))]
pub at: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
#[salvo(parameter(parameter_in = Query))]
pub membership: Option<MembershipEventFilter>,
#[serde(skip_serializing_if = "Option::is_none")]
#[salvo(parameter(parameter_in = Query))]
pub not_membership: Option<MembershipEventFilter>,
}
#[derive(ToSchema, Serialize, Debug)]
pub struct MembersResBody {
#[salvo(schema(value_type = Vec<Object>))]
pub chunk: Vec<RawJson<RoomMemberEvent>>,
}
impl MembersResBody {
pub fn new(chunk: Vec<RawJson<RoomMemberEvent>>) -> Self {
Self { chunk }
}
}
#[doc = include_str!(concat!(env!("CARGO_MANIFEST_DIR"), "/src/doc/string_enum.md"))]
#[derive(ToSchema, Clone, PartialEq, Eq, StringEnum)]
#[palpo_enum(rename_all = "lowercase")]
#[non_exhaustive]
pub enum MembershipEventFilter {
Join,
Invite,
Leave,
Ban,
#[doc(hidden)]
_Custom(PrivOwnedStr),
}
#[derive(ToSchema, Serialize, Debug)]
pub struct JoinedMembersResBody {
pub joined: BTreeMap<OwnedUserId, RoomMember>,
}
impl JoinedMembersResBody {
pub fn new(joined: BTreeMap<OwnedUserId, RoomMember>) -> Self {
Self { joined }
}
}
#[derive(ToSchema, Clone, Debug, Default, Deserialize, Serialize)]
pub struct RoomMember {
#[serde(default)]
pub display_name: Option<String>,
#[serde(
// skip_serializing_if = "Option::is_none",
default,
deserialize_with = "crate::serde::empty_string_as_none"
)]
pub avatar_url: Option<OwnedMxcUri>,
}
impl RoomMember {
pub fn new(display_name: Option<String>, avatar_url: Option<OwnedMxcUri>) -> Self {
Self {
display_name,
avatar_url,
}
}
}