@waline/vercel 1.22.0 → 1.23.0

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.22.0",
3
+ "version": "1.23.0",
4
4
  "description": "vercel server for waline comment system",
5
5
  "keywords": [
6
6
  "waline",
@@ -1,6 +1,5 @@
1
1
  const BaseRest = require('./rest');
2
2
 
3
- // title, time, url, xid
4
3
  module.exports = class extends BaseRest {
5
4
  constructor(ctx) {
6
5
  super(ctx);
@@ -11,7 +10,7 @@ module.exports = class extends BaseRest {
11
10
  }
12
11
 
13
12
  async getAction() {
14
- const { path } = this.get();
13
+ const { path, type } = this.get();
15
14
 
16
15
  // path is required
17
16
  if (!Array.isArray(path) || !path.length) {
@@ -21,42 +20,69 @@ module.exports = class extends BaseRest {
21
20
  const resp = await this.modelInstance.select({ url: ['IN', path] });
22
21
 
23
22
  if (think.isEmpty(resp)) {
24
- return this.json(0);
25
- }
23
+ const data = type.reduce((o, field) => {
24
+ o[field] = 0;
25
+
26
+ return o;
27
+ }, {});
26
28
 
27
- if (path.length === 1) {
28
- return this.json(resp[0].time);
29
+ return this.json(type.length === 1 ? data[type[0]] : data);
29
30
  }
30
31
 
31
32
  const respObj = resp.reduce((o, n) => {
32
- o[n.url] = n.time;
33
+ o[n.url] = n;
33
34
 
34
35
  return o;
35
36
  }, {});
36
37
 
37
- return this.json(path.map((url) => respObj[url] || 0));
38
+ const data = [];
39
+
40
+ for (let i = 0; i < path.length; i++) {
41
+ const url = path[i];
42
+ let counters = {};
43
+
44
+ for (let j = 0; j < type.length; j++) {
45
+ const field = type[j];
46
+
47
+ counters[field] =
48
+ respObj[url] && respObj[url][field] ? respObj[url][field] : 0;
49
+ }
50
+
51
+ if (type.length === 1) {
52
+ counters = counters[type[0]];
53
+ }
54
+ data.push(counters);
55
+ }
56
+
57
+ return this.json(path.length === 1 ? data[0] : data);
38
58
  }
39
59
 
40
60
  async postAction() {
41
- const { path } = this.post();
61
+ const { path, type, action } = this.post();
42
62
  const resp = await this.modelInstance.select({ url: path });
43
63
 
44
64
  if (think.isEmpty(resp)) {
45
- const time = 1;
65
+ if (action === 'desc') {
66
+ return this.json(0);
67
+ }
68
+
69
+ const count = 1;
46
70
 
47
71
  await this.modelInstance.add(
48
- { url: path, time },
72
+ { url: path, [type]: count },
49
73
  { access: { read: true, write: true } }
50
74
  );
51
75
 
52
- return this.json(time);
76
+ return this.json(count);
53
77
  }
54
78
 
55
79
  const ret = await this.modelInstance.update(
56
- (counter) => ({ time: counter.time + 1 }),
80
+ (counter) => ({
81
+ [type]: action === 'desc' ? counter[type] - 1 : counter[type] + 1,
82
+ }),
57
83
  { objectId: ['IN', resp.map(({ objectId }) => objectId)] }
58
84
  );
59
85
 
60
- return this.json(ret[0].time);
86
+ return this.json(ret[0][type]);
61
87
  }
62
88
  };
@@ -1,29 +1,5 @@
1
- const fs = require('fs');
2
- const util = require('util');
3
1
  const BaseRest = require('./rest');
4
2
 
5
- const readFileAsync = util.promisify(fs.readFile);
6
-
7
- function formatID(data, idGenerator) {
8
- const objectIdMap = {};
9
-
10
- for (let i = 0; i < data.length; i++) {
11
- const { objectId } = data[i];
12
-
13
- objectIdMap[objectId] = idGenerator(data[i], i, data);
14
- }
15
-
16
- for (let i = 0; i < data.length; i++) {
17
- ['objectId', 'pid', 'rid']
18
- .filter((k) => data[i][k])
19
- .forEach((k) => {
20
- data[i][k] = objectIdMap[data[i][k]];
21
- });
22
- }
23
-
24
- return data;
25
- }
26
-
27
3
  module.exports = class extends BaseRest {
28
4
  async getAction() {
29
5
  const exportData = {
@@ -52,95 +28,41 @@ module.exports = class extends BaseRest {
52
28
  }
53
29
 
54
30
  async postAction() {
55
- const file = this.file('file');
56
-
57
- try {
58
- const jsonText = await readFileAsync(file.path, 'utf-8');
59
- const importData = JSON.parse(jsonText);
60
-
61
- if (!importData || importData.type !== 'waline') {
62
- return this.fail(this.locale('import data format not support!'));
63
- }
64
-
65
- const idMaps = {};
66
- const storage = this.config('storage');
67
-
68
- for (let i = 0; i < importData.tables.length; i++) {
69
- const tableName = importData.tables[i];
70
- const model = this.service(`storage/${storage}`, tableName);
71
-
72
- idMaps[tableName] = new Map();
73
- let data = importData.data[tableName];
74
-
75
- if (['postgresql', 'mysql', 'sqlite'].includes(storage)) {
76
- let i = 0;
77
-
78
- data = formatID(data, () => (i = i + 1));
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
- });
88
- }
89
-
90
- // delete all data at first
91
- await model.delete({});
92
- // then add data one by one
93
- for (let j = 0; j < data.length; j++) {
94
- const ret = await model.add(data[j]);
31
+ const { table } = this.get();
32
+ const item = this.post();
33
+ const storage = this.config('storage');
34
+ const model = this.service(`storage/${storage}`, table);
35
+
36
+ if (storage === 'leancloud' || storage === 'mysql') {
37
+ item.insertedAt && (item.insertedAt = new Date(item.insertedAt));
38
+ item.createdAt && (item.createdAt = new Date(item.createdAt));
39
+ item.updatedAt && (item.updatedAt = new Date(item.updatedAt));
40
+ }
95
41
 
96
- idMaps[tableName].set(data[j].objectId, ret.objectId);
97
- }
98
- }
42
+ delete item.objectId;
43
+ const resp = await model.add(item);
99
44
 
100
- const cmtModel = this.service(`storage/${storage}`, 'Comment');
101
- const commentData = importData.data.Comment;
102
- const willUpdateData = [];
45
+ return this.success(resp);
46
+ }
103
47
 
104
- for (let i = 0; i < commentData.length; i++) {
105
- const cmt = commentData[i];
106
- const willUpdateItem = {};
48
+ async putAction() {
49
+ const { table, objectId } = this.get();
50
+ const data = this.post();
51
+ const storage = this.config('storage');
52
+ const model = this.service(`storage/${storage}`, table);
107
53
 
108
- [
109
- { tableName: 'Comment', field: 'pid' },
110
- { tableName: 'Comment', field: 'rid' },
111
- { tableName: 'Users', field: 'user_id' },
112
- ].forEach(({ tableName, field }) => {
113
- if (!cmt[field]) {
114
- return;
115
- }
116
- const oldId = cmt[field];
117
- const newId = idMaps[tableName].get(cmt[field]);
54
+ await model.update(data, { objectId });
118
55
 
119
- if (oldId && newId && oldId !== newId) {
120
- willUpdateItem[field] = newId;
121
- }
122
- });
123
- if (!think.isEmpty(willUpdateItem)) {
124
- willUpdateData.push([
125
- willUpdateItem,
126
- { objectId: idMaps.Comment.get(cmt.objectId) },
127
- ]);
128
- }
129
- }
130
- for (let i = 0; i < willUpdateData.length; i++) {
131
- const [data, where] = willUpdateData[i];
56
+ return this.success();
57
+ }
132
58
 
133
- await cmtModel.update(data, where);
134
- }
59
+ async deleteAction() {
60
+ const { table } = this.get();
61
+ const storage = this.config('storage');
62
+ const model = this.service(`storage/${storage}`, table);
135
63
 
136
- return this.success();
137
- } catch (e) {
138
- if (think.isPrevent(e)) {
139
- return this.success();
140
- }
141
- console.log(e);
64
+ await model.delete({});
142
65
 
143
- return this.fail(e.message);
144
- }
66
+ return this.success();
145
67
  }
146
68
  };
@@ -26,7 +26,8 @@ module.exports = class extends think.Controller {
26
26
  el: '#waline',
27
27
  path: params.get('path') || '/',
28
28
  lang: params.get('lng'),
29
- serverURL: location.protocol + '//' + location.host + location.pathname.replace(/\\/+$/, '')
29
+ serverURL: location.protocol + '//' + location.host + location.pathname.replace(/\\/+$/, ''),
30
+ recaptchaV3Key: '${process.env.RECAPTCHA_V3_KEY || ''}',
30
31
  });
31
32
  </script>
32
33
  </body>
@@ -4,6 +4,24 @@ module.exports = class extends Base {
4
4
  getAction() {
5
5
  this.rules = {
6
6
  path: { array: true },
7
+ type: { array: true, default: ['time'] },
8
+ };
9
+ }
10
+
11
+ postAction() {
12
+ this.rules = {
13
+ path: {
14
+ string: true,
15
+ },
16
+ type: {
17
+ string: true,
18
+ default: 'time',
19
+ },
20
+ action: {
21
+ string: true,
22
+ in: ['inc', 'desc'],
23
+ default: 'inc',
24
+ }
7
25
  };
8
26
  }
9
27
  };
package/src/logic/db.js CHANGED
@@ -23,9 +23,51 @@ module.exports = class extends Base {
23
23
  async getAction() {}
24
24
 
25
25
  /**
26
- * @api {GET} /db import site data
26
+ * @api {POST} /db import site data
27
27
  * @apiGroup Site
28
28
  * @apiVersion 0.0.1
29
29
  */
30
- async postAction() {}
30
+ async postAction() {
31
+ this.rules = {
32
+ table: {
33
+ string: true,
34
+ required: true,
35
+ method: 'GET',
36
+ },
37
+ };
38
+ }
39
+
40
+ /**
41
+ * @api {PUT} /db update site table data
42
+ * @apiGroup Site
43
+ * @apiVersion 0.0.1
44
+ */
45
+ async putAction() {
46
+ this.rules = {
47
+ table: {
48
+ string: true,
49
+ required: true,
50
+ method: 'GET',
51
+ },
52
+ objectId: {
53
+ required: true,
54
+ method: 'GET',
55
+ },
56
+ };
57
+ }
58
+
59
+ /**
60
+ * @api {DELETE} /db clean site data
61
+ * @apiGroup Site
62
+ * @apiVersion 0.0.1
63
+ */
64
+ async deleteAction() {
65
+ this.rules = {
66
+ table: {
67
+ string: true,
68
+ required: true,
69
+ method: 'GET',
70
+ },
71
+ };
72
+ }
31
73
  };