Skip to content

Commit

Permalink
[NEW] Options to filter discussion and livechat on Admin > Rooms (#15019
Browse files Browse the repository at this point in the history
)
  • Loading branch information
ggazzo authored and sampaiodiego committed Jul 21, 2019
1 parent b758df7 commit 841a2ce
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 24 deletions.
12 changes: 6 additions & 6 deletions app/models/server/models/Rooms.js
Original file line number Diff line number Diff line change
Expand Up @@ -661,13 +661,13 @@ export class Rooms extends Base {
return this.find(query, options);
}

findByTypes(types, options) {
findByTypes(types, discussion = false, options = {}) {
const query = {
t: {
$in: types,
},
prid: { $exists: discussion },
};

return this.find(query, options);
}

Expand Down Expand Up @@ -720,10 +720,11 @@ export class Rooms extends Base {
return this.find(query, options);
}

findByNameContaining(name, options) {
findByNameContaining(name, discussion = false, options = {}) {
const nameRegex = new RegExp(s.trim(s.escapeRegExp(name)), 'i');

const query = {
prid: { $exists: discussion },
$or: [
{ name: nameRegex },
{
Expand All @@ -732,17 +733,17 @@ export class Rooms extends Base {
},
],
};

return this.find(query, options);
}

findByNameContainingAndTypes(name, types, options) {
findByNameContainingAndTypes(name, types, discussion = false, options = {}) {
const nameRegex = new RegExp(s.trim(s.escapeRegExp(name)), 'i');

const query = {
t: {
$in: types,
},
prid: { $exists: discussion },
$or: [
{ name: nameRegex },
{
Expand All @@ -751,7 +752,6 @@ export class Rooms extends Base {
},
],
};

return this.find(query, options);
}

Expand Down
6 changes: 4 additions & 2 deletions app/ui-admin/client/rooms/adminRooms.html
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
<label><input type="checkbox" name="room-type" value="c"> {{_ "Channels"}}</label>
<label><input type="checkbox" name="room-type" value="d"> {{_ "Direct_Messages"}}</label>
<label><input type="checkbox" name="room-type" value="p"> {{_ "Private_Groups"}}</label>
<label><input type="checkbox" name="room-type" value="l"> {{_ "Livechat"}}</label>
<label><input type="checkbox" name="room-type" value="dicussions"> {{_ "Discussions"}}</label>
</form>
<div class="results">
{{{_ "Showing_results" roomCount}}}
Expand All @@ -39,10 +41,10 @@
{{#each rooms}}
<tr>
<td width="30%"><div class="rc-table-wrapper">
<div class="rc-table-avatar">{{> avatar username=name roomIcon="true"}}</div>
<div class="rc-table-avatar">{{> avatar url=url roomIcon="true"}}</div>
<div class="rc-table-info">
<span class="rc-table-title">
{{>icon icon="hashtag" block="rc-table-icon"}} {{name}}
{{>icon icon=getIcon block="rc-table-icon"}} {{roomName}}
</span>
</div>
</div></td>
Expand Down
20 changes: 16 additions & 4 deletions app/ui-admin/client/rooms/adminRooms.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,15 @@ import { ChannelSettings } from '../../../channel-settings';
export const AdminChatRoom = new Mongo.Collection('rocketchat_room');

Template.adminRooms.helpers({
url() {
return roomTypes.getConfig(this.t).getAvatarPath(this);
},
getIcon() {
return roomTypes.getIcon(this);
},
roomName() {
return roomTypes.getRoomName(this.t, this);
},
searchText() {
const instance = Template.instance();
return instance.filter && instance.filter.get();
Expand Down Expand Up @@ -110,11 +119,12 @@ Template.adminRooms.onCreated(function() {
return hasAllPermission('view-room-administration');
},
});
const allowedTypes = ['c', 'd', 'p'];
this.autorun(function() {
const filter = instance.filter.get();
let types = instance.types.get();
if (types.length === 0) {
types = ['c', 'd', 'p'];
types = allowedTypes;
}
const limit = instance.limit.get();
const subscription = instance.subscribe('adminRooms', filter, types, limit);
Expand All @@ -130,13 +140,15 @@ Template.adminRooms.onCreated(function() {
types = [];
}
let query = {};
const discussion = types.includes('dicussions');
filter = s.trim(filter);
if (filter) {
const filterReg = new RegExp(s.escapeRegExp(filter), 'i');
query = { $or: [{ name: filterReg }, { t: 'd', usernames: filterReg }] };
query = { ...discussion && { prid: { $exists: true } }, $or: [{ name: filterReg }, { t: 'd', usernames: filterReg }] };
}
if (types.length) {
query.t = { $in: types };

if (types.filter((type) => type !== 'dicussions').length) {
query.t = { $in: types.filter((type) => type !== 'dicussions') };
}
const limit = instance.limit && instance.limit.get();
return AdminChatRoom.find(query, { limit, sort: { default: -1, name: 1 } });
Expand Down
29 changes: 17 additions & 12 deletions app/ui-admin/server/publications/adminRooms.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,25 @@
import { Meteor } from 'meteor/meteor';
import _ from 'underscore';
import s from 'underscore.string';

import { hasPermission } from '../../../authorization';
import { Rooms } from '../../../models';
import { Rooms } from '../../../models/server';

Meteor.publish('adminRooms', function(filter, types = [], limit) {
const showTypes = Array.isArray(types) ? types.filter((type) => type !== 'dicussions') : [];
const discussion = types.includes('dicussions');

Meteor.publish('adminRooms', function(filter, types, limit) {
if (!this.userId) {
return this.ready();
}

if (hasPermission(this.userId, 'view-room-administration') !== true) {
return this.ready();
}
if (!_.isArray(types)) {
types = [];
}

const options = {
fields: {
prid: 1,
fname: 1,
name: 1,
t: 1,
cl: 1,
Expand All @@ -40,14 +42,17 @@ Meteor.publish('adminRooms', function(filter, types, limit) {
},
};

filter = s.trim(filter);
if (filter && types.length) {
const name = s.trim(filter);

if (name && showTypes.length) {
// CACHE: can we stop using publications here?
return Rooms.findByNameContainingAndTypes(filter, types, options);
} if (types.length) {
return Rooms.findByNameContainingAndTypes(name, showTypes, discussion, options);
}

if (showTypes.length) {
// CACHE: can we stop using publications here?
return Rooms.findByTypes(types, options);
return Rooms.findByTypes(showTypes, discussion, options);
}
// CACHE: can we stop using publications here?
return Rooms.findByNameContaining(filter, options);
return Rooms.findByNameContaining(filter, discussion, options);
});

0 comments on commit 841a2ce

Please sign in to comment.