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

Commit 230a6b0

Browse files
authored
refactor(abuse-report): enhance workflow && re-org (#351)
* refactor(abuse-report): wip * refactor(abuse-report): wip * refactor(abuse-report): wip * refactor(abuse-report): wip * refactor(abuse-report): wip * refactor(abuse-report): wip
1 parent 1810740 commit 230a6b0

File tree

65 files changed

+852
-231
lines changed

Some content is hidden

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

65 files changed

+852
-231
lines changed

lib/groupher_server/cms/abuse_report.ex

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,18 @@ defmodule GroupherServer.CMS.AbuseReport do
66
import Ecto.Changeset
77

88
alias GroupherServer.{Accounts, CMS}
9-
alias CMS.{ArticleComment, Embeds, Post, Job}
9+
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 account_id operate_user_id deal_with is_closed report_cases_count)a
12+
@optional_fields ~w(article_comment_id post_id job_id repo_id account_id operate_user_id deal_with is_closed report_cases_count)a
1313
@update_fields ~w(operate_user_id deal_with is_closed report_cases_count)a
1414

1515
@type t :: %AbuseReport{}
1616
schema "abuse_reports" do
1717
belongs_to(:article_comment, ArticleComment, foreign_key: :article_comment_id)
1818
belongs_to(:post, Post, foreign_key: :post_id)
1919
belongs_to(:job, Job, foreign_key: :job_id)
20+
belongs_to(:repo, Repo, foreign_key: :repo_id)
2021
belongs_to(:account, Accounts.User, foreign_key: :account_id)
2122

2223
embeds_many(:report_cases, Embeds.AbuseReportCase, on_replace: :delete)

lib/groupher_server/cms/cms.ex

Lines changed: 7 additions & 4 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,13 @@ 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
165-
defdelegate create_report(type, content_id, args, user), to: AbuseReport
162+
# TODO: move report to abuse report module
163+
defdelegate create_report(type, content_id, reason, attr, user), to: AbuseReport
164+
defdelegate report_article(thread, article_id, reason, attr, user), to: AbuseReport
165+
defdelegate undo_report_article(thread, article_id, user), to: AbuseReport
166+
defdelegate list_reports(type, content_id, filter), to: AbuseReport
167+
defdelegate report_article_comment(comment_id, reason, attr, user), to: ArticleCommentAction
168+
defdelegate undo_report_article_comment(comment_id, user), to: AbuseReport
166169

167170
# Passport CURD
168171
defdelegate stamp_passport(rules, user), to: PassportCURD

lib/groupher_server/cms/delegates/abuse_report.ex

Lines changed: 117 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,58 +3,152 @@ 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

19-
def create_report(type, content_id, %{reason: reason}, %User{} = user) do
20+
@doc """
21+
list paged reports for both comment and article
22+
"""
23+
def list_reports(type, content_id, %{page: page, size: size} = filter) do
24+
with {:ok, info} <- match(type) do
25+
query = from(r in AbuseReport, where: field(r, ^info.foreign_key) == ^content_id)
26+
27+
query
28+
|> QueryBuilder.filter_pack(filter)
29+
|> ORM.paginater(~m(page size)a)
30+
|> done()
31+
end
32+
end
33+
34+
@doc """
35+
report article content
36+
"""
37+
def report_article(thread, article_id, reason, attr, %User{} = user) do
38+
with {:ok, info} <- match(thread),
39+
{:ok, article} <- ORM.find(info.model, article_id) do
40+
Multi.new()
41+
|> Multi.run(:create_abuse_report, fn _, _ ->
42+
create_report(thread, article_id, reason, attr, user)
43+
end)
44+
|> Multi.run(:update_report_flag, fn _, _ ->
45+
update_report_meta(info, article, true)
46+
end)
47+
|> Repo.transaction()
48+
|> result()
49+
end
50+
end
51+
52+
def undo_report_article_comment(comment_id, %User{} = user) do
53+
undo_report_article(:article_comment, comment_id, user)
54+
end
55+
56+
@doc """
57+
undo report article content
58+
"""
59+
def undo_report_article(thread, article_id, %User{} = user) do
60+
with {:ok, info} <- match(thread),
61+
{:ok, article} <- ORM.find(info.model, article_id) do
62+
Multi.new()
63+
|> Multi.run(:delete_abuse_report, fn _, _ ->
64+
delete_report(thread, article_id, user)
65+
end)
66+
|> Multi.run(:update_report_flag, fn _, _ ->
67+
update_report_meta(info, article, false)
68+
end)
69+
|> Repo.transaction()
70+
|> result()
71+
end
72+
end
73+
74+
def create_report(type, content_id, reason, attr, %User{} = user) do
2075
with {:ok, info} <- match(type),
2176
{:ok, report} <- not_reported_before(info, content_id, user) do
2277
case report do
2378
nil ->
24-
updated_report_cases = [
79+
report_cases = [
2580
%{
2681
reason: reason,
27-
additional_reason: "additional_reason",
82+
attr: attr,
2883
user: %{login: user.login, nickname: user.nickname}
2984
}
3085
]
3186

3287
args =
33-
%{report_cases_count: 1, report_cases: updated_report_cases}
88+
%{report_cases_count: 1, report_cases: report_cases}
3489
|> Map.put(info.foreign_key, content_id)
3590

3691
AbuseReport |> ORM.create(args)
3792

3893
_ ->
39-
updated_report_cases =
94+
user = %{login: user.login, nickname: user.nickname}
95+
96+
report_cases =
4097
report.report_cases
4198
|> List.insert_at(
4299
length(report.report_cases),
43-
%Embeds.AbuseReportCase{
44-
reason: reason,
45-
additional_reason: "additional_reason",
46-
user: %{login: user.login, nickname: user.nickname}
47-
}
100+
%Embeds.AbuseReportCase{reason: reason, attr: attr, user: user}
48101
)
49102

50103
report
51-
|> Ecto.Changeset.change(%{report_cases_count: length(updated_report_cases)})
52-
|> Ecto.Changeset.put_embed(:report_cases, updated_report_cases)
104+
|> Ecto.Changeset.change(%{report_cases_count: length(report_cases)})
105+
|> Ecto.Changeset.put_embed(:report_cases, report_cases)
106+
|> Repo.update()
107+
end
108+
end
109+
end
110+
111+
defp delete_report(thread, content_id, %User{} = user) do
112+
with {:ok, info} <- match(thread),
113+
{:error, _} <- not_reported_before(info, content_id, user),
114+
{:ok, report} = ORM.find_by(AbuseReport, Map.put(%{}, info.foreign_key, content_id)) do
115+
case length(report.report_cases) do
116+
1 ->
117+
ORM.delete(report)
118+
119+
_ ->
120+
report_cases = report.report_cases |> Enum.reject(&(&1.user.login == user.login))
121+
122+
report
123+
|> Ecto.Changeset.change(%{report_cases_count: length(report_cases)})
124+
|> Ecto.Changeset.put_embed(:report_cases, report_cases)
53125
|> Repo.update()
54126
end
55127
end
56128
end
57129

130+
# update is_reported flag and reported_count in mete for article or comment
131+
defp update_report_meta(info, content, is_reported) do
132+
case ORM.find_by(AbuseReport, Map.put(%{}, info.foreign_key, content.id)) do
133+
{:ok, record} ->
134+
reported_count = record.report_cases |> length
135+
meta = content.meta |> Map.merge(%{reported_count: reported_count}) |> strip_struct
136+
137+
content
138+
|> Ecto.Changeset.change(%{is_reported: is_reported})
139+
|> Ecto.Changeset.put_embed(:meta, meta)
140+
|> Repo.update()
141+
142+
{:error, _} ->
143+
meta = content.meta |> Map.merge(%{reported_count: 0}) |> strip_struct
144+
145+
content
146+
|> Ecto.Changeset.change(%{is_reported: false})
147+
|> Ecto.Changeset.put_embed(:meta, meta)
148+
|> Repo.update()
149+
end
150+
end
151+
58152
defp not_reported_before(info, content_id, %User{login: login}) do
59153
query = from(r in AbuseReport, where: field(r, ^info.foreign_key) == ^content_id)
60154

@@ -71,9 +165,13 @@ defmodule GroupherServer.CMS.Delegate.AbuseReport do
71165
|> length
72166
|> Kernel.>(0)
73167

74-
if not reported_before,
75-
do: {:ok, report},
76-
else: {:error, "#{login} already reported"}
168+
if not reported_before, do: {:ok, report}, else: {:error, "#{login} already reported"}
77169
end
78170
end
171+
172+
defp result({:ok, %{update_report_flag: result}}), do: result |> done()
173+
174+
defp result({:error, _, result, _steps}) do
175+
{:error, result}
176+
end
79177
end

lib/groupher_server/cms/delegates/article_comment_action.ex

Lines changed: 7 additions & 14 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-
def report_article_comment(comment_id, %User{} = user) do
105-
with {:ok, comment} <-
106-
ORM.find(ArticleComment, comment_id) do
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
107107
Multi.new()
108108
|> Multi.run(:create_abuse_report, fn _, _ ->
109-
CMS.create_report(:article_comment, comment_id, %{reason: "todo fucked"}, user)
109+
CMS.create_report(:article_comment, comment_id, reason, attr, 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,14 +122,6 @@ defmodule GroupherServer.CMS.Delegate.ArticleCommentAction do
121122
end
122123
end
123124

124-
@doc "fold a comment"
125-
def unreport_article_comment(comment_id, %User{} = _user) do
126-
with {:ok, comment} <-
127-
ORM.find(ArticleComment, comment_id) do
128-
comment |> ORM.update(%{is_reported: false})
129-
end
130-
end
131-
132125
@doc "reply to exsiting comment"
133126
def reply_article_comment(comment_id, content, %User{} = user) do
134127
with {:ok, target_comment} <-

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/abuse_report_case.ex

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,19 @@ defmodule GroupherServer.CMS.Embeds.AbuseReportCase do
88
alias GroupherServer.CMS
99
alias CMS.Embeds
1010

11+
@optional_fields [:reason, :attr]
12+
1113
embedded_schema do
1214
field(:reason, :string)
13-
field(:additional_reason, :string)
15+
field(:attr, :string)
1416
embeds_one(:user, Embeds.User, on_replace: :delete)
1517

1618
timestamps(type: :utc_datetime)
1719
end
1820

1921
def changeset(struct, params) do
2022
struct
21-
|> cast(params, [:reason, :additional_reason])
23+
|> cast(params, @optional_fields)
2224
|> cast_embed(:user, required: true, with: &Embeds.User.changeset/2)
2325
end
2426
end

lib/groupher_server/cms/embeds/article_comment_meta.ex

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,15 @@ defmodule GroupherServer.CMS.Embeds.ArticleCommentMeta do
77

88
import Ecto.Changeset
99

10-
@optional_fields ~w(is_article_author_upvoted is_solution report_count is_reply_to_others)a
10+
@optional_fields ~w(is_article_author_upvoted is_solution report_count is_reply_to_others reported_count)a
1111

1212
@default_meta %{
1313
is_article_author_upvoted: false,
1414
is_solution: false,
1515
is_reply_to_others: false,
1616
report_count: 0,
17-
upvoted_user_ids: []
17+
upvoted_user_ids: [],
18+
reported_count: 0
1819
}
1920

2021
@doc "for test usage"
@@ -29,6 +30,7 @@ defmodule GroupherServer.CMS.Embeds.ArticleCommentMeta do
2930
field(:report_count, :integer, default: 0)
3031

3132
field(:upvoted_user_ids, {:array, :integer}, default: [])
33+
field(:reported_count, :integer, default: 0)
3234
end
3335

3436
def changeset(struct, params) do

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)

0 commit comments

Comments
 (0)