Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(Group): Prevent members from removing others #1906

Merged
merged 7 commits into from
Mar 8, 2024
73 changes: 50 additions & 23 deletions ui/src/layouts/chats/presentation/chat/edit_group.rs
Original file line number Diff line number Diff line change
@@ -3,6 +3,7 @@ use std::collections::{BTreeMap, HashMap, HashSet};

use common::{
icons::outline::Shape as Icon,
icons::Icon as IconElement,
language::get_local_text,
state::{Identity, State},
warp_runner::{RayGunCmd, WarpCmd},
@@ -45,6 +46,8 @@ pub fn EditGroup(cx: Scope) -> Element {
let edit_group_action = use_state(cx, || EditGroupAction::Remove);
let conv_id = state.read().get_active_chat().unwrap().id;

let creator_id = state.read().get_active_chat()?.creator.clone()?;

let friends_did_already_in_group = state.read().get_active_chat().unwrap().participants;

let friends_list: HashMap<DID, Identity> = HashMap::from_iter(
@@ -111,6 +114,9 @@ pub fn EditGroup(cx: Scope) -> Element {
}
});

let creator_did2 = creator_id.clone();
let am_i_group_creator = creator_id == state.read().did_key();

cx.render(rsx!(
div {
id: "edit-members",
@@ -162,13 +168,16 @@ pub fn EditGroup(cx: Scope) -> Element {
aria_label: "friend-group",
friends.iter().map(
|_friend| {
let is_group_creator = creator_did2.clone() == _friend.clone().did_key();
rsx!(
friend_row {
add_or_remove: if *edit_group_action.current() == EditGroupAction::Add {
"add".into()
} else {
"remove".into()
},
friend_is_group_creator: is_group_creator,
am_i_group_creator: am_i_group_creator,
friend: _friend.clone(),
minimal: minimal,
conv_id: conv_id,
@@ -196,6 +205,8 @@ pub fn EditGroup(cx: Scope) -> Element {
#[derive(Props, Eq, PartialEq)]
pub struct FriendRowProps {
add_or_remove: String,
friend_is_group_creator: bool,
am_i_group_creator: bool,
minimal: bool,
friend: Identity,
conv_id: Uuid,
@@ -272,34 +283,50 @@ fn friend_row(cx: Scope<FriendRowProps>) -> Element {
_friend.username(),
},
},
Button {
aria_label: if cx.props.add_or_remove == "add" {
get_local_text("uplink.add")
} else {
get_local_text("uplink.remove")
},
icon: if cx.props.add_or_remove == "add" {
Icon::UserPlus
} else {
Icon::UserMinus
},
text: if cx.props.minimal { String::new() }
else if cx.props.add_or_remove == "add" {
if cx.props.friend_is_group_creator {
rsx!(
div {
class: "group-creator-container",
IconElement {
icon: Icon::Satellite
}
span {
class: "group-creator-text",
get_local_text("messages.group-creator-label")
}
}
)
}
if cx.props.am_i_group_creator || cx.props.add_or_remove == "add" {
rsx!(Button {
aria_label: if cx.props.add_or_remove == "add" {
get_local_text("uplink.add")
} else {
get_local_text("uplink.remove")
}
,
onpress: move |_| {
let mut friends = selected_friends.get().clone();
friends.clear();
selected_friends.set(vec![_friend.did_key()].into_iter().collect());
if cx.props.add_or_remove == "add" {
ch.send(ChanCmd::AddParticipants);
},
icon: if cx.props.add_or_remove == "add" {
Icon::UserPlus
} else {
ch.send(ChanCmd::RemoveParticipants);
Icon::UserMinus
},
text: if cx.props.minimal { String::new() }
else if cx.props.add_or_remove == "add" {
get_local_text("uplink.add")
} else {
get_local_text("uplink.remove")
}
,
onpress: move |_| {
let mut friends = selected_friends.get().clone();
friends.clear();
selected_friends.set(vec![_friend.did_key()].into_iter().collect());
if cx.props.add_or_remove == "add" {
ch.send(ChanCmd::AddParticipants);
} else {
ch.send(ChanCmd::RemoveParticipants);
}
}
}
})
}
}
))