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

Commit c56723a

Browse files
authored
refactor(cms-macros): more macros for cms articles && community (#361)
* refactor(cms-macros): missing doc * refactor: wip * refactor: wip * refactor: wip, macro in macro * refactor: general_article_fields wip * refactor: general_article_fields wip * refactor: general_article_fields wip * refactor: add doc for general_article_fields * refactor: fmt * refactor: job * refactor: job fix * refactor: repo * refactor: repo fix && missing tests * refactor: post_communities macro * refactor: community_article macro * refactor: add some doc * refactor: move article_thread config to config.ex * refactor: rm unused warning
1 parent c7cf29e commit c56723a

32 files changed

+1434
-227
lines changed

config/config.exs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,8 @@ config :groupher_server, :customization,
6161
sidebar_communities_index: %{}
6262

6363
config :groupher_server, :article,
64+
# NOTE: do not change unless you know what you are doing
65+
article_threads: [:post, :job, :repo],
6466
# NOTE: if you want to add/remove emotion, just edit the list below
6567
# and migrate the field to table "articles_users_emotions"
6668
supported_emotions: [

lib/groupher_server/application.ex

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@ defmodule GroupherServer.Application do
77
@spec start(any, any) :: {:error, any} | {:ok, pid}
88
def start(_type, _args) do
99
import Supervisor.Spec
10-
import Cachex.Spec
11-
1210
alias Helper.Cache
1311

1412
# Define workers and child supervisors to be supervised

lib/groupher_server/cms/abuse_report.ex

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,16 @@ defmodule GroupherServer.CMS.AbuseReport do
44

55
use Ecto.Schema
66
use Accessible
7+
78
import Ecto.Changeset
9+
import Helper.Utils, only: [get_config: 2]
810
import GroupherServer.CMS.Helper.Macros
911
import GroupherServer.CMS.Helper.Utils, only: [articles_foreign_key_constraint: 1]
1012

1113
alias GroupherServer.{Accounts, CMS}
1214
alias CMS.{ArticleComment, Embeds}
1315

14-
@article_threads CMS.Community.article_threads()
16+
@article_threads get_config(:article, :article_threads)
1517

1618
# @required_fields ~w(article_comment_id user_id recived_user_id)a
1719
@optional_fields ~w(article_comment_id account_id operate_user_id deal_with report_cases_count)a
@@ -31,7 +33,7 @@ defmodule GroupherServer.CMS.AbuseReport do
3133

3234
field(:deal_with, :string)
3335

34-
article_belongs_to()
36+
article_belongs_to_fields()
3537
timestamps(type: :utc_datetime)
3638
end
3739

lib/groupher_server/cms/article_collect.ex

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,16 @@ defmodule GroupherServer.CMS.ArticleCollect do
33
alias __MODULE__
44

55
use Ecto.Schema
6+
67
import Ecto.Changeset
8+
import Helper.Utils, only: [get_config: 2]
79
import GroupherServer.CMS.Helper.Macros
810
import GroupherServer.CMS.Helper.Utils, only: [articles_foreign_key_constraint: 1]
911

1012
alias GroupherServer.{Accounts, CMS}
1113
alias Accounts.{User, CollectFolder}
1214

13-
@article_threads CMS.Community.article_threads()
15+
@article_threads get_config(:article, :article_threads)
1416

1517
@required_fields ~w(user_id)a
1618
@optional_fields ~w(thread)a
@@ -23,7 +25,7 @@ defmodule GroupherServer.CMS.ArticleCollect do
2325
belongs_to(:user, User, foreign_key: :user_id)
2426
embeds_many(:collect_folders, CollectFolder, on_replace: :delete)
2527

26-
article_belongs_to()
28+
article_belongs_to_fields()
2729
timestamps(type: :utc_datetime)
2830
end
2931

lib/groupher_server/cms/article_comment.ex

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,15 @@ defmodule GroupherServer.CMS.ArticleComment do
66
use Accessible
77

88
import Ecto.Changeset
9+
import Helper.Utils, only: [get_config: 2]
910
import GroupherServer.CMS.Helper.Macros
1011
import GroupherServer.CMS.Helper.Utils, only: [articles_foreign_key_constraint: 1]
1112

1213
alias GroupherServer.{Accounts, CMS}
1314
alias CMS.{Embeds, ArticleCommentUpvote}
1415

1516
# alias Helper.HTML
16-
@article_threads CMS.Community.article_threads()
17+
@article_threads get_config(:article, :article_threads)
1718

1819
@required_fields ~w(body_html author_id)a
1920
@optional_fields ~w(reply_to_id replies_count is_folded is_deleted floor is_article_author)a
@@ -77,7 +78,7 @@ defmodule GroupherServer.CMS.ArticleComment do
7778

7879
has_many(:upvotes, {"articles_comments_upvotes", ArticleCommentUpvote})
7980

80-
article_belongs_to()
81+
article_belongs_to_fields()
8182
timestamps(type: :utc_datetime)
8283
end
8384

lib/groupher_server/cms/article_pinned_comment.ex

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,15 @@ defmodule GroupherServer.CMS.ArticlePinnedComment do
66
use Accessible
77

88
import Ecto.Changeset
9+
import Helper.Utils, only: [get_config: 2]
910
import GroupherServer.CMS.Helper.Macros
1011
import GroupherServer.CMS.Helper.Utils, only: [articles_foreign_key_constraint: 1]
1112

1213
alias GroupherServer.CMS
1314
alias CMS.ArticleComment
1415

1516
# alias Helper.HTML
16-
@article_threads CMS.Community.article_threads()
17+
@article_threads get_config(:article, :article_threads)
1718

1819
@required_fields ~w(article_comment_id)a
1920
# @optional_fields ~w(post_id job_id repo_id)a
@@ -24,7 +25,7 @@ defmodule GroupherServer.CMS.ArticlePinnedComment do
2425
schema "articles_pinned_comments" do
2526
belongs_to(:article_comment, ArticleComment, foreign_key: :article_comment_id)
2627

27-
article_belongs_to()
28+
article_belongs_to_fields()
2829
timestamps(type: :utc_datetime)
2930
end
3031

lib/groupher_server/cms/article_upvote.ex

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,16 @@ defmodule GroupherServer.CMS.ArticleUpvote do
33
alias __MODULE__
44

55
use Ecto.Schema
6+
67
import Ecto.Changeset
8+
import Helper.Utils, only: [get_config: 2]
79
import GroupherServer.CMS.Helper.Macros
810
import GroupherServer.CMS.Helper.Utils, only: [articles_foreign_key_constraint: 1]
911

1012
alias GroupherServer.{Accounts, CMS}
1113
alias Accounts.User
1214

13-
@article_threads CMS.Community.article_threads()
15+
@article_threads get_config(:article, :article_threads)
1416

1517
@required_fields ~w(user_id)a
1618
@optional_fields ~w(thread)a
@@ -22,7 +24,7 @@ defmodule GroupherServer.CMS.ArticleUpvote do
2224
field(:thread, :string)
2325
belongs_to(:user, User, foreign_key: :user_id)
2426

25-
article_belongs_to()
27+
article_belongs_to_fields()
2628
timestamps(type: :utc_datetime)
2729
end
2830

lib/groupher_server/cms/article_user_emotion.ex

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ defmodule GroupherServer.CMS.ArticleUserEmotion do
2929
alias GroupherServer.{Accounts, CMS}
3030

3131
@supported_emotions get_config(:article, :supported_emotions)
32-
@article_threads CMS.Community.article_threads()
32+
@article_threads get_config(:article, :article_threads)
3333

3434
@required_fields ~w(user_id recived_user_id)a
3535
@optional_fields Enum.map(@article_threads, &:"#{&1}_id") ++
@@ -41,7 +41,7 @@ defmodule GroupherServer.CMS.ArticleUserEmotion do
4141
belongs_to(:user, Accounts.User, foreign_key: :user_id)
4242

4343
emotion_fields()
44-
article_belongs_to()
44+
article_belongs_to_fields()
4545
timestamps(type: :utc_datetime)
4646
end
4747

lib/groupher_server/cms/community.ex

Lines changed: 2 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -4,29 +4,25 @@ defmodule GroupherServer.CMS.Community do
44

55
use Ecto.Schema
66
import Ecto.Changeset
7+
import GroupherServer.CMS.Helper.Macros
78

89
alias GroupherServer.{Accounts, CMS}
910

1011
alias CMS.{
1112
Category,
12-
Post,
13-
Repo,
14-
Job,
1513
CommunityThread,
1614
CommunitySubscriber,
1715
CommunityEditor,
1816
CommunityWiki,
1917
CommunityCheatsheet
2018
}
2119

22-
@article_threads [:post, :job, :repo]
2320
@max_pinned_article_count_per_thread 2
2421

2522
@required_fields ~w(title desc user_id logo raw)a
2623
# @required_fields ~w(title desc user_id)a
2724
@optional_fields ~w(label geo_info index aka)a
2825

29-
def article_threads, do: @article_threads
3026
def max_pinned_article_count_per_thread, do: @max_pinned_article_count_per_thread
3127

3228
schema "communities" do
@@ -59,30 +55,7 @@ defmodule GroupherServer.CMS.Community do
5955
# on_replace: :delete
6056
)
6157

62-
many_to_many(
63-
:posts,
64-
Post,
65-
join_through: "communities_posts",
66-
join_keys: [community_id: :id, post_id: :id]
67-
)
68-
69-
many_to_many(
70-
:repos,
71-
Repo,
72-
join_through: "communities_repos",
73-
join_keys: [community_id: :id, repo_id: :id]
74-
)
75-
76-
many_to_many(
77-
:jobs,
78-
Job,
79-
join_through: "communities_jobs",
80-
join_keys: [community_id: :id, job_id: :id]
81-
)
82-
83-
# posts_managers
84-
# jobs_managers
85-
# tuts_managers
58+
community_article_fields()
8659
#
8760
# posts_block_list ...
8861
timestamps(type: :utc_datetime)

lib/groupher_server/cms/delegates/abuse_report.ex

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ defmodule GroupherServer.CMS.Delegate.AbuseReport do
33
CURD and operations for article comments
44
"""
55
import Ecto.Query, warn: false
6-
import Helper.Utils, only: [done: 1, strip_struct: 1]
6+
import Helper.Utils, only: [done: 1, strip_struct: 1, get_config: 2]
77

88
import GroupherServer.CMS.Helper.Matcher2
99
import ShortMaps
@@ -17,9 +17,9 @@ defmodule GroupherServer.CMS.Delegate.AbuseReport do
1717

1818
alias Ecto.Multi
1919

20+
@article_threads get_config(:article, :article_threads)
2021
@report_threshold_for_fold ArticleComment.report_threshold_for_fold()
2122

22-
@article_threads Community.article_threads()
2323
@export_author_keys [:id, :login, :nickname, :avatar]
2424
@export_article_keys [:id, :title, :digest, :upvotes_count, :views]
2525
@export_report_keys [

0 commit comments

Comments
 (0)