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

Commit d7831aa

Browse files
committed
refactor(abuse-report): wip
1 parent b35d121 commit d7831aa

File tree

19 files changed

+227
-111
lines changed

19 files changed

+227
-111
lines changed

lib/groupher_server/cms/delegates/abuse_report.ex

Lines changed: 51 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,6 @@ defmodule GroupherServer.CMS.Delegate.AbuseReport do
1919

2020
@report_threshold_for_fold ArticleComment.report_threshold_for_fold()
2121

22-
# filter = %{
23-
# contentType: account | post | job | repo | article_comment | community
24-
# contentId: ...
25-
# operate_user_id,
26-
# min_case_count,
27-
# max_case_count,
28-
# page
29-
# size
30-
# }
31-
3222
@article_threads [:post, :job, :repo]
3323
@export_author_keys [:id, :login, :nickname, :avatar]
3424
@export_article_keys [:id, :title, :digest, :upvotes_count, :views]
@@ -45,30 +35,22 @@ defmodule GroupherServer.CMS.Delegate.AbuseReport do
4535
@doc """
4636
list paged reports for article comemnts
4737
"""
48-
def list_reports(%{content_type: :user, content_id: content_id} = filter) do
49-
%{page: page, size: size} = filter
50-
51-
with {:ok, info} <- match(:account_user) do
38+
def list_reports(%{content_type: :account, content_id: content_id} = filter) do
39+
with {:ok, info} <- match(:account) do
5240
query =
5341
from(r in AbuseReport,
5442
where: field(r, ^info.foreign_key) == ^content_id,
5543
preload: :account
5644
)
5745

58-
query
59-
|> QueryBuilder.filter_pack(filter)
60-
|> ORM.paginater(~m(page size)a)
61-
|> reports_formater(:account_user)
62-
|> done()
46+
do_list_reports(query, :account, filter)
6347
end
6448
end
6549

6650
@doc """
6751
list paged reports for article comemnts
6852
"""
6953
def list_reports(%{content_type: :article_comment, content_id: content_id} = filter) do
70-
%{page: page, size: size} = filter
71-
7254
with {:ok, info} <- match(:article_comment) do
7355
query =
7456
from(r in AbuseReport,
@@ -77,11 +59,7 @@ defmodule GroupherServer.CMS.Delegate.AbuseReport do
7759
preload: [article_comment: :author]
7860
)
7961

80-
query
81-
|> QueryBuilder.filter_pack(filter)
82-
|> ORM.paginater(~m(page size)a)
83-
|> reports_formater(:article_comment)
84-
|> done()
62+
do_list_reports(query, :article_comment, filter)
8563
end
8664
end
8765

@@ -90,32 +68,62 @@ defmodule GroupherServer.CMS.Delegate.AbuseReport do
9068
"""
9169
def list_reports(%{content_type: thread, content_id: content_id} = filter)
9270
when thread in @article_threads do
93-
%{page: page, size: size} = filter
94-
9571
with {:ok, info} <- match(thread) do
9672
query =
9773
from(r in AbuseReport,
9874
where: field(r, ^info.foreign_key) == ^content_id,
9975
preload: [^thread, :operate_user]
10076
)
10177

102-
query
103-
|> QueryBuilder.filter_pack(filter)
104-
|> ORM.paginater(~m(page size)a)
105-
|> reports_formater(thread)
106-
|> done()
78+
do_list_reports(query, thread, filter)
79+
end
80+
end
81+
82+
# def list_reports(%{content_type: thread} = filter) when thread in @article_threads do
83+
def list_reports(%{content_type: thread} = filter) do
84+
IO.inspect(filter, label: "list filter 1-")
85+
86+
with {:ok, info} <- match(thread) do
87+
query =
88+
from(r in AbuseReport,
89+
where: not is_nil(field(r, ^info.foreign_key)),
90+
preload: [^thread, :operate_user],
91+
preload: [article_comment: :author]
92+
)
93+
94+
do_list_reports(query, thread, filter)
10795
end
10896
end
10997

98+
def list_reports(filter) do
99+
query = from(r in AbuseReport, preload: [:operate_user])
100+
101+
do_list_reports(query, filter)
102+
end
103+
104+
defp do_list_reports(query, thread, filter) do
105+
%{page: page, size: size} = filter
106+
107+
query
108+
|> QueryBuilder.filter_pack(filter)
109+
|> ORM.paginater(~m(page size)a)
110+
|> reports_formater(thread)
111+
|> done()
112+
end
113+
114+
defp do_list_reports(query, %{page: page, size: size}) do
115+
query |> ORM.paginater(~m(page size)a) |> done()
116+
end
117+
110118
@doc """
111119
report an account
112120
"""
113121
def report_account(account_id, reason, attr, user) do
114-
with {:ok, info} <- match(:account_user),
122+
with {:ok, info} <- match(:account),
115123
{:ok, account} <- ORM.find(info.model, account_id) do
116124
Multi.new()
117125
|> Multi.run(:create_abuse_report, fn _, _ ->
118-
create_report(:account_user, account.id, reason, attr, user)
126+
create_report(:account, account.id, reason, attr, user)
119127
end)
120128
|> Multi.run(:update_report_meta, fn _, _ ->
121129
update_report_meta(info, account)
@@ -129,11 +137,11 @@ defmodule GroupherServer.CMS.Delegate.AbuseReport do
129137
undo report article content
130138
"""
131139
def undo_report_account(account_id, %User{} = user) do
132-
with {:ok, info} <- match(:account_user),
140+
with {:ok, info} <- match(:account),
133141
{:ok, account} <- ORM.find(info.model, account_id) do
134142
Multi.new()
135143
|> Multi.run(:delete_abuse_report, fn _, _ ->
136-
delete_report(:account_user, account.id, user)
144+
delete_report(:account, account.id, user)
137145
end)
138146
|> Multi.run(:update_report_meta, fn _, _ ->
139147
update_report_meta(info, account)
@@ -270,16 +278,14 @@ defmodule GroupherServer.CMS.Delegate.AbuseReport do
270278
safe_meta = if is_nil(content.meta), do: info.default_meta, else: content.meta
271279
reported_user_ids = report_cases |> Enum.map(& &1.user.user_id)
272280

273-
meta =
274-
safe_meta
275-
|> Map.merge(%{reported_count: reported_count, reported_user_ids: reported_user_ids})
276-
|> strip_struct
281+
safe_meta
282+
|> Map.merge(%{reported_count: reported_count, reported_user_ids: reported_user_ids})
283+
|> strip_struct
277284

278285
{:error, _} ->
279286
safe_meta = if is_nil(content.meta), do: info.default_meta, else: content.meta
280287

281-
meta =
282-
safe_meta |> Map.merge(%{reported_count: 0, reported_user_ids: []}) |> strip_struct
288+
safe_meta |> Map.merge(%{reported_count: 0, reported_user_ids: []}) |> strip_struct
283289
end
284290

285291
content |> ORM.update_meta(meta)
@@ -305,7 +311,7 @@ defmodule GroupherServer.CMS.Delegate.AbuseReport do
305311
end
306312
end
307313

308-
defp reports_formater(%{entries: entries} = paged_reports, :account_user) do
314+
defp reports_formater(%{entries: entries} = paged_reports, :account) do
309315
paged_reports
310316
|> Map.put(
311317
:entries,
@@ -340,7 +346,7 @@ defmodule GroupherServer.CMS.Delegate.AbuseReport do
340346
end
341347

342348
defp extract_account_info(%AbuseReport{} = report) do
343-
account = report |> Map.get(:account) |> Map.take(@export_author_keys)
349+
report |> Map.get(:account) |> Map.take(@export_author_keys)
344350
end
345351

346352
# TODO: original community and communities info

lib/groupher_server/cms/helper/matcher2.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ defmodule GroupherServer.CMS.Helper.Matcher2 do
3232
{:error, "not supported"}
3333
end
3434

35-
def match(:account_user) do
35+
def match(:account) do
3636
{:ok,
3737
%{
3838
model: User,

lib/groupher_server_web/resolvers/cms_resolver.ex

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,10 @@ defmodule GroupherServerWeb.Resolvers.CMS do
6262
CMS.paged_articles(thread, filter)
6363
end
6464

65+
def list_reports(_root, ~m(filter)a, _) do
66+
CMS.list_reports(filter)
67+
end
68+
6569
def wiki(_root, ~m(community)a, _info), do: CMS.get_wiki(%Community{raw: community})
6670
def cheatsheet(_root, ~m(community)a, _info), do: CMS.get_cheatsheet(%Community{raw: community})
6771

lib/groupher_server_web/schema/cms/cms_misc.ex

Lines changed: 16 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -40,32 +40,10 @@ defmodule GroupherServerWeb.Schema.CMS.Misc do
4040
# value(:watch)
4141
end
4242

43-
enum :react_thread do
44-
value(:post)
45-
value(:job)
46-
value(:repo)
47-
# home community
48-
value(:tech)
49-
value(:city)
50-
value(:share)
51-
value(:radar)
52-
end
53-
5443
enum :cms_comment do
5544
value(:post_comment)
5645
end
5746

58-
enum :commentable_thread do
59-
value(:post)
60-
value(:job)
61-
value(:repo)
62-
# home community
63-
value(:tech)
64-
value(:city)
65-
value(:share)
66-
value(:radar)
67-
end
68-
6947
enum :thread do
7048
value(:post)
7149
value(:job)
@@ -83,11 +61,6 @@ defmodule GroupherServerWeb.Schema.CMS.Misc do
8361
value(:company)
8462
end
8563

86-
enum :order_enum do
87-
value(:asc)
88-
value(:desc)
89-
end
90-
9164
enum :when_enum do
9265
value(:today)
9366
value(:this_week)
@@ -297,11 +270,25 @@ defmodule GroupherServerWeb.Schema.CMS.Misc do
297270
field(:color, :string)
298271
end
299272

300-
enum :search_part do
301-
value(:community)
273+
enum :report_content_type do
302274
value(:post)
303275
value(:job)
304276
value(:repo)
277+
value(:account)
278+
value(:article_comment)
279+
# value(:community)
280+
end
281+
282+
@desc """
283+
abuse report filter
284+
"""
285+
input_object :report_filter do
286+
field(:content_type, :report_content_type)
287+
field(:content_id, :id)
288+
pagination_args()
289+
# operate_user_id,
290+
# min_case_count,
291+
# max_case_count,
305292
end
306293

307294
@doc """

lib/groupher_server_web/schema/cms/cms_queries.ex

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,9 +79,6 @@ defmodule GroupherServerWeb.Schema.CMS.Queries do
7979
resolve(&R.CMS.cheatsheet/3)
8080
end
8181

82-
article_reacted_users_query(:upvot, &R.CMS.upvoted_users/3)
83-
article_reacted_users_query(:collect, &R.CMS.collected_users/3)
84-
8582
# get all tags
8683
@desc "get paged tags"
8784
field :paged_tags, :paged_tags do
@@ -204,6 +201,16 @@ defmodule GroupherServerWeb.Schema.CMS.Queries do
204201
resolve(&R.CMS.search_items/3)
205202
end
206203

204+
@desc "paged reports list"
205+
field :paged_abuse_reports, :paged_reports do
206+
arg(:filter, non_null(:report_filter))
207+
208+
resolve(&R.CMS.list_reports/3)
209+
end
210+
211+
article_reacted_users_query(:upvot, &R.CMS.upvoted_users/3)
212+
article_reacted_users_query(:collect, &R.CMS.collected_users/3)
213+
207214
article_queries(:post)
208215
article_queries(:job)
209216
article_queries(:repo)

lib/groupher_server_web/schema/cms/cms_types.ex

Lines changed: 46 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,33 @@ defmodule GroupherServerWeb.Schema.CMS.Types do
1313

1414
import_types(Schema.CMS.Misc)
1515

16+
######
17+
# common stands for minimal info of the type
18+
# usually used in abuse_report, feeds, etc ..
19+
object :common_user do
20+
field(:login, :string)
21+
field(:nickname, :string)
22+
field(:avatar, :string)
23+
end
24+
25+
object :common_article do
26+
field(:thread, :string)
27+
field(:id, :id)
28+
# field(:body_html, :string)
29+
field(:title, :string)
30+
field(:author, :user, resolve: dataloader(CMS, :author))
31+
end
32+
33+
object :common_article_comment do
34+
field(:id, :id)
35+
field(:body_html, :string)
36+
field(:upvotes_count, :integer)
37+
field(:author, :common_user)
38+
field(:article, :common_article)
39+
end
40+
41+
######
42+
1643
object :idlike do
1744
field(:id, :id)
1845
end
@@ -369,16 +396,25 @@ defmodule GroupherServerWeb.Schema.CMS.Types do
369396
timestamp_fields()
370397
end
371398

372-
object :comment do
373-
comments_fields()
399+
object :abuse_report do
400+
field(:id, :id)
401+
# field(:article_comment, :article_comment, resolve: dataloader(CMS, :article_comment))
402+
field(:article_comment, :common_article_comment)
403+
# field(:account, :user)
404+
field(:report_cases_count, :integer)
405+
field(:deal_with, :string)
406+
field(:operate_user, :user)
407+
408+
timestamp_fields()
374409
end
375410

376-
object :common_article do
377-
field(:thread, :string)
378-
field(:id, :id)
379-
# field(:body_html, :string)
380-
field(:title, :string)
381-
field(:author, :user, resolve: dataloader(CMS, :author))
411+
object :paged_reports do
412+
field(:entries, list_of(:abuse_report))
413+
pagination_fields()
414+
end
415+
416+
object :comment do
417+
comments_fields()
382418
end
383419

384420
object :post_comment do
@@ -457,12 +493,7 @@ defmodule GroupherServerWeb.Schema.CMS.Types do
457493
field(:is_edited, :boolean)
458494
field(:is_comment_locked, :boolean)
459495
# field(:linked_posts_count, :integer)
460-
# field(:linked_jobs_count, :integer)
461-
# field(:linked_works_count, :integer)
462-
463-
# reaction: %{
464-
# rocketCount: 0,
465-
# heartCount: 0,
466-
# }
467496
end
497+
498+
####### reports
468499
end

0 commit comments

Comments
 (0)