Skip to content

Commit

Permalink
fix: several bugs of self-hosted server (#416)
Browse files Browse the repository at this point in the history
* 改进 更新 IP 属地数据库
* 修复 私有部署批量获取文章评论数 API 异常
* 修复 私有部署访问量统计功能异常
* 修复 私有部署 IP 限流功能异常
  • Loading branch information
imaegoo authored Aug 2, 2022
1 parent 8460c4d commit 0aa6169
Show file tree
Hide file tree
Showing 12 changed files with 60 additions and 58 deletions.
2 changes: 1 addition & 1 deletion docs/.vuepress/theme/layouts/Layout.vue
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

<!-- Twikoo -->
<div id="twikoo"></div>
<component :is="'script'" src="https://cdn.jsdelivr.net/npm/twikoo@1.6.0/dist/twikoo.all.min.js" ref="twikooJs"></component>
<component :is="'script'" src="https://cdn.jsdelivr.net/npm/twikoo@1.6.1/dist/twikoo.all.min.js" ref="twikooJs"></component>
</div>
</template>
</ParentLayout>
Expand Down
12 changes: 6 additions & 6 deletions docs/en/quick-start.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ exports.main = require('twikoo-func').main
8. 创建完成后,点击“twikoo"进入云函数详情页,进入“函数代码”标签,点击“文件 - 新建文件”,输入 `package.json`,回车
9. 复制以下代码、粘贴到代码框中,点击“保存并安装依赖”
``` json
{ "dependencies": { "twikoo-func": "1.6.0" } }
{ "dependencies": { "twikoo-func": "1.6.1" } }
```

### 命令行部署
Expand Down Expand Up @@ -206,7 +206,7 @@ twikoo:

``` html
<div id="tcomment"></div>
<script src="https://cdn.jsdelivr.net/npm/twikoo@1.6.0/dist/twikoo.all.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/twikoo@1.6.1/dist/twikoo.all.min.js"></script>
<script>
twikoo.init({
envId: '您的环境id', // 腾讯云环境填 envId;Vercel 环境填地址(https://xxx.vercel.app)
Expand All @@ -224,10 +224,10 @@ twikoo.init({

如果遇到默认 CDN 加载速度缓慢,可更换其他 CDN 镜像。以下为可供选择的公共 CDN,其中一些 CDN 可能需要数天时间同步最新版本:

* `https://cdn.staticfile.org/twikoo/1.6.0/twikoo.all.min.js`
* `https://lib.baomitu.com/twikoo/1.6.0/twikoo.all.min.js`
* `https://cdn.bootcdn.net/ajax/libs/twikoo/1.6.0/twikoo.all.min.js`
* `https://cdn.jsdelivr.net/npm/twikoo@1.6.0/dist/twikoo.all.min.js`
* `https://cdn.staticfile.org/twikoo/1.6.1/twikoo.all.min.js`
* `https://lib.baomitu.com/twikoo/1.6.1/twikoo.all.min.js`
* `https://cdn.bootcdn.net/ajax/libs/twikoo/1.6.1/twikoo.all.min.js`
* `https://cdn.jsdelivr.net/npm/twikoo@1.6.1/dist/twikoo.all.min.js`

## 开启管理面板(腾讯云环境)

Expand Down
12 changes: 6 additions & 6 deletions docs/quick-start.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ exports.main = require('twikoo-func').main
8. 创建完成后,点击“twikoo"进入云函数详情页,进入“函数代码”标签,点击“文件 - 新建文件”,输入 `package.json`,回车
9. 复制以下代码、粘贴到代码框中,点击“保存并安装依赖”
``` json
{ "dependencies": { "twikoo-func": "1.6.0" } }
{ "dependencies": { "twikoo-func": "1.6.1" } }
```

### 命令行部署
Expand Down Expand Up @@ -208,7 +208,7 @@ twikoo:

``` html
<div id="tcomment"></div>
<script src="https://cdn.staticfile.org/twikoo/1.6.0/twikoo.all.min.js"></script>
<script src="https://cdn.staticfile.org/twikoo/1.6.1/twikoo.all.min.js"></script>
<script>
twikoo.init({
envId: '您的环境id', // 腾讯云环境填 envId;Vercel 环境填地址(https://xxx.vercel.app)
Expand All @@ -226,10 +226,10 @@ twikoo.init({

如果遇到默认 CDN 加载速度缓慢,可更换其他 CDN 镜像。以下为可供选择的公共 CDN,其中一些 CDN 可能需要数天时间同步最新版本:

* `https://cdn.staticfile.org/twikoo/1.6.0/twikoo.all.min.js`
* `https://lib.baomitu.com/twikoo/1.6.0/twikoo.all.min.js`
* `https://cdn.bootcdn.net/ajax/libs/twikoo/1.6.0/twikoo.all.min.js`
* `https://cdn.jsdelivr.net/npm/twikoo@1.6.0/dist/twikoo.all.min.js`
* `https://cdn.staticfile.org/twikoo/1.6.1/twikoo.all.min.js`
* `https://lib.baomitu.com/twikoo/1.6.1/twikoo.all.min.js`
* `https://cdn.bootcdn.net/ajax/libs/twikoo/1.6.1/twikoo.all.min.js`
* `https://cdn.jsdelivr.net/npm/twikoo@1.6.1/dist/twikoo.all.min.js`

## 开启管理面板(腾讯云环境)

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "twikoo",
"version": "1.6.0",
"version": "1.6.1",
"description": "A simple comment system.",
"keywords": [
"twikoojs",
Expand Down
2 changes: 1 addition & 1 deletion src/client/version.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
const version = '1.6.0'
const version = '1.6.1'

export { version }
4 changes: 2 additions & 2 deletions src/server/function/twikoo/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "twikoo-func",
"version": "1.6.0",
"version": "1.6.1",
"description": "A simple comment system.",
"author": "imaegoo <hello@imaegoo.com> (https://github.com/imaegoo)",
"license": "MIT",
Expand All @@ -13,14 +13,14 @@
"dependencies": {
"@cloudbase/manager-node": "^3.9.0",
"@cloudbase/node-sdk": "^2.5.0",
"@imaegoo/node-ip2region": "^2.1.1",
"akismet-api": "^5.1.0",
"axios": "^0.21.1",
"blueimp-md5": "^2.18.0",
"bowser": "^2.11.0",
"cheerio": "1.0.0-rc.5",
"crypto-js": "^4.0.0",
"dompurify": "^2.2.6",
"dy-node-ip2region": "^1.0.1",
"form-data": "^4.0.0",
"jsdom": "^16.4.0",
"marked": "^4.0.12",
Expand Down
6 changes: 6 additions & 0 deletions src/server/function/twikoo/utils/import.js
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,12 @@ const fn = {
// 解决 id 历史数据问题
parsed._id = comment._id.$oid
}
if (comment.pid === null) {
delete comment.pid
}
if (comment.rid === null) {
delete comment.rid
}
comments.push(parsed)
log(`${comment._id} 解析成功`)
} catch (e) {
Expand Down
2 changes: 1 addition & 1 deletion src/server/function/twikoo/utils/lib.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const { JSDOM } = require('jsdom') // document.window 服务器版
const axios = require('axios') // 发送 REST 请求
const bowser = require('bowser') // UserAgent 格式化
const createDOMPurify = require('dompurify') // 反 XSS
const ipToRegion = require('dy-node-ip2region') // IP 属地查询
const ipToRegion = require('@imaegoo/node-ip2region') // IP 属地查询
const marked = require('marked') // Markdown 解析
const md5 = require('blueimp-md5') // MD5 加解密
const nodemailer = require('nodemailer') // 发送邮件
Expand Down
66 changes: 31 additions & 35 deletions src/server/self-hosted/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,12 @@ const DOMPurify = createDOMPurify(window)

// 常量 / constants
const { RES_CODE, MAX_REQUEST_TIMES } = require('twikoo-func/utils/constants')
const TWIKOO_REQ_TIMES_CLEAR_TIME = parseInt(process.env.TWIKOO_REQ_TIMES_CLEAR_TIME) || 10 * 60 * 1000

// 全局变量 / variables
let db = null
let config
const requestTimes = {}
let requestTimes = {}

connectToDatabase()

Expand Down Expand Up @@ -468,8 +469,8 @@ async function commentImportForAdmin (event) {
default:
throw new Error(`不支持 ${event.source} 的导入,请更新 Twikoo 云函数至最新版本`)
}
const insertedCount = await bulkSaveComments(comments).length
log(`导入成功 ${insertedCount} 条评论`)
await bulkSaveComments(comments)
log('导入成功')
} catch (e) {
log(e.message)
}
Expand Down Expand Up @@ -503,10 +504,9 @@ async function readFile (file, type, log) {

// 批量导入评论
async function bulkSaveComments (comments) {
const batchRes = db
db
.getCollection('comment')
.insert(comments)
return batchRes
}

// 点赞 / 取消点赞
Expand All @@ -531,11 +531,11 @@ async function like (id, uid) {
// 取消赞
likes = likes.filter((item) => item !== uid)
}
const result = await record.findAndUpdate({ _id: id }, (obj) => {
await record.findAndUpdate({ _id: id }, (obj) => {
obj.like = likes
return obj
})
return result
return 1
}

/**
Expand Down Expand Up @@ -720,26 +720,23 @@ async function readCounter (url) {
* @param {String} event.title 文章标题
*/
async function incCounter (event) {
let result
result = db
.getCollection('counter')
.findAndUpdate({ url: event.url }, (obj) => {
obj.time = obj.time ? obj.time + 1 : 1
obj.title = event.title
obj.updated = Date.now()
const counter = db.getCollection('counter')
const result = counter.find({ url: event.url })[0]
if (result) {
result.time = result.time ? result.time + 1 : 1
result.title = event.title
result.updated = Date.now()
counter.update(result)
} else {
counter.insert({
url: event.url,
title: event.title,
time: 1,
created: Date.now(),
updated: Date.now()
})
if (result.modifiedCount === 0) {
result = db
.getCollection('counter')
.insert({
url: event.url,
title: event.title,
time: 1,
created: Date.now(),
updated: Date.now()
})
}
return result.modifiedCount || result.insertedCount
return 1
}

/**
Expand All @@ -757,20 +754,13 @@ async function getCommentsCount (event) {
if (!event.includeReply) {
query.rid = { $exists: false }
}
const result = db
.getCollection('comment')
.chain()
.aggregate([
{ $match: query },
{ $group: { _id: '$url', count: { $sum: 1 } } }
])
.data()
res.data = []
const commentCollection = db.getCollection('comment')
for (const url of event.urls) {
const record = result.find((item) => item._id === url)
const record = commentCollection.count({ ...query, url })
res.data.push({
url,
count: record ? record.count : 0
count: record || 0
})
}
} catch (e) {
Expand Down Expand Up @@ -915,3 +905,9 @@ async function createCollections () {
function getIp (request) {
return request.headers['x-forwarded-for'] || request.socket.remoteAddress || ''
}

function clearRequestTimes () {
requestTimes = {}
}

setTimeout(clearRequestTimes, TWIKOO_REQ_TIMES_CLEAR_TIME)
4 changes: 2 additions & 2 deletions src/server/self-hosted/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "tkserver",
"version": "1.6.0",
"version": "1.6.1",
"description": "A simple comment system.",
"keywords": [
"twikoo",
Expand All @@ -25,7 +25,7 @@
},
"dependencies": {
"lokijs": "^1.5.12",
"twikoo-func": "1.6.0",
"twikoo-func": "1.6.1",
"uuid": "^8.3.2"
}
}
2 changes: 1 addition & 1 deletion src/server/vercel-min/package.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{ "dependencies": { "twikoo-vercel": "1.6.0" } }
{ "dependencies": { "twikoo-vercel": "1.6.1" } }
4 changes: 2 additions & 2 deletions src/server/vercel/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "twikoo-vercel",
"version": "1.6.0",
"version": "1.6.1",
"description": "A simple comment system.",
"author": "imaegoo <hello@imaegoo.com> (https://github.com/imaegoo)",
"license": "MIT",
Expand All @@ -12,7 +12,7 @@
"homepage": "https://twikoo.js.org",
"dependencies": {
"mongodb": "^3.6.3",
"twikoo-func": "1.6.0",
"twikoo-func": "1.6.1",
"uuid": "^8.3.2"
}
}

0 comments on commit 0aa6169

Please sign in to comment.