Skip to content

Commit

Permalink
Revert "Revert "新用户accessToken缺失;topic展示的边缘条件兼容"" (#117)
Browse files Browse the repository at this point in the history
* Revert "Revert "新用户accessToken缺失;topic展示的边缘条件兼容""

This reverts commit 314773f.

* fix lint
  • Loading branch information
JacksonTian committed Apr 11, 2018
1 parent 314773f commit ac01b48
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 6 deletions.
6 changes: 5 additions & 1 deletion app.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
'use strict';

const uuid = require('uuid');

module.exports = app => {
if (app.config.debug) {
app.config.coreMiddleware.unshift('less');
Expand Down Expand Up @@ -46,6 +48,7 @@ module.exports = app => {
existUser = new ctx.model.User();
existUser.githubId = profile.id;
existUser.active = true;
existUser.accessToken = uuid.v4();
}

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

6 changes: 6 additions & 0 deletions app/controller/topic.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,12 @@ class TopicController extends Controller {

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

if (!topic) {
ctx.status = 404;
ctx.message = '此话题不存在或已被删除。';
return;
}

// 增加 visit_count
topic.visit_count += 1;
// 写入 DB
Expand Down
3 changes: 2 additions & 1 deletion app/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ module.exports = app => {
router.post('/signup', createUserLimit, sign.signup);
} else {
// 进行github验证
router.redirect('/singup', '/auth/github');
router.redirect('/signup', '/passport/github');
}

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

router.get('/search', search.index);
};

11 changes: 7 additions & 4 deletions app/service/reply.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict';

const Service = require('egg').Service;

class ReplyService extends Service {
/*
* 获取一条回复信息
Expand Down Expand Up @@ -52,21 +53,23 @@ class ReplyService extends Service {
*/
async getRepliesByTopicId(id) {
const query = { topic_id: id, deleted: false };
const replies = await this.ctx.model.Reply.find(query, '', {
let replies = await this.ctx.model.Reply.find(query, '', {
sort: 'create_at',
}).exec();

if (replies.length === 0) {
return [];
}

replies = replies.filter(function(item) {
return !item.content_is_html;
});

return Promise.all(
replies.map(async item => {
const author = await this.service.user.getUserById(item.author_id);
item.author = author || { _id: '' };
if (item.content_is_html) {
return;
}

item.content = await this.service.at.linkUsers(item.content);
return item;
})
Expand Down
33 changes: 33 additions & 0 deletions bin/generate_access_token.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
'use strict';

// 一次性脚本
// 为所有老用户生成 accessToken

const uuid = require('uuid');
const mongoose = require('mongoose');
const config = require('../config/config.prod.js')({});
const UserModel = require('../app/model/user')({
mongoose,
});

mongoose.connect(config.mongoose.url, function(err) {
if (err) {
console.error('connect to %s error: ', config.mongoose, err.message);
process.exit(1);
}
});

async function main() {
const users = await UserModel.find({
accessToken: {
$exists: false,
},
});
// console.log(users);
for (const user of users) {
user.accessToken = uuid.v4();
await user.save();
}
}

main();
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
"scripts": {
"start": "egg-scripts start --daemon --title=egg-server-cnode",
"stop": "egg-scripts stop --title=egg-server-cnode",
"restart": "npm run stop && npm run start",
"docker": "egg-scripts start --title=egg-server-cnode",
"dev": "egg-bin dev",
"debug": "egg-bin debug",
Expand Down

0 comments on commit ac01b48

Please sign in to comment.