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

Commit 31708ef

Browse files
committed
refactor(abuse-report): add reported_user_ids
1 parent 7e55780 commit 31708ef

File tree

12 files changed

+129
-123
lines changed

12 files changed

+129
-123
lines changed

lib/groupher_server/accounts/embeds/user_meta.ex

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,16 @@ defmodule GroupherServer.Accounts.Embeds.UserMeta do
99
@optional_fields ~w(reported_count)a
1010

1111
@default_meta %{
12-
reported_count: 0
12+
reported_count: 0,
13+
reported_user_ids: []
1314
}
1415

1516
@doc "for test usage"
1617
def default_meta(), do: @default_meta
1718

1819
embedded_schema do
1920
field(:reported_count, :integer, default: 0)
21+
field(:reported_user_ids, {:array, :integer}, default: [])
2022
end
2123

2224
def changeset(struct, params) do

lib/groupher_server/cms/abuse_report.ex

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ defmodule GroupherServer.CMS.AbuseReport do
99
alias CMS.{ArticleComment, Embeds, Post, Job, Repo}
1010

1111
# @required_fields ~w(article_comment_id user_id recived_user_id)a
12-
@optional_fields ~w(article_comment_id post_id job_id repo_id account_id operate_user_id deal_with is_confirmed report_cases_count)a
13-
@update_fields ~w(operate_user_id deal_with is_confirmed report_cases_count)a
12+
@optional_fields ~w(article_comment_id post_id job_id repo_id account_id operate_user_id deal_with report_cases_count)a
13+
@update_fields ~w(operate_user_id deal_with report_cases_count)a
1414

1515
@type t :: %AbuseReport{}
1616
schema "abuse_reports" do
@@ -26,7 +26,6 @@ defmodule GroupherServer.CMS.AbuseReport do
2626
belongs_to(:operate_user, Accounts.User, foreign_key: :operate_user_id)
2727

2828
field(:deal_with, :string)
29-
field(:is_confirmed, :boolean, default: false)
3029

3130
timestamps(type: :utc_datetime)
3231
end

lib/groupher_server/cms/cms.ex

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -121,10 +121,6 @@ defmodule GroupherServer.CMS do
121121

122122
defdelegate list_folded_article_comments(thread, article_id, filters), to: ArticleComment
123123
defdelegate list_folded_article_comments(thread, article_id, filters, user), to: ArticleComment
124-
defdelegate list_reported_article_comments(thread, article_id, filters), to: ArticleComment
125-
126-
defdelegate list_reported_article_comments(thread, article_id, filters, user),
127-
to: ArticleComment
128124

129125
defdelegate list_comment_replies(comment_id, filters), to: ArticleComment
130126
defdelegate list_comment_replies(comment_id, filters, user), to: ArticleComment
@@ -162,7 +158,7 @@ defmodule GroupherServer.CMS do
162158
# TODO: move report to abuse report module
163159
defdelegate create_report(type, content_id, reason, attr, user), to: AbuseReport
164160
defdelegate report_article(thread, article_id, reason, attr, user), to: AbuseReport
165-
defdelegate report_article_comment(comment_id, reason, attr, user), to: ArticleCommentAction
161+
defdelegate report_article_comment(comment_id, reason, attr, user), to: AbuseReport
166162
defdelegate report_account(account_id, reason, attr, user), to: AbuseReport
167163
defdelegate undo_report_account(account_id, user), to: AbuseReport
168164
defdelegate undo_report_article(thread, article_id, user), to: AbuseReport

lib/groupher_server/cms/delegates/abuse_report.ex

Lines changed: 52 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ defmodule GroupherServer.CMS.Delegate.AbuseReport do
1717

1818
alias Ecto.Multi
1919

20+
@report_threshold_for_fold ArticleComment.report_threshold_for_fold()
21+
2022
# filter = %{
2123
# contentType: account | post | job | repo | article_comment | community
2224
# contentId: ...
@@ -115,7 +117,7 @@ defmodule GroupherServer.CMS.Delegate.AbuseReport do
115117
|> Multi.run(:create_abuse_report, fn _, _ ->
116118
create_report(:account_user, account.id, reason, attr, user)
117119
end)
118-
|> Multi.run(:update_report_flag, fn _, _ ->
120+
|> Multi.run(:update_report_meta, fn _, _ ->
119121
update_report_meta(info, account)
120122
end)
121123
|> Repo.transaction()
@@ -133,7 +135,7 @@ defmodule GroupherServer.CMS.Delegate.AbuseReport do
133135
|> Multi.run(:delete_abuse_report, fn _, _ ->
134136
delete_report(:account_user, account.id, user)
135137
end)
136-
|> Multi.run(:update_report_flag, fn _, _ ->
138+
|> Multi.run(:update_report_meta, fn _, _ ->
137139
update_report_meta(info, account)
138140
end)
139141
|> Repo.transaction()
@@ -151,7 +153,7 @@ defmodule GroupherServer.CMS.Delegate.AbuseReport do
151153
|> Multi.run(:create_abuse_report, fn _, _ ->
152154
create_report(thread, article.id, reason, attr, user)
153155
end)
154-
|> Multi.run(:update_report_flag, fn _, _ ->
156+
|> Multi.run(:update_report_meta, fn _, _ ->
155157
update_report_meta(info, article)
156158
end)
157159
|> Repo.transaction()
@@ -169,14 +171,35 @@ defmodule GroupherServer.CMS.Delegate.AbuseReport do
169171
|> Multi.run(:delete_abuse_report, fn _, _ ->
170172
delete_report(thread, article.id, user)
171173
end)
172-
|> Multi.run(:update_report_flag, fn _, _ ->
174+
|> Multi.run(:update_report_meta, fn _, _ ->
173175
update_report_meta(info, article)
174176
end)
175177
|> Repo.transaction()
176178
|> result()
177179
end
178180
end
179181

182+
@doc "report a comment"
183+
def report_article_comment(comment_id, reason, attr, %User{} = user) do
184+
with {:ok, comment} <- ORM.find(ArticleComment, comment_id) do
185+
Multi.new()
186+
|> Multi.run(:create_abuse_report, fn _, _ ->
187+
CMS.create_report(:article_comment, comment_id, reason, attr, user)
188+
end)
189+
|> Multi.run(:update_report_meta, fn _, _ ->
190+
{:ok, info} = match(:article_comment)
191+
update_report_meta(info, comment)
192+
end)
193+
|> Multi.run(:fold_comment_report_too_many, fn _, %{create_abuse_report: abuse_report} ->
194+
if abuse_report.report_cases_count >= @report_threshold_for_fold,
195+
do: CMS.fold_article_comment(comment, user),
196+
else: {:ok, comment}
197+
end)
198+
|> Repo.transaction()
199+
|> result()
200+
end
201+
end
202+
180203
def undo_report_article_comment(comment_id, %User{} = user) do
181204
undo_report_article(:article_comment, comment_id, user)
182205
end
@@ -190,7 +213,7 @@ defmodule GroupherServer.CMS.Delegate.AbuseReport do
190213
%{
191214
reason: reason,
192215
attr: attr,
193-
user: %{login: user.login, nickname: user.nickname}
216+
user: %{user_id: user.id, login: user.login, nickname: user.nickname}
194217
}
195218
]
196219

@@ -201,7 +224,7 @@ defmodule GroupherServer.CMS.Delegate.AbuseReport do
201224
AbuseReport |> ORM.create(args)
202225

203226
_ ->
204-
user = %{login: user.login, nickname: user.nickname}
227+
user = %{user_id: user.id, login: user.login, nickname: user.nickname}
205228

206229
report_cases =
207230
report.report_cases
@@ -237,29 +260,29 @@ defmodule GroupherServer.CMS.Delegate.AbuseReport do
237260
end
238261
end
239262

240-
# update reported_count in mete for article or comment
263+
# update reported_count in mete for article | comment | account
241264
defp update_report_meta(info, content) do
242-
case ORM.find_by(AbuseReport, Map.put(%{}, info.foreign_key, content.id)) do
243-
{:ok, record} ->
244-
reported_count = record.report_cases |> length
245-
246-
safe_meta = if is_nil(content.meta), do: info.default_meta, else: content.meta
247-
meta = safe_meta |> Map.merge(%{reported_count: reported_count}) |> strip_struct
248-
249-
content
250-
|> Ecto.Changeset.change()
251-
|> Ecto.Changeset.put_embed(:meta, meta)
252-
|> Repo.update()
253-
254-
{:error, _} ->
255-
safe_meta = if is_nil(content.meta), do: info.default_meta, else: content.meta
256-
meta = safe_meta |> Map.merge(%{reported_count: 0}) |> strip_struct
257-
258-
content
259-
|> Ecto.Changeset.change()
260-
|> Ecto.Changeset.put_embed(:meta, meta)
261-
|> Repo.update()
262-
end
265+
meta =
266+
case ORM.find_by(AbuseReport, Map.put(%{}, info.foreign_key, content.id)) do
267+
{:ok, record} ->
268+
report_cases = record.report_cases
269+
reported_count = length(report_cases)
270+
safe_meta = if is_nil(content.meta), do: info.default_meta, else: content.meta
271+
reported_user_ids = report_cases |> Enum.map(& &1.user.user_id)
272+
273+
meta =
274+
safe_meta
275+
|> Map.merge(%{reported_count: reported_count, reported_user_ids: reported_user_ids})
276+
|> strip_struct
277+
278+
{:error, _} ->
279+
safe_meta = if is_nil(content.meta), do: info.default_meta, else: content.meta
280+
281+
meta =
282+
safe_meta |> Map.merge(%{reported_count: 0, reported_user_ids: []}) |> strip_struct
283+
end
284+
285+
content |> ORM.update_meta(meta)
263286
end
264287

265288
defp not_reported_before(info, content_id, %User{login: login}) do
@@ -352,7 +375,7 @@ defmodule GroupherServer.CMS.Delegate.AbuseReport do
352375
|> Map.merge(%{thread: article_thread})
353376
end
354377

355-
defp result({:ok, %{update_report_flag: result}}), do: result |> done()
378+
defp result({:ok, %{update_report_meta: result}}), do: result |> done()
356379
defp result({:ok, %{update_content_reported_flag: result}}), do: result |> done()
357380

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

lib/groupher_server/cms/delegates/article_comment_action.ex

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ defmodule GroupherServer.CMS.Delegate.ArticleCommentAction do
3030
alias Ecto.Multi
3131

3232
@max_parent_replies_count ArticleComment.max_parent_replies_count()
33-
@report_threshold_for_fold ArticleComment.report_threshold_for_fold()
3433
@pined_comment_limit ArticleComment.pined_comment_limit()
3534

3635
@spec pin_article_comment(Integer.t()) :: {:ok, ArticleComment.t()}
@@ -101,23 +100,6 @@ defmodule GroupherServer.CMS.Delegate.ArticleCommentAction do
101100
end
102101
end
103102

104-
@doc "report a comment"
105-
def report_article_comment(comment_id, reason, attr, %User{} = user) do
106-
with {:ok, comment} <- ORM.find(ArticleComment, comment_id) do
107-
Multi.new()
108-
|> Multi.run(:create_abuse_report, fn _, _ ->
109-
CMS.create_report(:article_comment, comment_id, reason, attr, user)
110-
end)
111-
|> Multi.run(:fold_comment_report_too_many, fn _, %{create_abuse_report: abuse_report} ->
112-
if abuse_report.report_cases_count >= @report_threshold_for_fold,
113-
do: fold_article_comment(comment, user),
114-
else: {:ok, comment}
115-
end)
116-
|> Repo.transaction()
117-
|> upsert_comment_result()
118-
end
119-
end
120-
121103
@doc "reply to exsiting comment"
122104
def reply_article_comment(comment_id, content, %User{} = user) do
123105
with {:ok, target_comment} <-

lib/groupher_server/cms/embeds/user.ex

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,14 @@ defmodule GroupherServer.CMS.Embeds.User do
77
import Ecto.Changeset
88

99
embedded_schema do
10+
field(:user_id, :integer)
1011
field(:login, :string)
1112
field(:nickname, :string)
1213
end
1314

1415
def changeset(struct, params) do
1516
struct
16-
|> cast(params, [:login, :nickname])
17+
|> cast(params, [:login, :nickname, :user_id])
1718
|> validate_required([:login, :nickname])
1819
end
1920
end

test/groupher_server/cms/abuse_reports/account_report_test.exs

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ defmodule GroupherServer.Test.CMS.AbuseReports.AccountReport do
3939

4040
@tag :wip3
4141
test "report an account should have a abuse report record", ~m(user user2)a do
42-
{:ok, report} = CMS.report_account(user.id, "reason", "attr_info", user2)
42+
{:ok, _report} = CMS.report_account(user.id, "reason", "attr_info", user2)
4343

4444
filter = %{content_type: :user, content_id: user.id, page: 1, size: 20}
4545
{:ok, all_reports} = CMS.list_reports(filter)
@@ -56,14 +56,35 @@ defmodule GroupherServer.Test.CMS.AbuseReports.AccountReport do
5656
assert user.meta.reported_count == 1
5757
end
5858

59-
@tag :wip3
59+
@tag :wip2
6060
test "can undo a report", ~m(user user2)a do
61-
{:ok, report} = CMS.report_account(user.id, "reason", "attr_info", user2)
61+
{:ok, _report} = CMS.report_account(user.id, "reason", "attr_info", user2)
62+
{:ok, user} = ORM.find(User, user.id)
63+
assert user2.id in user.meta.reported_user_ids
64+
6265
{:ok, _report} = CMS.undo_report_account(user.id, user2)
6366

6467
filter = %{content_type: :user, content_id: user.id, page: 1, size: 20}
6568
{:ok, all_reports} = CMS.list_reports(filter)
6669
assert all_reports.total_count == 0
70+
71+
{:ok, user} = ORM.find(User, user.id)
72+
assert user2.id not in user.meta.reported_user_ids
73+
end
74+
75+
@tag :wip2
76+
test "can undo a existed report", ~m(user user2 user3)a do
77+
{:ok, _report} = CMS.report_account(user.id, "reason", "attr_info", user2)
78+
{:ok, _report} = CMS.report_account(user.id, "reason", "attr_info", user3)
79+
{:ok, _report} = CMS.undo_report_account(user.id, user2)
80+
81+
filter = %{content_type: :user, content_id: user.id, page: 1, size: 20}
82+
{:ok, all_reports} = CMS.list_reports(filter)
83+
assert all_reports.total_count == 1
84+
85+
{:ok, user} = ORM.find(User, user.id)
86+
assert user2.id not in user.meta.reported_user_ids
87+
assert user3.id in user.meta.reported_user_ids
6788
end
6889

6990
@tag :wip3

test/groupher_server/cms/abuse_reports/job_report_test.exs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ defmodule GroupherServer.Test.CMS.AbuseReports.JobReport do
4949

5050
{:ok, job} = ORM.find(CMS.Job, job.id)
5151
assert job.meta.reported_count == 1
52+
assert user.id in job.meta.reported_user_ids
5253
end
5354

5455
@tag :wip3
@@ -60,6 +61,26 @@ defmodule GroupherServer.Test.CMS.AbuseReports.JobReport do
6061
filter = %{content_type: :job, content_id: job.id, page: 1, size: 20}
6162
{:ok, all_reports} = CMS.list_reports(filter)
6263
assert all_reports.total_count == 0
64+
65+
{:ok, job} = ORM.find(CMS.Job, job.id)
66+
assert user.id not in job.meta.reported_user_ids
67+
end
68+
69+
@tag :wip2
70+
test "can undo a existed report", ~m(community user user2 job_attrs)a do
71+
{:ok, job} = CMS.create_content(community, :job, job_attrs, user)
72+
{:ok, _report} = CMS.report_article(:job, job.id, "reason", "attr_info", user)
73+
{:ok, _report} = CMS.report_article(:job, job.id, "reason", "attr_info", user2)
74+
{:ok, _report} = CMS.undo_report_article(:job, job.id, user)
75+
76+
filter = %{content_type: :job, content_id: job.id, page: 1, size: 20}
77+
{:ok, all_reports} = CMS.list_reports(filter)
78+
assert all_reports.total_count == 1
79+
80+
{:ok, job} = ORM.find(CMS.Job, job.id)
81+
82+
assert user2.id in job.meta.reported_user_ids
83+
assert user.id not in job.meta.reported_user_ids
6384
end
6485

6586
@tag :wip3

test/groupher_server/cms/abuse_reports/post_report_test.exs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ defmodule GroupherServer.Test.CMS.AbuseReports.PostReport do
4949

5050
{:ok, post} = ORM.find(CMS.Post, post.id)
5151
assert post.meta.reported_count == 1
52+
assert user.id in post.meta.reported_user_ids
5253
end
5354

5455
@tag :wip3
@@ -60,6 +61,26 @@ defmodule GroupherServer.Test.CMS.AbuseReports.PostReport do
6061
filter = %{content_type: :post, content_id: post.id, page: 1, size: 20}
6162
{:ok, all_reports} = CMS.list_reports(filter)
6263
assert all_reports.total_count == 0
64+
65+
{:ok, post} = ORM.find(CMS.Post, post.id)
66+
assert user.id not in post.meta.reported_user_ids
67+
end
68+
69+
@tag :wip2
70+
test "can undo a existed report", ~m(community user user2 post_attrs)a do
71+
{:ok, post} = CMS.create_content(community, :post, post_attrs, user)
72+
{:ok, _report} = CMS.report_article(:post, post.id, "reason", "attr_info", user)
73+
{:ok, _report} = CMS.report_article(:post, post.id, "reason", "attr_info", user2)
74+
{:ok, _report} = CMS.undo_report_article(:post, post.id, user)
75+
76+
filter = %{content_type: :post, content_id: post.id, page: 1, size: 20}
77+
{:ok, all_reports} = CMS.list_reports(filter)
78+
assert all_reports.total_count == 1
79+
80+
{:ok, post} = ORM.find(CMS.Post, post.id)
81+
82+
assert user2.id in post.meta.reported_user_ids
83+
assert user.id not in post.meta.reported_user_ids
6384
end
6485

6586
@tag :wip3

0 commit comments

Comments
 (0)