Skip to content

Commit

Permalink
[IMPROVE] Add user to room on "Click to Join!" button press (#24041)
Browse files Browse the repository at this point in the history
* Display Join button in discussions inside channels, add user to room on 'Click To Join'

* Open jitsi call on Meteor.call callback

* Fixed jitsi

* Removed console.log

* Add user to room when clicking on the 'Call' tbb bar option

* Update getMessageFunction name

* Use canAccessRoomId function

Co-authored-by: Tasso Evangelista <tasso.evangelista@rocket.chat>
Co-authored-by: Pierre Lehnen <pierre.lehnen@rocket.chat>
Co-authored-by: Leonardo Ostjen Couto <leonardoostjen@gmail.com>
  • Loading branch information
4 people authored Feb 10, 2022
1 parent 4fdd558 commit 993c6fb
Show file tree
Hide file tree
Showing 8 changed files with 83 additions and 66 deletions.
12 changes: 1 addition & 11 deletions app/action-links/client/lib/actionLinks.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Meteor } from 'meteor/meteor';

import { handleError } from '../../../../client/lib/utils/handleError';
import { Messages, Subscriptions } from '../../../models/client';
import { Messages } from '../../../models/client';

// Action Links namespace creation.
export const actionLinks = {
Expand All @@ -24,16 +24,6 @@ export const actionLinks = {
});
}

const subscription = Subscriptions.findOne({
'rid': message.rid,
'u._id': userId,
});
if (!subscription) {
throw new Meteor.Error('error-not-allowed', 'Not allowed', {
function: 'actionLinks.getMessage',
});
}

if (!message.actionLinks || !message.actionLinks[name]) {
throw new Meteor.Error('error-invalid-actionlink', 'Invalid action link', {
function: 'actionLinks.getMessage',
Expand Down
30 changes: 12 additions & 18 deletions app/action-links/server/lib/actionLinks.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
import { Meteor } from 'meteor/meteor';

import { Messages, Subscriptions } from '../../../models/server';
import { getMessageForUser } from '../../../../server/lib/messages/getMessageForUser';

function getMessageById(messageId) {
try {
return Promise.await(getMessageForUser(messageId, Meteor.userId()));
} catch (e) {
throw new Meteor.Error(e.message, 'Invalid message', {
function: 'actionLinks.getMessage',
});
}
}

// Action Links namespace creation.
export const actionLinks = {
Expand All @@ -9,30 +19,14 @@ export const actionLinks = {
actionLinks.actions[name] = funct;
},
getMessage(name, messageId) {
const userId = Meteor.userId();
if (!userId) {
throw new Meteor.Error('error-invalid-user', 'Invalid user', {
function: 'actionLinks.getMessage',
});
}
const message = getMessageById(messageId);

const message = Messages.findOne({ _id: messageId });
if (!message) {
throw new Meteor.Error('error-invalid-message', 'Invalid message', {
function: 'actionLinks.getMessage',
});
}

const subscription = Subscriptions.findOne({
'rid': message.rid,
'u._id': userId,
});
if (!subscription) {
throw new Meteor.Error('error-not-allowed', 'Not allowed', {
function: 'actionLinks.getMessage',
});
}

if (!message.actionLinks || !message.actionLinks[name]) {
throw new Meteor.Error('error-invalid-actionlink', 'Invalid action link', {
function: 'actionLinks.getMessage',
Expand Down
11 changes: 1 addition & 10 deletions app/lib/lib/roomTypes/private.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Meteor } from 'meteor/meteor';

import { ChatRoom, ChatSubscription } from '../../../models';
import { ChatRoom } from '../../../models';
import { openRoom } from '../../../ui-utils';
import { settings } from '../../../settings';
import { hasAtLeastOnePermission, hasPermission } from '../../../authorization';
Expand Down Expand Up @@ -74,15 +74,6 @@ export class PrivateRoomType extends RoomTypeConfig {
return hasAtLeastOnePermission(['add-user-to-any-p-room', 'add-user-to-joined-room'], room._id);
}

canSendMessage(roomId) {
// TODO: remove duplicated code
return (
ChatSubscription.find({
rid: roomId,
}).count() > 0
);
}

allowRoomSettingChange(room, setting) {
switch (setting) {
case RoomSettingsEnum.JOIN_CODE:
Expand Down
16 changes: 1 addition & 15 deletions app/lib/lib/roomTypes/public.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Meteor } from 'meteor/meteor';

import { openRoom } from '../../../ui-utils';
import { ChatRoom, ChatSubscription } from '../../../models';
import { ChatRoom } from '../../../models';
import { settings } from '../../../settings';
import { hasAtLeastOnePermission } from '../../../authorization';
import { getUserPreference, RoomTypeConfig, RoomTypeRouteConfig, RoomSettingsEnum, UiTextContext, RoomMemberActions } from '../../../utils';
Expand Down Expand Up @@ -86,20 +86,6 @@ export class PublicRoomType extends RoomTypeConfig {
return hasAtLeastOnePermission(['add-user-to-any-c-room', 'add-user-to-joined-room'], room._id);
}

canSendMessage(roomId) {
const room = ChatRoom.findOne({ _id: roomId, t: 'c' }, { fields: { prid: 1 } });
if (room.prid) {
return true;
}

// TODO: remove duplicated code
return (
ChatSubscription.find({
rid: roomId,
}).count() > 0
);
}

enableMembersListProfile() {
return true;
}
Expand Down
1 change: 1 addition & 0 deletions app/ui-utils/client/lib/openRoom.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ export const openRoom = async function (type, name, render = true) {
}

NewRoomManager.open(room._id);
Session.set('openedRoom', room._id);

fireGlobalEvent('room-opened', _.omit(room, 'usernames'));

Expand Down
52 changes: 40 additions & 12 deletions app/videobridge/client/actionLink.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,48 @@ import { Rooms } from '../../models';
import { dispatchToastMessage } from '../../../client/lib/toast';

actionLinks.register('joinJitsiCall', function (message, params, instance) {
if (Session.get('openedRoom')) {
const rid = Session.get('openedRoom');
const rid = Session.get('openedRoom');
if (!rid) {
return;
}

const room = Rooms.findOne({ _id: rid });
const username = Meteor.user()?.username;

if (!room) {
dispatchToastMessage({ type: 'info', message: TAPi18n.__('Call Already Ended', '') });
return;
}

if (room?.muted?.includes(username)) {
dispatchToastMessage({ type: 'error', message: TAPi18n.__('You_have_been_muted', '') });
return;
}

const clickTime = new Date();
const jitsiTimeout = new Date(room.jitsiTimeout);

const room = Rooms.findOne({ _id: rid });
const username = Meteor.user()?.username;
const currentTime = new Date().getTime();
const jitsiTimeout = new Date((room && room.jitsiTimeout) || currentTime).getTime();
if (jitsiTimeout > clickTime) {
instance.tabBar.open('video');
return;
}

// Get updated room info from the server to check if the call is still happening
Meteor.call('getRoomById', rid, (err, result) => {
if (err) {
throw err;
}

if (room && room?.muted?.includes(username)) {
dispatchToastMessage({ type: 'error', message: TAPi18n.__('You_have_been_muted', '') });
} else if (jitsiTimeout > currentTime) {
// If the openedRoom has changed, abort
if (rid !== Session.get('openedRoom')) {
return;
}

if (result?.jitsiTimeout && result.jitsiTimeout instanceof Date && result.jitsiTimeout > clickTime) {
instance.tabBar.open('video');
} else {
dispatchToastMessage({ type: 'info', message: TAPi18n.__('Call Already Ended', '') });
return;
}
}

dispatchToastMessage({ type: 'info', message: TAPi18n.__('Call Already Ended', '') });
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { useMutableCallback, useSafely } from '@rocket.chat/fuselage-hooks';
import { clear } from '@rocket.chat/memo';
import React, { useRef, useEffect, useState, useMemo, useLayoutEffect, memo } from 'react';

import { Subscriptions } from '../../../../../../app/models/client';
import { HEARTBEAT, TIMEOUT, DEBOUNCE } from '../../../../../../app/videobridge/constants';
import { useConnectionStatus } from '../../../../../contexts/ConnectionStatusContext';
import { useSetModal } from '../../../../../contexts/ModalContext';
Expand Down Expand Up @@ -44,6 +45,7 @@ const CallJitsiWithData = ({ rid }) => {
const closeModal = useMutableCallback(() => setModal(null));
const generateAccessToken = useMethod('jitsi:generateAccessToken');
const updateTimeout = useMethod('jitsi:updateTimeout');
const joinRoom = useMethod('joinRoom');
const dispatchToastMessage = useToastMessageDispatch();
const t = useTranslation();

Expand Down Expand Up @@ -187,6 +189,10 @@ const CallJitsiWithData = ({ rid }) => {
}

setAccepted(true);
const sub = Subscriptions.findOne({ rid, 'u._id': user._id });
if (!sub) {
joinRoom(rid);
}

if (openNewWindow) {
handleClose();
Expand Down
21 changes: 21 additions & 0 deletions server/lib/messages/getMessageForUser.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import type { IUser } from '../../../definition/IUser';
import type { IMessage } from '../../../definition/IMessage/IMessage';
import { Messages } from '../../../app/models/server/raw';
import { canAccessRoomId } from '../../../app/authorization/server';

export async function getMessageForUser(messageId: IMessage['_id'], uid: IUser['_id']): Promise<IMessage | undefined> {
if (!uid) {
throw new Error('error-invalid-user');
}

const message = await Messages.findOne(messageId);
if (!message) {
return;
}

if (!canAccessRoomId(message.rid, uid)) {
throw new Error('error-not-allowed');
}

return message;
}

0 comments on commit 993c6fb

Please sign in to comment.