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

[IMPROVE] Add user to room on "Click to Join!" button press #24041

Merged
merged 17 commits into from
Feb 10, 2022
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
Show all changes
17 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 getMessage(messageId) {
matheusbsilva137 marked this conversation as resolved.
Show resolved Hide resolved
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 = getMessage(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 @@ -60,6 +60,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;
}

matheusbsilva137 marked this conversation as resolved.
Show resolved Hide resolved
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', '') });
});
});
27 changes: 27 additions & 0 deletions server/lib/messages/getMessageForUser.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import type { IUser } from '../../../definition/IUser';
import type { IMessage } from '../../../definition/IMessage/IMessage';
import { Messages, Rooms } from '../../../app/models/server/raw';
import { canAccessRoomAsync } from '../../../app/authorization/server/functions/canAccessRoom';

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;
}

// #ToDo: Remove this find and call canAccessRoomId once that is merged.
const room = await Rooms.findOne(message.rid);
matheusbsilva137 marked this conversation as resolved.
Show resolved Hide resolved
if (!room) {
throw new Error('error-not-allowed');
}

if (!(await canAccessRoomAsync(room, { _id: uid }))) {
throw new Error('error-not-allowed');
}

return message;
}