Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings
This repository was archived by the owner on Nov 8, 2022. It is now read-only.

Commit9133418

Browse files
committed
enhance comment
1 parent07d0f75 commit9133418

16 files changed

+389
-15
lines changed

‎lib/mastani_server/accounts/user.ex‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ defmodule MastaniServer.Accounts.User do
2020
has_one(:github_profile,GithubUser)
2121

2222
has_many(:subscribed_communities,{"communities_subscribers",CMS.CommunitySubscriber})
23+
# has_many(:favorite_posts, {"posts_favorites", PostFavorite}) ...
2324

2425
# has_many(::following_communities, {"communities_subscribers", CommunitySubscriber})
2526
# has_many(:follow_communities, {"communities_subscribers", CommunitySubscriber})

‎lib/mastani_server/cms/cms.ex‎

Lines changed: 70 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ defmodule MastaniServer.CMS do
1919
Community,
2020
Passport,
2121
CommunitySubscriber,
22-
CommunityEditor
22+
CommunityEditor,
23+
PostCommentReply
2324
}
2425

2526
aliasMastaniServer.{Repo,Accounts}
@@ -194,15 +195,77 @@ defmodule MastaniServer.CMS do
194195
@doc"""
195196
Creates a comment for psot, job ...
196197
"""
197-
defcreate_comment(part,react,part_id,user_id,body)do
198+
defcreate_comment(part,react,part_id,%Accounts.User{id:user_id},body)do
198199
with{:ok,action}<-match_action(part,react),
199200
{:ok,content}<-ORM.find(action.target,part_id),
200201
{:ok,user}<-ORM.find(Accounts.User,user_id)do
202+
# TODO
201203
attrs=%{post_id:content.id,author_id:user.id,body:body}
202204
action.reactor|>ORM.create(attrs)
203205
end
204206
end
205207

208+
deflist_comments(part,part_id,%{page:page,size:size}=filters)do
209+
with{:ok,action}<-match_action(part,:comment)do
210+
action.reactor
211+
|>where([c],c.post_id==^part_id)
212+
|>QueryBuilder.filter_pack(filters)
213+
|>ORM.paginater(page:page,size:size)
214+
|>done()
215+
end
216+
end
217+
218+
deflist_replies(part,comment_id,%Accounts.User{id:user_id})do
219+
with{:ok,action}<-match_action(part,:comment)do
220+
action.reactor
221+
|>where([c],c.author_id==^user_id)
222+
|>join(:inner,[c],rinassoc(c,:reply_to))
223+
|>where([c,r],r.id==^comment_id)
224+
|>Repo.all()
225+
|>done()
226+
end
227+
end
228+
229+
defreply_comment(part,comment_id,%Accounts.User{id:user_id},body)do
230+
with{:ok,action}<-match_action(part,:comment),
231+
{:ok,comment}<-ORM.find(action.reactor,comment_id)do
232+
attrs=%{post_id:comment.post_id,author_id:user_id,body:body,reply_to:comment}
233+
# TODO: use Multi task to refactor
234+
caseaction.reactor|>ORM.create(attrs)do
235+
{:ok,reply}->
236+
ORM.update(reply,%{reply_id:comment.id})
237+
238+
{:ok,_}=
239+
PostCommentReply|>ORM.create(%{post_comment_id:comment.id,reply_id:reply.id})
240+
241+
{:ok,reply}
242+
243+
{:error,error}->
244+
{:error,error}
245+
end
246+
end
247+
end
248+
249+
# can not use spectial: post_comment_id
250+
deflike_comment(part,comment_id,%Accounts.User{id:user_id})do
251+
with{:ok,action}<-match_action(part,:comment)do
252+
caseORM.find_by(action.like,post_comment_id:comment_id,user_id:user_id)do
253+
{:ok,_}->
254+
{:error,"user has liked this comment"}
255+
256+
{:error,_}->
257+
attrs=%{post_comment_id:comment_id,user_id:user_id}
258+
action.like|>ORM.create(attrs)
259+
end
260+
end
261+
end
262+
263+
defundo_like_comment(part,comment_id,%Accounts.User{id:user_id})do
264+
with{:ok,action}<-match_action(part,:comment)do
265+
ORM.findby_delete(action.like,post_comment_id:comment_id,user_id:user_id)
266+
end
267+
end
268+
206269
@doc"""
207270
subscribe a community. (ONLY community, post etc use watch )
208271
"""
@@ -246,8 +309,8 @@ defmodule MastaniServer.CMS do
246309
247310
with or without page info
248311
"""
249-
defreaction_users(part,react,id,%{page:page,size:size}=filters)
250-
whenvalid_reaction(part,react)do
312+
defreaction_users(part,react,id,%{page:page,size:size}=filters)do
313+
# when valid_reaction(part, react) do
251314
with{:ok,action}<-match_action(part,react),
252315
{:ok,where}<-dynamic_where(part,id)do
253316
# common_filter(action.reactor)
@@ -262,7 +325,9 @@ defmodule MastaniServer.CMS do
262325
@doc"""
263326
favorite / star / watch CMS contents like post / tuts / video ...
264327
"""
265-
defreaction(part,react,part_id,user_id)whenvalid_reaction(part,react)do
328+
# TODO: def reaction(part, react, part_id, %Accounts.User{id: user_id}) when valid_reaction(part, react) do
329+
defreaction(part,react,part_id,%Accounts.User{id:user_id})
330+
whenvalid_reaction(part,react)do
266331
with{:ok,action}<-match_action(part,react),
267332
{:ok,content}<-ORM.find(action.target,part_id),
268333
{:ok,user}<-ORM.find(Accounts.User,user_id)do

‎lib/mastani_server/cms/cms_misc.ex‎

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,17 @@ defmodule MastaniServer.CMS.Misc do
33
this module defined the matches and handy guard ...
44
"""
55
importEcto.Query,warn:false
6-
aliasMastaniServer.CMS.{Community,Post,PostFavorite,PostStar,PostComment,Tag,Community}
6+
7+
aliasMastaniServer.CMS.{
8+
Community,
9+
Post,
10+
PostFavorite,
11+
PostStar,
12+
PostComment,
13+
Tag,
14+
Community,
15+
PostCommentLike
16+
}
717

818
@support_part[:post,:video,:job]
919
@support_react[:favorite,:star,:watch,:comment,:tag,:self]
@@ -29,13 +39,19 @@ defmodule MastaniServer.CMS.Misc do
2939
defmatch_action(:post,:community),do:{:ok,%{target:Post,reactor:Community}}
3040

3141
defmatch_action(:post,:comment),
32-
do:{:ok,%{target:Post,reactor:PostComment,preload::author}}
42+
do:{:ok,%{target:Post,reactor:PostComment,preload::author,like:PostCommentLike}}
43+
44+
defmatch_action(:post_comment,:like),
45+
do:{:ok,%{target:PostComment,reactor:PostCommentLike,preload::author}}
3346

3447
defdynamic_where(part,id)do
3548
casepartdo
3649
:post->
3750
{:ok,dynamic([p],p.post_id==^id)}
3851

52+
:post_comment->
53+
{:ok,dynamic([p],p.post_comment_id==^id)}
54+
3955
:job->
4056
{:ok,dynamic([p],p.job_id==^id)}
4157

‎lib/mastani_server/cms/post_comment.ex‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,28 @@ defmodule MastaniServer.CMS.PostComment do
22
useEcto.Schema
33
importEcto.Changeset
44
aliasMastaniServer.Accounts
5-
aliasMastaniServer.CMS.{Post,PostComment}
5+
aliasMastaniServer.CMS.{Post,PostComment,PostCommentReply,PostCommentLike}
66

77
@required_fields~w(body author_id post_id)a
8+
@optional_fields~w(reply_id)a
89

910
schema"posts_comments"do
1011
field(:body,:string)
1112
belongs_to(:author,Accounts.User,foreign_key::author_id)
1213
belongs_to(:post,Post)
14+
belongs_to(:reply_to,PostComment,foreign_key::reply_id)
15+
16+
has_many(:replies,{"posts_comments_replies",PostCommentReply})
17+
has_many(:likes,{"posts_comments_likes",PostCommentLike})
18+
# has_many(:dislikes, {"posts_comments_dislikes", PostCommentDislike})
1319

1420
timestamps(type::utc_datetime)
1521
end
1622

1723
@docfalse
1824
defchangeset(%PostComment{}=post_comment,attrs)do
1925
post_comment
20-
|>cast(attrs,@required_fields)
26+
|>cast(attrs,@required_fields++@optional_fields)
2127
|>validate_required(@required_fields)
2228
|>foreign_key_constraint(:post_id)
2329
|>foreign_key_constraint(:author_id)
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
defmoduleMastaniServer.CMS.PostCommentLikedo
2+
useEcto.Schema
3+
importEcto.Changeset
4+
5+
aliasMastaniServer.Accounts
6+
aliasMastaniServer.CMS.{PostComment,PostCommentLike}
7+
8+
@required_fields~w(post_comment_id user_id)a
9+
10+
schema"posts_comments_likes"do
11+
belongs_to(:user,Accounts.User,foreign_key::user_id)
12+
belongs_to(:post_comment,PostComment,foreign_key::post_comment_id)
13+
14+
timestamps(type::utc_datetime)
15+
end
16+
17+
@docfalse
18+
defchangeset(%PostCommentLike{}=post_comment_like,attrs)do
19+
post_comment_like
20+
|>cast(attrs,@required_fields)
21+
|>validate_required(@required_fields)
22+
|>foreign_key_constraint(:post_comment_id)
23+
|>foreign_key_constraint(:user_id)
24+
|>unique_constraint(:user_id,name::posts_comments_likes_user_id_post_comment_id_index)
25+
end
26+
end
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
defmoduleMastaniServer.CMS.PostCommentReplydo
2+
useEcto.Schema
3+
importEcto.Changeset
4+
5+
aliasMastaniServer.CMS.{PostComment,PostCommentReply}
6+
7+
@required_fields~w(post_comment_id reply_id)a
8+
9+
schema"posts_comments_replies"do
10+
belongs_to(:post_comment,PostComment,foreign_key::post_comment_id)
11+
belongs_to(:reply,PostComment,foreign_key::reply_id)
12+
13+
timestamps(type::utc_datetime)
14+
end
15+
16+
@docfalse
17+
defchangeset(%PostCommentReply{}=post_comment_reply,attrs)do
18+
post_comment_reply
19+
|>cast(attrs,@required_fields)
20+
|>validate_required(@required_fields)
21+
|>foreign_key_constraint(:post_comment_id)
22+
|>foreign_key_constraint(:reply_id)
23+
end
24+
end

‎lib/mastani_server_web/middleware/general_error.ex‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ defmodule MastaniServerWeb.Middleware.GeneralError do
77

88
defcall(%{errors:[List=errors]}=resolution,_)do
99
message=[%{message:errors}]
10-
IO.inspect(errors,label:"GeneralError")
10+
11+
IO.inspect(errors,label:"TODO: GeneralError ...")
1112
%{resolution|value:[],errors:message}
1213
end
1314

‎lib/mastani_server_web/resolvers/cms_resolver.ex‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ defmodule MastaniServerWeb.Resolvers.CMS do
9999
end
100100

101101
defreaction(_root,~m(id part action)a,%{context:%{cur_user:user}})do
102-
CMS.reaction(part,action,id,user.id)
102+
CMS.reaction(part,action,id,%Accounts.User{id:user.id})
103103
end
104104

105105
defundo_reaction(_root,~m(id part action)a,%{context:%{cur_user:user}})do
@@ -116,6 +116,6 @@ defmodule MastaniServerWeb.Resolvers.CMS do
116116
defupdate_post(_root,args,_info),do:ORM.update(args.passport_source,args)
117117

118118
defcreate_comment(_root,~m(part id body)a,%{context:%{cur_user:user}})do
119-
CMS.create_comment(part,:comment,id,user.id,body)
119+
CMS.create_comment(part,:comment,id,%Accounts.User{id:user.id},body)
120120
end
121121
end

‎priv/gettext/zh_CN/LC_MESSAGES/fields.po‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,7 @@ msgid "nickname"
22
msgstr"昵称"
33

44
msgid"email"
5-
msgstr"邮箱"
5+
msgstr"邮箱"
6+
7+
msgid"user_id"
8+
msgstr"用户"
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
defmoduleMastaniServer.Repo.Migrations.AddReplytoToPostcommentsdo
2+
useEcto.Migration
3+
4+
defchangedo
5+
altertable(:posts_comments)do
6+
# add(:author_id, references(:users, on_delete: :delete_all), null: false)
7+
add(:reply_id,references(:posts_comments,on_delete::delete_all))
8+
end
9+
end
10+
end

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp