Skip to content
This repository was archived by the owner on Nov 8, 2022. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all 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
4 changes: 2 additions & 2 deletions lib/groupher_server/cms/cms.ex
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,8 @@ defmodule GroupherServer.CMS do

defdelegate mark_delete_article(thread, id), to: ArticleCURD
defdelegate undo_mark_delete_article(thread, id), to: ArticleCURD
defdelegate remove_article(thread, id), to: ArticleCURD
defdelegate remove_article(thread, id, reason), to: ArticleCURD
defdelegate delete_article(article), to: ArticleCURD
defdelegate delete_article(article, reason), to: ArticleCURD

defdelegate update_active_timestamp(thread, article), to: ArticleCURD
defdelegate sink_article(thread, id), to: ArticleCURD
Expand Down
54 changes: 32 additions & 22 deletions lib/groupher_server/cms/delegates/article_curd.ex
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,15 @@ defmodule GroupherServer.CMS.Delegate.ArticleCURD do
import GroupherServer.CMS.Helper.Matcher

import Helper.Utils,
only: [done: 1, pick_by: 2, module_to_atom: 1, get_config: 2, ensure: 2, module_to_upcase: 1]
only: [
done: 1,
pick_by: 2,
module_to_atom: 1,
get_config: 2,
ensure: 2,
module_to_upcase: 1,
thread_of_article: 1
]

import GroupherServer.CMS.Delegate.Helper, only: [mark_viewer_emotion_states: 2]
import Helper.ErrorCode
Expand Down Expand Up @@ -328,26 +336,28 @@ defmodule GroupherServer.CMS.Delegate.ArticleCURD do
@doc """
remove article forever
"""
def remove_article(thread, id, reason \\ @remove_article_hint) do
with {:ok, info} <- match(thread),
{:ok, article} <- ORM.find(info.model, id, preload: [:communities, [author: :user]]) do
Multi.new()
|> Multi.run(:remove_article, fn _, _ ->
article |> ORM.delete()
end)
|> Multi.run(:update_community_article_count, fn _, _ ->
CommunityCURD.update_community_count_field(article.communities, thread)
end)
|> Multi.run(:update_user_published_meta, fn _, _ ->
Accounts.update_published_states(article.author.user.id, thread)
end)
|> Multi.run(:delete_document, fn _, _ ->
Document.remove(thread, id)
end)
# TODO: notify author
|> Repo.transaction()
|> result()
end
def delete_article(article, reason \\ @remove_article_hint) do
article = Repo.preload(article, [:communities, [author: :user]])
{:ok, thread} = thread_of_article(article)

Multi.new()
|> Multi.run(:delete_article, fn _, _ ->
article |> ORM.delete()
end)
|> Multi.run(:update_community_article_count, fn _, _ ->
CommunityCURD.update_community_count_field(article.communities, thread)
end)
|> Multi.run(:update_user_published_meta, fn _, _ ->
Accounts.update_published_states(article.author.user.id, thread)
end)
|> Multi.run(:delete_document, fn _, _ ->
Document.remove(thread, article.id)
# for those history & test setup case
{:ok, :pass}
end)
# TODO: notify author
|> Repo.transaction()
|> result()
end

@spec ensure_author_exists(User.t()) :: {:ok, User.t()}
Expand Down Expand Up @@ -497,7 +507,7 @@ defmodule GroupherServer.CMS.Delegate.ArticleCURD do

defp result({:ok, %{update_edit_status: result}}), do: {:ok, result}
defp result({:ok, %{update_article: result}}), do: {:ok, result}
defp result({:ok, %{remove_article: result}}), do: {:ok, result}
defp result({:ok, %{delete_article: result}}), do: {:ok, result}
# NOTE: for read article, order is import
defp result({:ok, %{set_viewer_has_states: result}}), do: result |> done()
defp result({:ok, %{update_article_meta: result}}), do: {:ok, result}
Expand Down
4 changes: 3 additions & 1 deletion lib/groupher_server_web/resolvers/cms_resolver.ex
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,9 @@ defmodule GroupherServerWeb.Resolvers.CMS do
CMS.update_article(article, args)
end

def delete_article(_root, %{passport_source: content}, _info), do: ORM.delete(content)
def delete_article(_root, %{passport_source: article}, _info) do
CMS.delete_article(article)
end

# #######################
# article actions
Expand Down
2 changes: 1 addition & 1 deletion test/groupher_server/cms/articles/blog_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ defmodule GroupherServer.Test.Articles.Blog do
{:ok, _article_doc} = ORM.find_by(ArticleDocument, %{article_id: blog.id, thread: "BLOG"})
{:ok, _blog_doc} = ORM.find_by(BlogDocument, %{blog_id: blog.id})

CMS.remove_article(:blog, blog.id)
{:ok, _} = CMS.delete_article(blog)

{:error, _} = ORM.find(Blog, blog.id)
{:error, _} = ORM.find_by(ArticleDocument, %{article_id: blog.id, thread: "BLOG"})
Expand Down
2 changes: 1 addition & 1 deletion test/groupher_server/cms/articles/job_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ defmodule GroupherServer.Test.Articles.Job do
{:ok, _article_doc} = ORM.find_by(ArticleDocument, %{article_id: job.id, thread: "JOB"})
{:ok, _job_doc} = ORM.find_by(JobDocument, %{job_id: job.id})

CMS.remove_article(:job, job.id)
{:ok, _} = CMS.delete_article(job)

{:error, _} = ORM.find(Job, job.id)
{:error, _} = ORM.find_by(ArticleDocument, %{article_id: job.id, thread: "JOB"})
Expand Down
2 changes: 1 addition & 1 deletion test/groupher_server/cms/articles/post_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ defmodule GroupherServer.Test.CMS.Articles.Post do
{:ok, _article_doc} = ORM.find_by(ArticleDocument, %{article_id: post.id, thread: "POST"})
{:ok, _post_doc} = ORM.find_by(PostDocument, %{post_id: post.id})

CMS.remove_article(:post, post.id)
{:ok, _} = CMS.delete_article(post)

{:error, _} = ORM.find(Post, post.id)
{:error, _} = ORM.find_by(ArticleDocument, %{article_id: post.id, thread: "POST"})
Expand Down