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

Commit 0d5289c

Browse files
authored
refactor: move set/unset article from/to community (#356)
* refactor: move set/unset article from/to community * refactor: fix test error * refactor: fix naming test error * refactor: wip * refactor: naming * refactor: naming * refactor: naming * refactor: move logic adjust
1 parent 3dae2fc commit 0d5289c

File tree

29 files changed

+668
-434
lines changed

29 files changed

+668
-434
lines changed

cover/excoveralls.json

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.

lib/groupher_server/cms/cms.ex

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ defmodule GroupherServer.CMS do
1010
alias Delegate.{
1111
AbuseReport,
1212
ArticleCURD,
13-
ArticleOperation,
13+
ArticleCommunity,
1414
ArticleEmotion,
1515
ArticleComment,
1616
ArticleCollect,
@@ -97,20 +97,21 @@ defmodule GroupherServer.CMS do
9797
defdelegate set_collect_folder(collect, folder), to: ArticleCollect
9898
defdelegate undo_set_collect_folder(collect, folder), to: ArticleCollect
9999

100-
# ArticleOperation
100+
# ArticleCommunity
101101
# >> set flag on article, like: pin / unpin article
102-
defdelegate set_community_flags(community_info, queryable, attrs), to: ArticleOperation
103-
defdelegate pin_article(thread, id, community_id), to: ArticleOperation
104-
defdelegate undo_pin_article(thread, id, community_id), to: ArticleOperation
102+
defdelegate set_community_flags(community_info, queryable, attrs), to: ArticleCommunity
103+
defdelegate pin_article(thread, id, community_id), to: ArticleCommunity
104+
defdelegate undo_pin_article(thread, id, community_id), to: ArticleCommunity
105105

106-
defdelegate lock_article_comment(content), to: ArticleOperation
106+
defdelegate lock_article_comment(content), to: ArticleCommunity
107107

108108
# >> tag: set / unset
109-
defdelegate set_tag(thread, tag, content_id), to: ArticleOperation
110-
defdelegate unset_tag(thread, tag, content_id), to: ArticleOperation
109+
defdelegate set_tag(thread, tag, content_id), to: ArticleCommunity
110+
defdelegate unset_tag(thread, tag, content_id), to: ArticleCommunity
111111
# >> community: set / unset
112-
defdelegate set_community(community, thread, content_id), to: ArticleOperation
113-
defdelegate unset_community(community, thread, content_id), to: ArticleOperation
112+
defdelegate mirror_article(thread, article_id, community_id), to: ArticleCommunity
113+
defdelegate unmirror_article(thread, article_id, community_id), to: ArticleCommunity
114+
defdelegate move_article(thread, article_id, community_id), to: ArticleCommunity
114115

115116
defdelegate emotion_to_article(thread, article_id, args, user), to: ArticleEmotion
116117
defdelegate undo_emotion_to_article(thread, article_id, args, user), to: ArticleEmotion

lib/groupher_server/cms/delegates/article_operation.ex renamed to lib/groupher_server/cms/delegates/article_community.ex

Lines changed: 67 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
1-
defmodule GroupherServer.CMS.Delegate.ArticleOperation do
1+
defmodule GroupherServer.CMS.Delegate.ArticleCommunity do
22
@moduledoc """
33
set / unset operations for Article-like resource
44
"""
55
import GroupherServer.CMS.Helper.Matcher
6+
import GroupherServer.CMS.Helper.Matcher2
67
import Ecto.Query, warn: false
78

89
import Helper.ErrorCode
910
import ShortMaps
10-
import Helper.Utils, only: [strip_struct: 1]
11+
import Helper.Utils, only: [strip_struct: 1, done: 1]
1112
import GroupherServer.CMS.Helper.Matcher2
1213

1314
alias Helper.Types, as: T
@@ -28,6 +29,8 @@ defmodule GroupherServer.CMS.Delegate.ArticleOperation do
2829
alias GroupherServer.CMS.Repo, as: CMSRepo
2930
alias GroupherServer.Repo
3031

32+
alias Ecto.Multi
33+
3134
@max_pinned_article_count_per_thread Community.max_pinned_article_count_per_thread()
3235

3336
@spec pin_article(T.article_thread(), Integer.t(), Integer.t()) :: {:ok, PinnedArticle.t()}
@@ -102,30 +105,77 @@ defmodule GroupherServer.CMS.Delegate.ArticleOperation do
102105
end
103106

104107
@doc """
105-
set content to diffent community
108+
mirror article to other community
106109
"""
107-
def set_community(%Community{id: community_id}, thread, content_id) do
108-
with {:ok, action} <- match_action(thread, :community),
109-
{:ok, content} <- ORM.find(action.target, content_id, preload: :communities),
110-
{:ok, community} <- ORM.find(action.reactor, community_id) do
111-
content
110+
def mirror_article(thread, article_id, community_id) do
111+
with {:ok, info} <- match(thread),
112+
{:ok, article} <- ORM.find(info.model, article_id, preload: :communities),
113+
{:ok, community} <- ORM.find(Community, community_id) do
114+
article
112115
|> Ecto.Changeset.change()
113-
|> Ecto.Changeset.put_assoc(:communities, content.communities ++ [community])
116+
|> Ecto.Changeset.put_assoc(:communities, article.communities ++ [community])
114117
|> Repo.update()
115118
end
116119
end
117120

118-
def unset_community(%Community{id: community_id}, thread, content_id) do
119-
with {:ok, action} <- match_action(thread, :community),
120-
{:ok, content} <- ORM.find(action.target, content_id, preload: :communities),
121-
{:ok, community} <- ORM.find(action.reactor, community_id) do
122-
content
123-
|> Ecto.Changeset.change()
124-
|> Ecto.Changeset.put_assoc(:communities, content.communities -- [community])
125-
|> Repo.update()
121+
@doc """
122+
unmirror article to a community
123+
"""
124+
def unmirror_article(thread, article_id, community_id) do
125+
with {:ok, info} <- match(thread),
126+
{:ok, article} <-
127+
ORM.find(info.model, article_id, preload: [:communities, :original_community]),
128+
{:ok, community} <- ORM.find(Community, community_id) do
129+
case article.original_community.id == community.id do
130+
true ->
131+
raise_error(:mirror_article, "can not unmirror original_community")
132+
133+
false ->
134+
article
135+
|> Ecto.Changeset.change()
136+
|> Ecto.Changeset.put_assoc(:communities, article.communities -- [community])
137+
|> Repo.update()
138+
end
126139
end
127140
end
128141

142+
@doc """
143+
move article original community to other community
144+
"""
145+
def move_article(thread, article_id, community_id) do
146+
with {:ok, info} <- match(thread),
147+
{:ok, community} <- ORM.find(Community, community_id),
148+
{:ok, article} <-
149+
ORM.find(info.model, article_id, preload: [:communities, :original_community]) do
150+
cur_original_community = article.original_community
151+
152+
Multi.new()
153+
|> Multi.run(:change_original_community, fn _, _ ->
154+
article
155+
|> Ecto.Changeset.change()
156+
|> Ecto.Changeset.put_change(:original_community_id, community.id)
157+
|> Repo.update()
158+
end)
159+
|> Multi.run(:unmirror_article, fn _, %{change_original_community: article} ->
160+
article
161+
|> Ecto.Changeset.change()
162+
|> Ecto.Changeset.put_assoc(:communities, article.communities -- [cur_original_community])
163+
|> Repo.update()
164+
end)
165+
|> Multi.run(:mirror_target_community, fn _, %{unmirror_article: article} ->
166+
article
167+
|> Ecto.Changeset.change()
168+
|> Ecto.Changeset.put_assoc(:communities, article.communities ++ [community])
169+
|> Repo.update()
170+
end)
171+
|> Repo.transaction()
172+
|> result()
173+
end
174+
end
175+
176+
defp result({:ok, %{mirror_target_community: result}}), do: result |> done()
177+
defp result({:error, _, result, _steps}), do: {:error, result}
178+
129179
@doc """
130180
set general tag for post / tuts ...
131181
"""

lib/groupher_server/cms/delegates/article_curd.ex

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ defmodule GroupherServer.CMS.Delegate.ArticleCURD do
2020
alias Accounts.User
2121
alias CMS.{Author, Community, PinnedArticle, Embeds, Delegate, Tag}
2222

23-
alias Delegate.ArticleOperation
23+
alias Delegate.ArticleCommunity
2424

2525
alias Ecto.Multi
2626

@@ -130,19 +130,19 @@ defmodule GroupherServer.CMS.Delegate.ArticleCURD do
130130
{:ok, community} <- ORM.find(Community, cid) do
131131
Multi.new()
132132
|> Multi.run(:create_article, fn _, _ ->
133-
do_create_content(action.target, attrs, author, community)
133+
do_create_article(action.target, attrs, author, community)
134134
end)
135-
|> Multi.run(:set_community, fn _, %{create_article: content} ->
136-
ArticleOperation.set_community(community, thread, content.id)
135+
|> Multi.run(:mirror_article, fn _, %{create_article: article} ->
136+
ArticleCommunity.mirror_article(thread, article.id, community.id)
137137
end)
138-
|> Multi.run(:set_community_flag, fn _, %{create_article: content} ->
139-
exec_set_community_flag(community, content, action)
138+
|> Multi.run(:set_community_flag, fn _, %{create_article: article} ->
139+
exec_set_community_flag(community, article, action)
140140
end)
141-
|> Multi.run(:set_tag, fn _, %{create_article: content} ->
142-
exec_set_tag(thread, content.id, attrs)
141+
|> Multi.run(:set_tag, fn _, %{create_article: article} ->
142+
exec_set_tag(thread, article.id, attrs)
143143
end)
144-
|> Multi.run(:mention_users, fn _, %{create_article: content} ->
145-
Delivery.mention_from_content(community.raw, thread, content, attrs, %User{id: uid})
144+
|> Multi.run(:mention_users, fn _, %{create_article: article} ->
145+
Delivery.mention_from_content(community.raw, thread, article, attrs, %User{id: uid})
146146
{:ok, :pass}
147147
end)
148148
|> Multi.run(:log_action, fn _, _ ->
@@ -160,7 +160,7 @@ defmodule GroupherServer.CMS.Delegate.ArticleCURD do
160160
"""
161161
def notify_admin_new_content(%{id: id} = result) do
162162
target = result.__struct__
163-
preload = [:origial_community, author: :user]
163+
preload = [:original_community, author: :user]
164164

165165
with {:ok, content} <- ORM.find(target, id, preload: preload) do
166166
info = %{
@@ -186,10 +186,10 @@ defmodule GroupherServer.CMS.Delegate.ArticleCURD do
186186
ORM.update(content, args)
187187
end)
188188
|> Multi.run(:update_edit_status, fn _, %{update_article: update_article} ->
189-
ArticleOperation.update_edit_status(update_article)
189+
ArticleCommunity.update_edit_status(update_article)
190190
end)
191191
|> Multi.run(:update_tag, fn _, _ ->
192-
# TODO: move it to ArticleOperation module
192+
# TODO: move it to ArticleCommunity module
193193
exec_update_tags(content, args)
194194
end)
195195
|> Repo.transaction()
@@ -368,7 +368,7 @@ defmodule GroupherServer.CMS.Delegate.ArticleCURD do
368368
{:error, [message: "create cms content author", code: ecode(:create_fails)]}
369369
end
370370

371-
defp create_content_result({:error, :set_community, _result, _steps}) do
371+
defp create_content_result({:error, :mirror_article, _result, _steps}) do
372372
{:error, [message: "set community", code: ecode(:create_fails)]}
373373
end
374374

@@ -385,21 +385,21 @@ defmodule GroupherServer.CMS.Delegate.ArticleCURD do
385385
end
386386

387387
# for create content step in Multi.new
388-
defp do_create_content(target, attrs, %Author{id: aid}, %Community{id: cid}) do
388+
defp do_create_article(target, attrs, %Author{id: aid}, %Community{id: cid}) do
389389
target
390390
|> struct()
391391
|> target.changeset(attrs)
392392
|> Ecto.Changeset.put_change(:emotions, @default_emotions)
393393
|> Ecto.Changeset.put_change(:author_id, aid)
394-
|> Ecto.Changeset.put_change(:origial_community_id, integerfy(cid))
394+
|> Ecto.Changeset.put_change(:original_community_id, integerfy(cid))
395395
|> Ecto.Changeset.put_embed(:meta, @default_article_meta)
396396
|> Repo.insert()
397397
end
398398

399399
defp exec_set_tag(thread, id, %{tags: tags}) do
400400
try do
401401
Enum.each(tags, fn tag ->
402-
{:ok, _} = ArticleOperation.set_tag(thread, %Tag{id: tag.id}, id)
402+
{:ok, _} = ArticleCommunity.set_tag(thread, %Tag{id: tag.id}, id)
403403
end)
404404

405405
{:ok, "psss"}
@@ -412,7 +412,7 @@ defmodule GroupherServer.CMS.Delegate.ArticleCURD do
412412

413413
# TODO: flag 逻辑似乎有问题
414414
defp exec_set_community_flag(%Community{} = community, content, %{flag: _flag}) do
415-
ArticleOperation.set_community_flags(community, content, %{
415+
ArticleCommunity.set_community_flags(community, content, %{
416416
trash: false
417417
})
418418
end

lib/groupher_server/cms/embeds/article_meta.ex

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ defmodule GroupherServer.CMS.Embeds.ArticleMeta do
66
use Accessible
77
import Ecto.Changeset
88

9+
alias GroupherServer.CMS
10+
911
@optional_fields ~w(is_edited is_comment_locked upvoted_user_ids collected_user_ids viewed_user_ids reported_user_ids reported_count)a
1012

1113
@default_meta %{

lib/groupher_server/cms/job.ex

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ defmodule GroupherServer.CMS.Job do
2424

2525
@timestamps_opts [type: :utc_datetime_usec]
2626
@required_fields ~w(title company company_logo body digest length)a
27-
@optional_fields ~w(origial_community_id desc company_link link_addr copy_right salary exp education field finance scale article_comments_count article_comments_participators_count upvotes_count collects_count)a
27+
@optional_fields ~w(original_community_id desc company_link link_addr copy_right salary exp education field finance scale article_comments_count article_comments_participators_count upvotes_count collects_count)a
2828

2929
@type t :: %Job{}
3030
schema "cms_jobs" do
@@ -87,7 +87,7 @@ defmodule GroupherServer.CMS.Job do
8787
on_replace: :delete
8888
)
8989

90-
belongs_to(:origial_community, Community)
90+
belongs_to(:original_community, Community)
9191

9292
many_to_many(
9393
:communities,

lib/groupher_server/cms/post.ex

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ defmodule GroupherServer.CMS.Post do
2626

2727
@timestamps_opts [type: :utc_datetime_usec]
2828
@required_fields ~w(title body digest length)a
29-
@optional_fields ~w(origial_community_id link_addr copy_right link_addr link_icon article_comments_count article_comments_participators_count upvotes_count collects_count)a
29+
@optional_fields ~w(original_community_id link_addr copy_right link_addr link_icon article_comments_count article_comments_participators_count upvotes_count collects_count)a
3030

3131
@type t :: %Post{}
3232
schema "cms_posts" do
@@ -86,7 +86,7 @@ defmodule GroupherServer.CMS.Post do
8686
on_replace: :delete
8787
)
8888

89-
belongs_to(:origial_community, Community)
89+
belongs_to(:original_community, Community)
9090

9191
many_to_many(
9292
:communities,

lib/groupher_server/cms/repo.ex

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ defmodule GroupherServer.CMS.Repo do
2525

2626
@timestamps_opts [type: :utc_datetime_usec]
2727
@required_fields ~w(title owner_name owner_url repo_url desc readme star_count issues_count prs_count fork_count watch_count upvotes_count collects_count)a
28-
@optional_fields ~w(origial_community_id last_sync homepage_url release_tag license)a
28+
@optional_fields ~w(original_community_id last_sync homepage_url release_tag license)a
2929

3030
@type t :: %Repo{}
3131
schema "cms_repos" do
@@ -84,7 +84,7 @@ defmodule GroupherServer.CMS.Repo do
8484
on_replace: :delete
8585
)
8686

87-
belongs_to(:origial_community, Community)
87+
belongs_to(:original_community, Community)
8888

8989
many_to_many(
9090
:communities,

lib/groupher_server_web/resolvers/cms_resolver.ex

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -290,12 +290,16 @@ defmodule GroupherServerWeb.Resolvers.CMS do
290290

291291
def community_subscribers(_root, _args, _info), do: {:error, "invalid args"}
292292

293-
def set_community(_root, ~m(thread id community_id)a, _info) do
294-
CMS.set_community(%Community{id: community_id}, thread, id)
293+
def mirror_article(_root, ~m(thread id community_id)a, _info) do
294+
CMS.mirror_article(thread, id, community_id)
295295
end
296296

297-
def unset_community(_root, ~m(thread id community_id)a, _info) do
298-
CMS.unset_community(%Community{id: community_id}, thread, id)
297+
def unmirror_article(_root, ~m(thread id community_id)a, _info) do
298+
CMS.unmirror_article(thread, id, community_id)
299+
end
300+
301+
def move_article(_root, ~m(thread id community_id)a, _info) do
302+
CMS.move_article(thread, id, community_id)
299303
end
300304

301305
# #######################

lib/groupher_server_web/schema/cms/cms_types.ex

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ defmodule GroupherServerWeb.Schema.CMS.Types do
6767
field(:tags, list_of(:tag), resolve: dataloader(CMS, :tags))
6868

6969
field(:author, :user, resolve: dataloader(CMS, :author))
70-
field(:origial_community, :community, resolve: dataloader(CMS, :origial_community))
70+
field(:original_community, :community, resolve: dataloader(CMS, :original_community))
7171
field(:communities, list_of(:community), resolve: dataloader(CMS, :communities))
7272

7373
field(:meta, :article_meta)
@@ -123,7 +123,7 @@ defmodule GroupherServerWeb.Schema.CMS.Types do
123123

124124
field(:author, :user, resolve: dataloader(CMS, :author))
125125
field(:tags, list_of(:tag), resolve: dataloader(CMS, :tags))
126-
field(:origial_community, :community, resolve: dataloader(CMS, :origial_community))
126+
field(:original_community, :community, resolve: dataloader(CMS, :original_community))
127127
field(:communities, list_of(:community), resolve: dataloader(CMS, :communities))
128128

129129
field(:meta, :article_meta)
@@ -177,7 +177,7 @@ defmodule GroupherServerWeb.Schema.CMS.Types do
177177
field(:last_sync, :datetime)
178178

179179
field(:tags, list_of(:tag), resolve: dataloader(CMS, :tags))
180-
field(:origial_community, :community, resolve: dataloader(CMS, :origial_community))
180+
field(:original_community, :community, resolve: dataloader(CMS, :original_community))
181181
field(:communities, list_of(:community), resolve: dataloader(CMS, :communities))
182182

183183
viewer_has_state_fields()

0 commit comments

Comments
 (0)