Skip to content

Commit 26f8cab

Browse files
committed
RocketChat#582 Search tags
1 parent c804c4a commit 26f8cab

File tree

1 file changed

+29
-2
lines changed

1 file changed

+29
-2
lines changed

server/methods/messageSearch.js

+29-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { Meteor } from 'meteor/meteor';
22
import { Match, check } from 'meteor/check';
33
import s from 'underscore.string';
44

5-
import { Subscriptions, Messages } from '../../app/models';
5+
import { Subscriptions, Messages, Tags } from '../../app/models';
66
import { settings } from '../../app/settings';
77

88
Meteor.methods({
@@ -38,7 +38,7 @@ Meteor.methods({
3838
const currentUserName = user.username;
3939
const currentUserTimezoneOffset = user.utcOffset;
4040

41-
const query = {};
41+
let query = {};
4242
const options = {
4343
sort: {
4444
ts: -1,
@@ -198,17 +198,26 @@ Meteor.methods({
198198
// Query in message text
199199
text = text.trim().replace(/\s\s/g, ' ');
200200
if (text !== '') {
201+
const queryTags = {};
201202
if (/^\/.+\/[imxs]*$/.test(text)) {
202203
const r = text.split('/');
203204
query.msg = {
204205
$regex: r[1],
205206
$options: r[2],
206207
};
208+
queryTags.name = {
209+
$regex: r[1],
210+
$options: r[2],
211+
};
207212
} else if (settings.get('Message_AlwaysSearchRegExp')) {
208213
query.msg = {
209214
$regex: text,
210215
$options: 'i',
211216
};
217+
queryTags.name = {
218+
$regex: text,
219+
$options: 'i',
220+
};
212221
} else {
213222
query.$text = {
214223
$search: text,
@@ -219,6 +228,24 @@ Meteor.methods({
219228
},
220229
};
221230
}
231+
232+
if (Object.keys(queryTags).length > 0) {
233+
const tags = Tags.find(queryTags).fetch();
234+
if (tags.length > 0) {
235+
const expressions = [];
236+
expressions.push(query);
237+
238+
tags.forEach(tag => {
239+
const exp = {};
240+
exp['tags.' + tag._id] = {$exists: true};
241+
expressions.push(exp);
242+
});
243+
244+
query = {
245+
$or: expressions,
246+
};
247+
}
248+
}
222249
}
223250

224251
if (Object.keys(query).length > 0) {

0 commit comments

Comments
 (0)