pub mod v3 {
use std::time::Duration;
use ruma_common::{
api::{request, response, Metadata},
metadata, OwnedRoomId, OwnedUserId,
};
use serde::{de::Error, Deserialize, Deserializer, Serialize};
const METADATA: Metadata = metadata! {
method: PUT,
authentication: AccessToken,
rate_limited: true,
history: {
1.0 => "/_matrix/client/r0/rooms/:room_id/typing/:user_id",
1.1 => "/_matrix/client/v3/rooms/:room_id/typing/:user_id",
}
};
#[request(error = crate::Error)]
pub struct Request {
#[ruma_api(path)]
pub room_id: OwnedRoomId,
#[ruma_api(path)]
pub user_id: OwnedUserId,
#[serde(flatten)]
pub state: Typing,
}
#[response(error = crate::Error)]
#[derive(Default)]
pub struct Response {}
impl Request {
pub fn new(user_id: OwnedUserId, room_id: OwnedRoomId, state: Typing) -> Self {
Self { user_id, room_id, state }
}
}
impl Response {
pub fn new() -> Self {
Self {}
}
}
#[derive(Clone, Copy, Debug, Serialize)]
#[serde(into = "TypingInner")]
#[allow(clippy::exhaustive_enums)]
pub enum Typing {
No,
Yes(Duration),
}
#[derive(Deserialize, Serialize)]
struct TypingInner {
typing: bool,
#[serde(
with = "ruma_common::serde::duration::opt_ms",
default,
skip_serializing_if = "Option::is_none"
)]
timeout: Option<Duration>,
}
impl From<Typing> for TypingInner {
fn from(typing: Typing) -> Self {
match typing {
Typing::No => Self { typing: false, timeout: None },
Typing::Yes(time) => Self { typing: true, timeout: Some(time) },
}
}
}
impl<'de> Deserialize<'de> for Typing {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: Deserializer<'de>,
{
let inner = TypingInner::deserialize(deserializer)?;
match (inner.typing, inner.timeout) {
(false, _) => Ok(Self::No),
(true, Some(time)) => Ok(Self::Yes(time)),
_ => Err(D::Error::missing_field("timeout")),
}
}
}
}