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
2 changes: 1 addition & 1 deletion cover/excoveralls.json

Large diffs are not rendered by default.

21 changes: 11 additions & 10 deletions lib/groupher_server/cms/cms.ex
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ defmodule GroupherServer.CMS do
alias Delegate.{
AbuseReport,
ArticleCURD,
ArticleOperation,
ArticleCommunity,
ArticleEmotion,
ArticleComment,
ArticleCollect,
Expand Down Expand Up @@ -97,20 +97,21 @@ defmodule GroupherServer.CMS do
defdelegate set_collect_folder(collect, folder), to: ArticleCollect
defdelegate undo_set_collect_folder(collect, folder), to: ArticleCollect

# ArticleOperation
# ArticleCommunity
# >> set flag on article, like: pin / unpin article
defdelegate set_community_flags(community_info, queryable, attrs), to: ArticleOperation
defdelegate pin_article(thread, id, community_id), to: ArticleOperation
defdelegate undo_pin_article(thread, id, community_id), to: ArticleOperation
defdelegate set_community_flags(community_info, queryable, attrs), to: ArticleCommunity
defdelegate pin_article(thread, id, community_id), to: ArticleCommunity
defdelegate undo_pin_article(thread, id, community_id), to: ArticleCommunity

defdelegate lock_article_comment(content), to: ArticleOperation
defdelegate lock_article_comment(content), to: ArticleCommunity

# >> tag: set / unset
defdelegate set_tag(thread, tag, content_id), to: ArticleOperation
defdelegate unset_tag(thread, tag, content_id), to: ArticleOperation
defdelegate set_tag(thread, tag, content_id), to: ArticleCommunity
defdelegate unset_tag(thread, tag, content_id), to: ArticleCommunity
# >> community: set / unset
defdelegate set_community(community, thread, content_id), to: ArticleOperation
defdelegate unset_community(community, thread, content_id), to: ArticleOperation
defdelegate mirror_article(thread, article_id, community_id), to: ArticleCommunity
defdelegate unmirror_article(thread, article_id, community_id), to: ArticleCommunity
defdelegate move_article(thread, article_id, community_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
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
defmodule GroupherServer.CMS.Delegate.ArticleOperation do
defmodule GroupherServer.CMS.Delegate.ArticleCommunity do
@moduledoc """
set / unset operations for Article-like resource
"""
import GroupherServer.CMS.Helper.Matcher
import GroupherServer.CMS.Helper.Matcher2
import Ecto.Query, warn: false

import Helper.ErrorCode
import ShortMaps
import Helper.Utils, only: [strip_struct: 1]
import Helper.Utils, only: [strip_struct: 1, done: 1]
import GroupherServer.CMS.Helper.Matcher2

alias Helper.Types, as: T
Expand All @@ -28,6 +29,8 @@ defmodule GroupherServer.CMS.Delegate.ArticleOperation do
alias GroupherServer.CMS.Repo, as: CMSRepo
alias GroupherServer.Repo

alias Ecto.Multi

@max_pinned_article_count_per_thread Community.max_pinned_article_count_per_thread()

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

@doc """
set content to diffent community
mirror article to other community
"""
def set_community(%Community{id: community_id}, thread, content_id) do
with {:ok, action} <- match_action(thread, :community),
{:ok, content} <- ORM.find(action.target, content_id, preload: :communities),
{:ok, community} <- ORM.find(action.reactor, community_id) do
content
def mirror_article(thread, article_id, community_id) 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, content.communities ++ [community])
|> Ecto.Changeset.put_assoc(:communities, article.communities ++ [community])
|> Repo.update()
end
end

def unset_community(%Community{id: community_id}, thread, content_id) do
with {:ok, action} <- match_action(thread, :community),
{:ok, content} <- ORM.find(action.target, content_id, preload: :communities),
{:ok, community} <- ORM.find(action.reactor, community_id) do
content
|> Ecto.Changeset.change()
|> Ecto.Changeset.put_assoc(:communities, content.communities -- [community])
|> Repo.update()
@doc """
unmirror article to a community
"""
def unmirror_article(thread, article_id, community_id) do
with {:ok, info} <- match(thread),
{:ok, article} <-
ORM.find(info.model, article_id, preload: [:communities, :original_community]),
{: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
|> Ecto.Changeset.change()
|> Ecto.Changeset.put_assoc(:communities, article.communities -- [community])
|> Repo.update()
end
end
end

@doc """
move article original community to other community
"""
def move_article(thread, article_id, community_id) do
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

Multi.new()
|> Multi.run(:change_original_community, fn _, _ ->
article
|> Ecto.Changeset.change()
|> Ecto.Changeset.put_change(:original_community_id, community.id)
|> 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()
end)
|> Multi.run(:mirror_target_community, fn _, %{unmirror_article: article} ->
article
|> Ecto.Changeset.change()
|> Ecto.Changeset.put_assoc(:communities, article.communities ++ [community])
|> Repo.update()
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 """
set general tag for post / tuts ...
"""
Expand Down
36 changes: 18 additions & 18 deletions lib/groupher_server/cms/delegates/article_curd.ex
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ defmodule GroupherServer.CMS.Delegate.ArticleCURD do
alias Accounts.User
alias CMS.{Author, Community, PinnedArticle, Embeds, Delegate, Tag}

alias Delegate.ArticleOperation
alias Delegate.ArticleCommunity

alias Ecto.Multi

Expand Down Expand Up @@ -130,19 +130,19 @@ defmodule GroupherServer.CMS.Delegate.ArticleCURD do
{:ok, community} <- ORM.find(Community, cid) do
Multi.new()
|> Multi.run(:create_article, fn _, _ ->
do_create_content(action.target, attrs, author, community)
do_create_article(action.target, attrs, author, community)
end)
|> Multi.run(:set_community, fn _, %{create_article: content} ->
ArticleOperation.set_community(community, thread, content.id)
|> Multi.run(:mirror_article, fn _, %{create_article: article} ->
ArticleCommunity.mirror_article(thread, article.id, community.id)
end)
|> Multi.run(:set_community_flag, fn _, %{create_article: content} ->
exec_set_community_flag(community, content, action)
|> Multi.run(:set_community_flag, fn _, %{create_article: article} ->
exec_set_community_flag(community, article, action)
end)
|> Multi.run(:set_tag, fn _, %{create_article: content} ->
exec_set_tag(thread, content.id, attrs)
|> Multi.run(:set_tag, fn _, %{create_article: article} ->
exec_set_tag(thread, article.id, attrs)
end)
|> Multi.run(:mention_users, fn _, %{create_article: content} ->
Delivery.mention_from_content(community.raw, thread, content, attrs, %User{id: uid})
|> Multi.run(:mention_users, fn _, %{create_article: article} ->
Delivery.mention_from_content(community.raw, thread, article, attrs, %User{id: uid})
{:ok, :pass}
end)
|> Multi.run(:log_action, fn _, _ ->
Expand All @@ -160,7 +160,7 @@ defmodule GroupherServer.CMS.Delegate.ArticleCURD do
"""
def notify_admin_new_content(%{id: id} = result) do
target = result.__struct__
preload = [:origial_community, author: :user]
preload = [:original_community, author: :user]

with {:ok, content} <- ORM.find(target, id, preload: preload) do
info = %{
Expand All @@ -186,10 +186,10 @@ defmodule GroupherServer.CMS.Delegate.ArticleCURD do
ORM.update(content, args)
end)
|> Multi.run(:update_edit_status, fn _, %{update_article: update_article} ->
ArticleOperation.update_edit_status(update_article)
ArticleCommunity.update_edit_status(update_article)
end)
|> Multi.run(:update_tag, fn _, _ ->
# TODO: move it to ArticleOperation module
# TODO: move it to ArticleCommunity module
exec_update_tags(content, args)
end)
|> Repo.transaction()
Expand Down Expand Up @@ -368,7 +368,7 @@ defmodule GroupherServer.CMS.Delegate.ArticleCURD do
{:error, [message: "create cms content author", code: ecode(:create_fails)]}
end

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

Expand All @@ -385,21 +385,21 @@ defmodule GroupherServer.CMS.Delegate.ArticleCURD do
end

# for create content step in Multi.new
defp do_create_content(target, attrs, %Author{id: aid}, %Community{id: cid}) do
defp do_create_article(target, attrs, %Author{id: aid}, %Community{id: cid}) do
target
|> struct()
|> target.changeset(attrs)
|> Ecto.Changeset.put_change(:emotions, @default_emotions)
|> Ecto.Changeset.put_change(:author_id, aid)
|> Ecto.Changeset.put_change(:origial_community_id, integerfy(cid))
|> Ecto.Changeset.put_change(:original_community_id, integerfy(cid))
|> Ecto.Changeset.put_embed(:meta, @default_article_meta)
|> Repo.insert()
end

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

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

# TODO: flag 逻辑似乎有问题
defp exec_set_community_flag(%Community{} = community, content, %{flag: _flag}) do
ArticleOperation.set_community_flags(community, content, %{
ArticleCommunity.set_community_flags(community, content, %{
trash: false
})
end
Expand Down
2 changes: 2 additions & 0 deletions lib/groupher_server/cms/embeds/article_meta.ex
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ defmodule GroupherServer.CMS.Embeds.ArticleMeta do
use Accessible
import Ecto.Changeset

alias GroupherServer.CMS

@optional_fields ~w(is_edited is_comment_locked upvoted_user_ids collected_user_ids viewed_user_ids reported_user_ids reported_count)a

@default_meta %{
Expand Down
4 changes: 2 additions & 2 deletions lib/groupher_server/cms/job.ex
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ defmodule GroupherServer.CMS.Job do

@timestamps_opts [type: :utc_datetime_usec]
@required_fields ~w(title company company_logo body digest length)a
@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
@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

@type t :: %Job{}
schema "cms_jobs" do
Expand Down Expand Up @@ -87,7 +87,7 @@ defmodule GroupherServer.CMS.Job do
on_replace: :delete
)

belongs_to(:origial_community, Community)
belongs_to(:original_community, Community)

many_to_many(
:communities,
Expand Down
4 changes: 2 additions & 2 deletions lib/groupher_server/cms/post.ex
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ defmodule GroupherServer.CMS.Post do

@timestamps_opts [type: :utc_datetime_usec]
@required_fields ~w(title body digest length)a
@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
@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

@type t :: %Post{}
schema "cms_posts" do
Expand Down Expand Up @@ -86,7 +86,7 @@ defmodule GroupherServer.CMS.Post do
on_replace: :delete
)

belongs_to(:origial_community, Community)
belongs_to(:original_community, Community)

many_to_many(
:communities,
Expand Down
4 changes: 2 additions & 2 deletions lib/groupher_server/cms/repo.ex
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ defmodule GroupherServer.CMS.Repo do

@timestamps_opts [type: :utc_datetime_usec]
@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
@optional_fields ~w(origial_community_id last_sync homepage_url release_tag license)a
@optional_fields ~w(original_community_id last_sync homepage_url release_tag license)a

@type t :: %Repo{}
schema "cms_repos" do
Expand Down Expand Up @@ -84,7 +84,7 @@ defmodule GroupherServer.CMS.Repo do
on_replace: :delete
)

belongs_to(:origial_community, Community)
belongs_to(:original_community, Community)

many_to_many(
:communities,
Expand Down
12 changes: 8 additions & 4 deletions lib/groupher_server_web/resolvers/cms_resolver.ex
Original file line number Diff line number Diff line change
Expand Up @@ -290,12 +290,16 @@ defmodule GroupherServerWeb.Resolvers.CMS do

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

def set_community(_root, ~m(thread id community_id)a, _info) do
CMS.set_community(%Community{id: community_id}, thread, id)
def mirror_article(_root, ~m(thread id community_id)a, _info) do
CMS.mirror_article(thread, id, community_id)
end

def unset_community(_root, ~m(thread id community_id)a, _info) do
CMS.unset_community(%Community{id: community_id}, thread, id)
def unmirror_article(_root, ~m(thread id community_id)a, _info) 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)
end

# #######################
Expand Down
6 changes: 3 additions & 3 deletions lib/groupher_server_web/schema/cms/cms_types.ex
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ defmodule GroupherServerWeb.Schema.CMS.Types do
field(:tags, list_of(:tag), resolve: dataloader(CMS, :tags))

field(:author, :user, resolve: dataloader(CMS, :author))
field(:origial_community, :community, resolve: dataloader(CMS, :origial_community))
field(:original_community, :community, resolve: dataloader(CMS, :original_community))
field(:communities, list_of(:community), resolve: dataloader(CMS, :communities))

field(:meta, :article_meta)
Expand Down Expand Up @@ -123,7 +123,7 @@ defmodule GroupherServerWeb.Schema.CMS.Types do

field(:author, :user, resolve: dataloader(CMS, :author))
field(:tags, list_of(:tag), resolve: dataloader(CMS, :tags))
field(:origial_community, :community, resolve: dataloader(CMS, :origial_community))
field(:original_community, :community, resolve: dataloader(CMS, :original_community))
field(:communities, list_of(:community), resolve: dataloader(CMS, :communities))

field(:meta, :article_meta)
Expand Down Expand Up @@ -177,7 +177,7 @@ defmodule GroupherServerWeb.Schema.CMS.Types do
field(:last_sync, :datetime)

field(:tags, list_of(:tag), resolve: dataloader(CMS, :tags))
field(:origial_community, :community, resolve: dataloader(CMS, :origial_community))
field(:original_community, :community, resolve: dataloader(CMS, :original_community))
field(:communities, list_of(:community), resolve: dataloader(CMS, :communities))

viewer_has_state_fields()
Expand Down
Loading