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

Commit c0bbf97

Browse files
committed
refactor(article-comments): add is_article_author && meta
1 parent a3ae8ce commit c0bbf97

File tree

8 files changed

+110
-12
lines changed

8 files changed

+110
-12
lines changed

lib/groupher_server/cms/article_comment.ex

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ defmodule GroupherServer.CMS.ArticleComment do
8282
article_comment
8383
|> cast(attrs, @required_fields ++ @optional_fields)
8484
|> cast_embed(:emotions, required: true, with: &Embeds.ArticleCommentEmotion.changeset/2)
85+
|> cast_embed(:meta, required: true, with: &Embeds.ArticleCommentMeta.changeset/2)
8586
|> validate_required(@required_fields)
8687
|> generl_changeset
8788
end
@@ -90,7 +91,7 @@ defmodule GroupherServer.CMS.ArticleComment do
9091
def update_changeset(%ArticleComment{} = article_comment, attrs) do
9192
article_comment
9293
|> cast(attrs, @required_fields ++ @updatable_fields)
93-
# |> cast_embed(:emotions, required: false, with: &Embeds.ArticleCommentEmotion.changeset/2)
94+
|> cast_embed(:meta, required: true, with: &Embeds.ArticleCommentMeta.changeset/2)
9495
|> generl_changeset
9596
end
9697

lib/groupher_server/cms/author.ex

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,11 @@ defmodule GroupherServer.CMS.Author do
33
alias __MODULE__
44

55
use Ecto.Schema
6+
use Accessible
7+
68
import Ecto.Changeset
79

810
alias GroupherServer.{Accounts, CMS}
9-
1011
alias CMS.Post
1112

1213
@type t :: %Author{}

lib/groupher_server/cms/delegates/article_comment.ex

Lines changed: 59 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ defmodule GroupherServer.CMS.Delegate.ArticleComment do
88
import GroupherServer.CMS.Utils.Matcher2
99
import ShortMaps
1010

11+
alias Helper.Types, as: T
1112
alias Helper.{ORM, QueryBuilder}
1213
alias GroupherServer.{Accounts, CMS, Repo}
1314

@@ -33,6 +34,8 @@ defmodule GroupherServer.CMS.Delegate.ArticleComment do
3334
@delete_hint ArticleComment.delete_hint()
3435
@report_threshold_for_fold ArticleComment.report_threshold_for_fold()
3536

37+
@default_comment_meta Embeds.ArticleCommentMeta.default_meta()
38+
3639
@doc """
3740
list paged article comments
3841
"""
@@ -317,10 +320,10 @@ defmodule GroupherServer.CMS.Delegate.ArticleComment do
317320
end
318321

319322
@doc "upvote a comment"
320-
# TODO: user has upvoted logic, move to GraphQL
321323
def upvote_article_comment(comment_id, %User{id: user_id}) do
322324
with {:ok, comment} <- ORM.find(ArticleComment, comment_id),
323325
false <- comment.is_deleted do
326+
# IO.inspect(comment, label: "the comment")
324327
Multi.new()
325328
|> Multi.run(:create_comment_upvote, fn _, _ ->
326329
ORM.create(ArticleCommentUpvote, %{article_comment_id: comment.id, user_id: user_id})
@@ -330,15 +333,68 @@ defmodule GroupherServer.CMS.Delegate.ArticleComment do
330333
upvotes_count = Repo.aggregate(count_query, :count)
331334
ORM.update(comment, %{upvotes_count: upvotes_count})
332335
end)
336+
|> Multi.run(:check_article_author_upvoted, fn _, _ ->
337+
with {:ok, article} = get_full_comment(comment_id) do
338+
is_article_author_upvoted = article.author.id == user_id
339+
340+
case is_article_author_upvoted do
341+
true ->
342+
new_meta =
343+
comment.meta
344+
|> Map.from_struct()
345+
|> Map.merge(%{is_article_author_upvoted: true})
346+
347+
comment |> ORM.update(%{meta: new_meta})
348+
349+
false ->
350+
{:ok, :pass}
351+
end
352+
end
353+
end)
333354
|> Repo.transaction()
334355
|> upsert_comment_result()
335356
end
336357
end
337358

359+
@spec get_full_comment(String.t()) :: {:ok, T.article_info()} | {:error, nil}
360+
defp get_full_comment(comment_id) do
361+
query = from(c in ArticleComment, where: c.id == ^comment_id, preload: :post, preload: :job)
362+
363+
with {:ok, comment} <- Repo.one(query) |> done() do
364+
extract_article_info(comment)
365+
end
366+
end
367+
368+
defp extract_article_info(%ArticleComment{post: %Post{} = post}) when not is_nil(post) do
369+
do_extract_article_info(:post, post)
370+
end
371+
372+
defp extract_article_info(%ArticleComment{job: %Job{} = job}) when not is_nil(job) do
373+
do_extract_article_info(:job, job)
374+
end
375+
376+
@spec do_extract_article_info(T.article_thread(), T.article_common()) :: {:ok, T.article_info()}
377+
defp do_extract_article_info(thread, article) do
378+
with {:ok, article_with_author} <- Repo.preload(article, author: :user) |> done(),
379+
article_author <- get_in(article_with_author, [:author, :user]) do
380+
#
381+
article_info = %{title: article.title}
382+
383+
author_info = %{
384+
id: article_author.id,
385+
login: article_author.login,
386+
nickname: article_author.nickname
387+
}
388+
389+
{:ok, %{thread: thread, article: article_info, author: author_info}}
390+
end
391+
end
392+
338393
# creat article comment for parent or reply
339394
# set floor
340395
# TODO: parse editor-json
341396
# set default emotions
397+
# TODO: meta
342398
defp do_create_comment(
343399
content,
344400
foreign_key,
@@ -358,7 +414,8 @@ defmodule GroupherServer.CMS.Delegate.ArticleComment do
358414
body_html: content,
359415
emotions: @default_emotions,
360416
floor: floor,
361-
is_article_author: user_id == article_author_id
417+
is_article_author: user_id == article_author_id,
418+
meta: @default_comment_meta
362419
},
363420
foreign_key,
364421
article_id

lib/groupher_server/cms/embeds/article_comment_meta.ex

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@ defmodule GroupherServer.CMS.Embeds.ArticleCommentMeta do
33
general article comment meta info
44
"""
55
use Ecto.Schema
6+
import Ecto.Changeset
67

7-
alias CMS.Embeds
8+
@optional_fields ~w(is_article_author_upvoted is_solution report_count)a
89

910
@default_meta %{
1011
is_article_author_upvoted: false,
1112
is_solution: false,
12-
report_count: 0,
13-
report_users: []
13+
report_count: 0
1414
}
1515

1616
@doc "for test usage"
@@ -21,6 +21,10 @@ defmodule GroupherServer.CMS.Embeds.ArticleCommentMeta do
2121
field(:is_solution, :boolean, default: false)
2222

2323
field(:report_count, :integer, default: 0)
24-
embeds_many(:report_users, Embeds.User, on_replace: :delete)
24+
end
25+
26+
def changeset(struct, params) do
27+
struct
28+
|> cast(params, @optional_fields)
2529
end
2630
end

lib/groupher_server/cms/job.ex

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ defmodule GroupherServer.CMS.Job do
33
alias __MODULE__
44

55
use Ecto.Schema
6+
use Accessible
7+
68
import Ecto.Changeset
79

810
alias GroupherServer.CMS

lib/groupher_server/cms/post.ex

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ defmodule GroupherServer.CMS.Post do
33
alias __MODULE__
44

55
use Ecto.Schema
6+
use Accessible
7+
68
import Ecto.Changeset
79

810
alias GroupherServer.{CMS, Accounts}

lib/helper/types.ex

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,19 @@ defmodule Helper.Types do
2828
company: nil | String.t()
2929
}
3030

31+
@type article_thread :: :post | :job
32+
@type article_info :: %{
33+
thread: article_thread,
34+
article: %{
35+
title: String.t()
36+
},
37+
author: %{
38+
id: Integer.t(),
39+
login: String.t(),
40+
nickname: String.t()
41+
}
42+
}
43+
3144
@typedoc """
3245
editor.js's header tool data format
3346
"""

test/groupher_server/cms/article_comment_test.exs

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,13 @@ defmodule GroupherServer.Test.CMS.ArticleComment do
44
use GroupherServer.TestTools
55

66
alias Helper.ORM
7-
alias GroupherServer.CMS
7+
alias GroupherServer.{Accounts, CMS}
88

9-
alias CMS.{ArticleComment, Post, Job}
9+
alias CMS.{ArticleComment, Embeds, Post, Job}
1010

1111
@delete_hint CMS.ArticleComment.delete_hint()
1212
@report_threshold_for_fold ArticleComment.report_threshold_for_fold()
13+
@default_comment_meta Embeds.ArticleCommentMeta.default_meta()
1314

1415
setup do
1516
{:ok, user} = db_insert(:user)
@@ -21,7 +22,6 @@ defmodule GroupherServer.Test.CMS.ArticleComment do
2122
end
2223

2324
describe "[basic article comment]" do
24-
@tag :wip2
2525
test "post, job are supported by article comment.", ~m(user post job)a do
2626
post_comment_1 = "post_comment 1"
2727
post_comment_2 = "post_comment 2"
@@ -40,6 +40,12 @@ defmodule GroupherServer.Test.CMS.ArticleComment do
4040
assert List.first(post.article_comments).body_html == post_comment_1
4141
assert List.first(job.article_comments).body_html == job_comment_1
4242
end
43+
44+
@tag :wip2
45+
test "comment should have default meta after create", ~m(user post)a do
46+
{:ok, comment} = CMS.create_article_comment(:post, post.id, "post comment", user)
47+
assert comment.meta |> Map.from_struct() |> Map.delete(:id) == @default_comment_meta
48+
end
4349
end
4450

4551
describe "[article comment floor]" do
@@ -112,6 +118,18 @@ defmodule GroupherServer.Test.CMS.ArticleComment do
112118
assert List.first(comment.upvotes).user_id == user.id
113119
end
114120

121+
@tag :wip2
122+
test "article author upvote post comment will have flag", ~m(post user)a do
123+
comment = "post_comment"
124+
{:ok, comment} = CMS.create_article_comment(:post, post.id, comment, user)
125+
{:ok, author_user} = ORM.find(Accounts.User, post.author.user.id)
126+
127+
CMS.upvote_article_comment(comment.id, author_user)
128+
129+
{:ok, comment} = ORM.find(ArticleComment, comment.id, preload: :upvotes)
130+
assert comment.meta.is_article_author_upvoted
131+
end
132+
115133
@tag :wip
116134
test "user can upvote a job comment", ~m(user job)a do
117135
comment = "job_comment"
@@ -403,7 +421,7 @@ defmodule GroupherServer.Test.CMS.ArticleComment do
403421
{:ok, comment} = CMS.create_article_comment(:post, post.id, "commment", user)
404422
assert not comment.is_article_author
405423

406-
{:ok, author_user} = db_insert(:user, %{id: post.author_id})
424+
{:ok, author_user} = db_insert(:user, %{id: post.author.user.id})
407425
{:ok, comment} = CMS.create_article_comment(:post, post.id, "commment", author_user)
408426
assert comment.is_article_author
409427
end

0 commit comments

Comments
 (0)