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

feat: Add membersByHighestRole endpoints #29870

Open
wants to merge 68 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
68 commits
Select commit Hold shift + click to select a range
c0c0227
Add groups.membersByRole endpoint
matheusbsilva137 Jul 19, 2023
82ea93c
Update endpoint name and fix logic
matheusbsilva137 Jul 19, 2023
1c25855
Merge branch 'develop' of https://github.com/RocketChat/Rocket.Chat i…
matheusbsilva137 Jul 19, 2023
6c5db96
Add channels endpoint
matheusbsilva137 Jul 20, 2023
577c89a
Update endpoint response
matheusbsilva137 Jul 21, 2023
7690fa9
Merge branch 'develop' into feat/members-by-role
matheusbsilva137 Jul 21, 2023
66f3ce5
Remove let on aggregation
matheusbsilva137 Jul 21, 2023
b8f313a
Improve typing
matheusbsilva137 Jul 21, 2023
c838f9f
Improve formatting
matheusbsilva137 Jul 21, 2023
d6f0dcc
Evaluate length as boolean
matheusbsilva137 Jul 21, 2023
c224675
Create new variable to regex string
matheusbsilva137 Jul 21, 2023
6e87c8c
Make options field optional
matheusbsilva137 Jul 21, 2023
120ce5d
Return highestRole object instead of fields
matheusbsilva137 Jul 21, 2023
c6252da
Update reduce to map
matheusbsilva137 Jul 21, 2023
4f7b113
Merge branch 'develop' into feat/members-by-role
matheusbsilva137 Jul 23, 2023
d17470e
Fix params order
matheusbsilva137 Jul 25, 2023
8aa7f00
Merge branch 'develop' of https://github.com/RocketChat/Rocket.Chat i…
matheusbsilva137 Jul 25, 2023
6c90dbb
Merge branch 'feat/members-by-role' of https://github.com/RocketChat/…
matheusbsilva137 Jul 25, 2023
58f0a59
Merge branch 'develop' of https://github.com/RocketChat/Rocket.Chat i…
matheusbsilva137 Jul 28, 2023
569b80a
Fix lint
matheusbsilva137 Jul 31, 2023
46c8a18
Add allowDiskUse aggregation option
matheusbsilva137 Jul 31, 2023
96168d3
Use ajv on channels.membersByHighestRole endpoint typing
matheusbsilva137 Jul 31, 2023
2dda7cd
Merge branch 'develop' of https://github.com/RocketChat/Rocket.Chat i…
matheusbsilva137 Aug 2, 2023
0353898
Add end-to-end tests
matheusbsilva137 Aug 2, 2023
5a668fc
Make lookup query compatible with mongo 4.4
matheusbsilva137 Aug 3, 2023
1a736d5
Check if result is undefined before extracting props
matheusbsilva137 Aug 3, 2023
6ee11bd
Merge branch 'develop' of https://github.com/RocketChat/Rocket.Chat i…
matheusbsilva137 Aug 3, 2023
2f5ccc4
Improve tests
matheusbsilva137 Aug 10, 2023
a0e011b
Improve typing
matheusbsilva137 Aug 10, 2023
c6ffd7b
Change roles object key
matheusbsilva137 Aug 10, 2023
dba1c87
Add IUserWithRoles to core-typings
matheusbsilva137 Aug 10, 2023
61f97dc
Fix tests
matheusbsilva137 Aug 14, 2023
50bef1c
Merge branch 'develop' into feat/members-by-role
matheusbsilva137 Aug 14, 2023
1784481
Merge branch 'develop' into feat/members-by-role
matheusbsilva137 Aug 16, 2023
5b5a46e
Fix tests
matheusbsilva137 Aug 16, 2023
ae7355d
Fix endpoint fails for members with no roles
matheusbsilva137 Aug 16, 2023
cb83fbf
Fix tests
matheusbsilva137 Aug 17, 2023
d4ed1d3
Remove statusConnection check on tests
matheusbsilva137 Aug 17, 2023
a11dc35
Update roles to roomRoles in tests
matheusbsilva137 Aug 18, 2023
9164db8
oops
matheusbsilva137 Aug 18, 2023
5e3f29c
Improve translations
matheusbsilva137 Aug 18, 2023
a85e403
Revert "Improve translations"
matheusbsilva137 Aug 18, 2023
78f908f
Create thick-swans-drop.md
matheusbsilva137 Aug 18, 2023
865348d
Update changesets
matheusbsilva137 Aug 21, 2023
c5951dc
Merge branch 'develop' into feat/members-by-role
matheusbsilva137 Aug 24, 2023
ef014af
Merge branch 'develop' into feat/members-by-role
scuciatto Aug 24, 2023
8689619
Improve aggregation performance
matheusbsilva137 Aug 31, 2023
e6242ea
Fix tests
matheusbsilva137 Aug 31, 2023
c464f41
Merge branch 'develop' into feat/members-by-role
matheusbsilva137 Aug 31, 2023
5369d2f
Merge branch 'develop' into feat/members-by-role
matheusbsilva137 Aug 31, 2023
cd10d92
Merge branch 'develop' of https://github.com/RocketChat/Rocket.Chat i…
matheusbsilva137 Aug 31, 2023
6323a7c
Merge branch 'develop' into feat/members-by-role
matheusbsilva137 Aug 31, 2023
48874bc
Merge branch 'feat/members-by-role' of https://github.com/RocketChat/…
matheusbsilva137 Sep 5, 2023
806a2a4
Merge branch 'develop' into feat/members-by-role
matheusbsilva137 Sep 22, 2023
efefd11
Merge branch 'feat/members-by-role' of https://github.com/RocketChat/…
matheusbsilva137 Sep 22, 2023
432ac79
Remove skip param
matheusbsilva137 Sep 26, 2023
9627c99
Replace aggregation by 3 finds
matheusbsilva137 Sep 28, 2023
caa78a4
Remove offset from end-to-end tests
matheusbsilva137 Sep 28, 2023
ebbc00c
Replace owner/moderator finds by aggregation
matheusbsilva137 Sep 28, 2023
89da189
Fix typecheck
matheusbsilva137 Sep 29, 2023
8c5d745
Merge branch 'develop' of https://github.com/RocketChat/Rocket.Chat i…
matheusbsilva137 Sep 29, 2023
1b7112b
Merge branch 'develop' of https://github.com/RocketChat/Rocket.Chat i…
matheusbsilva137 Oct 2, 2023
62d6c0e
Merge branch 'develop' of https://github.com/RocketChat/Rocket.Chat i…
matheusbsilva137 Oct 6, 2023
c9052a5
Merge branch 'develop' into feat/members-by-role
matheusbsilva137 Oct 6, 2023
806040c
Merge branch 'develop' into feat/members-by-role
matheusbsilva137 Oct 9, 2023
c7caccc
Merge branch 'develop' into feat/members-by-role
matheusbsilva137 Dec 5, 2023
9db6554
Merge branch 'develop' of https://github.com/RocketChat/Rocket.Chat i…
matheusbsilva137 Feb 15, 2024
cd44cfc
Fix typecheck
matheusbsilva137 Feb 15, 2024
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
8 changes: 8 additions & 0 deletions .changeset/thick-swans-drop.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
"@rocket.chat/meteor": minor
"@rocket.chat/core-typings": minor
"@rocket.chat/model-typings": minor
"@rocket.chat/rest-typings": minor
---

Added `membersByHighestRole` endpoints, which enables users to retrieve room members sorted by their room-scoped roles
38 changes: 37 additions & 1 deletion apps/meteor/app/api/server/v1/channels.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Team, Room } from '@rocket.chat/core-services';
import type { IRoom, ISubscription, IUser, RoomType, IUpload } from '@rocket.chat/core-typings';
import type { IRoom, ISubscription, IUser, RoomType, IUpload, UserStatus } from '@rocket.chat/core-typings';
import { Integrations, Messages, Rooms, Subscriptions, Uploads, Users } from '@rocket.chat/models';
import {
isChannelsAddAllProps,
Expand All @@ -24,6 +24,7 @@ import { Meteor } from 'meteor/meteor';

import { isTruthy } from '../../../../lib/isTruthy';
import { findUsersOfRoom } from '../../../../server/lib/findUsersOfRoom';
import { findUsersOfRoomByHighestRole } from '../../../../server/lib/findUsersOfRoomByHighestRole';
import { hideRoomMethod } from '../../../../server/methods/hideRoom';
import { removeUserFromRoomMethod } from '../../../../server/methods/removeUserFromRoom';
import { canAccessRoomAsync } from '../../../authorization/server';
Expand Down Expand Up @@ -1094,6 +1095,41 @@ API.v1.addRoute(
},
);

API.v1.addRoute(
'channels.membersByHighestRole',
{ authRequired: true },
{
async get() {
const findResult = await findChannelByIdOrName({
params: this.queryParams,
checkedArchived: false,
});

if (findResult.broadcast && !(await hasPermissionAsync(this.userId, 'view-broadcast-member-list', findResult._id))) {
return API.v1.unauthorized();
}

const { count: limit } = await getPaginationItems(this.queryParams);
const { sort = {} } = await this.parseJsonQuery();
const { status, filter } = this.queryParams;

const { members, total } = await findUsersOfRoomByHighestRole({
rid: findResult._id,
...(status && { status: { $in: status as UserStatus[] } }),
limit,
filter,
...(sort?.username && { sort: { username: sort.username } }),
});

return API.v1.success({
members,
count: members.length,
total,
});
},
},
);

API.v1.addRoute(
'channels.online',
{ authRequired: true },
Expand Down
38 changes: 37 additions & 1 deletion apps/meteor/app/api/server/v1/groups.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { Team, isMeteorError } from '@rocket.chat/core-services';
import type { IIntegration, IUser, IRoom, RoomType } from '@rocket.chat/core-typings';
import type { IIntegration, IUser, IRoom, RoomType, UserStatus } from '@rocket.chat/core-typings';
import { Integrations, Messages, Rooms, Subscriptions, Uploads, Users } from '@rocket.chat/models';
import { check, Match } from 'meteor/check';
import { Meteor } from 'meteor/meteor';
import type { Filter } from 'mongodb';

import { findUsersOfRoom } from '../../../../server/lib/findUsersOfRoom';
import { findUsersOfRoomByHighestRole } from '../../../../server/lib/findUsersOfRoomByHighestRole';
import { hideRoomMethod } from '../../../../server/methods/hideRoom';
import { removeUserFromRoomMethod } from '../../../../server/methods/removeUserFromRoom';
import { canAccessRoomAsync, roomAccessAttributes } from '../../../authorization/server';
Expand Down Expand Up @@ -747,6 +748,41 @@ API.v1.addRoute(
},
);

API.v1.addRoute(
'groups.membersByHighestRole',
{ authRequired: true },
{
async get() {
const findResult = await findPrivateGroupByIdOrName({
params: this.queryParams,
userId: this.userId,
});

if (findResult.broadcast && !(await hasPermissionAsync(this.userId, 'view-broadcast-member-list', findResult.rid))) {
return API.v1.unauthorized();
}

const { count: limit } = await getPaginationItems(this.queryParams);
const { sort = {} } = await this.parseJsonQuery();
const { status, filter } = this.queryParams;

const { members, total } = await findUsersOfRoomByHighestRole({
rid: findResult.rid,
...(status && { status: { $in: status as UserStatus[] } }),
limit,
filter,
...(sort?.username && { sort: { username: sort.username } }),
});

return API.v1.success({
members,
count: members.length,
MarcosSpessatto marked this conversation as resolved.
Show resolved Hide resolved
total,
});
},
},
);

API.v1.addRoute(
'groups.messages',
{ authRequired: true },
Expand Down
2 changes: 1 addition & 1 deletion apps/meteor/app/api/server/v1/im.ts
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,7 @@ API.v1.addRoute(

const searchFields = settings.get<string>('Accounts_SearchFields').trim().split(',');

const { cursor, totalCount } = Users.findPaginatedByActiveUsersExcept(filter, [], options, searchFields, [extraQuery]);
const { cursor, totalCount } = Users.findPaginatedByActiveUsersExcept(filter, [], searchFields, options, [extraQuery]);

const [members, total] = await Promise.all([cursor.toArray(), totalCount]);

Expand Down
2 changes: 1 addition & 1 deletion apps/meteor/server/lib/findUsersOfRoom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export function findUsersOfRoom({ rid, status, skip = 0, limit = 0, filter = '',

const searchFields = settings.get<string>('Accounts_SearchFields').trim().split(',');

return Users.findPaginatedByActiveUsersExcept(filter, undefined, options, searchFields, [
return Users.findPaginatedByActiveUsersExcept(filter, undefined, searchFields, options, [
{
__rooms: rid,
...(status && { status }),
Expand Down
81 changes: 81 additions & 0 deletions apps/meteor/server/lib/findUsersOfRoomByHighestRole.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
import type { IUserWithRoleInfo, IUser, IRoom, UserStatus } from '@rocket.chat/core-typings';
import { Users, Subscriptions } from '@rocket.chat/models';
import type { Filter, FilterOperators, FindOptions } from 'mongodb';

import { settings } from '../../app/settings/server';

type FindUsersParam = {
rid: string;
status?: FilterOperators<UserStatus>;
skip?: number;
limit?: number;
filter?: string;
sort?: Record<string, any>;
};

export async function findUsersOfRoomByHighestRole({
rid,
status,
limit = 0,
filter = '',
sort,
}: FindUsersParam): Promise<{ members: IUserWithRoleInfo[]; total: number }> {
const options: FindOptions<IUser> = {
projection: {
name: 1,
username: 1,
nickname: 1,
status: 1,
avatarETag: 1,
_updatedAt: 1,
federated: 1,
statusConnection: 1,
},
sort: {
statusConnection: -1 as const,
...(sort || { ...(settings.get('UI_Use_Real_Name') && { name: 1 }), username: 1 }),
},
limit,
};
const extraQuery: Filter<IUser & { __rooms: IRoom['_id'][] }> = {
__rooms: rid,
...(status && { status }),
};
const searchFields = settings.get<string>('Accounts_SearchFields').trim().split(',');

// Find highest roles members (owners and moderators)
const result = await Subscriptions.findPaginatedActiveHighestRoleUsers(filter, rid, searchFields, options, extraQuery);
const {
members: highestRolesMembers = [],
totalCount: totalMembersWithRoles = { total: 0 },
ids = { allMembersIds: [] },
} = result[0] || {};
const { total: totalMembersWithRolesCount } = totalMembersWithRoles;
const { allMembersIds: highestRolesMembersIds } = ids;

if (limit <= highestRolesMembers.length) {
const totalMembersCount = await Users.countActiveUsersExcept(filter, highestRolesMembersIds, searchFields, [extraQuery]);
return { members: highestRolesMembers, total: totalMembersWithRolesCount + totalMembersCount };
}
if (options.limit) {
options.limit -= highestRolesMembers.length;
}

// Find average members
const { cursor, totalCount } = Users.findPaginatedByActiveUsersExcept(filter, highestRolesMembersIds, searchFields, options, [
extraQuery,
]);
const [members, totalMembersCount] = await Promise.all([await cursor.toArray(), totalCount]);
const membersWithHighestRoles = members.map(
(member): IUserWithRoleInfo => ({
...member,
highestRole: {
role: 'member',
level: 2,
},
}),
);

const allMembers = highestRolesMembers.concat(membersWithHighestRoles);
return { members: allMembers, total: totalMembersWithRolesCount + totalMembersCount };
}
2 changes: 1 addition & 1 deletion apps/meteor/server/methods/browseChannels.ts
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ const findUsers = async ({
};

if (workspace === 'all') {
const { cursor, totalCount } = Users.findPaginatedByActiveUsersExcept<FederatedUser>(text, [], options, searchFields);
const { cursor, totalCount } = Users.findPaginatedByActiveUsersExcept<FederatedUser>(text, [], searchFields, options);
const [results, total] = await Promise.all([cursor.toArray(), totalCount]);
return {
total,
Expand Down
91 changes: 91 additions & 0 deletions apps/meteor/server/models/raw/Subscriptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import type {
IRoom,
ISubscription,
IUser,
IUserWithRoleInfo,
RocketChatRecordDeleted,
RoomType,
SpotlightUser,
Expand Down Expand Up @@ -460,6 +461,96 @@ export class SubscriptionsRaw extends BaseRaw<ISubscription> implements ISubscri
.toArray();
}

async findPaginatedActiveHighestRoleUsers(
searchTerm: string,
rid: IRoom['_id'],
searchFields: string[],
options: FindOptions<IUser> = {},
extraQuery?: Filter<IUser>,
{ startsWith = false, endsWith = false }: { startsWith?: string | false; endsWith?: string | false } = {},
): Promise<{ members: IUserWithRoleInfo[]; totalCount: { total: number }; ids: { allMembersIds: string[] } }[]> {
const termRegex = new RegExp((startsWith ? '^' : '') + escapeRegExp(searchTerm) + (endsWith ? '$' : ''), 'i');
const orStatement = (searchFields || []).map((el) => ({ [el.trim()]: termRegex })) as { [x: string]: RegExp }[];

const limit =
options.limit !== 0
? [
{
$limit: options.limit,
},
]
: [];

return this.col
.aggregate<{ members: IUserWithRoleInfo[]; totalCount: { total: number }; ids: { allMembersIds: string[] } }>(
[
{
$match: {
rid,
roles: { $in: ['owner', 'moderator'] },
},
},
{
$lookup: {
from: 'users',
as: 'user',
let: { id: '$u._id' },
pipeline: [
{
$match: {
$expr: { $eq: ['$_id', '$$id'] },
username: { $exists: true },
active: true,
...(searchTerm && orStatement.length > 0 && { $or: orStatement }),
...extraQuery,
},
},
{ $project: options.projection },
],
},
},
{
$unwind: {
path: '$user',
},
},
{
$addFields: {
'user.highestRole': {
$cond: [{ $in: ['owner', '$roles'] }, { role: 'owner', level: 0 }, { role: 'moderator', level: 1 }],
},
},
},
{
$replaceRoot: { newRoot: '$user' },
},
{
$facet: {
members: [
{
$sort: {
'highestRole.level': 1,
...(options.sort as object),
},
},
...limit,
],
ids: [{ $group: { _id: null, allMembersIds: { $push: '$_id' } } }],
totalCount: [{ $count: 'total' }],
},
},
{
$unwind: { path: '$totalCount' },
},
{
$unwind: { path: '$ids' },
},
],
{ allowDiskUse: true },
)
.toArray();
}

incUnreadForRoomIdExcludingUserIds(roomId: IRoom['_id'], userIds: IUser['_id'][], inc: number): Promise<UpdateResult | Document> {
if (inc == null) {
inc = 1;
Expand Down
Loading
Loading