tkserver 1.6.45 → 1.7.1

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 +88 -14
  2. package/mongo.js +53 -14
  3. package/package.json +2 -2
package/index.js CHANGED
@@ -31,9 +31,11 @@ const {
31
31
  isQQ,
32
32
  addQQMailSuffix,
33
33
  getQQAvatar,
34
+ getQQNick,
34
35
  getPasswordStatus,
35
36
  preCheckSpam,
36
37
  checkTurnstileCaptcha,
38
+ checkGeeTestCaptcha,
37
39
  getConfig,
38
40
  getConfigForAdmin,
39
41
  validate
@@ -142,6 +144,9 @@ module.exports = async (request, response) => {
142
144
  case 'UPLOAD_IMAGE': // >= 1.5.0
143
145
  res = await uploadImage(event, config)
144
146
  break
147
+ case 'GET_QQ_NICK': // >= 1.7.0
148
+ res = await qqNickGet(event)
149
+ break
145
150
  case 'COMMENT_EXPORT_FOR_ADMIN': // >= 1.6.13
146
151
  res = await commentExportForAdmin(event)
147
152
  break
@@ -274,6 +279,7 @@ async function commentGet (event) {
274
279
  const uid = event.accessToken
275
280
  const isAdminUser = isAdmin(event.accessToken)
276
281
  const limit = parseInt(config.COMMENT_PAGE_SIZE) || 8
282
+ const sort = event.sort || 'newest'
277
283
  let more = false
278
284
  let condition
279
285
  let query
@@ -294,11 +300,21 @@ async function commentGet (event) {
294
300
  // 不包含置顶
295
301
  condition.top = { $ne: true }
296
302
  query = getCommentQuery({ condition, uid, isAdminUser })
303
+
304
+ let sortOrder
305
+ if (sort === 'oldest') {
306
+ sortOrder = [['created', false]]
307
+ } else if (sort === 'popular') {
308
+ sortOrder = [['ups', true], ['created', true]]
309
+ } else {
310
+ sortOrder = [['created', true]]
311
+ }
312
+
297
313
  let main = db
298
314
  .getCollection('comment')
299
315
  .chain()
300
316
  .find(query)
301
- .compoundsort([['created', true]])
317
+ .compoundsort(sortOrder)
302
318
  // 流式分页,通过多读 1 条的方式,确认是否还有更多评论
303
319
  .limit(limit + 1)
304
320
  .data()
@@ -558,30 +574,47 @@ async function bulkSaveComments (comments) {
558
574
  .insert(comments)
559
575
  }
560
576
 
561
- // 点赞 / 取消点赞
577
+ // 点赞 / 反对 / 取消
562
578
  async function commentLike (event) {
563
579
  const res = {}
564
580
  validate(event, ['id'])
565
- res.updated = await like(event.id, event.accessToken)
581
+ const type = event.type || 'up'
582
+ res.updated = await like(event.id, event.accessToken, type)
566
583
  return res
567
584
  }
568
585
 
569
- // 点赞 / 取消点赞
570
- async function like (id, uid) {
586
+ // 点赞 / 反对 / 取消
587
+ async function like (id, uid, type) {
571
588
  const record = db
572
589
  .getCollection('comment')
573
590
  const comment = await record
574
591
  .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)
592
+ const commentData = comment || {}
593
+ const ups = commentData.ups || []
594
+ const downs = commentData.downs || []
595
+
596
+ let newUps = [...ups]
597
+ let newDowns = [...downs]
598
+
599
+ if (type === 'up') {
600
+ if (ups.includes(uid)) {
601
+ newUps = ups.filter((item) => item !== uid)
602
+ } else {
603
+ newUps.push(uid)
604
+ newDowns = downs.filter((item) => item !== uid)
605
+ }
606
+ } else if (type === 'down') {
607
+ if (downs.includes(uid)) {
608
+ newDowns = downs.filter((item) => item !== uid)
609
+ } else {
610
+ newDowns.push(uid)
611
+ newUps = ups.filter((item) => item !== uid)
612
+ }
582
613
  }
614
+
583
615
  await record.findAndUpdate({ _id: id }, (obj) => {
584
- obj.like = likes
616
+ obj.ups = newUps
617
+ obj.downs = newDowns
585
618
  return obj
586
619
  })
587
620
  return 1
@@ -719,7 +752,33 @@ async function limitFilter (request) {
719
752
  }
720
753
 
721
754
  async function checkCaptcha (comment, request) {
722
- if (config.TURNSTILE_SITE_KEY && config.TURNSTILE_SECRET_KEY) {
755
+ logger.log('验证码配置:', {
756
+ CAPTCHA_PROVIDER: config.CAPTCHA_PROVIDER,
757
+ TURNSTILE_SITE_KEY: config.TURNSTILE_SITE_KEY,
758
+ GEETEST_CAPTCHA_ID: config.GEETEST_CAPTCHA_ID,
759
+ GEETEST_CAPTCHA_KEY: config.GEETEST_CAPTCHA_KEY ? '***' : undefined
760
+ })
761
+ const provider = config.CAPTCHA_PROVIDER
762
+ if ((!provider || provider === 'Turnstile') && config.TURNSTILE_SITE_KEY && config.TURNSTILE_SECRET_KEY) {
763
+ await checkTurnstileCaptcha({
764
+ ip: getIp(request),
765
+ turnstileToken: comment.turnstileToken,
766
+ turnstileTokenSecretKey: config.TURNSTILE_SECRET_KEY
767
+ })
768
+ }
769
+ if ((!provider || provider === 'Geetest') && config.GEETEST_CAPTCHA_ID && config.GEETEST_CAPTCHA_KEY) {
770
+ await checkGeeTestCaptcha({
771
+ geeTestCaptchaId: config.GEETEST_CAPTCHA_ID,
772
+ geeTestCaptchaKey: config.GEETEST_CAPTCHA_KEY,
773
+ geeTestLotNumber: comment.geeTestLotNumber,
774
+ geeTestCaptchaOutput: comment.geeTestCaptchaOutput,
775
+ geeTestPassToken: comment.geeTestPassToken,
776
+ geeTestGenTime: comment.geeTestGenTime
777
+ })
778
+ } else if (config.TURNSTILE_SITE_KEY) {
779
+ if (!config.TURNSTILE_SECRET_KEY) {
780
+ throw new Error('Turnstile 验证码配置不完整,缺少 TURNSTILE_SECRET_KEY')
781
+ }
723
782
  await checkTurnstileCaptcha({
724
783
  ip: getIp(request),
725
784
  turnstileToken: comment.turnstileToken,
@@ -864,6 +923,21 @@ async function getRecentComments (event) {
864
923
  return res
865
924
  }
866
925
 
926
+ // 获取 QQ 昵称
927
+ async function qqNickGet (event) {
928
+ const res = {}
929
+ try {
930
+ validate(event, ['qq'])
931
+ const nick = await getQQNick(event.qq, config.QQ_API_KEY)
932
+ res.code = RES_CODE.SUCCESS
933
+ res.nick = nick
934
+ } catch (e) {
935
+ res.code = RES_CODE.FAIL
936
+ res.message = e.message
937
+ }
938
+ return res
939
+ }
940
+
867
941
  // 修改配置
868
942
  async function setConfig (event) {
869
943
  const isAdminUser = isAdmin(event.accessToken)
package/mongo.js CHANGED
@@ -32,6 +32,7 @@ const {
32
32
  getPasswordStatus,
33
33
  preCheckSpam,
34
34
  checkTurnstileCaptcha,
35
+ checkGeeTestCaptcha,
35
36
  getConfig,
36
37
  getConfigForAdmin,
37
38
  validate
@@ -268,6 +269,7 @@ async function commentGet (event) {
268
269
  const uid = event.accessToken
269
270
  const isAdminUser = isAdmin(event.accessToken)
270
271
  const limit = parseInt(config.COMMENT_PAGE_SIZE) || 8
272
+ const sort = event.sort || 'newest'
271
273
  let more = false
272
274
  let condition
273
275
  let query
@@ -288,10 +290,21 @@ async function commentGet (event) {
288
290
  // 不包含置顶
289
291
  condition.top = { $ne: true }
290
292
  query = getCommentQuery({ condition, uid, isAdminUser })
293
+
294
+ let orderField = 'created'
295
+ let orderDirection = -1
296
+ if (sort === 'oldest') {
297
+ orderField = 'created'
298
+ orderDirection = 1
299
+ } else if (sort === 'popular') {
300
+ orderField = 'ups'
301
+ orderDirection = -1
302
+ }
303
+
291
304
  let main = await db
292
305
  .collection('comment')
293
306
  .find(query)
294
- .sort({ created: -1 })
307
+ .sort({ [orderField]: orderDirection })
295
308
  // 流式分页,通过多读 1 条的方式,确认是否还有更多评论
296
309
  .limit(limit + 1)
297
310
  .toArray()
@@ -547,30 +560,46 @@ async function bulkSaveComments (comments) {
547
560
  return batchRes.insertedCount
548
561
  }
549
562
 
550
- // 点赞 / 取消点赞
563
+ // 点赞 / 反对 / 取消
551
564
  async function commentLike (event) {
552
565
  const res = {}
553
566
  validate(event, ['id'])
554
- res.updated = await like(event.id, event.accessToken)
567
+ const type = event.type || 'up'
568
+ res.updated = await like(event.id, event.accessToken, type)
555
569
  return res
556
570
  }
557
571
 
558
- // 点赞 / 取消点赞
559
- async function like (id, uid) {
572
+ // 点赞 / 反对 / 取消
573
+ async function like (id, uid, type) {
560
574
  const record = db
561
575
  .collection('comment')
562
576
  const comment = await record
563
577
  .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)
578
+ const commentData = comment || {}
579
+ const ups = commentData.ups || []
580
+ const downs = commentData.downs || []
581
+
582
+ let newUps = [...ups]
583
+ let newDowns = [...downs]
584
+
585
+ if (type === 'up') {
586
+ if (ups.includes(uid)) {
587
+ newUps = ups.filter((item) => item !== uid)
588
+ } else {
589
+ newUps.push(uid)
590
+ newDowns = downs.filter((item) => item !== uid)
591
+ }
592
+ } else if (type === 'down') {
593
+ if (downs.includes(uid)) {
594
+ newDowns = downs.filter((item) => item !== uid)
595
+ } else {
596
+ newDowns.push(uid)
597
+ newUps = ups.filter((item) => item !== uid)
598
+ }
571
599
  }
600
+
572
601
  const result = await record.updateOne({ _id: id }, {
573
- $set: { like: likes }
602
+ $set: { ups: newUps, downs: newDowns }
574
603
  })
575
604
  return result
576
605
  }
@@ -707,12 +736,22 @@ async function limitFilter (request) {
707
736
  }
708
737
 
709
738
  async function checkCaptcha (comment, request) {
710
- if (config.TURNSTILE_SITE_KEY && config.TURNSTILE_SECRET_KEY) {
739
+ const provider = config.CAPTCHA_PROVIDER
740
+ if ((!provider || provider === 'Turnstile') && config.TURNSTILE_SITE_KEY && config.TURNSTILE_SECRET_KEY) {
711
741
  await checkTurnstileCaptcha({
712
742
  ip: getIp(request),
713
743
  turnstileToken: comment.turnstileToken,
714
744
  turnstileTokenSecretKey: config.TURNSTILE_SECRET_KEY
715
745
  })
746
+ } else if (provider === 'Geetest' && config.GEETEST_CAPTCHA_ID && config.GEETEST_CAPTCHA_KEY) {
747
+ await checkGeeTestCaptcha({
748
+ geeTestCaptchaId: config.GEETEST_CAPTCHA_ID,
749
+ geeTestCaptchaKey: config.GEETEST_CAPTCHA_KEY,
750
+ geeTestLotNumber: comment.geeTestLotNumber,
751
+ geeTestCaptchaOutput: comment.geeTestCaptchaOutput,
752
+ geeTestPassToken: comment.geeTestPassToken,
753
+ geeTestGenTime: comment.geeTestGenTime
754
+ })
716
755
  }
717
756
  }
718
757
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "tkserver",
3
- "version": "1.6.45",
3
+ "version": "1.7.1",
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.1",
35
35
  "uuid": "^8.3.2"
36
36
  }
37
37
  }