forked from RocketChat/Rocket.Chat
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmessageSearch.js
More file actions
200 lines (185 loc) · 5.03 KB
/
Copy pathmessageSearch.js
File metadata and controls
200 lines (185 loc) · 5.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
Meteor.methods({
messageSearch(text, rid, limit) {
const result = {
messages: [],
users: [],
channels: []
};
const query = {};
const options = {
sort: {
ts: -1
},
limit: limit || 20
};
check(text, String);
check(rid, String);
check(limit, Match.Optional(Number));
const currentUserId = Meteor.userId();
if (!currentUserId) {
throw new Meteor.Error('error-invalid-user', 'Invalid user', {
method: 'messageSearch'
});
}
const currentUserName = Meteor.user().username;
const currentUserTimezoneOffset = Meteor.user().utcOffset;
// I would place these methods at the bottom of the file for clarity but travis doesn't appreciate that.
// (no-use-before-define)
function filterStarred() {
query['starred._id'] = currentUserId;
return '';
}
function filterUrl() {
query['urls.0'] = {
$exists: true
};
return '';
}
function filterPinned() {
query.pinned = true;
return '';
}
function filterLocation() {
query.location = {
$exist: true
};
return '';
}
function filterBeforeDate(_, day, month, year) {
month--;
const beforeDate = new Date(year, month, day);
beforeDate.setHours(beforeDate.getUTCHours() + beforeDate.getTimezoneOffset()/60 + currentUserTimezoneOffset);
query.ts = {
$lte: beforeDate
};
return '';
}
function filterAfterDate(_, day, month, year) {
month--;
day++;
const afterDate = new Date(year, month, day);
afterDate.setUTCHours(afterDate.getUTCHours() + afterDate.getTimezoneOffset()/60 + currentUserTimezoneOffset);
if (query.ts) {
query.ts.$gte = afterDate;
} else {
query.ts = {
$gte: afterDate
};
}
return '';
}
function filterOnDate(_, day, month, year) {
month--;
const date = new Date(year, month, day);
date.setUTCHours(date.getUTCHours() + date.getTimezoneOffset()/60 + currentUserTimezoneOffset);
const dayAfter = new Date(date);
dayAfter.setDate(dayAfter.getDate() + 1);
delete query.ts;
query.ts = {
$gte: date,
$lt: dayAfter
};
return '';
}
function sortByTimestamp(_, direction) {
if (direction.startsWith('asc')) {
options.sort.ts = 1;
} else if (direction.startsWith('desc')) {
options.sort.ts = -1;
}
return '';
}
/*
text = 'from:rodrigo mention:gabriel chat'
*/
// Query for senders
const from = [];
text = text.replace(/from:([a-z0-9.-_]+)/ig, function(match, username) {
if (username === 'me' && !from.includes(currentUserName)) {
username = currentUserName;
}
from.push(username);
return '';
});
if (from.length > 0) {
query['u.username'] = {
$regex: from.join('|'),
$options: 'i'
};
}
// Query for senders
const mention = [];
text = text.replace(/mention:([a-z0-9.-_]+)/ig, function(match, username) {
mention.push(username);
return '';
});
if (mention.length > 0) {
query['mentions.username'] = {
$regex: mention.join('|'),
$options: 'i'
};
}
// Filter on messages that are starred by the current user.
text = text.replace(/has:star/g, filterStarred);
// Filter on messages that have an url.
text = text.replace(/has:url|has:link/g, filterUrl);
// Filter on pinned messages.
text = text.replace(/is:pinned|has:pin/g, filterPinned);
// Filter on messages which have a location attached.
text = text.replace(/has:location|has:map/g, filterLocation);
// Filtering before/after/on a date
// matches dd-MM-yyyy, dd/MM/yyyy, dd-MM-yyyy, prefixed by before:, after: and on: respectively.
// Example: before:15/09/2016 after: 10-08-2016
// if "on:" is set, "before:" and "after:" are ignored.
text = text.replace(/before:(\d{1,2})[\/\.-](\d{1,2})[\/\.-](\d{4})/g, filterBeforeDate);
text = text.replace(/after:(\d{1,2})[\/\.-](\d{1,2})[\/\.-](\d{4})/g, filterAfterDate);
text = text.replace(/on:(\d{1,2})[\/\.-](\d{1,2})[\/\.-](\d{4})/g, filterOnDate);
// Sort order
text = text.replace(/(?:order|sort):(asc|ascend|ascending|desc|descend|descending)/g, sortByTimestamp);
// Query in message text
text = text.trim().replace(/\s\s/g, ' ');
if (text !== '') {
if (/^\/.+\/[imxs]*$/.test(text)) {
const r = text.split('/');
query.msg = {
$regex: r[1],
$options: r[2]
};
} else if (RocketChat.settings.get('Message_AlwaysSearchRegExp')) {
query.msg = {
$regex: text,
$options: 'i'
};
} else {
query.$text = {
$search: text
};
options.fields = {
score: {
$meta: 'textScore'
}
};
}
}
if (Object.keys(query).length > 0) {
query.t = {
$ne: 'rm' //hide removed messages (useful when searching for user messages)
};
query._hidden = {
$ne: true // don't return _hidden messages
};
if (rid != null) {
query.rid = rid;
if (Meteor.call('canAccessRoom', rid, currentUserId) !== false) {
if (!RocketChat.settings.get('Message_ShowEditedStatus')) {
options.fields = {
'editedAt': 0
};
}
result.messages = RocketChat.models.Messages.find(query, options).fetch();
}
}
}
return result;
}
});