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

Commit d5d316e

Browse files
committed
refactor(article-comments): re-org wip
1 parent f939ed1 commit d5d316e

File tree

2 files changed

+41
-70
lines changed

2 files changed

+41
-70
lines changed

lib/groupher_server/cms/cms.ex

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@ defmodule GroupherServer.CMS do
120120
to: ArticleComment
121121

122122
defdelegate list_comment_replies(comment_id, filters), to: ArticleComment
123+
defdelegate list_comment_replies(comment_id, filters, user), to: ArticleComment
123124

124125
defdelegate list_comments(thread, content_id, filters), to: CommentCURD
125126
defdelegate list_comments_participators(thread, content_id, filters), to: CommentCURD

lib/groupher_server/cms/delegates/article_comment.ex

Lines changed: 40 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@ defmodule GroupherServer.CMS.Delegate.ArticleComment do
4646

4747
def list_article_comments(thread, article_id, filters, :timeline, user) do
4848
where_query = dynamic([c], not c.is_folded and not c.is_reported and not c.is_pined)
49-
5049
do_list_article_comment(thread, article_id, filters, where_query, user)
5150
end
5251

@@ -63,6 +62,32 @@ defmodule GroupherServer.CMS.Delegate.ArticleComment do
6362
do_list_article_comment(thread, article_id, filters, where_query, user)
6463
end
6564

65+
def list_folded_article_comments(thread, article_id, filters) do
66+
where_query = dynamic([c], c.is_folded and not c.is_reported and not c.is_pined)
67+
do_list_article_comment(thread, article_id, filters, where_query, nil)
68+
end
69+
70+
def list_folded_article_comments(thread, article_id, filters, user) do
71+
where_query = dynamic([c], c.is_folded and not c.is_reported and not c.is_pined)
72+
do_list_article_comment(thread, article_id, filters, where_query, user)
73+
end
74+
75+
def list_reported_article_comments(thread, article_id, filters, user \\ nil)
76+
77+
def list_reported_article_comments(thread, article_id, filters, user) do
78+
where_query = dynamic([c], c.is_reported)
79+
do_list_article_comment(thread, article_id, filters, where_query, user)
80+
end
81+
82+
@doc """
83+
list paged comment replies
84+
"""
85+
def list_comment_replies(comment_id, filters, user \\ nil)
86+
87+
def list_comment_replies(comment_id, filters, user) do
88+
do_list_comment_replies(comment_id, filters, user)
89+
end
90+
6691
defp do_list_article_comment(thread, article_id, filters, where_query, user) do
6792
%{page: page, size: size} = filters
6893

@@ -80,6 +105,20 @@ defmodule GroupherServer.CMS.Delegate.ArticleComment do
80105
end
81106
end
82107

108+
defp do_list_comment_replies(comment_id, filters, user) do
109+
%{page: page, size: size} = filters
110+
111+
where_query =
112+
dynamic([c], not c.is_reported and not c.is_folded and c.reply_to_id == ^comment_id)
113+
114+
ArticleComment
115+
|> where(^where_query)
116+
|> QueryBuilder.filter_pack(filters)
117+
|> ORM.paginater(~m(page size)a)
118+
|> set_viewer_emotion_ifneed(user)
119+
|> done()
120+
end
121+
83122
defp add_pined_comments_ifneed(%{entries: entries} = paged_comments, thread, article_id, %{
84123
page: 1
85124
}) do
@@ -115,75 +154,6 @@ defmodule GroupherServer.CMS.Delegate.ArticleComment do
115154

116155
defp add_pined_comments_ifneed(paged_comments, _thread, _article_id, _), do: paged_comments
117156

118-
def list_folded_article_comments(thread, article_id, %{page: page, size: size} = filters) do
119-
with {:ok, thread_query} <- match(thread, :query, article_id) do
120-
ArticleComment
121-
|> where(^thread_query)
122-
|> where([c], c.is_folded == true and c.is_reported == false and c.is_pined == false)
123-
|> QueryBuilder.filter_pack(filters)
124-
|> ORM.paginater(~m(page size)a)
125-
|> done()
126-
end
127-
end
128-
129-
def list_folded_article_comments(
130-
thread,
131-
article_id,
132-
%{page: page, size: size} = filters,
133-
%User{} = user
134-
) do
135-
with {:ok, thread_query} <- match(thread, :query, article_id) do
136-
ArticleComment
137-
|> where(^thread_query)
138-
|> where([c], c.is_folded == true and c.is_reported == false and c.is_pined == false)
139-
|> QueryBuilder.filter_pack(filters)
140-
|> ORM.paginater(~m(page size)a)
141-
|> set_viewer_emotion_ifneed(user)
142-
|> done()
143-
end
144-
end
145-
146-
def list_reported_article_comments(thread, article_id, %{page: page, size: size} = filters) do
147-
with {:ok, thread_query} <- match(thread, :query, article_id) do
148-
ArticleComment
149-
|> where(^thread_query)
150-
|> where([c], c.is_reported == true)
151-
|> QueryBuilder.filter_pack(filters)
152-
|> ORM.paginater(~m(page size)a)
153-
|> done()
154-
end
155-
end
156-
157-
def list_reported_article_comments(
158-
thread,
159-
article_id,
160-
%{page: page, size: size} = filters,
161-
%User{} = user
162-
) do
163-
with {:ok, thread_query} <- match(thread, :query, article_id) do
164-
ArticleComment
165-
|> where(^thread_query)
166-
|> where([c], c.is_reported == true)
167-
|> QueryBuilder.filter_pack(filters)
168-
|> ORM.paginater(~m(page size)a)
169-
|> set_viewer_emotion_ifneed(user)
170-
|> done()
171-
end
172-
end
173-
174-
@doc """
175-
list paged comment replies
176-
"""
177-
def list_comment_replies(comment_id, %{page: page, size: size} = filters) do
178-
ArticleComment
179-
|> where([c], c.reply_to_id == ^comment_id)
180-
# TODO: test reported and folded status
181-
|> where([c], c.is_folded == false and c.is_reported == false)
182-
|> QueryBuilder.filter_pack(filters)
183-
|> ORM.paginater(~m(page size)a)
184-
|> done()
185-
end
186-
187157
@doc "pin a comment"
188158
def pin_article_comment(comment_id) do
189159
with {:ok, comment} <- ORM.find(ArticleComment, comment_id),

0 commit comments

Comments
 (0)