2020-08-14 09:31:31 +00:00
|
|
|
use crate::{
|
2020-08-18 20:26:03 +00:00
|
|
|
client_server,
|
2021-07-14 07:07:08 +00:00
|
|
|
database::DatabaseGuard,
|
2020-08-18 20:26:03 +00:00
|
|
|
pdu::{PduBuilder, PduEvent},
|
2021-04-13 16:17:51 +00:00
|
|
|
server_server, utils, ConduitResult, Database, Error, Result, Ruma,
|
2020-08-14 09:31:31 +00:00
|
|
|
};
|
2021-05-20 21:46:52 +00:00
|
|
|
use log::{debug, error, warn};
|
2021-04-25 12:10:07 +00:00
|
|
|
use member::{MemberEventContent, MembershipState};
|
2021-04-13 19:34:31 +00:00
|
|
|
use rocket::futures;
|
2020-07-30 16:14:47 +00:00
|
|
|
use ruma::{
|
2020-08-14 09:31:31 +00:00
|
|
|
api::{
|
|
|
|
client::{
|
|
|
|
error::ErrorKind,
|
2020-08-20 16:12:02 +00:00
|
|
|
r0::membership::{
|
|
|
|
ban_user, forget_room, get_member_events, invite_user, join_room_by_id,
|
|
|
|
join_room_by_id_or_alias, joined_members, joined_rooms, kick_user, leave_room,
|
|
|
|
unban_user, IncomingThirdPartySigned,
|
2020-08-14 09:31:31 +00:00
|
|
|
},
|
2020-07-30 16:14:47 +00:00
|
|
|
},
|
2021-04-25 12:10:07 +00:00
|
|
|
federation::{self, membership::create_invite},
|
|
|
|
},
|
|
|
|
events::{
|
|
|
|
pdu::Pdu,
|
|
|
|
room::{create::CreateEventContent, member},
|
|
|
|
EventType,
|
2020-07-30 16:14:47 +00:00
|
|
|
},
|
2021-04-26 16:20:20 +00:00
|
|
|
serde::{to_canonical_value, CanonicalJsonObject, CanonicalJsonValue, Raw},
|
2021-06-30 00:18:52 +00:00
|
|
|
state_res::{self, RoomVersion},
|
2021-04-25 12:10:07 +00:00
|
|
|
uint, EventId, RoomId, RoomVersionId, ServerName, UserId,
|
2020-07-30 16:14:47 +00:00
|
|
|
};
|
2021-04-14 12:41:39 +00:00
|
|
|
use std::{
|
2021-05-20 21:46:52 +00:00
|
|
|
collections::{btree_map::Entry, BTreeMap, HashSet},
|
2021-04-25 12:10:07 +00:00
|
|
|
convert::{TryFrom, TryInto},
|
|
|
|
sync::{Arc, RwLock},
|
2021-05-20 21:46:52 +00:00
|
|
|
time::{Duration, Instant},
|
2021-04-14 12:41:39 +00:00
|
|
|
};
|
2020-07-30 16:14:47 +00:00
|
|
|
|
|
|
|
#[cfg(feature = "conduit_bin")]
|
|
|
|
use rocket::{get, post};
|
|
|
|
|
|
|
|
#[cfg_attr(
|
|
|
|
feature = "conduit_bin",
|
|
|
|
post("/_matrix/client/r0/rooms/<_>/join", data = "<body>")
|
|
|
|
)]
|
2021-02-28 11:41:03 +00:00
|
|
|
#[tracing::instrument(skip(db, body))]
|
2020-08-14 09:31:31 +00:00
|
|
|
pub async fn join_room_by_id_route(
|
2021-07-14 07:07:08 +00:00
|
|
|
db: DatabaseGuard,
|
2020-09-08 15:32:03 +00:00
|
|
|
body: Ruma<join_room_by_id::Request<'_>>,
|
2020-07-30 16:14:47 +00:00
|
|
|
) -> ConduitResult<join_room_by_id::Response> {
|
2021-04-14 12:41:39 +00:00
|
|
|
let sender_user = body.sender_user.as_ref().expect("user is authenticated");
|
|
|
|
|
|
|
|
let mut servers = db
|
|
|
|
.rooms
|
|
|
|
.invite_state(&sender_user, &body.room_id)?
|
|
|
|
.unwrap_or_default()
|
|
|
|
.iter()
|
|
|
|
.filter_map(|event| {
|
|
|
|
serde_json::from_str::<serde_json::Value>(&event.json().to_string()).ok()
|
|
|
|
})
|
|
|
|
.filter_map(|event| event.get("sender").cloned())
|
|
|
|
.filter_map(|sender| sender.as_str().map(|s| s.to_owned()))
|
|
|
|
.filter_map(|sender| UserId::try_from(sender).ok())
|
|
|
|
.map(|user| user.server_name().to_owned())
|
|
|
|
.collect::<HashSet<_>>();
|
|
|
|
|
|
|
|
servers.insert(body.room_id.server_name().to_owned());
|
|
|
|
|
2021-07-14 07:07:08 +00:00
|
|
|
let ret = join_room_by_id_helper(
|
2020-08-19 23:30:28 +00:00
|
|
|
&db,
|
2020-10-18 18:33:12 +00:00
|
|
|
body.sender_user.as_ref(),
|
2020-08-19 23:30:28 +00:00
|
|
|
&body.room_id,
|
2021-04-14 12:41:39 +00:00
|
|
|
&servers,
|
2020-08-19 23:30:28 +00:00
|
|
|
body.third_party_signed.as_ref(),
|
|
|
|
)
|
2021-07-14 07:07:08 +00:00
|
|
|
.await;
|
|
|
|
|
|
|
|
db.flush().await?;
|
|
|
|
|
|
|
|
ret
|
2020-07-30 16:14:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg_attr(
|
|
|
|
feature = "conduit_bin",
|
|
|
|
post("/_matrix/client/r0/join/<_>", data = "<body>")
|
|
|
|
)]
|
2021-02-28 11:41:03 +00:00
|
|
|
#[tracing::instrument(skip(db, body))]
|
2020-08-14 09:31:31 +00:00
|
|
|
pub async fn join_room_by_id_or_alias_route(
|
2021-07-14 07:07:08 +00:00
|
|
|
db: DatabaseGuard,
|
2020-09-08 15:32:03 +00:00
|
|
|
body: Ruma<join_room_by_id_or_alias::Request<'_>>,
|
2020-07-30 16:14:47 +00:00
|
|
|
) -> ConduitResult<join_room_by_id_or_alias::Response> {
|
2021-04-14 12:41:39 +00:00
|
|
|
let sender_user = body.sender_user.as_ref().expect("user is authenticated");
|
|
|
|
|
2020-09-14 09:00:31 +00:00
|
|
|
let (servers, room_id) = match RoomId::try_from(body.room_id_or_alias.clone()) {
|
2021-04-14 12:41:39 +00:00
|
|
|
Ok(room_id) => {
|
|
|
|
let mut servers = db
|
|
|
|
.rooms
|
|
|
|
.invite_state(&sender_user, &room_id)?
|
|
|
|
.unwrap_or_default()
|
|
|
|
.iter()
|
|
|
|
.filter_map(|event| {
|
|
|
|
serde_json::from_str::<serde_json::Value>(&event.json().to_string()).ok()
|
|
|
|
})
|
|
|
|
.filter_map(|event| event.get("sender").cloned())
|
|
|
|
.filter_map(|sender| sender.as_str().map(|s| s.to_owned()))
|
|
|
|
.filter_map(|sender| UserId::try_from(sender).ok())
|
|
|
|
.map(|user| user.server_name().to_owned())
|
|
|
|
.collect::<HashSet<_>>();
|
|
|
|
|
|
|
|
servers.insert(room_id.server_name().to_owned());
|
|
|
|
(servers, room_id)
|
|
|
|
}
|
2020-08-14 09:31:31 +00:00
|
|
|
Err(room_alias) => {
|
2020-09-14 09:00:31 +00:00
|
|
|
let response = client_server::get_alias_helper(&db, &room_alias).await?;
|
|
|
|
|
2021-04-14 12:41:39 +00:00
|
|
|
(response.0.servers.into_iter().collect(), response.0.room_id)
|
2020-08-14 09:31:31 +00:00
|
|
|
}
|
|
|
|
};
|
2020-07-30 16:14:47 +00:00
|
|
|
|
2020-10-21 19:28:02 +00:00
|
|
|
let join_room_response = join_room_by_id_helper(
|
|
|
|
&db,
|
|
|
|
body.sender_user.as_ref(),
|
|
|
|
&room_id,
|
|
|
|
&servers,
|
|
|
|
body.third_party_signed.as_ref(),
|
|
|
|
)
|
|
|
|
.await?;
|
|
|
|
|
|
|
|
db.flush().await?;
|
|
|
|
|
2020-07-30 16:14:47 +00:00
|
|
|
Ok(join_room_by_id_or_alias::Response {
|
2020-10-21 19:28:02 +00:00
|
|
|
room_id: join_room_response.0.room_id,
|
2020-07-30 16:14:47 +00:00
|
|
|
}
|
|
|
|
.into())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg_attr(
|
|
|
|
feature = "conduit_bin",
|
|
|
|
post("/_matrix/client/r0/rooms/<_>/leave", data = "<body>")
|
|
|
|
)]
|
2021-02-28 11:41:03 +00:00
|
|
|
#[tracing::instrument(skip(db, body))]
|
2020-09-14 18:23:19 +00:00
|
|
|
pub async fn leave_room_route(
|
2021-07-14 07:07:08 +00:00
|
|
|
db: DatabaseGuard,
|
2020-09-08 15:32:03 +00:00
|
|
|
body: Ruma<leave_room::Request<'_>>,
|
2020-07-30 16:14:47 +00:00
|
|
|
) -> ConduitResult<leave_room::Response> {
|
2020-10-18 18:33:12 +00:00
|
|
|
let sender_user = body.sender_user.as_ref().expect("user is authenticated");
|
2020-07-30 16:14:47 +00:00
|
|
|
|
2021-04-13 13:00:45 +00:00
|
|
|
db.rooms.leave_room(sender_user, &body.room_id, &db).await?;
|
2020-07-30 16:14:47 +00:00
|
|
|
|
2020-10-21 19:28:02 +00:00
|
|
|
db.flush().await?;
|
|
|
|
|
2020-08-06 12:29:59 +00:00
|
|
|
Ok(leave_room::Response::new().into())
|
2020-07-30 16:14:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg_attr(
|
|
|
|
feature = "conduit_bin",
|
|
|
|
post("/_matrix/client/r0/rooms/<_>/invite", data = "<body>")
|
|
|
|
)]
|
2021-02-28 11:41:03 +00:00
|
|
|
#[tracing::instrument(skip(db, body))]
|
2020-09-14 18:23:19 +00:00
|
|
|
pub async fn invite_user_route(
|
2021-07-14 07:07:08 +00:00
|
|
|
db: DatabaseGuard,
|
2020-09-08 15:32:03 +00:00
|
|
|
body: Ruma<invite_user::Request<'_>>,
|
2020-07-30 16:14:47 +00:00
|
|
|
) -> ConduitResult<invite_user::Response> {
|
2020-10-18 18:33:12 +00:00
|
|
|
let sender_user = body.sender_user.as_ref().expect("user is authenticated");
|
2020-07-30 16:14:47 +00:00
|
|
|
|
2020-09-08 15:32:03 +00:00
|
|
|
if let invite_user::IncomingInvitationRecipient::UserId { user_id } = &body.recipient {
|
2021-04-25 12:10:07 +00:00
|
|
|
invite_helper(sender_user, user_id, &body.room_id, &db, false).await?;
|
2020-10-21 19:28:02 +00:00
|
|
|
db.flush().await?;
|
2021-07-15 21:17:58 +00:00
|
|
|
Ok(invite_user::Response {}.into())
|
2020-07-30 16:14:47 +00:00
|
|
|
} else {
|
|
|
|
Err(Error::BadRequest(ErrorKind::NotFound, "User not found."))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg_attr(
|
|
|
|
feature = "conduit_bin",
|
|
|
|
post("/_matrix/client/r0/rooms/<_>/kick", data = "<body>")
|
|
|
|
)]
|
2021-02-28 11:41:03 +00:00
|
|
|
#[tracing::instrument(skip(db, body))]
|
2020-09-14 18:23:19 +00:00
|
|
|
pub async fn kick_user_route(
|
2021-07-14 07:07:08 +00:00
|
|
|
db: DatabaseGuard,
|
2020-09-08 15:32:03 +00:00
|
|
|
body: Ruma<kick_user::Request<'_>>,
|
2020-07-30 16:14:47 +00:00
|
|
|
) -> ConduitResult<kick_user::Response> {
|
2020-10-18 18:33:12 +00:00
|
|
|
let sender_user = body.sender_user.as_ref().expect("user is authenticated");
|
2020-07-30 16:14:47 +00:00
|
|
|
|
|
|
|
let mut event = serde_json::from_value::<Raw<ruma::events::room::member::MemberEventContent>>(
|
|
|
|
db.rooms
|
|
|
|
.room_state_get(
|
|
|
|
&body.room_id,
|
|
|
|
&EventType::RoomMember,
|
|
|
|
&body.user_id.to_string(),
|
|
|
|
)?
|
|
|
|
.ok_or(Error::BadRequest(
|
|
|
|
ErrorKind::BadState,
|
|
|
|
"Cannot kick member that's not in the room.",
|
|
|
|
))?
|
2021-06-30 07:52:01 +00:00
|
|
|
.content
|
|
|
|
.clone(),
|
2020-07-30 16:14:47 +00:00
|
|
|
)
|
|
|
|
.expect("Raw::from_value always works")
|
|
|
|
.deserialize()
|
|
|
|
.map_err(|_| Error::bad_database("Invalid member event in database."))?;
|
|
|
|
|
|
|
|
event.membership = ruma::events::room::member::MembershipState::Leave;
|
|
|
|
// TODO: reason
|
|
|
|
|
2021-07-13 13:44:25 +00:00
|
|
|
let mutex = Arc::clone(
|
|
|
|
db.globals
|
|
|
|
.roomid_mutex
|
|
|
|
.write()
|
|
|
|
.unwrap()
|
|
|
|
.entry(body.room_id.clone())
|
|
|
|
.or_default(),
|
|
|
|
);
|
|
|
|
let mutex_lock = mutex.lock().await;
|
|
|
|
|
2020-10-05 20:19:22 +00:00
|
|
|
db.rooms.build_and_append_pdu(
|
|
|
|
PduBuilder {
|
|
|
|
event_type: EventType::RoomMember,
|
|
|
|
content: serde_json::to_value(event).expect("event is valid, we just created it"),
|
|
|
|
unsigned: None,
|
|
|
|
state_key: Some(body.user_id.to_string()),
|
|
|
|
redacts: None,
|
|
|
|
},
|
2020-10-18 18:33:12 +00:00
|
|
|
&sender_user,
|
2020-10-05 20:19:22 +00:00
|
|
|
&body.room_id,
|
2021-01-15 16:05:57 +00:00
|
|
|
&db,
|
2021-07-13 13:44:25 +00:00
|
|
|
&mutex_lock,
|
2020-10-05 20:19:22 +00:00
|
|
|
)?;
|
2020-07-30 16:14:47 +00:00
|
|
|
|
2021-07-13 13:44:25 +00:00
|
|
|
drop(mutex_lock);
|
|
|
|
|
2020-10-21 19:28:02 +00:00
|
|
|
db.flush().await?;
|
|
|
|
|
2020-08-06 12:29:59 +00:00
|
|
|
Ok(kick_user::Response::new().into())
|
2020-07-30 16:14:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg_attr(
|
|
|
|
feature = "conduit_bin",
|
|
|
|
post("/_matrix/client/r0/rooms/<_>/ban", data = "<body>")
|
|
|
|
)]
|
2021-02-28 11:41:03 +00:00
|
|
|
#[tracing::instrument(skip(db, body))]
|
2020-09-14 18:23:19 +00:00
|
|
|
pub async fn ban_user_route(
|
2021-07-14 07:07:08 +00:00
|
|
|
db: DatabaseGuard,
|
2020-09-08 15:32:03 +00:00
|
|
|
body: Ruma<ban_user::Request<'_>>,
|
2020-07-30 16:14:47 +00:00
|
|
|
) -> ConduitResult<ban_user::Response> {
|
2020-10-18 18:33:12 +00:00
|
|
|
let sender_user = body.sender_user.as_ref().expect("user is authenticated");
|
2020-07-30 16:14:47 +00:00
|
|
|
|
|
|
|
// TODO: reason
|
|
|
|
|
|
|
|
let event = db
|
|
|
|
.rooms
|
|
|
|
.room_state_get(
|
|
|
|
&body.room_id,
|
|
|
|
&EventType::RoomMember,
|
|
|
|
&body.user_id.to_string(),
|
|
|
|
)?
|
|
|
|
.map_or(
|
|
|
|
Ok::<_, Error>(member::MemberEventContent {
|
|
|
|
membership: member::MembershipState::Ban,
|
|
|
|
displayname: db.users.displayname(&body.user_id)?,
|
|
|
|
avatar_url: db.users.avatar_url(&body.user_id)?,
|
|
|
|
is_direct: None,
|
|
|
|
third_party_invite: None,
|
2021-07-15 21:17:58 +00:00
|
|
|
blurhash: db.users.blurhash(&body.user_id)?,
|
2020-07-30 16:14:47 +00:00
|
|
|
}),
|
2021-03-17 21:30:25 +00:00
|
|
|
|event| {
|
2021-06-30 07:52:01 +00:00
|
|
|
let mut event = serde_json::from_value::<Raw<member::MemberEventContent>>(
|
|
|
|
event.content.clone(),
|
|
|
|
)
|
|
|
|
.expect("Raw::from_value always works")
|
|
|
|
.deserialize()
|
|
|
|
.map_err(|_| Error::bad_database("Invalid member event in database."))?;
|
2020-07-30 16:14:47 +00:00
|
|
|
event.membership = ruma::events::room::member::MembershipState::Ban;
|
|
|
|
Ok(event)
|
|
|
|
},
|
|
|
|
)?;
|
|
|
|
|
2021-07-13 13:44:25 +00:00
|
|
|
let mutex = Arc::clone(
|
|
|
|
db.globals
|
|
|
|
.roomid_mutex
|
|
|
|
.write()
|
|
|
|
.unwrap()
|
|
|
|
.entry(body.room_id.clone())
|
|
|
|
.or_default(),
|
|
|
|
);
|
|
|
|
let mutex_lock = mutex.lock().await;
|
|
|
|
|
2020-10-05 20:19:22 +00:00
|
|
|
db.rooms.build_and_append_pdu(
|
|
|
|
PduBuilder {
|
|
|
|
event_type: EventType::RoomMember,
|
|
|
|
content: serde_json::to_value(event).expect("event is valid, we just created it"),
|
|
|
|
unsigned: None,
|
|
|
|
state_key: Some(body.user_id.to_string()),
|
|
|
|
redacts: None,
|
|
|
|
},
|
2020-10-18 18:33:12 +00:00
|
|
|
&sender_user,
|
2020-10-05 20:19:22 +00:00
|
|
|
&body.room_id,
|
2021-01-15 16:05:57 +00:00
|
|
|
&db,
|
2021-07-13 13:44:25 +00:00
|
|
|
&mutex_lock,
|
2020-10-05 20:19:22 +00:00
|
|
|
)?;
|
2020-07-30 16:14:47 +00:00
|
|
|
|
2021-07-13 13:44:25 +00:00
|
|
|
drop(mutex_lock);
|
|
|
|
|
2020-10-21 19:28:02 +00:00
|
|
|
db.flush().await?;
|
|
|
|
|
2020-08-06 12:29:59 +00:00
|
|
|
Ok(ban_user::Response::new().into())
|
2020-07-30 16:14:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg_attr(
|
|
|
|
feature = "conduit_bin",
|
|
|
|
post("/_matrix/client/r0/rooms/<_>/unban", data = "<body>")
|
|
|
|
)]
|
2021-02-28 11:41:03 +00:00
|
|
|
#[tracing::instrument(skip(db, body))]
|
2020-09-14 18:23:19 +00:00
|
|
|
pub async fn unban_user_route(
|
2021-07-14 07:07:08 +00:00
|
|
|
db: DatabaseGuard,
|
2020-09-08 15:32:03 +00:00
|
|
|
body: Ruma<unban_user::Request<'_>>,
|
2020-07-30 16:14:47 +00:00
|
|
|
) -> ConduitResult<unban_user::Response> {
|
2020-10-18 18:33:12 +00:00
|
|
|
let sender_user = body.sender_user.as_ref().expect("user is authenticated");
|
2020-07-30 16:14:47 +00:00
|
|
|
|
|
|
|
let mut event = serde_json::from_value::<Raw<ruma::events::room::member::MemberEventContent>>(
|
|
|
|
db.rooms
|
|
|
|
.room_state_get(
|
|
|
|
&body.room_id,
|
|
|
|
&EventType::RoomMember,
|
|
|
|
&body.user_id.to_string(),
|
|
|
|
)?
|
|
|
|
.ok_or(Error::BadRequest(
|
|
|
|
ErrorKind::BadState,
|
|
|
|
"Cannot unban a user who is not banned.",
|
|
|
|
))?
|
2021-06-30 07:52:01 +00:00
|
|
|
.content
|
|
|
|
.clone(),
|
2020-07-30 16:14:47 +00:00
|
|
|
)
|
|
|
|
.expect("from_value::<Raw<..>> can never fail")
|
|
|
|
.deserialize()
|
|
|
|
.map_err(|_| Error::bad_database("Invalid member event in database."))?;
|
|
|
|
|
|
|
|
event.membership = ruma::events::room::member::MembershipState::Leave;
|
|
|
|
|
2021-07-13 13:44:25 +00:00
|
|
|
let mutex = Arc::clone(
|
|
|
|
db.globals
|
|
|
|
.roomid_mutex
|
|
|
|
.write()
|
|
|
|
.unwrap()
|
|
|
|
.entry(body.room_id.clone())
|
|
|
|
.or_default(),
|
|
|
|
);
|
|
|
|
let mutex_lock = mutex.lock().await;
|
|
|
|
|
2020-10-05 20:19:22 +00:00
|
|
|
db.rooms.build_and_append_pdu(
|
|
|
|
PduBuilder {
|
|
|
|
event_type: EventType::RoomMember,
|
|
|
|
content: serde_json::to_value(event).expect("event is valid, we just created it"),
|
|
|
|
unsigned: None,
|
|
|
|
state_key: Some(body.user_id.to_string()),
|
|
|
|
redacts: None,
|
|
|
|
},
|
2020-10-18 18:33:12 +00:00
|
|
|
&sender_user,
|
2020-10-05 20:19:22 +00:00
|
|
|
&body.room_id,
|
2021-01-15 16:05:57 +00:00
|
|
|
&db,
|
2021-07-13 13:44:25 +00:00
|
|
|
&mutex_lock,
|
2020-10-05 20:19:22 +00:00
|
|
|
)?;
|
2020-07-30 16:14:47 +00:00
|
|
|
|
2021-07-13 13:44:25 +00:00
|
|
|
drop(mutex_lock);
|
|
|
|
|
2020-10-21 19:28:02 +00:00
|
|
|
db.flush().await?;
|
|
|
|
|
2020-08-06 12:29:59 +00:00
|
|
|
Ok(unban_user::Response::new().into())
|
2020-07-30 16:14:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg_attr(
|
|
|
|
feature = "conduit_bin",
|
|
|
|
post("/_matrix/client/r0/rooms/<_>/forget", data = "<body>")
|
|
|
|
)]
|
2021-02-28 11:41:03 +00:00
|
|
|
#[tracing::instrument(skip(db, body))]
|
2020-10-21 19:28:02 +00:00
|
|
|
pub async fn forget_room_route(
|
2021-07-14 07:07:08 +00:00
|
|
|
db: DatabaseGuard,
|
2020-09-08 15:32:03 +00:00
|
|
|
body: Ruma<forget_room::Request<'_>>,
|
2020-07-30 16:14:47 +00:00
|
|
|
) -> ConduitResult<forget_room::Response> {
|
2020-10-18 18:33:12 +00:00
|
|
|
let sender_user = body.sender_user.as_ref().expect("user is authenticated");
|
2020-07-30 16:14:47 +00:00
|
|
|
|
2020-10-18 18:33:12 +00:00
|
|
|
db.rooms.forget(&body.room_id, &sender_user)?;
|
2020-07-30 16:14:47 +00:00
|
|
|
|
2020-10-21 19:28:02 +00:00
|
|
|
db.flush().await?;
|
|
|
|
|
2020-08-06 12:29:59 +00:00
|
|
|
Ok(forget_room::Response::new().into())
|
2020-07-30 16:14:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg_attr(
|
|
|
|
feature = "conduit_bin",
|
|
|
|
get("/_matrix/client/r0/joined_rooms", data = "<body>")
|
|
|
|
)]
|
2021-02-28 11:41:03 +00:00
|
|
|
#[tracing::instrument(skip(db, body))]
|
2020-10-21 19:28:02 +00:00
|
|
|
pub async fn joined_rooms_route(
|
2021-07-14 07:07:08 +00:00
|
|
|
db: DatabaseGuard,
|
2020-07-30 16:14:47 +00:00
|
|
|
body: Ruma<joined_rooms::Request>,
|
|
|
|
) -> ConduitResult<joined_rooms::Response> {
|
2020-10-18 18:33:12 +00:00
|
|
|
let sender_user = body.sender_user.as_ref().expect("user is authenticated");
|
2020-07-30 16:14:47 +00:00
|
|
|
|
|
|
|
Ok(joined_rooms::Response {
|
|
|
|
joined_rooms: db
|
|
|
|
.rooms
|
2020-10-18 18:33:12 +00:00
|
|
|
.rooms_joined(&sender_user)
|
2020-07-30 16:14:47 +00:00
|
|
|
.filter_map(|r| r.ok())
|
|
|
|
.collect(),
|
|
|
|
}
|
|
|
|
.into())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg_attr(
|
|
|
|
feature = "conduit_bin",
|
|
|
|
get("/_matrix/client/r0/rooms/<_>/members", data = "<body>")
|
|
|
|
)]
|
2021-02-28 11:41:03 +00:00
|
|
|
#[tracing::instrument(skip(db, body))]
|
2020-10-21 19:28:02 +00:00
|
|
|
pub async fn get_member_events_route(
|
2021-07-14 07:07:08 +00:00
|
|
|
db: DatabaseGuard,
|
2020-09-08 15:32:03 +00:00
|
|
|
body: Ruma<get_member_events::Request<'_>>,
|
2020-07-30 16:14:47 +00:00
|
|
|
) -> ConduitResult<get_member_events::Response> {
|
2020-10-18 18:33:12 +00:00
|
|
|
let sender_user = body.sender_user.as_ref().expect("user is authenticated");
|
2020-07-30 16:14:47 +00:00
|
|
|
|
2020-10-18 18:33:12 +00:00
|
|
|
if !db.rooms.is_joined(sender_user, &body.room_id)? {
|
2020-07-30 16:14:47 +00:00
|
|
|
return Err(Error::BadRequest(
|
|
|
|
ErrorKind::Forbidden,
|
|
|
|
"You don't have permission to view this room.",
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(get_member_events::Response {
|
|
|
|
chunk: db
|
|
|
|
.rooms
|
2020-12-19 15:00:11 +00:00
|
|
|
.room_state_full(&body.room_id)?
|
|
|
|
.iter()
|
|
|
|
.filter(|(key, _)| key.0 == EventType::RoomMember)
|
|
|
|
.map(|(_, pdu)| pdu.to_member_event())
|
2020-07-30 16:14:47 +00:00
|
|
|
.collect(),
|
|
|
|
}
|
|
|
|
.into())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg_attr(
|
|
|
|
feature = "conduit_bin",
|
|
|
|
get("/_matrix/client/r0/rooms/<_>/joined_members", data = "<body>")
|
|
|
|
)]
|
2021-02-28 11:41:03 +00:00
|
|
|
#[tracing::instrument(skip(db, body))]
|
2020-10-21 19:28:02 +00:00
|
|
|
pub async fn joined_members_route(
|
2021-07-14 07:07:08 +00:00
|
|
|
db: DatabaseGuard,
|
2020-09-08 15:32:03 +00:00
|
|
|
body: Ruma<joined_members::Request<'_>>,
|
2020-07-30 16:14:47 +00:00
|
|
|
) -> ConduitResult<joined_members::Response> {
|
2020-10-18 18:33:12 +00:00
|
|
|
let sender_user = body.sender_user.as_ref().expect("user is authenticated");
|
2020-07-30 16:14:47 +00:00
|
|
|
|
|
|
|
if !db
|
|
|
|
.rooms
|
2020-10-18 18:33:12 +00:00
|
|
|
.is_joined(&sender_user, &body.room_id)
|
2020-07-30 16:14:47 +00:00
|
|
|
.unwrap_or(false)
|
|
|
|
{
|
|
|
|
return Err(Error::BadRequest(
|
|
|
|
ErrorKind::Forbidden,
|
|
|
|
"You aren't a member of the room.",
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut joined = BTreeMap::new();
|
|
|
|
for user_id in db.rooms.room_members(&body.room_id).filter_map(|r| r.ok()) {
|
|
|
|
let display_name = db.users.displayname(&user_id)?;
|
|
|
|
let avatar_url = db.users.avatar_url(&user_id)?;
|
|
|
|
|
|
|
|
joined.insert(
|
|
|
|
user_id,
|
|
|
|
joined_members::RoomMember {
|
|
|
|
display_name,
|
|
|
|
avatar_url,
|
|
|
|
},
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(joined_members::Response { joined }.into())
|
|
|
|
}
|
2020-08-19 23:30:28 +00:00
|
|
|
|
2021-03-13 15:30:12 +00:00
|
|
|
#[tracing::instrument(skip(db))]
|
2020-08-19 23:30:28 +00:00
|
|
|
async fn join_room_by_id_helper(
|
|
|
|
db: &Database,
|
2020-10-18 18:33:12 +00:00
|
|
|
sender_user: Option<&UserId>,
|
2020-08-19 23:30:28 +00:00
|
|
|
room_id: &RoomId,
|
2021-04-14 12:41:39 +00:00
|
|
|
servers: &HashSet<Box<ServerName>>,
|
2020-08-19 23:30:28 +00:00
|
|
|
_third_party_signed: Option<&IncomingThirdPartySigned>,
|
|
|
|
) -> ConduitResult<join_room_by_id::Response> {
|
2020-10-18 18:33:12 +00:00
|
|
|
let sender_user = sender_user.expect("user is authenticated");
|
2020-08-19 23:30:28 +00:00
|
|
|
|
2021-07-13 13:44:25 +00:00
|
|
|
let mutex = Arc::clone(
|
|
|
|
db.globals
|
|
|
|
.roomid_mutex
|
|
|
|
.write()
|
|
|
|
.unwrap()
|
|
|
|
.entry(room_id.clone())
|
|
|
|
.or_default(),
|
|
|
|
);
|
|
|
|
let mutex_lock = mutex.lock().await;
|
|
|
|
|
2020-08-19 23:30:28 +00:00
|
|
|
// Ask a remote server if we don't have this room
|
|
|
|
if !db.rooms.exists(&room_id)? && room_id.server_name() != db.globals.server_name() {
|
2020-09-14 09:00:31 +00:00
|
|
|
let mut make_join_response_and_server = Err(Error::BadServerResponse(
|
|
|
|
"No server available to assist in joining.",
|
|
|
|
));
|
|
|
|
|
|
|
|
for remote_server in servers {
|
2020-12-19 15:00:11 +00:00
|
|
|
let make_join_response = db
|
|
|
|
.sending
|
|
|
|
.send_federation_request(
|
|
|
|
&db.globals,
|
2021-01-14 19:39:56 +00:00
|
|
|
remote_server,
|
2020-12-19 15:00:11 +00:00
|
|
|
federation::membership::create_join_event_template::v1::Request {
|
|
|
|
room_id,
|
|
|
|
user_id: sender_user,
|
2021-03-26 10:10:45 +00:00
|
|
|
ver: &[RoomVersionId::Version6],
|
2020-12-19 15:00:11 +00:00
|
|
|
},
|
|
|
|
)
|
|
|
|
.await;
|
2020-09-14 09:00:31 +00:00
|
|
|
|
|
|
|
make_join_response_and_server = make_join_response.map(|r| (r, remote_server));
|
|
|
|
|
|
|
|
if make_join_response_and_server.is_ok() {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let (make_join_response, remote_server) = make_join_response_and_server?;
|
2020-08-19 23:30:28 +00:00
|
|
|
|
2021-03-26 10:10:45 +00:00
|
|
|
let room_version = match make_join_response.room_version {
|
|
|
|
Some(room_version) if room_version == RoomVersionId::Version6 => room_version,
|
|
|
|
_ => return Err(Error::BadServerResponse("Room version is not supported")),
|
|
|
|
};
|
|
|
|
|
2020-11-30 17:10:33 +00:00
|
|
|
let mut join_event_stub =
|
|
|
|
serde_json::from_str::<CanonicalJsonObject>(make_join_response.event.json().get())
|
2020-08-19 23:30:28 +00:00
|
|
|
.map_err(|_| {
|
|
|
|
Error::BadServerResponse("Invalid make_join event json received from server.")
|
|
|
|
})?;
|
|
|
|
|
2021-04-13 13:00:45 +00:00
|
|
|
// TODO: Is origin needed?
|
2020-08-19 23:30:28 +00:00
|
|
|
join_event_stub.insert(
|
|
|
|
"origin".to_owned(),
|
2021-04-26 16:20:20 +00:00
|
|
|
CanonicalJsonValue::String(db.globals.server_name().as_str().to_owned()),
|
2020-08-19 23:30:28 +00:00
|
|
|
);
|
|
|
|
join_event_stub.insert(
|
|
|
|
"origin_server_ts".to_owned(),
|
2021-04-26 16:20:20 +00:00
|
|
|
CanonicalJsonValue::Integer(
|
|
|
|
utils::millis_since_unix_epoch()
|
|
|
|
.try_into()
|
|
|
|
.expect("Timestamp is valid js_int value"),
|
|
|
|
),
|
2020-08-19 23:30:28 +00:00
|
|
|
);
|
2020-10-17 09:24:57 +00:00
|
|
|
join_event_stub.insert(
|
|
|
|
"content".to_owned(),
|
2020-11-30 17:10:33 +00:00
|
|
|
to_canonical_value(member::MemberEventContent {
|
2020-10-17 09:24:57 +00:00
|
|
|
membership: member::MembershipState::Join,
|
2020-10-18 18:33:12 +00:00
|
|
|
displayname: db.users.displayname(&sender_user)?,
|
|
|
|
avatar_url: db.users.avatar_url(&sender_user)?,
|
2020-10-17 09:24:57 +00:00
|
|
|
is_direct: None,
|
|
|
|
third_party_invite: None,
|
2021-07-15 21:17:58 +00:00
|
|
|
blurhash: db.users.blurhash(&sender_user)?,
|
2020-10-17 09:24:57 +00:00
|
|
|
})
|
|
|
|
.expect("event is valid, we just created it"),
|
|
|
|
);
|
2020-08-19 23:30:28 +00:00
|
|
|
|
2020-11-08 18:54:59 +00:00
|
|
|
// We don't leave the event id in the pdu because that's only allowed in v1 or v2 rooms
|
2020-11-30 17:10:33 +00:00
|
|
|
join_event_stub.remove("event_id");
|
2020-08-19 23:30:28 +00:00
|
|
|
|
2020-11-08 18:49:02 +00:00
|
|
|
// In order to create a compatible ref hash (EventID) the `hashes` field needs to be present
|
2020-08-19 23:30:28 +00:00
|
|
|
ruma::signatures::hash_and_sign_event(
|
|
|
|
db.globals.server_name().as_str(),
|
|
|
|
db.globals.keypair(),
|
2020-11-30 17:10:33 +00:00
|
|
|
&mut join_event_stub,
|
2021-03-26 10:10:45 +00:00
|
|
|
&room_version,
|
2020-08-19 23:30:28 +00:00
|
|
|
)
|
|
|
|
.expect("event is valid, we just created it");
|
|
|
|
|
2020-11-08 18:49:02 +00:00
|
|
|
// Generate event id
|
|
|
|
let event_id = EventId::try_from(&*format!(
|
|
|
|
"${}",
|
2021-03-26 10:10:45 +00:00
|
|
|
ruma::signatures::reference_hash(&join_event_stub, &room_version)
|
2020-11-08 18:49:02 +00:00
|
|
|
.expect("ruma can calculate reference hashes")
|
|
|
|
))
|
|
|
|
.expect("ruma's reference hashes are valid event ids");
|
|
|
|
|
2020-10-17 09:24:57 +00:00
|
|
|
// Add event_id back
|
2020-11-30 17:10:33 +00:00
|
|
|
join_event_stub.insert(
|
2020-10-27 23:10:09 +00:00
|
|
|
"event_id".to_owned(),
|
2021-04-26 16:20:20 +00:00
|
|
|
CanonicalJsonValue::String(event_id.as_str().to_owned()),
|
2020-10-27 23:10:09 +00:00
|
|
|
);
|
2020-10-17 09:24:57 +00:00
|
|
|
|
|
|
|
// It has enough fields to be called a proper event now
|
2020-11-30 17:10:33 +00:00
|
|
|
let join_event = join_event_stub;
|
2020-10-17 09:24:57 +00:00
|
|
|
|
2020-12-19 15:00:11 +00:00
|
|
|
let send_join_response = db
|
|
|
|
.sending
|
|
|
|
.send_federation_request(
|
|
|
|
&db.globals,
|
2021-01-14 19:39:56 +00:00
|
|
|
remote_server,
|
2020-12-19 15:00:11 +00:00
|
|
|
federation::membership::create_join_event::v2::Request {
|
|
|
|
room_id,
|
|
|
|
event_id: &event_id,
|
|
|
|
pdu: PduEvent::convert_to_outgoing_federation_event(join_event.clone()),
|
|
|
|
},
|
|
|
|
)
|
|
|
|
.await?;
|
2020-08-19 23:30:28 +00:00
|
|
|
|
2021-03-13 15:30:12 +00:00
|
|
|
let pdu = PduEvent::from_id_val(&event_id, join_event.clone())
|
2021-04-16 16:18:29 +00:00
|
|
|
.map_err(|_| Error::BadServerResponse("Invalid join event PDU."))?;
|
2021-03-13 15:30:12 +00:00
|
|
|
|
2021-03-25 22:55:40 +00:00
|
|
|
let mut state = BTreeMap::new();
|
2021-04-14 07:39:06 +00:00
|
|
|
let pub_key_map = RwLock::new(BTreeMap::new());
|
2021-04-13 19:34:31 +00:00
|
|
|
|
|
|
|
for result in futures::future::join_all(
|
|
|
|
send_join_response
|
|
|
|
.room_state
|
|
|
|
.state
|
|
|
|
.iter()
|
|
|
|
.map(|pdu| validate_and_add_event_id(pdu, &room_version, &pub_key_map, &db)),
|
|
|
|
)
|
|
|
|
.await
|
|
|
|
{
|
2021-04-14 07:39:06 +00:00
|
|
|
let (event_id, value) = match result {
|
|
|
|
Ok(t) => t,
|
2021-05-21 16:12:02 +00:00
|
|
|
Err(_) => continue,
|
2021-04-14 07:39:06 +00:00
|
|
|
};
|
|
|
|
|
2021-04-13 16:17:51 +00:00
|
|
|
let pdu = PduEvent::from_id_val(&event_id, value.clone()).map_err(|e| {
|
|
|
|
warn!("{:?}: {}", value, e);
|
|
|
|
Error::BadServerResponse("Invalid PDU in send_join response.")
|
|
|
|
})?;
|
2021-03-13 15:30:12 +00:00
|
|
|
|
2021-04-16 16:18:29 +00:00
|
|
|
db.rooms.add_pdu_outlier(&event_id, &value)?;
|
2021-03-13 15:30:12 +00:00
|
|
|
if let Some(state_key) = &pdu.state_key {
|
2021-03-17 21:30:25 +00:00
|
|
|
state.insert((pdu.kind.clone(), state_key.clone()), pdu.event_id.clone());
|
2021-03-13 15:30:12 +00:00
|
|
|
}
|
|
|
|
}
|
2020-08-22 01:44:55 +00:00
|
|
|
|
2021-03-13 15:30:12 +00:00
|
|
|
state.insert(
|
|
|
|
(
|
|
|
|
pdu.kind.clone(),
|
|
|
|
pdu.state_key.clone().expect("join event has state key"),
|
|
|
|
),
|
2021-03-17 21:30:25 +00:00
|
|
|
pdu.event_id.clone(),
|
2020-08-21 21:19:18 +00:00
|
|
|
);
|
2020-08-19 23:30:28 +00:00
|
|
|
|
2021-04-16 16:18:29 +00:00
|
|
|
if state.get(&(EventType::RoomCreate, "".to_owned())).is_none() {
|
|
|
|
return Err(Error::BadServerResponse("State contained no create event."));
|
|
|
|
}
|
|
|
|
|
|
|
|
db.rooms.force_state(room_id, state, &db)?;
|
2020-08-30 20:08:47 +00:00
|
|
|
|
2021-04-14 07:39:06 +00:00
|
|
|
for result in futures::future::join_all(
|
|
|
|
send_join_response
|
|
|
|
.room_state
|
|
|
|
.auth_chain
|
|
|
|
.iter()
|
|
|
|
.map(|pdu| validate_and_add_event_id(pdu, &room_version, &pub_key_map, &db)),
|
|
|
|
)
|
|
|
|
.await
|
|
|
|
{
|
|
|
|
let (event_id, value) = match result {
|
|
|
|
Ok(t) => t,
|
|
|
|
Err(_) => continue,
|
|
|
|
};
|
|
|
|
|
2021-04-16 16:18:29 +00:00
|
|
|
db.rooms.add_pdu_outlier(&event_id, &value)?;
|
2020-08-19 23:30:28 +00:00
|
|
|
}
|
2021-03-13 15:30:12 +00:00
|
|
|
|
2021-03-23 21:31:19 +00:00
|
|
|
// We append to state before appending the pdu, so we don't have a moment in time with the
|
|
|
|
// pdu without it's state. This is okay because append_pdu can't fail.
|
|
|
|
let statehashid = db.rooms.append_to_state(&pdu, &db.globals)?;
|
|
|
|
|
2021-03-13 15:30:12 +00:00
|
|
|
db.rooms.append_pdu(
|
|
|
|
&pdu,
|
|
|
|
utils::to_canonical_object(&pdu).expect("Pdu is valid canonical object"),
|
|
|
|
&[pdu.event_id.clone()],
|
|
|
|
db,
|
|
|
|
)?;
|
2021-03-23 21:31:19 +00:00
|
|
|
|
|
|
|
// We set the room state after inserting the pdu, so that we never have a moment in time
|
|
|
|
// where events in the current room state do not exist
|
|
|
|
db.rooms.set_room_state(&room_id, statehashid)?;
|
2020-09-12 19:30:07 +00:00
|
|
|
} else {
|
|
|
|
let event = member::MemberEventContent {
|
|
|
|
membership: member::MembershipState::Join,
|
2020-10-18 18:33:12 +00:00
|
|
|
displayname: db.users.displayname(&sender_user)?,
|
|
|
|
avatar_url: db.users.avatar_url(&sender_user)?,
|
2020-09-12 19:30:07 +00:00
|
|
|
is_direct: None,
|
|
|
|
third_party_invite: None,
|
2021-07-15 21:17:58 +00:00
|
|
|
blurhash: db.users.blurhash(&sender_user)?,
|
2020-09-12 19:30:07 +00:00
|
|
|
};
|
2020-08-19 23:30:28 +00:00
|
|
|
|
2020-10-05 20:19:22 +00:00
|
|
|
db.rooms.build_and_append_pdu(
|
|
|
|
PduBuilder {
|
|
|
|
event_type: EventType::RoomMember,
|
|
|
|
content: serde_json::to_value(event).expect("event is valid, we just created it"),
|
|
|
|
unsigned: None,
|
2020-10-18 18:33:12 +00:00
|
|
|
state_key: Some(sender_user.to_string()),
|
2020-10-05 20:19:22 +00:00
|
|
|
redacts: None,
|
|
|
|
},
|
2020-10-18 18:33:12 +00:00
|
|
|
&sender_user,
|
2020-10-05 20:19:22 +00:00
|
|
|
&room_id,
|
2021-01-15 16:05:57 +00:00
|
|
|
&db,
|
2021-07-13 13:44:25 +00:00
|
|
|
&mutex_lock,
|
2020-10-05 20:19:22 +00:00
|
|
|
)?;
|
2020-09-12 19:30:07 +00:00
|
|
|
}
|
2020-08-19 23:30:28 +00:00
|
|
|
|
2021-07-13 13:44:25 +00:00
|
|
|
drop(mutex_lock);
|
|
|
|
|
2021-04-13 13:00:45 +00:00
|
|
|
db.flush().await?;
|
|
|
|
|
2020-08-19 23:30:28 +00:00
|
|
|
Ok(join_room_by_id::Response::new(room_id.clone()).into())
|
|
|
|
}
|
2021-04-13 16:17:51 +00:00
|
|
|
|
|
|
|
async fn validate_and_add_event_id(
|
|
|
|
pdu: &Raw<Pdu>,
|
|
|
|
room_version: &RoomVersionId,
|
2021-04-13 19:34:31 +00:00
|
|
|
pub_key_map: &RwLock<BTreeMap<String, BTreeMap<String, String>>>,
|
2021-04-13 16:17:51 +00:00
|
|
|
db: &Database,
|
|
|
|
) -> Result<(EventId, CanonicalJsonObject)> {
|
|
|
|
let mut value = serde_json::from_str::<CanonicalJsonObject>(pdu.json().get()).map_err(|e| {
|
2021-05-21 16:12:02 +00:00
|
|
|
error!("Invalid PDU in server response: {:?}: {:?}", pdu, e);
|
2021-04-13 16:17:51 +00:00
|
|
|
Error::BadServerResponse("Invalid PDU in server response")
|
|
|
|
})?;
|
2021-05-20 21:46:52 +00:00
|
|
|
let event_id = EventId::try_from(&*format!(
|
|
|
|
"${}",
|
|
|
|
ruma::signatures::reference_hash(&value, &room_version)
|
|
|
|
.expect("ruma can calculate reference hashes")
|
|
|
|
))
|
|
|
|
.expect("ruma's reference hashes are valid event ids");
|
|
|
|
|
|
|
|
let back_off = |id| match db.globals.bad_event_ratelimiter.write().unwrap().entry(id) {
|
|
|
|
Entry::Vacant(e) => {
|
|
|
|
e.insert((Instant::now(), 1));
|
|
|
|
}
|
|
|
|
Entry::Occupied(mut e) => *e.get_mut() = (Instant::now(), e.get().1 + 1),
|
|
|
|
};
|
|
|
|
|
|
|
|
if let Some((time, tries)) = db
|
|
|
|
.globals
|
|
|
|
.bad_event_ratelimiter
|
|
|
|
.read()
|
|
|
|
.unwrap()
|
|
|
|
.get(&event_id)
|
|
|
|
{
|
|
|
|
// Exponential backoff
|
|
|
|
let mut min_elapsed_duration = Duration::from_secs(30) * (*tries) * (*tries);
|
|
|
|
if min_elapsed_duration > Duration::from_secs(60 * 60 * 24) {
|
|
|
|
min_elapsed_duration = Duration::from_secs(60 * 60 * 24);
|
|
|
|
}
|
|
|
|
|
|
|
|
if time.elapsed() < min_elapsed_duration {
|
|
|
|
debug!("Backing off from {}", event_id);
|
|
|
|
return Err(Error::BadServerResponse("bad event, still backing off"));
|
|
|
|
}
|
|
|
|
}
|
2021-04-13 16:17:51 +00:00
|
|
|
|
|
|
|
server_server::fetch_required_signing_keys(&value, pub_key_map, db).await?;
|
2021-04-13 19:34:31 +00:00
|
|
|
if let Err(e) = ruma::signatures::verify_event(
|
|
|
|
&*pub_key_map
|
|
|
|
.read()
|
|
|
|
.map_err(|_| Error::bad_database("RwLock is poisoned."))?,
|
|
|
|
&value,
|
|
|
|
room_version,
|
|
|
|
) {
|
2021-05-21 16:12:02 +00:00
|
|
|
warn!("Event {} failed verification {:?} {}", event_id, pdu, e);
|
2021-05-20 21:46:52 +00:00
|
|
|
back_off(event_id);
|
2021-04-13 19:34:31 +00:00
|
|
|
return Err(Error::BadServerResponse("Event failed verification."));
|
|
|
|
}
|
2021-04-13 16:17:51 +00:00
|
|
|
|
|
|
|
value.insert(
|
|
|
|
"event_id".to_owned(),
|
2021-04-26 16:20:20 +00:00
|
|
|
CanonicalJsonValue::String(event_id.as_str().to_owned()),
|
2021-04-13 16:17:51 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
Ok((event_id, value))
|
|
|
|
}
|
2021-04-25 12:10:07 +00:00
|
|
|
|
2021-07-13 13:44:25 +00:00
|
|
|
pub async fn invite_helper<'a>(
|
2021-04-25 12:10:07 +00:00
|
|
|
sender_user: &UserId,
|
|
|
|
user_id: &UserId,
|
|
|
|
room_id: &RoomId,
|
|
|
|
db: &Database,
|
|
|
|
is_direct: bool,
|
|
|
|
) -> Result<()> {
|
2021-07-13 13:44:25 +00:00
|
|
|
let mutex = Arc::clone(
|
|
|
|
db.globals
|
|
|
|
.roomid_mutex
|
|
|
|
.write()
|
|
|
|
.unwrap()
|
|
|
|
.entry(room_id.clone())
|
|
|
|
.or_default(),
|
|
|
|
);
|
|
|
|
let mutex_lock = mutex.lock().await;
|
|
|
|
|
2021-04-25 12:10:07 +00:00
|
|
|
if user_id.server_name() != db.globals.server_name() {
|
|
|
|
let prev_events = db
|
|
|
|
.rooms
|
|
|
|
.get_pdu_leaves(room_id)?
|
|
|
|
.into_iter()
|
|
|
|
.take(20)
|
|
|
|
.collect::<Vec<_>>();
|
|
|
|
|
|
|
|
let create_event = db
|
|
|
|
.rooms
|
|
|
|
.room_state_get(room_id, &EventType::RoomCreate, "")?;
|
|
|
|
|
|
|
|
let create_event_content = create_event
|
|
|
|
.as_ref()
|
|
|
|
.map(|create_event| {
|
2021-06-17 18:34:14 +00:00
|
|
|
serde_json::from_value::<Raw<CreateEventContent>>(create_event.content.clone())
|
|
|
|
.expect("Raw::from_value always works.")
|
|
|
|
.deserialize()
|
|
|
|
.map_err(|_| Error::bad_database("Invalid PowerLevels event in db."))
|
2021-04-25 12:10:07 +00:00
|
|
|
})
|
|
|
|
.transpose()?;
|
|
|
|
|
|
|
|
let create_prev_event = if prev_events.len() == 1
|
|
|
|
&& Some(&prev_events[0]) == create_event.as_ref().map(|c| &c.event_id)
|
|
|
|
{
|
2021-06-30 07:52:01 +00:00
|
|
|
create_event
|
2021-04-25 12:10:07 +00:00
|
|
|
} else {
|
|
|
|
None
|
|
|
|
};
|
|
|
|
|
|
|
|
// If there was no create event yet, assume we are creating a version 6 room right now
|
2021-05-07 23:54:24 +00:00
|
|
|
let room_version_id = create_event_content
|
|
|
|
.map_or(RoomVersionId::Version6, |create_event| {
|
|
|
|
create_event.room_version
|
|
|
|
});
|
|
|
|
let room_version = RoomVersion::new(&room_version_id).expect("room version is supported");
|
2021-04-25 12:10:07 +00:00
|
|
|
|
|
|
|
let content = serde_json::to_value(MemberEventContent {
|
|
|
|
avatar_url: None,
|
|
|
|
displayname: None,
|
|
|
|
is_direct: Some(is_direct),
|
|
|
|
membership: MembershipState::Invite,
|
|
|
|
third_party_invite: None,
|
2021-07-15 21:17:58 +00:00
|
|
|
blurhash: db.users.blurhash(&sender_user)?,
|
2021-04-25 12:10:07 +00:00
|
|
|
})
|
|
|
|
.expect("member event is valid value");
|
|
|
|
|
|
|
|
let state_key = user_id.to_string();
|
|
|
|
let kind = EventType::RoomMember;
|
|
|
|
|
|
|
|
let auth_events =
|
|
|
|
db.rooms
|
|
|
|
.get_auth_events(room_id, &kind, &sender_user, Some(&state_key), &content)?;
|
|
|
|
|
|
|
|
// Our depth is the maximum depth of prev_events + 1
|
|
|
|
let depth = prev_events
|
|
|
|
.iter()
|
|
|
|
.filter_map(|event_id| Some(db.rooms.get_pdu(event_id).ok()??.depth))
|
|
|
|
.max()
|
|
|
|
.unwrap_or_else(|| uint!(0))
|
|
|
|
+ uint!(1);
|
|
|
|
|
|
|
|
let mut unsigned = BTreeMap::new();
|
|
|
|
|
|
|
|
if let Some(prev_pdu) = db.rooms.room_state_get(room_id, &kind, &state_key)? {
|
2021-06-30 07:52:01 +00:00
|
|
|
unsigned.insert("prev_content".to_owned(), prev_pdu.content.clone());
|
2021-04-25 12:10:07 +00:00
|
|
|
unsigned.insert(
|
|
|
|
"prev_sender".to_owned(),
|
2021-06-30 07:52:01 +00:00
|
|
|
serde_json::to_value(&prev_pdu.sender).expect("UserId::to_value always works"),
|
2021-04-25 12:10:07 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
let pdu = PduEvent {
|
|
|
|
event_id: ruma::event_id!("$thiswillbefilledinlater"),
|
|
|
|
room_id: room_id.clone(),
|
|
|
|
sender: sender_user.clone(),
|
|
|
|
origin_server_ts: utils::millis_since_unix_epoch()
|
|
|
|
.try_into()
|
|
|
|
.expect("time is valid"),
|
|
|
|
kind,
|
|
|
|
content,
|
|
|
|
state_key: Some(state_key),
|
|
|
|
prev_events,
|
|
|
|
depth,
|
|
|
|
auth_events: auth_events
|
|
|
|
.iter()
|
|
|
|
.map(|(_, pdu)| pdu.event_id.clone())
|
|
|
|
.collect(),
|
|
|
|
redacts: None,
|
|
|
|
unsigned,
|
|
|
|
hashes: ruma::events::pdu::EventHash {
|
|
|
|
sha256: "aaa".to_owned(),
|
|
|
|
},
|
|
|
|
signatures: BTreeMap::new(),
|
|
|
|
};
|
|
|
|
|
|
|
|
let auth_check = state_res::auth_check(
|
|
|
|
&room_version,
|
|
|
|
&Arc::new(pdu.clone()),
|
|
|
|
create_prev_event,
|
|
|
|
&auth_events,
|
|
|
|
None, // TODO: third_party_invite
|
|
|
|
)
|
|
|
|
.map_err(|e| {
|
|
|
|
error!("{:?}", e);
|
|
|
|
Error::bad_database("Auth check failed.")
|
|
|
|
})?;
|
|
|
|
|
|
|
|
if !auth_check {
|
|
|
|
return Err(Error::BadRequest(
|
2021-05-22 16:49:30 +00:00
|
|
|
ErrorKind::Forbidden,
|
2021-04-25 12:10:07 +00:00
|
|
|
"Event is not authorized.",
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Hash and sign
|
|
|
|
let mut pdu_json =
|
|
|
|
utils::to_canonical_object(&pdu).expect("event is valid, we just created it");
|
|
|
|
|
|
|
|
pdu_json.remove("event_id");
|
|
|
|
|
|
|
|
// Add origin because synapse likes that (and it's required in the spec)
|
|
|
|
pdu_json.insert(
|
|
|
|
"origin".to_owned(),
|
|
|
|
to_canonical_value(db.globals.server_name())
|
|
|
|
.expect("server name is a valid CanonicalJsonValue"),
|
|
|
|
);
|
|
|
|
|
|
|
|
ruma::signatures::hash_and_sign_event(
|
|
|
|
db.globals.server_name().as_str(),
|
|
|
|
db.globals.keypair(),
|
|
|
|
&mut pdu_json,
|
2021-05-07 23:54:24 +00:00
|
|
|
&room_version_id,
|
2021-04-25 12:10:07 +00:00
|
|
|
)
|
|
|
|
.expect("event is valid, we just created it");
|
|
|
|
|
2021-07-13 13:44:25 +00:00
|
|
|
drop(mutex_lock);
|
|
|
|
|
2021-04-25 12:10:07 +00:00
|
|
|
let invite_room_state = db.rooms.calculate_invite_state(&pdu)?;
|
|
|
|
let response = db
|
|
|
|
.sending
|
|
|
|
.send_federation_request(
|
|
|
|
&db.globals,
|
|
|
|
user_id.server_name(),
|
|
|
|
create_invite::v2::Request {
|
|
|
|
room_id: room_id.clone(),
|
|
|
|
event_id: ruma::event_id!("$receivingservershouldsetthis"),
|
|
|
|
room_version: RoomVersionId::Version6,
|
|
|
|
event: PduEvent::convert_to_outgoing_federation_event(pdu_json),
|
|
|
|
invite_room_state,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
.await?;
|
|
|
|
|
|
|
|
let pub_key_map = RwLock::new(BTreeMap::new());
|
|
|
|
|
|
|
|
// We do not add the event_id field to the pdu here because of signature and hashes checks
|
|
|
|
let (event_id, value) = match crate::pdu::gen_event_id_canonical_json(&response.event) {
|
|
|
|
Ok(t) => t,
|
|
|
|
Err(_) => {
|
|
|
|
// Event could not be converted to canonical json
|
|
|
|
return Err(Error::BadRequest(
|
|
|
|
ErrorKind::InvalidParam,
|
|
|
|
"Could not convert event to canonical json.",
|
|
|
|
));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
let origin = serde_json::from_value::<Box<ServerName>>(
|
|
|
|
serde_json::to_value(value.get("origin").ok_or(Error::BadRequest(
|
|
|
|
ErrorKind::InvalidParam,
|
|
|
|
"Event needs an origin field.",
|
|
|
|
))?)
|
|
|
|
.expect("CanonicalJson is valid json value"),
|
|
|
|
)
|
|
|
|
.map_err(|_| Error::BadRequest(ErrorKind::InvalidParam, "Origin field is invalid."))?;
|
|
|
|
|
2021-07-01 17:55:26 +00:00
|
|
|
let pdu_id = server_server::handle_incoming_pdu(
|
|
|
|
&origin,
|
|
|
|
&event_id,
|
|
|
|
&room_id,
|
|
|
|
value,
|
|
|
|
true,
|
|
|
|
&db,
|
|
|
|
&pub_key_map,
|
|
|
|
)
|
|
|
|
.await
|
|
|
|
.map_err(|_| {
|
|
|
|
Error::BadRequest(
|
|
|
|
ErrorKind::InvalidParam,
|
|
|
|
"Error while handling incoming PDU.",
|
|
|
|
)
|
|
|
|
})?
|
|
|
|
.ok_or(Error::BadRequest(
|
|
|
|
ErrorKind::InvalidParam,
|
|
|
|
"Could not accept incoming PDU as timeline event.",
|
|
|
|
))?;
|
2021-04-25 12:10:07 +00:00
|
|
|
|
|
|
|
for server in db
|
|
|
|
.rooms
|
|
|
|
.room_servers(room_id)
|
|
|
|
.filter_map(|r| r.ok())
|
|
|
|
.filter(|server| &**server != db.globals.server_name())
|
|
|
|
{
|
|
|
|
db.sending.send_pdu(&server, &pdu_id)?;
|
|
|
|
}
|
|
|
|
|
|
|
|
return Ok(());
|
|
|
|
}
|
|
|
|
|
|
|
|
db.rooms.build_and_append_pdu(
|
|
|
|
PduBuilder {
|
|
|
|
event_type: EventType::RoomMember,
|
|
|
|
content: serde_json::to_value(member::MemberEventContent {
|
|
|
|
membership: member::MembershipState::Invite,
|
|
|
|
displayname: db.users.displayname(&user_id)?,
|
|
|
|
avatar_url: db.users.avatar_url(&user_id)?,
|
2021-05-28 20:53:00 +00:00
|
|
|
is_direct: Some(is_direct),
|
2021-04-25 12:10:07 +00:00
|
|
|
third_party_invite: None,
|
2021-07-15 21:17:58 +00:00
|
|
|
blurhash: db.users.blurhash(&sender_user)?,
|
2021-04-25 12:10:07 +00:00
|
|
|
})
|
|
|
|
.expect("event is valid, we just created it"),
|
|
|
|
unsigned: None,
|
|
|
|
state_key: Some(user_id.to_string()),
|
|
|
|
redacts: None,
|
|
|
|
},
|
|
|
|
&sender_user,
|
|
|
|
room_id,
|
|
|
|
&db,
|
2021-07-13 13:44:25 +00:00
|
|
|
&mutex_lock,
|
2021-04-25 12:10:07 +00:00
|
|
|
)?;
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|