tkserver 1.6.45 → 1.7.0

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 (3) hide show
  1. package/index.js +57 -13
  2. package/mongo.js +41 -13
  3. package/package.json +2 -2
package/index.js CHANGED
@@ -34,6 +34,7 @@ const {
34
34
  getPasswordStatus,
35
35
  preCheckSpam,
36
36
  checkTurnstileCaptcha,
37
+ checkGeeTestCaptcha,
37
38
  getConfig,
38
39
  getConfigForAdmin,
39
40
  validate
@@ -274,6 +275,7 @@ async function commentGet (event) {
274
275
  const uid = event.accessToken
275
276
  const isAdminUser = isAdmin(event.accessToken)
276
277
  const limit = parseInt(config.COMMENT_PAGE_SIZE) || 8
278
+ const sort = event.sort || 'newest'
277
279
  let more = false
278
280
  let condition
279
281
  let query
@@ -294,11 +296,21 @@ async function commentGet (event) {
294
296
  // 不包含置顶
295
297
  condition.top = { $ne: true }
296
298
  query = getCommentQuery({ condition, uid, isAdminUser })
299
+
300
+ let sortOrder
301
+ if (sort === 'oldest') {
302
+ sortOrder = [['created', false]]
303
+ } else if (sort === 'popular') {
304
+ sortOrder = [['ups', true], ['created', true]]
305
+ } else {
306
+ sortOrder = [['created', true]]
307
+ }
308
+
297
309
  let main = db
298
310
  .getCollection('comment')
299
311
  .chain()
300
312
  .find(query)
301
- .compoundsort([['created', true]])
313
+ .compoundsort(sortOrder)
302
314
  // 流式分页,通过多读 1 条的方式,确认是否还有更多评论
303
315
  .limit(limit + 1)
304
316
  .data()
@@ -558,30 +570,47 @@ async function bulkSaveComments (comments) {
558
570
  .insert(comments)
559
571
  }
560
572
 
561
- // 点赞 / 取消点赞
573
+ // 点赞 / 反对 / 取消
562
574
  async function commentLike (event) {
563
575
  const res = {}
564
576
  validate(event, ['id'])
565
- res.updated = await like(event.id, event.accessToken)
577
+ const type = event.type || 'up'
578
+ res.updated = await like(event.id, event.accessToken, type)
566
579
  return res
567
580
  }
568
581
 
569
- // 点赞 / 取消点赞
570
- async function like (id, uid) {
582
+ // 点赞 / 反对 / 取消
583
+ async function like (id, uid, type) {
571
584
  const record = db
572
585
  .getCollection('comment')
573
586
  const comment = await record
574
587
  .findOne({ _id: id })
575
- let likes = comment && comment.like ? comment.like : []
576
- if (likes.findIndex((item) => item === uid) === -1) {
577
- //
578
- likes.push(uid)
579
- } else {
580
- // 取消赞
581
- likes = likes.filter((item) => item !== uid)
588
+ const commentData = comment || {}
589
+ const ups = commentData.ups || []
590
+ const downs = commentData.downs || []
591
+
592
+ let newUps = [...ups]
593
+ let newDowns = [...downs]
594
+
595
+ if (type === 'up') {
596
+ if (ups.includes(uid)) {
597
+ newUps = ups.filter((item) => item !== uid)
598
+ } else {
599
+ newUps.push(uid)
600
+ newDowns = downs.filter((item) => item !== uid)
601
+ }
602
+ } else if (type === 'down') {
603
+ if (downs.includes(uid)) {
604
+ newDowns = downs.filter((item) => item !== uid)
605
+ } else {
606
+ newDowns.push(uid)
607
+ newUps = ups.filter((item) => item !== uid)
608
+ }
582
609
  }
610
+
583
611
  await record.findAndUpdate({ _id: id }, (obj) => {
584
- obj.like = likes
612
+ obj.ups = newUps
613
+ obj.downs = newDowns
585
614
  return obj
586
615
  })
587
616
  return 1
@@ -719,6 +748,11 @@ async function limitFilter (request) {
719
748
  }
720
749
 
721
750
  async function checkCaptcha (comment, request) {
751
+ logger.log('验证码配置:', {
752
+ TURNSTILE_SITE_KEY: config.TURNSTILE_SITE_KEY,
753
+ GEETEST_CAPTCHA_ID: config.GEETEST_CAPTCHA_ID,
754
+ GEETEST_CAPTCHA_KEY: config.GEETEST_CAPTCHA_KEY ? '***' : undefined
755
+ })
722
756
  if (config.TURNSTILE_SITE_KEY && config.TURNSTILE_SECRET_KEY) {
723
757
  await checkTurnstileCaptcha({
724
758
  ip: getIp(request),
@@ -726,6 +760,16 @@ async function checkCaptcha (comment, request) {
726
760
  turnstileTokenSecretKey: config.TURNSTILE_SECRET_KEY
727
761
  })
728
762
  }
763
+ if (config.GEETEST_CAPTCHA_ID && config.GEETEST_CAPTCHA_KEY) {
764
+ await checkGeeTestCaptcha({
765
+ geeTestCaptchaId: config.GEETEST_CAPTCHA_ID,
766
+ geeTestCaptchaKey: config.GEETEST_CAPTCHA_KEY,
767
+ geeTestLotNumber: comment.geeTestLotNumber,
768
+ geeTestCaptchaOutput: comment.geeTestCaptchaOutput,
769
+ geeTestPassToken: comment.geeTestPassToken,
770
+ geeTestGenTime: comment.geeTestGenTime
771
+ })
772
+ }
729
773
  }
730
774
 
731
775
  async function saveSpamCheckResult (comment, isSpam) {
package/mongo.js CHANGED
@@ -268,6 +268,7 @@ async function commentGet (event) {
268
268
  const uid = event.accessToken
269
269
  const isAdminUser = isAdmin(event.accessToken)
270
270
  const limit = parseInt(config.COMMENT_PAGE_SIZE) || 8
271
+ const sort = event.sort || 'newest'
271
272
  let more = false
272
273
  let condition
273
274
  let query
@@ -288,10 +289,21 @@ async function commentGet (event) {
288
289
  // 不包含置顶
289
290
  condition.top = { $ne: true }
290
291
  query = getCommentQuery({ condition, uid, isAdminUser })
292
+
293
+ let orderField = 'created'
294
+ let orderDirection = -1
295
+ if (sort === 'oldest') {
296
+ orderField = 'created'
297
+ orderDirection = 1
298
+ } else if (sort === 'popular') {
299
+ orderField = 'ups'
300
+ orderDirection = -1
301
+ }
302
+
291
303
  let main = await db
292
304
  .collection('comment')
293
305
  .find(query)
294
- .sort({ created: -1 })
306
+ .sort({ [orderField]: orderDirection })
295
307
  // 流式分页,通过多读 1 条的方式,确认是否还有更多评论
296
308
  .limit(limit + 1)
297
309
  .toArray()
@@ -547,30 +559,46 @@ async function bulkSaveComments (comments) {
547
559
  return batchRes.insertedCount
548
560
  }
549
561
 
550
- // 点赞 / 取消点赞
562
+ // 点赞 / 反对 / 取消
551
563
  async function commentLike (event) {
552
564
  const res = {}
553
565
  validate(event, ['id'])
554
- res.updated = await like(event.id, event.accessToken)
566
+ const type = event.type || 'up'
567
+ res.updated = await like(event.id, event.accessToken, type)
555
568
  return res
556
569
  }
557
570
 
558
- // 点赞 / 取消点赞
559
- async function like (id, uid) {
571
+ // 点赞 / 反对 / 取消
572
+ async function like (id, uid, type) {
560
573
  const record = db
561
574
  .collection('comment')
562
575
  const comment = await record
563
576
  .findOne({ _id: id })
564
- let likes = comment && comment.like ? comment.like : []
565
- if (likes.findIndex((item) => item === uid) === -1) {
566
- //
567
- likes.push(uid)
568
- } else {
569
- // 取消赞
570
- likes = likes.filter((item) => item !== uid)
577
+ const commentData = comment || {}
578
+ const ups = commentData.ups || []
579
+ const downs = commentData.downs || []
580
+
581
+ let newUps = [...ups]
582
+ let newDowns = [...downs]
583
+
584
+ if (type === 'up') {
585
+ if (ups.includes(uid)) {
586
+ newUps = ups.filter((item) => item !== uid)
587
+ } else {
588
+ newUps.push(uid)
589
+ newDowns = downs.filter((item) => item !== uid)
590
+ }
591
+ } else if (type === 'down') {
592
+ if (downs.includes(uid)) {
593
+ newDowns = downs.filter((item) => item !== uid)
594
+ } else {
595
+ newDowns.push(uid)
596
+ newUps = ups.filter((item) => item !== uid)
597
+ }
571
598
  }
599
+
572
600
  const result = await record.updateOne({ _id: id }, {
573
- $set: { like: likes }
601
+ $set: { ups: newUps, downs: newDowns }
574
602
  })
575
603
  return result
576
604
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "tkserver",
3
- "version": "1.6.45",
3
+ "version": "1.7.0",
4
4
  "description": "A simple comment system.",
5
5
  "keywords": [
6
6
  "twikoo",
@@ -31,7 +31,7 @@
31
31
  "get-user-ip": "^1.0.1",
32
32
  "lokijs": "^1.5.12",
33
33
  "mongodb": "^6.3.0",
34
- "twikoo-func": "1.6.45",
34
+ "twikoo-func": "1.7.0",
35
35
  "uuid": "^8.3.2"
36
36
  }
37
37
  }