twikoo-vercel 1.7.15 → 1.7.16

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 +43 -49
  2. package/package.json +2 -2
package/api/index.js CHANGED
@@ -75,10 +75,10 @@ const { RES_CODE, MAX_REQUEST_TIMES } = require('twikoo-func/utils/constants')
75
75
  // 全局变量 / variables
76
76
  let db = null
77
77
  let config
78
- let accessToken
79
78
  const requestTimes = {}
80
79
 
81
80
  module.exports = async (request, response) => {
81
+ let accessToken
82
82
  const event = request.body || {}
83
83
  logger.log('请求 IP:', getIp(request))
84
84
  logger.log('请求函数:', event.event)
@@ -99,28 +99,28 @@ module.exports = async (request, response) => {
99
99
  res = getFuncVersion({ VERSION })
100
100
  break
101
101
  case 'COMMENT_GET':
102
- res = await commentGet(event)
102
+ res = await commentGet(event, accessToken)
103
103
  break
104
104
  case 'COMMENT_GET_FOR_ADMIN':
105
- res = await commentGetForAdmin(event)
105
+ res = await commentGetForAdmin(event, accessToken)
106
106
  break
107
107
  case 'COMMENT_SET_FOR_ADMIN':
108
- res = await commentSetForAdmin(event)
108
+ res = await commentSetForAdmin(event, accessToken)
109
109
  break
110
110
  case 'COMMENT_DELETE_FOR_ADMIN':
111
- res = await commentDeleteForAdmin(event)
111
+ res = await commentDeleteForAdmin(event, accessToken)
112
112
  break
113
113
  case 'COMMENT_DELETE_FOR_USER':
114
- res = await commentDeleteForUser(event)
114
+ res = await commentDeleteForUser(event, accessToken)
115
115
  break
116
116
  case 'COMMENT_IMPORT_FOR_ADMIN':
117
- res = await commentImportForAdmin(event)
117
+ res = await commentImportForAdmin(event, accessToken)
118
118
  break
119
119
  case 'COMMENT_LIKE':
120
- res = await commentLike(event)
120
+ res = await commentLike(event, accessToken)
121
121
  break
122
122
  case 'COMMENT_SUBMIT':
123
- res = await commentSubmit(event, request)
123
+ res = await commentSubmit(event, request, accessToken)
124
124
  break
125
125
  case 'POST_SUBMIT':
126
126
  res = await postSubmit(event.comment, request)
@@ -132,16 +132,16 @@ module.exports = async (request, response) => {
132
132
  res = await getPasswordStatus(config, VERSION)
133
133
  break
134
134
  case 'SET_PASSWORD':
135
- res = await setPassword(event)
135
+ res = await setPassword(event, accessToken)
136
136
  break
137
137
  case 'GET_CONFIG':
138
- res = await getConfig({ config, VERSION, isAdmin: isAdmin() })
138
+ res = await getConfig({ config, VERSION, isAdmin: isAdmin(accessToken) })
139
139
  break
140
140
  case 'GET_CONFIG_FOR_ADMIN':
141
- res = await getConfigForAdmin({ config, isAdmin: isAdmin() })
141
+ res = await getConfigForAdmin({ config, isAdmin: isAdmin(accessToken) })
142
142
  break
143
143
  case 'SET_CONFIG':
144
- res = await setConfig(event)
144
+ res = await setConfig(event, accessToken)
145
145
  break
146
146
  case 'LOGIN':
147
147
  res = await login(event.password)
@@ -153,7 +153,7 @@ module.exports = async (request, response) => {
153
153
  res = await getRecentComments(event)
154
154
  break
155
155
  case 'EMAIL_TEST': // >= 1.4.6
156
- res = await emailTest(event, config, isAdmin())
156
+ res = await emailTest(event, config, isAdmin(accessToken))
157
157
  break
158
158
  case 'UPLOAD_IMAGE': // >= 1.5.0
159
159
  res = await uploadImage(event, config)
@@ -162,7 +162,7 @@ module.exports = async (request, response) => {
162
162
  res = await qqNickGet(event)
163
163
  break
164
164
  case 'COMMENT_EXPORT_FOR_ADMIN': // >= 1.6.13
165
- res = await commentExportForAdmin(event)
165
+ res = await commentExportForAdmin(event, accessToken)
166
166
  break
167
167
  case 'CAP_CHALLENGE':
168
168
  res = await capChallenge()
@@ -260,8 +260,8 @@ async function connectToDatabase (uri) {
260
260
  }
261
261
 
262
262
  // 写入管理密码
263
- async function setPassword (event) {
264
- const isAdminUser = isAdmin()
263
+ async function setPassword (event, accessToken) {
264
+ const isAdminUser = isAdmin(accessToken)
265
265
  // 如果数据库里没有密码,则写入密码
266
266
  // 如果数据库里有密码,则只有管理员可以写入密码
267
267
  if (config.ADMIN_PASS && !isAdminUser) {
@@ -291,12 +291,12 @@ async function login (password) {
291
291
  }
292
292
 
293
293
  // 读取评论
294
- async function commentGet (event) {
294
+ async function commentGet (event, accessToken) {
295
295
  const res = {}
296
296
  try {
297
297
  validate(event, ['url'])
298
- const uid = getUid()
299
- const isAdminUser = isAdmin()
298
+ const uid = accessToken
299
+ const isAdminUser = isAdmin(accessToken)
300
300
  const limit = parseInt(config.COMMENT_PAGE_SIZE) || 8
301
301
  const sort = event.sort || 'newest'
302
302
  let more = false
@@ -390,9 +390,9 @@ function getCommentQuery ({ condition, uid, isAdminUser }) {
390
390
  }
391
391
 
392
392
  // 管理员读取评论
393
- async function commentGetForAdmin (event) {
393
+ async function commentGetForAdmin (event, accessToken) {
394
394
  const res = {}
395
- const isAdminUser = isAdmin()
395
+ const isAdminUser = isAdmin(accessToken)
396
396
  if (isAdminUser) {
397
397
  validate(event, ['per', 'page'])
398
398
  const collection = db
@@ -448,9 +448,9 @@ function getCommentSearchCondition (event) {
448
448
  }
449
449
 
450
450
  // 管理员修改评论
451
- async function commentSetForAdmin (event) {
451
+ async function commentSetForAdmin (event, accessToken) {
452
452
  const res = {}
453
- const isAdminUser = isAdmin()
453
+ const isAdminUser = isAdmin(accessToken)
454
454
  if (isAdminUser) {
455
455
  validate(event, ['id', 'set'])
456
456
  const data = await db
@@ -471,9 +471,9 @@ async function commentSetForAdmin (event) {
471
471
  }
472
472
 
473
473
  // 管理员删除评论
474
- async function commentDeleteForAdmin (event) {
474
+ async function commentDeleteForAdmin (event, accessToken) {
475
475
  const res = {}
476
- const isAdminUser = isAdmin()
476
+ const isAdminUser = isAdmin(accessToken)
477
477
  if (isAdminUser) {
478
478
  validate(event, ['id'])
479
479
  const data = await db
@@ -489,10 +489,10 @@ async function commentDeleteForAdmin (event) {
489
489
  }
490
490
 
491
491
  // 用户删除自己的评论
492
- async function commentDeleteForUser (event) {
492
+ async function commentDeleteForUser (event, accessToken) {
493
493
  const res = {}
494
494
  try {
495
- const uid = getUid()
495
+ const uid = accessToken
496
496
  await checkCommentOwnership(event.id, uid, async (id) => {
497
497
  return db.collection('comment').findOne({ _id: id })
498
498
  })
@@ -507,13 +507,13 @@ async function commentDeleteForUser (event) {
507
507
  }
508
508
 
509
509
  // 管理员导入评论
510
- async function commentImportForAdmin (event) {
510
+ async function commentImportForAdmin (event, accessToken) {
511
511
  const res = {}
512
512
  let logText = ''
513
513
  const log = (message) => {
514
514
  logText += `${new Date().toLocaleString()} ${message}\n`
515
515
  }
516
- const isAdminUser = isAdmin()
516
+ const isAdminUser = isAdmin(accessToken)
517
517
  if (isAdminUser) {
518
518
  try {
519
519
  validate(event, ['source', 'file'])
@@ -563,9 +563,9 @@ async function commentImportForAdmin (event) {
563
563
  return res
564
564
  }
565
565
 
566
- async function commentExportForAdmin (event) {
566
+ async function commentExportForAdmin (event, accessToken) {
567
567
  const res = {}
568
- const isAdminUser = isAdmin()
568
+ const isAdminUser = isAdmin(accessToken)
569
569
  if (isAdminUser) {
570
570
  const collection = event.collection || 'comment'
571
571
  const data = await db
@@ -608,11 +608,11 @@ async function bulkSaveComments (comments) {
608
608
  }
609
609
 
610
610
  // 点赞 / 反对 / 取消
611
- async function commentLike (event) {
611
+ async function commentLike (event, accessToken) {
612
612
  const res = {}
613
613
  validate(event, ['id'])
614
614
  const type = event.type || 'up'
615
- res.updated = await like(event.id, getUid(), type)
615
+ res.updated = await like(event.id, accessToken, type)
616
616
  return res
617
617
  }
618
618
 
@@ -667,7 +667,7 @@ async function like (id, uid, type) {
667
667
  * @param {String} event.pid 回复的 ID
668
668
  * @param {String} event.rid 评论楼 ID
669
669
  */
670
- async function commentSubmit (event, request) {
670
+ async function commentSubmit (event, request, accessToken) {
671
671
  const res = {}
672
672
  // 参数校验
673
673
  validate(event, ['url', 'ua', 'comment'])
@@ -676,7 +676,7 @@ async function commentSubmit (event, request) {
676
676
  // 验证码
677
677
  await checkCaptcha(event, request)
678
678
  // 预检测、转换
679
- const data = await parse(event, request)
679
+ const data = await parse(event, request, accessToken)
680
680
  // 保存
681
681
  const comment = await save(data)
682
682
  res.id = comment.id
@@ -727,16 +727,16 @@ async function postSubmit (comment, request) {
727
727
  }
728
728
 
729
729
  // 将评论转为数据库存储格式
730
- async function parse (comment, request) {
730
+ async function parse (comment, request, accessToken) {
731
731
  const timestamp = Date.now()
732
- const isAdminUser = isAdmin()
732
+ const isAdminUser = isAdmin(accessToken)
733
733
  const isBloggerMail = equalsMail(comment.mail, config.BLOGGER_EMAIL)
734
734
  if (isBloggerMail && !isAdminUser) throw new Error('请先登录管理面板,再使用博主身份发送评论')
735
735
  if (comment.mail && !isValidEmail(comment.mail)) throw new Error('邮箱格式不合法')
736
736
  const hashMethod = config.GRAVATAR_CDN === 'cravatar.cn' ? md5 : sha256
737
737
  const commentDo = {
738
738
  _id: uuidv4().replace(/-/g, ''),
739
- uid: getUid(),
739
+ uid: accessToken,
740
740
  nick: comment.nick ? comment.nick : '匿名',
741
741
  mail: comment.mail ? comment.mail : '',
742
742
  mailMd5: comment.mail ? hashMethod(normalizeMail(comment.mail)) : '',
@@ -996,8 +996,8 @@ async function qqNickGet (event) {
996
996
  }
997
997
 
998
998
  // 修改配置
999
- async function setConfig (event) {
1000
- const isAdminUser = isAdmin()
999
+ async function setConfig (event, accessToken) {
1000
+ const isAdminUser = isAdmin(accessToken)
1001
1001
  if (isAdminUser) {
1002
1002
  writeConfig(event.config)
1003
1003
  return {
@@ -1064,15 +1064,9 @@ async function writeConfig (newConfig) {
1064
1064
  }
1065
1065
  }
1066
1066
 
1067
- // 获取用户 ID
1068
- function getUid () {
1069
- return accessToken
1070
- }
1071
-
1072
1067
  // 判断用户是否管理员
1073
- function isAdmin () {
1074
- const uid = getUid()
1075
- return config.ADMIN_PASS === md5(uid)
1068
+ function isAdmin (accessToken) {
1069
+ return config.ADMIN_PASS === md5(accessToken)
1076
1070
  }
1077
1071
 
1078
1072
  // 判断是否为递归调用(即云函数调用自身)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "twikoo-vercel",
3
- "version": "1.7.15",
3
+ "version": "1.7.16",
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.7.15",
16
+ "twikoo-func": "1.7.16",
17
17
  "uuid": "^8.3.2"
18
18
  }
19
19
  }