From 8711658e75134e50c69ec19244f0f31e1e5e3c60 Mon Sep 17 00:00:00 2001 From: Ajay Bura Date: Fri, 29 Oct 2021 17:13:33 +0530 Subject: [PATCH] Feature: invite/disinvite from profile viewer Signed-off-by: Ajay Bura --- .../profile-viewer/ProfileViewer.jsx | 43 +++++++++++++++++-- src/client/action/room.js | 17 +++++--- 2 files changed, 50 insertions(+), 10 deletions(-) diff --git a/src/app/organisms/profile-viewer/ProfileViewer.jsx b/src/app/organisms/profile-viewer/ProfileViewer.jsx index 17405e9..6705b29 100644 --- a/src/app/organisms/profile-viewer/ProfileViewer.jsx +++ b/src/app/organisms/profile-viewer/ProfileViewer.jsx @@ -78,19 +78,27 @@ SessionInfo.propTypes = { userId: PropTypes.string.isRequired, }; -function ProfileFooter({ userId, onRequestClose }) { +function ProfileFooter({ roomId, userId, onRequestClose }) { const [isCreatingDM, setIsCreatingDM] = useState(false); const [isIgnoring, setIsIgnoring] = useState(false); const [isUserIgnored, setIsUserIgnored] = useState(initMatrix.matrixClient.isUserIgnored(userId)); - const mx = initMatrix.matrixClient; const isMountedRef = useRef(true); + const mx = initMatrix.matrixClient; + const room = mx.getRoom(roomId); + const member = room.getMember(userId); + const isInvitable = member?.membership !== 'join' && member?.membership !== 'ban'; + + const [isInviting, setIsInviting] = useState(false); + const [isInvited, setIsInvited] = useState(member?.membership === 'invite'); useEffect(() => () => { isMountedRef.current = false; }, []); useEffect(() => { setIsUserIgnored(initMatrix.matrixClient.isUserIgnored(userId)); + setIsIgnoring(false); + setIsInviting(false); }, [userId]); async function openDM() { @@ -144,6 +152,24 @@ function ProfileFooter({ userId, onRequestClose }) { setIsIgnoring(false); } } + + async function toggleInvite() { + try { + setIsInviting(true); + let isInviteSent = false; + if (isInvited) await roomActions.kick(roomId, userId); + else { + await roomActions.invite(roomId, userId); + isInviteSent = true; + } + if (isMountedRef.current === false) return; + setIsInvited(isInviteSent); + setIsInviting(false); + } catch { + setIsInviting(false); + } + } + return (
- + { member?.membership === 'join' && } + {room.canInvite(mx.getUserId()) && isInvitable && ( + + )}