@waline/vercel 1.19.1 → 1.19.4

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@waline/vercel",
3
- "version": "1.19.1",
3
+ "version": "1.19.4",
4
4
  "description": "vercel server for waline comment system",
5
5
  "keywords": [
6
6
  "waline",
@@ -16,34 +16,34 @@
16
16
  "author": "lizheming <i@imnerd.org>",
17
17
  "dependencies": {
18
18
  "@cloudbase/node-sdk": "2.9.1",
19
- "@koa/cors": "3.3.0",
19
+ "@koa/cors": "3.4.1",
20
20
  "akismet": "2.0.7",
21
21
  "deta": "1.1.0",
22
- "dompurify": "2.3.10",
22
+ "dompurify": "2.4.0",
23
23
  "dy-node-ip2region": "1.0.1",
24
24
  "fast-csv": "4.3.6",
25
25
  "form-data": "4.0.0",
26
26
  "jsdom": "20.0.0",
27
27
  "jsonwebtoken": "8.5.1",
28
- "katex": "0.16.0",
29
- "leancloud-storage": "4.13.1",
28
+ "katex": "0.16.2",
29
+ "leancloud-storage": "4.13.2",
30
30
  "markdown-it": "13.0.1",
31
31
  "markdown-it-emoji": "2.0.2",
32
32
  "markdown-it-sub": "1.0.0",
33
33
  "markdown-it-sup": "1.0.0",
34
34
  "mathjax-full": "3.2.2",
35
- "node-fetch": "2",
36
- "nodemailer": "6.7.7",
35
+ "node-fetch": "2.6.7",
36
+ "nodemailer": "6.7.8",
37
37
  "nunjucks": "3.2.3",
38
38
  "phpass": "0.1.1",
39
- "prismjs": "1.28.0",
39
+ "prismjs": "1.29.0",
40
40
  "speakeasy": "2.0.0",
41
- "think-helper": "1.1.3",
41
+ "think-helper": "1.1.4",
42
42
  "think-logger3": "1.3.1",
43
43
  "think-model": "1.5.4",
44
44
  "think-model-mysql": "1.1.7",
45
45
  "think-model-postgresql": "1.1.7",
46
- "think-model-sqlite": "1.2.3",
46
+ "think-model-sqlite": "1.3.0",
47
47
  "think-mongo": "2.2.1",
48
48
  "think-router-rest": "1.0.5",
49
49
  "thinkjs": "3.2.14",
@@ -54,7 +54,7 @@ if (LEAN_KEY) {
54
54
  storage = 'postgresql';
55
55
  jwtKey = jwtKey || PG_PASSWORD;
56
56
  } else if (SQLITE_PATH) {
57
- storage = 'mysql';
57
+ storage = 'sqlite';
58
58
  } else if (MYSQL_DB) {
59
59
  storage = 'mysql';
60
60
  jwtKey = jwtKey || MYSQL_PASSWORD;
@@ -145,7 +145,8 @@ module.exports = class extends BaseRest {
145
145
 
146
146
  case 'count': {
147
147
  const { url } = this.get();
148
- const where = { url: ['IN', url] };
148
+ const where =
149
+ Array.isArray(url) && url.length ? { url: ['IN', url] } : {};
149
150
 
150
151
  if (think.isEmpty(userInfo) || this.config('storage') === 'deta') {
151
152
  where.status = ['NOT IN', ['waiting', 'spam']];
@@ -156,12 +157,19 @@ module.exports = class extends BaseRest {
156
157
  user_id: userInfo.objectId,
157
158
  };
158
159
  }
159
- const data = await this.modelInstance.select(where, { field: ['url'] });
160
- const counts = url.map(
161
- (u) => data.filter(({ url }) => url === u).length
162
- );
163
160
 
164
- return this.json(counts.length === 1 ? counts[0] : counts);
161
+ if (Array.isArray(url) && url.length > 1) {
162
+ const data = await this.modelInstance.select(where, {
163
+ field: ['url'],
164
+ });
165
+
166
+ return this.json(
167
+ url.map((u) => data.filter(({ url }) => url === u).length)
168
+ );
169
+ }
170
+ const data = await this.modelInstance.count(where);
171
+
172
+ return this.json(data);
165
173
  }
166
174
 
167
175
  case 'list': {
@@ -567,10 +575,20 @@ module.exports = class extends BaseRest {
567
575
  think.logger.debug(`Comment have been added to storage.`);
568
576
 
569
577
  let parentComment;
578
+ let parentUser;
570
579
 
571
580
  if (pid) {
572
581
  parentComment = await this.modelInstance.select({ objectId: pid });
573
582
  parentComment = parentComment[0];
583
+ if (parentComment.user_id) {
584
+ parentUser = await this.service(
585
+ `storage/${this.config('storage')}`,
586
+ 'User'
587
+ ).select({
588
+ objectId: parentComment.user_id,
589
+ });
590
+ parentUser = parentUser[0];
591
+ }
574
592
  }
575
593
 
576
594
  await this.ctx.webhook('new_comment', {
@@ -578,14 +596,27 @@ module.exports = class extends BaseRest {
578
596
  reply: parentComment,
579
597
  });
580
598
 
599
+ const cmtReturn = await formatCmt(
600
+ resp,
601
+ [userInfo],
602
+ this.config(),
603
+ userInfo
604
+ );
605
+ const parentReturn = parentComment
606
+ ? await formatCmt(
607
+ parentComment,
608
+ parentUser ? [parentUser] : [],
609
+ this.config(),
610
+ userInfo
611
+ )
612
+ : undefined;
613
+
581
614
  if (comment.status !== 'spam') {
582
615
  const notify = this.service('notify');
583
616
 
584
617
  await notify.run(
585
- { ...resp, comment: markdownParser(resp.comment), rawComment: comment },
586
- parentComment
587
- ? { ...parentComment, comment: markdownParser(parentComment.comment) }
588
- : undefined
618
+ { ...cmtReturn, mail: resp.mail, rawComment: comment },
619
+ parentReturn ? { ...parentReturn, mail: parentComment.mail } : undefined
589
620
  );
590
621
  }
591
622
 
@@ -642,17 +673,53 @@ module.exports = class extends BaseRest {
642
673
  data.status === 'approved' &&
643
674
  oldData.pid
644
675
  ) {
676
+ let cmtUser;
677
+
678
+ if (newData.user_id) {
679
+ cmtUser = await this.service(
680
+ `storage/${this.config('storage')}`,
681
+ 'User'
682
+ ).select({
683
+ objectId: newData.user_id,
684
+ });
685
+ cmtUser = cmtUser[0];
686
+ }
687
+
645
688
  let pComment = await this.modelInstance.select({
646
689
  objectId: oldData.pid,
647
690
  });
648
691
 
649
692
  pComment = pComment[0];
650
693
 
694
+ let pUser;
695
+
696
+ if (pComment.user_id) {
697
+ pUser = await this.service(
698
+ `storage/${this.config('storage')}`,
699
+ 'User'
700
+ ).select({
701
+ objectId: pComment.user_id,
702
+ });
703
+ pUser = pUser[0];
704
+ }
705
+
651
706
  const notify = this.service('notify');
707
+ const cmtReturn = await formatCmt(
708
+ newData,
709
+ cmtUser ? [cmtUser] : [],
710
+ this.config(),
711
+ userInfo
712
+ );
713
+ const pcmtReturn = await formatCmt(
714
+ pComment,
715
+ pUser ? [pUser] : [],
716
+ this.config(),
717
+ userInfo
718
+ );
652
719
 
653
720
  await notify.run(
654
- { ...newData, comment: markdownParser(newData.comment) },
655
- { ...pComment, comment: markdownParser(pComment.comment) },
721
+ { ...cmtReturn, mail: newData.mail },
722
+ { ...pcmtReturn, mail: pComment.mail },
656
723
  true
657
724
  );
658
725
  }
@@ -6,8 +6,10 @@ const readFileAsync = util.promisify(fs.readFile);
6
6
 
7
7
  function formatID(data, idGenerator) {
8
8
  const objectIdMap = {};
9
+
9
10
  for (let i = 0; i < data.length; i++) {
10
11
  const { objectId } = data[i];
12
+
11
13
  objectIdMap[objectId] = idGenerator(data[i], i, data);
12
14
  }
13
15
 
@@ -42,6 +44,7 @@ module.exports = class extends BaseRest {
42
44
  const model = this.service(`storage/${storage}`, tableName);
43
45
 
44
46
  const data = await model.select({});
47
+
45
48
  exportData.data[tableName] = data;
46
49
  }
47
50
 
@@ -68,15 +71,20 @@ module.exports = class extends BaseRest {
68
71
 
69
72
  idMaps[tableName] = new Map();
70
73
  let data = importData.data[tableName];
74
+
71
75
  if (['postgresql', 'mysql', 'sqlite'].includes(storage)) {
72
76
  let i = 0;
77
+
73
78
  data = formatID(data, () => (i = i + 1));
74
- } else if (storage === 'leancloud') {
75
- data
76
- .filter(({ insertedAt }) => insertedAt)
77
- .forEach((item) => {
78
- item.insertedAt = new Date(item.insertedAt);
79
- });
79
+ await model.setSeqId(1);
80
+ }
81
+
82
+ if (storage === 'leancloud' || storage === 'mysql') {
83
+ data.forEach((item) => {
84
+ item.insertedAt && (item.insertedAt = new Date(item.insertedAt));
85
+ item.createdAt && (item.createdAt = new Date(item.createdAt));
86
+ item.updatedAt && (item.updatedAt = new Date(item.updatedAt));
87
+ });
80
88
  }
81
89
 
82
90
  // delete all data at first
@@ -108,7 +116,7 @@ module.exports = class extends BaseRest {
108
116
  const oldId = cmt[field];
109
117
  const newId = idMaps[tableName].get(cmt[field]);
110
118
 
111
- if (oldId !== newId) {
119
+ if (oldId && newId && oldId !== newId) {
112
120
  willUpdateItem[field] = newId;
113
121
  }
114
122
  });
@@ -131,6 +139,7 @@ module.exports = class extends BaseRest {
131
139
  return this.success();
132
140
  }
133
141
  console.log(e);
142
+
134
143
  return this.fail(e.message);
135
144
  }
136
145
  }
@@ -89,7 +89,7 @@ module.exports = class extends Base {
89
89
  * @apiGroup Comment
90
90
  * @apiVersion 0.0.1
91
91
  *
92
- * @apiParam {String} url a array string join by comma just like `a` or `a,b`
92
+ * @apiParam {String} url a array string join by comma just like `a` or `a,b`, return site comment count if url empty
93
93
  *
94
94
  * @apiSuccessExample {Number} Single Path Response:
95
95
  * 300
@@ -131,7 +131,6 @@ module.exports = class extends Base {
131
131
  this.rules = {
132
132
  url: {
133
133
  array: true,
134
- required: true,
135
134
  },
136
135
  };
137
136
  break;
@@ -144,12 +144,11 @@ module.exports = class extends think.Service {
144
144
  querystring.set('corpsecret', `${QYWX_AM_AY[1]}`);
145
145
 
146
146
  const { access_token } = await fetch(
147
- `https://qyapi.weixin.qq.com/cgi-bin/gettoken`,
147
+ `https://qyapi.weixin.qq.com/cgi-bin/gettoken?${querystring.toString()}`,
148
148
  {
149
149
  headers: {
150
150
  'content-type': 'application/json',
151
151
  },
152
- body: querystring,
153
152
  }
154
153
  ).then((resp) => resp.json());
155
154
 
@@ -392,6 +391,8 @@ module.exports = class extends think.Service {
392
391
  const isReplyAuthor = AUTHOR
393
392
  ? parent && parent.mail.toLowerCase() === AUTHOR.toLowerCase()
394
393
  : false;
394
+ const isCommentSelf =
395
+ parent && parent.mail.toLowerCase() === comment.mail.toLowerCase();
395
396
 
396
397
  const title = mailSubjectAdmin || '{{site.name | safe}} 上有新评论了';
397
398
  const content =
@@ -436,7 +437,12 @@ module.exports = class extends think.Service {
436
437
  );
437
438
  const fakeMail = new RegExp(`@(${disallowList.join('|')})$`, 'i');
438
439
 
439
- if (parent && !fakeMail.test(parent.mail) && comment.status !== 'waiting') {
440
+ if (
441
+ parent &&
442
+ !fakeMail.test(parent.mail) &&
443
+ !isCommentSelf &&
444
+ comment.status !== 'waiting'
445
+ ) {
440
446
  mailList.push({
441
447
  to: parent.mail,
442
448
  title:
@@ -9,7 +9,7 @@ module.exports = class extends Base {
9
9
  }
10
10
 
11
11
  for (const k in filter) {
12
- if (k === 'objectId') {
12
+ if (k === 'objectId' || k === 'objectid') {
13
13
  where.id = filter[k];
14
14
  continue;
15
15
  }
@@ -78,6 +78,11 @@ module.exports = class extends Base {
78
78
  data.id = data.objectId;
79
79
  delete data.objectId;
80
80
  }
81
+ const date = new Date();
82
+ if (!data.createdAt)
83
+ data.createdAt = date;
84
+ if (!data.updatedAt)
85
+ data.updatedAt = date;
81
86
 
82
87
  const instance = this.model(this.tableName);
83
88
  const id = await instance.add(data);
@@ -108,4 +113,12 @@ module.exports = class extends Base {
108
113
 
109
114
  return instance.where(this.parseWhere(where)).delete();
110
115
  }
116
+
117
+ async setSeqId(id) {
118
+ const instance = this.model(this.tableName);
119
+
120
+ return instance.query(
121
+ `ALTER TABLE ${instance.tableName} AUTO_INCREMENT = ${id};`
122
+ );
123
+ }
111
124
  };
@@ -73,4 +73,12 @@ module.exports = class extends MySQL {
73
73
 
74
74
  return result;
75
75
  }
76
+
77
+ async setSeqId(id) {
78
+ const instance = this.model(this.tableName);
79
+
80
+ return instance.query(
81
+ `ALTER SEQUENCE ${instance.tableName}_seq RESTART WITH ${id};`
82
+ );
83
+ }
76
84
  };
@@ -0,0 +1,11 @@
1
+ const MySQL = require('./mysql');
2
+
3
+ module.exports = class extends MySQL {
4
+ async setSeqId(id) {
5
+ const instance = this.model(this.tableName);
6
+
7
+ return instance.query(
8
+ `UPDATE SQLITE_SEQUENCE SET SEQ=${id} WHERE NAME='${instance.tableName}';`
9
+ );
10
+ }
11
+ };