Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings
This repository was archived by the owner on Nov 8, 2022. It is now read-only.

refactor: enhance article move & mirror#436

Merged
mydearxym merged 6 commits intodevfromenhance-article-move-mirror
Oct 15, 2021
Merged
Show file tree
Hide file tree
Changes fromall commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletionslib/groupher_server/cms/cms.ex
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -136,8 +136,13 @@ defmodule GroupherServer.CMS do

# >> community: set / unset
defdelegate mirror_article(thread, article_id, community_id), to: ArticleCommunity
defdelegate mirror_article(thread, article_id, community_id, article_ids), to: ArticleCommunity
defdelegate unmirror_article(thread, article_id, community_id), to: ArticleCommunity
defdelegate move_article(thread, article_id, community_id), to: ArticleCommunity
defdelegate move_article(thread, article_id, community_id, article_ids), to: ArticleCommunity

defdelegate move_to_blackhole(thread, article_id, article_ids), to: ArticleCommunity
defdelegate move_to_blackhole(thread, article_id), to: ArticleCommunity

defdelegate emotion_to_article(thread, article_id, args, user), to: ArticleEmotion
defdelegate undo_emotion_to_article(thread, article_id, args, user), to: ArticleEmotion
Expand Down
88 changes: 64 additions & 24 deletionslib/groupher_server/cms/delegates/article_community.ex
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -12,8 +12,9 @@ defmodule GroupherServer.CMS.Delegate.ArticleCommunity do
alias Helper.Types, as: T
alias Helper.ORM

alias GroupherServer.CMS.Model.{Embeds, Community, PinnedArticle}
alias GroupherServer.Repo
alias GroupherServer.{CMS, Repo}
alias CMS.Model.{Embeds, Community, PinnedArticle}
alias CMS.Delegate.{ArticleTag}

alias Ecto.Multi

Expand DownExpand Up@@ -58,33 +59,45 @@ defmodule GroupherServer.CMS.Delegate.ArticleCommunity do
@doc """
mirror article to other community
"""
def mirror_article(thread, article_id, community_id) do
def mirror_article(thread, article_id, community_id, article_tag_ids \\ []) do
with {:ok, info} <- match(thread),
{:ok, article} <- ORM.find(info.model, article_id, preload: :communities),
{:ok, community} <- ORM.find(Community, community_id) do
article
|> Ecto.Changeset.change()
|> Ecto.Changeset.put_assoc(:communities, article.communities ++ [community])
|> Repo.update()
Multi.new()
|> Multi.run(:mirror_target_community, fn _, _ ->
article
|> Ecto.Changeset.change()
|> Ecto.Changeset.put_assoc(:communities, article.communities ++ [community])
|> Repo.update()
end)
|> Multi.run(:set_target_tags, fn _, %{mirror_target_community: article} ->
ArticleTag.set_article_tags(community, thread, article, %{article_tags: article_tag_ids})
end)
|> Repo.transaction()
|> result()
end
end

@doc """
unmirror article to a community
"""
def unmirror_article(thread, article_id, community_id) do
preload = [:communities, :original_community, :article_tags]

with {:ok, info} <- match(thread),
{:ok, article} <-
ORM.find(info.model, article_id, preload: [:communities, :original_community]),
{:ok, article} <- ORM.find(info.model, article_id, preload: preload),
{:ok, community} <- ORM.find(Community, community_id) do
case article.original_community.id == community.id do
true ->
raise_error(:mirror_article, "can not unmirror original_community")

false ->
article_tags = tags_without_community(article, community)

article
|> Ecto.Changeset.change()
|> Ecto.Changeset.put_assoc(:communities, article.communities -- [community])
|> Ecto.Changeset.put_assoc(:article_tags, article_tags)
|> Repo.update()
end
end
Expand All@@ -93,40 +106,57 @@ defmodule GroupherServer.CMS.Delegate.ArticleCommunity do
@doc """
move article original community to other community
"""
def move_article(thread, article_id, community_id) do
def move_article(thread, article_id, community_id, article_tag_ids \\ []) do
preload = [:communities, :original_community, :article_tags]

with {:ok, info} <- match(thread),
{:ok, community} <- ORM.find(Community, community_id),
{:ok, article} <-
ORM.find(info.model, article_id, preload: [:communities, :original_community]) do
cur_original_community = article.original_community
{:ok, article} <- ORM.find(info.model, article_id, preload: preload) do
original_community = article.original_community

Multi.new()
|> Multi.run(:change_original_community, fn _, _ ->
|> Multi.run(:move_article, fn _, _ ->
communities = (article.communities -- [original_community]) ++ [community]
article_tags = tags_without_community(article, original_community)

article
|> Ecto.Changeset.change()
|> Ecto.Changeset.put_change(:original_community_id, community.id)
|> Ecto.Changeset.put_assoc(:communities, communities)
|> Ecto.Changeset.put_assoc(:article_tags, article_tags)
|> Repo.update()
end)
|> Multi.run(:unmirror_article, fn _, %{change_original_community: article} ->
article
|> Ecto.Changeset.change()
|> Ecto.Changeset.put_assoc(:communities, article.communities -- [cur_original_community])
|> Repo.update()
|> Multi.run(:set_target_tags, fn _, %{move_article: article} ->
ArticleTag.set_article_tags(community, thread, article, %{article_tags: article_tag_ids})
end)
|> Multi.run(:mirror_target_community, fn _, %{unmirror_article: article} ->
|> Repo.transaction()
|> result()
end
end

def move_to_blackhole(thread, article_id, article_tag_ids \\ []) do
preload = [:communities, :original_community, :article_tags]

with {:ok, info} <- match(thread),
{:ok, community} <- ORM.find_by(Community, %{raw: "blackhole"}),
{:ok, article} <- ORM.find(info.model, article_id, preload: preload) do
Multi.new()
|> Multi.run(:set_community, fn _, _ ->
article
|> Ecto.Changeset.change()
|> Ecto.Changeset.put_assoc(:communities, article.communities ++ [community])
|> Ecto.Changeset.put_change(:original_community_id, community.id)
|> Ecto.Changeset.put_assoc(:communities, [community])
|> Ecto.Changeset.put_assoc(:article_tags, [])
|> Repo.update()
end)
|> Multi.run(:set_target_tags, fn _, %{set_community: article} ->
ArticleTag.set_article_tags(community, thread, article, %{article_tags: article_tag_ids})
end)
|> Repo.transaction()
|> result()
end
end

defp result({:ok, %{mirror_target_community: result}}), do: result |> done()
defp result({:error, _, result, _steps}), do: {:error, result}

@doc "update isEdited meta label if needed"
# TODO: diff history
def update_edit_status(%{meta: %Embeds.ArticleMeta{is_edited: _} = meta} = content) do
Expand DownExpand Up@@ -157,4 +187,14 @@ defmodule GroupherServer.CMS.Delegate.ArticleCommunity do
_ -> {:ok, :pass}
end
end

defp tags_without_community(article, %Community{id: community_id}) do
%{article_tags: article_tags} = article
article_tags -- Enum.filter(article_tags, &(&1.community_id === community_id))
end

defp result({:ok, %{set_target_tags: result}}), do: result |> done()
defp result({:ok, %{mirror_target_community: result}}), do: result |> done()

defp result({:error, _, result, _steps}), do: {:error, result}
end
11 changes: 8 additions & 3 deletionslib/groupher_server/cms/delegates/article_tag.ex
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -78,6 +78,8 @@ defmodule GroupherServer.CMS.Delegate.ArticleTag do
article_tag_ids = Enum.map(article_tag_ids, &to_string(&1))

Enum.all?(article_tag_ids, &Enum.member?(domain_tags_ids, &1))
else
_ -> false
end
end

Expand All@@ -86,17 +88,20 @@ defmodule GroupherServer.CMS.Delegate.ArticleTag do

used for create article with article_tags in args
"""
def set_article_tags(_, _, article, %{article_tags: []}), do: {:ok, article}

def set_article_tags(%Community{id: cid}, thread, article, %{article_tags: article_tag_ids}) do
check_filter = %{page: 1, size: 100, community_id: cid, thread: thread}

with true <- is_article_tag_in_some_thread?(article_tag_ids, check_filter) do
Enum.each(article_tag_ids, &set_article_tag(thread, article, &1)) |> done
with true <- is_article_tag_in_some_thread?(article_tag_ids, check_filter),
Enum.each(article_tag_ids, &set_article_tag(thread, article, &1)) |> done do
{:ok, article}
else
false -> raise_error(:invalid_domain_tag, "tag not in same community & thread")
end
end

def set_article_tags(_community, _thread,_id, _attrs), do: {:ok,:pass}
def set_article_tags(_community, _thread,article, _), do: {:ok,article}

@doc """
set article a tag
Expand Down
8 changes: 6 additions & 2 deletionslib/groupher_server_web/resolvers/cms_resolver.ex
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -293,8 +293,12 @@ defmodule GroupherServerWeb.Resolvers.CMS do
CMS.unmirror_article(thread, id, community_id)
end

def move_article(_root, ~m(thread id community_id)a, _info) do
CMS.move_article(thread, id, community_id)
def move_article(_root, ~m(thread id community_id article_tags)a, _info) do
CMS.move_article(thread, id, community_id, article_tags)
end

def move_to_blackhole(_root, ~m(thread id article_tags)a, _info) do
CMS.move_to_blackhole(thread, id, article_tags)
end

# #######################
Expand Down
13 changes: 13 additions & 0 deletionslib/groupher_server_web/schema/cms/mutations/operation.ex
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -112,6 +112,7 @@ defmodule GroupherServerWeb.Schema.CMS.Mutations.Operation do
arg(:id, non_null(:id))
arg(:community_id, non_null(:id))
arg(:thread, :thread, default_value: :post)
arg(:article_tags, list_of(:id), default_value: [])

middleware(M.Authorize, :login)
middleware(M.Passport, claim: "cms->t?.community.mirror")
Expand All@@ -134,10 +135,22 @@ defmodule GroupherServerWeb.Schema.CMS.Mutations.Operation do
arg(:id, non_null(:id))
arg(:community_id, non_null(:id))
arg(:thread, :thread, default_value: :post)
arg(:article_tags, list_of(:id), default_value: [])

middleware(M.Authorize, :login)
middleware(M.Passport, claim: "cms->t?.community.move")
resolve(&R.CMS.move_article/3)
end

@desc "move article to other community"
field :move_to_blackhole, :article do
arg(:id, non_null(:id))
arg(:thread, :thread, default_value: :post)
arg(:article_tags, list_of(:id), default_value: [])

middleware(M.Authorize, :login)
middleware(M.Passport, claim: "cms->blackeye")
resolve(&R.CMS.move_to_blackhole/3)
end
end
end
1 change: 1 addition & 0 deletionslib/helper/certification.ex
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -68,6 +68,7 @@ defmodule Helper.Certification do
build_article_rules(@article_rules) ++
[
"root",
"blackeye",
"system_accountant",
"system_notification.publish",
"stamp_passport",
Expand Down
Loading

[8]ページ先頭

©2009-2025 Movatter.jp