feat(media): add `/_matrix/media/v1/create` endpoint

https://spec.matrix.org/latest/client-server-api/#post_matrixmediav1create

Signed-off-by: strawberry <strawberry@puppygock.gay>
This commit is contained in:
strawberry 2024-05-13 23:13:55 -04:00
parent 434b5118cc
commit 0188a01871
2 changed files with 25 additions and 1 deletions

View File

@ -6,7 +6,7 @@ use reqwest::Url;
use ruma::api::client::{
error::{ErrorKind, RetryAfter},
media::{
create_content, get_content, get_content_as_filename, get_content_thumbnail, get_media_config,
create_content, create_mxc_uri, get_content, get_content_as_filename, get_content_thumbnail, get_media_config,
get_media_preview,
},
};
@ -59,6 +59,29 @@ pub(crate) async fn get_media_config_v1_route(
get_media_config_route(body).await.map(RumaResponse)
}
/// # `POST /_matrix/media/v1/create`
///
/// Creates a MXC URI.
///
/// <https://spec.matrix.org/latest/client-server-api/#post_matrixmediav1create>
///
/// TODO: implement `unused_expires_at`, prevent MXC URI creation spam by
/// keeping track of created MXC URIs with no content pushed to them per-user
pub(crate) async fn create_mxc_uri(body: Ruma<create_mxc_uri::v1::Request>) -> Result<create_mxc_uri::v1::Response> {
let _sender_user = body.sender_user.as_ref().expect("user is authenticated");
let mxc = format!(
"mxc://{}/{}",
services().globals.server_name(),
utils::random_string(MXC_LENGTH)
);
Ok(create_mxc_uri::v1::Response {
content_uri: mxc.into(),
unused_expires_at: None,
})
}
/// # `GET /_matrix/media/v3/preview_url`
///
/// Returns URL preview.

View File

@ -133,6 +133,7 @@ pub(crate) fn routes(config: &Config) -> Router {
.ruma_route(client_server::get_media_config_route)
.ruma_route(client_server::get_media_preview_route)
.ruma_route(client_server::create_content_route)
.ruma_route(client_server::create_mxc_uri)
// legacy v1 media routes
.route(
"/_matrix/media/v1/preview_url",