Skip to content

Commit

Permalink
Improve SetRoomDataInternal and SetRoomDataExternal
Browse files Browse the repository at this point in the history
  • Loading branch information
RipleyTom committed Jan 29, 2024
1 parent 6d78222 commit 4494f54
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 39 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,13 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [1.0.3] - 2024-01-30

### Fixed

- Added owner checks to SetRoomDataInternal and SetRoomDataExternal and rpcn now only sends notifications on actual modification


## [1.0.2] - 2024-01-29

### Fixed
Expand Down
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "rpcn"
version = "1.0.2"
version = "1.0.3"
authors = ["RipleyTom <[email protected]>"]
edition = "2021"

Expand Down
5 changes: 3 additions & 2 deletions src/server/client/cmd_room.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ impl Client {
pub fn req_set_roomdata_external(&mut self, data: &mut StreamExtractor) -> Result<ErrorType, ErrorType> {
let (com_id, setdata_req) = self.get_com_and_fb::<SetRoomDataExternalRequest>(data)?;

if let Err(e) = self.shared.room_manager.write().set_roomdata_external(&com_id, &setdata_req) {
if let Err(e) = self.shared.room_manager.write().set_roomdata_external(&com_id, &setdata_req, self.client_info.user_id) {
Ok(e)
} else {
Ok(ErrorType::NoError)
Expand All @@ -190,7 +190,7 @@ impl Client {
let res = self.shared.room_manager.write().set_roomdata_internal(&com_id, &setdata_req, self.client_info.user_id);

match res {
Ok((users, notif_data)) => {
Ok(Some((users, notif_data))) => {
let mut n_msg: Vec<u8> = Vec::new();
n_msg.extend(&room_id.to_le_bytes());
Client::add_data_packet(&mut n_msg, &notif_data);
Expand All @@ -199,6 +199,7 @@ impl Client {
self.self_notification(&notif);
Ok(ErrorType::NoError)
}
Ok(None) => Ok(ErrorType::NoError),
Err(e) => Ok(e),
}
}
Expand Down
101 changes: 66 additions & 35 deletions src/server/room_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1205,12 +1205,20 @@ impl RoomManager {
builder.finished_data().to_vec()
}

pub fn set_roomdata_external(&mut self, com_id: &ComId, req: &SetRoomDataExternalRequest) -> Result<(), ErrorType> {
pub fn set_roomdata_external(&mut self, com_id: &ComId, req: &SetRoomDataExternalRequest, user_id: i64) -> Result<(), ErrorType> {
if !self.room_exists(com_id, req.roomId()) {
return Err(ErrorType::NotFound);
}
let room = self.get_mut_room(com_id, req.roomId());

let member_id = room.get_member_id(user_id)?;
let is_room_owner = room.get_owner() == member_id;

if !is_room_owner {
// Should this return an error?
return Ok(());
}

if let Some(vec) = req.roomBinAttrExternal() {
for i in 0..vec.len() {
let room_binattr_external_from_fb = RoomBinAttr::from_flatbuffer(&vec.get(i));
Expand Down Expand Up @@ -1265,17 +1273,48 @@ impl RoomManager {

Ok(builder.finished_data().to_vec())
}
pub fn set_roomdata_internal(&mut self, com_id: &ComId, req: &SetRoomDataInternalRequest, user_id: i64) -> Result<(HashSet<i64>, Vec<u8>), ErrorType> {
pub fn set_roomdata_internal(&mut self, com_id: &ComId, req: &SetRoomDataInternalRequest, user_id: i64) -> Result<Option<(HashSet<i64>, Vec<u8>)>, ErrorType> {
if !self.room_exists(com_id, req.roomId()) {
return Err(ErrorType::NotFound);
}
let room = self.get_mut_room(com_id, req.roomId());
let member_id = room.get_member_id(user_id)?;

let is_room_owner = room.get_owner() == member_id;
let mut has_changed = false;

let old_password_slot_mask = room.password_slot_mask;
let old_flag_attr = room.flag_attr;
let flag_filter = req.flagFilter();
let flag_attr = req.flagAttr();
room.flag_attr = (flag_attr & flag_filter) | (room.flag_attr & !flag_filter);

if is_room_owner {
let flag_filter = req.flagFilter();
let flag_attr = req.flagAttr();
let new_room_flag_attr = (flag_attr & flag_filter) | (room.flag_attr & !flag_filter);

if new_room_flag_attr != room.flag_attr {
room.flag_attr = new_room_flag_attr;
has_changed = true;
}

// Group stuff TODO

if old_password_slot_mask != req.passwordSlotMask() {
room.password_slot_mask = req.passwordSlotMask();
has_changed = true;
}

if let Some(vec) = req.ownerPrivilegeRank() {
let mut succession_list: VecDeque<u16> = VecDeque::new();
for i in 0..vec.len() {
succession_list.push_back(vec.get(i));
}

if succession_list != room.owner_succession {
room.owner_succession = succession_list;
has_changed = true;
}
}
}

let new_binattr;
if let Some(vec) = req.roomBinAttrInternal() {
Expand All @@ -1292,44 +1331,36 @@ impl RoomManager {
vec_new_binattr.push(id);
}
new_binattr = Some(vec_new_binattr);
has_changed = true;
} else {
new_binattr = None;
}

// Group stuff TODO
if has_changed {
// Build the notification buffer
let mut builder = flatbuffers::FlatBufferBuilder::with_capacity(1024);
let room_data_internal = room.to_RoomDataInternal(&mut builder);
let fb_new_binattr = new_binattr.map(|vec_new_binattr| builder.create_vector(&vec_new_binattr));

let resp = RoomDataInternalUpdateInfo::create(
&mut builder,
&RoomDataInternalUpdateInfoArgs {
newRoomDataInternal: Some(room_data_internal),
prevFlagAttr: old_flag_attr,
prevRoomPasswordSlotMask: old_password_slot_mask,
newRoomGroup: None, // TODO
newRoomBinAttrInternal: fb_new_binattr,
},
);
builder.finish(resp, None);

let old_password_slot_mask = room.password_slot_mask;
room.password_slot_mask = req.passwordSlotMask();
let mut to_notif = room.get_room_user_ids();
to_notif.remove(&user_id);

if let Some(vec) = req.ownerPrivilegeRank() {
let mut succession_list: VecDeque<u16> = VecDeque::new();
for i in 0..vec.len() {
succession_list.push_back(vec.get(i));
}
room.owner_succession = succession_list;
return Ok(Some((to_notif, builder.finished_data().to_vec())));
}

// Build the notification buffer
let mut builder = flatbuffers::FlatBufferBuilder::with_capacity(1024);
let room_data_internal = room.to_RoomDataInternal(&mut builder);
let fb_new_binattr = new_binattr.map(|vec_new_binattr| builder.create_vector(&vec_new_binattr));

let resp = RoomDataInternalUpdateInfo::create(
&mut builder,
&RoomDataInternalUpdateInfoArgs {
newRoomDataInternal: Some(room_data_internal),
prevFlagAttr: old_flag_attr,
prevRoomPasswordSlotMask: old_password_slot_mask,
newRoomGroup: None, // TODO
newRoomBinAttrInternal: fb_new_binattr,
},
);
builder.finish(resp, None);

let mut to_notif = room.get_room_user_ids();
to_notif.remove(&user_id);

Ok((to_notif, builder.finished_data().to_vec()))
Ok(None)
}

pub fn get_roommemberdata_internal(&self, com_id: &ComId, req: &GetRoomMemberDataInternalRequest) -> Result<Vec<u8>, ErrorType> {
Expand Down

0 comments on commit 4494f54

Please sign in to comment.