Skip to content
This repository was archived by the owner on Nov 8, 2022. It is now read-only.

Commit 75ddf68

Browse files
authored
feat(lock-comment): workflow (#381)
* feat(lock-comment): basic logic done * feat(lock-comment): gq workflow for articles * feat(lock-comment): comemnt error handle & clean up * chore(article): adjust active_at test args * chore(article): adjust active_at test args
1 parent db723ec commit 75ddf68

File tree

19 files changed

+410
-78
lines changed

19 files changed

+410
-78
lines changed

lib/groupher_server/cms/cms.ex

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,6 @@ defmodule GroupherServer.CMS do
116116
# >> set flag on article, like: pin / unpin article
117117
defdelegate pin_article(thread, id, community_id), to: ArticleCommunity
118118
defdelegate undo_pin_article(thread, id, community_id), to: ArticleCommunity
119-
defdelegate lock_article_comment(thread, article_id), to: ArticleCommunity
120119

121120
# >> community: set / unset
122121
defdelegate mirror_article(thread, article_id, community_id), to: ArticleCommunity
@@ -146,6 +145,8 @@ defmodule GroupherServer.CMS do
146145
defdelegate upvote_article_comment(comment_id, user), to: ArticleCommentAction
147146
defdelegate undo_upvote_article_comment(comment_id, user), to: ArticleCommentAction
148147
defdelegate reply_article_comment(comment_id, args, user), to: ArticleCommentAction
148+
defdelegate lock_article_comment(thread, article_id), to: ArticleCommentAction
149+
defdelegate undo_lock_article_comment(thread, article_id), to: ArticleCommentAction
149150

150151
defdelegate pin_article_comment(comment_id), to: ArticleCommentAction
151152
defdelegate undo_pin_article_comment(comment_id), to: ArticleCommentAction

lib/groupher_server/cms/delegates/article_comment.ex

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ defmodule GroupherServer.CMS.Delegate.ArticleComment do
33
CURD and operations for article comments
44
"""
55
import Ecto.Query, warn: false
6-
import Helper.Utils, only: [done: 1]
6+
import Helper.Utils, only: [done: 1, ensure: 2]
77
import Helper.ErrorCode
88

99
import GroupherServer.CMS.Delegate.Helper, only: [mark_viewer_emotion_states: 3]
@@ -22,6 +22,7 @@ defmodule GroupherServer.CMS.Delegate.ArticleComment do
2222
@default_emotions Embeds.ArticleCommentEmotion.default_emotions()
2323
@delete_hint ArticleComment.delete_hint()
2424

25+
@default_article_meta Embeds.ArticleMeta.default_meta()
2526
@default_comment_meta Embeds.ArticleCommentMeta.default_meta()
2627
@pinned_comment_limit ArticleComment.pinned_comment_limit()
2728

@@ -92,9 +93,8 @@ defmodule GroupherServer.CMS.Delegate.ArticleComment do
9293
"""
9394
def create_article_comment(thread, article_id, content, %User{} = user) do
9495
with {:ok, info} <- match(thread),
95-
# make sure the article exsit
96-
# author is passed by middleware, it's exsit for sure
97-
{:ok, article} <- ORM.find(info.model, article_id, preload: [author: :user]) do
96+
{:ok, article} <- ORM.find(info.model, article_id, preload: [author: :user]),
97+
true <- can_comment?(article, user) do
9898
Multi.new()
9999
|> Multi.run(:create_article_comment, fn _, _ ->
100100
do_create_comment(content, info.foreign_key, article, user)
@@ -113,9 +113,20 @@ defmodule GroupherServer.CMS.Delegate.ArticleComment do
113113
end)
114114
|> Repo.transaction()
115115
|> result()
116+
else
117+
false -> raise_error(:article_comment_locked, "this article is forbid comment")
118+
{:error, error} -> {:error, error}
116119
end
117120
end
118121

122+
@doc "check is article can be comemnt or not"
123+
# TODO: check if use is in author's block list?
124+
def can_comment?(article, _user) do
125+
article_meta = ensure(article.meta, @default_article_meta)
126+
127+
not article_meta.is_comment_locked
128+
end
129+
119130
@doc """
120131
update a comment for article like psot, job ...
121132
"""

lib/groupher_server/cms/delegates/article_comment_action.ex

Lines changed: 42 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,16 @@ defmodule GroupherServer.CMS.Delegate.ArticleCommentAction do
33
CURD and operations for article comments
44
"""
55
import Ecto.Query, warn: false
6-
import Helper.Utils, only: [done: 1, strip_struct: 1, get_config: 2]
6+
import Helper.Utils, only: [done: 1, strip_struct: 1, get_config: 2, ensure: 2]
77
import Helper.ErrorCode
88

99
import GroupherServer.CMS.Delegate.ArticleComment,
10-
only: [add_participator_to_article: 2, do_create_comment: 4, update_article_comments_count: 2]
10+
only: [
11+
add_participator_to_article: 2,
12+
do_create_comment: 4,
13+
update_article_comments_count: 2,
14+
can_comment?: 2
15+
]
1116

1217
import GroupherServer.CMS.Helper.Matcher
1318

@@ -16,12 +21,20 @@ defmodule GroupherServer.CMS.Delegate.ArticleCommentAction do
1621
alias GroupherServer.{Accounts, CMS, Repo}
1722

1823
alias Accounts.User
19-
alias CMS.{ArticleComment, ArticlePinnedComment, ArticleCommentUpvote, ArticleCommentReply}
24+
25+
alias CMS.{
26+
ArticleComment,
27+
ArticlePinnedComment,
28+
ArticleCommentUpvote,
29+
ArticleCommentReply,
30+
Embeds
31+
}
2032

2133
alias Ecto.Multi
2234

2335
@article_threads get_config(:article, :threads)
2436

37+
@default_article_meta Embeds.ArticleMeta.default_meta()
2538
@max_parent_replies_count ArticleComment.max_parent_replies_count()
2639
@pinned_comment_limit ArticleComment.pinned_comment_limit()
2740

@@ -102,6 +115,7 @@ defmodule GroupherServer.CMS.Delegate.ArticleCommentAction do
102115
ORM.find_by(ArticleComment, %{id: comment_id, is_deleted: false}),
103116
replying_comment <- Repo.preload(target_comment, reply_to: :author),
104117
{thread, article} <- get_article(replying_comment),
118+
true <- can_comment?(article, user),
105119
{:ok, info} <- match(thread),
106120
parent_comment <- get_parent_comment(replying_comment) do
107121
Multi.new()
@@ -136,6 +150,9 @@ defmodule GroupherServer.CMS.Delegate.ArticleCommentAction do
136150
end)
137151
|> Repo.transaction()
138152
|> result()
153+
else
154+
false -> raise_error(:article_comment_locked, "this article is forbid comment")
155+
{:error, error} -> {:error, error}
139156
end
140157
end
141158

@@ -192,6 +209,28 @@ defmodule GroupherServer.CMS.Delegate.ArticleCommentAction do
192209
end
193210
end
194211

212+
@doc "lock comment of a article"
213+
def lock_article_comment(thread, id) do
214+
with {:ok, info} <- match(thread),
215+
{:ok, article} <- ORM.find(info.model, id) do
216+
article_meta = ensure(article.meta, @default_article_meta)
217+
meta = Map.merge(article_meta, %{is_comment_locked: true})
218+
219+
ORM.update_meta(article, meta)
220+
end
221+
end
222+
223+
@doc "undo lock comment of a article"
224+
def undo_lock_article_comment(thread, id) do
225+
with {:ok, info} <- match(thread),
226+
{:ok, article} <- ORM.find(info.model, id) do
227+
article_meta = ensure(article.meta, @default_article_meta)
228+
meta = Map.merge(article_meta, %{is_comment_locked: false})
229+
230+
ORM.update_meta(article, meta)
231+
end
232+
end
233+
195234
defp update_article_author_upvoted_info(%ArticleComment{} = comment, user_id) do
196235
with {:ok, article} = get_full_comment(comment.id) do
197236
is_article_author_upvoted = article.author.id == user_id

lib/groupher_server/cms/delegates/article_community.ex

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ defmodule GroupherServer.CMS.Delegate.ArticleCommunity do
66
import Ecto.Query, warn: false
77

88
import Helper.ErrorCode
9-
import Helper.Utils, only: [strip_struct: 1, done: 1, ensure: 2]
9+
import Helper.Utils, only: [strip_struct: 1, done: 1]
1010
import GroupherServer.CMS.Helper.Matcher
1111

1212
alias Helper.Types, as: T
@@ -144,17 +144,6 @@ defmodule GroupherServer.CMS.Delegate.ArticleCommunity do
144144

145145
def update_edit_status(content, _), do: {:ok, content}
146146

147-
@doc "lock comment of a article"
148-
def lock_article_comment(thread, id) do
149-
with {:ok, info} <- match(thread),
150-
{:ok, article} <- ORM.find(info.model, id) do
151-
article_meta = ensure(article.meta, @default_article_meta)
152-
meta = Map.merge(article_meta, %{is_comment_locked: true})
153-
154-
ORM.update_meta(article, meta)
155-
end
156-
end
157-
158147
# check if the thread has aready enough pinned articles
159148
defp check_pinned_article_count(community_id, thread) do
160149
thread_upcase = thread |> to_string |> String.upcase()

lib/groupher_server_web/resolvers/cms_resolver.ex

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,12 @@ defmodule GroupherServerWeb.Resolvers.CMS do
105105
# #######################
106106
# thread reaction ..
107107
# #######################
108+
def lock_article_comment(_root, ~m(id thread)a, _info), do: CMS.lock_article_comment(thread, id)
109+
110+
def undo_lock_article_comment(_root, ~m(id thread)a, _info) do
111+
CMS.undo_lock_article_comment(thread, id)
112+
end
113+
108114
def sink_article(_root, ~m(id thread)a, _info), do: CMS.sink_article(thread, id)
109115
def undo_sink_article(_root, ~m(id thread)a, _info), do: CMS.undo_sink_article(thread, id)
110116

lib/groupher_server_web/schema/Helper/mutations.ex

Lines changed: 56 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -5,34 +5,6 @@ defmodule GroupherServerWeb.Schema.Helper.Mutations do
55
alias GroupherServerWeb.Middleware, as: M
66
alias GroupherServerWeb.Resolvers, as: R
77

8-
defmacro article_sink_mutation(thread) do
9-
quote do
10-
@desc unquote("sink a #{thread}")
11-
field unquote(:"sink_#{thread}"), :article do
12-
arg(:id, non_null(:id))
13-
arg(:community_id, non_null(:id))
14-
arg(:thread, unquote(:"#{thread}_thread"), default_value: unquote(thread))
15-
16-
middleware(M.Authorize, :login)
17-
middleware(M.PassportLoader, source: :community)
18-
middleware(M.Passport, claim: unquote("cms->c?->#{to_string(thread)}.sink"))
19-
resolve(&R.CMS.sink_article/3)
20-
end
21-
22-
@desc unquote("undo sink to #{thread}")
23-
field unquote(:"undo_sink_#{thread}"), :article do
24-
arg(:id, non_null(:id))
25-
arg(:community_id, non_null(:id))
26-
arg(:thread, unquote(:"#{thread}_thread"), default_value: unquote(thread))
27-
28-
middleware(M.Authorize, :login)
29-
middleware(M.PassportLoader, source: :community)
30-
middleware(M.Passport, claim: unquote("cms->c?->#{to_string(thread)}.undo_sink"))
31-
resolve(&R.CMS.undo_sink_article/3)
32-
end
33-
end
34-
end
35-
368
defmacro article_upvote_mutation(thread) do
379
quote do
3810
@desc unquote("upvote to #{thread}")
@@ -172,4 +144,60 @@ defmodule GroupherServerWeb.Schema.Helper.Mutations do
172144
end
173145
end
174146
end
147+
148+
defmacro article_sink_mutation(thread) do
149+
quote do
150+
@desc unquote("sink a #{thread}")
151+
field unquote(:"sink_#{thread}"), :article do
152+
arg(:id, non_null(:id))
153+
arg(:community_id, non_null(:id))
154+
arg(:thread, unquote(:"#{thread}_thread"), default_value: unquote(thread))
155+
156+
middleware(M.Authorize, :login)
157+
middleware(M.PassportLoader, source: :community)
158+
middleware(M.Passport, claim: unquote("cms->c?->#{to_string(thread)}.sink"))
159+
resolve(&R.CMS.sink_article/3)
160+
end
161+
162+
@desc unquote("undo sink to #{thread}")
163+
field unquote(:"undo_sink_#{thread}"), :article do
164+
arg(:id, non_null(:id))
165+
arg(:community_id, non_null(:id))
166+
arg(:thread, unquote(:"#{thread}_thread"), default_value: unquote(thread))
167+
168+
middleware(M.Authorize, :login)
169+
middleware(M.PassportLoader, source: :community)
170+
middleware(M.Passport, claim: unquote("cms->c?->#{to_string(thread)}.undo_sink"))
171+
resolve(&R.CMS.undo_sink_article/3)
172+
end
173+
end
174+
end
175+
176+
defmacro article_lock_comment_mutation(thread) do
177+
quote do
178+
@desc unquote("lock comment to a #{thread}")
179+
field unquote(:"lock_#{thread}_comment"), :article do
180+
arg(:id, non_null(:id))
181+
arg(:community_id, non_null(:id))
182+
arg(:thread, unquote(:"#{thread}_thread"), default_value: unquote(thread))
183+
184+
middleware(M.Authorize, :login)
185+
middleware(M.PassportLoader, source: :community)
186+
middleware(M.Passport, claim: unquote("cms->c?->#{to_string(thread)}.lock_comment"))
187+
resolve(&R.CMS.lock_article_comment/3)
188+
end
189+
190+
@desc unquote("undo lock to a #{thread}")
191+
field unquote(:"undo_lock_#{thread}_comment"), :article do
192+
arg(:id, non_null(:id))
193+
arg(:community_id, non_null(:id))
194+
arg(:thread, unquote(:"#{thread}_thread"), default_value: unquote(thread))
195+
196+
middleware(M.Authorize, :login)
197+
middleware(M.PassportLoader, source: :community)
198+
middleware(M.Passport, claim: unquote("cms->c?->#{to_string(thread)}.undo_lock_comment"))
199+
resolve(&R.CMS.undo_lock_article_comment/3)
200+
end
201+
end
202+
end
175203
end

lib/groupher_server_web/schema/cms/mutations/job.ex

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,13 +55,14 @@ defmodule GroupherServerWeb.Schema.CMS.Mutations.Job do
5555
end
5656

5757
#############
58-
article_sink_mutation(:job)
5958
article_upvote_mutation(:job)
6059
article_pin_mutation(:job)
6160
article_mark_delete_mutation(:job)
6261
article_delete_mutation(:job)
6362
article_emotion_mutation(:job)
6463
article_report_mutation(:job)
64+
article_sink_mutation(:job)
65+
article_lock_comment_mutation(:job)
6566
#############
6667
end
6768
end

lib/groupher_server_web/schema/cms/mutations/post.ex

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,14 @@ defmodule GroupherServerWeb.Schema.CMS.Mutations.Post do
4545
end
4646

4747
#############
48-
article_sink_mutation(:post)
4948
article_upvote_mutation(:post)
5049
article_pin_mutation(:post)
5150
article_mark_delete_mutation(:post)
5251
article_delete_mutation(:post)
5352
article_emotion_mutation(:post)
5453
article_report_mutation(:post)
54+
article_sink_mutation(:post)
55+
article_lock_comment_mutation(:post)
5556
#############
5657
end
5758
end

lib/groupher_server_web/schema/cms/mutations/repo.ex

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,12 +71,13 @@ defmodule GroupherServerWeb.Schema.CMS.Mutations.Repo do
7171
end
7272

7373
#############
74-
article_sink_mutation(:repo)
7574
article_pin_mutation(:repo)
7675
article_mark_delete_mutation(:repo)
7776
article_delete_mutation(:repo)
7877
article_emotion_mutation(:repo)
7978
article_report_mutation(:repo)
79+
article_sink_mutation(:repo)
80+
article_lock_comment_mutation(:repo)
8081
#############
8182
end
8283
end

lib/helper/certification.ex

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,13 @@ defmodule Helper.Certification do
7272
"job.undo_sink",
7373
"repo.sink",
7474
"repo.undo_sink",
75+
# lock/undo_lock article comment
76+
"post.lock_comment",
77+
"post.undo_lock_comment",
78+
"job.lock_comment",
79+
"job.undo_lock_comment",
80+
"repo.lock_comment",
81+
"repo.undo_lock_comment",
7582
#
7683
"post.mark_delete",
7784
"post.undo_mark_delete",

0 commit comments

Comments
 (0)