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

Commit 2f69341

Browse files
authored
refactor(community): redesign (#364)
* refactor(community): rename table && clean up warnings * refactor(community): re-org macro helper file * refactor(community): wip * refactor(community): wip * refactor(community): more test * refactor(community): wip * refactor(community): wip * refactor(community): add views to community * refactor(community): move articles_count to outside field * refactor(test): skip old post comment test * chore: fmt * fix(article): add viewer_has states when read * refactor: sync subscribe count to community * refactor: move subscribe count & viewer_has logic * refactor(community): enhance read community & test * refactor(community): fmt * fix(community): meta nil edge-case * fix(community): editor count & tests * fix(community): remove editors subscribers in community fields * fix(community): remove editors subscribers in community fields * fix(community): error test * fix(community): error test * fix(community): error test
1 parent 9a59768 commit 2f69341

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

56 files changed

+1655
-634
lines changed

cover/excoveralls.json

+1-1
Large diffs are not rendered by default.

lib/groupher_server/accounts/delegates/collect_folder.ex

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ defmodule GroupherServer.Accounts.Delegate.CollectFolder do
33
user collect folder related
44
"""
55
import Ecto.Query, warn: false
6-
import GroupherServer.CMS.Helper.Matcher2
6+
import GroupherServer.CMS.Helper.Matcher
77

88
alias Helper.Types, as: T
99
alias Helper.QueryBuilder

lib/groupher_server/accounts/delegates/publish.ex

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ defmodule GroupherServer.Accounts.Delegate.Publish do
77
# import Helper.ErrorCode
88
import ShortMaps
99

10-
import GroupherServer.CMS.Helper.Matcher
10+
import GroupherServer.CMS.Helper.MatcherOld
1111

1212
alias Helper.{ORM, QueryBuilder}
1313
# alias GroupherServer.{Accounts, Repo}

lib/groupher_server/accounts/delegates/upvoted_articles.ex

-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ defmodule GroupherServer.Accounts.Delegate.UpvotedArticles do
22
@moduledoc """
33
get contents(posts, jobs ...) that user upvotes
44
"""
5-
# import GroupherServer.CMS.Helper.Matcher
65
import Ecto.Query, warn: false
76
import Helper.Utils, only: [done: 1]
87
import ShortMaps

lib/groupher_server/cms/cms.ex

+4-6
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@ defmodule GroupherServer.CMS do
3131
# see https://github.com/elixir-lang/elixir/issues/5306
3232

3333
# Community CURD: editors, thread, tag
34+
defdelegate read_community(args), to: CommunityCURD
35+
defdelegate read_community(args, user), to: CommunityCURD
36+
defdelegate create_community(args), to: CommunityCURD
37+
defdelegate update_community(id, args), to: CommunityCURD
3438
# >> editor ..
3539
defdelegate update_editor(user, community, title), to: CommunityCURD
3640
# >> geo info ..
@@ -51,9 +55,6 @@ defmodule GroupherServer.CMS do
5155
defdelegate unset_article_tag(thread, article_id, tag_id), to: ArticleTag
5256
defdelegate paged_article_tags(filter), to: ArticleTag
5357

54-
defdelegate create_tag(community, thread, attrs, user), to: CommunityCURD
55-
defdelegate update_tag(attrs), to: CommunityCURD
56-
5758
# >> wiki & cheatsheet (sync with github)
5859
defdelegate get_wiki(community), to: CommunitySync
5960
defdelegate get_cheatsheet(community), to: CommunitySync
@@ -113,9 +114,6 @@ defmodule GroupherServer.CMS do
113114
defdelegate undo_pin_article(thread, id, community_id), to: ArticleCommunity
114115
defdelegate lock_article_comment(article), to: ArticleCommunity
115116

116-
# >> tag: set / unset
117-
defdelegate set_tag(thread, tag, content_id), to: ArticleCommunity
118-
defdelegate unset_tag(thread, tag, content_id), to: ArticleCommunity
119117
# >> community: set / unset
120118
defdelegate mirror_article(thread, article_id, community_id), to: ArticleCommunity
121119
defdelegate unmirror_article(thread, article_id, community_id), to: ArticleCommunity

lib/groupher_server/cms/community.ex

+12
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ defmodule GroupherServer.CMS.Community do
99
alias GroupherServer.{Accounts, CMS}
1010

1111
alias CMS.{
12+
Embeds,
1213
Category,
1314
CommunityThread,
1415
CommunitySubscriber,
@@ -35,13 +36,22 @@ defmodule GroupherServer.CMS.Community do
3536
field(:raw, :string)
3637
field(:index, :integer)
3738
field(:geo_info, :map)
39+
field(:views, :integer)
3840

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

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

48+
field(:articles_count, :integer, default: 0)
49+
field(:editors_count, :integer, default: 0)
50+
field(:subscribers_count, :integer, default: 0)
51+
52+
field(:viewer_has_subscribed, :boolean, default: false, virtual: true)
53+
field(:viewer_is_editor, :boolean, default: false, virtual: true)
54+
4555
has_one(:wiki, CommunityWiki)
4656
has_one(:cheatsheet, CommunityCheatsheet)
4757

@@ -68,6 +78,7 @@ defmodule GroupherServer.CMS.Community do
6878
community
6979
|> cast(attrs, @optional_fields ++ @required_fields)
7080
|> validate_required(@required_fields)
81+
|> cast_embed(:meta, with: &Embeds.CommunityMeta.changeset/2)
7182
|> validate_length(:title, min: 1, max: 30)
7283
|> foreign_key_constraint(:user_id)
7384
|> unique_constraint(:title, name: :communities_title_index)
@@ -82,6 +93,7 @@ defmodule GroupherServer.CMS.Community do
8293
# |> unique_constraint(:title, name: :communities_title_index)
8394
community
8495
|> cast(attrs, @optional_fields ++ @required_fields)
96+
|> cast_embed(:meta, with: &Embeds.CommunityMeta.changeset/2)
8597
|> validate_length(:title, min: 1, max: 30)
8698
|> foreign_key_constraint(:user_id)
8799
|> unique_constraint(:title, name: :communities_title_index)

lib/groupher_server/cms/delegates/abuse_report.ex

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ defmodule GroupherServer.CMS.Delegate.AbuseReport do
55
import Ecto.Query, warn: false
66
import Helper.Utils, only: [done: 1, strip_struct: 1, get_config: 2]
77

8-
import GroupherServer.CMS.Helper.Matcher2
8+
import GroupherServer.CMS.Helper.Matcher
99
import ShortMaps
1010

1111
alias Helper.ORM

lib/groupher_server/cms/delegates/article_collect.ex

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ defmodule GroupherServer.CMS.Delegate.ArticleCollect do
22
@moduledoc """
33
reaction[upvote, collect, watch ...] on article [post, job...]
44
"""
5-
import GroupherServer.CMS.Helper.Matcher2
5+
import GroupherServer.CMS.Helper.Matcher
66
import Ecto.Query, warn: false
77
import Helper.Utils, only: [done: 1]
88

lib/groupher_server/cms/delegates/article_comment.ex

+8-8
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ defmodule GroupherServer.CMS.Delegate.ArticleComment do
77
import Helper.ErrorCode
88

99
import GroupherServer.CMS.Delegate.Helper, only: [mark_viewer_emotion_states: 3]
10-
import GroupherServer.CMS.Helper.Matcher2
10+
import GroupherServer.CMS.Helper.Matcher
1111
import ShortMaps
1212

1313
alias Helper.Types, as: T
@@ -106,7 +106,7 @@ defmodule GroupherServer.CMS.Delegate.ArticleComment do
106106
add_participator_to_article(article, user)
107107
end)
108108
|> Repo.transaction()
109-
|> upsert_comment_result()
109+
|> result()
110110
end
111111
end
112112

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

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

277-
defp upsert_comment_result({:ok, %{create_article_comment: result}}), do: {:ok, result}
278-
defp upsert_comment_result({:ok, %{delete_article_comment: result}}), do: {:ok, result}
277+
defp result({:ok, %{create_article_comment: result}}), do: {:ok, result}
278+
defp result({:ok, %{delete_article_comment: result}}), do: {:ok, result}
279279

280-
defp upsert_comment_result({:error, :create_article_comment, result, _steps}) do
280+
defp result({:error, :create_article_comment, result, _steps}) do
281281
raise_error(:create_comment, result)
282282
end
283283

284-
defp upsert_comment_result({:error, :add_participator, result, _steps}) do
284+
defp result({:error, :add_participator, result, _steps}) do
285285
{:error, result}
286286
end
287287

288-
defp upsert_comment_result({:error, _, result, _steps}), do: {:error, result}
288+
defp result({:error, _, result, _steps}), do: {:error, result}
289289
end

lib/groupher_server/cms/delegates/article_comment_action.ex

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ defmodule GroupherServer.CMS.Delegate.ArticleCommentAction do
99
import GroupherServer.CMS.Delegate.ArticleComment,
1010
only: [add_participator_to_article: 2, do_create_comment: 4, update_article_comments_count: 2]
1111

12-
import GroupherServer.CMS.Helper.Matcher2
12+
import GroupherServer.CMS.Helper.Matcher
1313

1414
alias Helper.Types, as: T
1515
alias Helper.ORM

lib/groupher_server/cms/delegates/article_community.ex

+1-2
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,11 @@ defmodule GroupherServer.CMS.Delegate.ArticleCommunity do
33
set / unset operations for Article-like resource
44
"""
55
import GroupherServer.CMS.Helper.Matcher
6-
import GroupherServer.CMS.Helper.Matcher2
76
import Ecto.Query, warn: false
87

98
import Helper.ErrorCode
109
import Helper.Utils, only: [strip_struct: 1, done: 1]
11-
import GroupherServer.CMS.Helper.Matcher2
10+
import GroupherServer.CMS.Helper.Matcher
1211

1312
alias Helper.Types, as: T
1413
alias Helper.ORM

lib/groupher_server/cms/delegates/article_curd.ex

+50-13
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ defmodule GroupherServer.CMS.Delegate.ArticleCURD do
44
"""
55
import Ecto.Query, warn: false
66

7-
import GroupherServer.CMS.Helper.Matcher2
7+
import GroupherServer.CMS.Helper.Matcher
88

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

22-
alias Delegate.{ArticleCommunity, ArticleTag}
22+
alias Delegate.{ArticleCommunity, ArticleTag, CommunityCURD}
2323

2424
alias Ecto.Multi
2525

@@ -47,8 +47,19 @@ defmodule GroupherServer.CMS.Delegate.ArticleCURD do
4747
|> Multi.run(:add_viewed_user, fn _, %{inc_views: article} ->
4848
update_viewed_user_list(article, user_id)
4949
end)
50+
|> Multi.run(:set_viewer_has_states, fn _, %{inc_views: article} ->
51+
article_meta = if is_nil(article.meta), do: @default_article_meta, else: article.meta
52+
53+
viewer_has_states = %{
54+
viewer_has_collected: user_id in article_meta.collected_user_ids,
55+
viewer_has_upvoted: user_id in article_meta.upvoted_user_ids,
56+
viewer_has_reported: user_id in article_meta.reported_user_ids
57+
}
58+
59+
{:ok, Map.merge(article, viewer_has_states)}
60+
end)
5061
|> Repo.transaction()
51-
|> read_result()
62+
|> result()
5263
end
5364
end
5465

@@ -135,6 +146,9 @@ defmodule GroupherServer.CMS.Delegate.ArticleCURD do
135146
|> Multi.run(:set_article_tags, fn _, %{create_article: article} ->
136147
ArticleTag.set_article_tags(community, thread, article, attrs)
137148
end)
149+
|> Multi.run(:update_community_article_count, fn _, _ ->
150+
CommunityCURD.update_community_count_field(community, thread)
151+
end)
138152
|> Multi.run(:mention_users, fn _, %{create_article: article} ->
139153
Delivery.mention_from_content(community.raw, thread, article, attrs, %User{id: uid})
140154
{:ok, :pass}
@@ -183,18 +197,42 @@ defmodule GroupherServer.CMS.Delegate.ArticleCURD do
183197
ArticleCommunity.update_edit_status(update_article)
184198
end)
185199
|> Repo.transaction()
186-
|> update_article_result()
200+
|> result()
187201
end
188202

203+
@doc """
204+
mark delete falst for an anticle
205+
"""
189206
def mark_delete_article(thread, id) do
190-
with {:ok, info} <- match(thread) do
191-
ORM.find_update(info.model, %{id: id, mark_delete: true})
207+
with {:ok, info} <- match(thread),
208+
{:ok, article} <- ORM.find(info.model, id, preload: :communities) do
209+
Multi.new()
210+
|> Multi.run(:update_article, fn _, _ ->
211+
ORM.update(article, %{mark_delete: true})
212+
end)
213+
|> Multi.run(:update_community_article_count, fn _, _ ->
214+
CommunityCURD.update_community_count_field(article.communities, thread)
215+
end)
216+
|> Repo.transaction()
217+
|> result()
192218
end
193219
end
194220

221+
@doc """
222+
undo mark delete falst for an anticle
223+
"""
195224
def undo_mark_delete_article(thread, id) do
196-
with {:ok, info} <- match(thread) do
197-
ORM.find_update(info.model, %{id: id, mark_delete: false})
225+
with {:ok, info} <- match(thread),
226+
{:ok, article} <- ORM.find(info.model, id, preload: :communities) do
227+
Multi.new()
228+
|> Multi.run(:update_article, fn _, _ ->
229+
ORM.update(article, %{mark_delete: false})
230+
end)
231+
|> Multi.run(:update_community_article_count, fn _, _ ->
232+
CommunityCURD.update_community_count_field(article.communities, thread)
233+
end)
234+
|> Repo.transaction()
235+
|> result()
198236
end
199237
end
200238

@@ -406,12 +444,11 @@ defmodule GroupherServer.CMS.Delegate.ArticleCURD do
406444
end
407445
end
408446

409-
defp update_article_result({:ok, %{update_edit_status: result}}), do: {:ok, result}
410-
defp update_article_result({:error, :update_article, result, _steps}), do: {:error, result}
411-
412-
defp read_result({:ok, %{inc_views: result}}), do: result |> done()
447+
defp result({:ok, %{update_edit_status: result}}), do: {:ok, result}
448+
defp result({:ok, %{update_article: result}}), do: {:ok, result}
449+
defp result({:ok, %{set_viewer_has_states: result}}), do: result |> done()
413450

414-
defp read_result({:error, _, result, _steps}) do
451+
defp result({:error, _, result, _steps}) do
415452
{:error, result}
416453
end
417454
end

lib/groupher_server/cms/delegates/article_emotion.ex

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ defmodule GroupherServer.CMS.Delegate.ArticleEmotion do
33
CURD and operations for article comments
44
"""
55
import Ecto.Query, warn: false
6-
import GroupherServer.CMS.Helper.Matcher2
6+
import GroupherServer.CMS.Helper.Matcher
77

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

lib/groupher_server/cms/delegates/article_tag.ex

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ defmodule GroupherServer.CMS.Delegate.ArticleTag do
33
community curd
44
"""
55
import Ecto.Query, warn: false
6-
import GroupherServer.CMS.Helper.Matcher2
6+
import GroupherServer.CMS.Helper.Matcher
77
import Helper.Validator.Guards, only: [g_is_id: 1]
8-
import Helper.Utils, only: [done: 1, camelize_map_key: 2, map_atom_values_to_upcase_str: 1]
8+
import Helper.Utils, only: [done: 1, map_atom_values_to_upcase_str: 1]
99
import GroupherServer.CMS.Delegate.ArticleCURD, only: [ensure_author_exists: 1]
1010
import ShortMaps
1111
import Helper.ErrorCode

lib/groupher_server/cms/delegates/article_upvote.ex

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ defmodule GroupherServer.CMS.Delegate.ArticleUpvote do
22
@moduledoc """
33
reaction[upvote, collect, watch ...] on article [post, job...]
44
"""
5-
import GroupherServer.CMS.Helper.Matcher2
5+
import GroupherServer.CMS.Helper.Matcher
66
import Ecto.Query, warn: false
77
import Helper.Utils, only: [done: 1]
88

lib/groupher_server/cms/delegates/comment_curd.ex

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ defmodule GroupherServer.CMS.Delegate.CommentCURD do
66
import Helper.Utils, only: [done: 1]
77
import Helper.ErrorCode
88

9-
import GroupherServer.CMS.Helper.Matcher
9+
import GroupherServer.CMS.Helper.MatcherOld
1010
import ShortMaps
1111

1212
alias Helper.{ORM, QueryBuilder}

0 commit comments

Comments
 (0)