Merge remote-tracking branch 'origin/next' into command-refactor
Fixed conflict with commit 78502aa6b1
This commit is contained in:
commit
4bbff69a24
23 changed files with 340 additions and 186 deletions
|
@ -60,15 +60,16 @@ pub async fn get_register_available_route(
|
|||
body: Ruma<get_username_availability::Request<'_>>,
|
||||
) -> ConduitResult<get_username_availability::Response> {
|
||||
// Validate user id
|
||||
let user_id = UserId::parse_with_server_name(body.username.clone(), db.globals.server_name())
|
||||
.ok()
|
||||
.filter(|user_id| {
|
||||
!user_id.is_historical() && user_id.server_name() == db.globals.server_name()
|
||||
})
|
||||
.ok_or(Error::BadRequest(
|
||||
ErrorKind::InvalidUsername,
|
||||
"Username is invalid.",
|
||||
))?;
|
||||
let user_id =
|
||||
UserId::parse_with_server_name(body.username.to_lowercase(), db.globals.server_name())
|
||||
.ok()
|
||||
.filter(|user_id| {
|
||||
!user_id.is_historical() && user_id.server_name() == db.globals.server_name()
|
||||
})
|
||||
.ok_or(Error::BadRequest(
|
||||
ErrorKind::InvalidUsername,
|
||||
"Username is invalid.",
|
||||
))?;
|
||||
|
||||
// Check if username is creative enough
|
||||
if db.users.exists(&user_id)? {
|
||||
|
|
|
@ -4,7 +4,10 @@ use crate::{
|
|||
};
|
||||
use ruma::api::client::{
|
||||
error::ErrorKind,
|
||||
r0::media::{create_content, get_content, get_content_thumbnail, get_media_config},
|
||||
r0::media::{
|
||||
create_content, get_content, get_content_as_filename, get_content_thumbnail,
|
||||
get_media_config,
|
||||
},
|
||||
};
|
||||
use std::convert::TryInto;
|
||||
|
||||
|
@ -71,7 +74,39 @@ pub async fn create_content_route(
|
|||
.into())
|
||||
}
|
||||
|
||||
/// # `POST /_matrix/media/r0/download/{serverName}/{mediaId}`
|
||||
pub async fn get_remote_content(
|
||||
db: &DatabaseGuard,
|
||||
mxc: &str,
|
||||
server_name: &ruma::ServerName,
|
||||
media_id: &str,
|
||||
) -> Result<get_content::Response, Error> {
|
||||
let content_response = db
|
||||
.sending
|
||||
.send_federation_request(
|
||||
&db.globals,
|
||||
server_name,
|
||||
get_content::Request {
|
||||
allow_remote: false,
|
||||
server_name,
|
||||
media_id,
|
||||
},
|
||||
)
|
||||
.await?;
|
||||
|
||||
db.media
|
||||
.create(
|
||||
mxc.to_string(),
|
||||
&db.globals,
|
||||
&content_response.content_disposition.as_deref(),
|
||||
&content_response.content_type.as_deref(),
|
||||
&content_response.file,
|
||||
)
|
||||
.await?;
|
||||
|
||||
Ok(content_response)
|
||||
}
|
||||
|
||||
/// # `GET /_matrix/media/r0/download/{serverName}/{mediaId}`
|
||||
///
|
||||
/// Load media from our server or over federation.
|
||||
///
|
||||
|
@ -100,36 +135,58 @@ pub async fn get_content_route(
|
|||
}
|
||||
.into())
|
||||
} else if &*body.server_name != db.globals.server_name() && body.allow_remote {
|
||||
let get_content_response = db
|
||||
.sending
|
||||
.send_federation_request(
|
||||
&db.globals,
|
||||
&body.server_name,
|
||||
get_content::Request {
|
||||
allow_remote: false,
|
||||
server_name: &body.server_name,
|
||||
media_id: &body.media_id,
|
||||
},
|
||||
)
|
||||
.await?;
|
||||
|
||||
db.media
|
||||
.create(
|
||||
mxc,
|
||||
&db.globals,
|
||||
&get_content_response.content_disposition.as_deref(),
|
||||
&get_content_response.content_type.as_deref(),
|
||||
&get_content_response.file,
|
||||
)
|
||||
.await?;
|
||||
|
||||
Ok(get_content_response.into())
|
||||
let remote_content_response =
|
||||
get_remote_content(&db, &mxc, &body.server_name, &body.media_id).await?;
|
||||
Ok(remote_content_response.into())
|
||||
} else {
|
||||
Err(Error::BadRequest(ErrorKind::NotFound, "Media not found."))
|
||||
}
|
||||
}
|
||||
|
||||
/// # `POST /_matrix/media/r0/thumbnail/{serverName}/{mediaId}`
|
||||
/// # `GET /_matrix/media/r0/download/{serverName}/{mediaId}/{fileName}`
|
||||
///
|
||||
/// Load media from our server or over federation, permitting desired filename.
|
||||
///
|
||||
/// - Only allows federation if `allow_remote` is true
|
||||
#[cfg_attr(
|
||||
feature = "conduit_bin",
|
||||
get("/_matrix/media/r0/download/<_>/<_>/<_>", data = "<body>")
|
||||
)]
|
||||
#[tracing::instrument(skip(db, body))]
|
||||
pub async fn get_content_as_filename_route(
|
||||
db: DatabaseGuard,
|
||||
body: Ruma<get_content_as_filename::Request<'_>>,
|
||||
) -> ConduitResult<get_content_as_filename::Response> {
|
||||
let mxc = format!("mxc://{}/{}", body.server_name, body.media_id);
|
||||
|
||||
if let Some(FileMeta {
|
||||
content_disposition: _,
|
||||
content_type,
|
||||
file,
|
||||
}) = db.media.get(&db.globals, &mxc).await?
|
||||
{
|
||||
Ok(get_content_as_filename::Response {
|
||||
file,
|
||||
content_type,
|
||||
content_disposition: Some(format!("inline; filename={}", body.filename)),
|
||||
}
|
||||
.into())
|
||||
} else if &*body.server_name != db.globals.server_name() && body.allow_remote {
|
||||
let remote_content_response =
|
||||
get_remote_content(&db, &mxc, &body.server_name, &body.media_id).await?;
|
||||
|
||||
Ok(get_content_as_filename::Response {
|
||||
content_disposition: Some(format!("inline: filename={}", body.filename)),
|
||||
content_type: remote_content_response.content_type,
|
||||
file: remote_content_response.file,
|
||||
}
|
||||
.into())
|
||||
} else {
|
||||
Err(Error::BadRequest(ErrorKind::NotFound, "Media not found."))
|
||||
}
|
||||
}
|
||||
|
||||
/// # `GET /_matrix/media/r0/thumbnail/{serverName}/{mediaId}`
|
||||
///
|
||||
/// Load media thumbnail from our server or over federation.
|
||||
///
|
||||
|
@ -150,7 +207,7 @@ pub async fn get_content_thumbnail_route(
|
|||
}) = db
|
||||
.media
|
||||
.get_thumbnail(
|
||||
mxc.clone(),
|
||||
&mxc,
|
||||
&db.globals,
|
||||
body.width
|
||||
.try_into()
|
||||
|
|
|
@ -655,7 +655,7 @@ async fn join_room_by_id_helper(
|
|||
|
||||
db.rooms.get_or_create_shortroomid(room_id, &db.globals)?;
|
||||
|
||||
let pdu = PduEvent::from_id_val(event_id, join_event.clone())
|
||||
let parsed_pdu = PduEvent::from_id_val(event_id, join_event.clone())
|
||||
.map_err(|_| Error::BadServerResponse("Invalid join event PDU."))?;
|
||||
|
||||
let mut state = HashMap::new();
|
||||
|
@ -695,14 +695,15 @@ async fn join_room_by_id_helper(
|
|||
}
|
||||
|
||||
let incoming_shortstatekey = db.rooms.get_or_create_shortstatekey(
|
||||
&pdu.kind,
|
||||
pdu.state_key
|
||||
&parsed_pdu.kind,
|
||||
parsed_pdu
|
||||
.state_key
|
||||
.as_ref()
|
||||
.expect("Pdu is a membership state event"),
|
||||
&db.globals,
|
||||
)?;
|
||||
|
||||
state.insert(incoming_shortstatekey, pdu.event_id.clone());
|
||||
state.insert(incoming_shortstatekey, parsed_pdu.event_id.clone());
|
||||
|
||||
let create_shortstatekey = db
|
||||
.rooms
|
||||
|
@ -738,12 +739,12 @@ async fn join_room_by_id_helper(
|
|||
|
||||
// We append to state before appending the pdu, so we don't have a moment in time with the
|
||||
// pdu without it's state. This is okay because append_pdu can't fail.
|
||||
let statehashid = db.rooms.append_to_state(&pdu, &db.globals)?;
|
||||
let statehashid = db.rooms.append_to_state(&parsed_pdu, &db.globals)?;
|
||||
|
||||
db.rooms.append_pdu(
|
||||
&pdu,
|
||||
utils::to_canonical_object(&pdu).expect("Pdu is valid canonical object"),
|
||||
iter::once(&*pdu.event_id),
|
||||
&parsed_pdu,
|
||||
join_event,
|
||||
iter::once(&*parsed_pdu.event_id),
|
||||
db,
|
||||
)?;
|
||||
|
||||
|
|
|
@ -344,10 +344,13 @@ pub async fn create_room_route(
|
|||
|
||||
// 6. Events listed in initial_state
|
||||
for event in &body.initial_state {
|
||||
let pdu_builder = PduBuilder::from(event.deserialize().map_err(|e| {
|
||||
let mut pdu_builder = event.deserialize_as::<PduBuilder>().map_err(|e| {
|
||||
warn!("Invalid initial state event: {:?}", e);
|
||||
Error::BadRequest(ErrorKind::InvalidParam, "Invalid initial state event.")
|
||||
})?);
|
||||
})?;
|
||||
|
||||
// Implicit state key defaults to ""
|
||||
pdu_builder.state_key.get_or_insert_with(|| "".to_owned());
|
||||
|
||||
// Silently skip encryption events if they are not allowed
|
||||
if pdu_builder.event_type == EventType::RoomEncryption && !db.globals.allow_encryption() {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue