twikoo-func 1.5.6 → 1.5.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.
- package/index.js +37 -27
- package/package.json +2 -1
package/index.js
CHANGED
|
@@ -21,7 +21,8 @@ const CryptoJS = require('crypto-js') // 编解码
|
|
|
21
21
|
const tencentcloud = require('tencentcloud-sdk-nodejs') // 腾讯云 API NODEJS SDK
|
|
22
22
|
const fs = require('fs')
|
|
23
23
|
const FormData = require('form-data') // 图片上传
|
|
24
|
-
const pushoo = require('pushoo').default
|
|
24
|
+
const pushoo = require('pushoo').default // 即时消息通知
|
|
25
|
+
const ipToRegion = require('dy-node-ip2region') // IP 属地查询
|
|
25
26
|
|
|
26
27
|
// 云函数 SDK / tencent cloudbase sdk
|
|
27
28
|
const app = tcb.init({ env: tcb.SYMBOL_CURRENT_ENV })
|
|
@@ -33,6 +34,9 @@ const _ = db.command
|
|
|
33
34
|
const window = new JSDOM('').window
|
|
34
35
|
const DOMPurify = createDOMPurify(window)
|
|
35
36
|
|
|
37
|
+
// 初始化 IP 属地
|
|
38
|
+
const ipRegionSearcher = ipToRegion.create()
|
|
39
|
+
|
|
36
40
|
// 常量 / constants
|
|
37
41
|
const RES_CODE = {
|
|
38
42
|
SUCCESS: 0,
|
|
@@ -343,13 +347,15 @@ function parseComment (comments, uid) {
|
|
|
343
347
|
function toCommentDto (comment, uid, replies = [], comments = []) {
|
|
344
348
|
let displayOs = ''
|
|
345
349
|
let displayBrowser = ''
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
350
|
+
if (config.SHOW_UA !== 'false') {
|
|
351
|
+
try {
|
|
352
|
+
const ua = bowser.getParser(comment.ua)
|
|
353
|
+
const os = ua.getOS()
|
|
354
|
+
displayOs = [os.name, os.versionName ? os.versionName : os.version].join(' ')
|
|
355
|
+
displayBrowser = [ua.getBrowserName(), ua.getBrowserVersion()].join(' ')
|
|
356
|
+
} catch (e) {
|
|
357
|
+
console.log('bowser 错误:', e)
|
|
358
|
+
}
|
|
353
359
|
}
|
|
354
360
|
return {
|
|
355
361
|
id: comment._id,
|
|
@@ -360,6 +366,7 @@ function toCommentDto (comment, uid, replies = [], comments = []) {
|
|
|
360
366
|
comment: comment.comment,
|
|
361
367
|
os: displayOs,
|
|
362
368
|
browser: displayBrowser,
|
|
369
|
+
ipRegion: config.SHOW_REGION ? getIpRegion({ ip: comment.ip }) : '',
|
|
363
370
|
master: comment.master,
|
|
364
371
|
like: comment.like ? comment.like.length : 0,
|
|
365
372
|
liked: comment.like ? comment.like.findIndex((item) => item === uid) > -1 : false,
|
|
@@ -440,7 +447,7 @@ function getCommentSearchCondition (event) {
|
|
|
440
447
|
|
|
441
448
|
function parseCommentForAdmin (comments) {
|
|
442
449
|
for (const comment of comments) {
|
|
443
|
-
comment.
|
|
450
|
+
comment.ipRegion = getIpRegion({ ip: comment.ip, detail: true })
|
|
444
451
|
}
|
|
445
452
|
return comments
|
|
446
453
|
}
|
|
@@ -1439,24 +1446,6 @@ async function uploadImage (event) {
|
|
|
1439
1446
|
return res
|
|
1440
1447
|
}
|
|
1441
1448
|
|
|
1442
|
-
async function uploadImageTo7Bu ({ photo, fileName, config, res }) {
|
|
1443
|
-
// 去不图床旧版本 https://7bu.top
|
|
1444
|
-
// TODO: 2022 年 4 月 30 日后去不图床将会升级新版本,此处逻辑要同步更新
|
|
1445
|
-
const formData = new FormData()
|
|
1446
|
-
formData.append('image', base64UrlToReadStream(photo, fileName))
|
|
1447
|
-
const uploadResult = await axios.post('https://7bu.top/api/upload', formData, {
|
|
1448
|
-
headers: {
|
|
1449
|
-
...formData.getHeaders(),
|
|
1450
|
-
token: config.IMAGE_CDN_TOKEN
|
|
1451
|
-
}
|
|
1452
|
-
})
|
|
1453
|
-
if (uploadResult.data.code === 200) {
|
|
1454
|
-
res.data = uploadResult.data.data
|
|
1455
|
-
} else {
|
|
1456
|
-
throw new Error(uploadResult.data.msg)
|
|
1457
|
-
}
|
|
1458
|
-
}
|
|
1459
|
-
|
|
1460
1449
|
async function uploadImageToSmms ({ photo, fileName, config, res }) {
|
|
1461
1450
|
// SM.MS 图床 https://sm.ms
|
|
1462
1451
|
const formData = new FormData()
|
|
@@ -1670,6 +1659,27 @@ async function isAdmin () {
|
|
|
1670
1659
|
return ADMIN_USER_ID === userInfo.userInfo.customUserId
|
|
1671
1660
|
}
|
|
1672
1661
|
|
|
1662
|
+
/**
|
|
1663
|
+
* 获取 IP 属地
|
|
1664
|
+
* @param detail true 返回省市运营商,false 只返回省
|
|
1665
|
+
* @returns {String}
|
|
1666
|
+
*/
|
|
1667
|
+
function getIpRegion ({ ip, detail = false }) {
|
|
1668
|
+
if (!ip) return ''
|
|
1669
|
+
try {
|
|
1670
|
+
const { region } = ipRegionSearcher.btreeSearchSync(ip)
|
|
1671
|
+
const [,, province, city, isp] = region.split('|')
|
|
1672
|
+
if (detail) {
|
|
1673
|
+
return province === city ? [city, isp].join(' ') : [province, city, isp].join(' ')
|
|
1674
|
+
} else {
|
|
1675
|
+
return province
|
|
1676
|
+
}
|
|
1677
|
+
} catch (e) {
|
|
1678
|
+
console.error('IP 属地查询失败:', e)
|
|
1679
|
+
return ''
|
|
1680
|
+
}
|
|
1681
|
+
}
|
|
1682
|
+
|
|
1673
1683
|
// 判断是否为递归调用(即云函数调用自身)
|
|
1674
1684
|
function isRecursion (context) {
|
|
1675
1685
|
const envObj = tcb.getCloudbaseContext(context)
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "twikoo-func",
|
|
3
|
-
"version": "1.5.
|
|
3
|
+
"version": "1.5.9",
|
|
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",
|
|
@@ -20,6 +20,7 @@
|
|
|
20
20
|
"cheerio": "1.0.0-rc.5",
|
|
21
21
|
"crypto-js": "^4.0.0",
|
|
22
22
|
"dompurify": "^2.2.6",
|
|
23
|
+
"dy-node-ip2region": "^1.0.1",
|
|
23
24
|
"form-data": "^4.0.0",
|
|
24
25
|
"jsdom": "^16.4.0",
|
|
25
26
|
"marked": "^4.0.12",
|