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

Commit c55ddba

Browse files
committed
refactor(article-comment): wip
1 parent 8e72477 commit c55ddba

File tree

4 files changed

+78
-134
lines changed

4 files changed

+78
-134
lines changed

lib/groupher_server/cms/delegates/article_comment.ex

Lines changed: 63 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,7 @@ defmodule GroupherServer.CMS.Delegate.ArticleComment do
1414
alias GroupherServer.{Accounts, CMS, Repo}
1515

1616
alias Accounts.User
17-
# TODO: why Post?
18-
alias CMS.{ArticleComment, ArticlePinedComment, Embeds, Post}
17+
alias CMS.{ArticleComment, ArticlePinedComment, Embeds}
1918
alias Ecto.Multi
2019

2120
@max_participator_count ArticleComment.max_participator_count()
@@ -141,6 +140,68 @@ defmodule GroupherServer.CMS.Delegate.ArticleComment do
141140
|> upsert_comment_result()
142141
end
143142

143+
# add participator to article-like content (Post, Job ...) and update count
144+
def add_participator_to_article(
145+
%{article_comments_participators: article_comments_participators} = article,
146+
%User{} = user
147+
) do
148+
total_participators = article_comments_participators |> List.insert_at(0, user) |> Enum.uniq()
149+
new_comment_participators = total_participators |> Enum.slice(0, @max_participator_count)
150+
total_participators_count = length(total_participators)
151+
152+
article
153+
|> Ecto.Changeset.change()
154+
|> Ecto.Changeset.put_change(:article_comments_participators_count, total_participators_count)
155+
|> Ecto.Changeset.put_embed(:article_comments_participators, new_comment_participators)
156+
|> Repo.update()
157+
end
158+
159+
def add_participator_to_article(_, _), do: {:ok, :pass}
160+
161+
# update comment's parent article's comments total count
162+
@spec update_article_comments_count(ArticleComment.t(), :inc | :dec) :: ArticleComment.t()
163+
def update_article_comments_count(%ArticleComment{} = comment, opt) do
164+
with {:ok, article_info} <- match(:comment_article, comment),
165+
{:ok, article} <- ORM.find(article_info.model, article_info.id) do
166+
count_query =
167+
from(c in ArticleComment, where: field(c, ^article_info.foreign_key) == ^article_info.id)
168+
169+
cur_count = Repo.aggregate(count_query, :count)
170+
171+
# dec 是 comment 还没有删除的时候的操作,和 inc 不同
172+
# 因为 dec 操作如果放在 delete 后面,那么 update 会失败
173+
case opt do
174+
:inc -> ORM.update(article, %{article_comments_count: cur_count})
175+
:dec -> ORM.update(article, %{article_comments_count: Enum.max([1, cur_count]) - 1})
176+
end
177+
end
178+
end
179+
180+
# creat article comment for parent or reply
181+
# set floor
182+
# TODO: parse editor-json
183+
# set default emotions
184+
def do_create_comment(content, foreign_key, article, %User{id: user_id}) do
185+
count_query = from(c in ArticleComment, where: field(c, ^foreign_key) == ^article.id)
186+
floor = Repo.aggregate(count_query, :count) + 1
187+
188+
ArticleComment
189+
|> ORM.create(
190+
Map.put(
191+
%{
192+
author_id: user_id,
193+
body_html: content,
194+
emotions: @default_emotions,
195+
floor: floor,
196+
is_article_author: user_id == article.author.user.id,
197+
meta: @default_comment_meta
198+
},
199+
foreign_key,
200+
article.id
201+
)
202+
)
203+
end
204+
144205
defp do_list_article_comment(thread, article_id, filters, where_query, user) do
145206
%{page: page, size: size} = filters
146207

@@ -210,69 +271,6 @@ defmodule GroupherServer.CMS.Delegate.ArticleComment do
210271

211272
defp add_pined_comments_ifneed(paged_comments, _thread, _article_id, _), do: paged_comments
212273

213-
# update comment's parent article's comments total count
214-
@spec update_article_comments_count(ArticleComment.t(), :inc | :dec) :: ArticleComment.t()
215-
defp update_article_comments_count(%ArticleComment{} = comment, opt) do
216-
with {:ok, article_info} <- match(:comment_article, comment),
217-
{:ok, article} <- ORM.find(article_info.model, article_info.id) do
218-
count_query =
219-
from(c in ArticleComment, where: field(c, ^article_info.foreign_key) == ^article_info.id)
220-
221-
cur_count = Repo.aggregate(count_query, :count)
222-
223-
# dec 是 comment 还没有删除的时候的操作,和 inc 不同
224-
# 因为 dec 操作如果放在 delete 后面,那么 update 会失败
225-
case opt do
226-
:inc -> ORM.update(article, %{article_comments_count: cur_count})
227-
:dec -> ORM.update(article, %{article_comments_count: Enum.max([1, cur_count]) - 1})
228-
end
229-
end
230-
end
231-
232-
# creat article comment for parent or reply
233-
# set floor
234-
# TODO: parse editor-json
235-
# set default emotions
236-
defp do_create_comment(content, foreign_key, article, %User{id: user_id}) do
237-
count_query = from(c in ArticleComment, where: field(c, ^foreign_key) == ^article.id)
238-
floor = Repo.aggregate(count_query, :count) + 1
239-
240-
ArticleComment
241-
|> ORM.create(
242-
Map.put(
243-
%{
244-
author_id: user_id,
245-
body_html: content,
246-
emotions: @default_emotions,
247-
floor: floor,
248-
is_article_author: user_id == article.author.user.id,
249-
meta: @default_comment_meta
250-
},
251-
foreign_key,
252-
article.id
253-
)
254-
)
255-
end
256-
257-
# add participator to article-like content (Post, Job ...) and update count
258-
defp add_participator_to_article(%Post{} = article, %User{} = user) do
259-
total_participators =
260-
article.article_comments_participators
261-
|> List.insert_at(0, user)
262-
|> Enum.uniq()
263-
264-
new_comment_participators = total_participators |> Enum.slice(0, @max_participator_count)
265-
total_participators_count = length(total_participators)
266-
267-
article
268-
|> Ecto.Changeset.change()
269-
|> Ecto.Changeset.put_change(:article_comments_participators_count, total_participators_count)
270-
|> Ecto.Changeset.put_embed(:article_comments_participators, new_comment_participators)
271-
|> Repo.update()
272-
end
273-
274-
defp add_participator_to_article(_, _), do: {:ok, :pass}
275-
276274
defp user_in_logins?([], _), do: false
277275
defp user_in_logins?(ids_list, %User{login: login}), do: Enum.member?(ids_list, login)
278276

lib/groupher_server/cms/delegates/article_comment_action.ex

Lines changed: 3 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ defmodule GroupherServer.CMS.Delegate.ArticleCommentAction do
66
import Helper.Utils, only: [done: 1, strip_struct: 1]
77
import Helper.ErrorCode
88

9+
import GroupherServer.CMS.Delegate.ArticleComment,
10+
only: [add_participator_to_article: 2, do_create_comment: 4, update_article_comments_count: 2]
11+
912
import GroupherServer.CMS.Utils.Matcher2
1013

1114
alias Helper.Types, as: T
@@ -26,12 +29,8 @@ defmodule GroupherServer.CMS.Delegate.ArticleCommentAction do
2629

2730
alias Ecto.Multi
2831

29-
@max_participator_count ArticleComment.max_participator_count()
3032
@max_parent_replies_count ArticleComment.max_parent_replies_count()
31-
@default_emotions Embeds.ArticleCommentEmotion.default_emotions()
3233
@report_threshold_for_fold ArticleComment.report_threshold_for_fold()
33-
34-
@default_comment_meta Embeds.ArticleCommentMeta.default_meta()
3534
@pined_comment_limit ArticleComment.pined_comment_limit()
3635

3736
@spec pin_article_comment(Integer.t()) :: {:ok, ArticleComment.t()}
@@ -241,50 +240,6 @@ defmodule GroupherServer.CMS.Delegate.ArticleCommentAction do
241240
end
242241
end
243242

244-
# update comment's parent article's comments total count
245-
@spec update_article_comments_count(ArticleComment.t(), :inc | :dec) :: ArticleComment.t()
246-
defp update_article_comments_count(%ArticleComment{} = comment, opt) do
247-
with {:ok, article_info} <- match(:comment_article, comment),
248-
{:ok, article} <- ORM.find(article_info.model, article_info.id) do
249-
count_query =
250-
from(c in ArticleComment, where: field(c, ^article_info.foreign_key) == ^article_info.id)
251-
252-
cur_count = Repo.aggregate(count_query, :count)
253-
254-
# dec 是 comment 还没有删除的时候的操作,和 inc 不同
255-
# 因为 dec 操作如果放在 delete 后面,那么 update 会失败
256-
case opt do
257-
:inc -> ORM.update(article, %{article_comments_count: cur_count})
258-
:dec -> ORM.update(article, %{article_comments_count: Enum.max([1, cur_count]) - 1})
259-
end
260-
end
261-
end
262-
263-
# creat article comment for parent or reply
264-
# set floor
265-
# TODO: parse editor-json
266-
# set default emotions
267-
defp do_create_comment(content, foreign_key, article, %User{id: user_id}) do
268-
count_query = from(c in ArticleComment, where: field(c, ^foreign_key) == ^article.id)
269-
floor = Repo.aggregate(count_query, :count) + 1
270-
271-
ArticleComment
272-
|> ORM.create(
273-
Map.put(
274-
%{
275-
author_id: user_id,
276-
body_html: content,
277-
emotions: @default_emotions,
278-
floor: floor,
279-
is_article_author: user_id == article.author.user.id,
280-
meta: @default_comment_meta
281-
},
282-
foreign_key,
283-
article.id
284-
)
285-
)
286-
end
287-
288243
# 设计盖楼只保留一个层级,回复楼中的评论都会被放到顶楼的 replies 中
289244
defp get_parent_comment(%ArticleComment{reply_to_id: nil} = comment) do
290245
comment
@@ -318,26 +273,6 @@ defmodule GroupherServer.CMS.Delegate.ArticleCommentAction do
318273
{:ok, parent_comment}
319274
end
320275

321-
# add participator to article-like content (Post, Job ...) and update count
322-
defp add_participator_to_article(%Post{} = article, %User{} = user) do
323-
total_participators =
324-
article.article_comments_participators
325-
|> List.insert_at(0, user)
326-
|> Enum.uniq()
327-
328-
new_comment_participators = total_participators |> Enum.slice(0, @max_participator_count)
329-
330-
total_participators_count = length(total_participators)
331-
332-
article
333-
|> Ecto.Changeset.change()
334-
|> Ecto.Changeset.put_change(:article_comments_participators_count, total_participators_count)
335-
|> Ecto.Changeset.put_embed(:article_comments_participators, new_comment_participators)
336-
|> Repo.update()
337-
end
338-
339-
defp add_participator_to_article(_, _), do: {:ok, :pass}
340-
341276
defp get_article(%ArticleComment{post_id: post_id} = comment) when not is_nil(post_id) do
342277
with {:ok, article} <- ORM.find(Post, comment.post_id, preload: [author: :user]) do
343278
{:post, article}

lib/groupher_server/cms/job.ex

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ defmodule GroupherServer.CMS.Job do
77

88
import Ecto.Changeset
99

10-
alias GroupherServer.CMS
10+
alias GroupherServer.{CMS, Accounts}
1111

1212
alias CMS.{
1313
Author,
@@ -67,6 +67,8 @@ defmodule GroupherServer.CMS.Job do
6767
has_many(:article_comments, {"articles_comments", ArticleComment})
6868
field(:article_comments_count, :integer, default: 0)
6969
field(:article_comments_participators_count, :integer, default: 0)
70+
# 评论参与者,只保留最近 5 个
71+
embeds_many(:article_comments_participators, Accounts.User, on_replace: :delete)
7072

7173
many_to_many(
7274
:tags,
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
defmodule GroupherServer.Repo.Migrations.AddArticleCommentsParticipatorsToJob do
2+
use Ecto.Migration
3+
4+
def change do
5+
alter table(:cms_jobs) do
6+
add(:article_comments_participators, :map)
7+
end
8+
end
9+
end

0 commit comments

Comments
 (0)