Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
Show all changes
19 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
5 changes: 5 additions & 0 deletions .changeset/calm-penguins-do.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@rocket.chat/meteor": patch
---

Now we are considering channels with auto-join inside teams on user creation
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import { Message } from '@rocket.chat/core-services';
import type { IUser } from '@rocket.chat/core-typings';
import { Subscriptions, Rooms } from '@rocket.chat/models';
import { Subscriptions } from '@rocket.chat/models';

import { callbacks } from '../../../../lib/callbacks';
import { getSubscriptionAutotranslateDefaultConfig } from '../../../../server/lib/getSubscriptionAutotranslateDefaultConfig';
import { getDefaultChannels } from './getDefaultChannels';

export const addUserToDefaultChannels = async function (user: IUser, silenced?: boolean): Promise<void> {
await callbacks.run('beforeJoinDefaultChannels', user);
const defaultRooms = await Rooms.findByDefaultAndTypes(true, ['c', 'p'], {
projection: { usernames: 0 },
}).toArray();
const defaultRooms = await getDefaultChannels();

for await (const room of defaultRooms) {
if (!(await Subscriptions.findOneByRoomIdAndUserId(room._id, user._id, { projection: { _id: 1 } }))) {
const autoTranslateConfig = getSubscriptionAutotranslateDefaultConfig(user);
Expand Down
25 changes: 25 additions & 0 deletions apps/meteor/app/lib/server/functions/getDefaultChannels.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import type { IRoom } from '@rocket.chat/core-typings';
import { Rooms } from '@rocket.chat/models';

export async function getDefaultChannels(): Promise<IRoom[]> {
const defaultRooms = await Rooms.findByDefaultAndTypes(true, ['c', 'p'], {
projection: { usernames: 0 },
}).toArray();
const roomsThatAreGoingToBeJoined = new Set(defaultRooms.map((room) => room._id));

// If any of those are teams, we need to get all the channels that have the auto-join flag as well
const teamRooms = defaultRooms.filter((room) => room.teamMain && room.teamId);
if (teamRooms.length > 0) {
for await (const teamRoom of teamRooms) {
const defaultTeamRooms = await Rooms.findDefaultRoomsForTeam(teamRoom.teamId).toArray();

const defaultTeamRoomsThatWereNotAlreadyAdded = defaultTeamRooms.filter((channel) => !roomsThatAreGoingToBeJoined.has(channel._id));

defaultTeamRoomsThatWereNotAlreadyAdded.forEach((channel) => roomsThatAreGoingToBeJoined.add(channel._id));
// Add the channels to the defaultRooms list
defaultRooms.push(...defaultTeamRoomsThatWereNotAlreadyAdded);
}
}

return defaultRooms;
}
14 changes: 14 additions & 0 deletions apps/meteor/tests/data/rooms.helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,17 @@ function actionRoom({ action, type, roomId }) {
export const deleteRoom = ({ type, roomId }) => actionRoom({ action: 'delete', type, roomId });

export const closeRoom = ({ type, roomId }) => actionRoom({ action: 'close', type, roomId });

export const setDefaultRoom = ({ roomId }) => {
return request
.post(api('rooms.saveRoomSettings'))
.set(credentials)
.send({
rid: roomId,
default: true,
favorite: {
defaultValue: true,
favorite: false
}
Comment thread
MarcosSpessatto marked this conversation as resolved.
Resolved
});
};
84 changes: 83 additions & 1 deletion apps/meteor/tests/end-to-end/api/01-users.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { MAX_BIO_LENGTH, MAX_NICKNAME_LENGTH } from '../../data/constants.ts';
import { customFieldText, clearCustomFields, setCustomFields } from '../../data/custom-fields.js';
import { imgURL } from '../../data/interactions';
import { updatePermission, updateSetting } from '../../data/permissions.helper';
import { createRoom, deleteRoom } from '../../data/rooms.helper';
import { createRoom, deleteRoom, setDefaultRoom } from '../../data/rooms.helper';
import { adminEmail, preferences, password, adminUsername } from '../../data/user';
import { createUser, login, deleteUser, getUserStatus, getUserByUsername } from '../../data/users.helper.js';

Expand All @@ -28,6 +28,19 @@ async function joinChannel(userCredentials, roomId) {
});
}

async function createTeam(userCredentials, teamName) {
Comment thread
matheusbsilva137 marked this conversation as resolved.
Resolved
const res = await request.post(api('teams.create')).set(userCredentials).send({
name: teamName,
type: 0,
});

return res.body.team._id;
}

function deleteGroup(groupId) {
Comment thread
matheusbsilva137 marked this conversation as resolved.
Resolved
return request.post(api('groups.delete')).set(credentials).query({ roomId: groupId });
}

const targetUser = {};

describe('[Users]', function () {
Expand Down Expand Up @@ -277,6 +290,75 @@ describe('[Users]', function () {
.end(done);
});
});

describe('auto join default channels', () => {
let defaultTeamId;
let group;
let user;

before(async () => {
const teamName = `defaultTeam_${Date.now()}`;
user = await createUser({ joinDefaultChannels: true });

defaultTeamId = await createTeam(credentials, teamName);

await setDefaultRoom({ roomId: defaultTeamId, roomName: teamName });
});

after(async () => {
Comment thread
Gustrb marked this conversation as resolved.
Resolved
if (group) {
await deleteGroup(group.body.group._id);
}
await deleteRoom({ roomId: defaultTeamId, type: 'c' });
await deleteUser(user);
});

it('should create a subscription for the user in the default channels if the joinDefaultChannels is true', async () => {
await request
.get(api('subscriptions.getOne'))
.set(credentials)
.query({ roomId: defaultTeamId })
.expect('Content-Type', 'application/json')
.expect(200)
.expect((res) => {
expect(res.body).to.have.property('success', true);
Comment thread
matheusbsilva137 marked this conversation as resolved.
Resolved
});
});

it('should create a subscription for the user in all the auto join channels of the team', async () => {
Comment thread
matheusbsilva137 marked this conversation as resolved.
Resolved
group = await request
Comment thread
matheusbsilva137 marked this conversation as resolved.
Resolved
.post(api('groups.create'))
.set(credentials)
.send({
name: `defaultGroup_${Date.now()}`,
readonly: false,
members: [user.username],
extraData: {
broadcast: false,
encrypted: false,
teamId: defaultTeamId,
topic: '',
},
});
await request.post(api('teams.updateRoom')).set(credentials).send({
roomId: group.body.group._id,
isDefault: true,
});

const user2 = await createUser({ joinDefaultChannels: true });
Comment thread
matheusbsilva137 marked this conversation as resolved.
Resolved
const user2Credentials = await login(user2.username, password);

await request
.get(api('subscriptions.getOne'))
.set(user2Credentials)
.query({ roomId: group.body.group._id })
.expect('Content-Type', 'application/json')
.expect(200)
.expect((res) => {
expect(res.body).to.have.property('success', true);
Comment thread
matheusbsilva137 marked this conversation as resolved.
Resolved
});
});
});
});

describe('[/users.register]', () => {
Expand Down