twikoo-func 1.6.12 → 1.6.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.
package/index.js CHANGED
@@ -8,8 +8,7 @@ const { version: VERSION } = require('./package.json')
8
8
  const tcb = require('@cloudbase/node-sdk') // 云开发 SDK
9
9
  const {
10
10
  $,
11
- JSDOM,
12
- createDOMPurify,
11
+ getDomPurify,
13
12
  md5,
14
13
  xml2js
15
14
  } = require('./utils/lib')
@@ -35,6 +34,7 @@ const {
35
34
  commentImportValine,
36
35
  commentImportDisqus,
37
36
  commentImportArtalk,
37
+ commentImportArtalk2,
38
38
  commentImportTwikoo
39
39
  } = require('./utils/import')
40
40
  const { postCheckSpam } = require('./utils/spam')
@@ -46,10 +46,7 @@ const app = tcb.init({ env: tcb.SYMBOL_CURRENT_ENV })
46
46
  const auth = app.auth()
47
47
  const db = app.database()
48
48
  const _ = db.command
49
-
50
- // 初始化反 XSS
51
- const window = new JSDOM('').window
52
- const DOMPurify = createDOMPurify(window)
49
+ const DOMPurify = getDomPurify()
53
50
 
54
51
  // 常量 / constants
55
52
  const { RES_CODE, MAX_REQUEST_TIMES } = require('./utils/constants')
@@ -131,6 +128,9 @@ exports.main = async (event, context) => {
131
128
  case 'UPLOAD_IMAGE': // >= 1.5.0
132
129
  res = await uploadImage(event, config)
133
130
  break
131
+ case 'COMMENT_EXPORT_FOR_ADMIN': // >= 1.6.13
132
+ res = await commentExportForAdmin(event)
133
+ break
134
134
  default:
135
135
  if (event.event) {
136
136
  res.code = RES_CODE.EVENT_NOT_EXIST
@@ -430,6 +430,11 @@ async function commentImportForAdmin (event) {
430
430
  comments = await commentImportArtalk(artalkDb, log)
431
431
  break
432
432
  }
433
+ case 'artalk2': {
434
+ const artalkDb = await readFile(event.fileId, 'json', log)
435
+ comments = await commentImportArtalk2(artalkDb, log)
436
+ break
437
+ }
433
438
  case 'twikoo': {
434
439
  const twikooDb = await readFile(event.fileId, 'json', log)
435
440
  comments = await commentImportTwikoo(twikooDb, log)
@@ -455,6 +460,23 @@ async function commentImportForAdmin (event) {
455
460
  return res
456
461
  }
457
462
 
463
+ async function commentExportForAdmin (event) {
464
+ const res = {}
465
+ const isAdminUser = await isAdmin()
466
+ if (isAdminUser) {
467
+ const collection = event.collection || 'comment'
468
+ const data = await db
469
+ .collection(collection)
470
+ .get()
471
+ res.code = RES_CODE.SUCCESS
472
+ res.data = data.data
473
+ } else {
474
+ res.code = RES_CODE.NEED_LOGIN
475
+ res.message = '请先登录'
476
+ }
477
+ return res
478
+ }
479
+
458
480
  // 读取云存储中的文件并转为 js object
459
481
  async function readFile (fileId, type, log) {
460
482
  try {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "twikoo-func",
3
- "version": "1.6.12",
3
+ "version": "1.6.13",
4
4
  "description": "A simple comment system.",
5
5
  "author": "imaegoo <hello@imaegoo.com> (https://github.com/imaegoo)",
6
6
  "license": "MIT",
package/utils/import.js CHANGED
@@ -1,5 +1,5 @@
1
1
  const { getRelativeUrl } = require('.')
2
- const { marked, DOMPurify, md5 } = require('./lib')
2
+ const { marked, getDomPurify, md5 } = require('./lib')
3
3
 
4
4
  const fn = {
5
5
  // 兼容 Leancloud 两种 JSON 导出格式
@@ -135,13 +135,13 @@ const fn = {
135
135
  log('Artalk 评论文件格式有误')
136
136
  return
137
137
  }
138
+ const DOMPurify = getDomPurify()
138
139
  marked.setOptions({
139
140
  renderer: new marked.Renderer(),
140
141
  gfm: true,
141
142
  tables: true,
142
143
  breaks: true,
143
144
  pedantic: false,
144
- sanitize: true,
145
145
  smartLists: true,
146
146
  smartypants: true
147
147
  })
@@ -160,7 +160,7 @@ const fn = {
160
160
  pid: comment.rid ? `artalk${comment.rid}` : '',
161
161
  rid: comment.rid ? `artalk${comment.rid}` : '',
162
162
  master: false,
163
- comment: DOMPurify.sanitize(marked(comment.content)),
163
+ comment: DOMPurify.sanitize(marked.marked(comment.content)),
164
164
  url: getRelativeUrl(comment.page_key),
165
165
  href: comment.page_key,
166
166
  created: new Date(comment.date).getTime(),
@@ -175,6 +175,53 @@ const fn = {
175
175
  log(`解析成功 ${comments.length} 条评论`)
176
176
  return comments
177
177
  },
178
+ // Artalk v2 导入
179
+ async commentImportArtalk2 (artalkDb, log) {
180
+ const comments = []
181
+ if (!artalkDb || !artalkDb.length) {
182
+ log('Artalk v2 评论文件格式有误')
183
+ return
184
+ }
185
+ const DOMPurify = getDomPurify()
186
+ marked.setOptions({
187
+ renderer: new marked.Renderer(),
188
+ gfm: true,
189
+ tables: true,
190
+ breaks: true,
191
+ pedantic: false,
192
+ smartLists: true,
193
+ smartypants: true
194
+ })
195
+ log(`共 ${artalkDb.length} 条评论`)
196
+ for (const comment of artalkDb) {
197
+ try {
198
+ const parsed = {
199
+ _id: `artalk${comment.id}`,
200
+ nick: comment.nick,
201
+ ip: comment.ip,
202
+ mail: comment.email,
203
+ mailMd5: md5(comment.email),
204
+ isSpam: comment.is_pending,
205
+ ua: comment.ua || '',
206
+ link: comment.link,
207
+ pid: comment.rid ? `artalk${comment.rid}` : '',
208
+ rid: comment.rid ? `artalk${comment.rid}` : '',
209
+ master: false,
210
+ comment: DOMPurify.sanitize(marked.marked(comment.content)),
211
+ url: getRelativeUrl(comment.page_key),
212
+ href: comment.page_key,
213
+ created: new Date(comment.created_at).getTime(),
214
+ updated: Date.now()
215
+ }
216
+ comments.push(parsed)
217
+ log(`${comment.id} 解析成功`)
218
+ } catch (e) {
219
+ log(`${comment.id} 解析失败:${e.message}`)
220
+ }
221
+ }
222
+ log(`解析成功 ${comments.length} 条评论`)
223
+ return comments
224
+ },
178
225
  // Twikoo 导入
179
226
  async commentImportTwikoo (twikooDb, log) {
180
227
  let arr
package/utils/lib.js CHANGED
@@ -14,15 +14,21 @@ const pushoo = require('pushoo').default // 即时消息通知
14
14
  const tencentcloud = require('tencentcloud-sdk-nodejs') // 腾讯云 API NODEJS SDK
15
15
  const xml2js = require('xml2js') // XML 解析
16
16
 
17
+ function getDomPurify () {
18
+ // 初始化反 XSS
19
+ const window = new JSDOM('').window
20
+ const DOMPurify = createDOMPurify(window)
21
+ return DOMPurify
22
+ }
23
+
17
24
  module.exports = {
18
25
  $,
19
26
  AkismetClient,
20
27
  CryptoJS,
21
28
  FormData,
22
- JSDOM,
23
29
  axios,
24
30
  bowser,
25
- createDOMPurify,
31
+ getDomPurify,
26
32
  ipToRegion,
27
33
  marked,
28
34
  md5,