add get_pdu_owned sans Arc; improve client/room/event handler
Signed-off-by: Jason Volk <jason@zemos.net>
This commit is contained in:
parent
3ed2c17f98
commit
7450c654ae
7 changed files with 50 additions and 38 deletions
|
@ -6,7 +6,7 @@ use std::{
|
||||||
};
|
};
|
||||||
|
|
||||||
use conduit::{debug_error, err, info, trace, utils, utils::string::EMPTY, warn, Error, PduEvent, Result};
|
use conduit::{debug_error, err, info, trace, utils, utils::string::EMPTY, warn, Error, PduEvent, Result};
|
||||||
use futures::StreamExt;
|
use futures::{FutureExt, StreamExt};
|
||||||
use ruma::{
|
use ruma::{
|
||||||
api::{client::error::ErrorKind, federation::event::get_room_state},
|
api::{client::error::ErrorKind, federation::event::get_room_state},
|
||||||
events::room::message::RoomMessageEventContent,
|
events::room::message::RoomMessageEventContent,
|
||||||
|
@ -246,6 +246,7 @@ pub(super) async fn get_remote_pdu(
|
||||||
.rooms
|
.rooms
|
||||||
.timeline
|
.timeline
|
||||||
.backfill_pdu(&server, response.pdu)
|
.backfill_pdu(&server, response.pdu)
|
||||||
|
.boxed()
|
||||||
.await?;
|
.await?;
|
||||||
|
|
||||||
let json_text = serde_json::to_string_pretty(&json).expect("canonical json is valid json");
|
let json_text = serde_json::to_string_pretty(&json).expect("canonical json is valid json");
|
||||||
|
|
|
@ -374,7 +374,9 @@ pub(crate) async fn invite_user_route(
|
||||||
return Ok(invite_user::v3::Response {});
|
return Ok(invite_user::v3::Response {});
|
||||||
}
|
}
|
||||||
|
|
||||||
invite_helper(&services, sender_user, user_id, &body.room_id, body.reason.clone(), false).await?;
|
invite_helper(&services, sender_user, user_id, &body.room_id, body.reason.clone(), false)
|
||||||
|
.boxed()
|
||||||
|
.await?;
|
||||||
Ok(invite_user::v3::Response {})
|
Ok(invite_user::v3::Response {})
|
||||||
} else {
|
} else {
|
||||||
Err(Error::BadRequest(ErrorKind::NotFound, "User not found."))
|
Err(Error::BadRequest(ErrorKind::NotFound, "User not found."))
|
||||||
|
|
|
@ -2,7 +2,7 @@ use std::{cmp::max, collections::BTreeMap};
|
||||||
|
|
||||||
use axum::extract::State;
|
use axum::extract::State;
|
||||||
use conduit::{debug_info, debug_warn, err, Err};
|
use conduit::{debug_info, debug_warn, err, Err};
|
||||||
use futures::{FutureExt, StreamExt};
|
use futures::{FutureExt, StreamExt, TryFutureExt};
|
||||||
use ruma::{
|
use ruma::{
|
||||||
api::client::{
|
api::client::{
|
||||||
error::ErrorKind,
|
error::ErrorKind,
|
||||||
|
@ -486,34 +486,28 @@ pub(crate) async fn create_room_route(
|
||||||
/// - You have to currently be joined to the room (TODO: Respect history
|
/// - You have to currently be joined to the room (TODO: Respect history
|
||||||
/// visibility)
|
/// visibility)
|
||||||
pub(crate) async fn get_room_event_route(
|
pub(crate) async fn get_room_event_route(
|
||||||
State(services): State<crate::State>, body: Ruma<get_room_event::v3::Request>,
|
State(services): State<crate::State>, ref body: Ruma<get_room_event::v3::Request>,
|
||||||
) -> Result<get_room_event::v3::Response> {
|
) -> Result<get_room_event::v3::Response> {
|
||||||
let sender_user = body.sender_user.as_ref().expect("user is authenticated");
|
|
||||||
|
|
||||||
let event = services
|
|
||||||
.rooms
|
|
||||||
.timeline
|
|
||||||
.get_pdu(&body.event_id)
|
|
||||||
.await
|
|
||||||
.map_err(|_| err!(Request(NotFound("Event {} not found.", &body.event_id))))?;
|
|
||||||
|
|
||||||
if !services
|
|
||||||
.rooms
|
|
||||||
.state_accessor
|
|
||||||
.user_can_see_event(sender_user, &event.room_id, &body.event_id)
|
|
||||||
.await
|
|
||||||
{
|
|
||||||
return Err(Error::BadRequest(
|
|
||||||
ErrorKind::forbidden(),
|
|
||||||
"You don't have permission to view this event.",
|
|
||||||
));
|
|
||||||
}
|
|
||||||
|
|
||||||
let mut event = (*event).clone();
|
|
||||||
event.add_age()?;
|
|
||||||
|
|
||||||
Ok(get_room_event::v3::Response {
|
Ok(get_room_event::v3::Response {
|
||||||
event: event.to_room_event(),
|
event: services
|
||||||
|
.rooms
|
||||||
|
.timeline
|
||||||
|
.get_pdu_owned(&body.event_id)
|
||||||
|
.map_err(|_| err!(Request(NotFound("Event {} not found.", &body.event_id))))
|
||||||
|
.and_then(|event| async move {
|
||||||
|
services
|
||||||
|
.rooms
|
||||||
|
.state_accessor
|
||||||
|
.user_can_see_event(body.sender_user(), &event.room_id, &body.event_id)
|
||||||
|
.await
|
||||||
|
.then_some(event)
|
||||||
|
.ok_or_else(|| err!(Request(Forbidden("You don't have permission to view this event."))))
|
||||||
|
})
|
||||||
|
.map_ok(|mut event| {
|
||||||
|
event.add_age().ok();
|
||||||
|
event.to_room_event()
|
||||||
|
})
|
||||||
|
.await?,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,3 +1,5 @@
|
||||||
|
#![allow(clippy::toplevel_ref_arg)]
|
||||||
|
|
||||||
pub mod client;
|
pub mod client;
|
||||||
pub mod router;
|
pub mod router;
|
||||||
pub mod server;
|
pub mod server;
|
||||||
|
|
|
@ -253,7 +253,9 @@ pub(crate) async fn create_join_event_v1_route(
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
let room_state = create_join_event(&services, body.origin(), &body.room_id, &body.pdu).await?;
|
let room_state = create_join_event(&services, body.origin(), &body.room_id, &body.pdu)
|
||||||
|
.boxed()
|
||||||
|
.await?;
|
||||||
|
|
||||||
Ok(create_join_event::v1::Response {
|
Ok(create_join_event::v1::Response {
|
||||||
room_state,
|
room_state,
|
||||||
|
@ -296,7 +298,9 @@ pub(crate) async fn create_join_event_v2_route(
|
||||||
auth_chain,
|
auth_chain,
|
||||||
state,
|
state,
|
||||||
event,
|
event,
|
||||||
} = create_join_event(&services, body.origin(), &body.room_id, &body.pdu).await?;
|
} = create_join_event(&services, body.origin(), &body.room_id, &body.pdu)
|
||||||
|
.boxed()
|
||||||
|
.await?;
|
||||||
let room_state = create_join_event::v2::RoomState {
|
let room_state = create_join_event::v2::RoomState {
|
||||||
members_omitted: false,
|
members_omitted: false,
|
||||||
auth_chain,
|
auth_chain,
|
||||||
|
|
|
@ -123,15 +123,18 @@ impl Data {
|
||||||
///
|
///
|
||||||
/// Checks the `eventid_outlierpdu` Tree if not found in the timeline.
|
/// Checks the `eventid_outlierpdu` Tree if not found in the timeline.
|
||||||
pub(super) async fn get_pdu(&self, event_id: &EventId) -> Result<Arc<PduEvent>> {
|
pub(super) async fn get_pdu(&self, event_id: &EventId) -> Result<Arc<PduEvent>> {
|
||||||
|
self.get_pdu_owned(event_id).await.map(Arc::new)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Returns the pdu.
|
||||||
|
///
|
||||||
|
/// Checks the `eventid_outlierpdu` Tree if not found in the timeline.
|
||||||
|
pub(super) async fn get_pdu_owned(&self, event_id: &EventId) -> Result<PduEvent> {
|
||||||
if let Ok(pdu) = self.get_non_outlier_pdu(event_id).await {
|
if let Ok(pdu) = self.get_non_outlier_pdu(event_id).await {
|
||||||
return Ok(Arc::new(pdu));
|
return Ok(pdu);
|
||||||
}
|
}
|
||||||
|
|
||||||
self.eventid_outlierpdu
|
self.eventid_outlierpdu.get(event_id).await.deserialized()
|
||||||
.get(event_id)
|
|
||||||
.await
|
|
||||||
.deserialized()
|
|
||||||
.map(Arc::new)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Like get_non_outlier_pdu(), but without the expense of fetching and
|
/// Like get_non_outlier_pdu(), but without the expense of fetching and
|
||||||
|
|
|
@ -244,6 +244,11 @@ impl Service {
|
||||||
/// Checks the `eventid_outlierpdu` Tree if not found in the timeline.
|
/// Checks the `eventid_outlierpdu` Tree if not found in the timeline.
|
||||||
pub async fn get_pdu(&self, event_id: &EventId) -> Result<Arc<PduEvent>> { self.db.get_pdu(event_id).await }
|
pub async fn get_pdu(&self, event_id: &EventId) -> Result<Arc<PduEvent>> { self.db.get_pdu(event_id).await }
|
||||||
|
|
||||||
|
/// Returns the pdu.
|
||||||
|
///
|
||||||
|
/// Checks the `eventid_outlierpdu` Tree if not found in the timeline.
|
||||||
|
pub async fn get_pdu_owned(&self, event_id: &EventId) -> Result<PduEvent> { self.db.get_pdu_owned(event_id).await }
|
||||||
|
|
||||||
/// Checks if pdu exists
|
/// Checks if pdu exists
|
||||||
///
|
///
|
||||||
/// Checks the `eventid_outlierpdu` Tree if not found in the timeline.
|
/// Checks the `eventid_outlierpdu` Tree if not found in the timeline.
|
||||||
|
@ -885,6 +890,7 @@ impl Service {
|
||||||
vec![(*pdu.event_id).to_owned()],
|
vec![(*pdu.event_id).to_owned()],
|
||||||
state_lock,
|
state_lock,
|
||||||
)
|
)
|
||||||
|
.boxed()
|
||||||
.await?;
|
.await?;
|
||||||
|
|
||||||
// We set the room state after inserting the pdu, so that we never have a moment
|
// We set the room state after inserting the pdu, so that we never have a moment
|
||||||
|
@ -1104,7 +1110,7 @@ impl Service {
|
||||||
match response {
|
match response {
|
||||||
Ok(response) => {
|
Ok(response) => {
|
||||||
for pdu in response.pdus {
|
for pdu in response.pdus {
|
||||||
if let Err(e) = self.backfill_pdu(backfill_server, pdu).await {
|
if let Err(e) = self.backfill_pdu(backfill_server, pdu).boxed().await {
|
||||||
warn!("Failed to add backfilled pdu in room {room_id}: {e}");
|
warn!("Failed to add backfilled pdu in room {room_id}: {e}");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue