@@ -6,23 +6,21 @@ defmodule GroupherServer.CMS.ArticleComment do
66 use Accessible
77
88 import Ecto.Changeset
9+ import GroupherServer.CMS.Helper.Macros
10+ import GroupherServer.CMS.Helper.Utils , only: [ articles_foreign_key_constraint: 1 ]
911
1012 alias GroupherServer . { Accounts , CMS }
11-
12- alias CMS . {
13- Post ,
14- Job ,
15- Repo ,
16- Embeds ,
17- ArticleCommentUpvote
18- }
13+ alias CMS . { Embeds , ArticleCommentUpvote }
1914
2015 # alias Helper.HTML
16+ @ article_threads CMS.Community . article_threads ( )
2117
2218 @ required_fields ~w( body_html author_id) a
23- @ optional_fields ~w( post_id job_id repo_id reply_to_id replies_count is_folded is_deleted floor is_article_author) a
19+ @ optional_fields ~w( reply_to_id replies_count is_folded is_deleted floor is_article_author) a
2420 @ updatable_fields ~w( is_folded is_deleted floor upvotes_count is_pinned) a
2521
22+ @ article_fields @ article_threads |> Enum . map ( & :"#{ & 1 } _id" )
23+
2624 @ max_participator_count 5
2725 @ max_parent_replies_count 3
2826
@@ -69,10 +67,6 @@ defmodule GroupherServer.CMS.ArticleComment do
6967 field ( :is_pinned , :boolean , default: false )
7068 field ( :viewer_has_upvoted , :boolean , default: false , virtual: true )
7169
72- belongs_to ( :post , Post , foreign_key: :post_id )
73- belongs_to ( :job , Job , foreign_key: :job_id )
74- belongs_to ( :repo , Repo , foreign_key: :repo_id )
75-
7670 belongs_to ( :reply_to , ArticleComment , foreign_key: :reply_to_id )
7771
7872 embeds_many ( :replies , ArticleComment , on_replace: :delete )
@@ -83,13 +77,14 @@ defmodule GroupherServer.CMS.ArticleComment do
8377
8478 has_many ( :upvotes , { "articles_comments_upvotes" , ArticleCommentUpvote } )
8579
80+ article_belongs_to ( )
8681 timestamps ( type: :utc_datetime )
8782 end
8883
8984 @ doc false
9085 def changeset ( % ArticleComment { } = article_comment , attrs ) do
9186 article_comment
92- |> cast ( attrs , @ required_fields ++ @ optional_fields )
87+ |> cast ( attrs , @ required_fields ++ @ optional_fields ++ @ article_fields )
9388 |> cast_embed ( :emotions , required: true , with: & Embeds.ArticleCommentEmotion . changeset / 2 )
9489 |> cast_embed ( :meta , required: true , with: & Embeds.ArticleCommentMeta . changeset / 2 )
9590 |> validate_required ( @ required_fields )
@@ -99,14 +94,15 @@ defmodule GroupherServer.CMS.ArticleComment do
9994 # @doc false
10095 def update_changeset ( % ArticleComment { } = article_comment , attrs ) do
10196 article_comment
102- |> cast ( attrs , @ required_fields ++ @ updatable_fields )
97+ |> cast ( attrs , @ required_fields ++ @ updatable_fields ++ @ article_fields )
10398 |> cast_embed ( :meta , required: true , with: & Embeds.ArticleCommentMeta . changeset / 2 )
10499 |> generl_changeset
105100 end
106101
107102 defp generl_changeset ( content ) do
108103 content
109104 |> foreign_key_constraint ( :author_id )
105+ |> articles_foreign_key_constraint
110106
111107 # |> validate_length(:body_html, min: 3, max: 2000)
112108 # |> HTML.safe_string(:body_html)
0 commit comments