twikoo-vercel 1.7.16 → 1.7.18

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/api/index.js +78 -2
  2. package/package.json +2 -2
package/api/index.js CHANGED
@@ -290,11 +290,30 @@ async function login (password) {
290
290
  }
291
291
  }
292
292
 
293
+ function getSearchKeyword (event) {
294
+ if (event.keyword === undefined || event.keyword === null) return ''
295
+ if (typeof event.keyword !== 'string') throw new Error('搜索关键词必须是字符串')
296
+ const keyword = event.keyword.trim()
297
+ if (keyword.length > 100) throw new Error('搜索关键词不能超过 100 个字符')
298
+ return keyword
299
+ }
300
+
301
+ function commentMatchesKeyword (comment, keyword) {
302
+ return [comment.nick, comment.comment].some(value =>
303
+ typeof value === 'string' && value.toLowerCase().includes(keyword)
304
+ )
305
+ }
306
+
307
+ function escapeRegExp (value) {
308
+ return value.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')
309
+ }
310
+
293
311
  // 读取评论
294
312
  async function commentGet (event, accessToken) {
295
313
  const res = {}
296
314
  try {
297
315
  validate(event, ['url'])
316
+ if (getSearchKeyword(event)) return commentSearch(event, accessToken)
298
317
  const uid = accessToken
299
318
  const isAdminUser = isAdmin(accessToken)
300
319
  const limit = parseInt(config.COMMENT_PAGE_SIZE) || 8
@@ -380,6 +399,62 @@ async function commentGet (event, accessToken) {
380
399
  return res
381
400
  }
382
401
 
402
+ async function commentSearch (event, accessToken) {
403
+ const res = {}
404
+ try {
405
+ validate(event, ['url'])
406
+ const keyword = getSearchKeyword(event).toLowerCase()
407
+ const page = Math.max(parseInt(event.page) || 1, 1)
408
+ const uid = accessToken
409
+ const isAdminUser = isAdmin(accessToken)
410
+ const limit = parseInt(config.COMMENT_PAGE_SIZE) || 8
411
+ const sort = event.sort || 'newest'
412
+ let more = false
413
+
414
+ const condition = { url: { $in: getUrlQuery(event.url) } }
415
+ const query = getCommentQuery({ condition, uid, isAdminUser })
416
+ const searchComments = await db.collection('comment').find(query).toArray()
417
+ const matchedRoots = new Set(searchComments
418
+ .filter(comment => commentMatchesKeyword(comment, keyword))
419
+ .map(comment => String(comment.rid || comment._id)))
420
+ let main = searchComments.filter(comment =>
421
+ (!comment.rid || comment.rid === '') && matchedRoots.has(String(comment._id))
422
+ )
423
+ const count = main.length
424
+
425
+ main.sort((a, b) => {
426
+ if (sort === 'oldest') return a.created - b.created
427
+ if (sort === 'popular') {
428
+ const ups = (b.ups || []).length - (a.ups || []).length
429
+ if (ups) return ups
430
+ }
431
+ return b.created - a.created
432
+ })
433
+ let top = []
434
+ if (!config.TOP_DISABLED) {
435
+ if (page === 1) top = main.filter(comment => comment.top === true)
436
+ main = main.filter(comment => comment.top !== true)
437
+ }
438
+ main = main.slice((page - 1) * limit, page * limit + 1)
439
+
440
+ if (main.length > limit) {
441
+ more = true
442
+ main = main.slice(0, limit)
443
+ }
444
+ main = [...top, ...main]
445
+
446
+ const mainIds = new Set(main.map(comment => comment._id.toString()))
447
+ const reply = searchComments.filter(comment => mainIds.has(String(comment.rid)))
448
+ res.data = parseComment([...main, ...reply], uid, config)
449
+ res.more = more
450
+ res.count = count
451
+ } catch (e) {
452
+ res.data = []
453
+ res.message = e.message
454
+ }
455
+ return res
456
+ }
457
+
383
458
  function getCommentQuery ({ condition, uid, isAdminUser }) {
384
459
  return {
385
460
  $or: [
@@ -427,9 +502,10 @@ function getCommentSearchCondition (event) {
427
502
  break
428
503
  }
429
504
  }
430
- if (event.keyword) {
505
+ const keyword = getSearchKeyword(event)
506
+ if (keyword) {
431
507
  const regExp = {
432
- $regex: event.keyword,
508
+ $regex: escapeRegExp(keyword),
433
509
  $options: 'i'
434
510
  }
435
511
  condition = {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "twikoo-vercel",
3
- "version": "1.7.16",
3
+ "version": "1.7.18",
4
4
  "description": "A simple comment system.",
5
5
  "author": "imaegoo <hello@imaegoo.com> (https://github.com/imaegoo)",
6
6
  "license": "MIT",
@@ -13,7 +13,7 @@
13
13
  "dependencies": {
14
14
  "get-user-ip": "^1.0.1",
15
15
  "mongodb": "^6.3.0",
16
- "twikoo-func": "1.7.16",
16
+ "twikoo-func": "1.7.18",
17
17
  "uuid": "^8.3.2"
18
18
  }
19
19
  }