use reqwest::Url;
use salvo::prelude::*;
use serde::{Deserialize, Serialize};
use crate::sending::{SendRequest, SendResult};
use crate::{EventId, OwnedEventId, OwnedRoomId, OwnedServerName, OwnedTransactionId, RawJsonValue, UnixMillis};
#[derive(ToSchema, Serialize, Debug)]
pub struct EventByTimestampResBody {
pub event_id: OwnedEventId,
pub origin_server_ts: UnixMillis,
}
impl EventByTimestampResBody {
pub fn new(event_id: OwnedEventId, origin_server_ts: UnixMillis) -> Self {
Self {
event_id,
origin_server_ts,
}
}
}
pub fn get_events_request(
origin: &str,
event_id: &EventId,
include_unredacted_content: Option<bool>,
) -> SendResult<SendRequest> {
let url = Url::parse(&format!("{origin}/_matrix/federation/v1/event/{event_id}"))?;
Ok(crate::sending::get(url))
}
#[derive(ToSchema, Serialize, Deserialize, Debug)]
pub struct EventResBody {
pub origin: OwnedServerName,
pub origin_server_ts: UnixMillis,
#[serde(rename = "pdus", with = "crate::serde::single_element_seq")]
#[salvo(schema(value_type = Object, additional_properties = true))]
pub pdu: Box<RawJsonValue>,
}
impl EventResBody {
pub fn new(origin: OwnedServerName, origin_server_ts: UnixMillis, pdu: Box<RawJsonValue>) -> Self {
Self {
origin,
origin_server_ts,
pdu,
}
}
}
#[derive(ToSchema, Deserialize, Debug)]
pub struct MissingEventReqBody {
#[serde(default = "default_limit", skip_serializing_if = "is_default_limit")]
pub limit: usize,
#[serde(default, skip_serializing_if = "crate::serde::is_default")]
pub min_depth: u64,
pub earliest_events: Vec<OwnedEventId>,
pub latest_events: Vec<OwnedEventId>,
}
#[derive(ToSchema, Serialize, Debug)]
pub struct MissingEventResBody {
#[salvo(schema(value_type = Vec<Object>))]
pub events: Vec<Box<RawJsonValue>>,
}
impl MissingEventResBody {
pub fn new(events: Vec<Box<RawJsonValue>>) -> Self {
Self { events }
}
}
fn default_limit() -> usize {
10
}
fn is_default_limit(val: &usize) -> bool {
*val == default_limit()
}
pub fn room_state_ids_request(origin: &str, args: RoomStateAtEventReqArgs) -> SendResult<SendRequest> {
let mut url = Url::parse(&format!("{origin}/_matrix/federation/v1/state_ids/{}", args.room_id))?;
url.query_pairs_mut().append_pair("event_id", args.event_id.as_str());
Ok(crate::sending::get(url))
}
#[derive(ToParameters, Deserialize, Debug)]
pub struct RoomStateAtEventReqArgs {
#[salvo(parameter(parameter_in = Path))]
pub room_id: OwnedRoomId,
#[salvo(parameter(parameter_in = Query))]
pub event_id: OwnedEventId,
}
#[derive(ToSchema, Deserialize, Serialize, Debug)]
pub struct RoomStateIdsResBody {
pub auth_chain_ids: Vec<OwnedEventId>,
pub pdu_ids: Vec<OwnedEventId>,
}
impl RoomStateIdsResBody {
pub fn new(auth_chain_ids: Vec<OwnedEventId>, pdu_ids: Vec<OwnedEventId>) -> Self {
Self {
auth_chain_ids,
pdu_ids,
}
}
}
#[derive(ToParameters, Deserialize, Debug)]
pub struct RoomStateReqArgs {
#[salvo(parameter(parameter_in = Path))]
pub room_id: OwnedRoomId,
#[salvo(parameter(parameter_in = Query))]
pub event_id: OwnedEventId,
}
#[derive(ToSchema, Serialize, Debug)]
pub struct RoomStateResBody {
#[salvo(schema(value_type = Object))]
pub auth_chain: Vec<Box<RawJsonValue>>,
#[salvo(schema(value_type = Vec<Object>))]
pub pdus: Vec<Box<RawJsonValue>>,
}
#[derive(ToSchema, Deserialize, Debug)]
pub struct PingReqBody {
#[serde(skip_serializing_if = "Option::is_none")]
pub transaction_id: Option<OwnedTransactionId>,
}