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

refactor(comments-redesign): rewrite, enhance #319

Merged
merged 48 commits into from
Apr 17, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
48 commits
Select commit Hold shift + click to select a range
775cd1c
refactor(article-comments): basic write & upvote impl
mydearxym Apr 8, 2021
f18a4d1
refactor(article-comments): wip
mydearxym Apr 8, 2021
b6911fd
refactor(article-comments): test embed association workflow
mydearxym Apr 9, 2021
7048f0c
refactor(article-comments): comment_participators workflow test, WOW!
mydearxym Apr 9, 2021
9ad0cd1
refactor(article-comments): add participators after comment created
mydearxym Apr 11, 2021
5c06ab0
refactor(article-comments): add gq test
mydearxym Apr 11, 2021
69de473
refactor(article-comments): clean up
mydearxym Apr 11, 2021
b4fff1a
refactor(article-comments): fix test err/warings
mydearxym Apr 11, 2021
1b034b1
refactor(article-comments): nested reply wip
mydearxym Apr 12, 2021
e2d4382
refactor(article-comments): more test on reply
mydearxym Apr 13, 2021
3c34d83
refactor(article-comments): replies basic logic done
mydearxym Apr 13, 2021
c661ef4
refactor(article-comments): replies count logic
mydearxym Apr 13, 2021
f60e436
refactor(article-comments): paged replies logic
mydearxym Apr 13, 2021
fa06c78
refactor(article-comments): basic emotion setup
mydearxym Apr 13, 2021
c5a830a
refactor(post-meta): emotions wip
mydearxym Apr 13, 2021
b858bb9
refactor(article-comments): basic emotion wip
mydearxym Apr 13, 2021
d8bf2c3
refactor(article-comments): emotion curd done
mydearxym Apr 14, 2021
6b5bfa6
refactor(article-comments): viewer_has_emotioned wip
mydearxym Apr 14, 2021
9150348
refactor(article-comments): re-org wip
mydearxym Apr 14, 2021
f76cbd0
refactor(article-comments): re-org wip
mydearxym Apr 14, 2021
62f2265
refactor(article-comments): re-org wip
mydearxym Apr 14, 2021
586373f
refactor(article-comments): naming re-org
mydearxym Apr 14, 2021
52c1349
refactor(article-comments): clean up
mydearxym Apr 14, 2021
1b98ba9
refactor(article-comments): make emotion dynamicly
mydearxym Apr 15, 2021
46d6250
refactor(article-comments): fold/unfold done
mydearxym Apr 15, 2021
2904cdd
refactor(article-comments): paged fold/unfold done
mydearxym Apr 15, 2021
9838245
refactor(article-comments): paged report/unreport done
mydearxym Apr 15, 2021
5da19b3
refactor(article-comments): floor done
mydearxym Apr 15, 2021
325b834
refactor(article-comments): write_comment -> create_article_comment
mydearxym Apr 15, 2021
8bf2fe9
refactor(article-comments): upvote_comment -> upvote_article_comment
mydearxym Apr 15, 2021
b033be8
refactor(article-comments): delete comment logic
mydearxym Apr 15, 2021
9e639c9
refactor(article-comments): is_article_author flag
mydearxym Apr 15, 2021
af627c9
refactor(article-comments): upvotes_count logic
mydearxym Apr 15, 2021
c40fd00
refactor(article-comments): rm old job comments & logic
mydearxym Apr 15, 2021
14ed577
refactor(article-comments): rm old repo comments & logic
mydearxym Apr 15, 2021
c232ec8
refactor(article-comments): rm job/repo GQ comments test
mydearxym Apr 15, 2021
bef5f32
Merge branch 'dev' into comments-redesign
mydearxym Apr 15, 2021
bb7baca
refactor(article-comments): rm post comment dislike
mydearxym Apr 15, 2021
3b51f03
refactor(article-comments): add meta embeds
mydearxym Apr 15, 2021
ad7814e
refactor(article-comments): abuse_reports setup
mydearxym Apr 15, 2021
f523b9d
refactor(article-comments): abuse_reports wip
mydearxym Apr 16, 2021
2794e01
refactor(article-comments): abuse_reports wip
mydearxym Apr 16, 2021
e22113a
refactor(article-comments): abuse_reports re-org wip
mydearxym Apr 16, 2021
ba52fac
refactor(article-comments): clean up unused var in tests
mydearxym Apr 16, 2021
7e57070
refactor(article-comments): @report_threshold_for_fold logic
mydearxym Apr 16, 2021
a0ac4be
refactor(article-comments): wip
mydearxym Apr 16, 2021
ea2598f
refactor(article-comments): fix broken tests
mydearxym Apr 17, 2021
d772a78
refactor(post-meta): fix ci tests & warnings
mydearxym Apr 17, 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
45 changes: 45 additions & 0 deletions lib/groupher_server/cms/abuse_report.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
defmodule GroupherServer.CMS.AbuseReport do
@moduledoc false
alias __MODULE__

use Ecto.Schema
import Ecto.Changeset

alias GroupherServer.{Accounts, CMS}
alias CMS.{ArticleComment, Embeds, Post, Job}

# @required_fields ~w(article_comment_id user_id recived_user_id)a
@optional_fields ~w(article_comment_id post_id job_id account_id operate_user_id deal_with is_closed report_cases_count)a
@update_fields ~w(operate_user_id deal_with is_closed report_cases_count)a

@type t :: %AbuseReport{}
schema "abuse_reports" do
belongs_to(:article_comment, ArticleComment, foreign_key: :article_comment_id)
belongs_to(:post, Post, foreign_key: :post_id)
belongs_to(:job, Job, foreign_key: :job_id)
belongs_to(:account, Accounts.User, foreign_key: :account_id)

embeds_many(:report_cases, Embeds.AbuseReportCase, on_replace: :delete)
field(:report_cases_count, :integer, default: 0)

belongs_to(:operate_user, Accounts.User, foreign_key: :operate_user_id)

field(:deal_with, :string)
field(:is_closed, :boolean, default: false)

timestamps(type: :utc_datetime)
end

@doc false
def changeset(%AbuseReport{} = struct, attrs) do
struct
|> cast(attrs, @optional_fields)
|> cast_embed(:report_cases, required: true, with: &Embeds.AbuseReportCase.changeset/2)
end

def update_changeset(%AbuseReport{} = struct, attrs) do
struct
|> cast(attrs, @update_fields)
|> cast_embed(:report_cases, required: true, with: &Embeds.AbuseReportCase.changeset/2)
end
end
104 changes: 104 additions & 0 deletions lib/groupher_server/cms/article_comment.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
defmodule GroupherServer.CMS.ArticleComment do
@moduledoc false
alias __MODULE__

use Ecto.Schema
use Accessible

import Ecto.Changeset

alias GroupherServer.{Accounts, CMS}

alias CMS.{
Post,
Job,
Embeds,
ArticleCommentUpvote
}

# alias Helper.HTML

@required_fields ~w(body_html author_id)a
@optional_fields ~w(post_id job_id reply_to_id replies_count is_folded is_reported is_deleted floor is_article_author)a
@updatable_fields ~w(is_folded is_reported is_deleted floor upvotes_count)a

@max_participator_count 5
@max_parent_replies_count 3

@supported_emotions [:downvote, :beer, :heart, :biceps, :orz, :confused, :pill]
@max_latest_emotion_users_count 5

@delete_hint "this comment is deleted"
# 举报超过此数评论会被自动折叠
@report_threshold_for_fold 5

@doc "latest participators stores in article comment_participators field"
def max_participator_count(), do: @max_participator_count
@doc "latest replies stores in article_comment replies field, used for frontend display"
def max_parent_replies_count(), do: @max_parent_replies_count

@doc "操作某 emotion 的最近用户"
def max_latest_emotion_users_count(), do: @max_latest_emotion_users_count

def supported_emotions(), do: @supported_emotions
def delete_hint(), do: @delete_hint

def report_threshold_for_fold, do: @report_threshold_for_fold

@type t :: %ArticleComment{}
schema "articles_comments" do
field(:body_html, :string)
field(:replies_count, :integer, default: 0)

# 是否被折叠
field(:is_folded, :boolean, default: false)
# 是否被举报
field(:is_reported, :boolean, default: false)
# 是否被删除
field(:is_deleted, :boolean, default: false)
# 楼层
field(:floor, :integer, default: 0)

# 是否是评论文章的作者
field(:is_article_author, :boolean, default: false)
field(:upvotes_count, :integer, default: 0)

belongs_to(:author, Accounts.User, foreign_key: :author_id)
belongs_to(:post, Post, foreign_key: :post_id)
belongs_to(:job, Job, foreign_key: :job_id)
belongs_to(:reply_to, ArticleComment, foreign_key: :reply_to_id)

embeds_many(:replies, ArticleComment, on_replace: :delete)
embeds_one(:emotions, Embeds.ArticleCommentEmotion, on_replace: :update)
embeds_one(:meta, Embeds.ArticleCommentMeta, on_replace: :update)

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

timestamps(type: :utc_datetime)
end

@doc false
def changeset(%ArticleComment{} = article_comment, attrs) do
article_comment
|> cast(attrs, @required_fields ++ @optional_fields)
|> cast_embed(:emotions, required: true, with: &Embeds.ArticleCommentEmotion.changeset/2)
|> validate_required(@required_fields)
|> generl_changeset
end

# @doc false
def update_changeset(%ArticleComment{} = article_comment, attrs) do
article_comment
|> cast(attrs, @required_fields ++ @updatable_fields)
# |> cast_embed(:emotions, required: false, with: &Embeds.ArticleCommentEmotion.changeset/2)
|> generl_changeset
end

defp generl_changeset(content) do
content
|> foreign_key_constraint(:author_id)

# |> validate_length(:body_html, min: 3, max: 2000)
# |> HTML.safe_string(:body_html)
end
end
24 changes: 24 additions & 0 deletions lib/groupher_server/cms/article_comment_participator.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
defmodule GroupherServer.CMS.ArticleCommentParticipator do
@moduledoc false

use Ecto.Schema

alias GroupherServer.Accounts.User

# alias CMS.{
# Post,
# Job,
# ArticleCommentUpvote
# }

# alias Helper.HTML

# @required_fields ~w(user_id)a
# @optional_fields ~w(post_id job_id)a

embedded_schema do
# field(:reply_time, :string)

belongs_to(:user, User)
end
end
29 changes: 29 additions & 0 deletions lib/groupher_server/cms/article_comment_reply.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
defmodule GroupherServer.CMS.ArticleCommentReply do
@moduledoc false
alias __MODULE__

use Ecto.Schema
import Ecto.Changeset

alias GroupherServer.CMS
alias CMS.ArticleComment

@required_fields ~w(article_comment_id reply_to_id)a

@type t :: %ArticleCommentReply{}
schema "articles_comments_replies" do
belongs_to(:article_comment, ArticleComment, foreign_key: :article_comment_id)
belongs_to(:reply_to, ArticleComment, foreign_key: :reply_to_id)

timestamps(type: :utc_datetime)
end

@doc false
def changeset(%ArticleCommentReply{} = article_comment_reply, attrs) do
article_comment_reply
|> cast(attrs, @required_fields)
|> validate_required(@required_fields)
|> foreign_key_constraint(:article_comment_id)
|> foreign_key_constraint(:reply_to_id)
end
end
32 changes: 32 additions & 0 deletions lib/groupher_server/cms/article_comment_upvote.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
defmodule GroupherServer.CMS.ArticleCommentUpvote do
@moduledoc false
alias __MODULE__

use Ecto.Schema
import Ecto.Changeset

alias GroupherServer.{Accounts, CMS}
alias CMS.ArticleComment

@required_fields ~w(article_comment_id user_id)a

@type t :: %ArticleCommentUpvote{}
schema "articles_comments_upvotes" do
belongs_to(:user, Accounts.User, foreign_key: :user_id)
belongs_to(:article_comment, ArticleComment, foreign_key: :article_comment_id)

timestamps(type: :utc_datetime)
end

@doc false
def changeset(%ArticleCommentUpvote{} = article_comment_upvote, attrs) do
article_comment_upvote
|> cast(attrs, @required_fields)
|> validate_required(@required_fields)
|> foreign_key_constraint(:article_comment_id)
|> foreign_key_constraint(:user_id)
|> unique_constraint(:user_id,
name: :articles_comments_upvotes_user_id_article_comment_id_index
)
end
end
40 changes: 40 additions & 0 deletions lib/groupher_server/cms/article_comment_user_emotion.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
defmodule GroupherServer.CMS.ArticleCommentUserEmotion do
@moduledoc false
alias __MODULE__

use Ecto.Schema
import Ecto.Changeset

alias GroupherServer.{Accounts, CMS}
alias CMS.ArticleComment

@required_fields ~w(article_comment_id user_id recived_user_id)a
@optional_fields ~w(downvote beer heart biceps orz confused pill)a

@type t :: %ArticleCommentUserEmotion{}
schema "articles_comments_users_emotions" do
belongs_to(:article_comment, ArticleComment, foreign_key: :article_comment_id)
belongs_to(:recived_user, Accounts.User, foreign_key: :recived_user_id)
belongs_to(:user, Accounts.User, foreign_key: :user_id)

field(:downvote, :boolean, default: false)
field(:beer, :boolean, default: false)
field(:heart, :boolean, default: false)
field(:biceps, :boolean, default: false)
field(:orz, :boolean, default: false)
field(:confused, :boolean, default: false)
field(:pill, :boolean, default: false)

timestamps(type: :utc_datetime)
end

@doc false
def changeset(%ArticleCommentUserEmotion{} = struct, attrs) do
struct
|> cast(attrs, @required_fields ++ @optional_fields)
|> validate_required(@required_fields)
|> foreign_key_constraint(:article_comment_id)
|> foreign_key_constraint(:user_id)
|> foreign_key_constraint(:recived_user_id)
end
end
30 changes: 27 additions & 3 deletions lib/groupher_server/cms/cms.ex
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@ defmodule GroupherServer.CMS do
alias GroupherServer.CMS.Delegate

alias Delegate.{
AbuseReport,
ArticleCURD,
ArticleOperation,
ArticleReaction,
FavoritedContents,
ArticleComment,
CommentCURD,
CommunitySync,
CommentReaction,
Expand Down Expand Up @@ -106,22 +108,44 @@ defmodule GroupherServer.CMS do
defdelegate unset_community(community, thread, content_id), to: ArticleOperation

# Comment CURD
defdelegate list_article_comments(thread, article_id, filters), to: ArticleComment
defdelegate list_article_comments(thread, article_id, filters, user), to: ArticleComment
defdelegate list_folded_article_comments(thread, article_id, filters), to: ArticleComment
defdelegate list_folded_article_comments(thread, article_id, filters, user), to: ArticleComment
defdelegate list_reported_article_comments(thread, article_id, filters), to: ArticleComment

defdelegate list_reported_article_comments(thread, article_id, filters, user),
to: ArticleComment

defdelegate list_comment_replies(comment_id, filters), to: ArticleComment

defdelegate list_comments(thread, content_id, filters), to: CommentCURD
defdelegate list_comments_participators(thread, content_id, filters), to: CommentCURD

defdelegate create_article_comment(thread, article_id, args, user), to: ArticleComment
defdelegate upvote_article_comment(comment_id, user), to: ArticleComment
defdelegate delete_article_comment(comment_id, user), to: ArticleComment
defdelegate reply_article_comment(comment_id, args, user), to: ArticleComment

defdelegate make_emotion(comment_id, args, user), to: ArticleComment
defdelegate fold_article_comment(comment_id, user), to: ArticleComment
defdelegate unfold_article_comment(comment_id, user), to: ArticleComment
defdelegate report_article_comment(comment_id, user), to: ArticleComment
defdelegate unreport_article_comment(comment_id, user), to: ArticleComment

defdelegate create_comment(thread, content_id, args, user), to: CommentCURD
defdelegate update_comment(thread, id, args, user), to: CommentCURD
defdelegate delete_comment(thread, content_id), to: CommentCURD
defdelegate list_replies(thread, comment, user), to: CommentCURD
defdelegate reply_comment(thread, comment, args, user), to: CommentCURD

# report
defdelegate create_report(type, content_id, args, user), to: AbuseReport

# Comment Reaction
# >> like / undo like
defdelegate like_comment(thread, comment, user), to: CommentReaction
defdelegate undo_like_comment(thread, comment, user), to: CommentReaction
# >> dislike / undo dislike
defdelegate dislike_comment(thread, comment, user), to: CommentReaction
defdelegate undo_dislike_comment(thread, comment, user), to: CommentReaction

# Passport CURD
defdelegate stamp_passport(rules, user), to: PassportCURD
Expand Down
1 change: 0 additions & 1 deletion lib/groupher_server/cms/community.ex
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ defmodule GroupherServer.CMS.Community do
alias CMS.{
Category,
Post,
Video,
Repo,
Job,
CommunityThread,
Expand Down
Loading