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

Commit e7c5876

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

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

55 files changed

+469
-218
lines changed

lib/groupher_server/accounts/embeds/collect_folder_meta.ex

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
defmodule GroupherServer.CMS.Embeds.CollectFolderMeta.Macros do
1+
defmodule GroupherServer.Accounts.Embeds.CollectFolderMeta.Macros do
22
@moduledoc """
33
general fields for each folder meta
44
@@ -31,7 +31,7 @@ defmodule GroupherServer.Accounts.Embeds.CollectFolderMeta do
3131
"""
3232
use Ecto.Schema
3333
import Ecto.Changeset
34-
import GroupherServer.CMS.Embeds.CollectFolderMeta.Macros
34+
import GroupherServer.Accounts.Embeds.CollectFolderMeta.Macros
3535

3636
alias GroupherServer.Accounts.CollectFolder
3737

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
defmodule GroupherServer.Accounts.Embeds.UserMeta do
2+
@moduledoc """
3+
general article meta info for article-like content, like post, job, works ...
4+
"""
5+
use Ecto.Schema
6+
use Accessible
7+
import Ecto.Changeset
8+
9+
@optional_fields ~w(reported_count)a
10+
11+
@default_meta %{
12+
reported_count: 0
13+
}
14+
15+
@doc "for test usage"
16+
def default_meta(), do: @default_meta
17+
18+
embedded_schema do
19+
field(:reported_count, :integer, default: 0)
20+
end
21+
22+
def changeset(struct, params) do
23+
struct
24+
|> cast(params, @optional_fields)
25+
end
26+
end

lib/groupher_server/accounts/user.ex

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ defmodule GroupherServer.Accounts.User do
99

1010
alias GroupherServer.Accounts.{
1111
Achievement,
12+
Embeds,
1213
Customization,
1314
EducationBackground,
1415
CollectFolder,
@@ -60,6 +61,9 @@ defmodule GroupherServer.Accounts.User do
6061
# field(:paid_member, :boolean)
6162
# field(:platinum_member, :boolean)
6263

64+
field(:is_reported, :boolean, default: false)
65+
embeds_one(:meta, Embeds.UserMeta, on_replace: :update)
66+
6367
has_one(:customization, Customization)
6468
has_one(:purchase, Purchase)
6569

@@ -70,6 +74,7 @@ defmodule GroupherServer.Accounts.User do
7074
def changeset(%User{} = user, attrs) do
7175
user
7276
|> update_changeset(attrs)
77+
|> cast_embed(:meta, required: false, with: &Embeds.UserMeta.changeset/2)
7378
|> validate_required(@required_fields)
7479

7580
# |> unique_constraint(:username)
@@ -80,6 +85,7 @@ defmodule GroupherServer.Accounts.User do
8085
|> cast(attrs, @optional_fields ++ @required_fields)
8186
|> cast_embed(:education_backgrounds, with: &EducationBackground.changeset/2)
8287
|> cast_embed(:work_backgrounds, with: &WorkBackground.changeset/2)
88+
|> cast_embed(:meta, required: false, with: &Embeds.UserMeta.changeset/2)
8389
|> validate_length(:nickname, min: 3, max: 30)
8490
|> validate_length(:bio, min: 3, max: 100)
8591
|> validate_inclusion(:sex, ["dude", "girl"])

lib/groupher_server/cms/cms.ex

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,9 +162,11 @@ defmodule GroupherServer.CMS do
162162
# TODO: move report to abuse report module
163163
defdelegate create_report(type, content_id, reason, attr, user), to: AbuseReport
164164
defdelegate report_article(thread, article_id, reason, attr, user), to: AbuseReport
165+
defdelegate report_article_comment(comment_id, reason, attr, user), to: ArticleCommentAction
166+
defdelegate report_account(account_id, reason, attr, user), to: AbuseReport
167+
defdelegate undo_report_account(account_id, user), to: AbuseReport
165168
defdelegate undo_report_article(thread, article_id, user), to: AbuseReport
166169
defdelegate list_reports(filter), to: AbuseReport
167-
defdelegate report_article_comment(comment_id, reason, attr, user), to: ArticleCommentAction
168170
defdelegate undo_report_article_comment(comment_id, user), to: AbuseReport
169171

170172
# Passport CURD

lib/groupher_server/cms/delegates/abuse_report.ex

Lines changed: 108 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ defmodule GroupherServer.CMS.Delegate.AbuseReport do
2929
# }
3030

3131
@article_threads [:post, :job, :repo]
32+
@export_author_keys [:id, :login, :nickname, :avatar]
33+
@export_article_keys [:id, :title, :digest, :upvotes_count, :views]
3234
@export_report_keys [
3335
:id,
3436
:deal_with,
@@ -40,8 +42,26 @@ defmodule GroupherServer.CMS.Delegate.AbuseReport do
4042
:updated_at
4143
]
4244

43-
@export_author_keys [:id, :login, :nickname, :avatar]
44-
@export_article_keys [:id, :title, :digest, :upvotes_count, :views]
45+
@doc """
46+
list paged reports for article comemnts
47+
"""
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
52+
query =
53+
from(r in AbuseReport,
54+
where: field(r, ^info.foreign_key) == ^content_id,
55+
preload: :account
56+
)
57+
58+
query
59+
|> QueryBuilder.filter_pack(filter)
60+
|> ORM.paginater(~m(page size)a)
61+
|> reports_formater(:account_user)
62+
|> done()
63+
end
64+
end
4565

4666
@doc """
4767
list paged reports for article comemnts
@@ -87,42 +107,39 @@ defmodule GroupherServer.CMS.Delegate.AbuseReport do
87107
end
88108
end
89109

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))
110+
def report_account(account_id, reason, attr, user) do
111+
with {:ok, info} <- match(:account_user),
112+
{:ok, account} <- ORM.find(info.model, account_id) do
113+
Multi.new()
114+
|> Multi.run(:create_abuse_report, fn _, _ ->
115+
create_report(:account_user, account.id, reason, attr, user)
97116
end)
98-
)
99-
end
100-
101-
defp reports_formater(%{entries: entries} = paged_reports, thread)
102-
when thread in @article_threads do
103-
paged_reports
104-
|> Map.put(
105-
:entries,
106-
Enum.map(entries, fn report ->
107-
basic_report = report |> Map.take(@export_report_keys)
108-
basic_report |> Map.put(:article, extract_article_info(thread, report))
117+
|> Multi.run(:update_report_flag, fn _, _ ->
118+
update_report_meta(info, account)
109119
end)
110-
)
120+
|> Repo.transaction()
121+
|> result()
122+
end
111123
end
112124

113-
# TODO: original community and communities info
114-
defp extract_article_info(thread, %AbuseReport{} = report) do
115-
article = Map.get(report, thread)
116-
117-
article
118-
|> Map.take(@export_article_keys)
119-
|> Map.merge(%{thread: thread})
125+
@doc """
126+
undo report article content
127+
"""
128+
def undo_report_account(account_id, %User{} = user) do
129+
with {:ok, info} <- match(:account_user),
130+
{:ok, account} <- ORM.find(info.model, account_id) do
131+
Multi.new()
132+
|> Multi.run(:delete_abuse_report, fn _, _ ->
133+
delete_report(:account_user, account.id, user)
134+
end)
135+
|> Multi.run(:update_report_flag, fn _, _ ->
136+
update_report_meta(info, account)
137+
end)
138+
|> Repo.transaction()
139+
|> result()
140+
end
120141
end
121142

122-
# def report_account(user_id) do
123-
# # TODO*
124-
# end
125-
126143
@doc """
127144
report article content
128145
"""
@@ -131,10 +148,10 @@ defmodule GroupherServer.CMS.Delegate.AbuseReport do
131148
{:ok, article} <- ORM.find(info.model, article_id) do
132149
Multi.new()
133150
|> Multi.run(:create_abuse_report, fn _, _ ->
134-
create_report(thread, article_id, reason, attr, user)
151+
create_report(thread, article.id, reason, attr, user)
135152
end)
136153
|> Multi.run(:update_report_flag, fn _, _ ->
137-
update_report_meta(info, article, true)
154+
update_report_meta(info, article)
138155
end)
139156
|> Repo.transaction()
140157
|> result()
@@ -149,10 +166,10 @@ defmodule GroupherServer.CMS.Delegate.AbuseReport do
149166
{:ok, article} <- ORM.find(info.model, article_id) do
150167
Multi.new()
151168
|> Multi.run(:delete_abuse_report, fn _, _ ->
152-
delete_report(thread, article_id, user)
169+
delete_report(thread, article.id, user)
153170
end)
154171
|> Multi.run(:update_report_flag, fn _, _ ->
155-
update_report_meta(info, article, false)
172+
update_report_meta(info, article)
156173
end)
157174
|> Repo.transaction()
158175
|> result()
@@ -219,23 +236,26 @@ defmodule GroupherServer.CMS.Delegate.AbuseReport do
219236
end
220237
end
221238

222-
# update is_reported flag and reported_count in mete for article or comment
223-
defp update_report_meta(info, content, is_reported) do
239+
# update reported_count in mete for article or comment
240+
defp update_report_meta(info, content) do
224241
case ORM.find_by(AbuseReport, Map.put(%{}, info.foreign_key, content.id)) do
225242
{:ok, record} ->
226243
reported_count = record.report_cases |> length
227-
meta = content.meta |> Map.merge(%{reported_count: reported_count}) |> strip_struct
244+
245+
safe_meta = if is_nil(content.meta), do: info.default_meta, else: content.meta
246+
meta = safe_meta |> Map.merge(%{reported_count: reported_count}) |> strip_struct
228247

229248
content
230-
|> Ecto.Changeset.change(%{is_reported: is_reported})
249+
|> Ecto.Changeset.change()
231250
|> Ecto.Changeset.put_embed(:meta, meta)
232251
|> Repo.update()
233252

234253
{:error, _} ->
235-
meta = content.meta |> Map.merge(%{reported_count: 0}) |> strip_struct
254+
safe_meta = if is_nil(content.meta), do: info.default_meta, else: content.meta
255+
meta = safe_meta |> Map.merge(%{reported_count: 0}) |> strip_struct
236256

237257
content
238-
|> Ecto.Changeset.change(%{is_reported: false})
258+
|> Ecto.Changeset.change()
239259
|> Ecto.Changeset.put_embed(:meta, meta)
240260
|> Repo.update()
241261
end
@@ -261,6 +281,52 @@ defmodule GroupherServer.CMS.Delegate.AbuseReport do
261281
end
262282
end
263283

284+
defp reports_formater(%{entries: entries} = paged_reports, :account_user) do
285+
paged_reports
286+
|> Map.put(
287+
:entries,
288+
Enum.map(entries, fn report ->
289+
basic_report = report |> Map.take(@export_report_keys)
290+
basic_report |> Map.put(:account, extract_account_info(report))
291+
end)
292+
)
293+
end
294+
295+
defp reports_formater(%{entries: entries} = paged_reports, :article_comment) do
296+
paged_reports
297+
|> Map.put(
298+
:entries,
299+
Enum.map(entries, fn report ->
300+
basic_report = report |> Map.take(@export_report_keys)
301+
basic_report |> Map.put(:article_comment, extract_article_comment_info(report))
302+
end)
303+
)
304+
end
305+
306+
defp reports_formater(%{entries: entries} = paged_reports, thread)
307+
when thread in @article_threads do
308+
paged_reports
309+
|> Map.put(
310+
:entries,
311+
Enum.map(entries, fn report ->
312+
basic_report = report |> Map.take(@export_report_keys)
313+
basic_report |> Map.put(:article, extract_article_info(thread, report))
314+
end)
315+
)
316+
end
317+
318+
defp extract_account_info(%AbuseReport{} = report) do
319+
account = report |> Map.get(:account) |> Map.take(@export_author_keys)
320+
end
321+
322+
# TODO: original community and communities info
323+
defp extract_article_info(thread, %AbuseReport{} = report) do
324+
report
325+
|> Map.get(thread)
326+
|> Map.take(@export_article_keys)
327+
|> Map.merge(%{thread: thread})
328+
end
329+
264330
def extract_article_comment_info(%AbuseReport{} = report) do
265331
keys = [:id, :upvotes_count, :body_html]
266332
author = Map.take(report.article_comment.author, @export_author_keys)

lib/groupher_server/cms/helper/matcher2.ex

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,18 @@ defmodule GroupherServer.CMS.Helper.Matcher2 do
55

66
import Ecto.Query, warn: false
77

8-
alias GroupherServer.CMS
8+
alias GroupherServer.{Accounts, CMS}
99

10+
alias Accounts.User
1011
alias CMS.{ArticleComment, Post, Job, Repo}
1112

1213
def match(:article_comment) do
13-
{:ok, %{model: ArticleComment, foreign_key: :article_comment_id}}
14+
{:ok,
15+
%{
16+
model: ArticleComment,
17+
foreign_key: :article_comment_id,
18+
default_meta: CMS.Embeds.ArticleCommentMeta.default_meta()
19+
}}
1420
end
1521

1622
def match(:comment_article, %ArticleComment{post_id: post_id}) when not is_nil(post_id) do
@@ -25,29 +31,41 @@ defmodule GroupherServer.CMS.Helper.Matcher2 do
2531
{:error, "not supported"}
2632
end
2733

34+
def match(:account_user) do
35+
{:ok,
36+
%{
37+
model: User,
38+
foreign_key: :account_id,
39+
default_meta: Accounts.Embeds.UserMeta.default_meta()
40+
}}
41+
end
42+
2843
# used for paged pin articles
2944
def match(Post) do
3045
{:ok, %{thread: :post}}
3146
end
3247

3348
def match(:post) do
34-
{:ok, %{model: Post, foreign_key: :post_id}}
49+
{:ok,
50+
%{model: Post, foreign_key: :post_id, default_meta: CMS.Embeds.ArticleMeta.default_meta()}}
3551
end
3652

3753
def match(Job) do
3854
{:ok, %{thread: :job}}
3955
end
4056

4157
def match(:job) do
42-
{:ok, %{model: Job, foreign_key: :job_id}}
58+
{:ok,
59+
%{model: Job, foreign_key: :job_id, default_meta: CMS.Embeds.ArticleMeta.default_meta()}}
4360
end
4461

4562
def match(Repo) do
4663
{:ok, %{thread: :repo}}
4764
end
4865

4966
def match(:repo) do
50-
{:ok, %{model: Repo, foreign_key: :repo_id}}
67+
{:ok,
68+
%{model: Repo, foreign_key: :repo_id, default_meta: CMS.Embeds.ArticleMeta.default_meta()}}
5169
end
5270

5371
def match(:post, :query, id), do: {:ok, dynamic([c], c.post_id == ^id)}

lib/helper/utils/utils.ex

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,8 @@ defmodule Helper.Utils do
173173
struct |> Map.from_struct() |> Map.delete(:id)
174174
end
175175

176+
def strip_struct(map) when is_map(map), do: map
177+
176178
@doc "html uniq id generator for editorjs"
177179
@spec uid(:html, map) :: String.t()
178180
def uid(:html, %{"id" => id}) when g_none_empty_str(id), do: id
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
defmodule GroupherServer.Repo.Migrations.AddReportMetaToUser do
2+
use Ecto.Migration
3+
4+
def change do
5+
alter table(:users) do
6+
add(:is_reported, :boolean, default: false)
7+
add(:meta, :map)
8+
end
9+
end
10+
end

0 commit comments

Comments
 (0)