@waline/vercel 1.17.0 → 1.17.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/package.json +2 -2
- package/src/controller/comment.js +1 -1
- package/src/controller/token.js +11 -6
- package/src/controller/user.js +1 -1
- package/src/extend/think.js +9 -1
- package/src/logic/base.js +1 -0
- package/src/service/storage/github.js +1 -0
- package/src/service/storage/leancloud.js +127 -4
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@waline/vercel",
|
|
3
|
-
"version": "1.17.
|
|
3
|
+
"version": "1.17.3",
|
|
4
4
|
"description": "vercel server for waline comment system",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"waline",
|
|
@@ -31,7 +31,7 @@
|
|
|
31
31
|
"markdown-it-sub": "^1.0.0",
|
|
32
32
|
"markdown-it-sup": "^1.0.0",
|
|
33
33
|
"mathjax-full": "^3.2.0",
|
|
34
|
-
"nodemailer": "^6.7.
|
|
34
|
+
"nodemailer": "^6.7.5",
|
|
35
35
|
"nunjucks": "^3.2.3",
|
|
36
36
|
"phpass": "^0.1.1",
|
|
37
37
|
"prismjs": "^1.28.0",
|
|
@@ -365,7 +365,7 @@ module.exports = class extends BaseRest {
|
|
|
365
365
|
});
|
|
366
366
|
comments.forEach((cmt) => {
|
|
367
367
|
const countItem = (counts || []).find(({ mail, user_id }) => {
|
|
368
|
-
if (user_id) {
|
|
368
|
+
if (cmt.user_id) {
|
|
369
369
|
return user_id === cmt.user_id;
|
|
370
370
|
}
|
|
371
371
|
return mail === cmt.mail;
|
package/src/controller/token.js
CHANGED
|
@@ -47,13 +47,18 @@ module.exports = class extends BaseRest {
|
|
|
47
47
|
}
|
|
48
48
|
}
|
|
49
49
|
|
|
50
|
-
let
|
|
51
|
-
|
|
52
|
-
avatar
|
|
53
|
-
|
|
50
|
+
let avatarUrl = user[0].avatar
|
|
51
|
+
? user[0].avatar
|
|
52
|
+
: await think.service('avatar').stringify({
|
|
53
|
+
mail: user[0].email,
|
|
54
|
+
nick: user[0].display_name,
|
|
55
|
+
link: user[0].url,
|
|
56
|
+
});
|
|
57
|
+
const { avatarProxy } = think.config();
|
|
58
|
+
if (avatarProxy) {
|
|
59
|
+
avatarUrl = avatarProxy + '?url=' + encodeURIComponent(avatarUrl);
|
|
54
60
|
}
|
|
55
|
-
user[0].avatar =
|
|
56
|
-
|
|
61
|
+
user[0].avatar = avatarUrl;
|
|
57
62
|
return this.success({
|
|
58
63
|
...user[0],
|
|
59
64
|
password: null,
|
package/src/controller/user.js
CHANGED
package/src/extend/think.js
CHANGED
|
@@ -24,6 +24,10 @@ module.exports = {
|
|
|
24
24
|
},
|
|
25
25
|
promiseAllQueue(promises, taskNum) {
|
|
26
26
|
return new Promise((resolve, reject) => {
|
|
27
|
+
if (!promises.length) {
|
|
28
|
+
return resolve();
|
|
29
|
+
}
|
|
30
|
+
|
|
27
31
|
const ret = [];
|
|
28
32
|
let index = 0;
|
|
29
33
|
let count = 0;
|
|
@@ -55,7 +59,11 @@ module.exports = {
|
|
|
55
59
|
|
|
56
60
|
try {
|
|
57
61
|
const search = helper.promisify(regionSearch.btreeSearch, regionSearch);
|
|
58
|
-
const
|
|
62
|
+
const result = await search(ip);
|
|
63
|
+
if (!result) {
|
|
64
|
+
return '';
|
|
65
|
+
}
|
|
66
|
+
const { region } = result;
|
|
59
67
|
const [, , province, city, isp] = region.split('|');
|
|
60
68
|
const address = Array.from(new Set([province, city, isp]));
|
|
61
69
|
return address.slice(0, depth).join(' ');
|
package/src/logic/base.js
CHANGED
|
@@ -126,6 +126,95 @@ module.exports = class extends Base {
|
|
|
126
126
|
return data;
|
|
127
127
|
}
|
|
128
128
|
|
|
129
|
+
async _getCmtGroupByMailUserIdCache(key, where) {
|
|
130
|
+
if (this.tableName !== 'Comment' || key !== 'user_id_mail') {
|
|
131
|
+
return [];
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
const cacheTableName = `cache_group_count_${key}`;
|
|
135
|
+
const currentTableName = this.tableName;
|
|
136
|
+
this.tableName = cacheTableName;
|
|
137
|
+
const cacheData = await this.select({ _complex: where._complex });
|
|
138
|
+
this.tableName = currentTableName;
|
|
139
|
+
return cacheData;
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
async _setCmtGroupByMailUserIdCache(key, data) {
|
|
143
|
+
if (this.tableName !== 'Comment' || key !== 'user_id_mail') {
|
|
144
|
+
return;
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
const cacheTableName = `cache_group_count_${key}`;
|
|
148
|
+
const currentTableName = this.tableName;
|
|
149
|
+
this.tableName = cacheTableName;
|
|
150
|
+
|
|
151
|
+
await think.promiseAllQueue(
|
|
152
|
+
data.map((item) => {
|
|
153
|
+
if (item.user_id && !think.isString(item.user_id)) {
|
|
154
|
+
item.user_id = item.user_id.toString();
|
|
155
|
+
}
|
|
156
|
+
return this.add(item);
|
|
157
|
+
}),
|
|
158
|
+
1
|
|
159
|
+
);
|
|
160
|
+
this.tableName = currentTableName;
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
async _updateCmtGroupByMailUserIdCache(data, method) {
|
|
164
|
+
if (this.tableName !== 'Comment') {
|
|
165
|
+
return;
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
if (!data.user_id && !data.mail) {
|
|
169
|
+
return;
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
const cacheTableName = `cache_group_count_user_id_mail`;
|
|
173
|
+
const cacheData = await this.select({
|
|
174
|
+
_complex: {
|
|
175
|
+
_logic: 'or',
|
|
176
|
+
user_id: think.isObject(data.user_id)
|
|
177
|
+
? data.user_id.toString()
|
|
178
|
+
: data.user_id,
|
|
179
|
+
mail: data.mail,
|
|
180
|
+
},
|
|
181
|
+
});
|
|
182
|
+
if (think.isEmpty(data)) {
|
|
183
|
+
return;
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
let count = cacheData[0].count;
|
|
187
|
+
switch (method) {
|
|
188
|
+
case 'add':
|
|
189
|
+
if (data.status === 'approved') {
|
|
190
|
+
count += 1;
|
|
191
|
+
}
|
|
192
|
+
break;
|
|
193
|
+
case 'udpate_status':
|
|
194
|
+
if (data.status === 'approved') {
|
|
195
|
+
count += 1;
|
|
196
|
+
} else {
|
|
197
|
+
count -= 1;
|
|
198
|
+
}
|
|
199
|
+
break;
|
|
200
|
+
case 'delete':
|
|
201
|
+
count -= 1;
|
|
202
|
+
break;
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
const currentTableName = this.tableName;
|
|
206
|
+
this.tableName = cacheTableName;
|
|
207
|
+
await this.update({ count }, { objectId: cacheData[0].objectId }).catch(
|
|
208
|
+
(e) => {
|
|
209
|
+
if (e.code === 101) {
|
|
210
|
+
return;
|
|
211
|
+
}
|
|
212
|
+
throw e;
|
|
213
|
+
}
|
|
214
|
+
);
|
|
215
|
+
this.tableName = currentTableName;
|
|
216
|
+
}
|
|
217
|
+
|
|
129
218
|
async count(where = {}, options = {}) {
|
|
130
219
|
const instance = this.where(this.tableName, where);
|
|
131
220
|
if (!options.group) {
|
|
@@ -137,7 +226,19 @@ module.exports = class extends Base {
|
|
|
137
226
|
});
|
|
138
227
|
}
|
|
139
228
|
|
|
140
|
-
//
|
|
229
|
+
// get group count cache by group field where data
|
|
230
|
+
const cacheData = await this._getCmtGroupByMailUserIdCache(
|
|
231
|
+
options.group.join('_'),
|
|
232
|
+
where
|
|
233
|
+
);
|
|
234
|
+
const cacheDataMap = {};
|
|
235
|
+
for (let i = 0; i < cacheData.length; i++) {
|
|
236
|
+
const key = options.group
|
|
237
|
+
.map((item) => cacheData[i][item] || undefined)
|
|
238
|
+
.join('_');
|
|
239
|
+
cacheDataMap[key] = cacheData[i];
|
|
240
|
+
}
|
|
241
|
+
|
|
141
242
|
const counts = [];
|
|
142
243
|
const countsPromise = [];
|
|
143
244
|
for (let i = 0; i < options.group.length; i++) {
|
|
@@ -148,10 +249,23 @@ module.exports = class extends Base {
|
|
|
148
249
|
|
|
149
250
|
const groupFlatValue = {};
|
|
150
251
|
options.group.slice(0, i).forEach((group) => {
|
|
151
|
-
groupFlatValue[group] =
|
|
252
|
+
groupFlatValue[group] = undefined;
|
|
152
253
|
});
|
|
153
254
|
|
|
154
255
|
for (let j = 0; j < where._complex[groupName][1].length; j++) {
|
|
256
|
+
const cacheKey = options.group
|
|
257
|
+
.map(
|
|
258
|
+
(item) =>
|
|
259
|
+
({
|
|
260
|
+
...groupFlatValue,
|
|
261
|
+
[groupName]: where._complex[groupName][1][j],
|
|
262
|
+
}[item] || undefined)
|
|
263
|
+
)
|
|
264
|
+
.join('_');
|
|
265
|
+
if (cacheDataMap[cacheKey]) {
|
|
266
|
+
continue;
|
|
267
|
+
}
|
|
268
|
+
|
|
155
269
|
const groupWhere = {
|
|
156
270
|
...where,
|
|
157
271
|
...groupFlatValue,
|
|
@@ -172,8 +286,10 @@ module.exports = class extends Base {
|
|
|
172
286
|
}
|
|
173
287
|
}
|
|
174
288
|
|
|
175
|
-
await think.promiseAllQueue(countsPromise,
|
|
176
|
-
|
|
289
|
+
await think.promiseAllQueue(countsPromise, 1);
|
|
290
|
+
// cache data
|
|
291
|
+
await this._setCmtGroupByMailUserIdCache(options.group.join('_'), counts);
|
|
292
|
+
return [...cacheData, ...counts];
|
|
177
293
|
}
|
|
178
294
|
|
|
179
295
|
async add(
|
|
@@ -190,6 +306,7 @@ module.exports = class extends Base {
|
|
|
190
306
|
instance.setACL(acl);
|
|
191
307
|
|
|
192
308
|
const resp = await instance.save();
|
|
309
|
+
await this._updateCmtGroupByMailUserIdCache(data, 'add');
|
|
193
310
|
return resp.toJSON();
|
|
194
311
|
}
|
|
195
312
|
|
|
@@ -199,11 +316,16 @@ module.exports = class extends Base {
|
|
|
199
316
|
|
|
200
317
|
return Promise.all(
|
|
201
318
|
ret.map(async (item) => {
|
|
319
|
+
const _oldStatus = item.get('status');
|
|
202
320
|
if (think.isFunction(data)) {
|
|
203
321
|
item.set(data(item.toJSON()));
|
|
204
322
|
} else {
|
|
205
323
|
item.set(data);
|
|
206
324
|
}
|
|
325
|
+
const _newStatus = item.get('status');
|
|
326
|
+
if (_newStatus && _oldStatus !== _newStatus) {
|
|
327
|
+
await this._updateCmtGroupByMailUserIdCache(data, 'update_status');
|
|
328
|
+
}
|
|
207
329
|
|
|
208
330
|
const resp = await item.save();
|
|
209
331
|
return resp.toJSON();
|
|
@@ -214,6 +336,7 @@ module.exports = class extends Base {
|
|
|
214
336
|
async delete(where) {
|
|
215
337
|
const instance = this.where(this.tableName, where);
|
|
216
338
|
const data = await instance.find();
|
|
339
|
+
await this._updateCmtGroupByMailUserIdCache(data, 'delete');
|
|
217
340
|
|
|
218
341
|
return AV.Object.destroyAll(data);
|
|
219
342
|
}
|