twikoo-vercel 1.4.9 → 1.4.13

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 +31 -6
  2. package/package.json +1 -1
package/api/index.js CHANGED
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * Twikoo vercel function v1.4.9
2
+ * Twikoo vercel function v1.4.13
3
3
  * (c) 2020-present iMaeGoo
4
4
  * Released under the MIT License.
5
5
  */
@@ -27,7 +27,7 @@ const window = new JSDOM('').window
27
27
  const DOMPurify = createDOMPurify(window)
28
28
 
29
29
  // 常量 / constants
30
- const VERSION = '1.4.9'
30
+ const VERSION = '1.4.13'
31
31
  const RES_CODE = {
32
32
  SUCCESS: 0,
33
33
  NO_PARAM: 100,
@@ -852,7 +852,7 @@ async function commentSubmit (event) {
852
852
  axios.post(`https://${process.env.VERCEL_URL}`, {
853
853
  event: 'POST_SUBMIT',
854
854
  comment
855
- }, { headers: { 'x-twikoo-recursion': 'true' } }),
855
+ }, { headers: { 'x-twikoo-recursion': config.ADMIN_PASS || 'true' } }),
856
856
  // 如果超过 5 秒还没收到异步返回,直接继续,减少用户等待的时间
857
857
  new Promise((resolve) => setTimeout(resolve, 5000))
858
858
  ])
@@ -891,6 +891,7 @@ async function sendNotice (comment) {
891
891
  noticeWeChat(comment),
892
892
  noticePushPlus(comment),
893
893
  noticeWeComPush(comment),
894
+ noticeDingTalkHook(comment),
894
895
  noticeQQ(comment)
895
896
  ]).catch(console.error)
896
897
  return { code: RES_CODE.SUCCESS }
@@ -940,7 +941,8 @@ async function noticeMaster (comment) {
940
941
  'SC_SENDKEY',
941
942
  'QM_SENDKEY',
942
943
  'PUSH_PLUS_TOKEN',
943
- 'WECOM_API_URL'
944
+ 'WECOM_API_URL',
945
+ 'DINGTALK_WEBHOOK_URL'
944
946
  ]
945
947
  // 判断是否存在即时消息推送配置
946
948
  const hasIMPushConfig = IM_PUSH_CONFIGS.some(item => !!config[item])
@@ -948,6 +950,9 @@ async function noticeMaster (comment) {
948
950
  if (hasIMPushConfig && config.SC_MAIL_NOTIFY !== 'true') return
949
951
  const SITE_NAME = config.SITE_NAME
950
952
  const NICK = comment.nick
953
+ const IMG = getAvatar(comment)
954
+ const IP = comment.ip
955
+ const MAIL = comment.mail
951
956
  const COMMENT = comment.comment
952
957
  const SITE_URL = config.SITE_URL
953
958
  const POST_URL = appendHashToUrl(comment.href || SITE_URL + comment.url, comment.id)
@@ -958,6 +963,9 @@ async function noticeMaster (comment) {
958
963
  .replace(/\${SITE_URL}/g, SITE_URL)
959
964
  .replace(/\${SITE_NAME}/g, SITE_NAME)
960
965
  .replace(/\${NICK}/g, NICK)
966
+ .replace(/\${IMG}/g, IMG)
967
+ .replace(/\${IP}/g, IP)
968
+ .replace(/\${MAIL}/g, MAIL)
961
969
  .replace(/\${COMMENT}/g, COMMENT)
962
970
  .replace(/\${POST_URL}/g, POST_URL)
963
971
  } else {
@@ -1046,6 +1054,18 @@ async function noticeWeComPush (comment) {
1046
1054
  console.log('WinxinPush 通知结果:', sendResult)
1047
1055
  }
1048
1056
 
1057
+ // 自定义钉钉WebHook通知
1058
+ async function noticeDingTalkHook (comment) {
1059
+ if (!config.DINGTALK_WEBHOOK_URL) {
1060
+ console.log('没有配置 DingTalk_WebHook,放弃钉钉WebHook推送')
1061
+ return
1062
+ }
1063
+ if (config.BLOGGER_EMAIL === comment.mail) return
1064
+ const DingTalkContent = config.SITE_NAME + '有新评论啦!🎉🎉' + '\n\n' + '@' + comment.nick + ' 说:' + $(comment.comment).text() + '\n' + 'E-mail: ' + comment.mail + '\n' + 'IP: ' + comment.ip + '\n' + '点此查看完整内容:' + appendHashToUrl(comment.href || config.SITE_URL + comment.url, comment.id)
1065
+ const sendResult = await axios.post(config.DINGTALK_WEBHOOK_URL, { msgtype: 'text', text: { content: DingTalkContent } })
1066
+ console.log('钉钉WebHook 通知结果:', sendResult)
1067
+ }
1068
+
1049
1069
  // QQ通知
1050
1070
  async function noticeQQ (comment) {
1051
1071
  if (!config.QM_SENDKEY) {
@@ -1093,6 +1113,8 @@ async function noticeReply (currentComment) {
1093
1113
  // 回复自己的评论,不邮件通知
1094
1114
  if (currentComment.mail === parentComment.mail) return
1095
1115
  const PARENT_NICK = parentComment.nick
1116
+ const IMG = getAvatar(currentComment)
1117
+ const PARENT_IMG = getAvatar(parentComment)
1096
1118
  const SITE_NAME = config.SITE_NAME
1097
1119
  const NICK = currentComment.nick
1098
1120
  const COMMENT = currentComment.comment
@@ -1103,6 +1125,8 @@ async function noticeReply (currentComment) {
1103
1125
  let emailContent
1104
1126
  if (config.MAIL_TEMPLATE) {
1105
1127
  emailContent = config.MAIL_TEMPLATE
1128
+ .replace(/\${IMG}/g, IMG)
1129
+ .replace(/\${PARENT_IMG}/g, PARENT_IMG)
1106
1130
  .replace(/\${SITE_URL}/g, SITE_URL)
1107
1131
  .replace(/\${SITE_NAME}/g, SITE_NAME)
1108
1132
  .replace(/\${PARENT_NICK}/g, PARENT_NICK)
@@ -1458,7 +1482,7 @@ function getAvatar (comment) {
1458
1482
  if (comment.avatar) {
1459
1483
  return comment.avatar
1460
1484
  } else {
1461
- const gravatarCdn = config.GRAVATAR_CDN || 'cn.gravatar.com'
1485
+ const gravatarCdn = config.GRAVATAR_CDN || 'cravatar.cn'
1462
1486
  const defaultGravatar = config.DEFAULT_GRAVATAR || 'identicon'
1463
1487
  const mailMd5 = comment.mailMd5 || md5(comment.mail)
1464
1488
  return `https://${gravatarCdn}/avatar/${mailMd5}?d=${defaultGravatar}`
@@ -1504,6 +1528,7 @@ async function getConfig () {
1504
1528
  DEFAULT_GRAVATAR: config.DEFAULT_GRAVATAR,
1505
1529
  SHOW_IMAGE: config.SHOW_IMAGE || 'true',
1506
1530
  IMAGE_CDN: config.IMAGE_CDN,
1531
+ IMAGE_CDN_TOKEN: config.IMAGE_CDN_TOKEN,
1507
1532
  SHOW_EMOTION: config.SHOW_EMOTION || 'true',
1508
1533
  EMOTION_CDN: config.EMOTION_CDN,
1509
1534
  COMMENT_PLACEHOLDER: config.COMMENT_PLACEHOLDER,
@@ -1600,7 +1625,7 @@ async function isAdmin () {
1600
1625
 
1601
1626
  // 判断是否为递归调用(即云函数调用自身)
1602
1627
  function isRecursion () {
1603
- return request.headers['x-twikoo-recursion'] === 'true'
1628
+ return request.headers['x-twikoo-recursion'] === (config.ADMIN_PASS || 'true')
1604
1629
  }
1605
1630
 
1606
1631
  // 建立数据库 collections
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "twikoo-vercel",
3
- "version": "1.4.9",
3
+ "version": "1.4.13",
4
4
  "description": "A simple comment system based on Tencent CloudBase (tcb).",
5
5
  "author": "imaegoo <hello@imaegoo.com> (https://github.com/imaegoo)",
6
6
  "license": "MIT",