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

refactor(community): redesign #364

Merged
merged 23 commits into from
May 22, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
e36a8a1
refactor(community): rename table && clean up warnings
mydearxym May 19, 2021
e507108
refactor(community): re-org macro helper file
mydearxym May 19, 2021
f1ce81d
refactor(community): wip
mydearxym May 20, 2021
04bd8a9
refactor(community): wip
mydearxym May 20, 2021
9ceb26d
refactor(community): more test
mydearxym May 20, 2021
4508d66
refactor(community): wip
mydearxym May 20, 2021
74a6d85
refactor(community): wip
mydearxym May 20, 2021
d628c48
refactor(community): add views to community
mydearxym May 20, 2021
176c5a2
refactor(community): move articles_count to outside field
mydearxym May 20, 2021
a1bc01f
refactor(test): skip old post comment test
mydearxym May 20, 2021
8252888
chore: fmt
mydearxym May 20, 2021
0ec4880
fix(article): add viewer_has states when read
mydearxym May 21, 2021
0766085
refactor: sync subscribe count to community
mydearxym May 21, 2021
8cd6ee8
refactor: move subscribe count & viewer_has logic
mydearxym May 21, 2021
a49dc93
refactor(community): enhance read community & test
mydearxym May 22, 2021
af3bfc2
refactor(community): fmt
mydearxym May 22, 2021
bea44ab
fix(community): meta nil edge-case
mydearxym May 22, 2021
9f50d45
fix(community): editor count & tests
mydearxym May 22, 2021
ca57175
fix(community): remove editors subscribers in community fields
mydearxym May 22, 2021
2ea73d3
fix(community): remove editors subscribers in community fields
mydearxym May 22, 2021
2f53345
fix(community): error test
mydearxym May 22, 2021
e78a315
fix(community): error test
mydearxym May 22, 2021
e56b586
fix(community): error test
mydearxym May 22, 2021
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.

2 changes: 1 addition & 1 deletion lib/groupher_server/accounts/delegates/collect_folder.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ defmodule GroupherServer.Accounts.Delegate.CollectFolder do
user collect folder related
"""
import Ecto.Query, warn: false
import GroupherServer.CMS.Helper.Matcher2
import GroupherServer.CMS.Helper.Matcher

alias Helper.Types, as: T
alias Helper.QueryBuilder
Expand Down
2 changes: 1 addition & 1 deletion lib/groupher_server/accounts/delegates/publish.ex
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ defmodule GroupherServer.Accounts.Delegate.Publish do
# import Helper.ErrorCode
import ShortMaps

import GroupherServer.CMS.Helper.Matcher
import GroupherServer.CMS.Helper.MatcherOld

alias Helper.{ORM, QueryBuilder}
# alias GroupherServer.{Accounts, Repo}
Expand Down
1 change: 0 additions & 1 deletion lib/groupher_server/accounts/delegates/upvoted_articles.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ defmodule GroupherServer.Accounts.Delegate.UpvotedArticles do
@moduledoc """
get contents(posts, jobs ...) that user upvotes
"""
# import GroupherServer.CMS.Helper.Matcher
import Ecto.Query, warn: false
import Helper.Utils, only: [done: 1]
import ShortMaps
Expand Down
10 changes: 4 additions & 6 deletions lib/groupher_server/cms/cms.ex
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ defmodule GroupherServer.CMS do
# see https://github.com/elixir-lang/elixir/issues/5306

# Community CURD: editors, thread, tag
defdelegate read_community(args), to: CommunityCURD
defdelegate read_community(args, user), to: CommunityCURD
defdelegate create_community(args), to: CommunityCURD
defdelegate update_community(id, args), to: CommunityCURD
# >> editor ..
defdelegate update_editor(user, community, title), to: CommunityCURD
# >> geo info ..
Expand All @@ -51,9 +55,6 @@ defmodule GroupherServer.CMS do
defdelegate unset_article_tag(thread, article_id, tag_id), to: ArticleTag
defdelegate paged_article_tags(filter), to: ArticleTag

defdelegate create_tag(community, thread, attrs, user), to: CommunityCURD
defdelegate update_tag(attrs), to: CommunityCURD

# >> wiki & cheatsheet (sync with github)
defdelegate get_wiki(community), to: CommunitySync
defdelegate get_cheatsheet(community), to: CommunitySync
Expand Down Expand Up @@ -113,9 +114,6 @@ defmodule GroupherServer.CMS do
defdelegate undo_pin_article(thread, id, community_id), to: ArticleCommunity
defdelegate lock_article_comment(article), to: ArticleCommunity

# >> tag: set / unset
defdelegate set_tag(thread, tag, content_id), to: ArticleCommunity
defdelegate unset_tag(thread, tag, content_id), to: ArticleCommunity
# >> community: set / unset
defdelegate mirror_article(thread, article_id, community_id), to: ArticleCommunity
defdelegate unmirror_article(thread, article_id, community_id), to: ArticleCommunity
Expand Down
12 changes: 12 additions & 0 deletions lib/groupher_server/cms/community.ex
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ defmodule GroupherServer.CMS.Community do
alias GroupherServer.{Accounts, CMS}

alias CMS.{
Embeds,
Category,
CommunityThread,
CommunitySubscriber,
Expand All @@ -35,13 +36,22 @@ defmodule GroupherServer.CMS.Community do
field(:raw, :string)
field(:index, :integer)
field(:geo_info, :map)
field(:views, :integer)

embeds_one(:meta, Embeds.CommunityMeta, on_replace: :delete)
belongs_to(:author, Accounts.User, foreign_key: :user_id)

has_many(:threads, {"communities_threads", CommunityThread})
has_many(:subscribers, {"communities_subscribers", CommunitySubscriber})
has_many(:editors, {"communities_editors", CommunityEditor})

field(:articles_count, :integer, default: 0)
field(:editors_count, :integer, default: 0)
field(:subscribers_count, :integer, default: 0)

field(:viewer_has_subscribed, :boolean, default: false, virtual: true)
field(:viewer_is_editor, :boolean, default: false, virtual: true)

has_one(:wiki, CommunityWiki)
has_one(:cheatsheet, CommunityCheatsheet)

Expand All @@ -68,6 +78,7 @@ defmodule GroupherServer.CMS.Community do
community
|> cast(attrs, @optional_fields ++ @required_fields)
|> validate_required(@required_fields)
|> cast_embed(:meta, with: &Embeds.CommunityMeta.changeset/2)
|> validate_length(:title, min: 1, max: 30)
|> foreign_key_constraint(:user_id)
|> unique_constraint(:title, name: :communities_title_index)
Expand All @@ -82,6 +93,7 @@ defmodule GroupherServer.CMS.Community do
# |> unique_constraint(:title, name: :communities_title_index)
community
|> cast(attrs, @optional_fields ++ @required_fields)
|> cast_embed(:meta, with: &Embeds.CommunityMeta.changeset/2)
|> validate_length(:title, min: 1, max: 30)
|> foreign_key_constraint(:user_id)
|> unique_constraint(:title, name: :communities_title_index)
Expand Down
2 changes: 1 addition & 1 deletion lib/groupher_server/cms/delegates/abuse_report.ex
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ defmodule GroupherServer.CMS.Delegate.AbuseReport do
import Ecto.Query, warn: false
import Helper.Utils, only: [done: 1, strip_struct: 1, get_config: 2]

import GroupherServer.CMS.Helper.Matcher2
import GroupherServer.CMS.Helper.Matcher
import ShortMaps

alias Helper.ORM
Expand Down
2 changes: 1 addition & 1 deletion lib/groupher_server/cms/delegates/article_collect.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ defmodule GroupherServer.CMS.Delegate.ArticleCollect do
@moduledoc """
reaction[upvote, collect, watch ...] on article [post, job...]
"""
import GroupherServer.CMS.Helper.Matcher2
import GroupherServer.CMS.Helper.Matcher
import Ecto.Query, warn: false
import Helper.Utils, only: [done: 1]

Expand Down
16 changes: 8 additions & 8 deletions lib/groupher_server/cms/delegates/article_comment.ex
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ defmodule GroupherServer.CMS.Delegate.ArticleComment do
import Helper.ErrorCode

import GroupherServer.CMS.Delegate.Helper, only: [mark_viewer_emotion_states: 3]
import GroupherServer.CMS.Helper.Matcher2
import GroupherServer.CMS.Helper.Matcher
import ShortMaps

alias Helper.Types, as: T
Expand Down Expand Up @@ -106,7 +106,7 @@ defmodule GroupherServer.CMS.Delegate.ArticleComment do
add_participator_to_article(article, user)
end)
|> Repo.transaction()
|> upsert_comment_result()
|> result()
end
end

Expand All @@ -130,7 +130,7 @@ defmodule GroupherServer.CMS.Delegate.ArticleComment do
ORM.update(comment, %{body_html: @delete_hint, is_deleted: true})
end)
|> Repo.transaction()
|> upsert_comment_result()
|> result()
end

# add participator to article-like content (Post, Job ...) and update count
Expand Down Expand Up @@ -274,16 +274,16 @@ defmodule GroupherServer.CMS.Delegate.ArticleComment do
Map.merge(paged_comments, %{entries: entries})
end

defp upsert_comment_result({:ok, %{create_article_comment: result}}), do: {:ok, result}
defp upsert_comment_result({:ok, %{delete_article_comment: result}}), do: {:ok, result}
defp result({:ok, %{create_article_comment: result}}), do: {:ok, result}
defp result({:ok, %{delete_article_comment: result}}), do: {:ok, result}

defp upsert_comment_result({:error, :create_article_comment, result, _steps}) do
defp result({:error, :create_article_comment, result, _steps}) do
raise_error(:create_comment, result)
end

defp upsert_comment_result({:error, :add_participator, result, _steps}) do
defp result({:error, :add_participator, result, _steps}) do
{:error, result}
end

defp upsert_comment_result({:error, _, result, _steps}), do: {:error, result}
defp result({:error, _, result, _steps}), do: {:error, result}
end
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ defmodule GroupherServer.CMS.Delegate.ArticleCommentAction do
import GroupherServer.CMS.Delegate.ArticleComment,
only: [add_participator_to_article: 2, do_create_comment: 4, update_article_comments_count: 2]

import GroupherServer.CMS.Helper.Matcher2
import GroupherServer.CMS.Helper.Matcher

alias Helper.Types, as: T
alias Helper.ORM
Expand Down
3 changes: 1 addition & 2 deletions lib/groupher_server/cms/delegates/article_community.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,11 @@ defmodule GroupherServer.CMS.Delegate.ArticleCommunity do
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 Helper.Utils, only: [strip_struct: 1, done: 1]
import GroupherServer.CMS.Helper.Matcher2
import GroupherServer.CMS.Helper.Matcher

alias Helper.Types, as: T
alias Helper.ORM
Expand Down
63 changes: 50 additions & 13 deletions lib/groupher_server/cms/delegates/article_curd.ex
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ defmodule GroupherServer.CMS.Delegate.ArticleCURD do
"""
import Ecto.Query, warn: false

import GroupherServer.CMS.Helper.Matcher2
import GroupherServer.CMS.Helper.Matcher

import Helper.Utils,
only: [done: 1, pick_by: 2, integerfy: 1, strip_struct: 1, module_to_thread: 1]
Expand All @@ -19,7 +19,7 @@ defmodule GroupherServer.CMS.Delegate.ArticleCURD do
alias Accounts.User
alias CMS.{Author, Community, PinnedArticle, Embeds, Delegate}

alias Delegate.{ArticleCommunity, ArticleTag}
alias Delegate.{ArticleCommunity, ArticleTag, CommunityCURD}

alias Ecto.Multi

Expand Down Expand Up @@ -47,8 +47,19 @@ defmodule GroupherServer.CMS.Delegate.ArticleCURD do
|> Multi.run(:add_viewed_user, fn _, %{inc_views: article} ->
update_viewed_user_list(article, user_id)
end)
|> Multi.run(:set_viewer_has_states, fn _, %{inc_views: article} ->
article_meta = if is_nil(article.meta), do: @default_article_meta, else: article.meta

viewer_has_states = %{
viewer_has_collected: user_id in article_meta.collected_user_ids,
viewer_has_upvoted: user_id in article_meta.upvoted_user_ids,
viewer_has_reported: user_id in article_meta.reported_user_ids
}

{:ok, Map.merge(article, viewer_has_states)}
end)
|> Repo.transaction()
|> read_result()
|> result()
end
end

Expand Down Expand Up @@ -135,6 +146,9 @@ defmodule GroupherServer.CMS.Delegate.ArticleCURD do
|> Multi.run(:set_article_tags, fn _, %{create_article: article} ->
ArticleTag.set_article_tags(community, thread, article, attrs)
end)
|> Multi.run(:update_community_article_count, fn _, _ ->
CommunityCURD.update_community_count_field(community, thread)
end)
|> Multi.run(:mention_users, fn _, %{create_article: article} ->
Delivery.mention_from_content(community.raw, thread, article, attrs, %User{id: uid})
{:ok, :pass}
Expand Down Expand Up @@ -183,18 +197,42 @@ defmodule GroupherServer.CMS.Delegate.ArticleCURD do
ArticleCommunity.update_edit_status(update_article)
end)
|> Repo.transaction()
|> update_article_result()
|> result()
end

@doc """
mark delete falst for an anticle
"""
def mark_delete_article(thread, id) do
with {:ok, info} <- match(thread) do
ORM.find_update(info.model, %{id: id, mark_delete: true})
with {:ok, info} <- match(thread),
{:ok, article} <- ORM.find(info.model, id, preload: :communities) do
Multi.new()
|> Multi.run(:update_article, fn _, _ ->
ORM.update(article, %{mark_delete: true})
end)
|> Multi.run(:update_community_article_count, fn _, _ ->
CommunityCURD.update_community_count_field(article.communities, thread)
end)
|> Repo.transaction()
|> result()
end
end

@doc """
undo mark delete falst for an anticle
"""
def undo_mark_delete_article(thread, id) do
with {:ok, info} <- match(thread) do
ORM.find_update(info.model, %{id: id, mark_delete: false})
with {:ok, info} <- match(thread),
{:ok, article} <- ORM.find(info.model, id, preload: :communities) do
Multi.new()
|> Multi.run(:update_article, fn _, _ ->
ORM.update(article, %{mark_delete: false})
end)
|> Multi.run(:update_community_article_count, fn _, _ ->
CommunityCURD.update_community_count_field(article.communities, thread)
end)
|> Repo.transaction()
|> result()
end
end

Expand Down Expand Up @@ -406,12 +444,11 @@ defmodule GroupherServer.CMS.Delegate.ArticleCURD do
end
end

defp update_article_result({:ok, %{update_edit_status: result}}), do: {:ok, result}
defp update_article_result({:error, :update_article, result, _steps}), do: {:error, result}

defp read_result({:ok, %{inc_views: result}}), do: result |> done()
defp result({:ok, %{update_edit_status: result}}), do: {:ok, result}
defp result({:ok, %{update_article: result}}), do: {:ok, result}
defp result({:ok, %{set_viewer_has_states: result}}), do: result |> done()

defp read_result({:error, _, result, _steps}) do
defp result({:error, _, result, _steps}) do
{:error, result}
end
end
2 changes: 1 addition & 1 deletion lib/groupher_server/cms/delegates/article_emotion.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ defmodule GroupherServer.CMS.Delegate.ArticleEmotion do
CURD and operations for article comments
"""
import Ecto.Query, warn: false
import GroupherServer.CMS.Helper.Matcher2
import GroupherServer.CMS.Helper.Matcher

import GroupherServer.CMS.Delegate.Helper, only: [update_emotions_field: 4]

Expand Down
4 changes: 2 additions & 2 deletions lib/groupher_server/cms/delegates/article_tag.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ defmodule GroupherServer.CMS.Delegate.ArticleTag do
community curd
"""
import Ecto.Query, warn: false
import GroupherServer.CMS.Helper.Matcher2
import GroupherServer.CMS.Helper.Matcher
import Helper.Validator.Guards, only: [g_is_id: 1]
import Helper.Utils, only: [done: 1, camelize_map_key: 2, map_atom_values_to_upcase_str: 1]
import Helper.Utils, only: [done: 1, map_atom_values_to_upcase_str: 1]
import GroupherServer.CMS.Delegate.ArticleCURD, only: [ensure_author_exists: 1]
import ShortMaps
import Helper.ErrorCode
Expand Down
2 changes: 1 addition & 1 deletion lib/groupher_server/cms/delegates/article_upvote.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ defmodule GroupherServer.CMS.Delegate.ArticleUpvote do
@moduledoc """
reaction[upvote, collect, watch ...] on article [post, job...]
"""
import GroupherServer.CMS.Helper.Matcher2
import GroupherServer.CMS.Helper.Matcher
import Ecto.Query, warn: false
import Helper.Utils, only: [done: 1]

Expand Down
2 changes: 1 addition & 1 deletion lib/groupher_server/cms/delegates/comment_curd.ex
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ defmodule GroupherServer.CMS.Delegate.CommentCURD do
import Helper.Utils, only: [done: 1]
import Helper.ErrorCode

import GroupherServer.CMS.Helper.Matcher
import GroupherServer.CMS.Helper.MatcherOld
import ShortMaps

alias Helper.{ORM, QueryBuilder}
Expand Down
Loading