|
|
|
|
@ -7,6 +7,7 @@ use log::error;
@@ -7,6 +7,7 @@ use log::error;
|
|
|
|
|
use ruma::{ |
|
|
|
|
api::client::error::ErrorKind, |
|
|
|
|
events::{ |
|
|
|
|
ignored_user_list, |
|
|
|
|
room::{ |
|
|
|
|
join_rules, member, |
|
|
|
|
power_levels::{self, PowerLevelsEventContent}, |
|
|
|
|
@ -255,6 +256,7 @@ impl Rooms {
@@ -255,6 +256,7 @@ impl Rooms {
|
|
|
|
|
&self, |
|
|
|
|
pdu_builder: PduBuilder, |
|
|
|
|
globals: &super::globals::Globals, |
|
|
|
|
account_data: &super::account_data::AccountData, |
|
|
|
|
) -> Result<EventId> { |
|
|
|
|
let PduBuilder { |
|
|
|
|
room_id, |
|
|
|
|
@ -386,7 +388,7 @@ impl Rooms {
@@ -386,7 +388,7 @@ impl Rooms {
|
|
|
|
|
.join_rule) |
|
|
|
|
})?; |
|
|
|
|
|
|
|
|
|
let authorized = if target_membership == member::MembershipState::Join { |
|
|
|
|
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( |
|
|
|
|
@ -459,19 +461,11 @@ impl Rooms {
@@ -459,19 +461,11 @@ impl Rooms {
|
|
|
|
|
} |
|
|
|
|
} else { |
|
|
|
|
false |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
if authorized { |
|
|
|
|
// Update our membership info
|
|
|
|
|
self.update_membership(&room_id, &target_user_id, &target_membership)?; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
authorized |
|
|
|
|
} |
|
|
|
|
EventType::RoomCreate => prev_events.is_empty(), |
|
|
|
|
// Not allow any of the following events if the sender is not joined.
|
|
|
|
|
_ if sender_membership != member::MembershipState::Join => false, |
|
|
|
|
|
|
|
|
|
_ => { |
|
|
|
|
// TODO
|
|
|
|
|
sender_power.unwrap_or(&power_levels.users_default) |
|
|
|
|
@ -523,7 +517,7 @@ impl Rooms {
@@ -523,7 +517,7 @@ impl Rooms {
|
|
|
|
|
.expect("time is valid"), |
|
|
|
|
kind: event_type.clone(), |
|
|
|
|
content: content.clone(), |
|
|
|
|
state_key, |
|
|
|
|
state_key: state_key.clone(), |
|
|
|
|
prev_events, |
|
|
|
|
depth: depth |
|
|
|
|
.try_into() |
|
|
|
|
@ -579,23 +573,50 @@ impl Rooms {
@@ -579,23 +573,50 @@ impl Rooms {
|
|
|
|
|
self.roomstateid_pdu.insert(key, &*pdu_json.to_string())?; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if let EventType::RoomRedaction = event_type { |
|
|
|
|
if let Some(redact_id) = &redacts { |
|
|
|
|
// TODO: Reason
|
|
|
|
|
let _reason = |
|
|
|
|
serde_json::from_value::<Raw<redaction::RedactionEventContent>>(content) |
|
|
|
|
.expect("Raw::from_value always works.") |
|
|
|
|
.deserialize() |
|
|
|
|
.map_err(|_| { |
|
|
|
|
Error::BadRequest( |
|
|
|
|
ErrorKind::InvalidParam, |
|
|
|
|
"Invalid redaction event content.", |
|
|
|
|
) |
|
|
|
|
})? |
|
|
|
|
.reason; |
|
|
|
|
|
|
|
|
|
self.redact_pdu(&redact_id)?; |
|
|
|
|
match event_type { |
|
|
|
|
EventType::RoomRedaction => { |
|
|
|
|
if let Some(redact_id) = &redacts { |
|
|
|
|
// TODO: Reason
|
|
|
|
|
let _reason = |
|
|
|
|
serde_json::from_value::<Raw<redaction::RedactionEventContent>>(content) |
|
|
|
|
.expect("Raw::from_value always works.") |
|
|
|
|
.deserialize() |
|
|
|
|
.map_err(|_| { |
|
|
|
|
Error::BadRequest( |
|
|
|
|
ErrorKind::InvalidParam, |
|
|
|
|
"Invalid redaction event content.", |
|
|
|
|
) |
|
|
|
|
})? |
|
|
|
|
.reason; |
|
|
|
|
|
|
|
|
|
self.redact_pdu(&redact_id)?; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
EventType::RoomMember => { |
|
|
|
|
if let Some(state_key) = state_key { |
|
|
|
|
// if the state_key fails
|
|
|
|
|
let target_user_id = UserId::try_from(state_key) |
|
|
|
|
.expect("This state_key was previously validated"); |
|
|
|
|
// Update our membership info, we do this here incase a user is invited
|
|
|
|
|
// and immediately leaves we need the DB to record the invite event for auth
|
|
|
|
|
self.update_membership( |
|
|
|
|
&room_id, |
|
|
|
|
&target_user_id, |
|
|
|
|
serde_json::from_value::<member::MemberEventContent>(content).map_err( |
|
|
|
|
|_| { |
|
|
|
|
Error::BadRequest( |
|
|
|
|
ErrorKind::InvalidParam, |
|
|
|
|
"Invalid redaction event content.", |
|
|
|
|
) |
|
|
|
|
}, |
|
|
|
|
)?, |
|
|
|
|
&sender, |
|
|
|
|
account_data, |
|
|
|
|
globals, |
|
|
|
|
)?; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
_ => {} |
|
|
|
|
} |
|
|
|
|
self.edus.room_read_set(&room_id, &sender, index)?; |
|
|
|
|
|
|
|
|
|
@ -741,8 +762,12 @@ impl Rooms {
@@ -741,8 +762,12 @@ impl Rooms {
|
|
|
|
|
&self, |
|
|
|
|
room_id: &RoomId, |
|
|
|
|
user_id: &UserId, |
|
|
|
|
membership: &member::MembershipState, |
|
|
|
|
mut member_content: member::MemberEventContent, |
|
|
|
|
sender: &UserId, |
|
|
|
|
account_data: &super::account_data::AccountData, |
|
|
|
|
globals: &super::globals::Globals, |
|
|
|
|
) -> Result<()> { |
|
|
|
|
let membership = member_content.membership; |
|
|
|
|
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()); |
|
|
|
|
@ -760,6 +785,37 @@ impl Rooms {
@@ -760,6 +785,37 @@ impl Rooms {
|
|
|
|
|
self.userroomid_left.remove(&userroom_id)?; |
|
|
|
|
} |
|
|
|
|
member::MembershipState::Invite => { |
|
|
|
|
// We want to know if the sender is ignored by the receiver
|
|
|
|
|
let is_ignored = account_data |
|
|
|
|
.get::<ignored_user_list::IgnoredUserListEvent>( |
|
|
|
|
None, // Ignored users are in global account data
|
|
|
|
|
&user_id, // Receiver
|
|
|
|
|
EventType::IgnoredUserList, |
|
|
|
|
)? |
|
|
|
|
.map_or(false, |ignored| { |
|
|
|
|
ignored.content.ignored_users.contains(&sender) |
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
if is_ignored { |
|
|
|
|
member_content.membership = member::MembershipState::Leave; |
|
|
|
|
|
|
|
|
|
self.append_pdu( |
|
|
|
|
PduBuilder { |
|
|
|
|
room_id: room_id.clone(), |
|
|
|
|
sender: user_id.clone(), |
|
|
|
|
event_type: EventType::RoomMember, |
|
|
|
|
content: serde_json::to_value(member_content) |
|
|
|
|
.expect("event is valid, we just created it"), |
|
|
|
|
unsigned: None, |
|
|
|
|
state_key: Some(user_id.to_string()), |
|
|
|
|
redacts: None, |
|
|
|
|
}, |
|
|
|
|
globals, |
|
|
|
|
account_data, |
|
|
|
|
)?; |
|
|
|
|
|
|
|
|
|
return Ok(()); |
|
|
|
|
} |
|
|
|
|
self.userroomid_invited.insert(&userroom_id, &[])?; |
|
|
|
|
self.roomuserid_invited.insert(&roomuser_id, &[])?; |
|
|
|
|
self.userroomid_joined.remove(&userroom_id)?; |
|
|
|
|
|