Skip to content
This repository was archived by the owner on Mar 3, 2020. It is now read-only.

Commit e6994bb

Browse files
committed
implemented broken subsubsubcomment
1 parent 5805f80 commit e6994bb

File tree

5 files changed

+36
-6
lines changed

5 files changed

+36
-6
lines changed

velog-backend/.eslintrc.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ module.exports = {
1717
"func-names": 0,
1818
"object-property-newline": 0,
1919
"no-unused-vars": 1,
20+
"no-confusing-arrow": 0,
21+
"no-continue": 0,
22+
"no-await-in-loop": 0
2023
},
2124
"plugins": [
2225
"flowtype"

velog-backend/src/database/models/Comment.js

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,20 +63,43 @@ Comment.readComment = async function (commentId: string) {
6363
}
6464
};
6565

66+
Comment.getChildrenOf = function (id) {
67+
return Comment.findAll({
68+
include: [{ model: User, attributes: ['username'] }],
69+
where: {
70+
reply_to: id,
71+
},
72+
});
73+
};
74+
6675
Comment.listComments = async function (postId: string) {
6776
try {
6877
const data = await Comment.findAll({
6978
include: [{ model: User, attributes: ['username'] }],
7079
where: {
7180
fk_post_id: postId,
81+
level: 0,
7282
},
7383
order: [
7484
['created_at', 'DESC'],
7585
],
7686
limit: 20,
7787
});
7888
if (!data) return [];
79-
return data.map(this.serialize);
89+
const comments = data.map(c => c.toJSON());
90+
for (let i = 0; i < comments.length; i++) {
91+
if (!comments[i].has_replies) continue;
92+
const c2 = (await Comment.getChildrenOf(comments[i].id))
93+
.map(c => c.toJSON());
94+
comments[i].children = c2.map(c => c.toJSON());
95+
for (let j = 0; j < c2.length; j++) {
96+
if (c2[j].has_replies) continue;
97+
const c3 = (await Comment.getChildrenOf(c2[j].id))
98+
.map(c => c.toJSON());
99+
c2[j].children = c3;
100+
}
101+
}
102+
return comments;
80103
} catch (e) {
81104
throw e;
82105
}

velog-backend/src/router/posts/post/comments/comments.ctrl.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ export const writeComment: Middleware = async (ctx: Context) => {
3131
if (replyTo) {
3232
// check that it exists
3333
try {
34-
const c = await Comment.findById(replyTo, { raw: true });
34+
const c = await Comment.findById(replyTo);
3535
if (!c) {
3636
ctx.status = 404;
3737
ctx.body = {
@@ -46,6 +46,10 @@ export const writeComment: Middleware = async (ctx: Context) => {
4646
processedReplyTo = c.reply_to;
4747
}
4848
// TODO: update hasReply
49+
c.has_reply = true;
50+
await c.update({
51+
has_replies: true,
52+
});
4953
} catch (e) {
5054
ctx.throw(500, e);
5155
}

velog-frontend/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@
114114
"eslint-plugin-import": "^2.8.0",
115115
"eslint-plugin-jsx-a11y": "^6.0.3",
116116
"eslint-plugin-react": "^7.5.1",
117-
"flow-bin": "^0.61.0"
117+
"flow-bin": "^0.67.1"
118118
},
119119
"proxy": "http://localhost:4000/"
120120
}

velog-frontend/yarn.lock

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2676,9 +2676,9 @@ flatten@^1.0.2:
26762676
version "1.0.2"
26772677
resolved "https://registry.yarnpkg.com/flatten/-/flatten-1.0.2.tgz#dae46a9d78fbe25292258cc1e780a41d95c03782"
26782678

2679-
flow-bin@^0.61.0:
2680-
version "0.61.0"
2681-
resolved "https://registry.yarnpkg.com/flow-bin/-/flow-bin-0.61.0.tgz#d0473a8c35dbbf4de573823f4932124397d32d35"
2679+
flow-bin@^0.67.1:
2680+
version "0.67.1"
2681+
resolved "https://registry.yarnpkg.com/flow-bin/-/flow-bin-0.67.1.tgz#eabb7197cce870ac9442cfd04251c7ddc30377db"
26822682

26832683
follow-redirects@^1.2.5:
26842684
version "1.2.6"

0 commit comments

Comments
 (0)