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

Commit c7eaaf6

Browse files
authored
refactor(naming): re-org & fold workflow enhance (#413)
* refactor(naming): re-org cms helper import * refactor(import): fix import & clean up warnings * refactor(matcher): clean up unused matcher * chore: fmt * chore: fmt * refactor(fold-comment): update article meta & tests
1 parent 94f72cf commit c7eaaf6

File tree

24 files changed

+154
-124
lines changed

24 files changed

+154
-124
lines changed

lib/groupher_server/cms/delegates/article_curd.ex

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,10 @@ defmodule GroupherServer.CMS.Delegate.ArticleCURD do
1313
module_to_atom: 1,
1414
get_config: 2,
1515
ensure: 2,
16-
module_to_upcase: 1,
17-
thread_of_article: 1
16+
module_to_upcase: 1
1817
]
1918

20-
import GroupherServer.CMS.Delegate.Helper, only: [mark_viewer_emotion_states: 2]
19+
import GroupherServer.CMS.Delegate.Helper, only: [mark_viewer_emotion_states: 2, thread_of: 1]
2120
import Helper.ErrorCode
2221
import ShortMaps
2322

@@ -353,7 +352,7 @@ defmodule GroupherServer.CMS.Delegate.ArticleCURD do
353352
"""
354353
def delete_article(article, reason \\ @remove_article_hint) do
355354
article = Repo.preload(article, [:communities, [author: :user]])
356-
{:ok, thread} = thread_of_article(article)
355+
{:ok, thread} = thread_of(article)
357356

358357
Multi.new()
359358
|> Multi.run(:delete_article, fn _, _ ->

lib/groupher_server/cms/delegates/cited_artiment.ex

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,14 @@ defmodule GroupherServer.CMS.Delegate.CitedArtiment do
55
import Ecto.Query, warn: false
66

77
import Helper.Utils,
8-
only: [done: 1, get_config: 2, thread_of_article: 1, atom_values_to_upcase: 1, to_upcase: 1]
8+
only: [
9+
done: 1,
10+
get_config: 2,
11+
atom_values_to_upcase: 1,
12+
to_upcase: 1
13+
]
14+
15+
import GroupherServer.CMS.Delegate.Helper, only: [thread_of: 1, article_of: 1]
916

1017
import GroupherServer.CMS.Helper.Matcher
1118
import ShortMaps
@@ -45,7 +52,7 @@ defmodule GroupherServer.CMS.Delegate.CitedArtiment do
4552
end
4653

4754
def batch_delete_by(article) do
48-
with {:ok, thread} <- thread_of_article(article),
55+
with {:ok, thread} <- thread_of(article),
4956
{:ok, info} <- match(thread) do
5057
thread = to_upcase(thread)
5158

@@ -104,9 +111,9 @@ defmodule GroupherServer.CMS.Delegate.CitedArtiment do
104111
defp shape(%CitedArtiment{comment_id: comment_id} = cited) when not is_nil(comment_id) do
105112
%{block_linker: block_linker, comment: comment, inserted_at: inserted_at} = cited
106113

107-
comment_thread = comment.thread |> String.downcase() |> String.to_atom()
108-
article = comment |> Map.get(comment_thread)
109-
article_thread = thread_to_atom(article.meta.thread)
114+
{:ok, article} = article_of(comment)
115+
{:ok, article_thread} = thread_of(article)
116+
110117
user = comment.author |> Map.take([:login, :nickname, :avatar])
111118

112119
article
@@ -144,6 +151,4 @@ defmodule GroupherServer.CMS.Delegate.CitedArtiment do
144151
defp citing_thread(cited) do
145152
@article_threads |> Enum.find(fn thread -> not is_nil(Map.get(cited, :"#{thread}_id")) end)
146153
end
147-
148-
defp thread_to_atom(thread), do: thread |> String.downcase() |> String.to_atom()
149154
end

lib/groupher_server/cms/delegates/comment_action.ex

Lines changed: 36 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ defmodule GroupherServer.CMS.Delegate.CommentAction do
44
"""
55
import Ecto.Query, warn: false
66
import Helper.Utils, only: [done: 1, strip_struct: 1, get_config: 2, ensure: 2]
7+
import GroupherServer.CMS.Delegate.Helper, only: [article_of: 1, thread_of: 1]
8+
79
import Helper.ErrorCode
810

911
import GroupherServer.CMS.Delegate.CommentCurd,
@@ -40,26 +42,25 @@ defmodule GroupherServer.CMS.Delegate.CommentAction do
4042
{:ok, info} <- match(full_comment.thread) do
4143
Multi.new()
4244
|> Multi.run(:checked_pined_comments_count, fn _, _ ->
43-
{:ok, pined_comments_count} =
45+
pined_comments_query =
4446
from(p in PinnedComment,
4547
where: field(p, ^info.foreign_key) == ^full_comment.article.id
4648
)
47-
|> ORM.count()
4849

49-
case pined_comments_count >= @pinned_comment_limit do
50-
true -> {:error, "max #{@pinned_comment_limit} pinned comment for each article"}
51-
false -> {:ok, :pass}
50+
with {:ok, pined_comments_count} <- ORM.count(pined_comments_query) do
51+
case pined_comments_count >= @pinned_comment_limit do
52+
true -> {:error, "max #{@pinned_comment_limit} pinned comment for each article"}
53+
false -> {:ok, :pass}
54+
end
5255
end
5356
end)
5457
|> Multi.run(:update_comment_flag, fn _, _ ->
5558
ORM.update(comment, %{is_pinned: true})
5659
end)
5760
|> Multi.run(:add_pined_comment, fn _, _ ->
58-
PinnedComment
59-
|> ORM.create(
60-
%{comment_id: comment.id}
61-
|> Map.put(info.foreign_key, full_comment.article.id)
62-
)
61+
attrs = %{comment_id: comment.id} |> Map.put(info.foreign_key, full_comment.article.id)
62+
63+
PinnedComment |> ORM.create(attrs)
6364
end)
6465
|> Repo.transaction()
6566
|> result()
@@ -81,21 +82,19 @@ defmodule GroupherServer.CMS.Delegate.CommentAction do
8182
end
8283

8384
@doc "fold a comment"
84-
def fold_comment(%Comment{} = comment, %User{} = _user) do
85-
comment |> ORM.update(%{is_folded: true})
86-
end
85+
def fold_comment(%Comment{} = comment, %User{} = _user), do: do_fold_comment(comment, true)
8786

88-
@doc "fold a comment"
87+
@doc "fold a comment by id"
8988
def fold_comment(comment_id, %User{} = _user) do
9089
with {:ok, comment} <- ORM.find(Comment, comment_id) do
91-
comment |> ORM.update(%{is_folded: true})
90+
do_fold_comment(comment, true)
9291
end
9392
end
9493

9594
@doc "unfold a comment"
9695
def unfold_comment(comment_id, %User{} = _user) do
9796
with {:ok, comment} <- ORM.find(Comment, comment_id) do
98-
comment |> ORM.update(%{is_folded: false})
97+
do_fold_comment(comment, false)
9998
end
10099
end
101100

@@ -249,6 +248,26 @@ defmodule GroupherServer.CMS.Delegate.CommentAction do
249248
end
250249
end
251250

251+
# do (un)fold and update folded count in article meta
252+
defp do_fold_comment(%Comment{} = comment, is_folded) when is_boolean(is_folded) do
253+
Multi.new()
254+
|> Multi.run(:fold_comment, fn _, _ ->
255+
comment |> ORM.update(%{is_folded: is_folded})
256+
end)
257+
|> Multi.run(:update_article_fold_count, fn _, _ ->
258+
{:ok, article} = article_of(comment)
259+
{:ok, article_thread} = thread_of(article)
260+
261+
{:ok, %{total_count: total_count}} =
262+
CMS.paged_folded_comments(article_thread, article.id, %{page: 1, size: 1})
263+
264+
meta = article.meta |> Map.put(:folded_comment_count, total_count)
265+
article |> ORM.update_meta(meta)
266+
end)
267+
|> Repo.transaction()
268+
|> result()
269+
end
270+
252271
defp update_article_author_upvoted_info(%Comment{} = comment, user_id) do
253272
with {:ok, article} = get_full_comment(comment.id) do
254273
is_article_author_upvoted = article.author.id == user_id
@@ -367,9 +386,9 @@ defmodule GroupherServer.CMS.Delegate.CommentAction do
367386
defp result({:ok, %{create_comment: result}}), do: {:ok, result}
368387
defp result({:ok, %{add_reply_to: result}}), do: {:ok, result}
369388
defp result({:ok, %{upvote_comment_done: result}}), do: {:ok, result}
370-
defp result({:ok, %{fold_comment_report_too_many: result}}), do: {:ok, result}
371389
defp result({:ok, %{update_comment_flag: result}}), do: {:ok, result}
372390
defp result({:ok, %{delete_comment: result}}), do: {:ok, result}
391+
defp result({:ok, %{fold_comment: result}}), do: {:ok, result}
373392

374393
defp result({:error, :create_comment, result, _steps}) do
375394
raise_error(:create_comment, result)

lib/groupher_server/cms/delegates/comment_curd.ex

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,21 @@ defmodule GroupherServer.CMS.Delegate.CommentCurd do
66
import Helper.Utils, only: [done: 1, ensure: 2]
77
import Helper.ErrorCode
88

9-
import GroupherServer.CMS.Delegate.Helper, only: [mark_viewer_emotion_states: 3]
9+
import GroupherServer.CMS.Delegate.Helper,
10+
only: [mark_viewer_emotion_states: 3, article_of: 1, thread_of: 1]
11+
1012
import GroupherServer.CMS.Helper.Matcher
1113
import ShortMaps
1214

1315
alias Helper.Types, as: T
14-
alias Helper.{Later, ORM, QueryBuilder, Converter}
1516
alias GroupherServer.{Accounts, CMS, Repo}
16-
alias CMS.Model.Post
17-
alias CMS.Delegate.Hooks
1817

1918
alias Accounts.Model.User
20-
alias CMS.Model.{Comment, PinnedComment, Embeds}
19+
alias CMS.Model.{Post, Comment, PinnedComment, Embeds}
20+
21+
alias CMS.Delegate.Hooks
22+
alias Helper.{Later, ORM, QueryBuilder, Converter}
23+
2124
alias Ecto.Multi
2225

2326
@max_participator_count Comment.max_participator_count()
@@ -259,10 +262,7 @@ defmodule GroupherServer.CMS.Delegate.CommentCurd do
259262
end
260263

261264
# add participator to article-like(Post, Job ...) and update count
262-
def add_participant_to_article(
263-
%{comments_participants: participants} = article,
264-
%User{} = user
265-
) do
265+
def add_participant_to_article(%{comments_participants: participants} = article, %User{} = user) do
266266
total_participants = participants |> List.insert_at(0, user) |> Enum.uniq_by(& &1.id)
267267

268268
latest_participants = total_participants |> Enum.slice(0, @max_participator_count)
@@ -280,10 +280,12 @@ defmodule GroupherServer.CMS.Delegate.CommentCurd do
280280
# update comment's parent article's comments total count
281281
@spec update_comments_count(Comment.t(), :inc | :dec) :: Comment.t()
282282
def update_comments_count(%Comment{} = comment, opt) do
283-
with {:ok, article_info} <- match(:comment_article, comment),
284-
{:ok, article} <- ORM.find(article_info.model, article_info.id) do
283+
with {:ok, article} <- article_of(comment),
284+
{:ok, article_thread} <- thread_of(article) do
285+
foreign_key = :"#{article_thread}_id"
286+
285287
{:ok, cur_count} =
286-
from(c in Comment, where: field(c, ^article_info.foreign_key) == ^article_info.id)
288+
from(c in Comment, where: field(c, ^foreign_key) == ^article.id)
287289
|> ORM.count()
288290

289291
# dec 是 comment 还没有删除的时候的操作,和 inc 不同

lib/groupher_server/cms/delegates/document.ex

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ defmodule GroupherServer.CMS.Delegate.Document do
33
CURD operation on post/job ...
44
"""
55
import Ecto.Query, warn: false
6-
import Helper.Utils, only: [done: 1, thread_of_article: 2, get_config: 2]
6+
import Helper.Utils, only: [done: 1, thread_of: 2, get_config: 2]
77

88
import Helper.ErrorCode
99
import ShortMaps
@@ -28,7 +28,7 @@ defmodule GroupherServer.CMS.Delegate.Document do
2828

2929
# for create artilce step in Multi.new
3030
def create(article, %{body: body} = attrs) do
31-
with {:ok, article_thread} <- thread_of_article(article, :upcase),
31+
with {:ok, article_thread} <- thread_of(article, :upcase),
3232
{:ok, parsed} <- Converter.Article.parse_body(body) do
3333
attrs = Map.take(parsed, [:body, :body_html])
3434

@@ -59,7 +59,7 @@ defmodule GroupherServer.CMS.Delegate.Document do
5959
update both article and thread document
6060
"""
6161
def update(article, %{body: body} = attrs) when not is_nil(body) do
62-
with {:ok, article_thread} <- thread_of_article(article, :upcase),
62+
with {:ok, article_thread} <- thread_of(article, :upcase),
6363
{:ok, article_doc} <- find_article_document(article_thread, article),
6464
{:ok, thread_doc} <- find_thread_document(article_thread, article),
6565
{:ok, parsed} <- Converter.Article.parse_body(body) do
@@ -82,7 +82,7 @@ defmodule GroupherServer.CMS.Delegate.Document do
8282

8383
# 只更新 title 的情况
8484
def update(article, %{title: title} = attrs) do
85-
with {:ok, article_thread} <- thread_of_article(article, :upcase),
85+
with {:ok, article_thread} <- thread_of(article, :upcase),
8686
{:ok, article_doc} <- find_article_document(article_thread, article) do
8787
article_doc |> ORM.update(%{title: attrs.title})
8888
end

lib/groupher_server/cms/delegates/helper.ex

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@ defmodule GroupherServer.CMS.Delegate.Helper do
22
@moduledoc """
33
helpers for GroupherServer.CMS.Delegate
44
"""
5-
import Helper.Utils, only: [get_config: 2, done: 1, strip_struct: 1]
65
import Ecto.Query, warn: false
76
import GroupherServer.CMS.Helper.Matcher
87
import ShortMaps
8+
import Helper.Utils, only: [get_config: 2, done: 1, strip_struct: 1]
99

1010
alias Helper.{ORM, QueryBuilder}
1111
alias GroupherServer.{Accounts, Repo, CMS}
@@ -42,13 +42,29 @@ defmodule GroupherServer.CMS.Delegate.Helper do
4242
end
4343

4444
@doc "get parent article of a comment"
45-
def parent_article_of(%Comment{} = comment) do
46-
article_thread = comment.thread |> String.downcase() |> String.to_atom()
45+
def article_of(%Comment{} = comment) do
46+
with {:ok, article_thread} <- thread_of(comment) do
47+
comment |> Repo.preload(article_thread) |> Map.get(article_thread) |> done
48+
end
49+
end
50+
51+
def article_of(_), do: {:error, "only support comment"}
4752

48-
comment |> Repo.preload(article_thread) |> Map.get(article_thread) |> done
53+
# get thread of comment
54+
def thread_of(%Comment{thread: thread}) do
55+
thread |> String.downcase() |> String.to_atom() |> done
4956
end
5057

51-
def parent_article_of(_), do: {:error, "only support comment"}
58+
# get thread of article
59+
def thread_of(%{meta: %{thread: thread}}) do
60+
thread |> String.downcase() |> String.to_atom() |> done
61+
end
62+
63+
def thread_of(_), do: {:error, "invalid article"}
64+
65+
def thread_of(%{meta: %{thread: thread}}, :upcase) do
66+
thread |> to_string |> String.upcase() |> done
67+
end
5268

5369
#######
5470
# emotion related

lib/groupher_server/cms/delegates/hooks/cite.ex

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,10 @@ defmodule GroupherServer.CMS.Delegate.Hooks.Cite do
2929
"""
3030

3131
import Ecto.Query, warn: false
32-
import Helper.Utils, only: [get_config: 2, thread_of_article: 1]
32+
import Helper.Utils, only: [get_config: 2]
33+
3334
import GroupherServer.CMS.Helper.Matcher
34-
import GroupherServer.CMS.Delegate.Helper, only: [preload_author: 1]
35+
import GroupherServer.CMS.Delegate.Helper, only: [preload_author: 1, thread_of: 1]
3536
import GroupherServer.CMS.Delegate.Hooks.Helper, only: [merge_same_block_linker: 2]
3637

3738
import Helper.ErrorCode
@@ -221,7 +222,7 @@ defmodule GroupherServer.CMS.Delegate.Hooks.Cite do
221222
# cite article in article
222223
# 文章之间相互引用
223224
defp shape(article, %{type: :article, artiment: cited}, block_id) do
224-
{:ok, thread} = thread_of_article(article)
225+
{:ok, thread} = thread_of(article)
225226
{:ok, info} = match(thread)
226227

227228
%{
@@ -241,7 +242,7 @@ defmodule GroupherServer.CMS.Delegate.Hooks.Cite do
241242
# cite comment in article
242243
# 文章中引用评论
243244
defp shape(article, %{type: :comment, artiment: cited}, block_id) do
244-
{:ok, thread} = thread_of_article(article)
245+
{:ok, thread} = thread_of(article)
245246
{:ok, info} = match(thread)
246247

247248
%{

lib/groupher_server/cms/delegates/hooks/mention.ex

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ defmodule GroupherServer.CMS.Delegate.Hooks.Mention do
55
parse and fmt(see shape function) mentions to Delivery module
66
"""
77
import Ecto.Query, warn: false
8-
import Helper.Utils, only: [get_config: 2, thread_of_article: 1]
8+
import Helper.Utils, only: [get_config: 2]
99

10-
import GroupherServer.CMS.Delegate.Helper, only: [preload_author: 1, author_of: 1]
10+
import GroupherServer.CMS.Delegate.Helper, only: [preload_author: 1, author_of: 1, thread_of: 1]
1111
import GroupherServer.CMS.Delegate.Hooks.Helper, only: [merge_same_block_linker: 2]
1212

1313
alias GroupherServer.{Accounts, CMS, Delivery, Repo}
@@ -91,7 +91,7 @@ defmodule GroupherServer.CMS.Delegate.Hooks.Mention do
9191
end
9292

9393
defp shape(article, to_user_id, block_id) do
94-
{:ok, thread} = thread_of_article(article)
94+
{:ok, thread} = thread_of(article)
9595

9696
%{
9797
thread: thread,

0 commit comments

Comments
 (0)