Skip to content

Commit ac01b48

Browse files
authored
Revert "Revert "新用户accessToken缺失;topic展示的边缘条件兼容"" (#117)
* Revert "Revert "新用户accessToken缺失;topic展示的边缘条件兼容"" This reverts commit 314773f. * fix lint
1 parent 314773f commit ac01b48

File tree

6 files changed

+54
-6
lines changed

6 files changed

+54
-6
lines changed

app.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
'use strict';
22

3+
const uuid = require('uuid');
4+
35
module.exports = app => {
46
if (app.config.debug) {
57
app.config.coreMiddleware.unshift('less');
@@ -46,6 +48,7 @@ module.exports = app => {
4648
existUser = new ctx.model.User();
4749
existUser.githubId = profile.id;
4850
existUser.active = true;
51+
existUser.accessToken = uuid.v4();
4952
}
5053

5154
// 用户存在,更新字段
@@ -61,7 +64,7 @@ module.exports = app => {
6164
if (ex.message.indexOf('duplicate key error') !== -1) {
6265
let err;
6366
if (ex.message.indexOf('email') !== -1) {
64-
err = new Error('您 GitHub 账号的 Email 与之前在 CNodejs 注册的用户名重复了');
67+
err = new Error('您 GitHub 账号的 Email 与之前在 CNodejs 注册的 Email 重复了');
6568
err.code = 'duplicate_email';
6669
throw err;
6770
}
@@ -123,3 +126,4 @@ module.exports = app => {
123126
return user;
124127
});
125128
};
129+

app/controller/topic.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,12 @@ class TopicController extends Controller {
3131

3232
const [ topic, author, replies ] = await service.topic.getFullTopic(topic_id);
3333

34+
if (!topic) {
35+
ctx.status = 404;
36+
ctx.message = '此话题不存在或已被删除。';
37+
return;
38+
}
39+
3440
// 增加 visit_count
3541
topic.visit_count += 1;
3642
// 写入 DB

app/router.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ module.exports = app => {
2929
router.post('/signup', createUserLimit, sign.signup);
3030
} else {
3131
// 进行github验证
32-
router.redirect('/singup', '/auth/github');
32+
router.redirect('/signup', '/passport/github');
3333
}
3434

3535
const localStrategy = app.passport.authenticate('local', {
@@ -108,3 +108,4 @@ module.exports = app => {
108108

109109
router.get('/search', search.index);
110110
};
111+

app/service/reply.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
'use strict';
22

33
const Service = require('egg').Service;
4+
45
class ReplyService extends Service {
56
/*
67
* 获取一条回复信息
@@ -52,21 +53,23 @@ class ReplyService extends Service {
5253
*/
5354
async getRepliesByTopicId(id) {
5455
const query = { topic_id: id, deleted: false };
55-
const replies = await this.ctx.model.Reply.find(query, '', {
56+
let replies = await this.ctx.model.Reply.find(query, '', {
5657
sort: 'create_at',
5758
}).exec();
5859

5960
if (replies.length === 0) {
6061
return [];
6162
}
6263

64+
replies = replies.filter(function(item) {
65+
return !item.content_is_html;
66+
});
67+
6368
return Promise.all(
6469
replies.map(async item => {
6570
const author = await this.service.user.getUserById(item.author_id);
6671
item.author = author || { _id: '' };
67-
if (item.content_is_html) {
68-
return;
69-
}
72+
7073
item.content = await this.service.at.linkUsers(item.content);
7174
return item;
7275
})

bin/generate_access_token.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
'use strict';
2+
3+
// 一次性脚本
4+
// 为所有老用户生成 accessToken
5+
6+
const uuid = require('uuid');
7+
const mongoose = require('mongoose');
8+
const config = require('../config/config.prod.js')({});
9+
const UserModel = require('../app/model/user')({
10+
mongoose,
11+
});
12+
13+
mongoose.connect(config.mongoose.url, function(err) {
14+
if (err) {
15+
console.error('connect to %s error: ', config.mongoose, err.message);
16+
process.exit(1);
17+
}
18+
});
19+
20+
async function main() {
21+
const users = await UserModel.find({
22+
accessToken: {
23+
$exists: false,
24+
},
25+
});
26+
// console.log(users);
27+
for (const user of users) {
28+
user.accessToken = uuid.v4();
29+
await user.save();
30+
}
31+
}
32+
33+
main();

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
"scripts": {
4747
"start": "egg-scripts start --daemon --title=egg-server-cnode",
4848
"stop": "egg-scripts stop --title=egg-server-cnode",
49+
"restart": "npm run stop && npm run start",
4950
"docker": "egg-scripts start --title=egg-server-cnode",
5051
"dev": "egg-bin dev",
5152
"debug": "egg-bin debug",

0 commit comments

Comments
 (0)