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

Commit d27a043

Browse files
authored
refactor(article-comment): forbid comment when article lock (#321)
* refactor(article-comments): lock article comment logic * refactor(article): fix general meta
1 parent 271d095 commit d27a043

File tree

14 files changed

+84
-43
lines changed

14 files changed

+84
-43
lines changed

lib/groupher_server/cms/cms.ex

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ defmodule GroupherServer.CMS do
9292
defdelegate pin_content(queryable, community_id), to: ArticleOperation
9393
defdelegate undo_pin_content(queryable, community_id), to: ArticleOperation
9494

95+
defdelegate lock_article_comment(content), to: ArticleOperation
9596
# defdelegate pin_content(queryable, community_id, thread), to: ArticleOperation
9697
# defdelegate undo_pin_content(queryable, community_id, thread, topic), to: ArticleOperation
9798
# defdelegate undo_pin_content(queryable, community_id, thread), to: ArticleOperation

lib/groupher_server/cms/delegates/article_curd.ex

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,15 @@ defmodule GroupherServer.CMS.Delegate.ArticleCURD do
1111
alias GroupherServer.{Accounts, CMS, Delivery, Email, Repo, Statistics}
1212

1313
alias Accounts.User
14-
alias CMS.{Author, Community, Delegate, Tag, Topic}
14+
alias CMS.{Author, Community, Embeds, Delegate, Tag, Topic}
1515

1616
alias Delegate.ArticleOperation
1717
alias Helper.{Later, ORM, QueryBuilder}
1818

1919
alias Ecto.Multi
2020

21+
@default_article_meta Embeds.ArticleMeta.default_meta()
22+
2123
@doc """
2224
login user read cms content by add views count and viewer record
2325
"""
@@ -69,14 +71,11 @@ defmodule GroupherServer.CMS.Delegate.ArticleCURD do
6971
{:ok, community} <- ORM.find(Community, cid) do
7072
Multi.new()
7173
|> Multi.run(:create_content, fn _, _ ->
72-
exec_create_content(action.target, attrs, author, community)
74+
do_create_content(action.target, attrs, author, community)
7375
end)
7476
|> Multi.run(:set_community, fn _, %{create_content: content} ->
7577
ArticleOperation.set_community(community, thread, content.id)
7678
end)
77-
|> Multi.run(:set_meta, fn _, %{create_content: content} ->
78-
ArticleOperation.set_meta(thread, content.id)
79-
end)
8079
|> Multi.run(:set_topic, fn _, %{create_content: content} ->
8180
exec_set_topic(thread, content.id, attrs)
8281
end)
@@ -130,8 +129,8 @@ defmodule GroupherServer.CMS.Delegate.ArticleCURD do
130129
|> Multi.run(:update_content, fn _, _ ->
131130
ORM.update(content, args)
132131
end)
133-
|> Multi.run(:update_meta, fn _, %{update_content: update_content} ->
134-
ArticleOperation.update_meta(update_content, :is_edited)
132+
|> Multi.run(:update_edit_status, fn _, %{update_content: update_content} ->
133+
ArticleOperation.update_edit_status(update_content)
135134
end)
136135
|> Multi.run(:update_tag, fn _, _ ->
137136
# TODO: move it to ArticleOperation moudel
@@ -400,10 +399,6 @@ defmodule GroupherServer.CMS.Delegate.ArticleCURD do
400399
{:error, [message: "set community", code: ecode(:create_fails)]}
401400
end
402401

403-
defp create_content_result({:error, :set_meta, _result, _steps}) do
404-
{:error, [message: "set meta info", code: ecode(:create_fails)]}
405-
end
406-
407402
defp create_content_result({:error, :set_community_flag, _result, _steps}) do
408403
{:error, [message: "set community flag", code: ecode(:create_fails)]}
409404
end
@@ -420,7 +415,7 @@ defmodule GroupherServer.CMS.Delegate.ArticleCURD do
420415
{:error, [message: "log action", code: ecode(:create_fails)]}
421416
end
422417

423-
defp update_content_result({:ok, %{update_meta: result}}), do: {:ok, result}
418+
defp update_content_result({:ok, %{update_edit_status: result}}), do: {:ok, result}
424419
defp update_content_result({:error, :update_content, result, _steps}), do: {:error, result}
425420
defp update_content_result({:error, :update_tag, result, _steps}), do: {:error, result}
426421

@@ -429,12 +424,13 @@ defmodule GroupherServer.CMS.Delegate.ArticleCURD do
429424
defp content_id(:repo, id), do: %{repo_id: id}
430425

431426
# for create content step in Multi.new
432-
defp exec_create_content(target, attrs, %Author{id: aid}, %Community{id: cid}) do
427+
defp do_create_content(target, attrs, %Author{id: aid}, %Community{id: cid}) do
433428
target
434429
|> struct()
435430
|> target.changeset(attrs)
436431
|> Ecto.Changeset.put_change(:author_id, aid)
437432
|> Ecto.Changeset.put_change(:origial_community_id, integerfy(cid))
433+
|> Ecto.Changeset.put_embed(:meta, @default_article_meta)
438434
|> Repo.insert()
439435
end
440436

lib/groupher_server/cms/delegates/article_operation.ex

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -255,17 +255,9 @@ defmodule GroupherServer.CMS.Delegate.ArticleOperation do
255255

256256
def set_topic(_topic, _thread, _content_id), do: {:ok, :pass}
257257

258-
@doc "set meta info"
259-
# embeds_one do not have default option, so we init it with empty map mannully
260-
# see: https://github.com/elixir-ecto/ecto/issues/2634
261-
def set_meta(:post, content_id) do
262-
ORM.update_by(Post, [id: content_id], %{meta: %{}})
263-
end
264-
265-
def set_meta(_, _), do: {:ok, :pass}
266-
267258
@doc "update isEdited meta label if needed"
268-
def update_meta(%Post{meta: %Embeds.ArticleMeta{is_edited: false} = meta} = content, :is_edited) do
259+
# TODO: diff history
260+
def update_edit_status(%{meta: %Embeds.ArticleMeta{is_edited: false} = meta} = content) do
269261
new_meta =
270262
meta
271263
|> Map.from_struct()
@@ -276,13 +268,29 @@ defmodule GroupherServer.CMS.Delegate.ArticleOperation do
276268
end
277269

278270
# for test or exsiting articles
279-
def update_meta(%Post{meta: nil} = content, :is_edited) do
271+
def update_edit_status(%{meta: nil} = content) do
280272
new_meta = Embeds.ArticleMeta.default_meta() |> Map.merge(%{is_edited: true})
281273

282274
do_update_meta(content, new_meta)
283275
end
284276

285-
def update_meta(content, _), do: {:ok, content}
277+
def update_edit_status(content, _), do: {:ok, content}
278+
279+
@doc "lock comment of a article"
280+
# TODO: record it to ArticleLog
281+
def lock_article_comment(
282+
%{meta: %Embeds.ArticleMeta{is_comment_locked: false} = meta} = content
283+
) do
284+
new_meta =
285+
meta
286+
|> Map.from_struct()
287+
|> Map.delete(:id)
288+
|> Map.merge(%{is_comment_locked: true})
289+
290+
do_update_meta(content, new_meta)
291+
end
292+
293+
def lock_article_comment(content), do: {:ok, content}
286294

287295
# TODO: put it into ORM helper
288296
defp do_update_meta(%{meta: _} = content, meta_params) do

lib/groupher_server/cms/embeds/article_meta.ex

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,26 +3,27 @@ defmodule GroupherServer.CMS.Embeds.ArticleMeta do
33
general article meta info for article-like content, like post, job, works ...
44
"""
55
use Ecto.Schema
6+
import Ecto.Changeset
7+
8+
@optional_fields ~w(is_edited is_comment_locked is_reported)a
69

710
@default_meta %{
811
is_edited: false,
9-
forbid_comment: false,
12+
is_comment_locked: false,
1013
is_reported: false
11-
# linkedPostsCount: 0,
12-
# linkedJobsCount: 0,
13-
# linkedWorksCount: 0,
14-
# reaction: %{
15-
# rocketCount: 0,
16-
# heartCount: 0,
17-
# }
1814
}
1915

2016
@doc "for test usage"
2117
def default_meta(), do: @default_meta
2218

2319
embedded_schema do
2420
field(:is_edited, :boolean, default: false)
25-
field(:forbid_comment, :boolean, default: false)
21+
field(:is_comment_locked, :boolean, default: false)
2622
field(:is_reported, :boolean, default: false)
2723
end
24+
25+
def changeset(struct, params) do
26+
struct
27+
|> cast(params, @optional_fields)
28+
end
2829
end

lib/groupher_server/cms/job.ex

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ defmodule GroupherServer.CMS.Job do
1111

1212
alias CMS.{
1313
Author,
14+
Embeds,
1415
ArticleComment,
1516
Community,
1617
JobFavorite,
@@ -36,6 +37,9 @@ defmodule GroupherServer.CMS.Job do
3637
field(:body, :string)
3738
belongs_to(:author, Author)
3839
field(:views, :integer, default: 0)
40+
41+
embeds_one(:meta, Embeds.ArticleMeta, on_replace: :update)
42+
3943
field(:link_addr, :string)
4044
field(:copy_right, :string)
4145

@@ -89,13 +93,15 @@ defmodule GroupherServer.CMS.Job do
8993
job
9094
|> cast(attrs, @optional_fields ++ @required_fields)
9195
|> validate_required(@required_fields)
96+
|> cast_embed(:meta, required: false, with: &Embeds.ArticleMeta.changeset/2)
9297
|> generl_changeset
9398
end
9499

95100
@doc false
96101
def update_changeset(%Job{} = job, attrs) do
97102
job
98103
|> cast(attrs, @optional_fields ++ @required_fields)
104+
# |> cast_embed(:meta, required: false, with: &Embeds.ArticleMeta.changeset/2)
99105
|> generl_changeset
100106
end
101107

lib/groupher_server/cms/post.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ defmodule GroupherServer.CMS.Post do
1313
Embeds,
1414
Author,
1515
ArticleComment,
16-
ArticleCommentParticipator,
1716
Community,
1817
PostComment,
1918
PostCommunityFlag,
@@ -105,6 +104,7 @@ defmodule GroupherServer.CMS.Post do
105104
post
106105
|> cast(attrs, @optional_fields ++ @required_fields)
107106
|> validate_required(@required_fields)
107+
|> cast_embed(:meta, required: false, with: &Embeds.ArticleMeta.changeset/2)
108108
|> generl_changeset
109109
end
110110

lib/groupher_server/cms/repo.ex

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ defmodule GroupherServer.CMS.Repo do
99

1010
alias CMS.{
1111
Author,
12+
Embeds,
1213
Community,
1314
RepoContributor,
1415
RepoFavorite,
@@ -48,6 +49,9 @@ defmodule GroupherServer.CMS.Repo do
4849
embeds_many(:contributors, RepoContributor, on_replace: :delete)
4950

5051
field(:views, :integer, default: 0)
52+
53+
embeds_one(:meta, Embeds.ArticleMeta, on_replace: :update)
54+
5155
belongs_to(:author, Author)
5256
has_many(:community_flags, {"repos_communities_flags", RepoCommunityFlag})
5357

@@ -87,13 +91,15 @@ defmodule GroupherServer.CMS.Repo do
8791
repo
8892
|> cast(attrs, @optional_fields ++ @required_fields)
8993
|> validate_required(@required_fields)
94+
|> cast_embed(:meta, required: false, with: &Embeds.ArticleMeta.changeset/2)
9095
|> generl_changeset
9196
end
9297

9398
@doc false
9499
def update_changeset(%Repo{} = repo, attrs) do
95100
repo
96101
|> cast(attrs, @optional_fields ++ @required_fields)
102+
# |> cast_embed(:meta, required: false, with: &Embeds.ArticleMeta.changeset/2)
97103
|> generl_changeset
98104
end
99105

lib/groupher_server_web/schema/cms/cms_types.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -428,7 +428,7 @@ defmodule GroupherServerWeb.Schema.CMS.Types do
428428
@desc "article meta info"
429429
object :article_meta do
430430
field(:is_edited, :boolean)
431-
field(:forbid_comment, :boolean)
431+
field(:is_comment_locked, :boolean)
432432
# field(:isReported, :boolean)
433433
# field(:linked_posts_count, :integer)
434434
# field(:linked_jobs_count, :integer)
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
defmodule GroupherServer.Repo.Migrations.AddMetaToArticleContent do
2+
use Ecto.Migration
3+
4+
def change do
5+
alter table(:cms_jobs) do
6+
add(:meta, :map)
7+
end
8+
9+
alter table(:cms_repos) do
10+
add(:meta, :map)
11+
end
12+
end
13+
end

test/groupher_server/accounts/published_contents_test.exs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ defmodule GroupherServer.Test.Accounts.PublishedContents do
6767
assert results.total_count == 0
6868
end
6969

70+
@tag :wip
7071
test "user can get paged published jobs", ~m(user user2 community community2)a do
7172
pub_jobs =
7273
Enum.reduce(1..@publish_count, [], fn _, acc ->

0 commit comments

Comments
 (0)