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 groups to the directory channels list #21687

Merged
merged 20 commits into from
May 21, 2021
Merged
Show file tree
Hide file tree
Changes from 14 commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
6b6fc58
Add groups to the directory channels list
matheusbsilva137 Apr 20, 2021
3fab5d8
Merge branch 'develop' into private-channels-directory
matheusbsilva137 Apr 22, 2021
1b23181
Update query to obtain private groups
matheusbsilva137 Apr 23, 2021
c1e67b9
Improve rooms query - remove unnecessary operators
matheusbsilva137 Apr 23, 2021
f2c8194
Merge branch 'develop' into private-channels-directory
KevLehman Apr 28, 2021
55c9520
Remove room's type check
matheusbsilva137 Apr 29, 2021
bb3ebc6
Merge branch 'develop' into private-channels-directory
sampaiodiego May 6, 2021
7f73092
Merge branch 'develop' into private-channels-directory
sampaiodiego May 7, 2021
fae1b98
Merge branch 'private-channels-directory' of github.com:RocketChat/Ro…
sampaiodiego May 12, 2021
2578b5d
Merge remote-tracking branch 'origin/develop' into private-channels-d…
sampaiodiego May 12, 2021
0749ffd
General improvements
sampaiodiego May 12, 2021
834abcc
Fix BelongsTo field
matheusbsilva137 May 13, 2021
cef097e
Merge branch 'develop' into private-channels-directory
matheusbsilva137 May 19, 2021
c5aca1a
Update search for teams' names: use a single teams main rooms list
matheusbsilva137 May 19, 2021
6ed1c4d
Merge branch 'develop' into private-channels-directory
sampaiodiego May 20, 2021
1f2f1bc
Merge branch 'private-channels-directory' of github.com:RocketChat/Ro…
sampaiodiego May 20, 2021
ed70cd2
Fill belongsTo field using the new getTeamsByIds method
matheusbsilva137 May 20, 2021
e2b2a3c
Replace result by cursor
matheusbsilva137 May 20, 2021
1af0618
Merge branch 'private-channels-directory' of github.com:RocketChat/Ro…
sampaiodiego May 21, 2021
d453e96
Remove duplicated method
sampaiodiego May 21, 2021
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
40 changes: 23 additions & 17 deletions app/models/server/models/Rooms.js
Original file line number Diff line number Diff line change
Expand Up @@ -535,6 +535,10 @@ export class Rooms extends Base {
return this._db.find(query, options);
}

findTeamMainRooms(options) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sry, a picky naming suggestion, since this is already on "rooms" model, I think we don't need to say we're finding rooms:

Suggested change
findTeamMainRooms(options) {
findTeamMain(options) {

if my other suggestion is accepted, this method might not be needed anymore though

return this._db.find({ teamMain: { $exists: true, $eq: true } }, options);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just a small change as both are equivalent but this is much easier to read:

Suggested change
return this._db.find({ teamMain: { $exists: true, $eq: true } }, options);
return this._db.find({ teamMain: true }, options);

}

findByNameOrFNameAndType(name, type, options) {
const query = {
t: type,
Expand All @@ -552,38 +556,40 @@ export class Rooms extends Base {
return this._db.find(query, options);
}

findByNameOrFNameAndTypeIncludingTeamRooms(name, type, teamIds, options) {
findByNameOrFNameAndRoomIdsIncludingTeamRooms(text, teamIds, roomIds, options) {
const searchTerm = text && new RegExp(text, 'i');

const query = {
t: type,
teamMain: {
$exists: false,
},
$and: [
{ teamMain: { $exists: false } },
{ prid: { $exists: false } },
{
$or: [
{
teamId: {
$exists: false,
},
t: 'c',
teamId: { $exists: false },
},
{
teamId: {
$in: teamIds,
},
t: 'c',
teamId: { $in: teamIds },
},
...roomIds?.length > 0 ? [{
_id: {
$in: roomIds,
},
}] : [],
],
},
{
...searchTerm ? [{
KevLehman marked this conversation as resolved.
Show resolved Hide resolved
$or: [{
name,
name: searchTerm,
}, {
fname: name,
fname: searchTerm,
}],
},
}] : [],
],
};

// do not use cache
return this._db.find(query, options);
}

Expand All @@ -603,7 +609,7 @@ export class Rooms extends Base {
};

if (text) {
const regex = new RegExp(s.trim(escapeRegExp(text)), 'i');
const regex = new RegExp(text, 'i');

query.$and.push({
$or: [{
Expand Down
11 changes: 6 additions & 5 deletions client/views/directory/ChannelsTable.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,16 +101,17 @@ function ChannelsTable() {
);

const channelRoute = useRoute('channel');
const groupsRoute = useRoute('group');

const { value: data = { result: [] } } = useEndpointData('directory', query);

const onClick = useMemo(
() => (name) => (e) => {
() => (name, type) => (e) => {
if (e.type === 'click' || e.key === 'Enter') {
channelRoute.push({ name });
type === 'c' ? channelRoute.push({ name }) : groupsRoute.push({ name });
}
},
[channelRoute],
[channelRoute, groupsRoute],
);

const formatDate = useFormatDate();
Expand All @@ -122,8 +123,8 @@ function ChannelsTable() {
return (
<Table.Row
key={_id}
onKeyDown={onClick(name)}
onClick={onClick(name)}
onKeyDown={onClick(name, t)}
onClick={onClick(name, t)}
tabIndex={0}
role='link'
action
Expand Down
21 changes: 15 additions & 6 deletions server/methods/browseChannels.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,24 @@ const sortUsers = function(field, direction) {
}
};

const getChannels = (user, canViewAnon, searchTerm, sort, pagination) => {
const getChannelsAndGroups = (user, canViewAnon, searchTerm, sort, pagination) => {
if ((!user && !canViewAnon) || (user && !hasPermission(user._id, 'view-c-room'))) {
return;
}

const teams = Promise.await(Team.getAllPublicTeams());
const teamIds = teams.map(({ _id }) => _id);
const teamsMains = Rooms.findTeamMainRooms({
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@KevLehman 's suggestion was great, but finding and storing all main rooms here even though none of them might be used if "result" doesn't have any team doesn't seem the best approach.

My suggestion would be:

  • filter results to get all rooms with teams
  • perform a find grabbing only the name of that rooms
  • map results to add the belongsTo field

rough example

const result = Rooms.findByNameOrFNameAndRoomIdsIncludingTeamRooms( ..... );
const results = result.fetch();

const teamIds = results.filter(({ teamId }) => teamId).map(({ teamId }) => teamId);

const teams = Rooms.findByIds(teamIds, { fields: { name: 1 } });

return results.map((room) => {
  if (room.teamId) {
    const team = teams.find((team) => team._id === room.teamId);
    if (team) room.belongsTo = team.name;
  }
});

fields: {
name: 1,
teamId: 1,
},
}).fetch();

const userTeamsIds = Promise.await(Team.listTeamsBySubscriberUserId(user._id, { projection: { teamId: 1 } }))?.map(({ teamId }) => teamId) || [];
const userRooms = user.__rooms;

const result = Rooms.findByNameOrFNameAndTypeIncludingTeamRooms(searchTerm, 'c', teamIds, {
const result = Rooms.findByNameOrFNameAndRoomIdsIncludingTeamRooms(searchTerm, [...userTeamsIds, ...teamIds], userRooms, {
...pagination,
sort: {
featured: -1,
Expand All @@ -78,7 +87,7 @@ const getChannels = (user, canViewAnon, searchTerm, sort, pagination) => {
const total = result.count(); // count ignores the `skip` and `limit` options
const results = result.fetch().map((room) => {
if (room.teamId) {
const team = teams.find((team) => team._id === room.teamId);
const team = teamsMains.find((mainRoom) => mainRoom.teamId === room.teamId);
if (team) {
room.belongsTo = team.name;
sampaiodiego marked this conversation as resolved.
Show resolved Hide resolved
}
Expand Down Expand Up @@ -202,7 +211,7 @@ const getUsers = (user, text, workspace, sort, pagination) => {

Meteor.methods({
browseChannels({ text = '', workspace = '', type = 'channels', sortBy = 'name', sortDirection = 'asc', page, offset, limit = 10 }) {
const regex = new RegExp(s.trim(escapeRegExp(text)), 'i');
const searchTerm = s.trim(escapeRegExp(text));

if (!['channels', 'users', 'teams'].includes(type) || !['asc', 'desc'].includes(sortDirection) || ((!page && page !== 0) && (!offset && offset !== 0))) {
return;
Expand Down Expand Up @@ -230,9 +239,9 @@ Meteor.methods({

switch (type) {
case 'channels':
return getChannels(user, canViewAnonymous, regex, sortChannels(sortBy, sortDirection), pagination);
return getChannelsAndGroups(user, canViewAnonymous, searchTerm, sortChannels(sortBy, sortDirection), pagination);
case 'teams':
return getTeams(user, text, sortChannels(sortBy, sortDirection), pagination);
return getTeams(user, searchTerm, sortChannels(sortBy, sortDirection), pagination);
case 'users':
return getUsers(user, text, workspace, sortUsers(sortBy, sortDirection), pagination);
default:
Expand Down
1 change: 1 addition & 0 deletions server/sdk/types/ITeamService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,4 +82,5 @@ export interface ITeamService {
getAllPublicTeams(options: FindOneOptions<ITeam>): Promise<Array<ITeam>>;
getMembersByTeamIds(teamIds: Array<string>, options: FindOneOptions<ITeamMember>): Promise<Array<ITeamMember>>;
update(uid: string, teamId: string, updateData: ITeamUpdateData): Promise<void>;
listTeamsBySubscriberUserId(uid: string, options?: FindOneOptions<ITeamMember>): Promise<Array<ITeamMember> | null>;
}