twikoo-func 1.7.7 → 1.7.9

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 +24 -1
  2. package/package.json +1 -1
  3. package/utils/index.js +13 -0
package/index.js CHANGED
@@ -34,7 +34,8 @@ const {
34
34
  checkCapCaptcha,
35
35
  getConfig,
36
36
  getConfigForAdmin,
37
- validate
37
+ validate,
38
+ checkCommentOwnership
38
39
  } = require('./utils')
39
40
  const {
40
41
  jsonParse,
@@ -146,6 +147,9 @@ exports.main = async (event, context) => {
146
147
  case 'COMMENT_EXPORT_FOR_ADMIN': // >= 1.6.13
147
148
  res = await commentExportForAdmin(event)
148
149
  break
150
+ case 'COMMENT_DELETE_FOR_USER':
151
+ res = await commentDeleteForUser(event)
152
+ break
149
153
  default:
150
154
  if (event.event) {
151
155
  res.code = RES_CODE.EVENT_NOT_EXIST
@@ -428,6 +432,25 @@ async function commentDeleteForAdmin (event) {
428
432
  return res
429
433
  }
430
434
 
435
+ // 用户删除自己的评论
436
+ async function commentDeleteForUser (event) {
437
+ const res = {}
438
+ try {
439
+ const uid = await getUid()
440
+ await checkCommentOwnership(event.id, uid, async (id) => {
441
+ const doc = await db.collection('comment').doc(id).get()
442
+ return doc.data && doc.data.length > 0 ? doc.data[0] : null
443
+ })
444
+ const data = await db.collection('comment').doc(event.id).delete()
445
+ res.code = RES_CODE.SUCCESS
446
+ res.deleted = data.deleted
447
+ } catch (e) {
448
+ res.code = RES_CODE.FAIL
449
+ res.message = e.message
450
+ }
451
+ return res
452
+ }
453
+
431
454
  // 管理员导入评论
432
455
  async function commentImportForAdmin (event) {
433
456
  const res = {}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "twikoo-func",
3
- "version": "1.7.7",
3
+ "version": "1.7.9",
4
4
  "description": "A simple comment system.",
5
5
  "author": "imaegoo <hello@imaegoo.com> (https://github.com/imaegoo)",
6
6
  "license": "MIT",
package/utils/index.js CHANGED
@@ -99,6 +99,7 @@ const fn = {
99
99
  ruser: fn.ruser(comment.pid, comments),
100
100
  top: comment.top,
101
101
  isSpam: comment.isSpam,
102
+ isOwner: comment.uid === uid,
102
103
  created: comment.created,
103
104
  updated: comment.updated
104
105
  }
@@ -445,6 +446,18 @@ const fn = {
445
446
  throw new Error(`参数"${requiredParam}"不合法`)
446
447
  }
447
448
  }
449
+ },
450
+ // 校验评论归属:确认评论存在且属于当前用户
451
+ async checkCommentOwnership (id, uid, getComment) {
452
+ fn.validate({ id }, ['id'])
453
+ const comment = await getComment(id)
454
+ if (!comment) {
455
+ throw new Error('评论不存在')
456
+ }
457
+ if (comment.uid !== uid) {
458
+ throw new Error('只能删除自己的评论')
459
+ }
460
+ return comment
448
461
  }
449
462
  }
450
463