tkserver 1.6.0 → 1.6.3
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 +34 -38
- package/package.json +2 -2
package/index.js
CHANGED
|
@@ -49,11 +49,12 @@ const DOMPurify = createDOMPurify(window)
|
|
|
49
49
|
|
|
50
50
|
// 常量 / constants
|
|
51
51
|
const { RES_CODE, MAX_REQUEST_TIMES } = require('twikoo-func/utils/constants')
|
|
52
|
+
const TWIKOO_REQ_TIMES_CLEAR_TIME = parseInt(process.env.TWIKOO_REQ_TIMES_CLEAR_TIME) || 10 * 60 * 1000
|
|
52
53
|
|
|
53
54
|
// 全局变量 / variables
|
|
54
55
|
let db = null
|
|
55
56
|
let config
|
|
56
|
-
|
|
57
|
+
let requestTimes = {}
|
|
57
58
|
|
|
58
59
|
connectToDatabase()
|
|
59
60
|
|
|
@@ -468,8 +469,8 @@ async function commentImportForAdmin (event) {
|
|
|
468
469
|
default:
|
|
469
470
|
throw new Error(`不支持 ${event.source} 的导入,请更新 Twikoo 云函数至最新版本`)
|
|
470
471
|
}
|
|
471
|
-
|
|
472
|
-
log(
|
|
472
|
+
await bulkSaveComments(comments)
|
|
473
|
+
log('导入成功')
|
|
473
474
|
} catch (e) {
|
|
474
475
|
log(e.message)
|
|
475
476
|
}
|
|
@@ -503,10 +504,9 @@ async function readFile (file, type, log) {
|
|
|
503
504
|
|
|
504
505
|
// 批量导入评论
|
|
505
506
|
async function bulkSaveComments (comments) {
|
|
506
|
-
|
|
507
|
+
db
|
|
507
508
|
.getCollection('comment')
|
|
508
509
|
.insert(comments)
|
|
509
|
-
return batchRes
|
|
510
510
|
}
|
|
511
511
|
|
|
512
512
|
// 点赞 / 取消点赞
|
|
@@ -531,11 +531,11 @@ async function like (id, uid) {
|
|
|
531
531
|
// 取消赞
|
|
532
532
|
likes = likes.filter((item) => item !== uid)
|
|
533
533
|
}
|
|
534
|
-
|
|
534
|
+
await record.findAndUpdate({ _id: id }, (obj) => {
|
|
535
535
|
obj.like = likes
|
|
536
536
|
return obj
|
|
537
537
|
})
|
|
538
|
-
return
|
|
538
|
+
return 1
|
|
539
539
|
}
|
|
540
540
|
|
|
541
541
|
/**
|
|
@@ -570,7 +570,7 @@ async function commentSubmit (event, request) {
|
|
|
570
570
|
console.log('开始异步垃圾检测、发送评论通知')
|
|
571
571
|
console.time('POST_SUBMIT')
|
|
572
572
|
await Promise.race([
|
|
573
|
-
axios.post(`
|
|
573
|
+
axios.post(`http://${request.headers.host}`, {
|
|
574
574
|
event: 'POST_SUBMIT',
|
|
575
575
|
comment
|
|
576
576
|
}, { headers: { 'x-twikoo-recursion': config.ADMIN_PASS || 'true' } }),
|
|
@@ -597,14 +597,14 @@ async function getParentComment (currentComment) {
|
|
|
597
597
|
const parentComment = db
|
|
598
598
|
.getCollection('comment')
|
|
599
599
|
.findOne({ _id: currentComment.pid })
|
|
600
|
-
return parentComment
|
|
600
|
+
return parentComment
|
|
601
601
|
}
|
|
602
602
|
|
|
603
603
|
// 异步垃圾检测、发送评论通知
|
|
604
604
|
async function postSubmit (comment, request) {
|
|
605
605
|
if (!isRecursion(request)) return { code: RES_CODE.FORBIDDEN }
|
|
606
606
|
// 垃圾检测
|
|
607
|
-
const isSpam = await postCheckSpam(comment)
|
|
607
|
+
const isSpam = await postCheckSpam(comment, config)
|
|
608
608
|
await saveSpamCheckResult(comment, isSpam)
|
|
609
609
|
// 发送通知
|
|
610
610
|
await sendNotice(comment, config, getParentComment)
|
|
@@ -720,26 +720,23 @@ async function readCounter (url) {
|
|
|
720
720
|
* @param {String} event.title 文章标题
|
|
721
721
|
*/
|
|
722
722
|
async function incCounter (event) {
|
|
723
|
-
|
|
724
|
-
result =
|
|
725
|
-
|
|
726
|
-
.
|
|
727
|
-
|
|
728
|
-
|
|
729
|
-
|
|
723
|
+
const counter = db.getCollection('counter')
|
|
724
|
+
const result = counter.find({ url: event.url })[0]
|
|
725
|
+
if (result) {
|
|
726
|
+
result.time = result.time ? result.time + 1 : 1
|
|
727
|
+
result.title = event.title
|
|
728
|
+
result.updated = Date.now()
|
|
729
|
+
counter.update(result)
|
|
730
|
+
} else {
|
|
731
|
+
counter.insert({
|
|
732
|
+
url: event.url,
|
|
733
|
+
title: event.title,
|
|
734
|
+
time: 1,
|
|
735
|
+
created: Date.now(),
|
|
736
|
+
updated: Date.now()
|
|
730
737
|
})
|
|
731
|
-
if (result.modifiedCount === 0) {
|
|
732
|
-
result = db
|
|
733
|
-
.getCollection('counter')
|
|
734
|
-
.insert({
|
|
735
|
-
url: event.url,
|
|
736
|
-
title: event.title,
|
|
737
|
-
time: 1,
|
|
738
|
-
created: Date.now(),
|
|
739
|
-
updated: Date.now()
|
|
740
|
-
})
|
|
741
738
|
}
|
|
742
|
-
return
|
|
739
|
+
return 1
|
|
743
740
|
}
|
|
744
741
|
|
|
745
742
|
/**
|
|
@@ -757,20 +754,13 @@ async function getCommentsCount (event) {
|
|
|
757
754
|
if (!event.includeReply) {
|
|
758
755
|
query.rid = { $exists: false }
|
|
759
756
|
}
|
|
760
|
-
const result = db
|
|
761
|
-
.getCollection('comment')
|
|
762
|
-
.chain()
|
|
763
|
-
.aggregate([
|
|
764
|
-
{ $match: query },
|
|
765
|
-
{ $group: { _id: '$url', count: { $sum: 1 } } }
|
|
766
|
-
])
|
|
767
|
-
.data()
|
|
768
757
|
res.data = []
|
|
758
|
+
const commentCollection = db.getCollection('comment')
|
|
769
759
|
for (const url of event.urls) {
|
|
770
|
-
const record =
|
|
760
|
+
const record = commentCollection.count({ ...query, url })
|
|
771
761
|
res.data.push({
|
|
772
762
|
url,
|
|
773
|
-
count: record
|
|
763
|
+
count: record || 0
|
|
774
764
|
})
|
|
775
765
|
}
|
|
776
766
|
} catch (e) {
|
|
@@ -915,3 +905,9 @@ async function createCollections () {
|
|
|
915
905
|
function getIp (request) {
|
|
916
906
|
return request.headers['x-forwarded-for'] || request.socket.remoteAddress || ''
|
|
917
907
|
}
|
|
908
|
+
|
|
909
|
+
function clearRequestTimes () {
|
|
910
|
+
requestTimes = {}
|
|
911
|
+
}
|
|
912
|
+
|
|
913
|
+
setTimeout(clearRequestTimes, TWIKOO_REQ_TIMES_CLEAR_TIME)
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "tkserver",
|
|
3
|
-
"version": "1.6.
|
|
3
|
+
"version": "1.6.3",
|
|
4
4
|
"description": "A simple comment system.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"twikoo",
|
|
@@ -25,7 +25,7 @@
|
|
|
25
25
|
},
|
|
26
26
|
"dependencies": {
|
|
27
27
|
"lokijs": "^1.5.12",
|
|
28
|
-
"twikoo-func": "1.6.
|
|
28
|
+
"twikoo-func": "1.6.3",
|
|
29
29
|
"uuid": "^8.3.2"
|
|
30
30
|
}
|
|
31
31
|
}
|