de-global services() from api

Signed-off-by: Jason Volk <jason@zemos.net>
This commit is contained in:
Jason Volk 2024-07-16 08:05:25 +00:00
parent 463f1a1287
commit 8b6018d77d
61 changed files with 1485 additions and 1320 deletions

View file

@ -1,3 +1,4 @@
use axum::extract::State;
use ruma::{
api::client::{
config::{get_global_account_data, get_room_account_data, set_global_account_data, set_room_account_data},
@ -10,15 +11,21 @@ use ruma::{
use serde::Deserialize;
use serde_json::{json, value::RawValue as RawJsonValue};
use crate::{services, Error, Result, Ruma};
use crate::{service::Services, Error, Result, Ruma};
/// # `PUT /_matrix/client/r0/user/{userId}/account_data/{type}`
///
/// Sets some account data for the sender user.
pub(crate) async fn set_global_account_data_route(
body: Ruma<set_global_account_data::v3::Request>,
State(services): State<crate::State>, body: Ruma<set_global_account_data::v3::Request>,
) -> Result<set_global_account_data::v3::Response> {
set_account_data(None, &body.sender_user, &body.event_type.to_string(), body.data.json())?;
set_account_data(
services,
None,
&body.sender_user,
&body.event_type.to_string(),
body.data.json(),
)?;
Ok(set_global_account_data::v3::Response {})
}
@ -27,9 +34,10 @@ pub(crate) async fn set_global_account_data_route(
///
/// Sets some room account data for the sender user.
pub(crate) async fn set_room_account_data_route(
body: Ruma<set_room_account_data::v3::Request>,
State(services): State<crate::State>, body: Ruma<set_room_account_data::v3::Request>,
) -> Result<set_room_account_data::v3::Response> {
set_account_data(
services,
Some(&body.room_id),
&body.sender_user,
&body.event_type.to_string(),
@ -43,11 +51,11 @@ pub(crate) async fn set_room_account_data_route(
///
/// Gets some account data for the sender user.
pub(crate) async fn get_global_account_data_route(
body: Ruma<get_global_account_data::v3::Request>,
State(services): State<crate::State>, body: Ruma<get_global_account_data::v3::Request>,
) -> Result<get_global_account_data::v3::Response> {
let sender_user = body.sender_user.as_ref().expect("user is authenticated");
let event: Box<RawJsonValue> = services()
let event: Box<RawJsonValue> = services
.account_data
.get(None, sender_user, body.event_type.to_string().into())?
.ok_or_else(|| Error::BadRequest(ErrorKind::NotFound, "Data not found."))?;
@ -65,11 +73,11 @@ pub(crate) async fn get_global_account_data_route(
///
/// Gets some room account data for the sender user.
pub(crate) async fn get_room_account_data_route(
body: Ruma<get_room_account_data::v3::Request>,
State(services): State<crate::State>, body: Ruma<get_room_account_data::v3::Request>,
) -> Result<get_room_account_data::v3::Response> {
let sender_user = body.sender_user.as_ref().expect("user is authenticated");
let event: Box<RawJsonValue> = services()
let event: Box<RawJsonValue> = services
.account_data
.get(Some(&body.room_id), sender_user, body.event_type.clone())?
.ok_or_else(|| Error::BadRequest(ErrorKind::NotFound, "Data not found."))?;
@ -84,14 +92,15 @@ pub(crate) async fn get_room_account_data_route(
}
fn set_account_data(
room_id: Option<&RoomId>, sender_user: &Option<OwnedUserId>, event_type: &str, data: &RawJsonValue,
services: &Services, room_id: Option<&RoomId>, sender_user: &Option<OwnedUserId>, event_type: &str,
data: &RawJsonValue,
) -> Result<()> {
let sender_user = sender_user.as_ref().expect("user is authenticated");
let data: serde_json::Value =
serde_json::from_str(data.get()).map_err(|_| Error::BadRequest(ErrorKind::BadJson, "Data is invalid."))?;
services().account_data.update(
services.account_data.update(
room_id,
sender_user,
event_type.into(),