-
Notifications
You must be signed in to change notification settings - Fork 52
/
comment.ts
193 lines (176 loc) · 5.24 KB
/
comment.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
import { ApolloContext } from './../app';
import { gql, IResolvers, AuthenticationError, ApolloError } from 'apollo-server-koa';
import Comment from '../entity/Comment';
import { getRepository } from 'typeorm';
import Post from '../entity/Post';
import PostScore from '../entity/PostScore';
export const typeDef = gql`
type Comment {
id: ID!
text: String
likes: Int
level: Int
has_replies: Boolean
deleted: Boolean
user: User
replies: [Comment]
created_at: Date
replies_count: Int
}
extend type Query {
comment(comment_id: ID): Comment
subcomments(comment_id: ID): [Comment]
}
extend type Mutation {
writeComment(post_id: ID!, text: String!, comment_id: ID): Comment
removeComment(id: ID!): Boolean
editComment(id: ID!, text: String!): Comment
}
`;
type WriteCommentArgs = {
post_id: string;
text: string;
comment_id?: string;
};
export const resolvers: IResolvers<any, ApolloContext> = {
Comment: {
text: (parent: Comment) => {
if (parent.deleted) {
return null;
}
return parent.text;
},
user: (parent: Comment, _: any, { loaders }) => {
if (parent.deleted) {
return null;
}
if (parent.user) return parent.user;
const user = loaders.user.load(parent.fk_user_id);
return user;
},
replies: async (parent: Comment, args: any) => {
console.log(args);
// TODO: Optimize
if (!parent.has_replies) return [];
const comments = await getRepository(Comment).find({
where: {
reply_to: parent.id,
deleted: false
},
order: {
created_at: 'ASC'
}
});
return comments;
},
replies_count: async (parent: Comment) => {
if (!parent.has_replies) return 0;
const count = await getRepository(Comment).count({
where: {
reply_to: parent.id,
deleted: false
}
});
return count;
}
},
Query: {
comment: async (parent: any, { comment_id }) => {
const comment = await getRepository(Comment).findOne(comment_id);
return comment;
},
subcomments: async (parent: any, { comment_id }) => {
const comments = await getRepository(Comment).find({
where: {
reply_to: comment_id
},
order: {
created_at: 'ASC'
}
});
return comments;
}
},
Mutation: {
writeComment: async (parent: any, args, ctx) => {
if (!ctx.user_id) {
throw new AuthenticationError('Not Logged In');
}
const { post_id, comment_id, text } = args as WriteCommentArgs;
const post = await getRepository(Post).findOne(post_id);
if (!post) {
throw new ApolloError('Post not found', 'NOT_FOUND');
}
const commentRepo = getRepository(Comment);
const comment = new Comment();
if (comment_id) {
const commentTarget = await commentRepo.findOne(comment_id);
if (!commentTarget) {
throw new ApolloError('Target comment is not found', 'NOT_FOUND');
}
comment.level = commentTarget.level + 1;
comment.reply_to = comment_id;
if (comment.level >= 4) {
throw new ApolloError('Maximum comment level is 3', 'BAD_REQUEST');
}
commentTarget.has_replies = true;
await commentRepo.save(commentTarget);
}
comment.fk_user_id = ctx.user_id;
comment.text = text;
comment.fk_post_id = post_id;
await commentRepo.save(comment);
const postScoreRepo = getRepository(PostScore);
const score = new PostScore();
score.fk_post_id = post_id;
score.fk_user_id = ctx.user_id;
score.score = 1;
score.type = 'COMMENT';
postScoreRepo.save(score);
return comment;
},
removeComment: async (parent: any, { id }: any, ctx) => {
const commentRepo = getRepository(Comment);
const comment = await commentRepo.findOne(id);
if (!comment) {
throw new ApolloError('Comment not found');
}
if (!ctx.user_id) {
throw new AuthenticationError('Not Logged In');
}
if (ctx.user_id !== comment.fk_user_id) {
throw new ApolloError('No permission');
}
comment.deleted = true;
await commentRepo.save(comment);
const postScoreRepo = getRepository(PostScore);
const score = await postScoreRepo
.createQueryBuilder()
.where('fk_post_id = :postId', { postId: comment.fk_post_id })
.andWhere('fk_user_id = :userId', { userId: ctx.user_id })
.andWhere("type = 'COMMENT'")
.orderBy('created_at', 'DESC')
.getOne();
if (score) {
postScoreRepo.delete(score.id);
}
return true;
},
editComment: async (parent: any, { id, text }: any, ctx) => {
const commentRepo = getRepository(Comment);
const comment = await commentRepo.findOne(id);
if (!comment) {
throw new ApolloError('Comment not found');
}
if (!ctx.user_id) {
throw new AuthenticationError('Not Logged In');
}
if (ctx.user_id !== comment.fk_user_id) {
throw new ApolloError('No permission');
}
comment.text = text;
await commentRepo.save(comment);
return comment;
}
}
};