-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsearch.js
430 lines (340 loc) · 11.8 KB
/
search.js
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
var searchUserState = {
defaultState : 0,
searchEntryState : 1
}
var currentState = {}; //searchUserState.defaultState;
var buildings = require("./buildings.js").buildings;
var searchKeyboard = [ "🔎 Искать ещё", "Назад в меню" ];
var mdEscape = require('markdown-escape');
// basic settings command
bot.onText(new RegExp('^('+commands.search+'|\/search)'), function (msg, match) {
if ( msg.chat.type != 'private' ) {
return; //allow search only in private conversation
}
global.checkAuthentication(msg.from.id, function(result) {
if ( result == false ) {
return;
}
var redisUserKey = "users:"+msg.from.id;
redis.hgetall(redisUserKey, function (err, obj) {
var message = null;
if ( obj == null || obj["name"] == null || obj["house"] == null ) {
message = "Чтобы воспользоваться этой функцией, нужно сначала с помощью команды /settings настроить свой профиль и указать там свое имя и адрес.";
bot.sendMessage(msg.chat.id, message, {});
} else {
var opts = {
reply_markup: {
hide_keyboard: true,
one_time_keyboard: false
},
parse_mode: "markdown"
};
message = "Чтобы найти соседей, введите один из следующих запросов:\n\
*корпус 70* — для поиска соседей по № корпуса\n\
*лесная 5* — для поиска соседей по адресу\n\
*1258* — для поиска по ключевым словам, например номеру школы";
if ( message != null ) {
message += "\n\n_Если передумали — напишите «отмена»._";
bot.sendMessage(msg.chat.id, message, opts);
setTimeout(function () {
setCurrentState(msg.from.id, searchUserState.searchEntryState);
}, 100);
}
}
});
});
});
// search by building number
var buildingRegexp = "^корпус (\\d{1,2}[aа]*)";
bot.onText(new RegExp(buildingRegexp, 'i'), function (msg, match) {
if ( getCurrentState(msg.from.id) != searchUserState.searchEntryState ) {
return;
}
if ( msg.chat.type != 'private' ) {
return; //allow search only in private conversation
}
var bldNumber = match[1].toLowerCase().replace(/a/, "а");
var buildingInfo = buildings[bldNumber];
if ( buildingInfo != null ) {
searchNeighborsByAddress(msg.from.id, buildingInfo.street, buildingInfo.house);
}
});
// search by full address
var addressRegexp = '^(лесн|южн|север|зв[её]зд)[^\\d]+(\\d{1,2})$';
bot.onText(new RegExp(addressRegexp, 'i'), function (msg, match) {
if ( getCurrentState(msg.from.id) != searchUserState.searchEntryState ) {
return;
}
if ( msg.chat.type != 'private' ) {
return; //allow search only in private conversation
}
var strt = match[1];
var house = match[2];
var street = null;
switch (strt.toLowerCase()) {
case "лесн":
street = "Лесная";
break;
case "южн":
street = "Южная";
break;
case "север":
street = "Северный тупик";
break;
case "звёзд":
case "звезд":
street = "Звездная";
break;
}
if ( street != null ) {
searchNeighborsByAddress(msg.from.id, street, house);
}
});
// full text search
bot.onText(/^[^\/].{1,}/, function (msg, match) {
// work only in search state
if ( getCurrentState(msg.from.id) != searchUserState.searchEntryState ) { return; }
// don't capture commands
if ( match[0].indexOf("/") == 0 ) { return; }
// don't capture cancel commands
if ( match[0].match(/(отмена|назад в меню)/i) != null ) { return; }
// don't capture 'retry search' command
if ( match[0].match(new RegExp(searchKeyboard[0], 'i')) != null ) { return; }
// don't capture public messages
if ( msg.chat.type != 'private' ) { return; }
//don't catch search address requests
if (match[0].match(new RegExp(addressRegexp, 'i')) != null) { return; }
//don't catch search building requests
if (match[0].match(new RegExp(buildingRegexp, 'i')) != null) { return; }
searchNeighborsByText(msg.from.id, match[0]);
});
function searchNeighborsByText(userID, text) {
var searchOpts = {
name: text,
bio: text,
kids: text
};
findUsersWithParams(searchOpts, false, function (count, searchResultID, onlyOne) {
if ( count > 0 ) {
presentUserSearchResults(userID, count, searchResultID);
} else {
showNothingFound(userID, text);
}
});
}
function searchNeighborsByAddress(userID, street, house) {
var searchOpts = {
street: street,
house: "^"+house+"/"
};
findUsersWithParams(searchOpts, true, function (count, searchResultID) {
if ( count > 0 ) {
presentUserSearchResults(userID, count, searchResultID);
} else {
showNothingFound(userID, street+", "+house);
}
});
}
function findUsersWithParams(dict, shouldAllMatch, callback) {
redis.keys('users:*', function (err, userKeys) {
var foundUserIDs = [];
var batch = redis.batch();
var j = 0;
for ( var i = 0; i < userKeys.length; i++ ) {
var key = userKeys[i];
batch.hgetall(key, function (err, userObj) {
if ( userObj != null ) {
var passes = shouldAllMatch ? true : false;
for ( var searchKey in dict ) {
if ( userObj[searchKey] == null ) {
passes = false;
if ( shouldAllMatch ) { break; } else { continue; }
}
var match = userObj[searchKey].match(new RegExp(dict[searchKey], 'gi'));
if ( shouldAllMatch ) {
passes = passes && (match != null);
} else if ( match != null ) {
passes = true;
break;
}
}
if ( passes ) {
foundUserIDs.push(userKeys[j].substr(6)); //users:1234 -> 1234
}
}
j++; //internal concurrent counter
});
}
batch.exec(function(err, resp) {
if ( foundUserIDs.length > 0 ) {
var searchResultID = (new Date().getTime().toString()) + Math.floor(Math.random()*1000).toString();
for ( var i = 0; i < foundUserIDs.length; i++ ) {
redis.lpush("search:"+searchResultID, foundUserIDs[i]);
}
redis.expire("search:"+searchResultID, 4*60*60);
}
callback(foundUserIDs.length, searchResultID);
});
});
}
function presentUserSearchResults(toUserID, count, searchResultsID) {
if ( count == 0 ) {
return;
}
var cnt = 0;
var opts = {
parse_mode: "markdown"
};
if ( count > 1 ) {
opts["reply_markup"] = {
inline_keyboard: [ createInlineButtons(0, count, searchResultsID) ]
};
}
var firstUser = getSearchResultByID(searchResultsID, 0, function (length, userObj) {
message = createUserDescription(userObj, toUserID);
bot.sendMessage(toUserID, message, opts);
//setCurrentState(toUserID, searchUserState.defaultState);
opts["reply_markup"] = {
keyboard: [ searchKeyboard ],
resize_keyboard: true,
one_time_keyboard: true
};
bot.sendMessage(toUserID, "Вот что удалось найти, будем еще что-нибудь искать?", opts);
});
}
function showNothingFound(toUserID, searchQuery) {
var opts = {
reply_markup: {
hide_keyboard: true,
keyboard: [ searchKeyboard ],
resize_keyboard: true,
one_time_keyboard: true
},
parse_mode: "markdown"
};
message = "По запросу «"+searchQuery+"» ничего не найдено, попробуйте еще раз.\n_Если передумали — напишите «отмена»._";
bot.sendMessage(toUserID, message, opts);
}
bot.onText(new RegExp("^"+searchKeyboard[0]+"$"), function (msg, match) {
if ( msg.chat.type != 'private' ) {
return; //allow search only in private conversation
}
var opts = {
reply_markup: {
hide_keyboard: false
},
parse_mode: "markdown"
}
message = "Хорошо, попробуйте повторить свой запрос.\n_Если передумали — напишите «отмена»._";
bot.sendMessage(msg.from.id, message, opts);
setTimeout(function () {
setCurrentState(msg.from.id, searchUserState.searchEntryState);
}, 100);
});
bot.on('callback_query', function(msg) {
if ( msg.data == null || msg.data.length == 0 ) {
return;
}
if ( msg.data == "listBegins" ) {
bot.answerCallbackQuery(msg.id, "Это начало списка", true);
} else if ( msg.data == "listEnds" ) {
bot.answerCallbackQuery(msg.id, "Это конец списка", true);
} else if ( msg.data == "nop") {
bot.answerCallbackQuery(msg.id, "", false);
} else {
var matches = msg.data.match(/^search([\>\<])(\d+)\@(.+)$/);
if ( matches ) {
var cnt = parseInt(matches[2]);
var searchID = parseInt(matches[3]);
getSearchResultByID(searchID, cnt, function (length, userObj) {
if ( cnt >= 0 && length > 0 && cnt < length ) {
var newDesc = createUserDescription(userObj, msg.from.id);
var opts = {
reply_markup: {
inline_keyboard: [ createInlineButtons(cnt, length, searchID) ],
},
parse_mode: "markdown",
message_id: msg.message.message_id,
chat_id: msg.message.chat.id
};
redis.expire("search:"+searchID, 4*60*60);
bot.editMessageText(newDesc, opts);
} else {
bot.answerCallbackQuery(msg.id, "Результат этого поиска уже устарел", true);
}
});
}
}
});
function createInlineButtons(position, length, resultID) {
var backBtn = { text: "<< ", callback_data: "search<"+(position-1)+"@"+resultID };
var frwdBtn = { text: " >>", callback_data: "search>"+(position+1)+"@"+resultID };
var middle = { text: (position+1) + " из " + length, callback_data: "nop" };
if ( position == 0 ) {
backBtn.text = " ";
backBtn.callback_data = "listBegins";
}
if ( position == length-1 ) {
frwdBtn.text = " ";
frwdBtn.callback_data = "listEnds";
}
if ( length == 1 ) {
return null;
} else {
return [ backBtn, middle, frwdBtn ]
}
}
var tagsToReplace = {
'&': '&',
'<': '<',
'>': '>'
};
function replaceTag(tag) {
return tagsToReplace[tag] || tag;
}
function safe_tags_replace(str) {
var res = str.replace(/[&<>]/g, replaceTag);
res = res.replace("\\/", "/");
res = res.replace("_", "\\_");
return res;
}
function createUserDescription(user, forUserID) {
var desc = "*" + user["name"] + "*";
if ( user["house"] != null ) { desc += "\n" + user["street"] + ", " + user["house"] + "\n"; }
if ( user["username"] != null ) { desc += "\n💬 @" + mdEscape(user["username"]); }
//show tel number only to admins
if ( global.admins && global.admins.indexOf(parseInt(forUserID)) != -1 ) {
if ( user["tel"] != null ) { desc += "\n☎️ " + user["tel"]; }
}
if ( user["email"] != null ) { desc += "\n✉️ " + safe_tags_replace(user["email"]); }
if ( user["kids"] != null ) { desc += "\n👶 " + safe_tags_replace(mdEscape(user["kids"])); }
if ( user["bio"] != null ) { desc += "\n📖 " + safe_tags_replace(mdEscape(user["bio"])); }
return desc;
}
function getSearchResultByID(searchResultID, position, callback) {
var redisKey = "search:"+searchResultID;
redis.llen(redisKey, function (err, length) {
if ( position >= 0 && length > 0 && position < length ) {
redis.lindex(redisKey, position, function (err, userKey) {
redis.hgetall("users:"+userKey, function (err, userObj) {
callback(length, userObj);
});
});
} else {
callback(0, null);
}
});
}
// reset state on cancel
bot.onText(new RegExp('^(отмена|\/cancel|назад в меню)', 'i'), function (msg, match) {
setCurrentState(msg.from.id, searchUserState.defaultState);
});
function setCurrentState(id, state) {
if ( currentState[id] == undefined ) {
currentState[id] = {};
}
currentState[id] = state;
}
function getCurrentState(id) {
return currentState[id];
}