Skip to content

Commit cf0f5ed

Browse files
committed
新用户accessToken缺失;topic展示的边缘条件兼容
1 parent 01fe122 commit cf0f5ed

File tree

6 files changed

+50
-7
lines changed

6 files changed

+50
-7
lines changed

app.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
'use strict';
2+
var uuid = require('node-uuid');
23

34
module.exports = app => {
45
if (app.config.debug) {
@@ -46,6 +47,7 @@ module.exports = app => {
4647
existUser = new ctx.model.User();
4748
existUser.githubId = profile.id;
4849
existUser.active = true;
50+
existUser.accessToken = uuid.v4();
4951
}
5052

5153
// 用户存在,更新字段
@@ -61,7 +63,7 @@ module.exports = app => {
6163
if (ex.message.indexOf('duplicate key error') !== -1) {
6264
let err;
6365
if (ex.message.indexOf('email') !== -1) {
64-
err = new Error('您 GitHub 账号的 Email 与之前在 CNodejs 注册的用户名重复了');
66+
err = new Error('您 GitHub 账号的 Email 与之前在 CNodejs 注册的 Email 重复了');
6567
err.code = 'duplicate_email';
6668
throw err;
6769
}
@@ -123,3 +125,4 @@ module.exports = app => {
123125
return user;
124126
});
125127
};
128+

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: 5 additions & 2 deletions
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', {
@@ -43,7 +43,9 @@ module.exports = app => {
4343
router.get('/active_account', sign.activeAccount); // 帐号激活
4444

4545
// github oauth
46-
app.passport.mount('github');
46+
const github = app.passport.authenticate('github');
47+
app.get('/passport/github', github);
48+
app.get('/passport/github/callback', github);
4749

4850
router.get('/search_pass', sign.showSearchPass); // 找回密码页面
4951
router.post('/search_pass', sign.updateSearchPass); // 更新密码
@@ -108,3 +110,4 @@ module.exports = app => {
108110

109111
router.get('/search', search.index);
110112
};
113+

app/service/reply.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,21 +52,23 @@ class ReplyService extends Service {
5252
*/
5353
async getRepliesByTopicId(id) {
5454
const query = { topic_id: id, deleted: false };
55-
const replies = await this.ctx.model.Reply.find(query, '', {
55+
let replies = await this.ctx.model.Reply.find(query, '', {
5656
sort: 'create_at',
5757
}).exec();
5858

5959
if (replies.length === 0) {
6060
return [];
6161
}
6262

63+
replies = replies.filter(function (item) {
64+
return !item.content_is_html
65+
})
66+
6367
return Promise.all(
6468
replies.map(async item => {
6569
const author = await this.service.user.getUserById(item.author_id);
6670
item.author = author || { _id: '' };
67-
if (item.content_is_html) {
68-
return;
69-
}
71+
7072
item.content = await this.service.at.linkUsers(item.content);
7173
return item;
7274
})

bin/generate_access_token.js

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

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
"loader-koa": "^2.0.1",
2323
"lodash": "^4.17.5",
2424
"markdown-it": "^8.4.1",
25+
"node-uuid": "^1.4.8",
2526
"nodemailer": "^4.6.2",
2627
"nodemailer-smtp-transport": "^2.7.4",
2728
"qiniu": "^7.1.3",
@@ -46,6 +47,7 @@
4647
"scripts": {
4748
"start": "egg-scripts start --daemon --title=egg-server-cnode",
4849
"stop": "egg-scripts stop --title=egg-server-cnode",
50+
"restart": "npm run stop && npm run start",
4951
"docker": "egg-scripts start --title=egg-server-cnode",
5052
"dev": "egg-bin dev",
5153
"debug": "egg-bin debug",

0 commit comments

Comments
 (0)