split send from messages; refactor client/messages; add filters to client/context
Signed-off-by: Jason Volk <jason@zemos.net>
This commit is contained in:
parent
6c9ecb031a
commit
7a09ac81e0
5 changed files with 403 additions and 438 deletions
|
@ -1,14 +1,25 @@
|
||||||
use std::collections::HashSet;
|
use std::iter::once;
|
||||||
|
|
||||||
use axum::extract::State;
|
use axum::extract::State;
|
||||||
use conduit::{err, error, Err};
|
use conduit::{
|
||||||
use futures::StreamExt;
|
err, error,
|
||||||
|
utils::{future::TryExtExt, stream::ReadyExt, IterStream},
|
||||||
|
Err, Result,
|
||||||
|
};
|
||||||
|
use futures::{future::try_join, StreamExt, TryFutureExt};
|
||||||
use ruma::{
|
use ruma::{
|
||||||
api::client::{context::get_context, filter::LazyLoadOptions},
|
api::client::{context::get_context, filter::LazyLoadOptions},
|
||||||
events::{StateEventType, TimelineEventType::*},
|
events::StateEventType,
|
||||||
|
UserId,
|
||||||
};
|
};
|
||||||
|
|
||||||
use crate::{Result, Ruma};
|
use crate::{
|
||||||
|
client::message::{event_filter, ignored_filter, update_lazy, visibility_filter, LazySet},
|
||||||
|
Ruma,
|
||||||
|
};
|
||||||
|
|
||||||
|
const LIMIT_MAX: usize = 100;
|
||||||
|
const LIMIT_DEFAULT: usize = 10;
|
||||||
|
|
||||||
/// # `GET /_matrix/client/r0/rooms/{roomId}/context/{eventId}`
|
/// # `GET /_matrix/client/r0/rooms/{roomId}/context/{eventId}`
|
||||||
///
|
///
|
||||||
|
@ -19,33 +30,43 @@ use crate::{Result, Ruma};
|
||||||
pub(crate) async fn get_context_route(
|
pub(crate) async fn get_context_route(
|
||||||
State(services): State<crate::State>, body: Ruma<get_context::v3::Request>,
|
State(services): State<crate::State>, body: Ruma<get_context::v3::Request>,
|
||||||
) -> Result<get_context::v3::Response> {
|
) -> Result<get_context::v3::Response> {
|
||||||
let sender_user = body.sender_user.as_ref().expect("user is authenticated");
|
let filter = &body.filter;
|
||||||
let sender_device = body.sender_device.as_ref().expect("user is authenticated");
|
let sender = body.sender();
|
||||||
|
let (sender_user, _) = sender;
|
||||||
|
|
||||||
|
// Use limit or else 10, with maximum 100
|
||||||
|
let limit: usize = body
|
||||||
|
.limit
|
||||||
|
.try_into()
|
||||||
|
.unwrap_or(LIMIT_DEFAULT)
|
||||||
|
.min(LIMIT_MAX);
|
||||||
|
|
||||||
// some clients, at least element, seem to require knowledge of redundant
|
// some clients, at least element, seem to require knowledge of redundant
|
||||||
// members for "inline" profiles on the timeline to work properly
|
// members for "inline" profiles on the timeline to work properly
|
||||||
let (lazy_load_enabled, lazy_load_send_redundant) = match &body.filter.lazy_load_options {
|
let lazy_load_enabled = matches!(filter.lazy_load_options, LazyLoadOptions::Enabled { .. });
|
||||||
LazyLoadOptions::Enabled {
|
|
||||||
include_redundant_members,
|
|
||||||
} => (true, *include_redundant_members),
|
|
||||||
LazyLoadOptions::Disabled => (false, cfg!(feature = "element_hacks")),
|
|
||||||
};
|
|
||||||
|
|
||||||
let mut lazy_loaded = HashSet::with_capacity(100);
|
let lazy_load_redundant = if let LazyLoadOptions::Enabled {
|
||||||
|
include_redundant_members,
|
||||||
|
} = filter.lazy_load_options
|
||||||
|
{
|
||||||
|
include_redundant_members
|
||||||
|
} else {
|
||||||
|
false
|
||||||
|
};
|
||||||
|
|
||||||
let base_token = services
|
let base_token = services
|
||||||
.rooms
|
.rooms
|
||||||
.timeline
|
.timeline
|
||||||
.get_pdu_count(&body.event_id)
|
.get_pdu_count(&body.event_id)
|
||||||
.await
|
.map_err(|_| err!(Request(NotFound("Event not found."))));
|
||||||
.map_err(|_| err!(Request(NotFound("Base event id not found."))))?;
|
|
||||||
|
|
||||||
let base_event = services
|
let base_event = services
|
||||||
.rooms
|
.rooms
|
||||||
.timeline
|
.timeline
|
||||||
.get_pdu(&body.event_id)
|
.get_pdu(&body.event_id)
|
||||||
.await
|
.map_err(|_| err!(Request(NotFound("Base event not found."))));
|
||||||
.map_err(|_| err!(Request(NotFound("Base event not found."))))?;
|
|
||||||
|
let (base_token, base_event) = try_join(base_token, base_event).await?;
|
||||||
|
|
||||||
let room_id = &base_event.room_id;
|
let room_id = &base_event.room_id;
|
||||||
|
|
||||||
|
@ -58,136 +79,50 @@ pub(crate) async fn get_context_route(
|
||||||
return Err!(Request(Forbidden("You don't have permission to view this event.")));
|
return Err!(Request(Forbidden("You don't have permission to view this event.")));
|
||||||
}
|
}
|
||||||
|
|
||||||
if !services
|
|
||||||
.rooms
|
|
||||||
.lazy_loading
|
|
||||||
.lazy_load_was_sent_before(sender_user, sender_device, room_id, &base_event.sender)
|
|
||||||
.await || lazy_load_send_redundant
|
|
||||||
{
|
|
||||||
lazy_loaded.insert(base_event.sender.as_str().to_owned());
|
|
||||||
}
|
|
||||||
|
|
||||||
// Use limit or else 10, with maximum 100
|
|
||||||
let limit = usize::try_from(body.limit).unwrap_or(10).min(100);
|
|
||||||
|
|
||||||
let base_event = base_event.to_room_event();
|
|
||||||
|
|
||||||
let events_before: Vec<_> = services
|
let events_before: Vec<_> = services
|
||||||
.rooms
|
.rooms
|
||||||
.timeline
|
.timeline
|
||||||
.pdus_until(sender_user, room_id, base_token)
|
.pdus_until(sender_user, room_id, base_token)
|
||||||
.await?
|
.await?
|
||||||
|
.ready_filter_map(|item| event_filter(item, filter))
|
||||||
|
.filter_map(|item| ignored_filter(&services, item, sender_user))
|
||||||
|
.filter_map(|item| visibility_filter(&services, item, sender_user))
|
||||||
.take(limit / 2)
|
.take(limit / 2)
|
||||||
.filter_map(|(count, pdu)| async move {
|
|
||||||
// list of safe and common non-state events to ignore
|
|
||||||
if matches!(
|
|
||||||
&pdu.kind,
|
|
||||||
RoomMessage
|
|
||||||
| Sticker | CallInvite
|
|
||||||
| CallNotify | RoomEncrypted
|
|
||||||
| Image | File | Audio
|
|
||||||
| Voice | Video | UnstablePollStart
|
|
||||||
| PollStart | KeyVerificationStart
|
|
||||||
| Reaction | Emote
|
|
||||||
| Location
|
|
||||||
) && services
|
|
||||||
.users
|
|
||||||
.user_is_ignored(&pdu.sender, sender_user)
|
|
||||||
.await
|
|
||||||
{
|
|
||||||
return None;
|
|
||||||
}
|
|
||||||
|
|
||||||
services
|
|
||||||
.rooms
|
|
||||||
.state_accessor
|
|
||||||
.user_can_see_event(sender_user, room_id, &pdu.event_id)
|
|
||||||
.await
|
|
||||||
.then_some((count, pdu))
|
|
||||||
})
|
|
||||||
.collect()
|
.collect()
|
||||||
.await;
|
.await;
|
||||||
|
|
||||||
for (_, event) in &events_before {
|
|
||||||
if !services
|
|
||||||
.rooms
|
|
||||||
.lazy_loading
|
|
||||||
.lazy_load_was_sent_before(sender_user, sender_device, room_id, &event.sender)
|
|
||||||
.await || lazy_load_send_redundant
|
|
||||||
{
|
|
||||||
lazy_loaded.insert(event.sender.as_str().to_owned());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
let start_token = events_before
|
|
||||||
.last()
|
|
||||||
.map_or_else(|| base_token.stringify(), |(count, _)| count.stringify());
|
|
||||||
|
|
||||||
let events_after: Vec<_> = services
|
let events_after: Vec<_> = services
|
||||||
.rooms
|
.rooms
|
||||||
.timeline
|
.timeline
|
||||||
.pdus_after(sender_user, room_id, base_token)
|
.pdus_after(sender_user, room_id, base_token)
|
||||||
.await?
|
.await?
|
||||||
|
.ready_filter_map(|item| event_filter(item, filter))
|
||||||
|
.filter_map(|item| ignored_filter(&services, item, sender_user))
|
||||||
|
.filter_map(|item| visibility_filter(&services, item, sender_user))
|
||||||
.take(limit / 2)
|
.take(limit / 2)
|
||||||
.filter_map(|(count, pdu)| async move {
|
|
||||||
// list of safe and common non-state events to ignore
|
|
||||||
if matches!(
|
|
||||||
&pdu.kind,
|
|
||||||
RoomMessage
|
|
||||||
| Sticker | CallInvite
|
|
||||||
| CallNotify | RoomEncrypted
|
|
||||||
| Image | File | Audio
|
|
||||||
| Voice | Video | UnstablePollStart
|
|
||||||
| PollStart | KeyVerificationStart
|
|
||||||
| Reaction | Emote
|
|
||||||
| Location
|
|
||||||
) && services
|
|
||||||
.users
|
|
||||||
.user_is_ignored(&pdu.sender, sender_user)
|
|
||||||
.await
|
|
||||||
{
|
|
||||||
return None;
|
|
||||||
}
|
|
||||||
|
|
||||||
services
|
|
||||||
.rooms
|
|
||||||
.state_accessor
|
|
||||||
.user_can_see_event(sender_user, room_id, &pdu.event_id)
|
|
||||||
.await
|
|
||||||
.then_some((count, pdu))
|
|
||||||
})
|
|
||||||
.collect()
|
.collect()
|
||||||
.await;
|
.await;
|
||||||
|
|
||||||
for (_, event) in &events_after {
|
let lazy = once(&(base_token, (*base_event).clone()))
|
||||||
if !services
|
.chain(events_before.iter())
|
||||||
.rooms
|
.chain(events_after.iter())
|
||||||
.lazy_loading
|
.stream()
|
||||||
.lazy_load_was_sent_before(sender_user, sender_device, room_id, &event.sender)
|
.fold(LazySet::new(), |lazy, item| {
|
||||||
.await || lazy_load_send_redundant
|
update_lazy(&services, room_id, sender, lazy, item, lazy_load_redundant)
|
||||||
{
|
})
|
||||||
lazy_loaded.insert(event.sender.as_str().to_owned());
|
.await;
|
||||||
}
|
|
||||||
}
|
let state_id = events_after
|
||||||
|
.last()
|
||||||
|
.map_or(body.event_id.as_ref(), |(_, e)| e.event_id.as_ref());
|
||||||
|
|
||||||
let shortstatehash = services
|
let shortstatehash = services
|
||||||
.rooms
|
.rooms
|
||||||
.state_accessor
|
.state_accessor
|
||||||
.pdu_shortstatehash(
|
.pdu_shortstatehash(state_id)
|
||||||
events_after
|
.or_else(|_| services.rooms.state.get_room_shortstatehash(room_id))
|
||||||
.last()
|
|
||||||
.map_or(&*body.event_id, |(_, e)| &*e.event_id),
|
|
||||||
)
|
|
||||||
.await
|
.await
|
||||||
.map_or(
|
.map_err(|e| err!(Database("State hash not found: {e}")))?;
|
||||||
services
|
|
||||||
.rooms
|
|
||||||
.state
|
|
||||||
.get_room_shortstatehash(room_id)
|
|
||||||
.await
|
|
||||||
.expect("All rooms have state"),
|
|
||||||
|hash| hash,
|
|
||||||
);
|
|
||||||
|
|
||||||
let state_ids = services
|
let state_ids = services
|
||||||
.rooms
|
.rooms
|
||||||
|
@ -196,48 +131,61 @@ pub(crate) async fn get_context_route(
|
||||||
.await
|
.await
|
||||||
.map_err(|e| err!(Database("State not found: {e}")))?;
|
.map_err(|e| err!(Database("State not found: {e}")))?;
|
||||||
|
|
||||||
let end_token = events_after
|
let lazy = &lazy;
|
||||||
.last()
|
let state: Vec<_> = state_ids
|
||||||
.map_or_else(|| base_token.stringify(), |(count, _)| count.stringify());
|
.iter()
|
||||||
|
.stream()
|
||||||
let mut state = Vec::with_capacity(state_ids.len());
|
.filter_map(|(shortstatekey, event_id)| {
|
||||||
|
services
|
||||||
for (shortstatekey, id) in state_ids {
|
|
||||||
let (event_type, state_key) = services
|
|
||||||
.rooms
|
.rooms
|
||||||
.short
|
.short
|
||||||
.get_statekey_from_short(shortstatekey)
|
.get_statekey_from_short(*shortstatekey)
|
||||||
.await?;
|
.map_ok(move |(event_type, state_key)| (event_type, state_key, event_id))
|
||||||
|
.ok()
|
||||||
if event_type != StateEventType::RoomMember {
|
})
|
||||||
let Ok(pdu) = services.rooms.timeline.get_pdu(&id).await else {
|
.filter_map(|(event_type, state_key, event_id)| async move {
|
||||||
error!("Pdu in state not found: {id}");
|
if lazy_load_enabled && event_type == StateEventType::RoomMember {
|
||||||
continue;
|
let user_id: &UserId = state_key.as_str().try_into().ok()?;
|
||||||
};
|
if !lazy.contains(user_id) {
|
||||||
|
return None;
|
||||||
state.push(pdu.to_state_event());
|
|
||||||
} else if !lazy_load_enabled || lazy_loaded.contains(&state_key) {
|
|
||||||
let Ok(pdu) = services.rooms.timeline.get_pdu(&id).await else {
|
|
||||||
error!("Pdu in state not found: {id}");
|
|
||||||
continue;
|
|
||||||
};
|
|
||||||
|
|
||||||
state.push(pdu.to_state_event());
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
services
|
||||||
|
.rooms
|
||||||
|
.timeline
|
||||||
|
.get_pdu(event_id)
|
||||||
|
.await
|
||||||
|
.inspect_err(|_| error!("Pdu in state not found: {event_id}"))
|
||||||
|
.map(|pdu| pdu.to_state_event())
|
||||||
|
.ok()
|
||||||
|
})
|
||||||
|
.collect()
|
||||||
|
.await;
|
||||||
|
|
||||||
Ok(get_context::v3::Response {
|
Ok(get_context::v3::Response {
|
||||||
start: Some(start_token),
|
event: Some(base_event.to_room_event()),
|
||||||
end: Some(end_token),
|
|
||||||
|
start: events_before
|
||||||
|
.last()
|
||||||
|
.map_or_else(|| base_token.stringify(), |(count, _)| count.stringify())
|
||||||
|
.into(),
|
||||||
|
|
||||||
|
end: events_after
|
||||||
|
.last()
|
||||||
|
.map_or_else(|| base_token.stringify(), |(count, _)| count.stringify())
|
||||||
|
.into(),
|
||||||
|
|
||||||
events_before: events_before
|
events_before: events_before
|
||||||
.iter()
|
.into_iter()
|
||||||
.map(|(_, pdu)| pdu.to_room_event())
|
.map(|(_, pdu)| pdu.to_room_event())
|
||||||
.collect(),
|
.collect(),
|
||||||
event: Some(base_event),
|
|
||||||
events_after: events_after
|
events_after: events_after
|
||||||
.iter()
|
.into_iter()
|
||||||
.map(|(_, pdu)| pdu.to_room_event())
|
.map(|(_, pdu)| pdu.to_room_event())
|
||||||
.collect(),
|
.collect(),
|
||||||
|
|
||||||
state,
|
state,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,111 +1,52 @@
|
||||||
use std::collections::{BTreeMap, HashSet};
|
use std::collections::HashSet;
|
||||||
|
|
||||||
use axum::extract::State;
|
use axum::extract::State;
|
||||||
use conduit::{
|
use conduit::{
|
||||||
err,
|
at, is_equal_to,
|
||||||
utils::{IterStream, ReadyExt},
|
utils::{
|
||||||
Err, PduCount,
|
result::{FlatOk, LogErr},
|
||||||
|
IterStream, ReadyExt,
|
||||||
|
},
|
||||||
|
Event, PduCount, Result,
|
||||||
};
|
};
|
||||||
use futures::{FutureExt, StreamExt};
|
use futures::{FutureExt, StreamExt};
|
||||||
use ruma::{
|
use ruma::{
|
||||||
api::client::{
|
api::{
|
||||||
filter::RoomEventFilter,
|
client::{filter::RoomEventFilter, message::get_message_events},
|
||||||
message::{get_message_events, send_message_event},
|
Direction,
|
||||||
},
|
},
|
||||||
events::{MessageLikeEventType, StateEventType, TimelineEventType::*},
|
events::{AnyStateEvent, StateEventType, TimelineEventType, TimelineEventType::*},
|
||||||
UserId,
|
serde::Raw,
|
||||||
|
DeviceId, OwnedUserId, RoomId, UserId,
|
||||||
};
|
};
|
||||||
use serde_json::from_str;
|
use service::{rooms::timeline::PdusIterItem, Services};
|
||||||
use service::rooms::timeline::PdusIterItem;
|
|
||||||
|
|
||||||
use crate::{
|
use crate::Ruma;
|
||||||
service::{pdu::PduBuilder, Services},
|
|
||||||
utils, Result, Ruma,
|
|
||||||
};
|
|
||||||
|
|
||||||
/// # `PUT /_matrix/client/v3/rooms/{roomId}/send/{eventType}/{txnId}`
|
pub(crate) type LazySet = HashSet<OwnedUserId>;
|
||||||
///
|
|
||||||
/// Send a message event into the room.
|
|
||||||
///
|
|
||||||
/// - Is a NOOP if the txn id was already used before and returns the same event
|
|
||||||
/// id again
|
|
||||||
/// - The only requirement for the content is that it has to be valid json
|
|
||||||
/// - Tries to send the event into the room, auth rules will determine if it is
|
|
||||||
/// allowed
|
|
||||||
pub(crate) async fn send_message_event_route(
|
|
||||||
State(services): State<crate::State>, body: Ruma<send_message_event::v3::Request>,
|
|
||||||
) -> Result<send_message_event::v3::Response> {
|
|
||||||
let sender_user = body.sender_user.as_deref().expect("user is authenticated");
|
|
||||||
let sender_device = body.sender_device.as_deref();
|
|
||||||
let appservice_info = body.appservice_info.as_ref();
|
|
||||||
|
|
||||||
// Forbid m.room.encrypted if encryption is disabled
|
/// list of safe and common non-state events to ignore
|
||||||
if MessageLikeEventType::RoomEncrypted == body.event_type && !services.globals.allow_encryption() {
|
const IGNORED_MESSAGE_TYPES: &[TimelineEventType] = &[
|
||||||
return Err!(Request(Forbidden("Encryption has been disabled")));
|
RoomMessage,
|
||||||
}
|
Sticker,
|
||||||
|
CallInvite,
|
||||||
|
CallNotify,
|
||||||
|
RoomEncrypted,
|
||||||
|
Image,
|
||||||
|
File,
|
||||||
|
Audio,
|
||||||
|
Voice,
|
||||||
|
Video,
|
||||||
|
UnstablePollStart,
|
||||||
|
PollStart,
|
||||||
|
KeyVerificationStart,
|
||||||
|
Reaction,
|
||||||
|
Emote,
|
||||||
|
Location,
|
||||||
|
];
|
||||||
|
|
||||||
let state_lock = services.rooms.state.mutex.lock(&body.room_id).await;
|
const LIMIT_MAX: usize = 100;
|
||||||
|
const LIMIT_DEFAULT: usize = 10;
|
||||||
if body.event_type == MessageLikeEventType::CallInvite
|
|
||||||
&& services.rooms.directory.is_public_room(&body.room_id).await
|
|
||||||
{
|
|
||||||
return Err!(Request(Forbidden("Room call invites are not allowed in public rooms")));
|
|
||||||
}
|
|
||||||
|
|
||||||
// Check if this is a new transaction id
|
|
||||||
if let Ok(response) = services
|
|
||||||
.transaction_ids
|
|
||||||
.existing_txnid(sender_user, sender_device, &body.txn_id)
|
|
||||||
.await
|
|
||||||
{
|
|
||||||
// The client might have sent a txnid of the /sendToDevice endpoint
|
|
||||||
// This txnid has no response associated with it
|
|
||||||
if response.is_empty() {
|
|
||||||
return Err!(Request(InvalidParam(
|
|
||||||
"Tried to use txn id already used for an incompatible endpoint."
|
|
||||||
)));
|
|
||||||
}
|
|
||||||
|
|
||||||
return Ok(send_message_event::v3::Response {
|
|
||||||
event_id: utils::string_from_bytes(&response)
|
|
||||||
.map(TryInto::try_into)
|
|
||||||
.map_err(|e| err!(Database("Invalid event_id in txnid data: {e:?}")))??,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
let mut unsigned = BTreeMap::new();
|
|
||||||
unsigned.insert("transaction_id".to_owned(), body.txn_id.to_string().into());
|
|
||||||
|
|
||||||
let content =
|
|
||||||
from_str(body.body.body.json().get()).map_err(|e| err!(Request(BadJson("Invalid JSON body: {e}"))))?;
|
|
||||||
|
|
||||||
let event_id = services
|
|
||||||
.rooms
|
|
||||||
.timeline
|
|
||||||
.build_and_append_pdu(
|
|
||||||
PduBuilder {
|
|
||||||
event_type: body.event_type.clone().into(),
|
|
||||||
content,
|
|
||||||
unsigned: Some(unsigned),
|
|
||||||
timestamp: appservice_info.and(body.timestamp),
|
|
||||||
..Default::default()
|
|
||||||
},
|
|
||||||
sender_user,
|
|
||||||
&body.room_id,
|
|
||||||
&state_lock,
|
|
||||||
)
|
|
||||||
.await?;
|
|
||||||
|
|
||||||
services
|
|
||||||
.transaction_ids
|
|
||||||
.add_txnid(sender_user, sender_device, &body.txn_id, event_id.as_bytes());
|
|
||||||
|
|
||||||
drop(state_lock);
|
|
||||||
|
|
||||||
Ok(send_message_event::v3::Response {
|
|
||||||
event_id: event_id.into(),
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
/// # `GET /_matrix/client/r0/rooms/{roomId}/messages`
|
/// # `GET /_matrix/client/r0/rooms/{roomId}/messages`
|
||||||
///
|
///
|
||||||
|
@ -116,209 +57,171 @@ pub(crate) async fn send_message_event_route(
|
||||||
pub(crate) async fn get_message_events_route(
|
pub(crate) async fn get_message_events_route(
|
||||||
State(services): State<crate::State>, body: Ruma<get_message_events::v3::Request>,
|
State(services): State<crate::State>, body: Ruma<get_message_events::v3::Request>,
|
||||||
) -> Result<get_message_events::v3::Response> {
|
) -> Result<get_message_events::v3::Response> {
|
||||||
let sender_user = body.sender_user.as_ref().expect("user is authenticated");
|
let sender = body.sender();
|
||||||
let sender_device = body.sender_device.as_ref().expect("user is authenticated");
|
let (sender_user, sender_device) = sender;
|
||||||
|
|
||||||
let room_id = &body.room_id;
|
let room_id = &body.room_id;
|
||||||
let filter = &body.filter;
|
let filter = &body.filter;
|
||||||
|
|
||||||
let limit = usize::try_from(body.limit).unwrap_or(10).min(100);
|
let from_default = match body.dir {
|
||||||
let from = match body.from.as_ref() {
|
Direction::Forward => PduCount::min(),
|
||||||
Some(from) => PduCount::try_from_string(from)?,
|
Direction::Backward => PduCount::max(),
|
||||||
None => match body.dir {
|
|
||||||
ruma::api::Direction::Forward => PduCount::min(),
|
|
||||||
ruma::api::Direction::Backward => PduCount::max(),
|
|
||||||
},
|
|
||||||
};
|
};
|
||||||
|
|
||||||
let to = body
|
let from = body
|
||||||
.to
|
.from
|
||||||
.as_ref()
|
.as_deref()
|
||||||
.and_then(|t| PduCount::try_from_string(t).ok());
|
.map(PduCount::try_from_string)
|
||||||
|
.transpose()?
|
||||||
|
.unwrap_or(from_default);
|
||||||
|
|
||||||
|
let to = body.to.as_deref().map(PduCount::try_from_string).flat_ok();
|
||||||
|
|
||||||
|
let limit: usize = body
|
||||||
|
.limit
|
||||||
|
.try_into()
|
||||||
|
.unwrap_or(LIMIT_DEFAULT)
|
||||||
|
.min(LIMIT_MAX);
|
||||||
|
|
||||||
services
|
services
|
||||||
.rooms
|
.rooms
|
||||||
.lazy_loading
|
.lazy_loading
|
||||||
.lazy_load_confirm_delivery(sender_user, sender_device, room_id, from);
|
.lazy_load_confirm_delivery(sender_user, sender_device, room_id, from);
|
||||||
|
|
||||||
let mut resp = get_message_events::v3::Response::new();
|
if matches!(body.dir, Direction::Backward) {
|
||||||
let mut lazy_loaded = HashSet::new();
|
|
||||||
let next_token;
|
|
||||||
match body.dir {
|
|
||||||
ruma::api::Direction::Forward => {
|
|
||||||
let events_after: Vec<PdusIterItem> = services
|
|
||||||
.rooms
|
|
||||||
.timeline
|
|
||||||
.pdus_after(sender_user, room_id, from)
|
|
||||||
.await?
|
|
||||||
.ready_filter_map(|item| event_filter(item, filter))
|
|
||||||
.filter_map(|item| visibility_filter(&services, item, sender_user))
|
|
||||||
.ready_take_while(|(count, _)| Some(*count) != to) // Stop at `to`
|
|
||||||
.take(limit)
|
|
||||||
.collect()
|
|
||||||
.boxed()
|
|
||||||
.await;
|
|
||||||
|
|
||||||
for (_, event) in &events_after {
|
|
||||||
/* TODO: Remove the not "element_hacks" check when these are resolved:
|
|
||||||
* https://github.com/vector-im/element-android/issues/3417
|
|
||||||
* https://github.com/vector-im/element-web/issues/21034
|
|
||||||
*/
|
|
||||||
if !cfg!(feature = "element_hacks")
|
|
||||||
&& !services
|
|
||||||
.rooms
|
|
||||||
.lazy_loading
|
|
||||||
.lazy_load_was_sent_before(sender_user, sender_device, room_id, &event.sender)
|
|
||||||
.await
|
|
||||||
{
|
|
||||||
lazy_loaded.insert(event.sender.clone());
|
|
||||||
}
|
|
||||||
|
|
||||||
if cfg!(features = "element_hacks") {
|
|
||||||
lazy_loaded.insert(event.sender.clone());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
next_token = events_after.last().map(|(count, _)| count).copied();
|
|
||||||
|
|
||||||
let events_after: Vec<_> = events_after
|
|
||||||
.into_iter()
|
|
||||||
.stream()
|
|
||||||
.filter_map(|(_, pdu)| async move {
|
|
||||||
// list of safe and common non-state events to ignore
|
|
||||||
if matches!(
|
|
||||||
&pdu.kind,
|
|
||||||
RoomMessage
|
|
||||||
| Sticker | CallInvite
|
|
||||||
| CallNotify | RoomEncrypted
|
|
||||||
| Image | File | Audio
|
|
||||||
| Voice | Video | UnstablePollStart
|
|
||||||
| PollStart | KeyVerificationStart
|
|
||||||
| Reaction | Emote | Location
|
|
||||||
) && services
|
|
||||||
.users
|
|
||||||
.user_is_ignored(&pdu.sender, sender_user)
|
|
||||||
.await
|
|
||||||
{
|
|
||||||
return None;
|
|
||||||
}
|
|
||||||
|
|
||||||
Some(pdu.to_room_event())
|
|
||||||
})
|
|
||||||
.collect()
|
|
||||||
.await;
|
|
||||||
|
|
||||||
resp.start = from.stringify();
|
|
||||||
resp.end = next_token.map(|count| count.stringify());
|
|
||||||
resp.chunk = events_after;
|
|
||||||
},
|
|
||||||
ruma::api::Direction::Backward => {
|
|
||||||
services
|
services
|
||||||
.rooms
|
.rooms
|
||||||
.timeline
|
.timeline
|
||||||
.backfill_if_required(room_id, from)
|
.backfill_if_required(room_id, from)
|
||||||
.boxed()
|
.boxed()
|
||||||
.await?;
|
.await
|
||||||
|
.log_err()
|
||||||
|
.ok();
|
||||||
|
}
|
||||||
|
|
||||||
let events_before: Vec<PdusIterItem> = services
|
let it = match body.dir {
|
||||||
|
Direction::Forward => services
|
||||||
|
.rooms
|
||||||
|
.timeline
|
||||||
|
.pdus_after(sender_user, room_id, from)
|
||||||
|
.await?
|
||||||
|
.boxed(),
|
||||||
|
|
||||||
|
Direction::Backward => services
|
||||||
.rooms
|
.rooms
|
||||||
.timeline
|
.timeline
|
||||||
.pdus_until(sender_user, room_id, from)
|
.pdus_until(sender_user, room_id, from)
|
||||||
.await?
|
.await?
|
||||||
.ready_filter_map(|item| event_filter(item, filter))
|
.boxed(),
|
||||||
.filter_map(|(count, pdu)| async move {
|
};
|
||||||
// list of safe and common non-state events to ignore
|
|
||||||
if matches!(
|
|
||||||
&pdu.kind,
|
|
||||||
RoomMessage
|
|
||||||
| Sticker | CallInvite
|
|
||||||
| CallNotify | RoomEncrypted
|
|
||||||
| Image | File | Audio
|
|
||||||
| Voice | Video | UnstablePollStart
|
|
||||||
| PollStart | KeyVerificationStart
|
|
||||||
| Reaction | Emote | Location
|
|
||||||
) && services
|
|
||||||
.users
|
|
||||||
.user_is_ignored(&pdu.sender, sender_user)
|
|
||||||
.await
|
|
||||||
{
|
|
||||||
return None;
|
|
||||||
}
|
|
||||||
|
|
||||||
Some((count, pdu))
|
let events: Vec<_> = it
|
||||||
})
|
.ready_take_while(|(count, _)| Some(*count) != to)
|
||||||
|
.ready_filter_map(|item| event_filter(item, filter))
|
||||||
|
.filter_map(|item| ignored_filter(&services, item, sender_user))
|
||||||
.filter_map(|item| visibility_filter(&services, item, sender_user))
|
.filter_map(|item| visibility_filter(&services, item, sender_user))
|
||||||
.ready_take_while(|(count, _)| Some(*count) != to) // Stop at `to`
|
|
||||||
.take(limit)
|
.take(limit)
|
||||||
.collect()
|
.collect()
|
||||||
.boxed()
|
|
||||||
.await;
|
.await;
|
||||||
|
|
||||||
for (_, event) in &events_before {
|
let lazy = events
|
||||||
|
.iter()
|
||||||
|
.stream()
|
||||||
|
.fold(LazySet::new(), |lazy, item| {
|
||||||
|
update_lazy(&services, room_id, sender, lazy, item, false)
|
||||||
|
})
|
||||||
|
.await;
|
||||||
|
|
||||||
|
let state = lazy
|
||||||
|
.iter()
|
||||||
|
.stream()
|
||||||
|
.filter_map(|user_id| get_member_event(&services, room_id, user_id))
|
||||||
|
.collect()
|
||||||
|
.await;
|
||||||
|
|
||||||
|
let next_token = events.last().map(|(count, _)| count).copied();
|
||||||
|
|
||||||
|
if !cfg!(feature = "element_hacks") {
|
||||||
|
if let Some(next_token) = next_token {
|
||||||
|
services
|
||||||
|
.rooms
|
||||||
|
.lazy_loading
|
||||||
|
.lazy_load_mark_sent(sender_user, sender_device, room_id, lazy, next_token);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
let chunk = events
|
||||||
|
.into_iter()
|
||||||
|
.map(at!(1))
|
||||||
|
.map(|pdu| pdu.to_room_event())
|
||||||
|
.collect();
|
||||||
|
|
||||||
|
Ok(get_message_events::v3::Response {
|
||||||
|
start: from.stringify(),
|
||||||
|
end: next_token.as_ref().map(PduCount::stringify),
|
||||||
|
chunk,
|
||||||
|
state,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn get_member_event(services: &Services, room_id: &RoomId, user_id: &UserId) -> Option<Raw<AnyStateEvent>> {
|
||||||
|
services
|
||||||
|
.rooms
|
||||||
|
.state_accessor
|
||||||
|
.room_state_get(room_id, &StateEventType::RoomMember, user_id.as_str())
|
||||||
|
.await
|
||||||
|
.map(|member_event| member_event.to_state_event())
|
||||||
|
.ok()
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(crate) async fn update_lazy(
|
||||||
|
services: &Services, room_id: &RoomId, sender: (&UserId, &DeviceId), mut lazy: LazySet, item: &PdusIterItem,
|
||||||
|
force: bool,
|
||||||
|
) -> LazySet {
|
||||||
|
let (_, event) = &item;
|
||||||
|
let (sender_user, sender_device) = sender;
|
||||||
|
|
||||||
/* TODO: Remove the not "element_hacks" check when these are resolved:
|
/* TODO: Remove the not "element_hacks" check when these are resolved:
|
||||||
* https://github.com/vector-im/element-android/issues/3417
|
* https://github.com/vector-im/element-android/issues/3417
|
||||||
* https://github.com/vector-im/element-web/issues/21034
|
* https://github.com/vector-im/element-web/issues/21034
|
||||||
*/
|
*/
|
||||||
if !cfg!(feature = "element_hacks")
|
if force || cfg!(features = "element_hacks") {
|
||||||
&& !services
|
lazy.insert(event.sender().into());
|
||||||
|
return lazy;
|
||||||
|
}
|
||||||
|
|
||||||
|
if !services
|
||||||
.rooms
|
.rooms
|
||||||
.lazy_loading
|
.lazy_loading
|
||||||
.lazy_load_was_sent_before(sender_user, sender_device, room_id, &event.sender)
|
.lazy_load_was_sent_before(sender_user, sender_device, room_id, event.sender())
|
||||||
.await
|
.await
|
||||||
{
|
{
|
||||||
lazy_loaded.insert(event.sender.clone());
|
lazy.insert(event.sender().into());
|
||||||
}
|
}
|
||||||
|
|
||||||
if cfg!(features = "element_hacks") {
|
lazy
|
||||||
lazy_loaded.insert(event.sender.clone());
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
next_token = events_before.last().map(|(count, _)| count).copied();
|
pub(crate) async fn ignored_filter(services: &Services, item: PdusIterItem, user_id: &UserId) -> Option<PdusIterItem> {
|
||||||
|
let (_, pdu) = &item;
|
||||||
|
|
||||||
let events_before: Vec<_> = events_before
|
if pdu.kind.to_cow_str() == "org.matrix.dummy_event" {
|
||||||
.into_iter()
|
return None;
|
||||||
.map(|(_, pdu)| pdu.to_room_event())
|
|
||||||
.collect();
|
|
||||||
|
|
||||||
resp.start = from.stringify();
|
|
||||||
resp.end = next_token.map(|count| count.stringify());
|
|
||||||
resp.chunk = events_before;
|
|
||||||
},
|
|
||||||
}
|
}
|
||||||
|
|
||||||
resp.state = lazy_loaded
|
if !IGNORED_MESSAGE_TYPES.iter().any(is_equal_to!(&pdu.kind)) {
|
||||||
.iter()
|
return Some(item);
|
||||||
.stream()
|
|
||||||
.filter_map(|ll_user_id| async move {
|
|
||||||
services
|
|
||||||
.rooms
|
|
||||||
.state_accessor
|
|
||||||
.room_state_get(room_id, &StateEventType::RoomMember, ll_user_id.as_str())
|
|
||||||
.await
|
|
||||||
.map(|member_event| member_event.to_state_event())
|
|
||||||
.ok()
|
|
||||||
})
|
|
||||||
.collect()
|
|
||||||
.await;
|
|
||||||
|
|
||||||
// remove the feature check when we are sure clients like element can handle it
|
|
||||||
if !cfg!(feature = "element_hacks") {
|
|
||||||
if let Some(next_token) = next_token {
|
|
||||||
services.rooms.lazy_loading.lazy_load_mark_sent(
|
|
||||||
sender_user,
|
|
||||||
sender_device,
|
|
||||||
room_id,
|
|
||||||
lazy_loaded,
|
|
||||||
next_token,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(resp)
|
if !services.users.user_is_ignored(&pdu.sender, user_id).await {
|
||||||
|
return Some(item);
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn visibility_filter(services: &Services, item: PdusIterItem, user_id: &UserId) -> Option<PdusIterItem> {
|
None
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(crate) async fn visibility_filter(
|
||||||
|
services: &Services, item: PdusIterItem, user_id: &UserId,
|
||||||
|
) -> Option<PdusIterItem> {
|
||||||
let (_, pdu) = &item;
|
let (_, pdu) = &item;
|
||||||
|
|
||||||
services
|
services
|
||||||
|
@ -329,7 +232,7 @@ async fn visibility_filter(services: &Services, item: PdusIterItem, user_id: &Us
|
||||||
.then_some(item)
|
.then_some(item)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn event_filter(item: PdusIterItem, filter: &RoomEventFilter) -> Option<PdusIterItem> {
|
pub(crate) fn event_filter(item: PdusIterItem, filter: &RoomEventFilter) -> Option<PdusIterItem> {
|
||||||
let (_, pdu) = &item;
|
let (_, pdu) = &item;
|
||||||
pdu.matches(filter).then_some(item)
|
pdu.matches(filter).then_some(item)
|
||||||
}
|
}
|
||||||
|
|
|
@ -23,6 +23,7 @@ pub(super) mod relations;
|
||||||
pub(super) mod report;
|
pub(super) mod report;
|
||||||
pub(super) mod room;
|
pub(super) mod room;
|
||||||
pub(super) mod search;
|
pub(super) mod search;
|
||||||
|
pub(super) mod send;
|
||||||
pub(super) mod session;
|
pub(super) mod session;
|
||||||
pub(super) mod space;
|
pub(super) mod space;
|
||||||
pub(super) mod state;
|
pub(super) mod state;
|
||||||
|
@ -65,6 +66,7 @@ pub(super) use relations::*;
|
||||||
pub(super) use report::*;
|
pub(super) use report::*;
|
||||||
pub(super) use room::*;
|
pub(super) use room::*;
|
||||||
pub(super) use search::*;
|
pub(super) use search::*;
|
||||||
|
pub(super) use send::*;
|
||||||
pub(super) use session::*;
|
pub(super) use session::*;
|
||||||
pub(super) use space::*;
|
pub(super) use space::*;
|
||||||
pub(super) use state::*;
|
pub(super) use state::*;
|
||||||
|
|
92
src/api/client/send.rs
Normal file
92
src/api/client/send.rs
Normal file
|
@ -0,0 +1,92 @@
|
||||||
|
use std::collections::BTreeMap;
|
||||||
|
|
||||||
|
use axum::extract::State;
|
||||||
|
use conduit::{err, Err};
|
||||||
|
use ruma::{api::client::message::send_message_event, events::MessageLikeEventType};
|
||||||
|
use serde_json::from_str;
|
||||||
|
|
||||||
|
use crate::{service::pdu::PduBuilder, utils, Result, Ruma};
|
||||||
|
|
||||||
|
/// # `PUT /_matrix/client/v3/rooms/{roomId}/send/{eventType}/{txnId}`
|
||||||
|
///
|
||||||
|
/// Send a message event into the room.
|
||||||
|
///
|
||||||
|
/// - Is a NOOP if the txn id was already used before and returns the same event
|
||||||
|
/// id again
|
||||||
|
/// - The only requirement for the content is that it has to be valid json
|
||||||
|
/// - Tries to send the event into the room, auth rules will determine if it is
|
||||||
|
/// allowed
|
||||||
|
pub(crate) async fn send_message_event_route(
|
||||||
|
State(services): State<crate::State>, body: Ruma<send_message_event::v3::Request>,
|
||||||
|
) -> Result<send_message_event::v3::Response> {
|
||||||
|
let sender_user = body.sender_user();
|
||||||
|
let sender_device = body.sender_device.as_deref();
|
||||||
|
let appservice_info = body.appservice_info.as_ref();
|
||||||
|
|
||||||
|
// Forbid m.room.encrypted if encryption is disabled
|
||||||
|
if MessageLikeEventType::RoomEncrypted == body.event_type && !services.globals.allow_encryption() {
|
||||||
|
return Err!(Request(Forbidden("Encryption has been disabled")));
|
||||||
|
}
|
||||||
|
|
||||||
|
let state_lock = services.rooms.state.mutex.lock(&body.room_id).await;
|
||||||
|
|
||||||
|
if body.event_type == MessageLikeEventType::CallInvite
|
||||||
|
&& services.rooms.directory.is_public_room(&body.room_id).await
|
||||||
|
{
|
||||||
|
return Err!(Request(Forbidden("Room call invites are not allowed in public rooms")));
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check if this is a new transaction id
|
||||||
|
if let Ok(response) = services
|
||||||
|
.transaction_ids
|
||||||
|
.existing_txnid(sender_user, sender_device, &body.txn_id)
|
||||||
|
.await
|
||||||
|
{
|
||||||
|
// The client might have sent a txnid of the /sendToDevice endpoint
|
||||||
|
// This txnid has no response associated with it
|
||||||
|
if response.is_empty() {
|
||||||
|
return Err!(Request(InvalidParam(
|
||||||
|
"Tried to use txn id already used for an incompatible endpoint."
|
||||||
|
)));
|
||||||
|
}
|
||||||
|
|
||||||
|
return Ok(send_message_event::v3::Response {
|
||||||
|
event_id: utils::string_from_bytes(&response)
|
||||||
|
.map(TryInto::try_into)
|
||||||
|
.map_err(|e| err!(Database("Invalid event_id in txnid data: {e:?}")))??,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
let mut unsigned = BTreeMap::new();
|
||||||
|
unsigned.insert("transaction_id".to_owned(), body.txn_id.to_string().into());
|
||||||
|
|
||||||
|
let content =
|
||||||
|
from_str(body.body.body.json().get()).map_err(|e| err!(Request(BadJson("Invalid JSON body: {e}"))))?;
|
||||||
|
|
||||||
|
let event_id = services
|
||||||
|
.rooms
|
||||||
|
.timeline
|
||||||
|
.build_and_append_pdu(
|
||||||
|
PduBuilder {
|
||||||
|
event_type: body.event_type.clone().into(),
|
||||||
|
content,
|
||||||
|
unsigned: Some(unsigned),
|
||||||
|
timestamp: appservice_info.and(body.timestamp),
|
||||||
|
..Default::default()
|
||||||
|
},
|
||||||
|
sender_user,
|
||||||
|
&body.room_id,
|
||||||
|
&state_lock,
|
||||||
|
)
|
||||||
|
.await?;
|
||||||
|
|
||||||
|
services
|
||||||
|
.transaction_ids
|
||||||
|
.add_txnid(sender_user, sender_device, &body.txn_id, event_id.as_bytes());
|
||||||
|
|
||||||
|
drop(state_lock);
|
||||||
|
|
||||||
|
Ok(send_message_event::v3::Response {
|
||||||
|
event_id: event_id.into(),
|
||||||
|
})
|
||||||
|
}
|
|
@ -3,7 +3,9 @@ use std::{mem, ops::Deref};
|
||||||
use axum::{async_trait, body::Body, extract::FromRequest};
|
use axum::{async_trait, body::Body, extract::FromRequest};
|
||||||
use bytes::{BufMut, BytesMut};
|
use bytes::{BufMut, BytesMut};
|
||||||
use conduit::{debug, err, trace, utils::string::EMPTY, Error, Result};
|
use conduit::{debug, err, trace, utils::string::EMPTY, Error, Result};
|
||||||
use ruma::{api::IncomingRequest, CanonicalJsonValue, OwnedDeviceId, OwnedServerName, OwnedUserId, ServerName, UserId};
|
use ruma::{
|
||||||
|
api::IncomingRequest, CanonicalJsonValue, DeviceId, OwnedDeviceId, OwnedServerName, OwnedUserId, ServerName, UserId,
|
||||||
|
};
|
||||||
use service::Services;
|
use service::Services;
|
||||||
|
|
||||||
use super::{auth, auth::Auth, request, request::Request};
|
use super::{auth, auth::Auth, request, request::Request};
|
||||||
|
@ -40,10 +42,28 @@ where
|
||||||
T: IncomingRequest + Send + Sync + 'static,
|
T: IncomingRequest + Send + Sync + 'static,
|
||||||
{
|
{
|
||||||
#[inline]
|
#[inline]
|
||||||
pub(crate) fn sender_user(&self) -> &UserId { self.sender_user.as_deref().expect("user is authenticated") }
|
pub(crate) fn sender(&self) -> (&UserId, &DeviceId) { (self.sender_user(), self.sender_device()) }
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
pub(crate) fn origin(&self) -> &ServerName { self.origin.as_deref().expect("server is authenticated") }
|
pub(crate) fn sender_user(&self) -> &UserId {
|
||||||
|
self.sender_user
|
||||||
|
.as_deref()
|
||||||
|
.expect("user must be authenticated for this handler")
|
||||||
|
}
|
||||||
|
|
||||||
|
#[inline]
|
||||||
|
pub(crate) fn sender_device(&self) -> &DeviceId {
|
||||||
|
self.sender_device
|
||||||
|
.as_deref()
|
||||||
|
.expect("user must be authenticated and device identified")
|
||||||
|
}
|
||||||
|
|
||||||
|
#[inline]
|
||||||
|
pub(crate) fn origin(&self) -> &ServerName {
|
||||||
|
self.origin
|
||||||
|
.as_deref()
|
||||||
|
.expect("server must be authenticated for this handler")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[async_trait]
|
#[async_trait]
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue