@waline/vercel 1.3.1 → 1.5.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,11 +1,12 @@
1
1
  {
2
2
  "name": "@waline/vercel",
3
- "version": "1.3.1",
3
+ "version": "1.5.0",
4
4
  "description": "vercel server for waline comment system",
5
5
  "repository": "https://github.com/walinejs/waline",
6
6
  "license": "MIT",
7
7
  "author": "lizheming <i@imnerd.org>",
8
8
  "dependencies": {
9
+ "@byteinspire/api": "^1.0.12",
9
10
  "@cloudbase/node-sdk": "^2.7.1",
10
11
  "@koa/cors": "^3.1.0",
11
12
  "akismet": "^2.0.6",
@@ -17,6 +17,7 @@ const {
17
17
  AVATAR_PROXY,
18
18
  GITHUB_TOKEN,
19
19
  DETA_PROJECT_KEY,
20
+ INSPIRECLOUD_SERVICE_SECRET,
20
21
  OAUTH_URL,
21
22
 
22
23
  MARKDOWN_CONFIG = '{}',
@@ -62,6 +63,9 @@ if (LEAN_KEY) {
62
63
  } else if (DETA_PROJECT_KEY) {
63
64
  storage = 'deta';
64
65
  jwtKey = jwtKey || DETA_PROJECT_KEY;
66
+ } else if (INSPIRECLOUD_SERVICE_SECRET) {
67
+ storage = 'inspirecloud';
68
+ jwtKey = jwtKey || INSPIRECLOUD_SERVICE_SECRET;
65
69
  }
66
70
 
67
71
  if (think.env === 'cloudbase' && storage === 'sqlite') {
@@ -3,6 +3,8 @@ const routerREST = require('think-router-rest');
3
3
  const isDev = think.env === 'development';
4
4
  const isTcb = think.env === 'cloudbase';
5
5
  const isDeta = think.env === 'deta' || process.env.DETA_RUNTIME === 'true';
6
+ const isAliyunFC =
7
+ think.env === 'aliyun-fc' || Boolean(process.env.FC_RUNTIME_VERSION);
6
8
 
7
9
  module.exports = [
8
10
  {
@@ -15,7 +17,7 @@ module.exports = [
15
17
  options: {
16
18
  logRequest: isDev,
17
19
  sendResponseTime: isDev,
18
- requestTimeoutCallback: isTcb || isDeta ? false : () => {},
20
+ requestTimeoutCallback: isTcb || isDeta || isAliyunFC ? false : () => {},
19
21
  },
20
22
  },
21
23
 
@@ -4,6 +4,7 @@ const BaseRest = require('./rest');
4
4
  const akismet = require('../service/akismet');
5
5
  const { getMarkdownParser } = require('../service/markdown');
6
6
 
7
+ const markdownParser = getMarkdownParser();
7
8
  async function formatCmt(
8
9
  { ua, user_id, ...comment },
9
10
  users = [],
@@ -39,6 +40,7 @@ async function formatCmt(
39
40
  comment.mail ? comment.mail.toLowerCase() : comment.mail
40
41
  );
41
42
 
43
+ comment.comment = markdownParser(comment.comment);
42
44
  return comment;
43
45
  }
44
46
 
@@ -49,8 +51,6 @@ module.exports = class extends BaseRest {
49
51
  `storage/${this.config('storage')}`,
50
52
  'Comment'
51
53
  );
52
-
53
- this.parser = getMarkdownParser();
54
54
  }
55
55
 
56
56
  async getAction() {
@@ -248,17 +248,14 @@ module.exports = class extends BaseRest {
248
248
  rid,
249
249
  ua,
250
250
  url,
251
+ comment,
251
252
  ip: this.ctx.ip,
252
253
  insertedAt: new Date(),
253
- comment: this.parser(comment),
254
254
  user_id: this.ctx.state.userInfo.objectId,
255
255
  };
256
256
 
257
257
  if (pid) {
258
- data.comment = data.comment.replace(
259
- '<p>',
260
- `<p><a class="at" href="#${pid}">@${at}</a>: `
261
- );
258
+ data.comment = `[@${at}](#${pid}): ` + data.comment;
262
259
  }
263
260
 
264
261
  think.logger.debug('Post Comment initial Data:', data);
@@ -24,7 +24,10 @@ module.exports = class extends think.Controller {
24
24
  type,
25
25
  })}`;
26
26
  return this.redirect(
27
- `${oauthUrl}/${type}?${qs.stringify({ redirect: redirectUrl })}`
27
+ `${oauthUrl}/${type}?${qs.stringify({
28
+ redirect: redirectUrl,
29
+ state: this.ctx.state.token,
30
+ })}`
28
31
  );
29
32
  }
30
33
 
@@ -38,7 +41,10 @@ module.exports = class extends think.Controller {
38
41
  redirect,
39
42
  type,
40
43
  })}`;
41
- params.state = qs.stringify({ redirect: redirectUrl, state: '' });
44
+ params.state = qs.stringify({
45
+ redirect: redirectUrl,
46
+ state: this.ctx.state.token || '',
47
+ });
42
48
  }
43
49
 
44
50
  const user = await request({
@@ -83,7 +89,7 @@ module.exports = class extends think.Controller {
83
89
  objectId: current.objectId,
84
90
  });
85
91
 
86
- return this.success();
92
+ return this.redirect('/ui/profile');
87
93
  }
88
94
 
89
95
  const userByEmail = await this.modelInstance.select({ email: user.email });
@@ -78,7 +78,7 @@ module.exports = class extends BaseRest {
78
78
  }
79
79
 
80
80
  async putAction() {
81
- const { display_name, url, password, github } = this.post();
81
+ const { display_name, url, password } = this.post();
82
82
  const { objectId } = this.ctx.state.userInfo;
83
83
 
84
84
  const updateData = {};
@@ -95,9 +95,13 @@ module.exports = class extends BaseRest {
95
95
  updateData.password = new PasswordHash().hashPassword(password);
96
96
  }
97
97
 
98
- if (think.isString(github)) {
99
- updateData.github = github;
100
- }
98
+ const socials = ['github', 'twitter', 'facebook', 'google', 'weibo', 'qq'];
99
+ socials.forEach((social) => {
100
+ const nextSocial = this.post(social);
101
+ if (think.isString(nextSocial)) {
102
+ updateData[social] = nextSocial;
103
+ }
104
+ });
101
105
 
102
106
  if (think.isEmpty(updateData)) {
103
107
  return this.success();
package/src/logic/base.js CHANGED
@@ -79,5 +79,6 @@ module.exports = class extends think.Logic {
79
79
  userInfo.avatar = avatarUrl;
80
80
  userInfo.mailMd5 = helper.md5(userInfo.email);
81
81
  this.ctx.state.userInfo = userInfo;
82
+ this.ctx.state.token = token;
82
83
  }
83
84
  };
@@ -0,0 +1,152 @@
1
+ const inspirecloud = require('@byteinspire/api');
2
+ const Base = require('./base');
3
+
4
+ module.exports = class extends Base {
5
+ constructor(tableName) {
6
+ super(tableName);
7
+ this.db = inspirecloud.db;
8
+ }
9
+
10
+ where(where) {
11
+ if (think.isEmpty(where)) {
12
+ return;
13
+ }
14
+
15
+ const _where = {};
16
+ const parseKey = (k) => (k === 'objectId' ? '_id' : k);
17
+ for (const k in where) {
18
+ if (think.isString(where[k])) {
19
+ _where[parseKey(k)] =
20
+ k === 'objectId' ? this.db.ObjectId(where[k]) : where[k];
21
+ continue;
22
+ }
23
+ if (where[k] === undefined) {
24
+ _where[parseKey(k)] = undefined;
25
+ }
26
+ if (Array.isArray(where[k])) {
27
+ if (where[k][0]) {
28
+ const handler = where[k][0].toUpperCase();
29
+ switch (handler) {
30
+ case 'IN':
31
+ _where[parseKey(k)] = this.db.in(
32
+ k === 'objectId'
33
+ ? where[k][1].map(this.db.ObjectId)
34
+ : where[k][1]
35
+ );
36
+ break;
37
+ case 'NOT IN':
38
+ _where[parseKey(k)] = this.db.nin(
39
+ k === 'objectId'
40
+ ? where[k][1].map(this.db.ObjectId)
41
+ : where[k][1]
42
+ );
43
+ break;
44
+ case 'LIKE': {
45
+ const first = where[k][1][0];
46
+ const last = where[k][1].slice(-1);
47
+ let reg;
48
+ if (first === '%' && last === '%') {
49
+ reg = new RegExp(where[k][1].slice(1, -1));
50
+ } else if (first === '%') {
51
+ reg = new RegExp(where[k][1].slice(1) + '$');
52
+ } else if (last === '%') {
53
+ reg = new RegExp('^' + where[k][1].slice(0, -1));
54
+ }
55
+ _where[parseKey(k)] = this.db.regex(reg);
56
+ break;
57
+ }
58
+ case '!=':
59
+ _where[parseKey(k)] = this.db.ne(where[k]);
60
+ break;
61
+ case '>':
62
+ _where[parseKey(k)] = this.db.gt(where[k]);
63
+ break;
64
+ }
65
+ }
66
+ }
67
+ }
68
+
69
+ return _where;
70
+ }
71
+
72
+ async _select(where, { desc, limit, offset, field } = {}) {
73
+ const instance = this.db.table(this.tableName);
74
+ const query = instance.where(this.where(where));
75
+
76
+ if (desc) {
77
+ query.sort({ [desc]: -1 });
78
+ }
79
+ if (limit) {
80
+ query.limit(limit);
81
+ }
82
+ if (offset) {
83
+ query.skip(offset);
84
+ }
85
+ if (field) {
86
+ const _field = {};
87
+ field.forEach((f) => {
88
+ _field[f] = 1;
89
+ });
90
+ query.projection(_field);
91
+ }
92
+
93
+ const data = await query.find();
94
+ data.forEach((item) => {
95
+ item.objectId = item._id.toString();
96
+ delete item._id;
97
+ });
98
+ return data;
99
+ }
100
+
101
+ async select(where, options = {}) {
102
+ let data = [];
103
+ let ret = [];
104
+ do {
105
+ options.offset = (options.offset || 0) + data.length;
106
+ ret = await this._select(where, options);
107
+ data = data.concat(ret);
108
+ } while (ret.length === 1000);
109
+
110
+ return data;
111
+ }
112
+
113
+ async count(where = {}) {
114
+ const instance = this.db.table(this.tableName);
115
+ const query = instance.where(this.where(where));
116
+
117
+ return query.count();
118
+ }
119
+
120
+ async add(data) {
121
+ const instance = this.db.table(this.tableName);
122
+ const tableData = instance.create(data);
123
+ await instance.save(tableData);
124
+
125
+ tableData.objectId = tableData._id.toString();
126
+ delete tableData._id;
127
+ return tableData;
128
+ }
129
+
130
+ async update(data, where) {
131
+ const instance = this.db.table(this.tableName);
132
+ const query = instance.where(this.where(where));
133
+ const items = await query.find();
134
+
135
+ return Promise.all(
136
+ items.map(async (item) => {
137
+ const updateData = typeof data === 'function' ? data(item) : data;
138
+ for (const k in updateData) {
139
+ item[k] = updateData[k];
140
+ }
141
+ await instance.save(item);
142
+ return item;
143
+ })
144
+ );
145
+ }
146
+
147
+ async delete(where) {
148
+ const instance = this.db.table(this.tableName);
149
+ const query = instance.where(this.where(where));
150
+ return query.delete();
151
+ }
152
+ };