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

feat(lock-comment): workflow #381

Merged
merged 5 commits into from
Jun 2, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
3 changes: 2 additions & 1 deletion lib/groupher_server/cms/cms.ex
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,6 @@ defmodule GroupherServer.CMS do
# >> set flag on article, like: pin / unpin article
defdelegate pin_article(thread, id, community_id), to: ArticleCommunity
defdelegate undo_pin_article(thread, id, community_id), to: ArticleCommunity
defdelegate lock_article_comment(thread, article_id), to: ArticleCommunity

# >> community: set / unset
defdelegate mirror_article(thread, article_id, community_id), to: ArticleCommunity
Expand Down Expand Up @@ -146,6 +145,8 @@ defmodule GroupherServer.CMS do
defdelegate upvote_article_comment(comment_id, user), to: ArticleCommentAction
defdelegate undo_upvote_article_comment(comment_id, user), to: ArticleCommentAction
defdelegate reply_article_comment(comment_id, args, user), to: ArticleCommentAction
defdelegate lock_article_comment(thread, article_id), to: ArticleCommentAction
defdelegate undo_lock_article_comment(thread, article_id), to: ArticleCommentAction

defdelegate pin_article_comment(comment_id), to: ArticleCommentAction
defdelegate undo_pin_article_comment(comment_id), to: ArticleCommentAction
Expand Down
19 changes: 15 additions & 4 deletions lib/groupher_server/cms/delegates/article_comment.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ defmodule GroupherServer.CMS.Delegate.ArticleComment do
CURD and operations for article comments
"""
import Ecto.Query, warn: false
import Helper.Utils, only: [done: 1]
import Helper.Utils, only: [done: 1, ensure: 2]
import Helper.ErrorCode

import GroupherServer.CMS.Delegate.Helper, only: [mark_viewer_emotion_states: 3]
Expand All @@ -22,6 +22,7 @@ defmodule GroupherServer.CMS.Delegate.ArticleComment do
@default_emotions Embeds.ArticleCommentEmotion.default_emotions()
@delete_hint ArticleComment.delete_hint()

@default_article_meta Embeds.ArticleMeta.default_meta()
@default_comment_meta Embeds.ArticleCommentMeta.default_meta()
@pinned_comment_limit ArticleComment.pinned_comment_limit()

Expand Down Expand Up @@ -92,9 +93,8 @@ defmodule GroupherServer.CMS.Delegate.ArticleComment do
"""
def create_article_comment(thread, article_id, content, %User{} = user) do
with {:ok, info} <- match(thread),
# make sure the article exsit
# author is passed by middleware, it's exsit for sure
{:ok, article} <- ORM.find(info.model, article_id, preload: [author: :user]) do
{:ok, article} <- ORM.find(info.model, article_id, preload: [author: :user]),
true <- can_comment?(article, user) do
Multi.new()
|> Multi.run(:create_article_comment, fn _, _ ->
do_create_comment(content, info.foreign_key, article, user)
Expand All @@ -113,9 +113,20 @@ defmodule GroupherServer.CMS.Delegate.ArticleComment do
end)
|> Repo.transaction()
|> result()
else
false -> raise_error(:article_comment_locked, "this article is forbid comment")
{:error, error} -> {:error, error}
end
end

@doc "check is article can be comemnt or not"
# TODO: check if use is in author's block list?
def can_comment?(article, _user) do
article_meta = ensure(article.meta, @default_article_meta)

not article_meta.is_comment_locked
end

@doc """
update a comment for article like psot, job ...
"""
Expand Down
45 changes: 42 additions & 3 deletions lib/groupher_server/cms/delegates/article_comment_action.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,16 @@ defmodule GroupherServer.CMS.Delegate.ArticleCommentAction do
CURD and operations for article comments
"""
import Ecto.Query, warn: false
import Helper.Utils, only: [done: 1, strip_struct: 1, get_config: 2]
import Helper.Utils, only: [done: 1, strip_struct: 1, get_config: 2, ensure: 2]
import Helper.ErrorCode

import GroupherServer.CMS.Delegate.ArticleComment,
only: [add_participator_to_article: 2, do_create_comment: 4, update_article_comments_count: 2]
only: [
add_participator_to_article: 2,
do_create_comment: 4,
update_article_comments_count: 2,
can_comment?: 2
]

import GroupherServer.CMS.Helper.Matcher

Expand All @@ -16,12 +21,20 @@ defmodule GroupherServer.CMS.Delegate.ArticleCommentAction do
alias GroupherServer.{Accounts, CMS, Repo}

alias Accounts.User
alias CMS.{ArticleComment, ArticlePinnedComment, ArticleCommentUpvote, ArticleCommentReply}

alias CMS.{
ArticleComment,
ArticlePinnedComment,
ArticleCommentUpvote,
ArticleCommentReply,
Embeds
}

alias Ecto.Multi

@article_threads get_config(:article, :threads)

@default_article_meta Embeds.ArticleMeta.default_meta()
@max_parent_replies_count ArticleComment.max_parent_replies_count()
@pinned_comment_limit ArticleComment.pinned_comment_limit()

Expand Down Expand Up @@ -102,6 +115,7 @@ defmodule GroupherServer.CMS.Delegate.ArticleCommentAction do
ORM.find_by(ArticleComment, %{id: comment_id, is_deleted: false}),
replying_comment <- Repo.preload(target_comment, reply_to: :author),
{thread, article} <- get_article(replying_comment),
true <- can_comment?(article, user),
{:ok, info} <- match(thread),
parent_comment <- get_parent_comment(replying_comment) do
Multi.new()
Expand Down Expand Up @@ -136,6 +150,9 @@ defmodule GroupherServer.CMS.Delegate.ArticleCommentAction do
end)
|> Repo.transaction()
|> result()
else
false -> raise_error(:article_comment_locked, "this article is forbid comment")
{:error, error} -> {:error, error}
end
end

Expand Down Expand Up @@ -192,6 +209,28 @@ defmodule GroupherServer.CMS.Delegate.ArticleCommentAction do
end
end

@doc "lock comment of a article"
def lock_article_comment(thread, id) do
with {:ok, info} <- match(thread),
{:ok, article} <- ORM.find(info.model, id) do
article_meta = ensure(article.meta, @default_article_meta)
meta = Map.merge(article_meta, %{is_comment_locked: true})

ORM.update_meta(article, meta)
end
end

@doc "undo lock comment of a article"
def undo_lock_article_comment(thread, id) do
with {:ok, info} <- match(thread),
{:ok, article} <- ORM.find(info.model, id) do
article_meta = ensure(article.meta, @default_article_meta)
meta = Map.merge(article_meta, %{is_comment_locked: false})

ORM.update_meta(article, meta)
end
end

defp update_article_author_upvoted_info(%ArticleComment{} = comment, user_id) do
with {:ok, article} = get_full_comment(comment.id) do
is_article_author_upvoted = article.author.id == user_id
Expand Down
13 changes: 1 addition & 12 deletions lib/groupher_server/cms/delegates/article_community.ex
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ defmodule GroupherServer.CMS.Delegate.ArticleCommunity do
import Ecto.Query, warn: false

import Helper.ErrorCode
import Helper.Utils, only: [strip_struct: 1, done: 1, ensure: 2]
import Helper.Utils, only: [strip_struct: 1, done: 1]
import GroupherServer.CMS.Helper.Matcher

alias Helper.Types, as: T
Expand Down Expand Up @@ -144,17 +144,6 @@ defmodule GroupherServer.CMS.Delegate.ArticleCommunity do

def update_edit_status(content, _), do: {:ok, content}

@doc "lock comment of a article"
def lock_article_comment(thread, id) do
with {:ok, info} <- match(thread),
{:ok, article} <- ORM.find(info.model, id) do
article_meta = ensure(article.meta, @default_article_meta)
meta = Map.merge(article_meta, %{is_comment_locked: true})

ORM.update_meta(article, meta)
end
end

# check if the thread has aready enough pinned articles
defp check_pinned_article_count(community_id, thread) do
thread_upcase = thread |> to_string |> String.upcase()
Expand Down
6 changes: 6 additions & 0 deletions lib/groupher_server_web/resolvers/cms_resolver.ex
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,12 @@ defmodule GroupherServerWeb.Resolvers.CMS do
# #######################
# thread reaction ..
# #######################
def lock_article_comment(_root, ~m(id thread)a, _info), do: CMS.lock_article_comment(thread, id)

def undo_lock_article_comment(_root, ~m(id thread)a, _info) do
CMS.undo_lock_article_comment(thread, id)
end

def sink_article(_root, ~m(id thread)a, _info), do: CMS.sink_article(thread, id)
def undo_sink_article(_root, ~m(id thread)a, _info), do: CMS.undo_sink_article(thread, id)

Expand Down
84 changes: 56 additions & 28 deletions lib/groupher_server_web/schema/Helper/mutations.ex
Original file line number Diff line number Diff line change
Expand Up @@ -5,34 +5,6 @@ defmodule GroupherServerWeb.Schema.Helper.Mutations do
alias GroupherServerWeb.Middleware, as: M
alias GroupherServerWeb.Resolvers, as: R

defmacro article_sink_mutation(thread) do
quote do
@desc unquote("sink a #{thread}")
field unquote(:"sink_#{thread}"), :article do
arg(:id, non_null(:id))
arg(:community_id, non_null(:id))
arg(:thread, unquote(:"#{thread}_thread"), default_value: unquote(thread))

middleware(M.Authorize, :login)
middleware(M.PassportLoader, source: :community)
middleware(M.Passport, claim: unquote("cms->c?->#{to_string(thread)}.sink"))
resolve(&R.CMS.sink_article/3)
end

@desc unquote("undo sink to #{thread}")
field unquote(:"undo_sink_#{thread}"), :article do
arg(:id, non_null(:id))
arg(:community_id, non_null(:id))
arg(:thread, unquote(:"#{thread}_thread"), default_value: unquote(thread))

middleware(M.Authorize, :login)
middleware(M.PassportLoader, source: :community)
middleware(M.Passport, claim: unquote("cms->c?->#{to_string(thread)}.undo_sink"))
resolve(&R.CMS.undo_sink_article/3)
end
end
end

defmacro article_upvote_mutation(thread) do
quote do
@desc unquote("upvote to #{thread}")
Expand Down Expand Up @@ -172,4 +144,60 @@ defmodule GroupherServerWeb.Schema.Helper.Mutations do
end
end
end

defmacro article_sink_mutation(thread) do
quote do
@desc unquote("sink a #{thread}")
field unquote(:"sink_#{thread}"), :article do
arg(:id, non_null(:id))
arg(:community_id, non_null(:id))
arg(:thread, unquote(:"#{thread}_thread"), default_value: unquote(thread))

middleware(M.Authorize, :login)
middleware(M.PassportLoader, source: :community)
middleware(M.Passport, claim: unquote("cms->c?->#{to_string(thread)}.sink"))
resolve(&R.CMS.sink_article/3)
end

@desc unquote("undo sink to #{thread}")
field unquote(:"undo_sink_#{thread}"), :article do
arg(:id, non_null(:id))
arg(:community_id, non_null(:id))
arg(:thread, unquote(:"#{thread}_thread"), default_value: unquote(thread))

middleware(M.Authorize, :login)
middleware(M.PassportLoader, source: :community)
middleware(M.Passport, claim: unquote("cms->c?->#{to_string(thread)}.undo_sink"))
resolve(&R.CMS.undo_sink_article/3)
end
end
end

defmacro article_lock_comment_mutation(thread) do
quote do
@desc unquote("lock comment to a #{thread}")
field unquote(:"lock_#{thread}_comment"), :article do
arg(:id, non_null(:id))
arg(:community_id, non_null(:id))
arg(:thread, unquote(:"#{thread}_thread"), default_value: unquote(thread))

middleware(M.Authorize, :login)
middleware(M.PassportLoader, source: :community)
middleware(M.Passport, claim: unquote("cms->c?->#{to_string(thread)}.lock_comment"))
resolve(&R.CMS.lock_article_comment/3)
end

@desc unquote("undo lock to a #{thread}")
field unquote(:"undo_lock_#{thread}_comment"), :article do
arg(:id, non_null(:id))
arg(:community_id, non_null(:id))
arg(:thread, unquote(:"#{thread}_thread"), default_value: unquote(thread))

middleware(M.Authorize, :login)
middleware(M.PassportLoader, source: :community)
middleware(M.Passport, claim: unquote("cms->c?->#{to_string(thread)}.undo_lock_comment"))
resolve(&R.CMS.undo_lock_article_comment/3)
end
end
end
end
3 changes: 2 additions & 1 deletion lib/groupher_server_web/schema/cms/mutations/job.ex
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,14 @@ defmodule GroupherServerWeb.Schema.CMS.Mutations.Job do
end

#############
article_sink_mutation(:job)
article_upvote_mutation(:job)
article_pin_mutation(:job)
article_mark_delete_mutation(:job)
article_delete_mutation(:job)
article_emotion_mutation(:job)
article_report_mutation(:job)
article_sink_mutation(:job)
article_lock_comment_mutation(:job)
#############
end
end
3 changes: 2 additions & 1 deletion lib/groupher_server_web/schema/cms/mutations/post.ex
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,14 @@ defmodule GroupherServerWeb.Schema.CMS.Mutations.Post do
end

#############
article_sink_mutation(:post)
article_upvote_mutation(:post)
article_pin_mutation(:post)
article_mark_delete_mutation(:post)
article_delete_mutation(:post)
article_emotion_mutation(:post)
article_report_mutation(:post)
article_sink_mutation(:post)
article_lock_comment_mutation(:post)
#############
end
end
3 changes: 2 additions & 1 deletion lib/groupher_server_web/schema/cms/mutations/repo.ex
Original file line number Diff line number Diff line change
Expand Up @@ -71,12 +71,13 @@ defmodule GroupherServerWeb.Schema.CMS.Mutations.Repo do
end

#############
article_sink_mutation(:repo)
article_pin_mutation(:repo)
article_mark_delete_mutation(:repo)
article_delete_mutation(:repo)
article_emotion_mutation(:repo)
article_report_mutation(:repo)
article_sink_mutation(:repo)
article_lock_comment_mutation(:repo)
#############
end
end
7 changes: 7 additions & 0 deletions lib/helper/certification.ex
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,13 @@ defmodule Helper.Certification do
"job.undo_sink",
"repo.sink",
"repo.undo_sink",
# lock/undo_lock article comment
"post.lock_comment",
"post.undo_lock_comment",
"job.lock_comment",
"job.undo_lock_comment",
"repo.lock_comment",
"repo.undo_lock_comment",
#
"post.mark_delete",
"post.undo_mark_delete",
Expand Down
1 change: 1 addition & 0 deletions lib/helper/error_code.ex
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ defmodule Helper.ErrorCode do
def ecode(:mirror_article), do: @article_base + 5
def ecode(:invalid_domain_tag), do: @article_base + 6
def ecode(:undo_sink_old_article), do: @article_base + 7
def ecode(:article_comment_locked), do: @article_base + 8

def ecode, do: @default_base
# def ecode(_), do: @default_base
Expand Down
7 changes: 6 additions & 1 deletion test/groupher_server/cms/articles/post_meta_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,19 @@ defmodule GroupherServer.Test.CMS.PostMeta do
assert post.meta.is_edited
end

test "post's lock article should work", ~m(user community post_attrs)a do
test "post's lock/undo_lock article should work", ~m(user community post_attrs)a do
{:ok, post} = CMS.create_article(community, :post, post_attrs, user)
assert not post.meta.is_comment_locked

{:ok, _} = CMS.lock_article_comment(:post, post.id)
{:ok, post} = ORM.find_by(Post, id: post.id)

assert post.meta.is_comment_locked

{:ok, _} = CMS.undo_lock_article_comment(:post, post.id)
{:ok, post} = ORM.find_by(Post, id: post.id)

assert not post.meta.is_comment_locked
end

# TODO:
Expand Down
Loading