2024-06-14 22:08:44 +00:00
|
|
|
use std::collections::{BTreeMap, HashSet};
|
2024-03-05 19:48:54 -05:00
|
|
|
|
2024-07-16 08:05:25 +00:00
|
|
|
use axum::extract::State;
|
2024-08-08 17:18:30 +00:00
|
|
|
use conduit::{err, utils::ReadyExt, Err, PduCount};
|
|
|
|
use futures::{FutureExt, StreamExt};
|
2020-08-06 08:29:59 -04:00
|
|
|
use ruma::{
|
|
|
|
api::client::{
|
|
|
|
error::ErrorKind,
|
2024-04-05 06:34:56 -07:00
|
|
|
filter::{RoomEventFilter, UrlFilter},
|
2022-02-18 15:33:14 +01:00
|
|
|
message::{get_message_events, send_message_event},
|
2020-08-06 08:29:59 -04:00
|
|
|
},
|
2024-03-31 00:22:12 -04:00
|
|
|
events::{MessageLikeEventType, StateEventType},
|
2024-08-08 17:18:30 +00:00
|
|
|
UserId,
|
2020-12-31 08:40:49 -05:00
|
|
|
};
|
2024-04-05 06:34:56 -07:00
|
|
|
use serde_json::{from_str, Value};
|
2024-08-08 17:18:30 +00:00
|
|
|
use service::rooms::timeline::PdusIterItem;
|
2024-03-05 19:48:54 -05:00
|
|
|
|
2024-07-16 08:05:25 +00:00
|
|
|
use crate::{
|
|
|
|
service::{pdu::PduBuilder, Services},
|
2024-08-08 17:18:30 +00:00
|
|
|
utils, Error, Result, Ruma,
|
2024-07-16 08:05:25 +00:00
|
|
|
};
|
2020-07-30 18:14:47 +02:00
|
|
|
|
2024-01-17 19:54:17 -05:00
|
|
|
/// # `PUT /_matrix/client/v3/rooms/{roomId}/send/{eventType}/{txnId}`
|
2021-08-31 19:14:37 +02:00
|
|
|
///
|
|
|
|
/// 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
|
2024-04-22 23:48:57 -04:00
|
|
|
pub(crate) async fn send_message_event_route(
|
2024-07-16 08:05:25 +00:00
|
|
|
State(services): State<crate::State>, body: Ruma<send_message_event::v3::Request>,
|
2022-02-18 15:33:14 +01:00
|
|
|
) -> Result<send_message_event::v3::Response> {
|
2024-08-08 17:18:30 +00:00
|
|
|
let sender_user = body.sender_user.as_deref().expect("user is authenticated");
|
2020-12-08 10:33:44 +01:00
|
|
|
let sender_device = body.sender_device.as_deref();
|
2024-08-08 17:18:30 +00:00
|
|
|
let appservice_info = body.appservice_info.as_ref();
|
2024-03-05 19:48:54 -05:00
|
|
|
|
2021-09-24 22:44:26 +00:00
|
|
|
// Forbid m.room.encrypted if encryption is disabled
|
2024-07-16 08:05:25 +00:00
|
|
|
if MessageLikeEventType::RoomEncrypted == body.event_type && !services.globals.allow_encryption() {
|
2024-08-08 17:18:30 +00:00
|
|
|
return Err!(Request(Forbidden("Encryption has been disabled")));
|
2021-09-24 22:44:26 +00:00
|
|
|
}
|
2024-03-05 19:48:54 -05:00
|
|
|
|
2024-08-08 17:18:30 +00:00
|
|
|
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")));
|
2024-03-31 00:22:12 -04:00
|
|
|
}
|
2024-03-05 19:48:54 -05:00
|
|
|
|
2020-08-25 13:24:38 +02:00
|
|
|
// Check if this is a new transaction id
|
2024-08-08 17:18:30 +00:00
|
|
|
if let Ok(response) = services
|
2024-03-25 17:05:11 -04:00
|
|
|
.transaction_ids
|
2024-08-08 17:18:30 +00:00
|
|
|
.existing_txnid(sender_user, sender_device, &body.txn_id)
|
|
|
|
.await
|
2024-03-25 17:05:11 -04:00
|
|
|
{
|
2020-08-25 13:24:38 +02:00
|
|
|
// The client might have sent a txnid of the /sendToDevice endpoint
|
|
|
|
// This txnid has no response associated with it
|
|
|
|
if response.is_empty() {
|
2024-08-08 17:18:30 +00:00
|
|
|
return Err!(Request(InvalidParam(
|
|
|
|
"Tried to use txn id already used for an incompatible endpoint."
|
|
|
|
)));
|
2020-08-25 13:24:38 +02:00
|
|
|
}
|
2024-03-05 19:48:54 -05:00
|
|
|
|
2022-02-18 15:33:14 +01:00
|
|
|
return Ok(send_message_event::v3::Response {
|
2024-08-08 17:18:30 +00:00
|
|
|
event_id: utils::string_from_bytes(&response)
|
|
|
|
.map(TryInto::try_into)
|
|
|
|
.map_err(|e| err!(Database("Invalid event_id in txnid data: {e:?}")))??,
|
2022-02-18 15:33:14 +01:00
|
|
|
});
|
2020-08-25 13:24:38 +02:00
|
|
|
}
|
2024-03-05 19:48:54 -05:00
|
|
|
|
2020-12-31 08:40:49 -05:00
|
|
|
let mut unsigned = BTreeMap::new();
|
2022-01-17 14:35:38 +01:00
|
|
|
unsigned.insert("transaction_id".to_owned(), body.txn_id.to_string().into());
|
2024-03-05 19:48:54 -05:00
|
|
|
|
2024-08-08 17:18:30 +00:00
|
|
|
let content = from_str(body.body.body.json().get())
|
|
|
|
.map_err(|_| Error::BadRequest(ErrorKind::BadJson, "Invalid JSON body."))?;
|
|
|
|
|
2024-07-16 08:05:25 +00:00
|
|
|
let event_id = services
|
2023-11-25 18:29:38 -05:00
|
|
|
.rooms
|
|
|
|
.timeline
|
|
|
|
.build_and_append_pdu(
|
|
|
|
PduBuilder {
|
|
|
|
event_type: body.event_type.to_string().into(),
|
2024-08-08 17:18:30 +00:00
|
|
|
content,
|
2023-11-25 18:29:38 -05:00
|
|
|
unsigned: Some(unsigned),
|
|
|
|
state_key: None,
|
|
|
|
redacts: None,
|
2024-08-08 17:18:30 +00:00
|
|
|
timestamp: appservice_info.and(body.timestamp),
|
2023-11-25 18:29:38 -05:00
|
|
|
},
|
|
|
|
sender_user,
|
|
|
|
&body.room_id,
|
|
|
|
&state_lock,
|
|
|
|
)
|
2024-08-08 17:18:30 +00:00
|
|
|
.await
|
|
|
|
.map(|event_id| (*event_id).to_owned())?;
|
2024-03-05 19:48:54 -05:00
|
|
|
|
2024-07-16 08:05:25 +00:00
|
|
|
services
|
2024-03-25 17:05:11 -04:00
|
|
|
.transaction_ids
|
2024-08-08 17:18:30 +00:00
|
|
|
.add_txnid(sender_user, sender_device, &body.txn_id, event_id.as_bytes());
|
2024-03-05 19:48:54 -05:00
|
|
|
|
2021-08-03 11:10:58 +02:00
|
|
|
drop(state_lock);
|
2024-03-05 19:48:54 -05:00
|
|
|
|
2024-08-08 17:18:30 +00:00
|
|
|
Ok(send_message_event::v3::Response {
|
|
|
|
event_id,
|
|
|
|
})
|
2020-07-30 18:14:47 +02:00
|
|
|
}
|
|
|
|
|
2021-08-31 19:14:37 +02:00
|
|
|
/// # `GET /_matrix/client/r0/rooms/{roomId}/messages`
|
|
|
|
///
|
|
|
|
/// Allows paginating through room history.
|
|
|
|
///
|
|
|
|
/// - Only works if the user is joined (TODO: always allow, but only show events
|
2024-06-16 00:36:49 +00:00
|
|
|
/// where the user was joined, depending on `history_visibility`)
|
2024-04-22 23:48:57 -04:00
|
|
|
pub(crate) async fn get_message_events_route(
|
2024-07-16 08:05:25 +00:00
|
|
|
State(services): State<crate::State>, body: Ruma<get_message_events::v3::Request>,
|
2022-02-18 15:33:14 +01:00
|
|
|
) -> Result<get_message_events::v3::Response> {
|
2020-10-18 20:33:12 +02:00
|
|
|
let sender_user = body.sender_user.as_ref().expect("user is authenticated");
|
2022-01-04 14:30:13 +01:00
|
|
|
let sender_device = body.sender_device.as_ref().expect("user is authenticated");
|
2024-03-05 19:48:54 -05:00
|
|
|
|
2024-08-08 17:18:30 +00:00
|
|
|
let room_id = &body.room_id;
|
|
|
|
let filter = &body.filter;
|
|
|
|
|
|
|
|
let limit = usize::try_from(body.limit).unwrap_or(10).min(100);
|
|
|
|
let from = match body.from.as_ref() {
|
|
|
|
Some(from) => PduCount::try_from_string(from)?,
|
2022-04-06 21:31:29 +02:00
|
|
|
None => match body.dir {
|
2023-02-26 16:29:06 +01:00
|
|
|
ruma::api::Direction::Forward => PduCount::min(),
|
|
|
|
ruma::api::Direction::Backward => PduCount::max(),
|
2022-04-06 21:31:29 +02:00
|
|
|
},
|
|
|
|
};
|
2024-03-05 19:48:54 -05:00
|
|
|
|
2024-03-25 17:05:11 -04:00
|
|
|
let to = body
|
|
|
|
.to
|
|
|
|
.as_ref()
|
|
|
|
.and_then(|t| PduCount::try_from_string(t).ok());
|
2024-03-05 19:48:54 -05:00
|
|
|
|
2024-07-16 08:05:25 +00:00
|
|
|
services
|
2024-03-25 17:05:11 -04:00
|
|
|
.rooms
|
|
|
|
.lazy_loading
|
2024-08-08 17:18:30 +00:00
|
|
|
.lazy_load_confirm_delivery(sender_user, sender_device, room_id, from);
|
2024-03-05 19:48:54 -05:00
|
|
|
|
2022-02-18 15:33:14 +01:00
|
|
|
let mut resp = get_message_events::v3::Response::new();
|
2022-01-04 14:30:13 +01:00
|
|
|
let mut lazy_loaded = HashSet::new();
|
2024-08-08 17:18:30 +00:00
|
|
|
let next_token;
|
2020-07-30 18:14:47 +02:00
|
|
|
match body.dir {
|
2023-02-26 16:29:06 +01:00
|
|
|
ruma::api::Direction::Forward => {
|
2024-08-08 17:18:30 +00:00
|
|
|
let events_after: Vec<PdusIterItem> = services
|
2020-07-30 18:14:47 +02:00
|
|
|
.rooms
|
2022-10-05 09:34:25 +02:00
|
|
|
.timeline
|
2024-08-08 17:18:30 +00:00
|
|
|
.pdus_after(sender_user, room_id, from)
|
|
|
|
.await?
|
|
|
|
.ready_filter_map(|item| contains_url_filter(item, filter))
|
|
|
|
.filter_map(|item| visibility_filter(&services, item, sender_user))
|
|
|
|
.ready_take_while(|(count, _)| Some(*count) != to) // Stop at `to`
|
2024-04-05 06:34:56 -07:00
|
|
|
.take(limit)
|
2024-08-08 17:18:30 +00:00
|
|
|
.collect()
|
|
|
|
.boxed()
|
|
|
|
.await;
|
2024-03-05 19:48:54 -05:00
|
|
|
|
2022-01-04 14:30:13 +01:00
|
|
|
for (_, event) in &events_after {
|
2024-03-23 23:13:40 -04:00
|
|
|
/* TODO: Remove the not "element_hacks" check when these are resolved:
|
2022-10-11 17:10:09 +02:00
|
|
|
* https://github.com/vector-im/element-android/issues/3417
|
|
|
|
* https://github.com/vector-im/element-web/issues/21034
|
2024-03-23 23:13:40 -04:00
|
|
|
*/
|
2024-03-23 23:40:09 -04:00
|
|
|
if !cfg!(feature = "element_hacks")
|
2024-08-08 17:18:30 +00:00
|
|
|
&& !services
|
|
|
|
.rooms
|
|
|
|
.lazy_loading
|
|
|
|
.lazy_load_was_sent_before(sender_user, sender_device, room_id, &event.sender)
|
|
|
|
.await
|
|
|
|
{
|
2022-01-04 14:30:13 +01:00
|
|
|
lazy_loaded.insert(event.sender.clone());
|
|
|
|
}
|
2024-03-23 23:13:40 -04:00
|
|
|
|
2024-08-08 17:18:30 +00:00
|
|
|
if cfg!(features = "element_hacks") {
|
|
|
|
lazy_loaded.insert(event.sender.clone());
|
|
|
|
}
|
2022-01-04 14:30:13 +01:00
|
|
|
}
|
2024-03-05 19:48:54 -05:00
|
|
|
|
2022-01-04 14:30:13 +01:00
|
|
|
next_token = events_after.last().map(|(count, _)| count).copied();
|
2024-03-05 19:48:54 -05:00
|
|
|
|
2024-03-25 17:05:11 -04:00
|
|
|
let events_after: Vec<_> = events_after
|
|
|
|
.into_iter()
|
|
|
|
.map(|(_, pdu)| pdu.to_room_event())
|
|
|
|
.collect();
|
2024-03-05 19:48:54 -05:00
|
|
|
|
2023-02-20 22:59:45 +01:00
|
|
|
resp.start = from.stringify();
|
|
|
|
resp.end = next_token.map(|count| count.stringify());
|
2022-01-04 14:30:13 +01:00
|
|
|
resp.chunk = events_after;
|
2020-07-30 18:14:47 +02:00
|
|
|
},
|
2023-02-26 16:29:06 +01:00
|
|
|
ruma::api::Direction::Backward => {
|
2024-07-16 08:05:25 +00:00
|
|
|
services
|
2024-03-25 17:05:11 -04:00
|
|
|
.rooms
|
|
|
|
.timeline
|
2024-08-08 17:18:30 +00:00
|
|
|
.backfill_if_required(room_id, from)
|
|
|
|
.boxed()
|
2024-03-25 17:05:11 -04:00
|
|
|
.await?;
|
2024-08-08 17:18:30 +00:00
|
|
|
|
|
|
|
let events_before: Vec<PdusIterItem> = services
|
2020-07-30 18:14:47 +02:00
|
|
|
.rooms
|
2022-10-05 09:34:25 +02:00
|
|
|
.timeline
|
2024-08-08 17:18:30 +00:00
|
|
|
.pdus_until(sender_user, room_id, from)
|
|
|
|
.await?
|
|
|
|
.ready_filter_map(|item| contains_url_filter(item, filter))
|
|
|
|
.filter_map(|item| visibility_filter(&services, item, sender_user))
|
|
|
|
.ready_take_while(|(count, _)| Some(*count) != to) // Stop at `to`
|
2024-04-05 06:34:56 -07:00
|
|
|
.take(limit)
|
2024-08-08 17:18:30 +00:00
|
|
|
.collect()
|
|
|
|
.boxed()
|
|
|
|
.await;
|
2024-03-05 19:48:54 -05:00
|
|
|
|
2022-01-04 14:30:13 +01:00
|
|
|
for (_, event) in &events_before {
|
2024-03-23 23:13:40 -04:00
|
|
|
/* TODO: Remove the not "element_hacks" check when these are resolved:
|
2022-10-11 17:10:09 +02:00
|
|
|
* https://github.com/vector-im/element-android/issues/3417
|
|
|
|
* https://github.com/vector-im/element-web/issues/21034
|
2024-03-23 23:13:40 -04:00
|
|
|
*/
|
2024-03-23 23:40:09 -04:00
|
|
|
if !cfg!(feature = "element_hacks")
|
2024-08-08 17:18:30 +00:00
|
|
|
&& !services
|
|
|
|
.rooms
|
|
|
|
.lazy_loading
|
|
|
|
.lazy_load_was_sent_before(sender_user, sender_device, room_id, &event.sender)
|
|
|
|
.await
|
|
|
|
{
|
2022-01-04 14:30:13 +01:00
|
|
|
lazy_loaded.insert(event.sender.clone());
|
|
|
|
}
|
2024-03-23 23:13:40 -04:00
|
|
|
|
2024-08-08 17:18:30 +00:00
|
|
|
if cfg!(features = "element_hacks") {
|
|
|
|
lazy_loaded.insert(event.sender.clone());
|
|
|
|
}
|
2022-01-04 14:30:13 +01:00
|
|
|
}
|
2024-03-05 19:48:54 -05:00
|
|
|
|
2022-01-04 14:30:13 +01:00
|
|
|
next_token = events_before.last().map(|(count, _)| count).copied();
|
2024-03-05 19:48:54 -05:00
|
|
|
|
2024-03-25 17:05:11 -04:00
|
|
|
let events_before: Vec<_> = events_before
|
|
|
|
.into_iter()
|
|
|
|
.map(|(_, pdu)| pdu.to_room_event())
|
|
|
|
.collect();
|
2024-03-05 19:48:54 -05:00
|
|
|
|
2023-02-20 22:59:45 +01:00
|
|
|
resp.start = from.stringify();
|
|
|
|
resp.end = next_token.map(|count| count.stringify());
|
2022-01-04 14:30:13 +01:00
|
|
|
resp.chunk = events_before;
|
2024-03-05 19:48:54 -05:00
|
|
|
},
|
2022-01-04 14:30:13 +01:00
|
|
|
}
|
2024-03-05 19:48:54 -05:00
|
|
|
|
2022-01-04 14:30:13 +01:00
|
|
|
resp.state = Vec::new();
|
|
|
|
for ll_id in &lazy_loaded {
|
2024-08-08 17:18:30 +00:00
|
|
|
if let Ok(member_event) = services
|
|
|
|
.rooms
|
|
|
|
.state_accessor
|
|
|
|
.room_state_get(room_id, &StateEventType::RoomMember, ll_id.as_str())
|
|
|
|
.await
|
2024-07-16 08:05:25 +00:00
|
|
|
{
|
2022-01-04 14:30:13 +01:00
|
|
|
resp.state.push(member_event.to_state_event());
|
2024-03-05 19:48:54 -05:00
|
|
|
}
|
2020-07-30 18:14:47 +02:00
|
|
|
}
|
2024-03-05 19:48:54 -05:00
|
|
|
|
2024-04-03 16:36:51 -04:00
|
|
|
// 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 {
|
2024-08-08 17:18:30 +00:00
|
|
|
services.rooms.lazy_loading.lazy_load_mark_sent(
|
|
|
|
sender_user,
|
|
|
|
sender_device,
|
|
|
|
room_id,
|
|
|
|
lazy_loaded,
|
|
|
|
next_token,
|
|
|
|
);
|
2024-04-03 16:36:51 -04:00
|
|
|
}
|
2022-01-04 14:30:13 +01:00
|
|
|
}
|
2024-03-05 19:48:54 -05:00
|
|
|
|
2022-01-22 16:58:32 +01:00
|
|
|
Ok(resp)
|
2020-07-30 18:14:47 +02:00
|
|
|
}
|
2024-04-05 06:34:56 -07:00
|
|
|
|
2024-08-08 17:18:30 +00:00
|
|
|
async fn visibility_filter(services: &Services, item: PdusIterItem, user_id: &UserId) -> Option<PdusIterItem> {
|
|
|
|
let (_, pdu) = &item;
|
|
|
|
|
2024-07-16 08:05:25 +00:00
|
|
|
services
|
2024-04-05 06:34:56 -07:00
|
|
|
.rooms
|
|
|
|
.state_accessor
|
2024-08-08 17:18:30 +00:00
|
|
|
.user_can_see_event(user_id, &pdu.room_id, &pdu.event_id)
|
|
|
|
.await
|
|
|
|
.then_some(item)
|
2024-04-05 06:34:56 -07:00
|
|
|
}
|
|
|
|
|
2024-08-08 17:18:30 +00:00
|
|
|
fn contains_url_filter(item: PdusIterItem, filter: &RoomEventFilter) -> Option<PdusIterItem> {
|
|
|
|
let (_, pdu) = &item;
|
|
|
|
|
2024-04-05 06:34:56 -07:00
|
|
|
if filter.url_filter.is_none() {
|
2024-08-08 17:18:30 +00:00
|
|
|
return Some(item);
|
2024-04-05 06:34:56 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
let content: Value = from_str(pdu.content.get()).unwrap();
|
2024-08-08 17:18:30 +00:00
|
|
|
let res = match filter.url_filter {
|
2024-04-05 06:34:56 -07:00
|
|
|
Some(UrlFilter::EventsWithoutUrl) => !content["url"].is_string(),
|
|
|
|
Some(UrlFilter::EventsWithUrl) => content["url"].is_string(),
|
|
|
|
None => true,
|
2024-08-08 17:18:30 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
res.then_some(item)
|
2024-04-05 06:34:56 -07:00
|
|
|
}
|