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

Commit 6fdecc0

Browse files
committed
refactor(abuse-report): wip
1 parent ab14775 commit 6fdecc0

File tree

9 files changed

+150
-68
lines changed

9 files changed

+150
-68
lines changed

lib/groupher_server/cms/article_comment.ex

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,15 @@ defmodule GroupherServer.CMS.ArticleComment do
1212
alias CMS.{
1313
Post,
1414
Job,
15+
Repo,
1516
Embeds,
1617
ArticleCommentUpvote
1718
}
1819

1920
# alias Helper.HTML
2021

2122
@required_fields ~w(body_html author_id)a
22-
@optional_fields ~w(post_id job_id reply_to_id replies_count is_folded is_reported is_deleted floor is_article_author)a
23+
@optional_fields ~w(post_id job_id repo_id reply_to_id replies_count is_folded is_reported is_deleted floor is_article_author)a
2324
@updatable_fields ~w(is_folded is_reported is_deleted floor upvotes_count is_pinned)a
2425

2526
@max_participator_count 5
@@ -72,6 +73,8 @@ defmodule GroupherServer.CMS.ArticleComment do
7273

7374
belongs_to(:post, Post, foreign_key: :post_id)
7475
belongs_to(:job, Job, foreign_key: :job_id)
76+
belongs_to(:repo, Repo, foreign_key: :repo_id)
77+
7578
belongs_to(:reply_to, ArticleComment, foreign_key: :reply_to_id)
7679

7780
embeds_many(:replies, ArticleComment, on_replace: :delete)

lib/groupher_server/cms/cms.ex

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,6 @@ defmodule GroupherServer.CMS do
163163
defdelegate create_report(type, content_id, reason, attr, user), to: AbuseReport
164164
defdelegate report_article(thread, article_id, reason, attr, user), to: AbuseReport
165165
defdelegate undo_report_article(thread, article_id, user), to: AbuseReport
166-
defdelegate list_reports(type, content_id, filter), to: AbuseReport
167166
defdelegate list_reports(filter), to: AbuseReport
168167
defdelegate report_article_comment(comment_id, reason, attr, user), to: ArticleCommentAction
169168
defdelegate undo_report_article_comment(comment_id, user), to: AbuseReport

lib/groupher_server/cms/delegates/abuse_report.ex

Lines changed: 76 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,12 @@ defmodule GroupherServer.CMS.Delegate.AbuseReport do
1313
alias GroupherServer.{Accounts, CMS, Repo}
1414

1515
alias Accounts.User
16-
alias CMS.{AbuseReport, Embeds}
16+
alias CMS.{AbuseReport, ArticleComment, Embeds}
1717

1818
alias Ecto.Multi
1919

2020
# filter = %{
21-
# contentType: account | post | job | repo | article_comment
21+
# contentType: account | post | job | repo | article_comment | community
2222
# contentId: ...
2323
# operate_user_id,
2424
# min_case_count,
@@ -28,9 +28,6 @@ defmodule GroupherServer.CMS.Delegate.AbuseReport do
2828
# size
2929
# }
3030

31-
# def list_all_reports(thread, filter)
32-
# def list_all_reports(community, filter)
33-
# def list_all_reports(filter)
3431
@article_threads [:post, :job, :repo]
3532
@export_report_keys [
3633
:id,
@@ -43,6 +40,34 @@ defmodule GroupherServer.CMS.Delegate.AbuseReport do
4340
:updated_at
4441
]
4542

43+
@export_author_keys [:id, :login, :nickname, :avatar]
44+
@export_article_keys [:id, :title, :digest, :upvotes_count, :views]
45+
46+
@doc """
47+
list paged reports for article comemnts
48+
"""
49+
def list_reports(%{content_type: :article_comment, content_id: content_id} = filter) do
50+
%{page: page, size: size} = filter
51+
52+
with {:ok, info} <- match(:article_comment) do
53+
query =
54+
from(r in AbuseReport,
55+
where: field(r, ^info.foreign_key) == ^content_id,
56+
preload: [article_comment: ^@article_threads],
57+
preload: [article_comment: :author]
58+
)
59+
60+
query
61+
|> QueryBuilder.filter_pack(filter)
62+
|> ORM.paginater(~m(page size)a)
63+
|> reports_formater(:article_comment)
64+
|> done()
65+
end
66+
end
67+
68+
@doc """
69+
list paged reports for article
70+
"""
4671
def list_reports(%{content_type: thread, content_id: content_id} = filter)
4772
when thread in @article_threads do
4873
%{page: page, size: size} = filter
@@ -62,6 +87,17 @@ defmodule GroupherServer.CMS.Delegate.AbuseReport do
6287
end
6388
end
6489

90+
defp reports_formater(%{entries: entries} = paged_reports, :article_comment) do
91+
paged_reports
92+
|> Map.put(
93+
:entries,
94+
Enum.map(entries, fn report ->
95+
basic_report = report |> Map.take(@export_report_keys)
96+
basic_report |> Map.put(:article_comment, extract_article_comment_info(report))
97+
end)
98+
)
99+
end
100+
65101
defp reports_formater(%{entries: entries} = paged_reports, thread)
66102
when thread in @article_threads do
67103
paged_reports
@@ -74,36 +110,18 @@ defmodule GroupherServer.CMS.Delegate.AbuseReport do
74110
)
75111
end
76112

113+
# TODO: original community and communities info
77114
defp extract_article_info(thread, %AbuseReport{} = report) do
78115
article = Map.get(report, thread)
79116

80-
%{
81-
thread: thread,
82-
id: article.id,
83-
title: article.title,
84-
digest: article.digest,
85-
upvotes_count: article.upvotes_count,
86-
views: article.views
87-
}
117+
article
118+
|> Map.take(@export_article_keys)
119+
|> Map.merge(%{thread: thread})
88120
end
89121

90-
@doc """
91-
list paged reports for both comment and article
92-
"""
93-
def list_reports(thread, content_id, %{page: page, size: size} = filter) do
94-
with {:ok, info} <- match(thread) do
95-
query =
96-
from(r in AbuseReport,
97-
where: field(r, ^info.foreign_key) == ^content_id,
98-
preload: [^thread, :operate_user]
99-
)
100-
101-
query
102-
|> QueryBuilder.filter_pack(filter)
103-
|> ORM.paginater(~m(page size)a)
104-
|> done()
105-
end
106-
end
122+
# def report_account(user_id) do
123+
# # TODO*
124+
# end
107125

108126
@doc """
109127
report article content
@@ -123,10 +141,6 @@ defmodule GroupherServer.CMS.Delegate.AbuseReport do
123141
end
124142
end
125143

126-
def undo_report_article_comment(comment_id, %User{} = user) do
127-
undo_report_article(:article_comment, comment_id, user)
128-
end
129-
130144
@doc """
131145
undo report article content
132146
"""
@@ -145,6 +159,10 @@ defmodule GroupherServer.CMS.Delegate.AbuseReport do
145159
end
146160
end
147161

162+
def undo_report_article_comment(comment_id, %User{} = user) do
163+
undo_report_article(:article_comment, comment_id, user)
164+
end
165+
148166
def create_report(type, content_id, reason, attr, %User{} = user) do
149167
with {:ok, info} <- match(type),
150168
{:ok, report} <- not_reported_before(info, content_id, user) do
@@ -243,6 +261,30 @@ defmodule GroupherServer.CMS.Delegate.AbuseReport do
243261
end
244262
end
245263

264+
def extract_article_comment_info(%AbuseReport{} = report) do
265+
keys = [:id, :upvotes_count, :body_html]
266+
author = Map.take(report.article_comment.author, @export_author_keys)
267+
268+
comment = Map.take(report.article_comment, keys)
269+
comment = Map.merge(comment, %{author: author})
270+
271+
article = extract_article_in_comment(report.article_comment)
272+
Map.merge(comment, %{article: article})
273+
end
274+
275+
defp extract_article_in_comment(%ArticleComment{} = article_comment) do
276+
article_thread =
277+
Enum.filter(@article_threads, fn thread ->
278+
not is_nil(Map.get(article_comment, :"#{thread}_id"))
279+
end)
280+
|> List.first()
281+
282+
article_comment
283+
|> Map.get(article_thread)
284+
|> Map.take(@export_article_keys)
285+
|> Map.merge(%{thread: article_thread})
286+
end
287+
246288
defp result({:ok, %{update_report_flag: result}}), do: result |> done()
247289

248290
defp result({:error, _, result, _steps}) do
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
defmodule GroupherServer.Repo.Migrations.AddRepoToArticleComment do
2+
use Ecto.Migration
3+
4+
def change do
5+
alter table(:articles_comments) do
6+
add(:repo_id, references(:cms_repos, on_delete: :delete_all))
7+
end
8+
9+
create(index(:articles_comments, [:repo_id]))
10+
end
11+
end

test/groupher_server/cms/abuse_reports/comment_report_test.exs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,29 +15,32 @@ defmodule GroupherServer.Test.CMS.AbuseReports.CommentReport do
1515
end
1616

1717
describe "[article comment report/unreport]" do
18-
@tag :wip3
18+
@tag :wip2
1919
test "report a comment should have a abuse report record", ~m(user post)a do
2020
{:ok, comment} = CMS.create_article_comment(:post, post.id, "commment", user)
2121
{:ok, _comment} = CMS.report_article_comment(comment.id, "reason", "attr", user)
2222

23-
{:ok, all_reports} = CMS.list_reports(:article_comment, comment.id, %{page: 1, size: 20})
23+
filter = %{content_type: :article_comment, content_id: comment.id, page: 1, size: 20}
24+
{:ok, all_reports} = CMS.list_reports(filter)
2425

2526
report = List.first(all_reports.entries)
2627
report_cases = report.report_cases
28+
assert report.article_comment.id == comment.id
2729

2830
assert all_reports.total_count == 1
2931
assert report.report_cases_count == 1
3032
assert List.first(report_cases).user.login == user.login
3133
end
3234

33-
@tag :wip3
35+
@tag :wip2
3436
test "different user report a comment should have same report with different report cases",
3537
~m(user user2 post)a do
3638
{:ok, comment} = CMS.create_article_comment(:post, post.id, "commment", user)
3739
{:ok, _} = CMS.report_article_comment(comment.id, "reason", "attr", user)
3840
{:ok, _} = CMS.report_article_comment(comment.id, "reason", "attr", user2)
3941

40-
{:ok, all_reports} = CMS.list_reports(:article_comment, comment.id, %{page: 1, size: 20})
42+
filter = %{content_type: :article_comment, content_id: comment.id, page: 1, size: 20}
43+
{:ok, all_reports} = CMS.list_reports(filter)
4144

4245
report = List.first(all_reports.entries)
4346
report_cases = report.report_cases

test/groupher_server/cms/abuse_reports/job_report_test.exs

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,32 @@ defmodule GroupherServer.Test.CMS.AbuseReports.JobReport do
1717
end
1818

1919
describe "[article job report/unreport]" do
20+
@tag :wip3
21+
test "list article reports should work", ~m(community user user2 job_attrs)a do
22+
{:ok, job} = CMS.create_content(community, :job, job_attrs, user)
23+
{:ok, _report} = CMS.report_article(:job, job.id, "reason", "attr_info", user)
24+
{:ok, _report} = CMS.report_article(:job, job.id, "reason", "attr_info", user2)
25+
26+
filter = %{content_type: :job, content_id: job.id, page: 1, size: 20}
27+
{:ok, all_reports} = CMS.list_reports(filter)
28+
29+
report = all_reports.entries |> List.first()
30+
assert report.article.id == job.id
31+
assert report.article.thread == :job
32+
end
33+
2034
@tag :wip3
2135
test "report a job should have a abuse report record", ~m(community user job_attrs)a do
2236
{:ok, job} = CMS.create_content(community, :job, job_attrs, user)
2337
{:ok, _report} = CMS.report_article(:job, job.id, "reason", "attr_info", user)
2438

25-
{:ok, all_reports} = CMS.list_reports(:job, job.id, %{page: 1, size: 20})
39+
filter = %{content_type: :job, content_id: job.id, page: 1, size: 20}
40+
{:ok, all_reports} = CMS.list_reports(filter)
2641

2742
report = List.first(all_reports.entries)
2843
report_cases = report.report_cases
2944

30-
assert report.job_id == job.id
45+
assert report.article.id == job.id
3146
assert all_reports.total_count == 1
3247
assert report.report_cases_count == 1
3348
assert List.first(report_cases).user.login == user.login
@@ -43,7 +58,8 @@ defmodule GroupherServer.Test.CMS.AbuseReports.JobReport do
4358
{:ok, _report} = CMS.report_article(:job, job.id, "reason", "attr_info", user)
4459
{:ok, _report} = CMS.undo_report_article(:job, job.id, user)
4560

46-
{:ok, all_reports} = CMS.list_reports(:job, job.id, %{page: 1, size: 20})
61+
filter = %{content_type: :job, content_id: job.id, page: 1, size: 20}
62+
{:ok, all_reports} = CMS.list_reports(filter)
4763
assert all_reports.total_count == 0
4864
end
4965

@@ -54,7 +70,8 @@ defmodule GroupherServer.Test.CMS.AbuseReports.JobReport do
5470
{:ok, _report} = CMS.report_article(:job, job.id, "reason", "attr_info", user)
5571
{:ok, _report} = CMS.report_article(:job, job.id, "reason", "attr_info", user2)
5672

57-
{:ok, all_reports} = CMS.list_reports(:job, job.id, %{page: 1, size: 20})
73+
filter = %{content_type: :job, content_id: job.id, page: 1, size: 20}
74+
{:ok, all_reports} = CMS.list_reports(filter)
5875
assert all_reports.total_count == 1
5976

6077
report = all_reports.entries |> List.first()
@@ -64,7 +81,8 @@ defmodule GroupherServer.Test.CMS.AbuseReports.JobReport do
6481

6582
{:ok, _report} = CMS.undo_report_article(:job, job.id, user)
6683

67-
{:ok, all_reports} = CMS.list_reports(:job, job.id, %{page: 1, size: 20})
84+
filter = %{content_type: :job, content_id: job.id, page: 1, size: 20}
85+
{:ok, all_reports} = CMS.list_reports(filter)
6886
assert all_reports.total_count == 1
6987

7088
report = all_reports.entries |> List.first()
@@ -80,7 +98,8 @@ defmodule GroupherServer.Test.CMS.AbuseReports.JobReport do
8098
{:ok, _report} = CMS.report_article(:job, job.id, "reason", "attr_info", user)
8199
{:ok, _report} = CMS.report_article(:job, job.id, "reason2", "attr_info 2", user2)
82100

83-
{:ok, all_reports} = CMS.list_reports(:job, job.id, %{page: 1, size: 20})
101+
filter = %{content_type: :job, content_id: job.id, page: 1, size: 20}
102+
{:ok, all_reports} = CMS.list_reports(filter)
84103

85104
report = List.first(all_reports.entries)
86105
report_cases = report.report_cases

0 commit comments

Comments
 (0)