Skip to content

Commit

Permalink
RocketChat#582 Search tags
Browse files Browse the repository at this point in the history
  • Loading branch information
shedoev committed May 18, 2020
1 parent c804c4a commit 26f8cab
Showing 1 changed file with 29 additions and 2 deletions.
31 changes: 29 additions & 2 deletions server/methods/messageSearch.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Meteor } from 'meteor/meteor';
import { Match, check } from 'meteor/check';
import s from 'underscore.string';

import { Subscriptions, Messages } from '../../app/models';
import { Subscriptions, Messages, Tags } from '../../app/models';
import { settings } from '../../app/settings';

Meteor.methods({
Expand Down Expand Up @@ -38,7 +38,7 @@ Meteor.methods({
const currentUserName = user.username;
const currentUserTimezoneOffset = user.utcOffset;

const query = {};
let query = {};
const options = {
sort: {
ts: -1,
Expand Down Expand Up @@ -198,17 +198,26 @@ Meteor.methods({
// Query in message text
text = text.trim().replace(/\s\s/g, ' ');
if (text !== '') {
const queryTags = {};
if (/^\/.+\/[imxs]*$/.test(text)) {
const r = text.split('/');
query.msg = {
$regex: r[1],
$options: r[2],
};
queryTags.name = {
$regex: r[1],
$options: r[2],
};
} else if (settings.get('Message_AlwaysSearchRegExp')) {
query.msg = {
$regex: text,
$options: 'i',
};
queryTags.name = {
$regex: text,
$options: 'i',
};
} else {
query.$text = {
$search: text,
Expand All @@ -219,6 +228,24 @@ Meteor.methods({
},
};
}

if (Object.keys(queryTags).length > 0) {
const tags = Tags.find(queryTags).fetch();
if (tags.length > 0) {
const expressions = [];
expressions.push(query);

tags.forEach(tag => {
const exp = {};
exp['tags.' + tag._id] = {$exists: true};
expressions.push(exp);
});

query = {
$or: expressions,
};
}
}
}

if (Object.keys(query).length > 0) {
Expand Down

0 comments on commit 26f8cab

Please sign in to comment.