2020-07-30 17:14:47 +01:00
|
|
|
use super::State;
|
2020-08-14 10:31:31 +01:00
|
|
|
use crate::{
|
2020-08-18 21:26:03 +01:00
|
|
|
client_server,
|
|
|
|
pdu::{PduBuilder, PduEvent},
|
2020-09-13 21:24:36 +01:00
|
|
|
server_server, utils, ConduitResult, Database, Error, Result, Ruma,
|
2020-08-14 10:31:31 +01:00
|
|
|
};
|
2020-09-12 20:30:07 +01:00
|
|
|
use log::warn;
|
2020-07-30 17:14:47 +01:00
|
|
|
use ruma::{
|
2020-08-14 10:31:31 +01:00
|
|
|
api::{
|
|
|
|
client::{
|
|
|
|
error::ErrorKind,
|
2020-08-20 17:12:02 +01: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 10:31:31 +01:00
|
|
|
},
|
2020-07-30 17:14:47 +01:00
|
|
|
},
|
2020-08-14 10:31:31 +01:00
|
|
|
federation,
|
2020-07-30 17:14:47 +01:00
|
|
|
},
|
2020-09-13 21:24:36 +01:00
|
|
|
events::pdu::Pdu,
|
2020-07-30 17:14:47 +01:00
|
|
|
events::{room::member, EventType},
|
2020-08-20 00:30:28 +01:00
|
|
|
EventId, Raw, RoomId, RoomVersionId, UserId,
|
2020-07-30 17:14:47 +01:00
|
|
|
};
|
2020-08-06 13:29:59 +01:00
|
|
|
use state_res::StateEvent;
|
2020-09-13 21:24:36 +01:00
|
|
|
use std::{
|
|
|
|
collections::BTreeMap, collections::HashMap, collections::HashSet, convert::TryFrom, sync::Arc,
|
|
|
|
};
|
2020-07-30 17:14:47 +01:00
|
|
|
|
|
|
|
#[cfg(feature = "conduit_bin")]
|
|
|
|
use rocket::{get, post};
|
|
|
|
|
|
|
|
#[cfg_attr(
|
|
|
|
feature = "conduit_bin",
|
|
|
|
post("/_matrix/client/r0/rooms/<_>/join", data = "<body>")
|
|
|
|
)]
|
2020-08-14 10:31:31 +01:00
|
|
|
pub async fn join_room_by_id_route(
|
2020-07-30 17:14:47 +01:00
|
|
|
db: State<'_, Database>,
|
2020-09-08 16:32:03 +01:00
|
|
|
body: Ruma<join_room_by_id::Request<'_>>,
|
2020-07-30 17:14:47 +01:00
|
|
|
) -> ConduitResult<join_room_by_id::Response> {
|
2020-08-20 00:30:28 +01:00
|
|
|
join_room_by_id_helper(
|
|
|
|
&db,
|
|
|
|
body.sender_id.as_ref(),
|
|
|
|
&body.room_id,
|
|
|
|
body.third_party_signed.as_ref(),
|
|
|
|
)
|
|
|
|
.await
|
2020-07-30 17:14:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg_attr(
|
|
|
|
feature = "conduit_bin",
|
|
|
|
post("/_matrix/client/r0/join/<_>", data = "<body>")
|
|
|
|
)]
|
2020-08-14 10:31:31 +01:00
|
|
|
pub async fn join_room_by_id_or_alias_route(
|
2020-07-30 17:14:47 +01:00
|
|
|
db: State<'_, Database>,
|
2020-09-08 16:32:03 +01:00
|
|
|
body: Ruma<join_room_by_id_or_alias::Request<'_>>,
|
2020-07-30 17:14:47 +01:00
|
|
|
) -> ConduitResult<join_room_by_id_or_alias::Response> {
|
2020-08-14 10:31:31 +01:00
|
|
|
let room_id = match RoomId::try_from(body.room_id_or_alias.clone()) {
|
|
|
|
Ok(room_id) => room_id,
|
|
|
|
Err(room_alias) => {
|
2020-08-21 22:19:18 +01:00
|
|
|
client_server::get_alias_helper(&db, &room_alias)
|
2020-08-20 17:12:02 +01:00
|
|
|
.await?
|
|
|
|
.0
|
|
|
|
.room_id
|
2020-08-14 10:31:31 +01:00
|
|
|
}
|
|
|
|
};
|
2020-07-30 17:14:47 +01:00
|
|
|
|
|
|
|
Ok(join_room_by_id_or_alias::Response {
|
2020-08-20 00:30:28 +01:00
|
|
|
room_id: join_room_by_id_helper(
|
2020-08-21 22:19:18 +01:00
|
|
|
&db,
|
2020-08-20 00:30:28 +01:00
|
|
|
body.sender_id.as_ref(),
|
|
|
|
&room_id,
|
|
|
|
body.third_party_signed.as_ref(),
|
|
|
|
)
|
|
|
|
.await?
|
|
|
|
.0
|
|
|
|
.room_id,
|
2020-07-30 17:14:47 +01:00
|
|
|
}
|
|
|
|
.into())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg_attr(
|
|
|
|
feature = "conduit_bin",
|
|
|
|
post("/_matrix/client/r0/rooms/<_>/leave", data = "<body>")
|
|
|
|
)]
|
|
|
|
pub fn leave_room_route(
|
|
|
|
db: State<'_, Database>,
|
2020-09-08 16:32:03 +01:00
|
|
|
body: Ruma<leave_room::Request<'_>>,
|
2020-07-30 17:14:47 +01:00
|
|
|
) -> ConduitResult<leave_room::Response> {
|
|
|
|
let sender_id = body.sender_id.as_ref().expect("user is authenticated");
|
|
|
|
|
|
|
|
let mut event = serde_json::from_value::<Raw<member::MemberEventContent>>(
|
|
|
|
db.rooms
|
|
|
|
.room_state_get(
|
|
|
|
&body.room_id,
|
|
|
|
&EventType::RoomMember,
|
|
|
|
&sender_id.to_string(),
|
|
|
|
)?
|
|
|
|
.ok_or(Error::BadRequest(
|
|
|
|
ErrorKind::BadState,
|
|
|
|
"Cannot leave a room you are not a member of.",
|
|
|
|
))?
|
|
|
|
.content,
|
|
|
|
)
|
|
|
|
.expect("from_value::<Raw<..>> can never fail")
|
|
|
|
.deserialize()
|
|
|
|
.map_err(|_| Error::bad_database("Invalid member event in database."))?;
|
|
|
|
|
|
|
|
event.membership = member::MembershipState::Leave;
|
|
|
|
|
2020-08-18 21:26:03 +01:00
|
|
|
db.rooms.build_and_append_pdu(
|
2020-07-30 17:14:47 +01:00
|
|
|
PduBuilder {
|
|
|
|
event_type: EventType::RoomMember,
|
|
|
|
content: serde_json::to_value(event).expect("event is valid, we just created it"),
|
|
|
|
unsigned: None,
|
|
|
|
state_key: Some(sender_id.to_string()),
|
|
|
|
redacts: None,
|
|
|
|
},
|
2020-09-12 20:30:07 +01:00
|
|
|
&sender_id,
|
|
|
|
&body.room_id,
|
2020-07-30 17:14:47 +01:00
|
|
|
&db.globals,
|
|
|
|
&db.account_data,
|
|
|
|
)?;
|
|
|
|
|
2020-08-06 13:29:59 +01:00
|
|
|
Ok(leave_room::Response::new().into())
|
2020-07-30 17:14:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg_attr(
|
|
|
|
feature = "conduit_bin",
|
|
|
|
post("/_matrix/client/r0/rooms/<_>/invite", data = "<body>")
|
|
|
|
)]
|
|
|
|
pub fn invite_user_route(
|
|
|
|
db: State<'_, Database>,
|
2020-09-08 16:32:03 +01:00
|
|
|
body: Ruma<invite_user::Request<'_>>,
|
2020-07-30 17:14:47 +01:00
|
|
|
) -> ConduitResult<invite_user::Response> {
|
|
|
|
let sender_id = body.sender_id.as_ref().expect("user is authenticated");
|
|
|
|
|
2020-09-08 16:32:03 +01:00
|
|
|
if let invite_user::IncomingInvitationRecipient::UserId { user_id } = &body.recipient {
|
2020-08-18 21:26:03 +01:00
|
|
|
db.rooms.build_and_append_pdu(
|
2020-07-30 17:14:47 +01:00
|
|
|
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)?,
|
|
|
|
is_direct: None,
|
|
|
|
third_party_invite: None,
|
|
|
|
})
|
|
|
|
.expect("event is valid, we just created it"),
|
|
|
|
unsigned: None,
|
|
|
|
state_key: Some(user_id.to_string()),
|
|
|
|
redacts: None,
|
|
|
|
},
|
2020-09-12 20:30:07 +01:00
|
|
|
&sender_id,
|
|
|
|
&body.room_id,
|
2020-07-30 17:14:47 +01:00
|
|
|
&db.globals,
|
|
|
|
&db.account_data,
|
|
|
|
)?;
|
|
|
|
|
|
|
|
Ok(invite_user::Response.into())
|
|
|
|
} else {
|
|
|
|
Err(Error::BadRequest(ErrorKind::NotFound, "User not found."))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg_attr(
|
|
|
|
feature = "conduit_bin",
|
|
|
|
post("/_matrix/client/r0/rooms/<_>/kick", data = "<body>")
|
|
|
|
)]
|
|
|
|
pub fn kick_user_route(
|
|
|
|
db: State<'_, Database>,
|
2020-09-08 16:32:03 +01:00
|
|
|
body: Ruma<kick_user::Request<'_>>,
|
2020-07-30 17:14:47 +01:00
|
|
|
) -> ConduitResult<kick_user::Response> {
|
|
|
|
let sender_id = body.sender_id.as_ref().expect("user is authenticated");
|
|
|
|
|
|
|
|
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.",
|
|
|
|
))?
|
|
|
|
.content,
|
|
|
|
)
|
|
|
|
.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
|
|
|
|
|
2020-08-18 21:26:03 +01:00
|
|
|
db.rooms.build_and_append_pdu(
|
2020-07-30 17:14:47 +01:00
|
|
|
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-09-12 20:30:07 +01:00
|
|
|
&sender_id,
|
|
|
|
&body.room_id,
|
2020-07-30 17:14:47 +01:00
|
|
|
&db.globals,
|
|
|
|
&db.account_data,
|
|
|
|
)?;
|
|
|
|
|
2020-08-06 13:29:59 +01:00
|
|
|
Ok(kick_user::Response::new().into())
|
2020-07-30 17:14:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg_attr(
|
|
|
|
feature = "conduit_bin",
|
|
|
|
post("/_matrix/client/r0/rooms/<_>/ban", data = "<body>")
|
|
|
|
)]
|
|
|
|
pub fn ban_user_route(
|
|
|
|
db: State<'_, Database>,
|
2020-09-08 16:32:03 +01:00
|
|
|
body: Ruma<ban_user::Request<'_>>,
|
2020-07-30 17:14:47 +01:00
|
|
|
) -> ConduitResult<ban_user::Response> {
|
|
|
|
let sender_id = body.sender_id.as_ref().expect("user is authenticated");
|
|
|
|
|
|
|
|
// 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,
|
|
|
|
}),
|
|
|
|
|event| {
|
|
|
|
let mut event =
|
|
|
|
serde_json::from_value::<Raw<member::MemberEventContent>>(event.content)
|
|
|
|
.expect("Raw::from_value always works")
|
|
|
|
.deserialize()
|
|
|
|
.map_err(|_| Error::bad_database("Invalid member event in database."))?;
|
|
|
|
event.membership = ruma::events::room::member::MembershipState::Ban;
|
|
|
|
Ok(event)
|
|
|
|
},
|
|
|
|
)?;
|
|
|
|
|
2020-08-18 21:26:03 +01:00
|
|
|
db.rooms.build_and_append_pdu(
|
2020-07-30 17:14:47 +01:00
|
|
|
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-09-12 20:30:07 +01:00
|
|
|
&sender_id,
|
|
|
|
&body.room_id,
|
2020-07-30 17:14:47 +01:00
|
|
|
&db.globals,
|
|
|
|
&db.account_data,
|
|
|
|
)?;
|
|
|
|
|
2020-08-06 13:29:59 +01:00
|
|
|
Ok(ban_user::Response::new().into())
|
2020-07-30 17:14:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg_attr(
|
|
|
|
feature = "conduit_bin",
|
|
|
|
post("/_matrix/client/r0/rooms/<_>/unban", data = "<body>")
|
|
|
|
)]
|
|
|
|
pub fn unban_user_route(
|
|
|
|
db: State<'_, Database>,
|
2020-09-08 16:32:03 +01:00
|
|
|
body: Ruma<unban_user::Request<'_>>,
|
2020-07-30 17:14:47 +01:00
|
|
|
) -> ConduitResult<unban_user::Response> {
|
|
|
|
let sender_id = body.sender_id.as_ref().expect("user is authenticated");
|
|
|
|
|
|
|
|
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.",
|
|
|
|
))?
|
|
|
|
.content,
|
|
|
|
)
|
|
|
|
.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;
|
|
|
|
|
2020-08-18 21:26:03 +01:00
|
|
|
db.rooms.build_and_append_pdu(
|
2020-07-30 17:14:47 +01:00
|
|
|
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-09-12 20:30:07 +01:00
|
|
|
&sender_id,
|
|
|
|
&body.room_id,
|
2020-07-30 17:14:47 +01:00
|
|
|
&db.globals,
|
|
|
|
&db.account_data,
|
|
|
|
)?;
|
|
|
|
|
2020-08-06 13:29:59 +01:00
|
|
|
Ok(unban_user::Response::new().into())
|
2020-07-30 17:14:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg_attr(
|
|
|
|
feature = "conduit_bin",
|
|
|
|
post("/_matrix/client/r0/rooms/<_>/forget", data = "<body>")
|
|
|
|
)]
|
|
|
|
pub fn forget_room_route(
|
|
|
|
db: State<'_, Database>,
|
2020-09-08 16:32:03 +01:00
|
|
|
body: Ruma<forget_room::Request<'_>>,
|
2020-07-30 17:14:47 +01:00
|
|
|
) -> ConduitResult<forget_room::Response> {
|
|
|
|
let sender_id = body.sender_id.as_ref().expect("user is authenticated");
|
|
|
|
|
|
|
|
db.rooms.forget(&body.room_id, &sender_id)?;
|
|
|
|
|
2020-08-06 13:29:59 +01:00
|
|
|
Ok(forget_room::Response::new().into())
|
2020-07-30 17:14:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg_attr(
|
|
|
|
feature = "conduit_bin",
|
|
|
|
get("/_matrix/client/r0/joined_rooms", data = "<body>")
|
|
|
|
)]
|
|
|
|
pub fn joined_rooms_route(
|
|
|
|
db: State<'_, Database>,
|
|
|
|
body: Ruma<joined_rooms::Request>,
|
|
|
|
) -> ConduitResult<joined_rooms::Response> {
|
|
|
|
let sender_id = body.sender_id.as_ref().expect("user is authenticated");
|
|
|
|
|
|
|
|
Ok(joined_rooms::Response {
|
|
|
|
joined_rooms: db
|
|
|
|
.rooms
|
|
|
|
.rooms_joined(&sender_id)
|
|
|
|
.filter_map(|r| r.ok())
|
|
|
|
.collect(),
|
|
|
|
}
|
|
|
|
.into())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg_attr(
|
|
|
|
feature = "conduit_bin",
|
|
|
|
get("/_matrix/client/r0/rooms/<_>/members", data = "<body>")
|
|
|
|
)]
|
|
|
|
pub fn get_member_events_route(
|
|
|
|
db: State<'_, Database>,
|
2020-09-08 16:32:03 +01:00
|
|
|
body: Ruma<get_member_events::Request<'_>>,
|
2020-07-30 17:14:47 +01:00
|
|
|
) -> ConduitResult<get_member_events::Response> {
|
|
|
|
let sender_id = body.sender_id.as_ref().expect("user is authenticated");
|
|
|
|
|
|
|
|
if !db.rooms.is_joined(sender_id, &body.room_id)? {
|
|
|
|
return Err(Error::BadRequest(
|
|
|
|
ErrorKind::Forbidden,
|
|
|
|
"You don't have permission to view this room.",
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(get_member_events::Response {
|
|
|
|
chunk: db
|
|
|
|
.rooms
|
|
|
|
.room_state_type(&body.room_id, &EventType::RoomMember)?
|
|
|
|
.values()
|
|
|
|
.map(|pdu| pdu.to_member_event())
|
|
|
|
.collect(),
|
|
|
|
}
|
|
|
|
.into())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg_attr(
|
|
|
|
feature = "conduit_bin",
|
|
|
|
get("/_matrix/client/r0/rooms/<_>/joined_members", data = "<body>")
|
|
|
|
)]
|
|
|
|
pub fn joined_members_route(
|
|
|
|
db: State<'_, Database>,
|
2020-09-08 16:32:03 +01:00
|
|
|
body: Ruma<joined_members::Request<'_>>,
|
2020-07-30 17:14:47 +01:00
|
|
|
) -> ConduitResult<joined_members::Response> {
|
|
|
|
let sender_id = body.sender_id.as_ref().expect("user is authenticated");
|
|
|
|
|
|
|
|
if !db
|
|
|
|
.rooms
|
|
|
|
.is_joined(&sender_id, &body.room_id)
|
|
|
|
.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-20 00:30:28 +01:00
|
|
|
|
|
|
|
async fn join_room_by_id_helper(
|
|
|
|
db: &Database,
|
|
|
|
sender_id: Option<&UserId>,
|
|
|
|
room_id: &RoomId,
|
|
|
|
_third_party_signed: Option<&IncomingThirdPartySigned>,
|
|
|
|
) -> ConduitResult<join_room_by_id::Response> {
|
|
|
|
let sender_id = sender_id.expect("user is authenticated");
|
|
|
|
|
|
|
|
// 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() {
|
|
|
|
let make_join_response = server_server::send_request(
|
|
|
|
&db,
|
|
|
|
room_id.server_name().to_string(),
|
|
|
|
federation::membership::create_join_event_template::v1::Request {
|
2020-09-08 16:32:03 +01:00
|
|
|
room_id,
|
|
|
|
user_id: sender_id,
|
|
|
|
ver: &[RoomVersionId::Version5, RoomVersionId::Version6],
|
2020-08-20 00:30:28 +01:00
|
|
|
},
|
|
|
|
)
|
|
|
|
.await?;
|
|
|
|
|
|
|
|
let mut join_event_stub_value =
|
|
|
|
serde_json::from_str::<serde_json::Value>(make_join_response.event.json().get())
|
|
|
|
.map_err(|_| {
|
|
|
|
Error::BadServerResponse("Invalid make_join event json received from server.")
|
|
|
|
})?;
|
|
|
|
|
|
|
|
let join_event_stub =
|
|
|
|
join_event_stub_value
|
|
|
|
.as_object_mut()
|
|
|
|
.ok_or(Error::BadServerResponse(
|
|
|
|
"Invalid make join event object received from server.",
|
|
|
|
))?;
|
|
|
|
|
|
|
|
join_event_stub.insert(
|
|
|
|
"origin".to_owned(),
|
|
|
|
db.globals.server_name().to_owned().to_string().into(),
|
|
|
|
);
|
|
|
|
join_event_stub.insert(
|
|
|
|
"origin_server_ts".to_owned(),
|
|
|
|
utils::millis_since_unix_epoch().into(),
|
|
|
|
);
|
|
|
|
|
|
|
|
// Generate event id
|
|
|
|
let event_id = EventId::try_from(&*format!(
|
|
|
|
"${}",
|
|
|
|
ruma::signatures::reference_hash(&join_event_stub_value)
|
|
|
|
.expect("ruma can calculate reference hashes")
|
|
|
|
))
|
|
|
|
.expect("ruma's reference hashes are valid event ids");
|
|
|
|
|
|
|
|
// We don't leave the event id into the pdu because that's only allowed in v1 or v2 rooms
|
|
|
|
let join_event_stub = join_event_stub_value.as_object_mut().unwrap();
|
|
|
|
join_event_stub.remove("event_id");
|
|
|
|
|
|
|
|
ruma::signatures::hash_and_sign_event(
|
|
|
|
db.globals.server_name().as_str(),
|
|
|
|
db.globals.keypair(),
|
|
|
|
&mut join_event_stub_value,
|
|
|
|
)
|
|
|
|
.expect("event is valid, we just created it");
|
|
|
|
|
|
|
|
let send_join_response = server_server::send_request(
|
|
|
|
&db,
|
|
|
|
room_id.server_name().to_string(),
|
2020-09-12 20:30:07 +01:00
|
|
|
federation::membership::create_join_event::v2::Request {
|
2020-09-08 16:32:03 +01:00
|
|
|
room_id,
|
|
|
|
event_id: &event_id,
|
2020-08-20 00:30:28 +01:00
|
|
|
pdu_stub: serde_json::from_value(join_event_stub_value)
|
2020-08-20 17:12:02 +01:00
|
|
|
.expect("we just created this event"),
|
2020-08-20 00:30:28 +01:00
|
|
|
},
|
|
|
|
)
|
|
|
|
.await?;
|
|
|
|
|
2020-09-13 21:24:36 +01:00
|
|
|
let add_event_id = |pdu: &Raw<Pdu>| {
|
|
|
|
let mut value = serde_json::from_str(pdu.json().get())
|
|
|
|
.expect("converting raw jsons to values always works");
|
|
|
|
let event_id = EventId::try_from(&*format!(
|
|
|
|
"${}",
|
|
|
|
ruma::signatures::reference_hash(&value)
|
|
|
|
.expect("ruma can calculate reference hashes")
|
|
|
|
))
|
|
|
|
.expect("ruma's reference hashes are valid event ids");
|
|
|
|
|
|
|
|
value
|
|
|
|
.as_object_mut()
|
|
|
|
.ok_or_else(|| Error::BadServerResponse("PDU is not an object."))?
|
|
|
|
.insert("event_id".to_owned(), event_id.to_string().into());
|
|
|
|
|
|
|
|
Ok((event_id, value))
|
|
|
|
};
|
|
|
|
|
|
|
|
let room_state = send_join_response.room_state.state.iter().map(add_event_id);
|
|
|
|
|
|
|
|
let state_events = room_state
|
|
|
|
.clone()
|
|
|
|
.map(|pdu: Result<(EventId, serde_json::Value)>| Ok(pdu?.0))
|
|
|
|
.collect::<Result<HashSet<EventId>>>()?;
|
|
|
|
|
|
|
|
let auth_chain = send_join_response
|
2020-08-20 00:30:28 +01:00
|
|
|
.room_state
|
2020-09-13 21:24:36 +01:00
|
|
|
.auth_chain
|
2020-08-20 00:30:28 +01:00
|
|
|
.iter()
|
2020-09-13 21:24:36 +01:00
|
|
|
.map(add_event_id);
|
2020-09-12 22:38:52 +01:00
|
|
|
|
2020-09-13 21:24:36 +01:00
|
|
|
let mut event_map = room_state
|
|
|
|
.chain(auth_chain)
|
|
|
|
.map(|r| {
|
|
|
|
let (event_id, value) = r?;
|
2020-09-12 20:30:07 +01:00
|
|
|
serde_json::from_value::<StateEvent>(value)
|
2020-09-13 21:24:36 +01:00
|
|
|
.map(|ev| (event_id, Arc::new(ev)))
|
2020-09-12 20:30:07 +01:00
|
|
|
.map_err(|e| {
|
|
|
|
warn!("{}", e);
|
|
|
|
Error::BadServerResponse("Invalid PDU bytes in send_join response.")
|
|
|
|
})
|
2020-08-20 00:30:28 +01:00
|
|
|
})
|
2020-09-13 21:24:36 +01:00
|
|
|
.collect::<Result<BTreeMap<EventId, Arc<StateEvent>>>>()?;
|
2020-08-20 00:30:28 +01:00
|
|
|
|
2020-08-30 21:08:47 +01:00
|
|
|
let control_events = event_map
|
2020-08-22 02:44:55 +01:00
|
|
|
.values()
|
|
|
|
.filter(|pdu| pdu.is_power_event())
|
2020-09-12 20:30:07 +01:00
|
|
|
.map(|pdu| pdu.event_id().clone())
|
2020-08-22 02:44:55 +01:00
|
|
|
.collect::<Vec<_>>();
|
|
|
|
|
2020-08-30 21:08:47 +01:00
|
|
|
// These events are not guaranteed to be sorted but they are resolved according to spec
|
|
|
|
// we auth them anyways to weed out faulty/malicious server. The following is basically the
|
|
|
|
// full state resolution algorithm.
|
2020-09-12 22:38:52 +01:00
|
|
|
let event_ids = event_map.keys().cloned().collect::<Vec<_>>();
|
|
|
|
|
2020-08-30 21:08:47 +01:00
|
|
|
let sorted_control_events = state_res::StateResolution::reverse_topological_power_sort(
|
2020-08-21 22:19:18 +01:00
|
|
|
&room_id,
|
2020-08-30 21:08:47 +01:00
|
|
|
&control_events,
|
2020-08-22 02:44:55 +01:00
|
|
|
&mut event_map,
|
|
|
|
&db.rooms,
|
2020-09-12 22:38:52 +01:00
|
|
|
&event_ids,
|
2020-08-22 02:44:55 +01:00
|
|
|
);
|
|
|
|
|
2020-08-30 21:08:47 +01:00
|
|
|
// Auth check each event against the "partial" state created by the preceding events
|
|
|
|
let resolved_control_events = state_res::StateResolution::iterative_auth_check(
|
2020-08-22 02:44:55 +01:00
|
|
|
room_id,
|
|
|
|
&RoomVersionId::Version6,
|
2020-08-30 21:08:47 +01:00
|
|
|
&sorted_control_events,
|
|
|
|
&BTreeMap::new(), // We have no "clean/resolved" events to add (these extend the `resolved_control_events`)
|
2020-08-22 02:44:55 +01:00
|
|
|
&mut event_map,
|
|
|
|
&db.rooms,
|
|
|
|
)
|
|
|
|
.expect("iterative auth check failed on resolved events");
|
|
|
|
|
2020-08-30 21:08:47 +01:00
|
|
|
// This removes the control events that failed auth, leaving the resolved
|
|
|
|
// to be mainline sorted
|
2020-08-22 02:44:55 +01:00
|
|
|
let events_to_sort = event_map
|
|
|
|
.keys()
|
2020-08-30 21:08:47 +01:00
|
|
|
.filter(|id| {
|
|
|
|
!sorted_control_events.contains(id)
|
|
|
|
|| resolved_control_events.values().any(|rid| *id == rid)
|
|
|
|
})
|
2020-08-22 02:44:55 +01:00
|
|
|
.cloned()
|
|
|
|
.collect::<Vec<_>>();
|
|
|
|
|
2020-08-30 21:08:47 +01:00
|
|
|
let power_level =
|
|
|
|
resolved_control_events.get(&(EventType::RoomPowerLevels, Some("".into())));
|
|
|
|
// Sort the remaining non control events
|
2020-08-22 02:44:55 +01:00
|
|
|
let sorted_event_ids = state_res::StateResolution::mainline_sort(
|
|
|
|
room_id,
|
|
|
|
&events_to_sort,
|
|
|
|
power_level,
|
2020-08-21 22:19:18 +01:00
|
|
|
&mut event_map,
|
|
|
|
&db.rooms,
|
|
|
|
);
|
2020-08-20 00:30:28 +01:00
|
|
|
|
2020-08-30 21:08:47 +01:00
|
|
|
let resolved_events = state_res::StateResolution::iterative_auth_check(
|
|
|
|
room_id,
|
|
|
|
&RoomVersionId::Version6,
|
|
|
|
&sorted_event_ids,
|
|
|
|
&resolved_control_events,
|
|
|
|
&mut event_map,
|
|
|
|
&db.rooms,
|
|
|
|
)
|
|
|
|
.expect("iterative auth check failed on resolved events");
|
|
|
|
|
2020-09-13 21:24:36 +01:00
|
|
|
let mut state = HashMap::new();
|
|
|
|
|
2020-08-30 21:08:47 +01:00
|
|
|
// filter the events that failed the auth check keeping the remaining events
|
|
|
|
// sorted correctly
|
|
|
|
for ev_id in sorted_event_ids
|
|
|
|
.iter()
|
|
|
|
.filter(|id| resolved_events.values().any(|rid| rid == *id))
|
|
|
|
{
|
2020-08-20 00:30:28 +01:00
|
|
|
// this is a `state_res::StateEvent` that holds a `ruma::Pdu`
|
|
|
|
let pdu = event_map
|
|
|
|
.get(ev_id)
|
|
|
|
.expect("Found event_id in sorted events that is not in resolved state");
|
|
|
|
|
|
|
|
// We do not rebuild the PDU in this case only insert to DB
|
2020-09-13 21:24:36 +01:00
|
|
|
let pdu_id =
|
|
|
|
db.rooms
|
|
|
|
.append_pdu(&PduEvent::from(&**pdu), &db.globals, &db.account_data)?;
|
|
|
|
|
|
|
|
if state_events.contains(ev_id) {
|
|
|
|
state.insert(
|
|
|
|
(
|
|
|
|
pdu.kind(),
|
|
|
|
pdu.state_key().expect("State events have a state key"),
|
|
|
|
),
|
|
|
|
pdu_id,
|
|
|
|
);
|
|
|
|
}
|
2020-08-20 00:30:28 +01:00
|
|
|
}
|
2020-09-13 21:24:36 +01:00
|
|
|
|
|
|
|
db.rooms.force_state(room_id, state)?;
|
2020-09-12 20:30:07 +01:00
|
|
|
} else {
|
|
|
|
let event = member::MemberEventContent {
|
|
|
|
membership: member::MembershipState::Join,
|
|
|
|
displayname: db.users.displayname(&sender_id)?,
|
|
|
|
avatar_url: db.users.avatar_url(&sender_id)?,
|
|
|
|
is_direct: None,
|
|
|
|
third_party_invite: None,
|
|
|
|
};
|
2020-08-20 00:30:28 +01:00
|
|
|
|
2020-09-12 20:30:07 +01: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(sender_id.to_string()),
|
|
|
|
redacts: None,
|
|
|
|
},
|
|
|
|
&sender_id,
|
|
|
|
&room_id,
|
|
|
|
&db.globals,
|
|
|
|
&db.account_data,
|
|
|
|
)?;
|
|
|
|
}
|
2020-08-20 00:30:28 +01:00
|
|
|
|
|
|
|
Ok(join_room_by_id::Response::new(room_id.clone()).into())
|
|
|
|
}
|