tkserver 1.6.2 → 1.6.5

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/index.js +22 -40
  2. package/package.json +2 -2
package/index.js CHANGED
@@ -11,7 +11,6 @@ const { v4: uuidv4 } = require('uuid') // 用户 id 生成
11
11
  const {
12
12
  $,
13
13
  JSDOM,
14
- axios,
15
14
  createDOMPurify,
16
15
  md5,
17
16
  xml2js
@@ -99,9 +98,6 @@ module.exports = async (request, response) => {
99
98
  case 'COMMENT_SUBMIT':
100
99
  res = await commentSubmit(event, request)
101
100
  break
102
- case 'POST_SUBMIT':
103
- res = await postSubmit(event.comment, request)
104
- break
105
101
  case 'COUNTER_GET':
106
102
  res = await counterGet(event)
107
103
  break
@@ -396,17 +392,17 @@ async function commentSetForAdmin (event) {
396
392
  const isAdminUser = isAdmin(event.accessToken)
397
393
  if (isAdminUser) {
398
394
  validate(event, ['id', 'set'])
399
- const data = db
395
+ db
400
396
  .getCollection('comment')
401
397
  .findAndUpdate({ _id: event.id }, (obj) => {
402
- return {
403
- ...obj,
404
- ...event.set,
405
- updated: Date.now()
398
+ for (const key of Object.keys(event.set)) {
399
+ obj[key] = event.set[key]
406
400
  }
401
+ obj.updated = Date.now()
402
+ return obj
407
403
  })
408
404
  res.code = RES_CODE.SUCCESS
409
- res.updated = data
405
+ res.updated = 1
410
406
  } else {
411
407
  res.code = RES_CODE.NEED_LOGIN
412
408
  res.message = '请先登录'
@@ -420,11 +416,11 @@ async function commentDeleteForAdmin (event) {
420
416
  const isAdminUser = isAdmin(event.accessToken)
421
417
  if (isAdminUser) {
422
418
  validate(event, ['id'])
423
- const data = db
419
+ db
424
420
  .getCollection('comment')
425
421
  .findAndRemove({ _id: event.id })
426
422
  res.code = RES_CODE.SUCCESS
427
- res.deleted = data.deletedCount
423
+ res.deleted = 1
428
424
  } else {
429
425
  res.code = RES_CODE.NEED_LOGIN
430
426
  res.message = '请先登录'
@@ -566,21 +562,9 @@ async function commentSubmit (event, request) {
566
562
  const comment = await save(data)
567
563
  res.id = comment.id
568
564
  // 异步垃圾检测、发送评论通知
569
- try {
570
- console.log('开始异步垃圾检测、发送评论通知')
571
- console.time('POST_SUBMIT')
572
- await Promise.race([
573
- axios.post(`http://${request.headers.host}`, {
574
- event: 'POST_SUBMIT',
575
- comment
576
- }, { headers: { 'x-twikoo-recursion': config.ADMIN_PASS || 'true' } }),
577
- // 如果超过 5 秒还没收到异步返回,直接继续,减少用户等待的时间
578
- new Promise((resolve) => setTimeout(resolve, 5000))
579
- ])
580
- console.timeEnd('POST_SUBMIT')
581
- } catch (e) {
582
- console.log('POST_SUBMIT 失败', e)
583
- }
565
+ console.log('开始异步垃圾检测、发送评论通知')
566
+ // 私有部署支持直接异步调用
567
+ postSubmit(comment)
584
568
  return res
585
569
  }
586
570
 
@@ -601,14 +585,17 @@ async function getParentComment (currentComment) {
601
585
  }
602
586
 
603
587
  // 异步垃圾检测、发送评论通知
604
- async function postSubmit (comment, request) {
605
- if (!isRecursion(request)) return { code: RES_CODE.FORBIDDEN }
606
- // 垃圾检测
607
- const isSpam = await postCheckSpam(comment, config)
608
- await saveSpamCheckResult(comment, isSpam)
609
- // 发送通知
610
- await sendNotice(comment, config, getParentComment)
611
- return { code: RES_CODE.SUCCESS }
588
+ async function postSubmit (comment) {
589
+ try {
590
+ console.log('POST_SUBMIT')
591
+ // 垃圾检测
592
+ const isSpam = await postCheckSpam(comment, config)
593
+ await saveSpamCheckResult(comment, isSpam)
594
+ // 发送通知
595
+ await sendNotice(comment, config, getParentComment)
596
+ } catch (e) {
597
+ console.log('POST_SUBMIT 失败', e)
598
+ }
612
599
  }
613
600
 
614
601
  // 将评论转为数据库存储格式
@@ -885,11 +872,6 @@ function isAdmin (accessToken) {
885
872
  return config.ADMIN_PASS === md5(accessToken)
886
873
  }
887
874
 
888
- // 判断是否为递归调用(即云函数调用自身)
889
- function isRecursion (request) {
890
- return request.headers['x-twikoo-recursion'] === (config.ADMIN_PASS || 'true')
891
- }
892
-
893
875
  // 建立数据库 collections
894
876
  async function createCollections () {
895
877
  const collections = ['comment', 'config', 'counter']
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "tkserver",
3
- "version": "1.6.2",
3
+ "version": "1.6.5",
4
4
  "description": "A simple comment system.",
5
5
  "keywords": [
6
6
  "twikoo",
@@ -25,7 +25,7 @@
25
25
  },
26
26
  "dependencies": {
27
27
  "lokijs": "^1.5.12",
28
- "twikoo-func": "1.6.2",
28
+ "twikoo-func": "1.6.5",
29
29
  "uuid": "^8.3.2"
30
30
  }
31
31
  }