twikoo-vercel 1.6.44 → 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 (2) hide show
  1. package/api/index.js +41 -13
  2. package/package.json +2 -2
package/api/index.js CHANGED
@@ -274,6 +274,7 @@ async function commentGet (event) {
274
274
  const uid = getUid()
275
275
  const isAdminUser = isAdmin()
276
276
  const limit = parseInt(config.COMMENT_PAGE_SIZE) || 8
277
+ const sort = event.sort || 'newest'
277
278
  let more = false
278
279
  let condition
279
280
  let query
@@ -294,10 +295,21 @@ async function commentGet (event) {
294
295
  // 不包含置顶
295
296
  condition.top = { $ne: true }
296
297
  query = getCommentQuery({ condition, uid, isAdminUser })
298
+
299
+ let orderField = 'created'
300
+ let orderDirection = -1
301
+ if (sort === 'oldest') {
302
+ orderField = 'created'
303
+ orderDirection = 1
304
+ } else if (sort === 'popular') {
305
+ orderField = 'ups'
306
+ orderDirection = -1
307
+ }
308
+
297
309
  let main = await db
298
310
  .collection('comment')
299
311
  .find(query)
300
- .sort({ created: -1 })
312
+ .sort({ [orderField]: orderDirection })
301
313
  // 流式分页,通过多读 1 条的方式,确认是否还有更多评论
302
314
  .limit(limit + 1)
303
315
  .toArray()
@@ -553,30 +565,46 @@ async function bulkSaveComments (comments) {
553
565
  return batchRes.insertedCount
554
566
  }
555
567
 
556
- // 点赞 / 取消点赞
568
+ // 点赞 / 反对 / 取消
557
569
  async function commentLike (event) {
558
570
  const res = {}
559
571
  validate(event, ['id'])
560
- res.updated = await like(event.id, getUid())
572
+ const type = event.type || 'up'
573
+ res.updated = await like(event.id, getUid(), type)
561
574
  return res
562
575
  }
563
576
 
564
- // 点赞 / 取消点赞
565
- async function like (id, uid) {
577
+ // 点赞 / 反对 / 取消
578
+ async function like (id, uid, type) {
566
579
  const record = db
567
580
  .collection('comment')
568
581
  const comment = await record
569
582
  .findOne({ _id: id })
570
- let likes = comment && comment.like ? comment.like : []
571
- if (likes.findIndex((item) => item === uid) === -1) {
572
- //
573
- likes.push(uid)
574
- } else {
575
- // 取消赞
576
- likes = likes.filter((item) => item !== uid)
583
+ const commentData = comment || {}
584
+ const ups = commentData.ups || []
585
+ const downs = commentData.downs || []
586
+
587
+ let newUps = [...ups]
588
+ let newDowns = [...downs]
589
+
590
+ if (type === 'up') {
591
+ if (ups.includes(uid)) {
592
+ newUps = ups.filter((item) => item !== uid)
593
+ } else {
594
+ newUps.push(uid)
595
+ newDowns = downs.filter((item) => item !== uid)
596
+ }
597
+ } else if (type === 'down') {
598
+ if (downs.includes(uid)) {
599
+ newDowns = downs.filter((item) => item !== uid)
600
+ } else {
601
+ newDowns.push(uid)
602
+ newUps = ups.filter((item) => item !== uid)
603
+ }
577
604
  }
605
+
578
606
  const result = await record.updateOne({ _id: id }, {
579
- $set: { like: likes }
607
+ $set: { ups: newUps, downs: newDowns }
580
608
  })
581
609
  return result
582
610
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "twikoo-vercel",
3
- "version": "1.6.44",
3
+ "version": "1.7.0",
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.6.44",
16
+ "twikoo-func": "1.7.0",
17
17
  "uuid": "^8.3.2"
18
18
  }
19
19
  }