Feature: invite/disinvite from profile viewer

Signed-off-by: Ajay Bura <ajbura@gmail.com>
This commit is contained in:
Ajay Bura 2021-10-29 17:13:33 +05:30
parent e25dc46863
commit 8711658e75
2 changed files with 50 additions and 10 deletions

View file

@ -78,19 +78,27 @@ SessionInfo.propTypes = {
userId: PropTypes.string.isRequired, userId: PropTypes.string.isRequired,
}; };
function ProfileFooter({ userId, onRequestClose }) { function ProfileFooter({ roomId, userId, onRequestClose }) {
const [isCreatingDM, setIsCreatingDM] = useState(false); const [isCreatingDM, setIsCreatingDM] = useState(false);
const [isIgnoring, setIsIgnoring] = useState(false); const [isIgnoring, setIsIgnoring] = useState(false);
const [isUserIgnored, setIsUserIgnored] = useState(initMatrix.matrixClient.isUserIgnored(userId)); const [isUserIgnored, setIsUserIgnored] = useState(initMatrix.matrixClient.isUserIgnored(userId));
const mx = initMatrix.matrixClient;
const isMountedRef = useRef(true); 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(() => () => { useEffect(() => () => {
isMountedRef.current = false; isMountedRef.current = false;
}, []); }, []);
useEffect(() => { useEffect(() => {
setIsUserIgnored(initMatrix.matrixClient.isUserIgnored(userId)); setIsUserIgnored(initMatrix.matrixClient.isUserIgnored(userId));
setIsIgnoring(false);
setIsInviting(false);
}, [userId]); }, [userId]);
async function openDM() { async function openDM() {
@ -144,6 +152,24 @@ function ProfileFooter({ userId, onRequestClose }) {
setIsIgnoring(false); 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 ( return (
<div className="profile-viewer__buttons"> <div className="profile-viewer__buttons">
<Button <Button
@ -153,7 +179,16 @@ function ProfileFooter({ userId, onRequestClose }) {
> >
{isCreatingDM ? 'Creating room...' : 'Message'} {isCreatingDM ? 'Creating room...' : 'Message'}
</Button> </Button>
<Button>Mention</Button> { member?.membership === 'join' && <Button>Mention</Button>}
{room.canInvite(mx.getUserId()) && isInvitable && (
<Button onClick={toggleInvite}>
{
isInvited
? `${isInviting ? 'Disinviting...' : 'Disinvite'}`
: `${isInviting ? 'Inviting...' : 'Invite'}`
}
</Button>
)}
<Button <Button
variant={isUserIgnored ? 'positive' : 'danger'} variant={isUserIgnored ? 'positive' : 'danger'}
onClick={toggleIgnore} onClick={toggleIgnore}
@ -169,6 +204,7 @@ function ProfileFooter({ userId, onRequestClose }) {
); );
} }
ProfileFooter.propTypes = { ProfileFooter.propTypes = {
roomId: PropTypes.string.isRequired,
userId: PropTypes.string.isRequired, userId: PropTypes.string.isRequired,
onRequestClose: PropTypes.func.isRequired, onRequestClose: PropTypes.func.isRequired,
}; };
@ -231,6 +267,7 @@ function ProfileViewer() {
<SessionInfo userId={userId} /> <SessionInfo userId={userId} />
{ userId !== mx.getUserId() && ( { userId !== mx.getUserId() && (
<ProfileFooter <ProfileFooter
roomId={roomId}
userId={userId} userId={userId}
onRequestClose={() => setIsOpen(false)} onRequestClose={() => setIsOpen(false)}
/> />

View file

@ -188,12 +188,15 @@ async function create(opts) {
async function invite(roomId, userId) { async function invite(roomId, userId) {
const mx = initMatrix.matrixClient; const mx = initMatrix.matrixClient;
try { const result = await mx.invite(roomId, userId);
const result = await mx.invite(roomId, userId); return result;
return result; }
} catch (e) {
throw new Error(e); async function kick(roomId, userId) {
} const mx = initMatrix.matrixClient;
const result = await mx.kick(roomId, userId);
return result;
} }
function createSpaceShortcut(roomId) { function createSpaceShortcut(roomId) {
@ -212,6 +215,6 @@ function deleteSpaceShortcut(roomId) {
export { export {
join, leave, join, leave,
create, invite, create, invite, kick,
createSpaceShortcut, deleteSpaceShortcut, createSpaceShortcut, deleteSpaceShortcut,
}; };