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

Commit a3ae8ce

Browse files
authored
refactor(comments-redesign): rewrite, enhance (#319)
* refactor(article-comments): basic write & upvote impl * refactor(article-comments): wip * refactor(article-comments): test embed association workflow * refactor(article-comments): comment_participators workflow test, WOW! * refactor(article-comments): add participators after comment created * refactor(article-comments): add gq test * refactor(article-comments): clean up * refactor(article-comments): fix test err/warings * refactor(article-comments): nested reply wip * refactor(article-comments): more test on reply * refactor(article-comments): replies basic logic done * refactor(article-comments): replies count logic * refactor(article-comments): paged replies logic * refactor(article-comments): basic emotion setup * refactor(post-meta): emotions wip * refactor(article-comments): basic emotion wip * refactor(article-comments): emotion curd done * refactor(article-comments): viewer_has_emotioned wip * refactor(article-comments): re-org wip * refactor(article-comments): re-org wip * refactor(article-comments): re-org wip * refactor(article-comments): naming re-org * refactor(article-comments): clean up * refactor(article-comments): make emotion dynamicly * refactor(article-comments): fold/unfold done * refactor(article-comments): paged fold/unfold done * refactor(article-comments): paged report/unreport done * refactor(article-comments): floor done * refactor(article-comments): write_comment -> create_article_comment * refactor(article-comments): upvote_comment -> upvote_article_comment * refactor(article-comments): delete comment logic * refactor(article-comments): is_article_author flag * refactor(article-comments): upvotes_count logic * refactor(article-comments): rm old job comments & logic * refactor(article-comments): rm old repo comments & logic * refactor(article-comments): rm job/repo GQ comments test * refactor(article-comments): rm post comment dislike * refactor(article-comments): add meta embeds * refactor(article-comments): abuse_reports setup * refactor(article-comments): abuse_reports wip * refactor(article-comments): abuse_reports wip * refactor(article-comments): abuse_reports re-org wip * refactor(article-comments): clean up unused var in tests * refactor(article-comments): @report_threshold_for_fold logic * refactor(article-comments): wip * refactor(article-comments): fix broken tests * refactor(post-meta): fix ci tests & warnings
1 parent 8a15031 commit a3ae8ce

File tree

85 files changed

+2349
-2860
lines changed

Some content is hidden

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

85 files changed

+2349
-2860
lines changed
+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
defmodule GroupherServer.CMS.AbuseReport do
2+
@moduledoc false
3+
alias __MODULE__
4+
5+
use Ecto.Schema
6+
import Ecto.Changeset
7+
8+
alias GroupherServer.{Accounts, CMS}
9+
alias CMS.{ArticleComment, Embeds, Post, Job}
10+
11+
# @required_fields ~w(article_comment_id user_id recived_user_id)a
12+
@optional_fields ~w(article_comment_id post_id job_id account_id operate_user_id deal_with is_closed report_cases_count)a
13+
@update_fields ~w(operate_user_id deal_with is_closed report_cases_count)a
14+
15+
@type t :: %AbuseReport{}
16+
schema "abuse_reports" do
17+
belongs_to(:article_comment, ArticleComment, foreign_key: :article_comment_id)
18+
belongs_to(:post, Post, foreign_key: :post_id)
19+
belongs_to(:job, Job, foreign_key: :job_id)
20+
belongs_to(:account, Accounts.User, foreign_key: :account_id)
21+
22+
embeds_many(:report_cases, Embeds.AbuseReportCase, on_replace: :delete)
23+
field(:report_cases_count, :integer, default: 0)
24+
25+
belongs_to(:operate_user, Accounts.User, foreign_key: :operate_user_id)
26+
27+
field(:deal_with, :string)
28+
field(:is_closed, :boolean, default: false)
29+
30+
timestamps(type: :utc_datetime)
31+
end
32+
33+
@doc false
34+
def changeset(%AbuseReport{} = struct, attrs) do
35+
struct
36+
|> cast(attrs, @optional_fields)
37+
|> cast_embed(:report_cases, required: true, with: &Embeds.AbuseReportCase.changeset/2)
38+
end
39+
40+
def update_changeset(%AbuseReport{} = struct, attrs) do
41+
struct
42+
|> cast(attrs, @update_fields)
43+
|> cast_embed(:report_cases, required: true, with: &Embeds.AbuseReportCase.changeset/2)
44+
end
45+
end
+104
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
defmodule GroupherServer.CMS.ArticleComment do
2+
@moduledoc false
3+
alias __MODULE__
4+
5+
use Ecto.Schema
6+
use Accessible
7+
8+
import Ecto.Changeset
9+
10+
alias GroupherServer.{Accounts, CMS}
11+
12+
alias CMS.{
13+
Post,
14+
Job,
15+
Embeds,
16+
ArticleCommentUpvote
17+
}
18+
19+
# alias Helper.HTML
20+
21+
@required_fields ~w(body_html author_id)a
22+
@optional_fields ~w(post_id job_id reply_to_id replies_count is_folded is_reported is_deleted floor is_article_author)a
23+
@updatable_fields ~w(is_folded is_reported is_deleted floor upvotes_count)a
24+
25+
@max_participator_count 5
26+
@max_parent_replies_count 3
27+
28+
@supported_emotions [:downvote, :beer, :heart, :biceps, :orz, :confused, :pill]
29+
@max_latest_emotion_users_count 5
30+
31+
@delete_hint "this comment is deleted"
32+
# 举报超过此数评论会被自动折叠
33+
@report_threshold_for_fold 5
34+
35+
@doc "latest participators stores in article comment_participators field"
36+
def max_participator_count(), do: @max_participator_count
37+
@doc "latest replies stores in article_comment replies field, used for frontend display"
38+
def max_parent_replies_count(), do: @max_parent_replies_count
39+
40+
@doc "操作某 emotion 的最近用户"
41+
def max_latest_emotion_users_count(), do: @max_latest_emotion_users_count
42+
43+
def supported_emotions(), do: @supported_emotions
44+
def delete_hint(), do: @delete_hint
45+
46+
def report_threshold_for_fold, do: @report_threshold_for_fold
47+
48+
@type t :: %ArticleComment{}
49+
schema "articles_comments" do
50+
field(:body_html, :string)
51+
field(:replies_count, :integer, default: 0)
52+
53+
# 是否被折叠
54+
field(:is_folded, :boolean, default: false)
55+
# 是否被举报
56+
field(:is_reported, :boolean, default: false)
57+
# 是否被删除
58+
field(:is_deleted, :boolean, default: false)
59+
# 楼层
60+
field(:floor, :integer, default: 0)
61+
62+
# 是否是评论文章的作者
63+
field(:is_article_author, :boolean, default: false)
64+
field(:upvotes_count, :integer, default: 0)
65+
66+
belongs_to(:author, Accounts.User, foreign_key: :author_id)
67+
belongs_to(:post, Post, foreign_key: :post_id)
68+
belongs_to(:job, Job, foreign_key: :job_id)
69+
belongs_to(:reply_to, ArticleComment, foreign_key: :reply_to_id)
70+
71+
embeds_many(:replies, ArticleComment, on_replace: :delete)
72+
embeds_one(:emotions, Embeds.ArticleCommentEmotion, on_replace: :update)
73+
embeds_one(:meta, Embeds.ArticleCommentMeta, on_replace: :update)
74+
75+
has_many(:upvotes, {"articles_comments_upvotes", ArticleCommentUpvote})
76+
77+
timestamps(type: :utc_datetime)
78+
end
79+
80+
@doc false
81+
def changeset(%ArticleComment{} = article_comment, attrs) do
82+
article_comment
83+
|> cast(attrs, @required_fields ++ @optional_fields)
84+
|> cast_embed(:emotions, required: true, with: &Embeds.ArticleCommentEmotion.changeset/2)
85+
|> validate_required(@required_fields)
86+
|> generl_changeset
87+
end
88+
89+
# @doc false
90+
def update_changeset(%ArticleComment{} = article_comment, attrs) do
91+
article_comment
92+
|> cast(attrs, @required_fields ++ @updatable_fields)
93+
# |> cast_embed(:emotions, required: false, with: &Embeds.ArticleCommentEmotion.changeset/2)
94+
|> generl_changeset
95+
end
96+
97+
defp generl_changeset(content) do
98+
content
99+
|> foreign_key_constraint(:author_id)
100+
101+
# |> validate_length(:body_html, min: 3, max: 2000)
102+
# |> HTML.safe_string(:body_html)
103+
end
104+
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
defmodule GroupherServer.CMS.ArticleCommentParticipator do
2+
@moduledoc false
3+
4+
use Ecto.Schema
5+
6+
alias GroupherServer.Accounts.User
7+
8+
# alias CMS.{
9+
# Post,
10+
# Job,
11+
# ArticleCommentUpvote
12+
# }
13+
14+
# alias Helper.HTML
15+
16+
# @required_fields ~w(user_id)a
17+
# @optional_fields ~w(post_id job_id)a
18+
19+
embedded_schema do
20+
# field(:reply_time, :string)
21+
22+
belongs_to(:user, User)
23+
end
24+
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
defmodule GroupherServer.CMS.ArticleCommentReply do
2+
@moduledoc false
3+
alias __MODULE__
4+
5+
use Ecto.Schema
6+
import Ecto.Changeset
7+
8+
alias GroupherServer.CMS
9+
alias CMS.ArticleComment
10+
11+
@required_fields ~w(article_comment_id reply_to_id)a
12+
13+
@type t :: %ArticleCommentReply{}
14+
schema "articles_comments_replies" do
15+
belongs_to(:article_comment, ArticleComment, foreign_key: :article_comment_id)
16+
belongs_to(:reply_to, ArticleComment, foreign_key: :reply_to_id)
17+
18+
timestamps(type: :utc_datetime)
19+
end
20+
21+
@doc false
22+
def changeset(%ArticleCommentReply{} = article_comment_reply, attrs) do
23+
article_comment_reply
24+
|> cast(attrs, @required_fields)
25+
|> validate_required(@required_fields)
26+
|> foreign_key_constraint(:article_comment_id)
27+
|> foreign_key_constraint(:reply_to_id)
28+
end
29+
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
defmodule GroupherServer.CMS.ArticleCommentUpvote do
2+
@moduledoc false
3+
alias __MODULE__
4+
5+
use Ecto.Schema
6+
import Ecto.Changeset
7+
8+
alias GroupherServer.{Accounts, CMS}
9+
alias CMS.ArticleComment
10+
11+
@required_fields ~w(article_comment_id user_id)a
12+
13+
@type t :: %ArticleCommentUpvote{}
14+
schema "articles_comments_upvotes" do
15+
belongs_to(:user, Accounts.User, foreign_key: :user_id)
16+
belongs_to(:article_comment, ArticleComment, foreign_key: :article_comment_id)
17+
18+
timestamps(type: :utc_datetime)
19+
end
20+
21+
@doc false
22+
def changeset(%ArticleCommentUpvote{} = article_comment_upvote, attrs) do
23+
article_comment_upvote
24+
|> cast(attrs, @required_fields)
25+
|> validate_required(@required_fields)
26+
|> foreign_key_constraint(:article_comment_id)
27+
|> foreign_key_constraint(:user_id)
28+
|> unique_constraint(:user_id,
29+
name: :articles_comments_upvotes_user_id_article_comment_id_index
30+
)
31+
end
32+
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
defmodule GroupherServer.CMS.ArticleCommentUserEmotion do
2+
@moduledoc false
3+
alias __MODULE__
4+
5+
use Ecto.Schema
6+
import Ecto.Changeset
7+
8+
alias GroupherServer.{Accounts, CMS}
9+
alias CMS.ArticleComment
10+
11+
@required_fields ~w(article_comment_id user_id recived_user_id)a
12+
@optional_fields ~w(downvote beer heart biceps orz confused pill)a
13+
14+
@type t :: %ArticleCommentUserEmotion{}
15+
schema "articles_comments_users_emotions" do
16+
belongs_to(:article_comment, ArticleComment, foreign_key: :article_comment_id)
17+
belongs_to(:recived_user, Accounts.User, foreign_key: :recived_user_id)
18+
belongs_to(:user, Accounts.User, foreign_key: :user_id)
19+
20+
field(:downvote, :boolean, default: false)
21+
field(:beer, :boolean, default: false)
22+
field(:heart, :boolean, default: false)
23+
field(:biceps, :boolean, default: false)
24+
field(:orz, :boolean, default: false)
25+
field(:confused, :boolean, default: false)
26+
field(:pill, :boolean, default: false)
27+
28+
timestamps(type: :utc_datetime)
29+
end
30+
31+
@doc false
32+
def changeset(%ArticleCommentUserEmotion{} = struct, attrs) do
33+
struct
34+
|> cast(attrs, @required_fields ++ @optional_fields)
35+
|> validate_required(@required_fields)
36+
|> foreign_key_constraint(:article_comment_id)
37+
|> foreign_key_constraint(:user_id)
38+
|> foreign_key_constraint(:recived_user_id)
39+
end
40+
end

lib/groupher_server/cms/cms.ex

+27-3
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,12 @@ defmodule GroupherServer.CMS do
88
alias GroupherServer.CMS.Delegate
99

1010
alias Delegate.{
11+
AbuseReport,
1112
ArticleCURD,
1213
ArticleOperation,
1314
ArticleReaction,
1415
FavoritedContents,
16+
ArticleComment,
1517
CommentCURD,
1618
CommunitySync,
1719
CommentReaction,
@@ -106,22 +108,44 @@ defmodule GroupherServer.CMS do
106108
defdelegate unset_community(community, thread, content_id), to: ArticleOperation
107109

108110
# Comment CURD
111+
defdelegate list_article_comments(thread, article_id, filters), to: ArticleComment
112+
defdelegate list_article_comments(thread, article_id, filters, user), to: ArticleComment
113+
defdelegate list_folded_article_comments(thread, article_id, filters), to: ArticleComment
114+
defdelegate list_folded_article_comments(thread, article_id, filters, user), to: ArticleComment
115+
defdelegate list_reported_article_comments(thread, article_id, filters), to: ArticleComment
116+
117+
defdelegate list_reported_article_comments(thread, article_id, filters, user),
118+
to: ArticleComment
119+
120+
defdelegate list_comment_replies(comment_id, filters), to: ArticleComment
121+
109122
defdelegate list_comments(thread, content_id, filters), to: CommentCURD
110123
defdelegate list_comments_participators(thread, content_id, filters), to: CommentCURD
111124

125+
defdelegate create_article_comment(thread, article_id, args, user), to: ArticleComment
126+
defdelegate upvote_article_comment(comment_id, user), to: ArticleComment
127+
defdelegate delete_article_comment(comment_id, user), to: ArticleComment
128+
defdelegate reply_article_comment(comment_id, args, user), to: ArticleComment
129+
130+
defdelegate make_emotion(comment_id, args, user), to: ArticleComment
131+
defdelegate fold_article_comment(comment_id, user), to: ArticleComment
132+
defdelegate unfold_article_comment(comment_id, user), to: ArticleComment
133+
defdelegate report_article_comment(comment_id, user), to: ArticleComment
134+
defdelegate unreport_article_comment(comment_id, user), to: ArticleComment
135+
112136
defdelegate create_comment(thread, content_id, args, user), to: CommentCURD
113137
defdelegate update_comment(thread, id, args, user), to: CommentCURD
114138
defdelegate delete_comment(thread, content_id), to: CommentCURD
115139
defdelegate list_replies(thread, comment, user), to: CommentCURD
116140
defdelegate reply_comment(thread, comment, args, user), to: CommentCURD
117141

142+
# report
143+
defdelegate create_report(type, content_id, args, user), to: AbuseReport
144+
118145
# Comment Reaction
119146
# >> like / undo like
120147
defdelegate like_comment(thread, comment, user), to: CommentReaction
121148
defdelegate undo_like_comment(thread, comment, user), to: CommentReaction
122-
# >> dislike / undo dislike
123-
defdelegate dislike_comment(thread, comment, user), to: CommentReaction
124-
defdelegate undo_dislike_comment(thread, comment, user), to: CommentReaction
125149

126150
# Passport CURD
127151
defdelegate stamp_passport(rules, user), to: PassportCURD

lib/groupher_server/cms/community.ex

-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ defmodule GroupherServer.CMS.Community do
1010
alias CMS.{
1111
Category,
1212
Post,
13-
Video,
1413
Repo,
1514
Job,
1615
CommunityThread,

0 commit comments

Comments
 (0)