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

Commit ef26b7d

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

File tree

12 files changed

+208
-28
lines changed

12 files changed

+208
-28
lines changed

lib/groupher_server/cms/cms.ex

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -143,8 +143,6 @@ defmodule GroupherServer.CMS do
143143

144144
defdelegate fold_article_comment(comment_id, user), to: ArticleCommentAction
145145
defdelegate unfold_article_comment(comment_id, user), to: ArticleCommentAction
146-
defdelegate report_article_comment(comment_id, user), to: ArticleCommentAction
147-
defdelegate unreport_article_comment(comment_id, user), to: ArticleCommentAction
148146

149147
defdelegate emotion_to_comment(comment_id, args, user), to: ArticleCommentEmotion
150148
defdelegate undo_emotion_to_comment(comment_id, args, user), to: ArticleCommentEmotion
@@ -161,8 +159,12 @@ defmodule GroupherServer.CMS do
161159
defdelegate list_comments(thread, content_id, filters), to: CommentCURD
162160
defdelegate list_comments_participators(thread, content_id, filters), to: CommentCURD
163161

164-
# report
162+
# TODO: move report to abuse report module
165163
defdelegate create_report(type, content_id, args, user), to: AbuseReport
164+
defdelegate report_article(thread, article_id, reason, attr, user), to: AbuseReport
165+
defdelegate list_reports(type, content_id, filter), to: AbuseReport
166+
defdelegate report_article_comment(comment_id, user), to: ArticleCommentAction
167+
defdelegate unreport_article_comment(comment_id, user), to: ArticleCommentAction
166168

167169
# Passport CURD
168170
defdelegate stamp_passport(rules, user), to: PassportCURD

lib/groupher_server/cms/delegates/abuse_report.ex

Lines changed: 93 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,102 @@ defmodule GroupherServer.CMS.Delegate.AbuseReport do
33
CURD and operations for article comments
44
"""
55
import Ecto.Query, warn: false
6-
# import Helper.Utils, only: [done: 1]
6+
import Helper.Utils, only: [done: 1, strip_struct: 1]
77

88
import GroupherServer.CMS.Helper.Matcher2
9-
# import ShortMaps
9+
import ShortMaps
1010

11-
alias Helper.{ORM}
11+
alias Helper.ORM
12+
alias Helper.QueryBuilder
1213
alias GroupherServer.{Accounts, CMS, Repo}
1314

1415
alias Accounts.User
1516
alias CMS.{AbuseReport, Embeds}
1617

17-
# alias Accounts.User
18+
alias Ecto.Multi
1819

20+
def list_reports(type, content_id, %{page: page, size: size} = filter) do
21+
with {:ok, info} <- match(type) do
22+
query = from(r in AbuseReport, where: field(r, ^info.foreign_key) == ^content_id)
23+
24+
query
25+
|> QueryBuilder.filter_pack(filter)
26+
|> ORM.paginater(~m(page size)a)
27+
|> done()
28+
end
29+
end
30+
31+
def report_article(thread, article_id, reason, attr, %User{} = user) do
32+
with {:ok, info} <- match(thread),
33+
{:ok, article} <- ORM.find(info.model, article_id) do
34+
Multi.new()
35+
|> Multi.run(:create_abuse_report, fn _, _ ->
36+
create_report(thread, article_id, reason, attr, user)
37+
end)
38+
|> 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
42+
43+
article
44+
|> Ecto.Changeset.change(%{is_reported: true})
45+
|> Ecto.Changeset.put_embed(:meta, meta)
46+
|> Repo.update()
47+
end)
48+
|> Repo.transaction()
49+
|> result()
50+
end
51+
end
52+
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
59+
60+
def create_report(type, content_id, reason, attr, %User{} = user) do
61+
with {:ok, info} <- match(type),
62+
{:ok, report} <- not_reported_before(info, content_id, user) do
63+
case report do
64+
nil ->
65+
updated_report_cases = [
66+
%{
67+
reason: reason,
68+
additional_reason: attr,
69+
user: %{login: user.login, nickname: user.nickname}
70+
}
71+
]
72+
73+
args =
74+
%{report_cases_count: 1, report_cases: updated_report_cases}
75+
|> Map.put(info.foreign_key, content_id)
76+
77+
AbuseReport |> ORM.create(args)
78+
79+
_ ->
80+
updated_report_cases =
81+
report.report_cases
82+
|> List.insert_at(
83+
length(report.report_cases),
84+
%Embeds.AbuseReportCase{
85+
reason: reason,
86+
additional_reason: "additional_reason",
87+
user: %{login: user.login, nickname: user.nickname}
88+
}
89+
)
90+
91+
report
92+
|> Ecto.Changeset.change(%{report_cases_count: length(updated_report_cases)})
93+
|> Ecto.Changeset.put_embed(:report_cases, updated_report_cases)
94+
|> Repo.update()
95+
end
96+
end
97+
end
98+
99+
@doc """
100+
create report record
101+
"""
19102
def create_report(type, content_id, %{reason: reason}, %User{} = user) do
20103
with {:ok, info} <- match(type),
21104
{:ok, report} <- not_reported_before(info, content_id, user) do
@@ -76,4 +159,10 @@ defmodule GroupherServer.CMS.Delegate.AbuseReport do
76159
else: {:error, "#{login} already reported"}
77160
end
78161
end
162+
163+
defp result({:ok, %{create_abuse_report: result}}), do: result |> done()
164+
165+
defp result({:error, _, result, _steps}) do
166+
{:error, result}
167+
end
79168
end

lib/groupher_server/cms/delegates/article_comment_action.ex

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ defmodule GroupherServer.CMS.Delegate.ArticleCommentAction do
8282
end
8383
end
8484

85+
@doc "fold a comment"
8586
def fold_article_comment(%ArticleComment{} = comment, %User{} = _user) do
8687
comment |> ORM.update(%{is_folded: true})
8788
end
@@ -93,22 +94,22 @@ defmodule GroupherServer.CMS.Delegate.ArticleCommentAction do
9394
end
9495
end
9596

96-
@doc "fold a comment"
97+
@doc "unfold a comment"
9798
def unfold_article_comment(comment_id, %User{} = _user) do
9899
with {:ok, comment} <- ORM.find(ArticleComment, comment_id) do
99100
comment |> ORM.update(%{is_folded: false})
100101
end
101102
end
102103

103-
@doc "fold a comment"
104+
@doc "report a comment"
104105
def report_article_comment(comment_id, %User{} = user) do
105-
with {:ok, comment} <-
106-
ORM.find(ArticleComment, comment_id) do
106+
with {:ok, comment} <- ORM.find(ArticleComment, comment_id) do
107107
Multi.new()
108108
|> Multi.run(:create_abuse_report, fn _, _ ->
109109
CMS.create_report(:article_comment, comment_id, %{reason: "todo fucked"}, user)
110110
end)
111111
|> Multi.run(:update_report_flag, fn _, _ ->
112+
# TODO: update report count in meta
112113
ORM.update(comment, %{is_reported: true})
113114
end)
114115
|> Multi.run(:fold_comment_report_too_many, fn _, %{create_abuse_report: abuse_report} ->
@@ -121,10 +122,9 @@ defmodule GroupherServer.CMS.Delegate.ArticleCommentAction do
121122
end
122123
end
123124

124-
@doc "fold a comment"
125+
@doc "unreport a comment"
125126
def unreport_article_comment(comment_id, %User{} = _user) do
126-
with {:ok, comment} <-
127-
ORM.find(ArticleComment, comment_id) do
127+
with {:ok, comment} <- ORM.find(ArticleComment, comment_id) do
128128
comment |> ORM.update(%{is_reported: false})
129129
end
130130
end

lib/groupher_server/cms/delegates/article_operation.ex

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,12 @@ defmodule GroupherServer.CMS.Delegate.ArticleOperation do
6262
end
6363
end
6464

65+
########
66+
########
67+
########
68+
########
69+
########
70+
6571
@doc """
6672
trash / untrash articles
6773
"""

lib/groupher_server/cms/embeds/article_meta.ex

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,16 @@ defmodule GroupherServer.CMS.Embeds.ArticleMeta do
66
use Accessible
77
import Ecto.Changeset
88

9-
@optional_fields ~w(is_edited is_comment_locked is_reported upvoted_user_ids collected_user_ids viewed_user_ids reported_user_ids)a
9+
@optional_fields ~w(is_edited is_comment_locked upvoted_user_ids collected_user_ids viewed_user_ids reported_user_ids reported_count)a
1010

1111
@default_meta %{
1212
is_edited: false,
1313
is_comment_locked: false,
14-
is_reported: false,
1514
upvoted_user_ids: [],
1615
collected_user_ids: [],
1716
viewed_user_ids: [],
18-
reported_user_ids: []
17+
reported_user_ids: [],
18+
reported_count: 0
1919
}
2020

2121
@doc "for test usage"
@@ -24,12 +24,12 @@ defmodule GroupherServer.CMS.Embeds.ArticleMeta do
2424
embedded_schema do
2525
field(:is_edited, :boolean, default: false)
2626
field(:is_comment_locked, :boolean, default: false)
27-
field(:is_reported, :boolean, default: false)
2827
# reaction history
2928
field(:upvoted_user_ids, {:array, :integer}, default: [])
3029
field(:collected_user_ids, {:array, :integer}, default: [])
3130
field(:viewed_user_ids, {:array, :integer}, default: [])
3231
field(:reported_user_ids, {:array, :integer}, default: [])
32+
field(:reported_count, :integer, default: 0)
3333
end
3434

3535
def changeset(struct, params) do

lib/groupher_server/cms/job.ex

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ defmodule GroupherServer.CMS.Job do
2424

2525
@timestamps_opts [type: :utc_datetime_usec]
2626
@required_fields ~w(title company company_logo body digest length)a
27-
@optional_fields ~w(origial_community_id desc company_link link_addr copy_right salary exp education field finance scale article_comments_count article_comments_participators_count upvotes_count collects_count)a
27+
@optional_fields ~w(origial_community_id desc company_link link_addr copy_right salary exp education field finance scale article_comments_count article_comments_participators_count upvotes_count collects_count is_reported)a
2828

2929
@type t :: %Job{}
3030
schema "cms_jobs" do
@@ -57,6 +57,7 @@ defmodule GroupherServer.CMS.Job do
5757
# NOTE: this one is tricky, pin is dynamic changed when return by func: add_pin_contents_ifneed
5858
field(:is_pinned, :boolean, default: false, virtual: true)
5959
field(:trash, :boolean, default_value: false, virtual: true)
60+
field(:is_reported, :boolean, default: false)
6061

6162
has_many(:upvotes, {"article_upvotes", ArticleUpvote})
6263
field(:upvotes_count, :integer, default: 0)

lib/groupher_server/cms/post.ex

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ defmodule GroupherServer.CMS.Post do
2626

2727
@timestamps_opts [type: :utc_datetime_usec]
2828
@required_fields ~w(title body digest length)a
29-
@optional_fields ~w(origial_community_id link_addr copy_right link_addr link_icon article_comments_count article_comments_participators_count upvotes_count collects_count)a
29+
@optional_fields ~w(origial_community_id link_addr copy_right link_addr link_icon article_comments_count article_comments_participators_count upvotes_count collects_count is_reported)a
3030

3131
@type t :: %Post{}
3232
schema "cms_posts" do
@@ -48,6 +48,7 @@ defmodule GroupherServer.CMS.Post do
4848
# field(:pin, :boolean, default_value: false, virtual: true)
4949
field(:is_pinned, :boolean, default: false, virtual: true)
5050
field(:trash, :boolean, default_value: false, virtual: true)
51+
field(:is_reported, :boolean, default: false)
5152

5253
field(:viewer_has_viewed, :boolean, default: false, virtual: true)
5354
field(:viewer_has_upvoted, :boolean, default: false, virtual: true)

lib/groupher_server/cms/repo.ex

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ defmodule GroupherServer.CMS.Repo do
2525

2626
@timestamps_opts [type: :utc_datetime_usec]
2727
@required_fields ~w(title owner_name owner_url repo_url desc readme star_count issues_count prs_count fork_count watch_count upvotes_count collects_count)a
28-
@optional_fields ~w(origial_community_id last_sync homepage_url release_tag license)a
28+
@optional_fields ~w(origial_community_id last_sync homepage_url release_tag license is_reported)a
2929

3030
@type t :: %Repo{}
3131
schema "cms_repos" do
@@ -61,6 +61,7 @@ defmodule GroupherServer.CMS.Repo do
6161
# NOTE: this one is tricky, pin is dynamic changed when return by func: add_pin_contents_ifneed
6262
field(:is_pinned, :boolean, default: false, virtual: true)
6363
field(:trash, :boolean, default_value: false)
64+
field(:is_reported, :boolean, default: false)
6465

6566
has_many(:upvotes, {"article_upvotes", ArticleUpvote})
6667
field(:upvotes_count, :integer, default: 0)
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
defmodule GroupherServer.Repo.Migrations.MoveIsReportedToArticle do
2+
use Ecto.Migration
3+
4+
def change do
5+
alter(table(:cms_posts), do: add(:is_reported, :boolean, default: false))
6+
alter(table(:cms_jobs), do: add(:is_reported, :boolean, default: false))
7+
alter(table(:cms_repos), do: add(:is_reported, :boolean, default: false))
8+
end
9+
end

test/groupher_server/cms/abuse_report_test.exs renamed to test/groupher_server/cms/abuse_reports/comment_report_test.exs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
defmodule GroupherServer.Test.CMS.AbuseReport do
1+
defmodule GroupherServer.Test.CMS.AbuseReports.CommentReport do
22
@moduledoc false
33

44
use GroupherServer.TestTools
@@ -18,12 +18,13 @@ defmodule GroupherServer.Test.CMS.AbuseReport do
1818
end
1919

2020
describe "[article comment report/unreport]" do
21-
@tag :wip
21+
@tag :wip3
2222
test "report a comment should have a abuse report record", ~m(user post)a do
2323
{:ok, comment} = CMS.create_article_comment(:post, post.id, "commment", user)
2424
{:ok, _comment} = CMS.report_article_comment(comment.id, user)
2525

26-
{:ok, all_reports} = ORM.find_all(AbuseReport, %{page: 1, size: 10})
26+
{:ok, all_reports} = CMS.list_reports(:article_comment, comment.id, %{page: 1, size: 20})
27+
2728
report = List.first(all_reports.entries)
2829
report_cases = report.report_cases
2930

@@ -32,14 +33,14 @@ defmodule GroupherServer.Test.CMS.AbuseReport do
3233
assert List.first(report_cases).user.login == user.login
3334
end
3435

35-
@tag :wip
36+
@tag :wip3
3637
test "different user report a comment should have same report with different report cases",
3738
~m(user user2 post)a do
3839
{:ok, comment} = CMS.create_article_comment(:post, post.id, "commment", user)
3940
{:ok, _} = CMS.report_article_comment(comment.id, user)
4041
{:ok, _} = CMS.report_article_comment(comment.id, user2)
4142

42-
{:ok, all_reports} = ORM.find_all(AbuseReport, %{page: 1, size: 10})
43+
{:ok, all_reports} = CMS.list_reports(:article_comment, comment.id, %{page: 1, size: 20})
4344

4445
report = List.first(all_reports.entries)
4546
report_cases = report.report_cases

0 commit comments

Comments
 (0)