feat: access control
parent
c8ba9dce01
commit
b6c0e9bfb2
|
@ -45,7 +45,7 @@ use ruma_client_api::{
|
||||||
};
|
};
|
||||||
use ruma_events::{
|
use ruma_events::{
|
||||||
collections::only::Event as EduEvent,
|
collections::only::Event as EduEvent,
|
||||||
room::{guest_access, history_visibility, join_rules},
|
room::{guest_access, history_visibility, join_rules, member},
|
||||||
EventJson, EventType,
|
EventJson, EventType,
|
||||||
};
|
};
|
||||||
use ruma_identifiers::{RoomId, RoomVersionId, UserId};
|
use ruma_identifiers::{RoomId, RoomVersionId, UserId};
|
||||||
|
@ -246,13 +246,11 @@ pub fn login_route(
|
||||||
) -> MatrixResult<login::Response> {
|
) -> MatrixResult<login::Response> {
|
||||||
// Validate login method
|
// Validate login method
|
||||||
let user_id =
|
let user_id =
|
||||||
if let (login::UserInfo::MatrixId(mut username), login::LoginInfo::Password { password }) =
|
// TODO: Other login methods
|
||||||
|
if let (login::UserInfo::MatrixId(username), login::LoginInfo::Password { password }) =
|
||||||
(body.user.clone(), body.login_info.clone())
|
(body.user.clone(), body.login_info.clone())
|
||||||
{
|
{
|
||||||
if !username.contains(':') {
|
if let Ok(user_id) = UserId::parse_with_server_name(username, db.globals.server_name()) {
|
||||||
username = format!("@{}:{}", username, db.globals.server_name());
|
|
||||||
}
|
|
||||||
if let Ok(user_id) = (*username).try_into() {
|
|
||||||
if let Some(hash) = db.users.password_hash(&user_id).unwrap() {
|
if let Some(hash) = db.users.password_hash(&user_id).unwrap() {
|
||||||
let hash_matches =
|
let hash_matches =
|
||||||
argon2::verify_encoded(&hash, password.as_bytes()).unwrap_or(false);
|
argon2::verify_encoded(&hash, password.as_bytes()).unwrap_or(false);
|
||||||
|
@ -349,6 +347,8 @@ pub fn get_pushrules_all_route(
|
||||||
body: Ruma<get_pushrules_all::Request>,
|
body: Ruma<get_pushrules_all::Request>,
|
||||||
) -> MatrixResult<get_pushrules_all::Response> {
|
) -> MatrixResult<get_pushrules_all::Response> {
|
||||||
let user_id = body.user_id.as_ref().expect("user is authenticated");
|
let user_id = body.user_id.as_ref().expect("user is authenticated");
|
||||||
|
warn!("TODO: get_pushrules_all_route");
|
||||||
|
|
||||||
if let Some(EduEvent::PushRules(pushrules)) = db
|
if let Some(EduEvent::PushRules(pushrules)) = db
|
||||||
.account_data
|
.account_data
|
||||||
.get(None, &user_id, &EventType::PushRules)
|
.get(None, &user_id, &EventType::PushRules)
|
||||||
|
@ -472,7 +472,7 @@ pub fn get_global_account_data_route(
|
||||||
MatrixResult(Err(Error {
|
MatrixResult(Err(Error {
|
||||||
kind: ErrorKind::NotFound,
|
kind: ErrorKind::NotFound,
|
||||||
message: "Data not found.".to_owned(),
|
message: "Data not found.".to_owned(),
|
||||||
status_code: http::StatusCode::NOT_FOUND,
|
status_code: http::StatusCode::BAD_REQUEST,
|
||||||
}))
|
}))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -485,76 +485,60 @@ pub fn set_displayname_route(
|
||||||
) -> MatrixResult<set_display_name::Response> {
|
) -> MatrixResult<set_display_name::Response> {
|
||||||
let user_id = body.user_id.as_ref().expect("user is authenticated");
|
let user_id = body.user_id.as_ref().expect("user is authenticated");
|
||||||
|
|
||||||
if let Some(displayname) = &body.displayname {
|
db.users
|
||||||
// Some("") will clear the displayname
|
.set_displayname(&user_id, body.displayname.clone())
|
||||||
if displayname == "" {
|
.unwrap();
|
||||||
db.users.set_displayname(&user_id, None).unwrap();
|
|
||||||
} else {
|
|
||||||
db.users
|
|
||||||
.set_displayname(&user_id, Some(displayname.clone()))
|
|
||||||
.unwrap();
|
|
||||||
}
|
|
||||||
|
|
||||||
// Send a new membership event into all joined rooms
|
// Send a new membership event into all joined rooms
|
||||||
for room_id in db.rooms.rooms_joined(&user_id) {
|
for room_id in db.rooms.rooms_joined(&user_id) {
|
||||||
let room_id = room_id.unwrap();
|
let room_id = room_id.unwrap();
|
||||||
db.rooms
|
db.rooms
|
||||||
.append_pdu(
|
.append_pdu(
|
||||||
room_id.clone(),
|
room_id.clone(),
|
||||||
user_id.clone(),
|
user_id.clone(),
|
||||||
EventType::RoomMember,
|
EventType::RoomMember,
|
||||||
serde_json::to_value(ruma_events::room::member::MemberEventContent {
|
serde_json::to_value(ruma_events::room::member::MemberEventContent {
|
||||||
displayname: Some(displayname.clone()),
|
displayname: body.displayname.clone(),
|
||||||
..serde_json::from_value::<EventJson<_>>(
|
..serde_json::from_value::<EventJson<_>>(
|
||||||
db.rooms
|
db.rooms
|
||||||
.room_state(&room_id)
|
.room_state(&room_id)
|
||||||
.unwrap()
|
.unwrap()
|
||||||
.get(&(EventType::RoomMember, user_id.to_string()))
|
.get(&(EventType::RoomMember, user_id.to_string()))
|
||||||
.expect("user should be part of the room")
|
.expect("user is part of the room")
|
||||||
.content
|
.content
|
||||||
.clone(),
|
.clone(),
|
||||||
)
|
)
|
||||||
.unwrap()
|
.unwrap()
|
||||||
.deserialize()
|
.deserialize()
|
||||||
.unwrap()
|
.unwrap()
|
||||||
})
|
})
|
||||||
.unwrap(),
|
.unwrap(),
|
||||||
None,
|
None,
|
||||||
Some(user_id.to_string()),
|
Some(user_id.to_string()),
|
||||||
&db.globals,
|
|
||||||
)
|
|
||||||
.unwrap();
|
|
||||||
}
|
|
||||||
|
|
||||||
// Presence update
|
|
||||||
db.global_edus
|
|
||||||
.update_globallatest(
|
|
||||||
&user_id,
|
|
||||||
EduEvent::Presence(ruma_events::presence::PresenceEvent {
|
|
||||||
content: ruma_events::presence::PresenceEventContent {
|
|
||||||
avatar_url: db.users.avatar_url(&user_id).unwrap(),
|
|
||||||
currently_active: None,
|
|
||||||
displayname: db.users.displayname(&user_id).unwrap(),
|
|
||||||
last_active_ago: Some(utils::millis_since_unix_epoch().try_into().unwrap()),
|
|
||||||
presence: ruma_events::presence::PresenceState::Online,
|
|
||||||
status_msg: None,
|
|
||||||
},
|
|
||||||
sender: user_id.clone(),
|
|
||||||
}),
|
|
||||||
&db.globals,
|
&db.globals,
|
||||||
)
|
)
|
||||||
.unwrap();
|
.unwrap();
|
||||||
} else {
|
|
||||||
// Send error on None
|
|
||||||
// Synapse returns a parsing error but the spec doesn't require this
|
|
||||||
debug!("Request was missing the displayname payload.");
|
|
||||||
return MatrixResult(Err(Error {
|
|
||||||
kind: ErrorKind::MissingParam,
|
|
||||||
message: "Missing displayname.".to_owned(),
|
|
||||||
status_code: http::StatusCode::BAD_REQUEST,
|
|
||||||
}));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Presence update
|
||||||
|
db.global_edus
|
||||||
|
.update_globallatest(
|
||||||
|
&user_id,
|
||||||
|
EduEvent::Presence(ruma_events::presence::PresenceEvent {
|
||||||
|
content: ruma_events::presence::PresenceEventContent {
|
||||||
|
avatar_url: db.users.avatar_url(&user_id).unwrap(),
|
||||||
|
currently_active: None,
|
||||||
|
displayname: db.users.displayname(&user_id).unwrap(),
|
||||||
|
last_active_ago: Some(utils::millis_since_unix_epoch().try_into().unwrap()),
|
||||||
|
presence: ruma_events::presence::PresenceState::Online,
|
||||||
|
status_msg: None,
|
||||||
|
},
|
||||||
|
sender: user_id.clone(),
|
||||||
|
}),
|
||||||
|
&db.globals,
|
||||||
|
)
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
MatrixResult(Ok(set_display_name::Response))
|
MatrixResult(Ok(set_display_name::Response))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -565,23 +549,9 @@ pub fn get_displayname_route(
|
||||||
_user_id: String,
|
_user_id: String,
|
||||||
) -> MatrixResult<get_display_name::Response> {
|
) -> MatrixResult<get_display_name::Response> {
|
||||||
let user_id = (*body).user_id.clone();
|
let user_id = (*body).user_id.clone();
|
||||||
if !db.users.exists(&user_id).unwrap() {
|
MatrixResult(Ok(get_display_name::Response {
|
||||||
// Return 404 if we don't have a profile for this id
|
displayname: db.users.displayname(&user_id).unwrap(),
|
||||||
debug!("Profile was not found.");
|
}))
|
||||||
return MatrixResult(Err(Error {
|
|
||||||
kind: ErrorKind::NotFound,
|
|
||||||
message: "Profile was not found.".to_owned(),
|
|
||||||
status_code: http::StatusCode::NOT_FOUND,
|
|
||||||
}));
|
|
||||||
}
|
|
||||||
if let Some(displayname) = db.users.displayname(&user_id).unwrap() {
|
|
||||||
return MatrixResult(Ok(get_display_name::Response {
|
|
||||||
displayname: Some(displayname),
|
|
||||||
}));
|
|
||||||
}
|
|
||||||
|
|
||||||
// The user has no displayname
|
|
||||||
MatrixResult(Ok(get_display_name::Response { displayname: None }))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[put("/_matrix/client/r0/profile/<_user_id>/avatar_url", data = "<body>")]
|
#[put("/_matrix/client/r0/profile/<_user_id>/avatar_url", data = "<body>")]
|
||||||
|
@ -592,76 +562,74 @@ pub fn set_avatar_url_route(
|
||||||
) -> MatrixResult<set_avatar_url::Response> {
|
) -> MatrixResult<set_avatar_url::Response> {
|
||||||
let user_id = body.user_id.as_ref().expect("user is authenticated");
|
let user_id = body.user_id.as_ref().expect("user is authenticated");
|
||||||
|
|
||||||
if !body.avatar_url.starts_with("mxc://") {
|
if let avatar_url = &body.avatar_url {
|
||||||
debug!("Request contains an invalid avatar_url.");
|
if !avatar_url.starts_with("mxc://") {
|
||||||
return MatrixResult(Err(Error {
|
debug!("Request contains an invalid avatar_url.");
|
||||||
kind: ErrorKind::InvalidParam,
|
return MatrixResult(Err(Error {
|
||||||
message: "avatar_url has to start with mxc://.".to_owned(),
|
kind: ErrorKind::InvalidParam,
|
||||||
status_code: http::StatusCode::BAD_REQUEST,
|
message: "avatar_url has to start with mxc://.".to_owned(),
|
||||||
}));
|
status_code: http::StatusCode::BAD_REQUEST,
|
||||||
}
|
}));
|
||||||
|
|
||||||
// TODO in the future when we can handle media uploads make sure that this url is our own server
|
|
||||||
// TODO also make sure this is valid mxc:// format (not only starting with it)
|
|
||||||
|
|
||||||
if body.avatar_url == "" {
|
|
||||||
db.users.set_avatar_url(&user_id, None).unwrap();
|
|
||||||
} else {
|
|
||||||
db.users
|
|
||||||
.set_avatar_url(&user_id, Some(body.avatar_url.clone()))
|
|
||||||
.unwrap();
|
|
||||||
|
|
||||||
// Send a new membership event into all joined rooms
|
|
||||||
for room_id in db.rooms.rooms_joined(&user_id) {
|
|
||||||
let room_id = room_id.unwrap();
|
|
||||||
db.rooms
|
|
||||||
.append_pdu(
|
|
||||||
room_id.clone(),
|
|
||||||
user_id.clone(),
|
|
||||||
EventType::RoomMember,
|
|
||||||
serde_json::to_value(ruma_events::room::member::MemberEventContent {
|
|
||||||
avatar_url: Some(body.avatar_url.clone()),
|
|
||||||
..serde_json::from_value::<EventJson<_>>(
|
|
||||||
db.rooms
|
|
||||||
.room_state(&room_id)
|
|
||||||
.unwrap()
|
|
||||||
.get(&(EventType::RoomMember, user_id.to_string()))
|
|
||||||
.expect("user should be part of the room")
|
|
||||||
.content
|
|
||||||
.clone(),
|
|
||||||
)
|
|
||||||
.unwrap()
|
|
||||||
.deserialize()
|
|
||||||
.unwrap()
|
|
||||||
})
|
|
||||||
.unwrap(),
|
|
||||||
None,
|
|
||||||
Some(user_id.to_string()),
|
|
||||||
&db.globals,
|
|
||||||
)
|
|
||||||
.unwrap();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Presence update
|
// TODO in the future when we can handle media uploads make sure that this url is our own server
|
||||||
db.global_edus
|
// TODO also make sure this is valid mxc:// format (not only starting with it)
|
||||||
.update_globallatest(
|
}
|
||||||
&user_id,
|
|
||||||
EduEvent::Presence(ruma_events::presence::PresenceEvent {
|
db.users
|
||||||
content: ruma_events::presence::PresenceEventContent {
|
.set_avatar_url(&user_id, Some(body.avatar_url.clone()))
|
||||||
avatar_url: db.users.avatar_url(&user_id).unwrap(),
|
.unwrap();
|
||||||
currently_active: None,
|
|
||||||
displayname: db.users.displayname(&user_id).unwrap(),
|
// Send a new membership event into all joined rooms
|
||||||
last_active_ago: Some(utils::millis_since_unix_epoch().try_into().unwrap()),
|
for room_id in db.rooms.rooms_joined(&user_id) {
|
||||||
presence: ruma_events::presence::PresenceState::Online,
|
let room_id = room_id.unwrap();
|
||||||
status_msg: None,
|
db.rooms
|
||||||
},
|
.append_pdu(
|
||||||
sender: user_id.clone(),
|
room_id.clone(),
|
||||||
}),
|
user_id.clone(),
|
||||||
|
EventType::RoomMember,
|
||||||
|
serde_json::to_value(ruma_events::room::member::MemberEventContent {
|
||||||
|
avatar_url: Some(body.avatar_url.clone()),
|
||||||
|
..serde_json::from_value::<EventJson<_>>(
|
||||||
|
db.rooms
|
||||||
|
.room_state(&room_id)
|
||||||
|
.unwrap()
|
||||||
|
.get(&(EventType::RoomMember, user_id.to_string()))
|
||||||
|
.expect("user should be part of the room")
|
||||||
|
.content
|
||||||
|
.clone(),
|
||||||
|
)
|
||||||
|
.unwrap()
|
||||||
|
.deserialize()
|
||||||
|
.unwrap()
|
||||||
|
})
|
||||||
|
.unwrap(),
|
||||||
|
None,
|
||||||
|
Some(user_id.to_string()),
|
||||||
&db.globals,
|
&db.globals,
|
||||||
)
|
)
|
||||||
.unwrap();
|
.unwrap();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Presence update
|
||||||
|
db.global_edus
|
||||||
|
.update_globallatest(
|
||||||
|
&user_id,
|
||||||
|
EduEvent::Presence(ruma_events::presence::PresenceEvent {
|
||||||
|
content: ruma_events::presence::PresenceEventContent {
|
||||||
|
avatar_url: db.users.avatar_url(&user_id).unwrap(),
|
||||||
|
currently_active: None,
|
||||||
|
displayname: db.users.displayname(&user_id).unwrap(),
|
||||||
|
last_active_ago: Some(utils::millis_since_unix_epoch().try_into().unwrap()),
|
||||||
|
presence: ruma_events::presence::PresenceState::Online,
|
||||||
|
status_msg: None,
|
||||||
|
},
|
||||||
|
sender: user_id.clone(),
|
||||||
|
}),
|
||||||
|
&db.globals,
|
||||||
|
)
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
MatrixResult(Ok(set_avatar_url::Response))
|
MatrixResult(Ok(set_avatar_url::Response))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -672,23 +640,9 @@ pub fn get_avatar_url_route(
|
||||||
_user_id: String,
|
_user_id: String,
|
||||||
) -> MatrixResult<get_avatar_url::Response> {
|
) -> MatrixResult<get_avatar_url::Response> {
|
||||||
let user_id = (*body).user_id.clone();
|
let user_id = (*body).user_id.clone();
|
||||||
if !db.users.exists(&user_id).unwrap() {
|
MatrixResult(Ok(get_avatar_url::Response {
|
||||||
// Return 404 if we don't have a profile for this id
|
avatar_url: db.users.avatar_url(&user_id).unwrap(),
|
||||||
debug!("Profile was not found.");
|
}))
|
||||||
return MatrixResult(Err(Error {
|
|
||||||
kind: ErrorKind::NotFound,
|
|
||||||
message: "Profile was not found.".to_owned(),
|
|
||||||
status_code: http::StatusCode::NOT_FOUND,
|
|
||||||
}));
|
|
||||||
}
|
|
||||||
if let Some(avatar_url) = db.users.avatar_url(&user_id).unwrap() {
|
|
||||||
return MatrixResult(Ok(get_avatar_url::Response {
|
|
||||||
avatar_url: Some(avatar_url),
|
|
||||||
}));
|
|
||||||
}
|
|
||||||
|
|
||||||
// The user has no avatar
|
|
||||||
MatrixResult(Ok(get_avatar_url::Response { avatar_url: None }))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[get("/_matrix/client/r0/profile/<_user_id>", data = "<body>")]
|
#[get("/_matrix/client/r0/profile/<_user_id>", data = "<body>")]
|
||||||
|
@ -713,7 +667,7 @@ pub fn get_profile_route(
|
||||||
MatrixResult(Err(Error {
|
MatrixResult(Err(Error {
|
||||||
kind: ErrorKind::NotFound,
|
kind: ErrorKind::NotFound,
|
||||||
message: "Profile was not found.".to_owned(),
|
message: "Profile was not found.".to_owned(),
|
||||||
status_code: http::StatusCode::NOT_FOUND,
|
status_code: http::StatusCode::BAD_REQUEST,
|
||||||
}))
|
}))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -947,10 +901,11 @@ pub fn create_room_route(
|
||||||
db: State<'_, Database>,
|
db: State<'_, Database>,
|
||||||
body: Ruma<create_room::Request>,
|
body: Ruma<create_room::Request>,
|
||||||
) -> MatrixResult<create_room::Response> {
|
) -> MatrixResult<create_room::Response> {
|
||||||
// TODO: check if room is unique
|
|
||||||
let room_id = RoomId::new(db.globals.server_name()).expect("host is valid");
|
let room_id = RoomId::new(db.globals.server_name()).expect("host is valid");
|
||||||
let user_id = body.user_id.as_ref().expect("user is authenticated");
|
let user_id = body.user_id.as_ref().expect("user is authenticated");
|
||||||
|
|
||||||
|
// TODO: Create alias and check if it already exists
|
||||||
|
|
||||||
db.rooms
|
db.rooms
|
||||||
.append_pdu(
|
.append_pdu(
|
||||||
room_id.clone(),
|
room_id.clone(),
|
||||||
|
@ -972,8 +927,24 @@ pub fn create_room_route(
|
||||||
)
|
)
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
||||||
|
// Join room
|
||||||
db.rooms
|
db.rooms
|
||||||
.join(&room_id, &user_id, &db.users, &db.globals)
|
.append_pdu(
|
||||||
|
room_id.clone(),
|
||||||
|
user_id.clone(),
|
||||||
|
EventType::RoomMember,
|
||||||
|
serde_json::to_value(member::MemberEventContent {
|
||||||
|
membership: member::MembershipState::Join,
|
||||||
|
displayname: db.users.displayname(&user_id).unwrap(),
|
||||||
|
avatar_url: db.users.avatar_url(&user_id).unwrap(),
|
||||||
|
is_direct: body.is_direct,
|
||||||
|
third_party_invite: None,
|
||||||
|
})
|
||||||
|
.unwrap(),
|
||||||
|
None,
|
||||||
|
Some(user_id.to_string()),
|
||||||
|
&db.globals,
|
||||||
|
)
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
||||||
// Figure out preset. We need it for power levels and preset specific events
|
// Figure out preset. We need it for power levels and preset specific events
|
||||||
|
@ -1145,7 +1116,22 @@ pub fn create_room_route(
|
||||||
// 4. Events implied by invite (and TODO: invite_3pid)
|
// 4. Events implied by invite (and TODO: invite_3pid)
|
||||||
for user in &body.invite {
|
for user in &body.invite {
|
||||||
db.rooms
|
db.rooms
|
||||||
.invite(&user_id, &room_id, user, &db.globals)
|
.append_pdu(
|
||||||
|
room_id.clone(),
|
||||||
|
user_id.clone(),
|
||||||
|
EventType::RoomMember,
|
||||||
|
serde_json::to_value(member::MemberEventContent {
|
||||||
|
membership: member::MembershipState::Invite,
|
||||||
|
displayname: db.users.displayname(&user).unwrap(),
|
||||||
|
avatar_url: db.users.avatar_url(&user).unwrap(),
|
||||||
|
is_direct: body.is_direct,
|
||||||
|
third_party_invite: None,
|
||||||
|
})
|
||||||
|
.unwrap(),
|
||||||
|
None,
|
||||||
|
Some(user.to_string()),
|
||||||
|
&db.globals,
|
||||||
|
)
|
||||||
.unwrap();
|
.unwrap();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1167,7 +1153,7 @@ pub fn get_alias_route(
|
||||||
return MatrixResult(Err(Error {
|
return MatrixResult(Err(Error {
|
||||||
kind: ErrorKind::NotFound,
|
kind: ErrorKind::NotFound,
|
||||||
message: "Room not found.".to_owned(),
|
message: "Room not found.".to_owned(),
|
||||||
status_code: http::StatusCode::NOT_FOUND,
|
status_code: http::StatusCode::BAD_REQUEST,
|
||||||
}));
|
}));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1191,23 +1177,54 @@ pub fn join_room_by_id_route(
|
||||||
) -> MatrixResult<join_room_by_id::Response> {
|
) -> MatrixResult<join_room_by_id::Response> {
|
||||||
let user_id = body.user_id.as_ref().expect("user is authenticated");
|
let user_id = body.user_id.as_ref().expect("user is authenticated");
|
||||||
|
|
||||||
if db
|
// TODO: Ask a remote server if we don't have this room
|
||||||
|
|
||||||
|
let event = db
|
||||||
.rooms
|
.rooms
|
||||||
.join(&body.room_id, &user_id, &db.users, &db.globals)
|
.room_state(&body.room_id)
|
||||||
.is_ok()
|
.unwrap()
|
||||||
{
|
.get(&(EventType::RoomMember, user_id.to_string()))
|
||||||
MatrixResult(Ok(join_room_by_id::Response {
|
.map_or_else(
|
||||||
room_id: body.room_id.clone(),
|
|| {
|
||||||
}))
|
// There was no existing membership event
|
||||||
} else {
|
member::MemberEventContent {
|
||||||
// We don't have this room. Let's ask a remote server
|
membership: member::MembershipState::Join,
|
||||||
// TODO
|
displayname: db.users.displayname(&user_id).unwrap(),
|
||||||
MatrixResult(Err(Error {
|
avatar_url: db.users.avatar_url(&user_id).unwrap(),
|
||||||
kind: ErrorKind::NotFound,
|
is_direct: None,
|
||||||
message: "Room not found.".to_owned(),
|
third_party_invite: None,
|
||||||
status_code: http::StatusCode::NOT_FOUND,
|
}
|
||||||
}))
|
},
|
||||||
}
|
|pdu| {
|
||||||
|
// We change the existing membership event
|
||||||
|
let mut event = serde_json::from_value::<EventJson<member::MemberEventContent>>(
|
||||||
|
pdu.content.clone(),
|
||||||
|
)
|
||||||
|
.unwrap()
|
||||||
|
.deserialize()
|
||||||
|
.unwrap();
|
||||||
|
event.membership = member::MembershipState::Join;
|
||||||
|
event.displayname = db.users.displayname(&user_id).unwrap();
|
||||||
|
event.avatar_url = db.users.avatar_url(&user_id).unwrap();
|
||||||
|
event
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
||||||
|
db.rooms
|
||||||
|
.append_pdu(
|
||||||
|
body.room_id.clone(),
|
||||||
|
user_id.clone(),
|
||||||
|
EventType::RoomMember,
|
||||||
|
serde_json::to_value(event).unwrap(),
|
||||||
|
None,
|
||||||
|
Some(user_id.to_string()),
|
||||||
|
&db.globals,
|
||||||
|
)
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
MatrixResult(Ok(join_room_by_id::Response {
|
||||||
|
room_id: body.room_id.clone(),
|
||||||
|
}))
|
||||||
}
|
}
|
||||||
|
|
||||||
#[post("/_matrix/client/r0/join/<_room_id_or_alias>", data = "<body>")]
|
#[post("/_matrix/client/r0/join/<_room_id_or_alias>", data = "<body>")]
|
||||||
|
@ -1223,7 +1240,7 @@ pub fn join_room_by_id_or_alias_route(
|
||||||
return MatrixResult(Err(Error {
|
return MatrixResult(Err(Error {
|
||||||
kind: ErrorKind::NotFound,
|
kind: ErrorKind::NotFound,
|
||||||
message: "Room alias not found.".to_owned(),
|
message: "Room alias not found.".to_owned(),
|
||||||
status_code: http::StatusCode::NOT_FOUND,
|
status_code: http::StatusCode::BAD_REQUEST,
|
||||||
}));
|
}));
|
||||||
} else {
|
} else {
|
||||||
// Ask creator server of the room to join TODO ask someone else when not available
|
// Ask creator server of the room to join TODO ask someone else when not available
|
||||||
|
@ -1257,9 +1274,19 @@ pub fn leave_room_route(
|
||||||
_room_id: String,
|
_room_id: String,
|
||||||
) -> MatrixResult<leave_room::Response> {
|
) -> MatrixResult<leave_room::Response> {
|
||||||
let user_id = body.user_id.as_ref().expect("user is authenticated");
|
let user_id = body.user_id.as_ref().expect("user is authenticated");
|
||||||
|
|
||||||
db.rooms
|
db.rooms
|
||||||
.leave(&user_id, &body.room_id, &user_id, &db.globals)
|
.append_pdu(
|
||||||
|
body.room_id.clone(),
|
||||||
|
user_id.clone(),
|
||||||
|
EventType::RoomMember,
|
||||||
|
json!({"membership": "leave"}),
|
||||||
|
None,
|
||||||
|
Some(user_id.to_string()),
|
||||||
|
&db.globals,
|
||||||
|
)
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
||||||
MatrixResult(Ok(leave_room::Response))
|
MatrixResult(Ok(leave_room::Response))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1270,7 +1297,9 @@ pub fn forget_room_route(
|
||||||
_room_id: String,
|
_room_id: String,
|
||||||
) -> MatrixResult<forget_room::Response> {
|
) -> MatrixResult<forget_room::Response> {
|
||||||
let user_id = body.user_id.as_ref().expect("user is authenticated");
|
let user_id = body.user_id.as_ref().expect("user is authenticated");
|
||||||
|
|
||||||
db.rooms.forget(&body.room_id, &user_id).unwrap();
|
db.rooms.forget(&body.room_id, &user_id).unwrap();
|
||||||
|
|
||||||
MatrixResult(Ok(forget_room::Response))
|
MatrixResult(Ok(forget_room::Response))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1281,20 +1310,32 @@ pub fn invite_user_route(
|
||||||
_room_id: String,
|
_room_id: String,
|
||||||
) -> MatrixResult<invite_user::Response> {
|
) -> MatrixResult<invite_user::Response> {
|
||||||
if let invite_user::InvitationRecipient::UserId { user_id } = &body.recipient {
|
if let invite_user::InvitationRecipient::UserId { user_id } = &body.recipient {
|
||||||
|
let event = member::MemberEventContent {
|
||||||
|
membership: member::MembershipState::Invite,
|
||||||
|
displayname: db.users.displayname(&user_id).unwrap(),
|
||||||
|
avatar_url: db.users.avatar_url(&user_id).unwrap(),
|
||||||
|
is_direct: None,
|
||||||
|
third_party_invite: None,
|
||||||
|
};
|
||||||
|
|
||||||
db.rooms
|
db.rooms
|
||||||
.invite(
|
.append_pdu(
|
||||||
&body.user_id.as_ref().expect("user is authenticated"),
|
body.room_id.clone(),
|
||||||
&body.room_id,
|
body.user_id.clone().expect("user is authenticated"),
|
||||||
&user_id,
|
EventType::RoomMember,
|
||||||
|
serde_json::to_value(event).unwrap(),
|
||||||
|
None,
|
||||||
|
Some(user_id.to_string()),
|
||||||
&db.globals,
|
&db.globals,
|
||||||
)
|
)
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
||||||
MatrixResult(Ok(invite_user::Response))
|
MatrixResult(Ok(invite_user::Response))
|
||||||
} else {
|
} else {
|
||||||
MatrixResult(Err(Error {
|
MatrixResult(Err(Error {
|
||||||
kind: ErrorKind::NotFound,
|
kind: ErrorKind::NotFound,
|
||||||
message: "User not found.".to_owned(),
|
message: "User not found.".to_owned(),
|
||||||
status_code: http::StatusCode::NOT_FOUND,
|
status_code: http::StatusCode::BAD_REQUEST,
|
||||||
}))
|
}))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1483,20 +1524,23 @@ pub fn create_message_event_route(
|
||||||
let mut unsigned = serde_json::Map::new();
|
let mut unsigned = serde_json::Map::new();
|
||||||
unsigned.insert("transaction_id".to_owned(), body.txn_id.clone().into());
|
unsigned.insert("transaction_id".to_owned(), body.txn_id.clone().into());
|
||||||
|
|
||||||
let event_id = db
|
if let Ok(event_id) = db.rooms.append_pdu(
|
||||||
.rooms
|
body.room_id.clone(),
|
||||||
.append_pdu(
|
user_id.clone(),
|
||||||
body.room_id.clone(),
|
body.event_type.clone(),
|
||||||
user_id.clone(),
|
serde_json::from_str(body.json_body.unwrap().get()).unwrap(),
|
||||||
body.event_type.clone(),
|
Some(unsigned),
|
||||||
serde_json::from_str(body.json_body.unwrap().get()).unwrap(),
|
None,
|
||||||
Some(unsigned),
|
&db.globals,
|
||||||
None,
|
) {
|
||||||
&db.globals,
|
MatrixResult(Ok(create_message_event::Response { event_id }))
|
||||||
)
|
} else {
|
||||||
.expect("message events are always okay");
|
MatrixResult(Err(Error {
|
||||||
|
kind: ErrorKind::Unknown,
|
||||||
MatrixResult(Ok(create_message_event::Response { event_id }))
|
message: "Failed to send message.".to_owned(),
|
||||||
|
status_code: http::StatusCode::BAD_REQUEST,
|
||||||
|
}))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[put(
|
#[put(
|
||||||
|
@ -1513,20 +1557,23 @@ pub fn create_state_event_for_key_route(
|
||||||
let user_id = body.user_id.as_ref().expect("user is authenticated");
|
let user_id = body.user_id.as_ref().expect("user is authenticated");
|
||||||
|
|
||||||
// Reponse of with/without key is the same
|
// Reponse of with/without key is the same
|
||||||
let event_id = db
|
if let Ok(event_id) = db.rooms.append_pdu(
|
||||||
.rooms
|
body.room_id.clone(),
|
||||||
.append_pdu(
|
user_id.clone(),
|
||||||
body.room_id.clone(),
|
body.event_type.clone(),
|
||||||
user_id.clone(),
|
serde_json::from_str(body.json_body.clone().unwrap().get()).unwrap(),
|
||||||
body.event_type.clone(),
|
None,
|
||||||
serde_json::from_str(body.json_body.clone().unwrap().get()).unwrap(),
|
Some(body.state_key.clone()),
|
||||||
None,
|
&db.globals,
|
||||||
Some(body.state_key.clone()),
|
) {
|
||||||
&db.globals,
|
MatrixResult(Ok(create_state_event_for_key::Response { event_id }))
|
||||||
)
|
} else {
|
||||||
.unwrap();
|
MatrixResult(Err(Error {
|
||||||
|
kind: ErrorKind::Unknown,
|
||||||
MatrixResult(Ok(create_state_event_for_key::Response { event_id }))
|
message: "Failed to send event.".to_owned(),
|
||||||
|
status_code: http::StatusCode::BAD_REQUEST,
|
||||||
|
}))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[put(
|
#[put(
|
||||||
|
@ -1542,20 +1589,23 @@ pub fn create_state_event_for_empty_key_route(
|
||||||
let user_id = body.user_id.as_ref().expect("user is authenticated");
|
let user_id = body.user_id.as_ref().expect("user is authenticated");
|
||||||
|
|
||||||
// Reponse of with/without key is the same
|
// Reponse of with/without key is the same
|
||||||
let event_id = db
|
if let Ok(event_id) = db.rooms.append_pdu(
|
||||||
.rooms
|
body.room_id.clone(),
|
||||||
.append_pdu(
|
user_id.clone(),
|
||||||
body.room_id.clone(),
|
body.event_type.clone(),
|
||||||
user_id.clone(),
|
serde_json::from_str(body.json_body.unwrap().get()).unwrap(),
|
||||||
body.event_type.clone(),
|
None,
|
||||||
serde_json::from_str(body.json_body.unwrap().get()).unwrap(),
|
Some("".to_owned()),
|
||||||
None,
|
&db.globals,
|
||||||
Some("".to_owned()),
|
) {
|
||||||
&db.globals,
|
MatrixResult(Ok(create_state_event_for_empty_key::Response { event_id }))
|
||||||
)
|
} else {
|
||||||
.unwrap();
|
MatrixResult(Err(Error {
|
||||||
|
kind: ErrorKind::Unknown,
|
||||||
MatrixResult(Ok(create_state_event_for_empty_key::Response { event_id }))
|
message: "Failed to send event.".to_owned(),
|
||||||
|
status_code: http::StatusCode::BAD_REQUEST,
|
||||||
|
}))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[get("/_matrix/client/r0/rooms/<_room_id>/state", data = "<body>")]
|
#[get("/_matrix/client/r0/rooms/<_room_id>/state", data = "<body>")]
|
||||||
|
@ -1564,15 +1614,25 @@ pub fn get_state_events_route(
|
||||||
body: Ruma<get_state_events::Request>,
|
body: Ruma<get_state_events::Request>,
|
||||||
_room_id: String,
|
_room_id: String,
|
||||||
) -> MatrixResult<get_state_events::Response> {
|
) -> MatrixResult<get_state_events::Response> {
|
||||||
MatrixResult(Ok(get_state_events::Response {
|
let user_id = body.user_id.as_ref().expect("user is authenticated");
|
||||||
room_state: db
|
|
||||||
.rooms
|
if db.rooms.is_joined(user_id, &body.room_id).unwrap() {
|
||||||
.room_state(&body.room_id)
|
MatrixResult(Ok(get_state_events::Response {
|
||||||
.unwrap()
|
room_state: db
|
||||||
.values()
|
.rooms
|
||||||
.map(|pdu| pdu.to_state_event())
|
.room_state(&body.room_id)
|
||||||
.collect(),
|
.unwrap()
|
||||||
}))
|
.values()
|
||||||
|
.map(|pdu| pdu.to_state_event())
|
||||||
|
.collect(),
|
||||||
|
}))
|
||||||
|
} else {
|
||||||
|
MatrixResult(Err(Error {
|
||||||
|
kind: ErrorKind::Forbidden,
|
||||||
|
message: "You don't have permission to view the room state.".to_owned(),
|
||||||
|
status_code: http::StatusCode::BAD_REQUEST,
|
||||||
|
}))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[get(
|
#[get(
|
||||||
|
@ -1586,19 +1646,29 @@ pub fn get_state_events_for_key_route(
|
||||||
_event_type: String,
|
_event_type: String,
|
||||||
_state_key: String,
|
_state_key: String,
|
||||||
) -> MatrixResult<get_state_events_for_key::Response> {
|
) -> MatrixResult<get_state_events_for_key::Response> {
|
||||||
if let Some(event) = db
|
let user_id = body.user_id.as_ref().expect("user is authenticated");
|
||||||
.rooms
|
|
||||||
.room_state(&body.room_id)
|
if db.rooms.is_joined(user_id, &body.room_id).unwrap() {
|
||||||
.unwrap()
|
if let Some(event) = db
|
||||||
.get(&(body.event_type.clone(), body.state_key.clone()))
|
.rooms
|
||||||
{
|
.room_state(&body.room_id)
|
||||||
MatrixResult(Ok(get_state_events_for_key::Response {
|
.unwrap()
|
||||||
content: serde_json::value::to_raw_value(event).unwrap(),
|
.get(&(body.event_type.clone(), body.state_key.clone()))
|
||||||
}))
|
{
|
||||||
|
MatrixResult(Ok(get_state_events_for_key::Response {
|
||||||
|
content: serde_json::value::to_raw_value(event).unwrap(),
|
||||||
|
}))
|
||||||
|
} else {
|
||||||
|
MatrixResult(Err(Error {
|
||||||
|
kind: ErrorKind::NotFound,
|
||||||
|
message: "State event not found.".to_owned(),
|
||||||
|
status_code: http::StatusCode::BAD_REQUEST,
|
||||||
|
}))
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
MatrixResult(Err(Error {
|
MatrixResult(Err(Error {
|
||||||
kind: ErrorKind::NotFound,
|
kind: ErrorKind::Forbidden,
|
||||||
message: "State event not found.".to_owned(),
|
message: "You don't have permission to view the room state.".to_owned(),
|
||||||
status_code: http::StatusCode::BAD_REQUEST,
|
status_code: http::StatusCode::BAD_REQUEST,
|
||||||
}))
|
}))
|
||||||
}
|
}
|
||||||
|
@ -1614,19 +1684,29 @@ pub fn get_state_events_for_empty_key_route(
|
||||||
_room_id: String,
|
_room_id: String,
|
||||||
_event_type: String,
|
_event_type: String,
|
||||||
) -> MatrixResult<get_state_events_for_key::Response> {
|
) -> MatrixResult<get_state_events_for_key::Response> {
|
||||||
if let Some(event) = db
|
let user_id = body.user_id.as_ref().expect("user is authenticated");
|
||||||
.rooms
|
|
||||||
.room_state(&body.room_id)
|
if db.rooms.is_joined(user_id, &body.room_id).unwrap() {
|
||||||
.unwrap()
|
if let Some(event) = db
|
||||||
.get(&(body.event_type.clone(), "".to_owned()))
|
.rooms
|
||||||
{
|
.room_state(&body.room_id)
|
||||||
MatrixResult(Ok(get_state_events_for_key::Response {
|
.unwrap()
|
||||||
content: serde_json::value::to_raw_value(event).unwrap(),
|
.get(&(body.event_type.clone(), "".to_owned()))
|
||||||
}))
|
{
|
||||||
|
MatrixResult(Ok(get_state_events_for_key::Response {
|
||||||
|
content: serde_json::value::to_raw_value(event).unwrap(),
|
||||||
|
}))
|
||||||
|
} else {
|
||||||
|
MatrixResult(Err(Error {
|
||||||
|
kind: ErrorKind::NotFound,
|
||||||
|
message: "State event not found.".to_owned(),
|
||||||
|
status_code: http::StatusCode::BAD_REQUEST,
|
||||||
|
}))
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
MatrixResult(Err(Error {
|
MatrixResult(Err(Error {
|
||||||
kind: ErrorKind::NotFound,
|
kind: ErrorKind::Forbidden,
|
||||||
message: "State event not found.".to_owned(),
|
message: "You don't have permission to view the room state.".to_owned(),
|
||||||
status_code: http::StatusCode::BAD_REQUEST,
|
status_code: http::StatusCode::BAD_REQUEST,
|
||||||
}))
|
}))
|
||||||
}
|
}
|
||||||
|
@ -1902,6 +1982,16 @@ pub fn get_message_events_route(
|
||||||
body: Ruma<get_message_events::Request>,
|
body: Ruma<get_message_events::Request>,
|
||||||
_room_id: String,
|
_room_id: String,
|
||||||
) -> MatrixResult<get_message_events::Response> {
|
) -> MatrixResult<get_message_events::Response> {
|
||||||
|
let user_id = body.user_id.as_ref().expect("user is authenticated");
|
||||||
|
|
||||||
|
if !db.rooms.is_joined(user_id, &body.room_id).unwrap() {
|
||||||
|
return MatrixResult(Err(Error {
|
||||||
|
kind: ErrorKind::Forbidden,
|
||||||
|
message: "You don't have permission to view this room.".to_owned(),
|
||||||
|
status_code: http::StatusCode::BAD_REQUEST,
|
||||||
|
}));
|
||||||
|
}
|
||||||
|
|
||||||
if let get_message_events::Direction::Forward = body.dir {
|
if let get_message_events::Direction::Forward = body.dir {
|
||||||
todo!();
|
todo!();
|
||||||
}
|
}
|
||||||
|
@ -1930,7 +2020,7 @@ pub fn get_message_events_route(
|
||||||
}))
|
}))
|
||||||
} else {
|
} else {
|
||||||
MatrixResult(Err(Error {
|
MatrixResult(Err(Error {
|
||||||
kind: ErrorKind::NotFound,
|
kind: ErrorKind::Unknown,
|
||||||
message: "Invalid from.".to_owned(),
|
message: "Invalid from.".to_owned(),
|
||||||
status_code: http::StatusCode::BAD_REQUEST,
|
status_code: http::StatusCode::BAD_REQUEST,
|
||||||
}))
|
}))
|
||||||
|
@ -2058,7 +2148,7 @@ pub fn get_content_route(
|
||||||
MatrixResult(Err(Error {
|
MatrixResult(Err(Error {
|
||||||
kind: ErrorKind::NotFound,
|
kind: ErrorKind::NotFound,
|
||||||
message: "Media not found.".to_owned(),
|
message: "Media not found.".to_owned(),
|
||||||
status_code: http::StatusCode::NOT_FOUND,
|
status_code: http::StatusCode::BAD_REQUEST,
|
||||||
}))
|
}))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -2087,7 +2177,7 @@ pub fn get_content_thumbnail_route(
|
||||||
MatrixResult(Err(Error {
|
MatrixResult(Err(Error {
|
||||||
kind: ErrorKind::NotFound,
|
kind: ErrorKind::NotFound,
|
||||||
message: "Media not found.".to_owned(),
|
message: "Media not found.".to_owned(),
|
||||||
status_code: http::StatusCode::NOT_FOUND,
|
status_code: http::StatusCode::BAD_REQUEST,
|
||||||
}))
|
}))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,11 +3,18 @@ mod edus;
|
||||||
pub use edus::RoomEdus;
|
pub use edus::RoomEdus;
|
||||||
|
|
||||||
use crate::{utils, Error, PduEvent, Result};
|
use crate::{utils, Error, PduEvent, Result};
|
||||||
use ruma_events::{room::power_levels::PowerLevelsEventContent, EventJson, EventType};
|
use log::error;
|
||||||
|
use ruma_events::{
|
||||||
|
room::{
|
||||||
|
join_rules, member,
|
||||||
|
power_levels::{self, PowerLevelsEventContent},
|
||||||
|
},
|
||||||
|
EventJson, EventType,
|
||||||
|
};
|
||||||
use ruma_identifiers::{EventId, RoomId, UserId};
|
use ruma_identifiers::{EventId, RoomId, UserId};
|
||||||
use serde_json::json;
|
|
||||||
use std::{
|
use std::{
|
||||||
collections::HashMap,
|
collections::{BTreeMap, HashMap},
|
||||||
convert::{TryFrom, TryInto},
|
convert::{TryFrom, TryInto},
|
||||||
mem,
|
mem,
|
||||||
};
|
};
|
||||||
|
@ -112,6 +119,20 @@ impl Rooms {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Returns the pdu.
|
||||||
|
pub fn get_pdu(&self, event_id: &EventId) -> Result<Option<PduEvent>> {
|
||||||
|
self.eventid_pduid
|
||||||
|
.get(event_id.to_string().as_bytes())?
|
||||||
|
.map_or(Ok(None), |pdu_id| {
|
||||||
|
Ok(serde_json::from_slice(
|
||||||
|
&self.pduid_pdu.get(pdu_id)?.ok_or(Error::BadDatabase(
|
||||||
|
"eventid_pduid points to nonexistent pdu",
|
||||||
|
))?,
|
||||||
|
)?)
|
||||||
|
.map(Some)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
/// Returns the leaf pdus of a room.
|
/// Returns the leaf pdus of a room.
|
||||||
pub fn get_pdu_leaves(&self, room_id: &RoomId) -> Result<Vec<EventId>> {
|
pub fn get_pdu_leaves(&self, room_id: &RoomId) -> Result<Vec<EventId>> {
|
||||||
let mut prefix = room_id.to_string().as_bytes().to_vec();
|
let mut prefix = room_id.to_string().as_bytes().to_vec();
|
||||||
|
@ -158,41 +179,227 @@ impl Rooms {
|
||||||
state_key: Option<String>,
|
state_key: Option<String>,
|
||||||
globals: &super::globals::Globals,
|
globals: &super::globals::Globals,
|
||||||
) -> Result<EventId> {
|
) -> Result<EventId> {
|
||||||
|
// TODO: Make sure this isn't called twice in parallel
|
||||||
|
|
||||||
|
let prev_events = self.get_pdu_leaves(&room_id)?;
|
||||||
|
|
||||||
// Is the event authorized?
|
// Is the event authorized?
|
||||||
if state_key.is_some() {
|
if let Some(state_key) = &state_key {
|
||||||
if let Some(pdu) = self
|
let power_levels = self
|
||||||
.room_state(&room_id)?
|
.room_state(&room_id)?
|
||||||
.get(&(EventType::RoomPowerLevels, "".to_owned()))
|
.get(&(EventType::RoomPowerLevels, "".to_owned()))
|
||||||
|
.map_or_else(
|
||||||
|
|| {
|
||||||
|
Ok::<_, Error>(power_levels::PowerLevelsEventContent {
|
||||||
|
ban: 50.into(),
|
||||||
|
events: BTreeMap::new(),
|
||||||
|
events_default: 0.into(),
|
||||||
|
invite: 50.into(),
|
||||||
|
kick: 50.into(),
|
||||||
|
redact: 50.into(),
|
||||||
|
state_default: 0.into(),
|
||||||
|
users: BTreeMap::new(),
|
||||||
|
users_default: 0.into(),
|
||||||
|
notifications:
|
||||||
|
ruma_events::room::power_levels::NotificationPowerLevels {
|
||||||
|
room: 50.into(),
|
||||||
|
},
|
||||||
|
})
|
||||||
|
},
|
||||||
|
|power_levels| {
|
||||||
|
Ok(
|
||||||
|
serde_json::from_value::<EventJson<PowerLevelsEventContent>>(
|
||||||
|
power_levels.content.clone(),
|
||||||
|
)?
|
||||||
|
.deserialize()?,
|
||||||
|
)
|
||||||
|
},
|
||||||
|
)?;
|
||||||
{
|
{
|
||||||
let power_levels = serde_json::from_value::<EventJson<PowerLevelsEventContent>>(
|
let sender_membership = self
|
||||||
pdu.content.clone(),
|
.room_state(&room_id)?
|
||||||
)?
|
.get(&(EventType::RoomMember, sender.to_string()))
|
||||||
.deserialize()?;
|
.map_or(Ok::<_, Error>(member::MembershipState::Leave), |pdu| {
|
||||||
|
Ok(
|
||||||
|
serde_json::from_value::<EventJson<member::MemberEventContent>>(
|
||||||
|
pdu.content.clone(),
|
||||||
|
)?
|
||||||
|
.deserialize()?
|
||||||
|
.membership,
|
||||||
|
)
|
||||||
|
})?;
|
||||||
|
|
||||||
match event_type {
|
let sender_power = power_levels.users.get(&sender).map_or_else(
|
||||||
|
|| {
|
||||||
|
if sender_membership != member::MembershipState::Join {
|
||||||
|
None
|
||||||
|
} else {
|
||||||
|
Some(&power_levels.users_default)
|
||||||
|
}
|
||||||
|
},
|
||||||
|
// If it's okay, wrap with Some(_)
|
||||||
|
Some,
|
||||||
|
);
|
||||||
|
|
||||||
|
if !match event_type {
|
||||||
EventType::RoomMember => {
|
EventType::RoomMember => {
|
||||||
// Member events are okay for now (TODO)
|
let target_user_id = UserId::try_from(&**state_key)?;
|
||||||
|
|
||||||
|
let current_membership = self
|
||||||
|
.room_state(&room_id)?
|
||||||
|
.get(&(EventType::RoomMember, target_user_id.to_string()))
|
||||||
|
.map_or(Ok::<_, Error>(member::MembershipState::Leave), |pdu| {
|
||||||
|
Ok(serde_json::from_value::<
|
||||||
|
EventJson<member::MemberEventContent>,
|
||||||
|
>(pdu.content.clone())?
|
||||||
|
.deserialize()?
|
||||||
|
.membership)
|
||||||
|
})?;
|
||||||
|
|
||||||
|
let target_membership = serde_json::from_value::<
|
||||||
|
EventJson<member::MemberEventContent>,
|
||||||
|
>(content.clone())?
|
||||||
|
.deserialize()?
|
||||||
|
.membership;
|
||||||
|
|
||||||
|
let target_power = power_levels.users.get(&target_user_id).map_or_else(
|
||||||
|
|| {
|
||||||
|
if target_membership != member::MembershipState::Join {
|
||||||
|
None
|
||||||
|
} else {
|
||||||
|
Some(&power_levels.users_default)
|
||||||
|
}
|
||||||
|
},
|
||||||
|
// If it's okay, wrap with Some(_)
|
||||||
|
Some,
|
||||||
|
);
|
||||||
|
|
||||||
|
let join_rules = self
|
||||||
|
.room_state(&room_id)?
|
||||||
|
.get(&(EventType::RoomJoinRules, "".to_owned()))
|
||||||
|
.map_or(join_rules::JoinRule::Public, |pdu| {
|
||||||
|
serde_json::from_value::<
|
||||||
|
EventJson<join_rules::JoinRulesEventContent>,
|
||||||
|
>(pdu.content.clone())
|
||||||
|
.unwrap()
|
||||||
|
.deserialize()
|
||||||
|
.unwrap()
|
||||||
|
.join_rule
|
||||||
|
});
|
||||||
|
|
||||||
|
if target_membership == member::MembershipState::Join {
|
||||||
|
let mut prev_events = prev_events.iter();
|
||||||
|
let prev_event = self
|
||||||
|
.get_pdu(prev_events.next().ok_or(Error::BadRequest(
|
||||||
|
"membership can't be the first event",
|
||||||
|
))?)?
|
||||||
|
.ok_or(Error::BadDatabase("pdu leave points to valid event"))?;
|
||||||
|
if prev_event.kind == EventType::RoomCreate
|
||||||
|
&& prev_event.prev_events.is_empty()
|
||||||
|
{
|
||||||
|
true
|
||||||
|
} else if sender != target_user_id {
|
||||||
|
false
|
||||||
|
} else if let member::MembershipState::Ban = current_membership {
|
||||||
|
false
|
||||||
|
} else if join_rules == join_rules::JoinRule::Invite
|
||||||
|
&& (current_membership == member::MembershipState::Join
|
||||||
|
|| current_membership == member::MembershipState::Invite)
|
||||||
|
{
|
||||||
|
true
|
||||||
|
} else if join_rules == join_rules::JoinRule::Public {
|
||||||
|
true
|
||||||
|
} else {
|
||||||
|
false
|
||||||
|
}
|
||||||
|
} else if target_membership == member::MembershipState::Invite {
|
||||||
|
if let Some(third_party_invite_json) = content.get("third_party_invite")
|
||||||
|
{
|
||||||
|
if current_membership == member::MembershipState::Ban {
|
||||||
|
false
|
||||||
|
} else {
|
||||||
|
let _third_party_invite =
|
||||||
|
serde_json::from_value::<member::ThirdPartyInvite>(
|
||||||
|
third_party_invite_json.clone(),
|
||||||
|
)?;
|
||||||
|
todo!("handle third party invites");
|
||||||
|
}
|
||||||
|
} else if sender_membership != member::MembershipState::Join {
|
||||||
|
false
|
||||||
|
} else if current_membership == member::MembershipState::Join
|
||||||
|
|| current_membership == member::MembershipState::Ban
|
||||||
|
{
|
||||||
|
false
|
||||||
|
} else if sender_power
|
||||||
|
.filter(|&p| p >= &power_levels.invite)
|
||||||
|
.is_some()
|
||||||
|
{
|
||||||
|
true
|
||||||
|
} else {
|
||||||
|
false
|
||||||
|
}
|
||||||
|
} else if target_membership == member::MembershipState::Leave {
|
||||||
|
if sender == target_user_id {
|
||||||
|
current_membership == member::MembershipState::Join
|
||||||
|
|| current_membership == member::MembershipState::Invite
|
||||||
|
} else if sender_membership != member::MembershipState::Join {
|
||||||
|
false
|
||||||
|
} else if current_membership == member::MembershipState::Ban
|
||||||
|
&& sender_power.filter(|&p| p < &power_levels.ban).is_some()
|
||||||
|
{
|
||||||
|
false
|
||||||
|
} else if sender_power.filter(|&p| p >= &power_levels.kick).is_some()
|
||||||
|
&& target_power < sender_power
|
||||||
|
{
|
||||||
|
true
|
||||||
|
} else {
|
||||||
|
false
|
||||||
|
}
|
||||||
|
} else if target_membership == member::MembershipState::Ban {
|
||||||
|
if sender_membership != member::MembershipState::Join {
|
||||||
|
false
|
||||||
|
} else if sender_power.filter(|&p| p >= &power_levels.ban).is_some()
|
||||||
|
&& target_power < sender_power
|
||||||
|
{
|
||||||
|
true
|
||||||
|
} else {
|
||||||
|
false
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
false
|
||||||
|
}
|
||||||
}
|
}
|
||||||
_ if power_levels
|
EventType::RoomCreate => prev_events.is_empty(),
|
||||||
.users
|
_ if sender_membership == member::MembershipState::Join => {
|
||||||
.get(&sender)
|
// TODO
|
||||||
.unwrap_or(&power_levels.users_default)
|
sender_power.unwrap_or(&power_levels.users_default)
|
||||||
<= &0.into() =>
|
>= &power_levels.state_default
|
||||||
{
|
|
||||||
// Not authorized
|
|
||||||
return Err(Error::BadRequest("event not authorized"));
|
|
||||||
}
|
}
|
||||||
// User has sufficient power
|
|
||||||
_ => {}
|
_ => false,
|
||||||
|
} {
|
||||||
|
error!("Unauthorized");
|
||||||
|
// Not authorized
|
||||||
|
return Err(Error::BadRequest("event not authorized"));
|
||||||
|
}
|
||||||
|
if event_type == EventType::RoomMember {
|
||||||
|
// TODO: Don't get this twice
|
||||||
|
let target_user_id = UserId::try_from(&**state_key)?;
|
||||||
|
self.update_membership(
|
||||||
|
&room_id,
|
||||||
|
&target_user_id,
|
||||||
|
&serde_json::from_value::<EventJson<member::MemberEventContent>>(
|
||||||
|
content.clone(),
|
||||||
|
)?
|
||||||
|
.deserialize()?
|
||||||
|
.membership,
|
||||||
|
)?;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
} else if !self.is_joined(&sender, &room_id)? {
|
||||||
|
return Err(Error::BadRequest("event not authorized"));
|
||||||
}
|
}
|
||||||
|
|
||||||
// prev_events are the leaves of the current graph. This method removes all leaves from the
|
|
||||||
// room and replaces them with our event
|
|
||||||
// TODO: Make sure this isn't called twice in parallel
|
|
||||||
let prev_events = self.get_pdu_leaves(&room_id)?;
|
|
||||||
|
|
||||||
// Our depth is the maximum depth of prev_events + 1
|
// Our depth is the maximum depth of prev_events + 1
|
||||||
let depth = prev_events
|
let depth = prev_events
|
||||||
.iter()
|
.iter()
|
||||||
|
@ -206,7 +413,7 @@ impl Rooms {
|
||||||
if let Some(state_key) = &state_key {
|
if let Some(state_key) = &state_key {
|
||||||
if let Some(prev_pdu) = self
|
if let Some(prev_pdu) = self
|
||||||
.room_state(&room_id)?
|
.room_state(&room_id)?
|
||||||
.get(&(event_type.clone(), state_key.clone()))
|
.get(&(event_type.clone(), state_key.to_owned()))
|
||||||
{
|
{
|
||||||
unsigned.insert("prev_content".to_owned(), prev_pdu.content.clone());
|
unsigned.insert("prev_content".to_owned(), prev_pdu.content.clone());
|
||||||
}
|
}
|
||||||
|
@ -348,63 +555,12 @@ impl Rooms {
|
||||||
.map(|(_, v)| Ok(serde_json::from_slice(&v)?))
|
.map(|(_, v)| Ok(serde_json::from_slice(&v)?))
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Makes a user join a room.
|
/// Makes a user join a room. Only call this if the membership is Join already
|
||||||
pub fn join(
|
fn update_membership(
|
||||||
&self,
|
&self,
|
||||||
room_id: &RoomId,
|
room_id: &RoomId,
|
||||||
user_id: &UserId,
|
user_id: &UserId,
|
||||||
users: &super::users::Users,
|
membership: &member::MembershipState,
|
||||||
globals: &super::globals::Globals,
|
|
||||||
) -> Result<()> {
|
|
||||||
if !self.exists(room_id)? {
|
|
||||||
return Err(Error::BadRequest("room does not exist"));
|
|
||||||
}
|
|
||||||
|
|
||||||
let mut userroom_id = user_id.to_string().as_bytes().to_vec();
|
|
||||||
userroom_id.push(0xff);
|
|
||||||
userroom_id.extend_from_slice(room_id.to_string().as_bytes());
|
|
||||||
|
|
||||||
let mut roomuser_id = room_id.to_string().as_bytes().to_vec();
|
|
||||||
roomuser_id.push(0xff);
|
|
||||||
roomuser_id.extend_from_slice(user_id.to_string().as_bytes());
|
|
||||||
|
|
||||||
self.userroomid_joined.insert(&userroom_id, &[])?;
|
|
||||||
self.roomuserid_joined.insert(&roomuser_id, &[])?;
|
|
||||||
self.userroomid_invited.remove(&userroom_id)?;
|
|
||||||
self.roomuserid_invited.remove(&roomuser_id)?;
|
|
||||||
self.userroomid_left.remove(&userroom_id)?;
|
|
||||||
|
|
||||||
let mut json = serde_json::Map::new();
|
|
||||||
json.insert("membership".to_owned(), "join".into());
|
|
||||||
|
|
||||||
if let Some(displayname) = users.displayname(&user_id).unwrap() {
|
|
||||||
json.insert("displayname".to_owned(), displayname.into());
|
|
||||||
}
|
|
||||||
|
|
||||||
if let Some(avatar_url) = users.avatar_url(&user_id).unwrap() {
|
|
||||||
json.insert("avatar_url".to_owned(), avatar_url.into());
|
|
||||||
}
|
|
||||||
|
|
||||||
self.append_pdu(
|
|
||||||
room_id.clone(),
|
|
||||||
user_id.clone(),
|
|
||||||
EventType::RoomMember,
|
|
||||||
json.into(),
|
|
||||||
None,
|
|
||||||
Some(user_id.to_string()),
|
|
||||||
globals,
|
|
||||||
)?;
|
|
||||||
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Makes a user leave a room.
|
|
||||||
pub fn leave(
|
|
||||||
&self,
|
|
||||||
sender: &UserId,
|
|
||||||
room_id: &RoomId,
|
|
||||||
user_id: &UserId,
|
|
||||||
globals: &super::globals::Globals,
|
|
||||||
) -> Result<()> {
|
) -> Result<()> {
|
||||||
let mut userroom_id = user_id.to_string().as_bytes().to_vec();
|
let mut userroom_id = user_id.to_string().as_bytes().to_vec();
|
||||||
userroom_id.push(0xff);
|
userroom_id.push(0xff);
|
||||||
|
@ -414,21 +570,30 @@ impl Rooms {
|
||||||
roomuser_id.push(0xff);
|
roomuser_id.push(0xff);
|
||||||
roomuser_id.extend_from_slice(user_id.to_string().as_bytes());
|
roomuser_id.extend_from_slice(user_id.to_string().as_bytes());
|
||||||
|
|
||||||
self.userroomid_joined.remove(&userroom_id)?;
|
match &membership {
|
||||||
self.roomuserid_joined.remove(&roomuser_id)?;
|
member::MembershipState::Join => {
|
||||||
self.userroomid_invited.remove(&userroom_id)?;
|
self.userroomid_joined.insert(&userroom_id, &[])?;
|
||||||
self.roomuserid_invited.remove(&userroom_id)?;
|
self.roomuserid_joined.insert(&roomuser_id, &[])?;
|
||||||
self.userroomid_left.insert(&userroom_id, &[])?;
|
self.userroomid_invited.remove(&userroom_id)?;
|
||||||
|
self.roomuserid_invited.remove(&roomuser_id)?;
|
||||||
self.append_pdu(
|
self.userroomid_left.remove(&userroom_id)?;
|
||||||
room_id.clone(),
|
}
|
||||||
sender.clone(),
|
member::MembershipState::Invite => {
|
||||||
EventType::RoomMember,
|
self.userroomid_invited.insert(&userroom_id, &[])?;
|
||||||
json!({"membership": "leave"}),
|
self.roomuserid_invited.insert(&roomuser_id, &[])?;
|
||||||
None,
|
self.userroomid_joined.remove(&userroom_id)?;
|
||||||
Some(user_id.to_string()),
|
self.roomuserid_joined.remove(&roomuser_id)?;
|
||||||
globals,
|
self.userroomid_left.remove(&userroom_id)?;
|
||||||
)?;
|
}
|
||||||
|
member::MembershipState::Leave | member::MembershipState::Ban => {
|
||||||
|
self.userroomid_left.insert(&userroom_id, &[])?;
|
||||||
|
self.userroomid_joined.remove(&userroom_id)?;
|
||||||
|
self.roomuserid_joined.remove(&roomuser_id)?;
|
||||||
|
self.userroomid_invited.remove(&userroom_id)?;
|
||||||
|
self.roomuserid_invited.remove(&roomuser_id)?;
|
||||||
|
}
|
||||||
|
_ => {}
|
||||||
|
}
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
@ -444,38 +609,6 @@ impl Rooms {
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Makes a user invite another user into room.
|
|
||||||
pub fn invite(
|
|
||||||
&self,
|
|
||||||
sender: &UserId,
|
|
||||||
room_id: &RoomId,
|
|
||||||
user_id: &UserId,
|
|
||||||
globals: &super::globals::Globals,
|
|
||||||
) -> Result<()> {
|
|
||||||
let mut userroom_id = user_id.to_string().as_bytes().to_vec();
|
|
||||||
userroom_id.push(0xff);
|
|
||||||
userroom_id.extend_from_slice(room_id.to_string().as_bytes());
|
|
||||||
|
|
||||||
let mut roomuser_id = room_id.to_string().as_bytes().to_vec();
|
|
||||||
roomuser_id.push(0xff);
|
|
||||||
roomuser_id.extend_from_slice(user_id.to_string().as_bytes());
|
|
||||||
|
|
||||||
self.userroomid_invited.insert(userroom_id, &[])?;
|
|
||||||
self.roomuserid_invited.insert(roomuser_id, &[])?;
|
|
||||||
|
|
||||||
self.append_pdu(
|
|
||||||
room_id.clone(),
|
|
||||||
sender.clone(),
|
|
||||||
EventType::RoomMember,
|
|
||||||
json!({"membership": "invite"}),
|
|
||||||
None,
|
|
||||||
Some(user_id.to_string()),
|
|
||||||
globals,
|
|
||||||
)?;
|
|
||||||
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Returns an iterator over all rooms a user joined.
|
/// Returns an iterator over all rooms a user joined.
|
||||||
pub fn room_members(&self, room_id: &RoomId) -> impl Iterator<Item = Result<UserId>> {
|
pub fn room_members(&self, room_id: &RoomId) -> impl Iterator<Item = Result<UserId>> {
|
||||||
self.roomuserid_joined
|
self.roomuserid_joined
|
||||||
|
@ -550,4 +683,28 @@ impl Rooms {
|
||||||
)?)?)
|
)?)?)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn is_joined(&self, user_id: &UserId, room_id: &RoomId) -> Result<bool> {
|
||||||
|
let mut userroom_id = user_id.to_string().as_bytes().to_vec();
|
||||||
|
userroom_id.push(0xff);
|
||||||
|
userroom_id.extend_from_slice(room_id.to_string().as_bytes());
|
||||||
|
|
||||||
|
Ok(self.userroomid_joined.get(userroom_id)?.is_some())
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn is_invited(&self, user_id: &UserId, room_id: &RoomId) -> Result<bool> {
|
||||||
|
let mut userroom_id = user_id.to_string().as_bytes().to_vec();
|
||||||
|
userroom_id.push(0xff);
|
||||||
|
userroom_id.extend_from_slice(room_id.to_string().as_bytes());
|
||||||
|
|
||||||
|
Ok(self.userroomid_invited.get(userroom_id)?.is_some())
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn is_left(&self, user_id: &UserId, room_id: &RoomId) -> Result<bool> {
|
||||||
|
let mut userroom_id = user_id.to_string().as_bytes().to_vec();
|
||||||
|
userroom_id.push(0xff);
|
||||||
|
userroom_id.extend_from_slice(room_id.to_string().as_bytes());
|
||||||
|
|
||||||
|
Ok(self.userroomid_left.get(userroom_id)?.is_some())
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue