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

Commit 271d095

Browse files
authored
Merge pull request #320 from coderplanets/article-comments-author-has-upvoted
refactor(article-comments): author has upvoted
2 parents a3ae8ce + 5549752 commit 271d095

File tree

19 files changed

+190
-92
lines changed

19 files changed

+190
-92
lines changed

cover/excoveralls.json

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.

lib/groupher_server/accounts/delegates/fans.ex

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,10 +75,10 @@ defmodule GroupherServer.Accounts.Delegate.Fans do
7575
{:ok, _follow_user} <- ORM.find(User, follower_id) do
7676
Multi.new()
7777
|> Multi.run(:delete_follower, fn _, _ ->
78-
ORM.findby_delete(UserFollower, %{user_id: follower_id, follower_id: user_id})
78+
ORM.findby_delete!(UserFollower, %{user_id: follower_id, follower_id: user_id})
7979
end)
8080
|> Multi.run(:delete_following, fn _, _ ->
81-
ORM.findby_delete(UserFollowing, %{user_id: user_id, following_id: follower_id})
81+
ORM.findby_delete!(UserFollowing, %{user_id: user_id, following_id: follower_id})
8282
end)
8383
|> Multi.run(:minus_achievement, fn _, _ ->
8484
Accounts.achieve(%User{id: follower_id}, :minus, :follow)

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/cms.ex

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@ defmodule GroupherServer.CMS do
124124

125125
defdelegate create_article_comment(thread, article_id, args, user), to: ArticleComment
126126
defdelegate upvote_article_comment(comment_id, user), to: ArticleComment
127+
defdelegate undo_upvote_article_comment(comment_id, user), to: ArticleComment
127128
defdelegate delete_article_comment(comment_id, user), to: ArticleComment
128129
defdelegate reply_article_comment(comment_id, args, user), to: ArticleComment
129130

lib/groupher_server/cms/delegates/article_comment.ex

Lines changed: 88 additions & 16 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
"""
@@ -263,7 +266,7 @@ defmodule GroupherServer.CMS.Delegate.ArticleComment do
263266
with {:ok, info} <- match(thread),
264267
# make sure the article exsit
265268
# author is passed by middleware, it's exsit for sure
266-
{:ok, article} <- ORM.find(info.model, article_id) do
269+
{:ok, article} <- ORM.find(info.model, article_id, preload: [author: :user]) do
267270
Multi.new()
268271
|> Multi.run(:create_article_comment, fn _, _ ->
269272
do_create_comment(content, info.foreign_key, article, user)
@@ -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,24 +333,58 @@ 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+
update_article_author_upvoted_info(comment, user_id)
338+
end)
339+
|> Repo.transaction()
340+
|> upsert_comment_result()
341+
end
342+
end
343+
344+
@doc "upvote a comment"
345+
def undo_upvote_article_comment(comment_id, %User{id: user_id}) do
346+
with {:ok, comment} <- ORM.find(ArticleComment, comment_id),
347+
false <- comment.is_deleted do
348+
Multi.new()
349+
|> Multi.run(:delete_comment_upvote, fn _, _ ->
350+
ORM.findby_delete(ArticleCommentUpvote, %{
351+
article_comment_id: comment.id,
352+
user_id: user_id
353+
})
354+
end)
355+
|> Multi.run(:desc_upvotes_count, fn _, _ ->
356+
count_query = from(c in ArticleCommentUpvote, where: c.article_comment_id == ^comment_id)
357+
upvotes_count = Repo.aggregate(count_query, :count)
358+
359+
ORM.update(comment, %{upvotes_count: Enum.max([upvotes_count - 1, 0])})
360+
end)
361+
|> Multi.run(:check_article_author_upvoted, fn _, %{desc_upvotes_count: updated_comment} ->
362+
update_article_author_upvoted_info(updated_comment, user_id)
363+
end)
333364
|> Repo.transaction()
334365
|> upsert_comment_result()
335366
end
336367
end
337368

369+
defp update_article_author_upvoted_info(%ArticleComment{} = comment, user_id) do
370+
with {:ok, article} = get_full_comment(comment.id) do
371+
is_article_author_upvoted = article.author.id == user_id
372+
373+
new_meta =
374+
comment.meta
375+
|> Map.from_struct()
376+
|> Map.merge(%{is_article_author_upvoted: is_article_author_upvoted})
377+
378+
comment |> ORM.update(%{meta: new_meta})
379+
end
380+
end
381+
338382
# creat article comment for parent or reply
339383
# set floor
340384
# TODO: parse editor-json
341385
# set default emotions
342-
defp do_create_comment(
343-
content,
344-
foreign_key,
345-
%{id: article_id, author_id: article_author_id},
346-
%User{
347-
id: user_id
348-
}
349-
) do
350-
count_query = from(c in ArticleComment, where: field(c, ^foreign_key) == ^article_id)
386+
defp do_create_comment(content, foreign_key, article, %User{id: user_id}) do
387+
count_query = from(c in ArticleComment, where: field(c, ^foreign_key) == ^article.id)
351388
floor = Repo.aggregate(count_query, :count) + 1
352389

353390
ArticleComment
@@ -358,10 +395,11 @@ defmodule GroupherServer.CMS.Delegate.ArticleComment do
358395
body_html: content,
359396
emotions: @default_emotions,
360397
floor: floor,
361-
is_article_author: user_id == article_author_id
398+
is_article_author: user_id == article.author.user.id,
399+
meta: @default_comment_meta
362400
},
363401
foreign_key,
364-
article_id
402+
article.id
365403
)
366404
)
367405
end
@@ -416,13 +454,13 @@ defmodule GroupherServer.CMS.Delegate.ArticleComment do
416454
defp add_participator_to_article(_, _), do: {:ok, :pass}
417455

418456
defp get_article(%ArticleComment{post_id: post_id} = comment) when not is_nil(post_id) do
419-
with {:ok, article} <- ORM.find(Post, comment.post_id) do
457+
with {:ok, article} <- ORM.find(Post, comment.post_id, preload: [author: :user]) do
420458
{:post, article}
421459
end
422460
end
423461

424462
defp get_article(%ArticleComment{job_id: job_id} = comment) when not is_nil(job_id) do
425-
with {:ok, article} <- ORM.find(Job, comment.job_id) do
463+
with {:ok, article} <- ORM.find(Job, comment.job_id, preload: [author: :user]) do
426464
{:job, article}
427465
end
428466
end
@@ -454,9 +492,43 @@ defmodule GroupherServer.CMS.Delegate.ArticleComment do
454492
%{paged_comments | entries: new_entries}
455493
end
456494

495+
@spec get_full_comment(String.t()) :: {:ok, T.article_info()} | {:error, nil}
496+
defp get_full_comment(comment_id) do
497+
query = from(c in ArticleComment, where: c.id == ^comment_id, preload: :post, preload: :job)
498+
499+
with {:ok, comment} <- Repo.one(query) |> done() do
500+
extract_article_info(comment)
501+
end
502+
end
503+
504+
defp extract_article_info(%ArticleComment{post: %Post{} = post}) when not is_nil(post) do
505+
do_extract_article_info(:post, post)
506+
end
507+
508+
defp extract_article_info(%ArticleComment{job: %Job{} = job}) when not is_nil(job) do
509+
do_extract_article_info(:job, job)
510+
end
511+
512+
@spec do_extract_article_info(T.article_thread(), T.article_common()) :: {:ok, T.article_info()}
513+
defp do_extract_article_info(thread, article) do
514+
with {:ok, article_with_author} <- Repo.preload(article, author: :user) |> done(),
515+
article_author <- get_in(article_with_author, [:author, :user]) do
516+
#
517+
article_info = %{title: article.title}
518+
519+
author_info = %{
520+
id: article_author.id,
521+
login: article_author.login,
522+
nickname: article_author.nickname
523+
}
524+
525+
{:ok, %{thread: thread, article: article_info, author: author_info}}
526+
end
527+
end
528+
457529
defp upsert_comment_result({:ok, %{create_article_comment: result}}), do: {:ok, result}
458530
defp upsert_comment_result({:ok, %{add_reply_to: result}}), do: {:ok, result}
459-
defp upsert_comment_result({:ok, %{inc_upvotes_count: result}}), do: {:ok, result}
531+
defp upsert_comment_result({:ok, %{check_article_author_upvoted: result}}), do: {:ok, result}
460532
defp upsert_comment_result({:ok, %{update_report_flag: result}}), do: {:ok, result}
461533
defp upsert_comment_result({:ok, %{update_comment_emotion: result}}), do: {:ok, result}
462534

lib/groupher_server/cms/delegates/comment_reaction.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ defmodule GroupherServer.CMS.Delegate.CommentReaction do
3737
with {:ok, action} <- match_action(thread, feeling) do
3838
clause = Map.merge(%{user_id: user_id}, merge_thread_comment_id(thread, comment_id))
3939
# clause = %{post_comment_id: comment_id, user_id: user_id}
40-
ORM.findby_delete(action.reactor, clause)
40+
ORM.findby_delete!(action.reactor, clause)
4141
ORM.find(action.target, comment_id)
4242
end
4343
end

lib/groupher_server/cms/delegates/community_operation.ex

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ defmodule GroupherServer.CMS.Delegate.CommunityOperation do
3535
"""
3636
def unset_category(%Community{id: community_id}, %Category{id: category_id}) do
3737
with {:ok, community_category} <-
38-
CommunityCategory |> ORM.findby_delete(~m(community_id category_id)a) do
38+
CommunityCategory |> ORM.findby_delete!(~m(community_id category_id)a) do
3939
Community |> ORM.find(community_category.community_id)
4040
end
4141
end
@@ -54,7 +54,7 @@ defmodule GroupherServer.CMS.Delegate.CommunityOperation do
5454
"""
5555
def unset_thread(%Community{id: community_id}, %Thread{id: thread_id}) do
5656
with {:ok, community_thread} <-
57-
CommunityThread |> ORM.findby_delete(~m(community_id thread_id)a) do
57+
CommunityThread |> ORM.findby_delete!(~m(community_id thread_id)a) do
5858
Community |> ORM.find(community_thread.community_id)
5959
end
6060
end
@@ -80,7 +80,7 @@ defmodule GroupherServer.CMS.Delegate.CommunityOperation do
8080
unset a community editor
8181
"""
8282
def unset_editor(%Community{id: community_id}, %User{id: user_id}) do
83-
with {:ok, _} <- ORM.findby_delete(CommunityEditor, ~m(user_id community_id)a),
83+
with {:ok, _} <- ORM.findby_delete!(CommunityEditor, ~m(user_id community_id)a),
8484
{:ok, _} <- PassportCURD.delete_passport(%User{id: user_id}) do
8585
User |> ORM.find(user_id)
8686
end
@@ -132,7 +132,7 @@ defmodule GroupherServer.CMS.Delegate.CommunityOperation do
132132
with {:ok, community} <- ORM.find(Community, community_id),
133133
true <- community.raw !== "home",
134134
{:ok, record} <-
135-
ORM.findby_delete(CommunitySubscriber, community_id: community.id, user_id: user_id) do
135+
ORM.findby_delete!(CommunitySubscriber, community_id: community.id, user_id: user_id) do
136136
Community |> ORM.find(record.community_id)
137137
else
138138
false ->
@@ -151,7 +151,7 @@ defmodule GroupherServer.CMS.Delegate.CommunityOperation do
151151
with {:ok, community} <- ORM.find(Community, community_id),
152152
true <- community.raw !== "home",
153153
{:ok, record} <-
154-
CommunitySubscriber |> ORM.findby_delete(community_id: community.id, user_id: user_id) do
154+
CommunitySubscriber |> ORM.findby_delete!(community_id: community.id, user_id: user_id) do
155155
update_community_geo(community_id, user_id, remote_ip, :dec)
156156
Community |> ORM.find(record.community_id)
157157
else
@@ -171,7 +171,7 @@ defmodule GroupherServer.CMS.Delegate.CommunityOperation do
171171
with {:ok, community} <- ORM.find(Community, community_id),
172172
true <- community.raw !== "home",
173173
{:ok, record} <-
174-
CommunitySubscriber |> ORM.findby_delete(community_id: community.id, user_id: user_id) do
174+
CommunitySubscriber |> ORM.findby_delete!(community_id: community.id, user_id: user_id) do
175175
update_community_geo_map(community.id, city, :dec)
176176
Community |> ORM.find(record.community_id)
177177
else

lib/groupher_server/cms/delegates/passport_curd.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ defmodule GroupherServer.CMS.Delegate.PassportCURD do
6565
end
6666

6767
def delete_passport(%Accounts.User{id: user_id}) do
68-
ORM.findby_delete(UserPasport, ~m(user_id)a)
68+
ORM.findby_delete!(UserPasport, ~m(user_id)a)
6969
end
7070

7171
defp reject_invalid_rules(rules) when is_map(rules) do

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

0 commit comments

Comments
 (0)