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

Commit 1b7ac5a

Browse files
committed
refactor: move set/unset article from/to community
1 parent 3dae2fc commit 1b7ac5a

File tree

19 files changed

+338
-63
lines changed

19 files changed

+338
-63
lines changed

lib/groupher_server/cms/cms.ex

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,8 +109,9 @@ defmodule GroupherServer.CMS do
109109
defdelegate set_tag(thread, tag, content_id), to: ArticleOperation
110110
defdelegate unset_tag(thread, tag, content_id), to: ArticleOperation
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 set_community(thread, article_id, community_id), to: ArticleOperation
113+
defdelegate unset_community(thread, article_id, community_id), to: ArticleOperation
114+
defdelegate move_article(thread, article_id, community_id), to: ArticleOperation
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_curd.ex

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -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(:set_community, fn _, %{create_article: article} ->
136+
ArticleOperation.set_community(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 = %{
@@ -385,13 +385,13 @@ 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

lib/groupher_server/cms/delegates/article_operation.ex

Lines changed: 34 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,12 @@ defmodule GroupherServer.CMS.Delegate.ArticleOperation do
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, integerfy: 1]
1112
import GroupherServer.CMS.Helper.Matcher2
1213

1314
alias Helper.Types, as: T
@@ -104,24 +105,44 @@ defmodule GroupherServer.CMS.Delegate.ArticleOperation do
104105
@doc """
105106
set content to diffent community
106107
"""
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
108+
def set_community(thread, article_id, community_id) do
109+
with {:ok, info} <- match(thread),
110+
{:ok, article} <- ORM.find(info.model, article_id, preload: :communities),
111+
{:ok, community} <- ORM.find(Community, community_id) do
112+
article
112113
|> Ecto.Changeset.change()
113-
|> Ecto.Changeset.put_assoc(:communities, content.communities ++ [community])
114+
|> Ecto.Changeset.put_assoc(:communities, article.communities ++ [community])
114115
|> Repo.update()
115116
end
116117
end
117118

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
119+
def unset_community(thread, article_id, community_id) do
120+
with {:ok, info} <- match(thread),
121+
{:ok, article} <-
122+
ORM.find(info.model, article_id, preload: [:communities, :original_community]),
123+
{:ok, community} <- ORM.find(Community, community_id) do
124+
case article.original_community.id == community_id do
125+
true ->
126+
raise_error(:mirror_community, "can not unmirror original_community")
127+
128+
false ->
129+
article
130+
|> Ecto.Changeset.change()
131+
|> Ecto.Changeset.put_assoc(:communities, article.communities -- [community])
132+
|> Repo.update()
133+
end
134+
end
135+
end
136+
137+
@doc """
138+
set article original community in meta
139+
"""
140+
def move_article(thread, article_id, community_id) do
141+
with {:ok, info} <- match(thread),
142+
{:ok, article} <- ORM.find(info.model, article_id) do
143+
article
123144
|> Ecto.Changeset.change()
124-
|> Ecto.Changeset.put_assoc(:communities, content.communities -- [community])
145+
|> Ecto.Changeset.put_change(:original_community_id, integerfy(community_id))
125146
|> Repo.update()
126147
end
127148
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: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -291,11 +291,11 @@ defmodule GroupherServerWeb.Resolvers.CMS do
291291
def community_subscribers(_root, _args, _info), do: {:error, "invalid args"}
292292

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

297297
def unset_community(_root, ~m(thread id community_id)a, _info) do
298-
CMS.unset_community(%Community{id: community_id}, thread, id)
298+
CMS.unset_community(thread, id, community_id)
299299
end
300300

301301
# #######################

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()

lib/helper/error_code.ex

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ defmodule Helper.ErrorCode do
4646
def ecode(:already_collected_in_folder), do: @article_base + 2
4747
def ecode(:delete_no_empty_collect_folder), do: @article_base + 3
4848
def ecode(:private_collect_folder), do: @article_base + 4
49+
def ecode(:mirror_community), do: @article_base + 5
4950

5051
def ecode, do: @default_base
5152
# def ecode(_), do: @default_base

0 commit comments

Comments
 (0)