Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: post topic/create #69

Merged
merged 3 commits into from
Mar 15, 2018
Merged

Conversation

forl
Copy link
Contributor

@forl forl commented Mar 13, 2018

Checklist
  • npm test passes
  • tests and/or benchmarks are included
  • documentation is changed or added
  • commit message follows commit guidelines
Affected core subsystem(s)

topic/create

Description of change

refactor: finish post topic/create

@codecov
Copy link

codecov bot commented Mar 13, 2018

Codecov Report

❗ No coverage uploaded for pull request base (master@bf13548). Click here to learn what that means.
The diff coverage is 100%.

Impacted file tree graph

@@            Coverage Diff            @@
##             master      #69   +/-   ##
=========================================
  Coverage          ?   89.25%           
=========================================
  Files             ?       35           
  Lines             ?     1238           
  Branches          ?        0           
=========================================
  Hits              ?     1105           
  Misses            ?      133           
  Partials          ?        0
Impacted Files Coverage Δ
app/router.js 96.61% <100%> (ø)
app/controller/topic.js 90.9% <100%> (ø)
config/config.default.js 100% <100%> (ø)
config/plugin.js 100% <100%> (ø)
app/middleware/topic_per_day_limit.js 100% <100%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update bf13548...7d8bbb9. Read the comment docs.


if (ctx.status === 302) {
// 新建话题成功
// TODO: 此处正确做法应该是使用redis的原子操作
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个地方应该用incr操作的,对redis库不熟,就暂时这样实现了,后面抽空看一下。

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

incr 后面不是实现了吗。可以改改。

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cache.incr之前的实现不对,我提了个单独的pr来实现cache.incr#73

#73 没有问题就merge一下吧

@forl forl changed the title Topic/create refactor: topic/create Mar 13, 2018
@forl forl changed the title refactor: topic/create refactor: post topic/create Mar 13, 2018
const { body } = ctx.request;
const title = (_.isString(body.title) && body.title.trim()) || '';
const tab = (_.isString(body.tab) && body.tab.trim()) || '';
const content = (_.isString(body.t_content) && body.t_content.trim()) || '';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

 减少对 lodash 的依赖

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

参数验证还是用 egg-validate


if (ctx.status === 302) {
// 新建话题成功
// TODO: 此处正确做法应该是使用redis的原子操作
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

incr 后面不是实现了吗。可以改改。

const { body } = ctx.request;
const title = (_.isString(body.title) && body.title.trim()) || '';
const tab = (_.isString(body.tab) && body.tab.trim()) || '';
const content = (_.isString(body.t_content) && body.t_content.trim()) || '';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

#70 看下这里的示例。

const { body } = ctx.request;
const title = (_.isString(body.title) && body.title.trim()) || '';
const tab = (_.isString(body.tab) && body.tab.trim()) || '';
const content = (_.isString(body.t_content) && body.t_content.trim()) || '';

// 得到所有的 tab, e.g. ['ask', 'share', ..]
const allTabs = tabs.map(function(tPair) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

const allTabs = tabs.map(tPair => tPair[0])

@JacksonTian
Copy link
Member

麻烦做下 rebase 吧。

@JacksonTian
Copy link
Member

$ git checkout topic/create
$ git remote add cnode https://github.com/cnodejs/egg-cnode.git
$ get fetch cnode
$ git rebase -i cnode/master

除了第一条外,其余全部用 f

@forl
Copy link
Contributor Author

forl commented Mar 14, 2018

@JacksonTian ,好的,你说的这个方法太好用了!

// 通知被@的用户
await service.at.sendMessageToMentionUsers(
content,
topic._id,
ctx.user._id
);

await ctx.redirect('/topic/' + topic._id);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

redirect 可以不用 await 的。不过这个好像不是你写的。

const tab = ctx.request.body.tab.trim();
const content = ctx.request.body.t_content.trim();
const { body } = ctx.request;
const title = (_.isString(body.title) && body.title.trim()) || '';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

减少对 lodash 的依赖。

const YYYYMMDD = moment().format('YYYYMMDD');
const key = `topic_per_user_per_day_${user._id}_${YYYYMMDD}`;

let currentCount = await service.cache.get(key) || 0;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个好像有歧义吧。

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

已改

module.exports = options => {

return async function(ctx, next) {
const { perDayPerUserLimitCount = 10 } = options || {};
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里改成箭头函数吧,默认值 {} 移动到参数里面

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

试过这样,但看起来并不简洁呢

const perDayPerUserLimitCount = ({ perDayPerUserLimitCount = 10 }) => (perDayPerUserLimitCount)(options);

改成了现在这样


// 储存新主题帖
const topic = await service.topic.newAndSave(
title,
content,
t_content,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个地方叫 t_content 的原因是?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

最初的代码是这样,前端是这样传的吧,我可以把前端一起改了

tab,
ctx.user._id
);

ctx.logger.info(topic);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这句日志是必要的吗

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

忘记去掉了

@JacksonTian JacksonTian merged commit dd2c5df into cnodejs:master Mar 15, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants