Skip to content

Commit

Permalink
fix: comment deletion error (#431)
Browse files Browse the repository at this point in the history
* 修复 私有部署 显示、隐藏、置顶、删除评论功能异常
* 修复 私有部署 某些场景下邮件通知不发送的问题,并完善了邮件通知的日志
* 文档 更新 FAQ
  • Loading branch information
imaegoo authored Aug 10, 2022
1 parent 85c0d26 commit d2072ec
Show file tree
Hide file tree
Showing 12 changed files with 91 additions and 93 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/[email protected].4/dist/twikoo.all.min.js" ref="twikooJs"></component>
<component :is="'script'" src="https://cdn.jsdelivr.net/npm/[email protected].5/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.4" } }
{ "dependencies": { "twikoo-func": "1.6.5" } }
```

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

``` html
<div id="tcomment"></div>
<script src="https://cdn.jsdelivr.net/npm/[email protected].4/dist/twikoo.all.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected].5/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.4/twikoo.all.min.js`
* `https://lib.baomitu.com/twikoo/1.6.4/twikoo.all.min.js`
* `https://cdn.bootcdn.net/ajax/libs/twikoo/1.6.4/twikoo.all.min.js`
* `https://cdn.jsdelivr.net/npm/[email protected].4/dist/twikoo.all.min.js`
* `https://cdn.staticfile.org/twikoo/1.6.5/twikoo.all.min.js`
* `https://lib.baomitu.com/twikoo/1.6.5/twikoo.all.min.js`
* `https://cdn.bootcdn.net/ajax/libs/twikoo/1.6.5/twikoo.all.min.js`
* `https://cdn.jsdelivr.net/npm/[email protected].5/dist/twikoo.all.min.js`

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

Expand Down
37 changes: 16 additions & 21 deletions docs/faq.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@

## 如何修改头像?

请前往 [https://cn.gravatar.com](https://cn.gravatar.com) 通过邮箱注册并设定头像,评论时,请留下相同的邮箱。
请前往 [https://cravatar.cn](https://cravatar.cn) 通过邮箱注册并设定头像,评论时,请留下相同的邮箱。

访客还可以通过输入数字 QQ 邮箱地址,使用 QQ 头像发表评论。

## 如何修改、重置管理员密码?

请前往[云开发控制台](https://console.cloud.tencent.com/tcb/database/collection/config)编辑配置,删除 config.ADMIN_PASS 配置项,然后前往 Twikoo 管理面板重新设置密码。
腾讯云请前往[云开发控制台](https://console.cloud.tencent.com/tcb/database/collection/config),Vercel 请前往 MongoDB,私有部署请直接编辑 `data/db.json.1`编辑配置,删除 config.ADMIN_PASS 配置项,然后前往 Twikoo 管理面板重新设置密码。

## 如何获得管理面板的私钥文件?

Expand All @@ -26,21 +26,6 @@

来展示访问量。暂不支持全站访问量统计。

## 如何测试 Akismet 反垃圾配置是否生效?

请填写 `viagra-test-123` 作为昵称,或填写 `[email protected]` 作为邮箱,发表评论,这条评论将一定会被视为垃圾评论。

需要注意的是,由于 Akismet 服务响应速度较慢(大约 6 秒),影响用户体验,Twikoo 采取 “先放行,后检测” 的策略,垃圾评论会在发表后短暂可见。

## 免费资源如何计算?

您可以在云开发[环境总览](https://console.cloud.tencent.com/tcb/env/overview)看到资源使用情况。Twikoo 会消耗**数据库****云函数**两种资源,两种资源的免费用量为——

* 数据库:读 50,000 次/日,写 50,000 次/日
* 云函数:40,000 GBs/月

Twikoo 云函数的内存消耗恒定为 0.1GB,由此可计算出 Twikoo 云函数每月有长达 400,000 秒的运行时长,而免费资源的瓶颈主要在数据库日读取次数限制。建议站长关注免费资源的使用情况。

## 如何启用 Katex 支持?

Twikoo 支持 Katex 公式,但为了限制 Twikoo 的包大小,Twikoo 没有内置完整的 Katex,您需要[在页面中额外加载 katex.js](https://katex.org/docs/browser.html)
Expand Down Expand Up @@ -101,6 +86,12 @@ Akismet (Automattic Kismet) 是应用广泛的一个垃圾留言过滤系统,
1. 注册 [akismet.com](https://akismet.com)
2. 选择 Akismet Personal 订阅,复制得到的 Akismet API Key,到 Twikoo 管理面板“反垃圾”模块中配置

### 如何测试 Akismet 反垃圾配置是否生效?

请填写 `viagra-test-123` 作为昵称,或填写 `[email protected]` 作为邮箱,发表评论,这条评论将一定会被视为垃圾评论。

需要注意的是,由于 Akismet 服务响应速度较慢(大约 6 秒),影响用户体验,Twikoo 采取 “先放行,后检测” 的策略,垃圾评论会在发表后短暂可见。

## 登录管理面板遇到错误 AUTH_INVALID_CUSTOM_LOGIN_TICKET

一般是配置好登录私钥之后,又重新下载了登录私钥,导致之前配置的登录私钥失效了。<br>
Expand All @@ -112,7 +103,9 @@ Akismet (Automattic Kismet) 是应用广泛的一个垃圾留言过滤系统,

为了避免频繁检查邮箱带来的性能问题,邮件配置有 10 分钟左右的缓存,如果确定配置没有问题,但测试失败,可以等待 10 分钟后再测试。

## Vercel 无法上传图片?
由于博主发表评论时,不会通知博主,如果您想实际测试通知功能,请注销管理面板后用非博主邮箱发表或回复评论。

## Vercel、私有部署无法上传图片?

腾讯云环境自带云存储,所以腾讯云环境下可以直接上传图片,图片保存在云存储中。然而 Vercel 环境没有,上传图片功能依赖第三方图床,请在管理面板中配置图床,Twikoo 支持以下图床:

Expand All @@ -123,8 +116,10 @@ Akismet (Automattic Kismet) 是应用广泛的一个垃圾留言过滤系统,
| smms | https://sm.ms | SMMS 图床,有免费套餐,请自行注册账号,`IMAGE_CDN_TOKEN` 可在 [Dashboard](https://sm.ms/home/apitoken) 中获取 |
| [lsky-pro](https://www.lsky.pro) | 私有部署 | 兰空图床 2.0 版本,`IMAGE_CDN` 请配置图床首页 URL 地址(如 `https://7bu.top`),`IMAGE_CDN_TOKEN` 获取方式请参考教程 [杜老师说图床:新版本去不图床 Token 的获取与清空](https://dusays.com/454/),获取到的 token 格式应为 `1\|1bJbwlqBfnggmOMEZqXT5XusaIwqiZjCDs7r1Ob5`|

## 能私有部署吗?
## 私有部署能连接自己的数据库吗?

不能。私有部署不需要连接外部数据库,数据存储在启动 twikoo 时所在目录下的 data 目录,您可以直接复制该目录以完成数据备份。

不能。如果您确实需要支持私有部署评论系统,请优先考虑其他评论系统
twikoo 私有部署版使用内置数据库。如果连接外部数据库,会增加部署难度,导致诸如 _连接字符串不会配置、服务器内存不足、数据库版本不兼容、高 ping、连接不稳定_ 等麻烦事

您可以在这个项目 [twikoo-docker](https://github.com/twikoojs/twikoo-docker) 中看到私有部署的实现进度。私有部署存在一系列问题,现阶段作者仍没有时间和精力解决。欢迎贡献
twikoo 内置的数据库为 LokiJS 数据库,支持的数据库容量大约为 1 GB
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.4" } }
{ "dependencies": { "twikoo-func": "1.6.5" } }
```

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

``` html
<div id="tcomment"></div>
<script src="https://cdn.staticfile.org/twikoo/1.6.4/twikoo.all.min.js"></script>
<script src="https://cdn.staticfile.org/twikoo/1.6.5/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.4/twikoo.all.min.js`
* `https://lib.baomitu.com/twikoo/1.6.4/twikoo.all.min.js`
* `https://cdn.bootcdn.net/ajax/libs/twikoo/1.6.4/twikoo.all.min.js`
* `https://cdn.jsdelivr.net/npm/[email protected].4/dist/twikoo.all.min.js`
* `https://cdn.staticfile.org/twikoo/1.6.5/twikoo.all.min.js`
* `https://lib.baomitu.com/twikoo/1.6.5/twikoo.all.min.js`
* `https://cdn.bootcdn.net/ajax/libs/twikoo/1.6.5/twikoo.all.min.js`
* `https://cdn.jsdelivr.net/npm/[email protected].5/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.4",
"version": "1.6.5",
"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.4'
const version = '1.6.5'

export { version }
2 changes: 1 addition & 1 deletion 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.4",
"version": "1.6.5",
"description": "A simple comment system.",
"author": "imaegoo <[email protected]> (https://github.com/imaegoo)",
"license": "MIT",
Expand Down
43 changes: 32 additions & 11 deletions src/server/function/twikoo/utils/notify.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,20 @@ const fn = {
},
// 博主通知
async noticeMaster (comment, config) {
if (!transporter) if (!await fn.initMailer({ config })) return
if (config.BLOGGER_EMAIL === comment.mail) return
if (!transporter && !await fn.initMailer({ config })) {
console.log('未配置邮箱或邮箱配置有误,不通知')
return
}
if (config.BLOGGER_EMAIL && config.BLOGGER_EMAIL === comment.mail) {
console.log('博主本人评论,不发送通知给博主')
return
}
// 判断是否存在即时消息推送配置
const hasIMPushConfig = config.PUSHOO_CHANNEL && config.PUSHOO_TOKEN
// 存在即时消息推送配置,则默认不发送邮件给博主
if (hasIMPushConfig && config.SC_MAIL_NOTIFY !== 'true') return
if (hasIMPushConfig && config.SC_MAIL_NOTIFY !== 'true') {
console.log('存在即时消息推送配置,默认不发送邮件给博主,您可以在管理面板修改此行为')
return
}
const SITE_NAME = config.SITE_NAME
const NICK = comment.nick
const IMG = getAvatar(comment, config)
Expand Down Expand Up @@ -114,7 +122,10 @@ const fn = {
console.log('没有配置 pushoo,放弃即时消息通知')
return
}
if (config.BLOGGER_EMAIL === comment.mail) return
if (config.BLOGGER_EMAIL && config.BLOGGER_EMAIL === comment.mail) {
console.log('博主本人评论,不发送通知给博主')
return
}
const pushContent = fn.getIMPushContent(comment, config)
const sendResult = await pushoo(config.PUSHOO_CHANNEL, {
token: config.PUSHOO_TOKEN,
Expand Down Expand Up @@ -153,13 +164,23 @@ const fn = {
},
// 回复通知
async noticeReply (currentComment, config, getParentComment) {
if (!currentComment.pid) return
if (!transporter) if (!await fn.initMailer({ config })) return
if (!currentComment.pid) {
console.log('无父级评论,不通知')
return
}
if (!transporter && !await fn.initMailer({ config })) {
console.log('未配置邮箱或邮箱配置有误,不通知')
return
}
const parentComment = await getParentComment(currentComment)
// 回复给博主,因为会发博主通知邮件,所以不再重复通知
if (config.BLOGGER_EMAIL === parentComment.mail) return
// 回复自己的评论,不邮件通知
if (currentComment.mail === parentComment.mail) return
if (config.BLOGGER_EMAIL === parentComment.mail) {
console.log('回复给博主,因为会发博主通知邮件,所以不再重复通知')
return
}
if (currentComment.mail === parentComment.mail) {
console.log('回复自己的评论,不邮件通知')
return
}
const PARENT_NICK = parentComment.nick
const IMG = getAvatar(currentComment, config)
const PARENT_IMG = getAvatar(parentComment, config)
Expand Down
62 changes: 22 additions & 40 deletions src/server/self-hosted/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ const { v4: uuidv4 } = require('uuid') // 用户 id 生成
const {
$,
JSDOM,
axios,
createDOMPurify,
md5,
xml2js
Expand Down Expand Up @@ -99,9 +98,6 @@ module.exports = async (request, response) => {
case 'COMMENT_SUBMIT':
res = await commentSubmit(event, request)
break
case 'POST_SUBMIT':
res = await postSubmit(event.comment, request)
break
case 'COUNTER_GET':
res = await counterGet(event)
break
Expand Down Expand Up @@ -396,17 +392,17 @@ async function commentSetForAdmin (event) {
const isAdminUser = isAdmin(event.accessToken)
if (isAdminUser) {
validate(event, ['id', 'set'])
const data = db
db
.getCollection('comment')
.findAndUpdate({ _id: event.id }, (obj) => {
return {
...obj,
...event.set,
updated: Date.now()
for (const key of Object.keys(event.set)) {
obj[key] = event.set[key]
}
obj.updated = Date.now()
return obj
})
res.code = RES_CODE.SUCCESS
res.updated = data
res.updated = 1
} else {
res.code = RES_CODE.NEED_LOGIN
res.message = '请先登录'
Expand All @@ -420,11 +416,11 @@ async function commentDeleteForAdmin (event) {
const isAdminUser = isAdmin(event.accessToken)
if (isAdminUser) {
validate(event, ['id'])
const data = db
db
.getCollection('comment')
.findAndRemove({ _id: event.id })
res.code = RES_CODE.SUCCESS
res.deleted = data.deletedCount
res.deleted = 1
} else {
res.code = RES_CODE.NEED_LOGIN
res.message = '请先登录'
Expand Down Expand Up @@ -566,21 +562,9 @@ async function commentSubmit (event, request) {
const comment = await save(data)
res.id = comment.id
// 异步垃圾检测、发送评论通知
try {
console.log('开始异步垃圾检测、发送评论通知')
console.time('POST_SUBMIT')
await Promise.race([
axios.post(`http://${request.headers.host}`, {
event: 'POST_SUBMIT',
comment
}, { headers: { 'x-twikoo-recursion': config.ADMIN_PASS || 'true' } }),
// 如果超过 5 秒还没收到异步返回,直接继续,减少用户等待的时间
new Promise((resolve) => setTimeout(resolve, 5000))
])
console.timeEnd('POST_SUBMIT')
} catch (e) {
console.log('POST_SUBMIT 失败', e)
}
console.log('开始异步垃圾检测、发送评论通知')
// 私有部署支持直接异步调用
postSubmit(comment)
return res
}

Expand All @@ -601,14 +585,17 @@ async function getParentComment (currentComment) {
}

// 异步垃圾检测、发送评论通知
async function postSubmit (comment, request) {
if (!isRecursion(request)) return { code: RES_CODE.FORBIDDEN }
// 垃圾检测
const isSpam = await postCheckSpam(comment, config)
await saveSpamCheckResult(comment, isSpam)
// 发送通知
await sendNotice(comment, config, getParentComment)
return { code: RES_CODE.SUCCESS }
async function postSubmit (comment) {
try {
console.log('POST_SUBMIT')
// 垃圾检测
const isSpam = await postCheckSpam(comment, config)
await saveSpamCheckResult(comment, isSpam)
// 发送通知
await sendNotice(comment, config, getParentComment)
} catch (e) {
console.log('POST_SUBMIT 失败', e)
}
}

// 将评论转为数据库存储格式
Expand Down Expand Up @@ -885,11 +872,6 @@ function isAdmin (accessToken) {
return config.ADMIN_PASS === md5(accessToken)
}

// 判断是否为递归调用(即云函数调用自身)
function isRecursion (request) {
return request.headers['x-twikoo-recursion'] === (config.ADMIN_PASS || 'true')
}

// 建立数据库 collections
async function createCollections () {
const collections = ['comment', 'config', 'counter']
Expand Down
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.4",
"version": "1.6.5",
"description": "A simple comment system.",
"keywords": [
"twikoo",
Expand All @@ -25,7 +25,7 @@
},
"dependencies": {
"lokijs": "^1.5.12",
"twikoo-func": "1.6.4",
"twikoo-func": "1.6.5",
"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.4" } }
{ "dependencies": { "twikoo-vercel": "1.6.5" } }
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.4",
"version": "1.6.5",
"description": "A simple comment system.",
"author": "imaegoo <[email protected]> (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.4",
"twikoo-func": "1.6.5",
"uuid": "^8.3.2"
}
}

0 comments on commit d2072ec

Please sign in to comment.