apply new rustfmt.toml changes, fix some clippy lints

Signed-off-by: strawberry <strawberry@puppygock.gay>
This commit is contained in:
strawberry 2024-12-15 00:05:47 -05:00
parent 0317cc8cc5
commit 77e0b76408
No known key found for this signature in database
296 changed files with 7147 additions and 4300 deletions

View file

@ -16,7 +16,8 @@ use crate::Ruma;
/// Retrieves events from before the sender joined the room, if the room's
/// history visibility allows.
pub(crate) async fn get_backfill_route(
State(services): State<crate::State>, ref body: Ruma<get_backfill::v1::Request>,
State(services): State<crate::State>,
ref body: Ruma<get_backfill::v1::Request>,
) -> Result<get_backfill::v1::Response> {
AccessCheck {
services: &services,

View file

@ -12,7 +12,8 @@ use crate::Ruma;
/// - Only works if a user of this server is currently invited or joined the
/// room
pub(crate) async fn get_event_route(
State(services): State<crate::State>, body: Ruma<get_event::v1::Request>,
State(services): State<crate::State>,
body: Ruma<get_event::v1::Request>,
) -> Result<get_event::v1::Response> {
let event = services
.rooms

View file

@ -17,7 +17,8 @@ use crate::Ruma;
///
/// - This does not include the event itself
pub(crate) async fn get_event_authorization_route(
State(services): State<crate::State>, body: Ruma<get_event_authorization::v1::Request>,
State(services): State<crate::State>,
body: Ruma<get_event_authorization::v1::Request>,
) -> Result<get_event_authorization::v1::Response> {
AccessCheck {
services: &services,
@ -40,8 +41,8 @@ pub(crate) async fn get_event_authorization_route(
.and_then(|val| val.as_str())
.ok_or_else(|| Error::bad_database("Invalid event in database."))?;
let room_id =
<&RoomId>::try_from(room_id_str).map_err(|_| Error::bad_database("Invalid room_id in event in database."))?;
let room_id = <&RoomId>::try_from(room_id_str)
.map_err(|_| Error::bad_database("Invalid room_id in event in database."))?;
let auth_chain = services
.rooms
@ -53,7 +54,5 @@ pub(crate) async fn get_event_authorization_route(
.collect()
.await;
Ok(get_event_authorization::v1::Response {
auth_chain,
})
Ok(get_event_authorization::v1::Response { auth_chain })
}

View file

@ -12,7 +12,8 @@ use crate::Ruma;
///
/// Retrieves events that the sender is missing.
pub(crate) async fn get_missing_events_route(
State(services): State<crate::State>, body: Ruma<get_missing_events::v1::Request>,
State(services): State<crate::State>,
body: Ruma<get_missing_events::v1::Request>,
) -> Result<get_missing_events::v1::Response> {
AccessCheck {
services: &services,
@ -87,7 +88,5 @@ pub(crate) async fn get_missing_events_route(
i = i.saturating_add(1);
}
Ok(get_missing_events::v1::Response {
events,
})
Ok(get_missing_events::v1::Response { events })
}

View file

@ -8,7 +8,8 @@ use crate::{Error, Result, Ruma};
/// Gets the space tree in a depth-first manner to locate child rooms of a given
/// space.
pub(crate) async fn get_hierarchy_route(
State(services): State<crate::State>, body: Ruma<get_hierarchy::v1::Request>,
State(services): State<crate::State>,
body: Ruma<get_hierarchy::v1::Request>,
) -> Result<get_hierarchy::v1::Response> {
if services.rooms.metadata.exists(&body.room_id).await {
services

View file

@ -16,7 +16,8 @@ use crate::Ruma;
/// Invites a remote user to a room.
#[tracing::instrument(skip_all, fields(%client), name = "invite")]
pub(crate) async fn create_invite_route(
State(services): State<crate::State>, InsecureClientIp(client): InsecureClientIp,
State(services): State<crate::State>,
InsecureClientIp(client): InsecureClientIp,
body: Ruma<create_invite::v2::Request>,
) -> Result<create_invite::v2::Response> {
// ACL check origin
@ -28,9 +29,7 @@ pub(crate) async fn create_invite_route(
if !services.server.supported_room_version(&body.room_version) {
return Err(Error::BadRequest(
ErrorKind::IncompatibleRoomVersion {
room_version: body.room_version.clone(),
},
ErrorKind::IncompatibleRoomVersion { room_version: body.room_version.clone() },
"Server does not support this room version.",
));
}
@ -102,11 +101,14 @@ pub(crate) async fn create_invite_route(
.try_into()
.map_err(|e| err!(Request(InvalidParam("Invalid sender property: {e}"))))?;
if services.rooms.metadata.is_banned(&body.room_id).await && !services.users.is_admin(&invited_user).await {
if services.rooms.metadata.is_banned(&body.room_id).await
&& !services.users.is_admin(&invited_user).await
{
return Err!(Request(Forbidden("This room is banned on this homeserver.")));
}
if services.globals.block_non_admin_invites() && !services.users.is_admin(&invited_user).await {
if services.globals.block_non_admin_invites() && !services.users.is_admin(&invited_user).await
{
return Err!(Request(Forbidden("This server does not allow room invites.")));
}

View file

@ -22,7 +22,9 @@ use ruma::{
/// this will be valid forever.
// Response type for this endpoint is Json because we need to calculate a
// signature for the response
pub(crate) async fn get_server_keys_route(State(services): State<crate::State>) -> Result<impl IntoResponse> {
pub(crate) async fn get_server_keys_route(
State(services): State<crate::State>,
) -> Result<impl IntoResponse> {
let server_name = services.globals.server_name();
let active_key_id = services.server_keys.active_key_id();
let mut all_keys = services.server_keys.verify_keys_for(server_name).await;
@ -72,6 +74,8 @@ fn expires_ts() -> MilliSecondsSinceUnixEpoch {
///
/// - Matrix does not support invalidating public keys, so the key returned by
/// this will be valid forever.
pub(crate) async fn get_server_keys_deprecated_route(State(services): State<crate::State>) -> impl IntoResponse {
pub(crate) async fn get_server_keys_deprecated_route(
State(services): State<crate::State>,
) -> impl IntoResponse {
get_server_keys_route(State(services)).await
}

View file

@ -23,7 +23,8 @@ use crate::{
///
/// Creates a join template.
pub(crate) async fn create_join_event_template_route(
State(services): State<crate::State>, body: Ruma<prepare_join_event::v1::Request>,
State(services): State<crate::State>,
body: Ruma<prepare_join_event::v1::Request>,
) -> Result<prepare_join_event::v1::Response> {
if !services.rooms.metadata.exists(&body.room_id).await {
return Err!(Request(NotFound("Room is unknown to this server.")));
@ -47,8 +48,8 @@ pub(crate) async fn create_join_event_template_route(
.contains(body.origin())
{
warn!(
"Server {} for remote user {} tried joining room ID {} which has a server name that is globally \
forbidden. Rejecting.",
"Server {} for remote user {} tried joining room ID {} which has a server name that \
is globally forbidden. Rejecting.",
body.origin(),
&body.user_id,
&body.room_id,
@ -72,9 +73,7 @@ pub(crate) async fn create_join_event_template_route(
let room_version_id = services.rooms.state.get_room_version(&body.room_id).await?;
if !body.ver.contains(&room_version_id) {
return Err(Error::BadRequest(
ErrorKind::IncompatibleRoomVersion {
room_version: room_version_id,
},
ErrorKind::IncompatibleRoomVersion { room_version: room_version_id },
"Room version not supported.",
));
}
@ -86,16 +85,25 @@ pub(crate) async fn create_join_event_template_route(
if matches!(room_version_id, V1 | V2 | V3 | V4 | V5 | V6 | V7) {
// room version does not support restricted join rules
None
} else if user_can_perform_restricted_join(&services, &body.user_id, &body.room_id, &room_version_id).await? {
} else if user_can_perform_restricted_join(
&services,
&body.user_id,
&body.room_id,
&room_version_id,
)
.await?
{
let Some(auth_user) = services
.rooms
.state_cache
.local_users_in_room(&body.room_id)
.filter(|user| {
services
.rooms
.state_accessor
.user_can_invite(&body.room_id, user, &body.user_id, &state_lock)
services.rooms.state_accessor.user_can_invite(
&body.room_id,
user,
&body.user_id,
&state_lock,
)
})
.boxed()
.next()
@ -116,13 +124,10 @@ pub(crate) async fn create_join_event_template_route(
.rooms
.timeline
.create_hash_and_sign_event(
PduBuilder::state(
body.user_id.to_string(),
&RoomMemberEventContent {
join_authorized_via_users_server,
..RoomMemberEventContent::new(MembershipState::Join)
},
),
PduBuilder::state(body.user_id.to_string(), &RoomMemberEventContent {
join_authorized_via_users_server,
..RoomMemberEventContent::new(MembershipState::Join)
}),
&body.user_id,
&body.room_id,
&state_lock,
@ -142,7 +147,10 @@ pub(crate) async fn create_join_event_template_route(
/// Checks whether the given user can join the given room via a restricted join.
pub(crate) async fn user_can_perform_restricted_join(
services: &Services, user_id: &UserId, room_id: &RoomId, room_version_id: &RoomVersionId,
services: &Services,
user_id: &UserId,
room_id: &RoomId,
room_version_id: &RoomVersionId,
) -> Result<bool> {
use RoomVersionId::*;
@ -159,13 +167,19 @@ pub(crate) async fn user_can_perform_restricted_join(
let Ok(join_rules_event_content) = services
.rooms
.state_accessor
.room_state_get_content::<RoomJoinRulesEventContent>(room_id, &StateEventType::RoomJoinRules, "")
.room_state_get_content::<RoomJoinRulesEventContent>(
room_id,
&StateEventType::RoomJoinRules,
"",
)
.await
else {
return Ok(false);
};
let (JoinRule::Restricted(r) | JoinRule::KnockRestricted(r)) = join_rules_event_content.join_rule else {
let (JoinRule::Restricted(r) | JoinRule::KnockRestricted(r)) =
join_rules_event_content.join_rule
else {
return Ok(false);
};
@ -195,12 +209,15 @@ pub(crate) async fn user_can_perform_restricted_join(
}
}
pub(crate) fn maybe_strip_event_id(pdu_json: &mut CanonicalJsonObject, room_version_id: &RoomVersionId) -> Result {
pub(crate) fn maybe_strip_event_id(
pdu_json: &mut CanonicalJsonObject,
room_version_id: &RoomVersionId,
) -> Result {
use RoomVersionId::*;
match room_version_id {
V1 | V2 => Ok(()),
_ => {
| V1 | V2 => Ok(()),
| _ => {
pdu_json.remove("event_id");
Ok(())
},

View file

@ -13,7 +13,8 @@ use crate::{service::pdu::PduBuilder, Ruma};
///
/// Creates a leave template.
pub(crate) async fn create_leave_event_template_route(
State(services): State<crate::State>, body: Ruma<prepare_leave_event::v1::Request>,
State(services): State<crate::State>,
body: Ruma<prepare_leave_event::v1::Request>,
) -> Result<prepare_leave_event::v1::Response> {
if !services.rooms.metadata.exists(&body.room_id).await {
return Err!(Request(NotFound("Room is unknown to this server.")));
@ -37,7 +38,10 @@ pub(crate) async fn create_leave_event_template_route(
.rooms
.timeline
.create_hash_and_sign_event(
PduBuilder::state(body.user_id.to_string(), &RoomMemberEventContent::new(MembershipState::Leave)),
PduBuilder::state(
body.user_id.to_string(),
&RoomMemberEventContent::new(MembershipState::Leave),
),
&body.user_id,
&body.room_id,
&state_lock,

View file

@ -16,7 +16,8 @@ use crate::Ruma;
/// Load media from our server.
#[tracing::instrument(skip_all, fields(%client), name = "media_get")]
pub(crate) async fn get_content_route(
State(services): State<crate::State>, InsecureClientIp(client): InsecureClientIp,
State(services): State<crate::State>,
InsecureClientIp(client): InsecureClientIp,
body: Ruma<get_content::v1::Request>,
) -> Result<get_content::v1::Response> {
let mxc = Mxc {
@ -33,7 +34,8 @@ pub(crate) async fn get_content_route(
return Err!(Request(NotFound("Media not found.")));
};
let content_disposition = make_content_disposition(content_disposition.as_ref(), content_type.as_deref(), None);
let content_disposition =
make_content_disposition(content_disposition.as_ref(), content_type.as_deref(), None);
let content = Content {
file: content.expect("entire file contents"),
content_type: content_type.map(Into::into),
@ -51,7 +53,8 @@ pub(crate) async fn get_content_route(
/// Load media thumbnail from our server.
#[tracing::instrument(skip_all, fields(%client), name = "media_thumbnail_get")]
pub(crate) async fn get_content_thumbnail_route(
State(services): State<crate::State>, InsecureClientIp(client): InsecureClientIp,
State(services): State<crate::State>,
InsecureClientIp(client): InsecureClientIp,
body: Ruma<get_content_thumbnail::v1::Request>,
) -> Result<get_content_thumbnail::v1::Response> {
let dim = Dim::from_ruma(body.width, body.height, body.method.clone())?;
@ -69,7 +72,8 @@ pub(crate) async fn get_content_thumbnail_route(
return Err!(Request(NotFound("Media not found.")));
};
let content_disposition = make_content_disposition(content_disposition.as_ref(), content_type.as_deref(), None);
let content_disposition =
make_content_disposition(content_disposition.as_ref(), content_type.as_deref(), None);
let content = Content {
file: content.expect("entire file contents"),
content_type: content_type.map(Into::into),

View file

@ -7,7 +7,8 @@ use crate::{Result, Ruma};
///
/// Get information about the user that generated the OpenID token.
pub(crate) async fn get_openid_userinfo_route(
State(services): State<crate::State>, body: Ruma<get_openid_userinfo::v1::Request>,
State(services): State<crate::State>,
body: Ruma<get_openid_userinfo::v1::Request>,
) -> Result<get_openid_userinfo::v1::Response> {
Ok(get_openid_userinfo::v1::Response::new(
services

View file

@ -15,7 +15,8 @@ use crate::{Error, Result, Ruma};
/// Lists the public rooms on this server.
#[tracing::instrument(skip_all, fields(%client), name = "publicrooms")]
pub(crate) async fn get_public_rooms_filtered_route(
State(services): State<crate::State>, InsecureClientIp(client): InsecureClientIp,
State(services): State<crate::State>,
InsecureClientIp(client): InsecureClientIp,
body: Ruma<get_public_rooms_filtered::v1::Request>,
) -> Result<get_public_rooms_filtered::v1::Response> {
if !services
@ -35,7 +36,9 @@ pub(crate) async fn get_public_rooms_filtered_route(
&body.room_network,
)
.await
.map_err(|_| Error::BadRequest(ErrorKind::Unknown, "Failed to return this server's public room list."))?;
.map_err(|_| {
Error::BadRequest(ErrorKind::Unknown, "Failed to return this server's public room list.")
})?;
Ok(get_public_rooms_filtered::v1::Response {
chunk: response.chunk,
@ -50,7 +53,8 @@ pub(crate) async fn get_public_rooms_filtered_route(
/// Lists the public rooms on this server.
#[tracing::instrument(skip_all, fields(%client), "publicrooms")]
pub(crate) async fn get_public_rooms_route(
State(services): State<crate::State>, InsecureClientIp(client): InsecureClientIp,
State(services): State<crate::State>,
InsecureClientIp(client): InsecureClientIp,
body: Ruma<get_public_rooms::v1::Request>,
) -> Result<get_public_rooms::v1::Response> {
if !services
@ -69,7 +73,9 @@ pub(crate) async fn get_public_rooms_route(
&body.room_network,
)
.await
.map_err(|_| Error::BadRequest(ErrorKind::Unknown, "Failed to return this server's public room list."))?;
.map_err(|_| {
Error::BadRequest(ErrorKind::Unknown, "Failed to return this server's public room list.")
})?;
Ok(get_public_rooms::v1::Response {
chunk: response.chunk,

View file

@ -19,7 +19,8 @@ use crate::Ruma;
///
/// Resolve a room alias to a room id.
pub(crate) async fn get_room_information_route(
State(services): State<crate::State>, body: Ruma<get_room_information::v1::Request>,
State(services): State<crate::State>,
body: Ruma<get_room_information::v1::Request>,
) -> Result<get_room_information::v1::Response> {
let room_id = services
.rooms
@ -50,10 +51,7 @@ pub(crate) async fn get_room_information_route(
servers.insert(0, services.globals.server_name().to_owned());
}
Ok(get_room_information::v1::Response {
room_id,
servers,
})
Ok(get_room_information::v1::Response { room_id, servers })
}
/// # `GET /_matrix/federation/v1/query/profile`
@ -61,7 +59,8 @@ pub(crate) async fn get_room_information_route(
///
/// Gets information on a profile.
pub(crate) async fn get_profile_information_route(
State(services): State<crate::State>, body: Ruma<get_profile_information::v1::Request>,
State(services): State<crate::State>,
body: Ruma<get_profile_information::v1::Request>,
) -> Result<get_profile_information::v1::Response> {
if !services
.globals
@ -88,14 +87,14 @@ pub(crate) async fn get_profile_information_route(
let mut custom_profile_fields = BTreeMap::new();
match &body.field {
Some(ProfileField::DisplayName) => {
| Some(ProfileField::DisplayName) => {
displayname = services.users.displayname(&body.user_id).await.ok();
},
Some(ProfileField::AvatarUrl) => {
| Some(ProfileField::AvatarUrl) => {
avatar_url = services.users.avatar_url(&body.user_id).await.ok();
blurhash = services.users.blurhash(&body.user_id).await.ok();
},
Some(custom_field) => {
| Some(custom_field) => {
if let Ok(value) = services
.users
.profile_key(&body.user_id, custom_field.as_str())
@ -104,7 +103,7 @@ pub(crate) async fn get_profile_information_route(
custom_profile_fields.insert(custom_field.to_string(), value);
}
},
None => {
| None => {
displayname = services.users.displayname(&body.user_id).await.ok();
avatar_url = services.users.avatar_url(&body.user_id).await.ok();
blurhash = services.users.blurhash(&body.user_id).await.ok();

View file

@ -2,15 +2,18 @@ use std::{collections::BTreeMap, net::IpAddr, time::Instant};
use axum::extract::State;
use axum_client_ip::InsecureClientIp;
use conduwuit::{debug, debug_warn, err, error, result::LogErr, trace, utils::ReadyExt, warn, Err, Error, Result};
use conduwuit::{
debug, debug_warn, err, error, result::LogErr, trace, utils::ReadyExt, warn, Err, Error,
Result,
};
use futures::StreamExt;
use ruma::{
api::{
client::error::ErrorKind,
federation::transactions::{
edu::{
DeviceListUpdateContent, DirectDeviceContent, Edu, PresenceContent, ReceiptContent,
SigningKeyUpdateContent, TypingContent,
DeviceListUpdateContent, DirectDeviceContent, Edu, PresenceContent,
ReceiptContent, SigningKeyUpdateContent, TypingContent,
},
send_transaction_message,
},
@ -38,7 +41,8 @@ type ResolvedMap = BTreeMap<OwnedEventId, Result<()>>;
/// Push EDUs and PDUs to this server.
#[tracing::instrument(skip_all, fields(%client, origin = body.origin().as_str()), name = "send")]
pub(crate) async fn send_transaction_message_route(
State(services): State<crate::State>, InsecureClientIp(client): InsecureClientIp,
State(services): State<crate::State>,
InsecureClientIp(client): InsecureClientIp,
body: Ruma<send_transaction_message::v1::Request>,
) -> Result<send_transaction_message::v1::Response> {
if body.origin() != body.body.origin {
@ -69,7 +73,8 @@ pub(crate) async fn send_transaction_message_route(
"Starting txn",
);
let resolved_map = handle_pdus(&services, &client, &body.pdus, body.origin(), &txn_start_time).await?;
let resolved_map =
handle_pdus(&services, &client, &body.pdus, body.origin(), &txn_start_time).await?;
handle_edus(&services, &client, &body.edus, body.origin()).await;
debug!(
@ -90,13 +95,17 @@ pub(crate) async fn send_transaction_message_route(
}
async fn handle_pdus(
services: &Services, _client: &IpAddr, pdus: &[Box<RawJsonValue>], origin: &ServerName, txn_start_time: &Instant,
services: &Services,
_client: &IpAddr,
pdus: &[Box<RawJsonValue>],
origin: &ServerName,
txn_start_time: &Instant,
) -> Result<ResolvedMap> {
let mut parsed_pdus = Vec::with_capacity(pdus.len());
for pdu in pdus {
parsed_pdus.push(match services.rooms.event_handler.parse_incoming_pdu(pdu).await {
Ok(t) => t,
Err(e) => {
| Ok(t) => t,
| Err(e) => {
debug_warn!("Could not parse PDU: {e}");
continue;
},
@ -145,26 +154,45 @@ async fn handle_pdus(
Ok(resolved_map)
}
async fn handle_edus(services: &Services, client: &IpAddr, edus: &[Raw<Edu>], origin: &ServerName) {
async fn handle_edus(
services: &Services,
client: &IpAddr,
edus: &[Raw<Edu>],
origin: &ServerName,
) {
for edu in edus
.iter()
.filter_map(|edu| serde_json::from_str::<Edu>(edu.json().get()).ok())
{
match edu {
Edu::Presence(presence) => handle_edu_presence(services, client, origin, presence).await,
Edu::Receipt(receipt) => handle_edu_receipt(services, client, origin, receipt).await,
Edu::Typing(typing) => handle_edu_typing(services, client, origin, typing).await,
Edu::DeviceListUpdate(content) => handle_edu_device_list_update(services, client, origin, content).await,
Edu::DirectToDevice(content) => handle_edu_direct_to_device(services, client, origin, content).await,
Edu::SigningKeyUpdate(content) => handle_edu_signing_key_update(services, client, origin, content).await,
Edu::_Custom(ref _custom) => {
| Edu::Presence(presence) => {
handle_edu_presence(services, client, origin, presence).await;
},
| Edu::Receipt(receipt) =>
handle_edu_receipt(services, client, origin, receipt).await,
| Edu::Typing(typing) => handle_edu_typing(services, client, origin, typing).await,
| Edu::DeviceListUpdate(content) => {
handle_edu_device_list_update(services, client, origin, content).await;
},
| Edu::DirectToDevice(content) => {
handle_edu_direct_to_device(services, client, origin, content).await;
},
| Edu::SigningKeyUpdate(content) => {
handle_edu_signing_key_update(services, client, origin, content).await;
},
| Edu::_Custom(ref _custom) => {
debug_warn!(?edus, "received custom/unknown EDU");
},
}
}
}
async fn handle_edu_presence(services: &Services, _client: &IpAddr, origin: &ServerName, presence: PresenceContent) {
async fn handle_edu_presence(
services: &Services,
_client: &IpAddr,
origin: &ServerName,
presence: PresenceContent,
) {
if !services.globals.allow_incoming_presence() {
return;
}
@ -193,7 +221,12 @@ async fn handle_edu_presence(services: &Services, _client: &IpAddr, origin: &Ser
}
}
async fn handle_edu_receipt(services: &Services, _client: &IpAddr, origin: &ServerName, receipt: ReceiptContent) {
async fn handle_edu_receipt(
services: &Services,
_client: &IpAddr,
origin: &ServerName,
receipt: ReceiptContent,
) {
if !services.globals.allow_incoming_read_receipts() {
return;
}
@ -230,7 +263,8 @@ async fn handle_edu_receipt(services: &Services, _client: &IpAddr, origin: &Serv
.await
{
for event_id in &user_updates.event_ids {
let user_receipts = BTreeMap::from([(user_id.clone(), user_updates.data.clone())]);
let user_receipts =
BTreeMap::from([(user_id.clone(), user_updates.data.clone())]);
let receipts = BTreeMap::from([(ReceiptType::Read, user_receipts)]);
let receipt_content = BTreeMap::from([(event_id.to_owned(), receipts)]);
let event = ReceiptEvent {
@ -255,7 +289,12 @@ async fn handle_edu_receipt(services: &Services, _client: &IpAddr, origin: &Serv
}
}
async fn handle_edu_typing(services: &Services, _client: &IpAddr, origin: &ServerName, typing: TypingContent) {
async fn handle_edu_typing(
services: &Services,
_client: &IpAddr,
origin: &ServerName,
typing: TypingContent,
) {
if !services.globals.config.allow_incoming_typing {
return;
}
@ -321,12 +360,12 @@ async fn handle_edu_typing(services: &Services, _client: &IpAddr, origin: &Serve
}
async fn handle_edu_device_list_update(
services: &Services, _client: &IpAddr, origin: &ServerName, content: DeviceListUpdateContent,
services: &Services,
_client: &IpAddr,
origin: &ServerName,
content: DeviceListUpdateContent,
) {
let DeviceListUpdateContent {
user_id,
..
} = content;
let DeviceListUpdateContent { user_id, .. } = content;
if user_id.server_name() != origin {
debug_warn!(
@ -340,14 +379,12 @@ async fn handle_edu_device_list_update(
}
async fn handle_edu_direct_to_device(
services: &Services, _client: &IpAddr, origin: &ServerName, content: DirectDeviceContent,
services: &Services,
_client: &IpAddr,
origin: &ServerName,
content: DirectDeviceContent,
) {
let DirectDeviceContent {
sender,
ev_type,
message_id,
messages,
} = content;
let DirectDeviceContent { sender, ev_type, message_id, messages } = content;
if sender.server_name() != origin {
debug_warn!(
@ -369,23 +406,28 @@ async fn handle_edu_direct_to_device(
for (target_user_id, map) in &messages {
for (target_device_id_maybe, event) in map {
let Ok(event) = event
.deserialize_as()
.map_err(|e| err!(Request(InvalidParam(error!("To-Device event is invalid: {e}")))))
else {
let Ok(event) = event.deserialize_as().map_err(|e| {
err!(Request(InvalidParam(error!("To-Device event is invalid: {e}"))))
}) else {
continue;
};
let ev_type = ev_type.to_string();
match target_device_id_maybe {
DeviceIdOrAllDevices::DeviceId(target_device_id) => {
| DeviceIdOrAllDevices::DeviceId(target_device_id) => {
services
.users
.add_to_device_event(&sender, target_user_id, target_device_id, &ev_type, event)
.add_to_device_event(
&sender,
target_user_id,
target_device_id,
&ev_type,
event,
)
.await;
},
DeviceIdOrAllDevices::AllDevices => {
| DeviceIdOrAllDevices::AllDevices => {
let (sender, ev_type, event) = (&sender, &ev_type, &event);
services
.users
@ -412,13 +454,12 @@ async fn handle_edu_direct_to_device(
}
async fn handle_edu_signing_key_update(
services: &Services, _client: &IpAddr, origin: &ServerName, content: SigningKeyUpdateContent,
services: &Services,
_client: &IpAddr,
origin: &ServerName,
content: SigningKeyUpdateContent,
) {
let SigningKeyUpdateContent {
user_id,
master_key,
self_signing_key,
} = content;
let SigningKeyUpdateContent { user_id, master_key, self_signing_key } = content;
if user_id.server_name() != origin {
debug_warn!(

View file

@ -16,7 +16,8 @@ use ruma::{
room::member::{MembershipState, RoomMemberEventContent},
StateEventType,
},
CanonicalJsonValue, OwnedEventId, OwnedRoomId, OwnedServerName, OwnedUserId, RoomId, ServerName,
CanonicalJsonValue, OwnedEventId, OwnedRoomId, OwnedServerName, OwnedUserId, RoomId,
ServerName,
};
use serde_json::value::{to_raw_value, RawValue as RawJsonValue};
use service::Services;
@ -25,7 +26,10 @@ use crate::Ruma;
/// helper method for /send_join v1 and v2
async fn create_join_event(
services: &Services, origin: &ServerName, room_id: &RoomId, pdu: &RawJsonValue,
services: &Services,
origin: &ServerName,
room_id: &RoomId,
pdu: &RawJsonValue,
) -> Result<create_join_event::v1::RoomState> {
if !services.rooms.metadata.exists(room_id).await {
return Err!(Request(NotFound("Room is unknown to this server.")));
@ -146,7 +150,8 @@ async fn create_join_event(
if !services.globals.user_is_local(&authorising_user) {
return Err!(Request(InvalidParam(
"Cannot authorise membership event through {authorising_user} as they do not belong to this homeserver"
"Cannot authorise membership event through {authorising_user} as they do not \
belong to this homeserver"
)));
}
@ -157,12 +162,19 @@ async fn create_join_event(
.await
{
return Err!(Request(InvalidParam(
"Authorising user {authorising_user} is not in the room you are trying to join, they cannot authorise \
your join."
"Authorising user {authorising_user} is not in the room you are trying to join, \
they cannot authorise your join."
)));
}
if !super::user_can_perform_restricted_join(services, &state_key, room_id, &room_version_id).await? {
if !super::user_can_perform_restricted_join(
services,
&state_key,
room_id,
&room_version_id,
)
.await?
{
return Err!(Request(UnableToAuthorizeJoin(
"Joining user did not pass restricted room's rules."
)));
@ -228,7 +240,9 @@ async fn create_join_event(
.event_ids_iter(room_id, starting_events)
.await?
.map(Ok)
.broad_and_then(|event_id| async move { services.rooms.timeline.get_pdu_json(&event_id).await })
.broad_and_then(|event_id| async move {
services.rooms.timeline.get_pdu_json(&event_id).await
})
.broad_and_then(|pdu| {
services
.sending
@ -252,7 +266,8 @@ async fn create_join_event(
///
/// Submits a signed join event.
pub(crate) async fn create_join_event_v1_route(
State(services): State<crate::State>, body: Ruma<create_join_event::v1::Request>,
State(services): State<crate::State>,
body: Ruma<create_join_event::v1::Request>,
) -> Result<create_join_event::v1::Response> {
if services
.globals
@ -261,8 +276,8 @@ pub(crate) async fn create_join_event_v1_route(
.contains(body.origin())
{
warn!(
"Server {} tried joining room ID {} through us who has a server name that is globally forbidden. \
Rejecting.",
"Server {} tried joining room ID {} through us who has a server name that is \
globally forbidden. Rejecting.",
body.origin(),
&body.room_id,
);
@ -277,8 +292,8 @@ pub(crate) async fn create_join_event_v1_route(
.contains(&server.to_owned())
{
warn!(
"Server {} tried joining room ID {} through us which has a server name that is globally forbidden. \
Rejecting.",
"Server {} tried joining room ID {} through us which has a server name that is \
globally forbidden. Rejecting.",
body.origin(),
&body.room_id,
);
@ -292,16 +307,15 @@ pub(crate) async fn create_join_event_v1_route(
.boxed()
.await?;
Ok(create_join_event::v1::Response {
room_state,
})
Ok(create_join_event::v1::Response { room_state })
}
/// # `PUT /_matrix/federation/v2/send_join/{roomId}/{eventId}`
///
/// Submits a signed join event.
pub(crate) async fn create_join_event_v2_route(
State(services): State<crate::State>, body: Ruma<create_join_event::v2::Request>,
State(services): State<crate::State>,
body: Ruma<create_join_event::v2::Request>,
) -> Result<create_join_event::v2::Response> {
if services
.globals
@ -320,8 +334,8 @@ pub(crate) async fn create_join_event_v2_route(
.contains(&server.to_owned())
{
warn!(
"Server {} tried joining room ID {} through us which has a server name that is globally forbidden. \
Rejecting.",
"Server {} tried joining room ID {} through us which has a server name that is \
globally forbidden. Rejecting.",
body.origin(),
&body.room_id,
);
@ -331,13 +345,10 @@ pub(crate) async fn create_join_event_v2_route(
}
}
let create_join_event::v1::RoomState {
auth_chain,
state,
event,
} = create_join_event(&services, body.origin(), &body.room_id, &body.pdu)
.boxed()
.await?;
let create_join_event::v1::RoomState { auth_chain, state, event } =
create_join_event(&services, body.origin(), &body.room_id, &body.pdu)
.boxed()
.await?;
let room_state = create_join_event::v2::RoomState {
members_omitted: false,
auth_chain,
@ -346,7 +357,5 @@ pub(crate) async fn create_join_event_v2_route(
servers_in_room: None,
};
Ok(create_join_event::v2::Response {
room_state,
})
Ok(create_join_event::v2::Response { room_state })
}

View file

@ -21,7 +21,8 @@ use crate::{
///
/// Submits a signed leave event.
pub(crate) async fn create_leave_event_v1_route(
State(services): State<crate::State>, body: Ruma<create_leave_event::v1::Request>,
State(services): State<crate::State>,
body: Ruma<create_leave_event::v1::Request>,
) -> Result<create_leave_event::v1::Response> {
create_leave_event(&services, body.origin(), &body.room_id, &body.pdu).await?;
@ -32,14 +33,20 @@ pub(crate) async fn create_leave_event_v1_route(
///
/// Submits a signed leave event.
pub(crate) async fn create_leave_event_v2_route(
State(services): State<crate::State>, body: Ruma<create_leave_event::v2::Request>,
State(services): State<crate::State>,
body: Ruma<create_leave_event::v2::Request>,
) -> Result<create_leave_event::v2::Response> {
create_leave_event(&services, body.origin(), &body.room_id, &body.pdu).await?;
Ok(create_leave_event::v2::Response::new())
}
async fn create_leave_event(services: &Services, origin: &ServerName, room_id: &RoomId, pdu: &RawJsonValue) -> Result {
async fn create_leave_event(
services: &Services,
origin: &ServerName,
room_id: &RoomId,
pdu: &RawJsonValue,
) -> Result {
if !services.rooms.metadata.exists(room_id).await {
return Err!(Request(NotFound("Room is unknown to this server.")));
}

View file

@ -12,7 +12,8 @@ use crate::Ruma;
///
/// Retrieves a snapshot of a room's state at a given event.
pub(crate) async fn get_room_state_route(
State(services): State<crate::State>, body: Ruma<get_room_state::v1::Request>,
State(services): State<crate::State>,
body: Ruma<get_room_state::v1::Request>,
) -> Result<get_room_state::v1::Response> {
AccessCheck {
services: &services,
@ -69,8 +70,5 @@ pub(crate) async fn get_room_state_route(
.try_collect()
.await?;
Ok(get_room_state::v1::Response {
auth_chain,
pdus,
})
Ok(get_room_state::v1::Response { auth_chain, pdus })
}

View file

@ -13,7 +13,8 @@ use crate::Ruma;
/// Retrieves a snapshot of a room's state at a given event, in the form of
/// event IDs.
pub(crate) async fn get_room_state_ids_route(
State(services): State<crate::State>, body: Ruma<get_room_state_ids::v1::Request>,
State(services): State<crate::State>,
body: Ruma<get_room_state_ids::v1::Request>,
) -> Result<get_room_state_ids::v1::Response> {
AccessCheck {
services: &services,
@ -49,8 +50,5 @@ pub(crate) async fn get_room_state_ids_route(
.collect()
.await;
Ok(get_room_state_ids::v1::Response {
auth_chain_ids,
pdu_ids,
})
Ok(get_room_state_ids::v1::Response { auth_chain_ids, pdu_ids })
}

View file

@ -18,7 +18,8 @@ use crate::{
///
/// Gets information on all devices of the user.
pub(crate) async fn get_devices_route(
State(services): State<crate::State>, body: Ruma<get_devices::v1::Request>,
State(services): State<crate::State>,
body: Ruma<get_devices::v1::Request>,
) -> Result<get_devices::v1::Response> {
if !services.globals.user_is_local(&body.user_id) {
return Err(Error::BadRequest(
@ -52,11 +53,7 @@ pub(crate) async fn get_devices_route(
services
.users
.get_device_keys(user_id, &device_id_clone)
.map_ok(|keys| UserDevice {
device_id,
keys,
device_display_name,
})
.map_ok(|keys| UserDevice { device_id, keys, device_display_name })
.map(Result::ok)
.await
})
@ -79,7 +76,8 @@ pub(crate) async fn get_devices_route(
///
/// Gets devices and identity keys for the given users.
pub(crate) async fn get_keys_route(
State(services): State<crate::State>, body: Ruma<get_keys::v1::Request>,
State(services): State<crate::State>,
body: Ruma<get_keys::v1::Request>,
) -> Result<get_keys::v1::Response> {
if body
.device_keys
@ -112,7 +110,8 @@ pub(crate) async fn get_keys_route(
///
/// Claims one-time keys.
pub(crate) async fn claim_keys_route(
State(services): State<crate::State>, body: Ruma<claim_keys::v1::Request>,
State(services): State<crate::State>,
body: Ruma<claim_keys::v1::Request>,
) -> Result<claim_keys::v1::Response> {
if body
.one_time_keys
@ -127,7 +126,5 @@ pub(crate) async fn claim_keys_route(
let result = claim_keys_helper(&services, &body.one_time_keys).await?;
Ok(claim_keys::v1::Response {
one_time_keys: result.one_time_keys,
})
Ok(claim_keys::v1::Response { one_time_keys: result.one_time_keys })
}

View file

@ -34,10 +34,11 @@ pub(super) async fn check(&self) -> Result {
let server_can_see: OptionFuture<_> = self
.event_id
.map(|event_id| {
self.services
.rooms
.state_accessor
.server_can_see_event(self.origin, self.room_id, event_id)
self.services.rooms.state_accessor.server_can_see_event(
self.origin,
self.room_id,
event_id,
)
})
.into();

View file

@ -7,12 +7,13 @@ use crate::{Error, Result, Ruma};
///
/// Returns the .well-known URL if it is configured, otherwise returns 404.
pub(crate) async fn well_known_server(
State(services): State<crate::State>, _body: Ruma<discover_homeserver::Request>,
State(services): State<crate::State>,
_body: Ruma<discover_homeserver::Request>,
) -> Result<discover_homeserver::Response> {
Ok(discover_homeserver::Response {
server: match services.server.config.well_known.server.as_ref() {
Some(server_name) => server_name.to_owned(),
None => return Err(Error::BadRequest(ErrorKind::NotFound, "Not found.")),
| Some(server_name) => server_name.to_owned(),
| None => return Err(Error::BadRequest(ErrorKind::NotFound, "Not found.")),
},
})
}