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

refactor(cms): remove post topic concept #326

Merged
merged 5 commits into from
Apr 27, 2021
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
8 changes: 0 additions & 8 deletions lib/groupher_server/cms/cms.ex
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ defmodule GroupherServer.CMS do
defdelegate create_tag(community, thread, attrs, user), to: CommunityCURD
defdelegate update_tag(attrs), to: CommunityCURD
defdelegate get_tags(community, thread), to: CommunityCURD
defdelegate get_tags(community, thread, topic), to: CommunityCURD
defdelegate get_tags(filter), to: CommunityCURD
# >> wiki & cheatsheet (sync with github)
defdelegate get_wiki(community), to: CommunitySync
Expand Down Expand Up @@ -87,22 +86,15 @@ defmodule GroupherServer.CMS do
# ArticleOperation
# >> set flag on article, like: pin / unpin article
defdelegate set_community_flags(community_info, queryable, attrs), to: ArticleOperation
defdelegate pin_content(queryable, community_id, topic), to: ArticleOperation
defdelegate undo_pin_content(queryable, community_id, topic), to: ArticleOperation
defdelegate pin_content(queryable, community_id), to: ArticleOperation
defdelegate undo_pin_content(queryable, community_id), to: ArticleOperation

defdelegate lock_article_comment(content), to: ArticleOperation
# defdelegate pin_content(queryable, community_id, thread), to: ArticleOperation
# defdelegate undo_pin_content(queryable, community_id, thread, topic), to: ArticleOperation
# defdelegate undo_pin_content(queryable, community_id, thread), to: ArticleOperation

# >> tag: set / unset
defdelegate set_tag(thread, tag, content_id), to: ArticleOperation
defdelegate unset_tag(thread, tag, content_id), to: ArticleOperation
defdelegate set_refined_tag(community, thread, topic, content_id), to: ArticleOperation
defdelegate set_refined_tag(community, thread, content_id), to: ArticleOperation
defdelegate unset_refined_tag(community, thread, topic, content_id), to: ArticleOperation
defdelegate unset_refined_tag(community, thread, content_id), to: ArticleOperation
# >> community: set / unset
defdelegate set_community(community, thread, content_id), to: ArticleOperation
Expand Down
28 changes: 4 additions & 24 deletions lib/groupher_server/cms/delegates/article_curd.ex
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ defmodule GroupherServer.CMS.Delegate.ArticleCURD do
alias GroupherServer.{Accounts, CMS, Delivery, Email, Repo, Statistics}

alias Accounts.User
alias CMS.{Author, Community, Embeds, Delegate, Tag, Topic}
alias CMS.{Author, Community, Embeds, Delegate, Tag}

alias Delegate.ArticleOperation
alias Helper.{Later, ORM, QueryBuilder}
Expand Down Expand Up @@ -76,9 +76,6 @@ defmodule GroupherServer.CMS.Delegate.ArticleCURD do
|> Multi.run(:set_community, fn _, %{create_content: content} ->
ArticleOperation.set_community(community, thread, content.id)
end)
|> Multi.run(:set_topic, fn _, %{create_content: content} ->
exec_set_topic(thread, content.id, attrs)
end)
|> Multi.run(:set_community_flag, fn _, %{create_content: content} ->
exec_set_community_flag(community, content, action)
end)
Expand Down Expand Up @@ -294,13 +291,9 @@ defmodule GroupherServer.CMS.Delegate.ArticleCURD do
{:ok, pined_content} =
CMS.PinedPost
|> join(:inner, [p], c in assoc(p, :community))
|> join(:inner, [p], t in assoc(p, :topic))
|> join(:inner, [p], content in assoc(p, :post))
|> where(
[p, c, t, content],
c.raw == ^community and t.raw == ^Map.get(filter, :topic, "posts")
)
|> select([p, c, t, content], content)
|> where([p, c, content], c.raw == ^community)
|> select([p, c, content], content)
# 10 pined contents per community/thread, at most
|> ORM.paginater(%{page: 1, size: 10})
|> done()
Expand Down Expand Up @@ -348,7 +341,7 @@ defmodule GroupherServer.CMS.Delegate.ArticleCURD do
defp should_add_pin?(%{page: 1, tag: :all, sort: :desc_inserted, read: :all} = filter) do
filter
|> Map.keys()
|> Enum.reject(fn x -> x in [:community, :tag, :sort, :read, :topic, :page, :size] end)
|> Enum.reject(fn x -> x in [:community, :tag, :sort, :read, :page, :size] end)
|> case do
[] -> {:ok, :pass}
_ -> {:error, :pass}
Expand Down Expand Up @@ -403,10 +396,6 @@ defmodule GroupherServer.CMS.Delegate.ArticleCURD do
{:error, [message: "set community flag", code: ecode(:create_fails)]}
end

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

defp create_content_result({:error, :set_tag, result, _steps}) do
{:error, result}
end
Expand Down Expand Up @@ -434,15 +423,6 @@ defmodule GroupherServer.CMS.Delegate.ArticleCURD do
|> Repo.insert()
end

defp exec_set_topic(thread, id, %{topic: topic}) do
ArticleOperation.set_topic(%Topic{title: topic}, thread, id)
end

# if topic is not provide, use posts as default
defp exec_set_topic(thread, id, _attrs) do
ArticleOperation.set_topic(%Topic{title: "posts"}, thread, id)
end

defp exec_set_tag(thread, id, %{tags: tags}) do
try do
Enum.each(tags, fn tag ->
Expand Down
65 changes: 10 additions & 55 deletions lib/groupher_server/cms/delegates/article_operation.ex
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ defmodule GroupherServer.CMS.Delegate.ArticleOperation do
JobCommunityFlag,
RepoCommunityFlag,
Tag,
Topic,
PinedPost,
PinedJob,
PinedRepo
Expand All @@ -27,13 +26,12 @@ defmodule GroupherServer.CMS.Delegate.ArticleOperation do
alias GroupherServer.CMS.Repo, as: CMSRepo
alias GroupherServer.Repo

def pin_content(%Post{id: post_id}, %Community{id: community_id}, topic) do
with {:ok, %{id: topic_id}} <- ORM.find_by(Topic, %{raw: topic}),
{:ok, pined} <-
def pin_content(%Post{id: post_id}, %Community{id: community_id}) do
with {:ok, pined} <-
ORM.findby_or_insert(
PinedPost,
~m(post_id community_id topic_id)a,
~m(post_id community_id topic_id)a
~m(post_id community_id)a,
~m(post_id community_id)a
) do
Post |> ORM.find(pined.post_id)
end
Expand All @@ -55,9 +53,8 @@ defmodule GroupherServer.CMS.Delegate.ArticleOperation do
end
end

def undo_pin_content(%Post{id: post_id}, %Community{id: community_id}, topic) do
with {:ok, %{id: topic_id}} <- ORM.find_by(Topic, %{raw: topic}),
{:ok, pined} <- ORM.find_by(PinedPost, ~m(post_id community_id topic_id)a),
def undo_pin_content(%Post{id: post_id}, %Community{id: community_id}) do
with {:ok, pined} <- ORM.find_by(PinedPost, ~m(post_id community_id)a),
{:ok, deleted} <- ORM.delete(pined) do
Post |> ORM.find(deleted.post_id)
end
Expand Down Expand Up @@ -177,55 +174,33 @@ defmodule GroupherServer.CMS.Delegate.ArticleOperation do
@doc """
set refined_tag to common content
"""
def set_refined_tag(%Community{id: community_id}, thread, topic_raw, content_id) do
def set_refined_tag(%Community{id: community_id}, thread, content_id) do
with {:ok, action} <- match_action(thread, :tag),
{:ok, content} <- ORM.find(action.target, content_id, preload: :tags),
{:ok, topic} <- ORM.find_by(Topic, %{raw: topic_raw}),
{:ok, tag} <-
ORM.find_by(action.reactor, %{
title: "refined",
community_id: community_id,
topic_id: topic.id
community_id: community_id
}) do
update_content_tag(content, tag)
end
end

def set_refined_tag(%Community{id: community_id}, thread, content_id) do
with {:ok, action} <- match_action(thread, :tag),
{:ok, content} <- ORM.find(action.target, content_id, preload: :tags),
{:ok, tag} <-
ORM.find_by(action.reactor, %{title: "refined", community_id: community_id}) do
update_content_tag(content, tag)
end
end

@doc """
unset refined_tag to common content
"""
def unset_refined_tag(%Community{id: community_id}, thread, topic_raw, content_id) do
def unset_refined_tag(%Community{id: community_id}, thread, content_id) do
with {:ok, action} <- match_action(thread, :tag),
{:ok, content} <- ORM.find(action.target, content_id, preload: :tags),
{:ok, topic} <- ORM.find_by(Topic, %{raw: topic_raw}),
{:ok, tag} <-
ORM.find_by(action.reactor, %{
title: "refined",
community_id: community_id,
topic_id: topic.id
community_id: community_id
}) do
update_content_tag(content, tag, :drop)
end
end

def unset_refined_tag(%Community{id: community_id}, thread, content_id) do
with {:ok, action} <- match_action(thread, :tag),
{:ok, content} <- ORM.find(action.target, content_id, preload: :tags),
{:ok, tag} <-
ORM.find_by(action.reactor, %{title: "refined", community_id: community_id}) do
update_content_tag(content, tag, :drop)
end
end

defp update_content_tag(content, %Tag{} = tag, opt \\ :add) do
new_tags = if opt == :add, do: content.tags ++ [tag], else: content.tags -- [tag]

Expand All @@ -235,26 +210,6 @@ defmodule GroupherServer.CMS.Delegate.ArticleOperation do
|> Repo.update()
end

@doc """
set topic only for post
"""
def set_topic(%Topic{title: title}, :post, content_id) do
with {:ok, content} <- ORM.find(Post, content_id, preload: :topics),
{:ok, topic} <-
ORM.findby_or_insert(Topic, %{title: title}, %{
title: title,
thread: "post",
raw: title
}) do
content
|> Ecto.Changeset.change()
|> Ecto.Changeset.put_assoc(:topics, content.topics ++ [topic])
|> Repo.update()
end
end

def set_topic(_topic, _thread, _content_id), do: {:ok, :pass}

@doc "update isEdited meta label if needed"
# TODO: diff history
def update_edit_status(%{meta: %Embeds.ArticleMeta{is_edited: false} = meta} = content) do
Expand Down
55 changes: 6 additions & 49 deletions lib/groupher_server/cms/delegates/community_curd.ex
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ defmodule GroupherServer.CMS.Delegate.CommunityCURD do
CommunityEditor,
CommunitySubscriber,
Tag,
Topic,
Thread
}

Expand Down Expand Up @@ -59,15 +58,13 @@ defmodule GroupherServer.CMS.Delegate.CommunityCURD do
@doc """
create a Tag base on type: post / tuts ...
"""
# TODO: change to create_tag(community, thread, attrs, ....)
def create_tag(%Community{id: community_id}, thread, attrs, %Accounts.User{id: user_id}) do
with {:ok, action} <- match_action(thread, :tag),
{:ok, author} <- ensure_author_exists(%Accounts.User{id: user_id}),
{:ok, _community} <- ORM.find(Community, community_id),
{:ok, topic} = find_or_insert_topic(attrs) do
{:ok, _community} <- ORM.find(Community, community_id) do
attrs =
attrs
|> Map.merge(%{author_id: author.id, topic_id: topic.id, community_id: community_id})
|> Map.merge(%{author_id: author.id, community_id: community_id})
|> map_atom_value(:string)
|> Map.merge(%{thread: thread |> to_string |> String.downcase()})

Expand All @@ -78,10 +75,8 @@ defmodule GroupherServer.CMS.Delegate.CommunityCURD do
def update_tag(%{id: _id} = attrs) do
~m(id title color)a = attrs |> map_atom_value(:string)

with {:ok, %{id: topic_id}} = find_or_insert_topic(attrs) do
Tag
|> ORM.find_update(~m(id title color topic_id)a)
end
Tag
|> ORM.find_update(~m(id title color)a)
end

@doc """
Expand Down Expand Up @@ -116,31 +111,12 @@ defmodule GroupherServer.CMS.Delegate.CommunityCURD do
@doc """
get tags belongs to a community / thread
"""
def get_tags(%Community{id: community_id}, thread, topic) when not is_nil(community_id) do
# thread = thread |> to_string |> String.upcase()
# topic = topic |> to_string |> String.upcase()
def get_tags(%Community{raw: community_raw}, thread) when not is_nil(community_raw) do
thread = thread |> to_string |> String.downcase()
topic = topic |> to_string |> String.downcase()

Tag
|> join(:inner, [t], c in assoc(t, :community))
|> join(:inner, [t], cp in assoc(t, :topic))
|> where([t, c, cp], c.id == ^community_id and t.thread == ^thread and cp.title == ^topic)
|> distinct([t], t.title)
|> Repo.all()
|> done()
end

def get_tags(%Community{raw: community_raw}, thread, topic) when not is_nil(community_raw) do
# thread = thread |> to_string |> String.upcase()
# topic = topic |> to_string |> String.upcase()
thread = thread |> to_string |> String.downcase()
topic = topic |> to_string |> String.downcase()

Tag
|> join(:inner, [t], c in assoc(t, :community))
|> join(:inner, [t], cp in assoc(t, :topic))
|> where([t, c, cp], c.raw == ^community_raw and t.thread == ^thread and cp.title == ^topic)
|> where([t, c], c.raw == ^community_raw and t.thread == ^thread)
|> distinct([t], t.title)
|> Repo.all()
|> done()
Expand Down Expand Up @@ -248,25 +224,6 @@ defmodule GroupherServer.CMS.Delegate.CommunityCURD do
end
end

defp find_or_insert_topic(%{topic: title} = attrs) when is_binary(title) do
title = title |> to_string() |> String.downcase()
thread = attrs.thread |> to_string() |> String.downcase()

ORM.findby_or_insert(Topic, %{title: title}, %{
title: title,
thread: thread,
raw: title
})
end

defp find_or_insert_topic(%{thread: thread}) do
find_or_insert_topic(%{topic: "posts", thread: thread})
end

defp find_or_insert_topic(_attrs) do
find_or_insert_topic(%{topic: "posts", thread: :post})
end

defp load_community_members(%Community{id: id}, queryable, %{page: page, size: size} = filters)
when not is_nil(id) do
queryable
Expand Down
Loading