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

Commit 0ee167c

Browse files
committed
refactor(abuse-report): wip
1 parent ef26b7d commit 0ee167c

File tree

3 files changed

+118
-33
lines changed

3 files changed

+118
-33
lines changed

lib/groupher_server/cms/cms.ex

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,7 @@ defmodule GroupherServer.CMS do
162162
# TODO: move report to abuse report module
163163
defdelegate create_report(type, content_id, args, user), to: AbuseReport
164164
defdelegate report_article(thread, article_id, reason, attr, user), to: AbuseReport
165+
defdelegate undo_report_article(thread, article_id, user), to: AbuseReport
165166
defdelegate list_reports(type, content_id, filter), to: AbuseReport
166167
defdelegate report_article_comment(comment_id, user), to: ArticleCommentAction
167168
defdelegate unreport_article_comment(comment_id, user), to: ArticleCommentAction

lib/groupher_server/cms/delegates/abuse_report.ex

Lines changed: 67 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -36,33 +36,76 @@ defmodule GroupherServer.CMS.Delegate.AbuseReport do
3636
create_report(thread, article_id, reason, attr, user)
3737
end)
3838
|> Multi.run(:update_report_flag, fn _, _ ->
39-
count_query = from(c in AbuseReport, where: field(c, ^info.foreign_key) == ^article.id)
40-
reported_count = Repo.aggregate(count_query, :count)
41-
meta = article.meta |> Map.merge(%{reported_count: reported_count}) |> strip_struct
39+
update_report_meta(info, article, true)
40+
end)
41+
|> Repo.transaction()
42+
|> result()
43+
end
44+
end
4245

43-
article
44-
|> Ecto.Changeset.change(%{is_reported: true})
45-
|> Ecto.Changeset.put_embed(:meta, meta)
46-
|> Repo.update()
46+
@doc "unreport an article"
47+
def undo_report_article(thread, article_id, %User{} = user) do
48+
with {:ok, info} <- match(thread),
49+
{:ok, article} <- ORM.find(info.model, article_id) do
50+
Multi.new()
51+
|> Multi.run(:delete_abuse_report, fn _, _ ->
52+
delete_report(thread, article_id, user)
53+
end)
54+
|> Multi.run(:update_report_flag, fn _, _ ->
55+
update_report_meta(info, article, false)
4756
end)
4857
|> Repo.transaction()
4958
|> result()
5059
end
5160
end
5261

53-
@doc "unreport a comment"
54-
# def unreport_article(thread, article_id, %User{} = _user) do
55-
# with {:ok, comment} <- ORM.find(ArticleComment, comment_id) do
56-
# comment |> ORM.update(%{is_reported: false})
57-
# end
58-
# end
62+
defp delete_report(thread, content_id, %User{} = user) do
63+
with {:ok, info} <- match(thread),
64+
{:error, _} <- not_reported_before(info, content_id, user),
65+
{:ok, report} = ORM.find_by(AbuseReport, Map.put(%{}, info.foreign_key, content_id)) do
66+
case length(report.report_cases) do
67+
1 ->
68+
ORM.delete(report)
69+
70+
_ ->
71+
report_cases = report.report_cases |> Enum.reject(&(&1.user.login == user.login))
72+
73+
report
74+
|> Ecto.Changeset.change(%{report_cases_count: length(report_cases)})
75+
|> Ecto.Changeset.put_embed(:report_cases, report_cases)
76+
|> Repo.update()
77+
end
78+
end
79+
end
80+
81+
# update is_reported flag and reported_count in mete for article or comment
82+
defp update_report_meta(info, content, is_reported) do
83+
case ORM.find_by(AbuseReport, Map.put(%{}, info.foreign_key, content.id)) do
84+
{:ok, record} ->
85+
reported_count = record.report_cases |> length
86+
meta = content.meta |> Map.merge(%{reported_count: reported_count}) |> strip_struct
87+
88+
content
89+
|> Ecto.Changeset.change(%{is_reported: is_reported})
90+
|> Ecto.Changeset.put_embed(:meta, meta)
91+
|> Repo.update()
92+
93+
{:error, _} ->
94+
meta = content.meta |> Map.merge(%{reported_count: 0}) |> strip_struct
95+
96+
content
97+
|> Ecto.Changeset.change(%{is_reported: false})
98+
|> Ecto.Changeset.put_embed(:meta, meta)
99+
|> Repo.update()
100+
end
101+
end
59102

60103
def create_report(type, content_id, reason, attr, %User{} = user) do
61104
with {:ok, info} <- match(type),
62105
{:ok, report} <- not_reported_before(info, content_id, user) do
63106
case report do
64107
nil ->
65-
updated_report_cases = [
108+
report_cases = [
66109
%{
67110
reason: reason,
68111
additional_reason: attr,
@@ -71,13 +114,13 @@ defmodule GroupherServer.CMS.Delegate.AbuseReport do
71114
]
72115

73116
args =
74-
%{report_cases_count: 1, report_cases: updated_report_cases}
117+
%{report_cases_count: 1, report_cases: report_cases}
75118
|> Map.put(info.foreign_key, content_id)
76119

77120
AbuseReport |> ORM.create(args)
78121

79122
_ ->
80-
updated_report_cases =
123+
report_cases =
81124
report.report_cases
82125
|> List.insert_at(
83126
length(report.report_cases),
@@ -89,13 +132,18 @@ defmodule GroupherServer.CMS.Delegate.AbuseReport do
89132
)
90133

91134
report
92-
|> Ecto.Changeset.change(%{report_cases_count: length(updated_report_cases)})
93-
|> Ecto.Changeset.put_embed(:report_cases, updated_report_cases)
135+
|> Ecto.Changeset.change(%{report_cases_count: length(report_cases)})
136+
|> Ecto.Changeset.put_embed(:report_cases, report_cases)
94137
|> Repo.update()
95138
end
96139
end
97140
end
98141

142+
##############
143+
##############
144+
##############
145+
##############
146+
99147
@doc """
100148
create report record
101149
"""
@@ -161,6 +209,7 @@ defmodule GroupherServer.CMS.Delegate.AbuseReport do
161209
end
162210

163211
defp result({:ok, %{create_abuse_report: result}}), do: result |> done()
212+
defp result({:ok, %{delete_abuse_report: result}}), do: result |> done()
164213

165214
defp result({:error, _, result, _steps}) do
166215
{:error, result}

test/groupher_server/cms/abuse_reports/post_report_test.exs

Lines changed: 50 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,11 @@ defmodule GroupherServer.Test.CMS.AbuseReports.PostReport do
55

66
alias Helper.ORM
77
alias GroupherServer.CMS
8-
9-
alias CMS.AbuseReport
8+
# alias CMS.AbuseReport
109

1110
setup do
1211
{:ok, user} = db_insert(:user)
1312
{:ok, user2} = db_insert(:user)
14-
{:ok, post} = db_insert(:post)
1513

1614
{:ok, community} = db_insert(:community)
1715
post_attrs = mock_attrs(:post, %{community_id: community.id})
@@ -23,7 +21,7 @@ defmodule GroupherServer.Test.CMS.AbuseReports.PostReport do
2321
@tag :wip2
2422
test "report a post should have a abuse report record", ~m(community user post_attrs)a do
2523
{:ok, post} = CMS.create_content(community, :post, post_attrs, user)
26-
{:ok, report} = CMS.report_article(:post, post.id, "reason", "attr_info", user)
24+
{:ok, _report} = CMS.report_article(:post, post.id, "reason", "attr_info", user)
2725

2826
{:ok, all_reports} = CMS.list_reports(:post, post.id, %{page: 1, size: 20})
2927

@@ -40,14 +38,50 @@ defmodule GroupherServer.Test.CMS.AbuseReports.PostReport do
4038
assert post.meta.reported_count == 1
4139
end
4240

43-
@tag :wip
41+
@tag :wip2
42+
test "can undo a report", ~m(community user post_attrs)a do
43+
{:ok, post} = CMS.create_content(community, :post, post_attrs, user)
44+
{:ok, _report} = CMS.report_article(:post, post.id, "reason", "attr_info", user)
45+
{:ok, _report} = CMS.undo_report_article(:post, post.id, user)
46+
47+
{:ok, all_reports} = CMS.list_reports(:post, post.id, %{page: 1, size: 20})
48+
assert all_reports.total_count == 0
49+
end
50+
51+
@tag :wip2
52+
test "can undo a report with other user report it too",
53+
~m(community user user2 post_attrs)a do
54+
{:ok, post} = CMS.create_content(community, :post, post_attrs, user)
55+
{:ok, _report} = CMS.report_article(:post, post.id, "reason", "attr_info", user)
56+
{:ok, _report} = CMS.report_article(:post, post.id, "reason", "attr_info", user2)
57+
58+
{:ok, all_reports} = CMS.list_reports(:post, post.id, %{page: 1, size: 20})
59+
assert all_reports.total_count == 1
60+
61+
report = all_reports.entries |> List.first()
62+
assert report.report_cases |> length == 2
63+
assert Enum.any?(report.report_cases, &(&1.user.login == user.login))
64+
assert Enum.any?(report.report_cases, &(&1.user.login == user2.login))
65+
66+
{:ok, _report} = CMS.undo_report_article(:post, post.id, user)
67+
68+
{:ok, all_reports} = CMS.list_reports(:post, post.id, %{page: 1, size: 20})
69+
assert all_reports.total_count == 1
70+
71+
report = all_reports.entries |> List.first()
72+
assert report.report_cases |> length == 1
73+
assert Enum.any?(report.report_cases, &(&1.user.login == user2.login))
74+
end
75+
76+
@tag :wip2
4477
test "different user report a comment should have same report with different report cases",
45-
~m(user user2 post)a do
46-
{:ok, comment} = CMS.create_article_comment(:post, post.id, "commment", user)
47-
{:ok, _} = CMS.report_article_comment(comment.id, user)
48-
{:ok, _} = CMS.report_article_comment(comment.id, user2)
78+
~m(community user user2 post_attrs)a do
79+
{:ok, post} = CMS.create_content(community, :post, post_attrs, user)
80+
81+
{:ok, _report} = CMS.report_article(:post, post.id, "reason", "attr_info", user)
82+
{:ok, _report} = CMS.report_article(:post, post.id, "reason2", "attr_info 2", user2)
4983

50-
{:ok, all_reports} = CMS.list_reports(:article_comment, comment.id, %{page: 1, size: 20})
84+
{:ok, all_reports} = CMS.list_reports(:post, post.id, %{page: 1, size: 20})
5185

5286
report = List.first(all_reports.entries)
5387
report_cases = report.report_cases
@@ -60,11 +94,12 @@ defmodule GroupherServer.Test.CMS.AbuseReports.PostReport do
6094
assert List.last(report_cases).user.login == user2.login
6195
end
6296

63-
@tag :wip
64-
test "same user can not report a comment twice", ~m(user post)a do
65-
{:ok, comment} = CMS.create_article_comment(:post, post.id, "commment", user)
66-
{:ok, comment} = CMS.report_article_comment(comment.id, user)
67-
assert {:error, _} = CMS.report_article_comment(comment.id, user)
97+
@tag :wip2
98+
test "same user can not report a comment twice", ~m(community post_attrs user)a do
99+
{:ok, post} = CMS.create_content(community, :post, post_attrs, user)
100+
101+
{:ok, _report} = CMS.report_article(:post, post.id, "reason", "attr_info", user)
102+
assert {:error, _report} = CMS.report_article(:post, post.id, "reason", "attr_info", user)
68103
end
69104
end
70105
end

0 commit comments

Comments
 (0)