Skip to content
This repository has been archived by the owner on Aug 30, 2024. It is now read-only.

Commit

Permalink
CORE-543 Set/Remove as Owner update is not reflected immediately (#50)
Browse files Browse the repository at this point in the history
* CORE-543 Set/Remove as Owner update is not reflected immediately

* CORE-543 Fixed an issue with Remove as Owner for himself

* CORE-534 Remove from room button is displayed if user is not owner anymore

* CORE-543 Potential fix for the issue with the isOwner property

* CORE-543 fix an isse with the changeOwner method

Co-authored-by: Sergey Volkov <sergey.volkov@coverwallet.com>
  • Loading branch information
Sergey-Volkov and Sergey Volkov authored Sep 9, 2021
1 parent e9d9cab commit 2f6bb28
Showing 1 changed file with 38 additions and 17 deletions.
55 changes: 38 additions & 17 deletions client/views/room/hooks/useUserInfoActions.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import { Button, ButtonGroup, Icon, Modal, Box } from '@rocket.chat/fuselage';
import { useAutoFocus, useMutableCallback } from '@rocket.chat/fuselage-hooks';
import { escapeHTML } from '@rocket.chat/string-helpers';
import { Meteor } from 'meteor/meteor';
import React, { useCallback, useMemo } from 'react';

import { hasPermission } from '../../../../app/authorization/client';
import { RoomRoles } from '../../../../app/models/client';
import { roomTypes, RoomMemberActions } from '../../../../app/utils/client';
import { handleError, roomTypes, RoomMemberActions } from '../../../../app/utils/client';
import { WebRTC } from '../../../../app/webrtc/client';
import { usePermission, useAllPermissions } from '../../../contexts/AuthorizationContext';
import { useAllPermissions } from '../../../contexts/AuthorizationContext';
import { useSetModal } from '../../../contexts/ModalContext';
import { useRoute } from '../../../contexts/RouterContext';
import { useMethod } from '../../../contexts/ServerContext';
Expand Down Expand Up @@ -151,12 +153,12 @@ export const useUserInfoActions = (user = {}, rid, reload) => {

const roomName = room && room.t && escapeHTML(roomTypes.getRoomName(room.t, room));

const userCanSetOwner = usePermission('set-owner', rid);
const userCanSetLeader = usePermission('set-leader', rid);
const userCanSetModerator = usePermission('set-moderator', rid);
const userCanMute = usePermission('mute-user', rid);
const userCanRemove = usePermission('remove-user', rid);
const userCanDirectMessage = usePermission('create-d');
const userCanSetOwner = hasPermission('set-owner', rid);
const userCanSetLeader = hasPermission('set-leader', rid);
const userCanSetModerator = hasPermission('set-moderator', rid);
const userCanMute = hasPermission('mute-user', rid);
const userCanRemove = hasPermission('remove-user', rid);
const userCanDirectMessage = hasPermission('create-d');

const shouldAllowCalls = getShouldAllowCalls(webRTCInstance);
const callInProgress = useReactiveValue(
Expand Down Expand Up @@ -221,18 +223,37 @@ export const useUserInfoActions = (user = {}, rid, reload) => {
);
}, [callInProgress, shouldAllowCalls, t, webRTCInstance]);

const changeOwnerEndpoint = isOwner ? 'removeOwner' : 'addOwner';
const changeOwnerEndpoint = isOwner ? 'removeRoomOwner' : 'addRoomOwner';
const changeOwnerMessage = isOwner
? 'User__username__removed_from__room_name__owners'
: 'User__username__is_now_a_owner_of__room_name_';
const changeOwner = useEndpointActionExperimental(
'POST',
`${endpointPrefix}.${changeOwnerEndpoint}`,
t(changeOwnerMessage, { username: user.username, room_name: roomName }),
);
const changeOwnerAction = useMutableCallback(async () =>
changeOwner({ roomId: rid, userId: uid }),
);

const changeOwner = (rid, uid, isOwner) => {
const userOwner = RoomRoles.findOne(
{ rid, 'u._id': uid, 'roles': 'owner' },
{ fields: { _id: 1 } },
);

if ((isOwner && userOwner == null) || (!isOwner && userOwner != null)) {
return;
}

Meteor.call(changeOwnerEndpoint, rid, uid, (error) => {
if (error) {
return handleError(error);
}
dispatchToastMessage({
type: 'success',
message: t(changeOwnerMessage, {
username: user.username,
room_name: roomName,
}),
});
});
};

const changeOwnerAction = useMutableCallback(async () => changeOwner(rid, uid, isOwner));

const changeOwnerOption = useMemo(
() =>
roomCanSetOwner &&
Expand Down

0 comments on commit 2f6bb28

Please sign in to comment.