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

Commit b377a53

Browse files
committed
refactor(abuse-report): wip
1 parent 0ee167c commit b377a53

Some content is hidden

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

46 files changed

+226
-263
lines changed

lib/groupher_server/cms/cms.ex

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -160,11 +160,11 @@ defmodule GroupherServer.CMS do
160160
defdelegate list_comments_participators(thread, content_id, filters), to: CommentCURD
161161

162162
# TODO: move report to abuse report module
163-
defdelegate create_report(type, content_id, args, user), to: AbuseReport
163+
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
166166
defdelegate list_reports(type, content_id, filter), to: AbuseReport
167-
defdelegate report_article_comment(comment_id, user), to: ArticleCommentAction
167+
defdelegate report_article_comment(comment_id, reason, attr, user), to: ArticleCommentAction
168168
defdelegate unreport_article_comment(comment_id, user), to: ArticleCommentAction
169169

170170
# Passport CURD

lib/groupher_server/cms/delegates/abuse_report.ex

Lines changed: 48 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ defmodule GroupherServer.CMS.Delegate.AbuseReport do
1717

1818
alias Ecto.Multi
1919

20+
@doc """
21+
list paged reports for both comment and article
22+
"""
2023
def list_reports(type, content_id, %{page: page, size: size} = filter) do
2124
with {:ok, info} <- match(type) do
2225
query = from(r in AbuseReport, where: field(r, ^info.foreign_key) == ^content_id)
@@ -28,6 +31,9 @@ defmodule GroupherServer.CMS.Delegate.AbuseReport do
2831
end
2932
end
3033

34+
@doc """
35+
report article content
36+
"""
3137
def report_article(thread, article_id, reason, attr, %User{} = user) do
3238
with {:ok, info} <- match(thread),
3339
{:ok, article} <- ORM.find(info.model, article_id) do
@@ -43,7 +49,9 @@ defmodule GroupherServer.CMS.Delegate.AbuseReport do
4349
end
4450
end
4551

46-
@doc "unreport an article"
52+
@doc """
53+
undo report article content
54+
"""
4755
def undo_report_article(thread, article_id, %User{} = user) do
4856
with {:ok, info} <- match(thread),
4957
{:ok, article} <- ORM.find(info.model, article_id) do
@@ -59,47 +67,6 @@ defmodule GroupherServer.CMS.Delegate.AbuseReport do
5967
end
6068
end
6169

62-
defp delete_report(thread, content_id, %User{} = user) do
63-
with {:ok, info} <- match(thread),
64-
{:error, _} <- not_reported_before(info, content_id, user),
65-
{:ok, report} = ORM.find_by(AbuseReport, Map.put(%{}, info.foreign_key, content_id)) do
66-
case length(report.report_cases) do
67-
1 ->
68-
ORM.delete(report)
69-
70-
_ ->
71-
report_cases = report.report_cases |> Enum.reject(&(&1.user.login == user.login))
72-
73-
report
74-
|> Ecto.Changeset.change(%{report_cases_count: length(report_cases)})
75-
|> Ecto.Changeset.put_embed(:report_cases, report_cases)
76-
|> Repo.update()
77-
end
78-
end
79-
end
80-
81-
# update is_reported flag and reported_count in mete for article or comment
82-
defp update_report_meta(info, content, is_reported) do
83-
case ORM.find_by(AbuseReport, Map.put(%{}, info.foreign_key, content.id)) do
84-
{:ok, record} ->
85-
reported_count = record.report_cases |> length
86-
meta = content.meta |> Map.merge(%{reported_count: reported_count}) |> strip_struct
87-
88-
content
89-
|> Ecto.Changeset.change(%{is_reported: is_reported})
90-
|> Ecto.Changeset.put_embed(:meta, meta)
91-
|> Repo.update()
92-
93-
{:error, _} ->
94-
meta = content.meta |> Map.merge(%{reported_count: 0}) |> strip_struct
95-
96-
content
97-
|> Ecto.Changeset.change(%{is_reported: false})
98-
|> Ecto.Changeset.put_embed(:meta, meta)
99-
|> Repo.update()
100-
end
101-
end
102-
10370
def create_report(type, content_id, reason, attr, %User{} = user) do
10471
with {:ok, info} <- match(type),
10572
{:ok, report} <- not_reported_before(info, content_id, user) do
@@ -126,7 +93,7 @@ defmodule GroupherServer.CMS.Delegate.AbuseReport do
12693
length(report.report_cases),
12794
%Embeds.AbuseReportCase{
12895
reason: reason,
129-
additional_reason: "additional_reason",
96+
additional_reason: attr,
13097
user: %{login: user.login, nickname: user.nickname}
13198
}
13299
)
@@ -139,53 +106,52 @@ defmodule GroupherServer.CMS.Delegate.AbuseReport do
139106
end
140107
end
141108

142-
##############
143-
##############
144-
##############
145-
##############
146-
147-
@doc """
148-
create report record
149-
"""
150-
def create_report(type, content_id, %{reason: reason}, %User{} = user) do
151-
with {:ok, info} <- match(type),
152-
{:ok, report} <- not_reported_before(info, content_id, user) do
153-
case report do
154-
nil ->
155-
updated_report_cases = [
156-
%{
157-
reason: reason,
158-
additional_reason: "additional_reason",
159-
user: %{login: user.login, nickname: user.nickname}
160-
}
161-
]
162-
163-
args =
164-
%{report_cases_count: 1, report_cases: updated_report_cases}
165-
|> Map.put(info.foreign_key, content_id)
166-
167-
AbuseReport |> ORM.create(args)
109+
defp delete_report(thread, content_id, %User{} = user) do
110+
with {:ok, info} <- match(thread),
111+
{:error, _} <- not_reported_before(info, content_id, user),
112+
{:ok, report} = ORM.find_by(AbuseReport, Map.put(%{}, info.foreign_key, content_id)) do
113+
case length(report.report_cases) do
114+
1 ->
115+
ORM.delete(report)
168116

169117
_ ->
170-
updated_report_cases =
171-
report.report_cases
172-
|> List.insert_at(
173-
length(report.report_cases),
174-
%Embeds.AbuseReportCase{
175-
reason: reason,
176-
additional_reason: "additional_reason",
177-
user: %{login: user.login, nickname: user.nickname}
178-
}
179-
)
118+
report_cases = report.report_cases |> Enum.reject(&(&1.user.login == user.login))
180119

181120
report
182-
|> Ecto.Changeset.change(%{report_cases_count: length(updated_report_cases)})
183-
|> Ecto.Changeset.put_embed(:report_cases, updated_report_cases)
121+
|> Ecto.Changeset.change(%{report_cases_count: length(report_cases)})
122+
|> Ecto.Changeset.put_embed(:report_cases, report_cases)
184123
|> Repo.update()
185124
end
186125
end
187126
end
188127

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

@@ -202,9 +168,7 @@ defmodule GroupherServer.CMS.Delegate.AbuseReport do
202168
|> length
203169
|> Kernel.>(0)
204170

205-
if not reported_before,
206-
do: {:ok, report},
207-
else: {:error, "#{login} already reported"}
171+
if not reported_before, do: {:ok, report}, else: {:error, "#{login} already reported"}
208172
end
209173
end
210174

lib/groupher_server/cms/delegates/article_comment_action.ex

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,11 +102,11 @@ defmodule GroupherServer.CMS.Delegate.ArticleCommentAction do
102102
end
103103

104104
@doc "report a comment"
105-
def report_article_comment(comment_id, %User{} = user) do
105+
def report_article_comment(comment_id, reason, attr, %User{} = user) do
106106
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 _, _ ->
112112
# TODO: update report count in meta

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

test/groupher_server/cms/abuse_reports/comment_report_test.exs

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,8 @@ defmodule GroupherServer.Test.CMS.AbuseReports.CommentReport do
33

44
use GroupherServer.TestTools
55

6-
alias Helper.ORM
76
alias GroupherServer.CMS
87

9-
alias CMS.AbuseReport
10-
118
setup do
129
{:ok, user} = db_insert(:user)
1310
{:ok, user2} = db_insert(:user)
@@ -18,10 +15,10 @@ defmodule GroupherServer.Test.CMS.AbuseReports.CommentReport do
1815
end
1916

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

2623
{:ok, all_reports} = CMS.list_reports(:article_comment, comment.id, %{page: 1, size: 20})
2724

@@ -33,12 +30,12 @@ defmodule GroupherServer.Test.CMS.AbuseReports.CommentReport do
3330
assert List.first(report_cases).user.login == user.login
3431
end
3532

36-
@tag :wip3
33+
@tag :wip2
3734
test "different user report a comment should have same report with different report cases",
3835
~m(user user2 post)a do
3936
{:ok, comment} = CMS.create_article_comment(:post, post.id, "commment", user)
40-
{:ok, _} = CMS.report_article_comment(comment.id, user)
41-
{:ok, _} = CMS.report_article_comment(comment.id, user2)
37+
{:ok, _} = CMS.report_article_comment(comment.id, "reason", "attr", user)
38+
{:ok, _} = CMS.report_article_comment(comment.id, "reason", "attr", user2)
4239

4340
{:ok, all_reports} = CMS.list_reports(:article_comment, comment.id, %{page: 1, size: 20})
4441

@@ -56,8 +53,8 @@ defmodule GroupherServer.Test.CMS.AbuseReports.CommentReport do
5653
@tag :wip
5754
test "same user can not report a comment twice", ~m(user post)a do
5855
{:ok, comment} = CMS.create_article_comment(:post, post.id, "commment", user)
59-
{:ok, comment} = CMS.report_article_comment(comment.id, user)
60-
assert {:error, _} = CMS.report_article_comment(comment.id, user)
56+
{:ok, comment} = CMS.report_article_comment(comment.id, "reason", "attr", user)
57+
assert {:error, _} = CMS.report_article_comment(comment.id, "reason", "attr", user)
6158
end
6259
end
6360
end

test/groupher_server/cms/comments/job_comment_emotions_test.exs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ defmodule GroupherServer.Test.CMS.Comments.JobCommentEmotions do
2121
end
2222

2323
describe "[emotion in paged article comment]" do
24-
@tag :wip3
24+
@tag :wip2
2525
test "login user should got viewer has emotioned status", ~m(job user)a do
2626
total_count = 0
2727
page_number = 10
@@ -76,7 +76,7 @@ defmodule GroupherServer.Test.CMS.Comments.JobCommentEmotions do
7676
assert @default_emotions == emotions
7777
end
7878

79-
@tag :wip3
79+
@tag :wip2
8080
test "can make emotion to comment", ~m(job user user2)a do
8181
parent_content = "parent comment"
8282
{:ok, parent_comment} = CMS.create_article_comment(:job, job.id, parent_content, user)
@@ -91,7 +91,7 @@ defmodule GroupherServer.Test.CMS.Comments.JobCommentEmotions do
9191
assert user_exist_in?(user2, emotions.latest_downvote_users)
9292
end
9393

94-
@tag :wip3
94+
@tag :wip2
9595
test "can undo emotion to comment", ~m(job user user2)a do
9696
parent_content = "parent comment"
9797
{:ok, parent_comment} = CMS.create_article_comment(:job, job.id, parent_content, user)
@@ -114,7 +114,7 @@ defmodule GroupherServer.Test.CMS.Comments.JobCommentEmotions do
114114
assert not user_exist_in?(user2, emotions.latest_downvote_users)
115115
end
116116

117-
@tag :wip3
117+
@tag :wip2
118118
test "same user make same emotion to same comment.", ~m(job user)a do
119119
parent_content = "parent comment"
120120
{:ok, parent_comment} = CMS.create_article_comment(:job, job.id, parent_content, user)
@@ -128,7 +128,7 @@ defmodule GroupherServer.Test.CMS.Comments.JobCommentEmotions do
128128
assert user_exist_in?(user, parent_comment.emotions.latest_downvote_users)
129129
end
130130

131-
@tag :wip3
131+
@tag :wip2
132132
test "same user same emotion to same comment only have one user_emotion record",
133133
~m(job user)a do
134134
parent_content = "parent comment"
@@ -152,7 +152,7 @@ defmodule GroupherServer.Test.CMS.Comments.JobCommentEmotions do
152152
assert record.heart
153153
end
154154

155-
@tag :wip3
155+
@tag :wip2
156156
test "different user can make same emotions on same comment", ~m(job user user2 user3)a do
157157
{:ok, parent_comment} = CMS.create_article_comment(:job, job.id, "parent comment", user)
158158

test/groupher_server/cms/comments/job_comment_replies_test.exs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ defmodule GroupherServer.Test.CMS.Comments.JobCommentReplies do
3333
assert exist_in?(replyed_comment, parent_comment.replies)
3434
end
3535

36-
@tag :wip3
36+
@tag :wip2
3737
test "deleted comment can not be reply", ~m(job user user2)a do
3838
parent_content = "parent comment"
3939
reply_content = "reply comment"
@@ -64,7 +64,7 @@ defmodule GroupherServer.Test.CMS.Comments.JobCommentReplies do
6464
assert exist_in?(replyed_comment_2, parent_comment.replies)
6565
end
6666

67-
@tag :wip3
67+
@tag :wip2
6868
test "reply to reply inside a comment should belong same parent comment",
6969
~m(job user user2)a do
7070
{:ok, parent_comment} = CMS.create_article_comment(:job, job.id, "parent comment", user)
@@ -90,7 +90,7 @@ defmodule GroupherServer.Test.CMS.Comments.JobCommentReplies do
9090
assert replyed_comment_3.reply_to_id == replyed_comment_2.id
9191
end
9292

93-
@tag :wip3
93+
@tag :wip2
9494
test "reply to reply inside a comment should have is_reply_to_others flag in meta",
9595
~m(job user user2)a do
9696
{:ok, parent_comment} = CMS.create_article_comment(:job, job.id, "parent comment", user)
@@ -110,7 +110,7 @@ defmodule GroupherServer.Test.CMS.Comments.JobCommentReplies do
110110
assert replyed_comment_3.meta.is_reply_to_others
111111
end
112112

113-
@tag :wip3
113+
@tag :wip2
114114
test "comment replies only contains @max_parent_replies_count replies", ~m(job user)a do
115115
total_reply_count = @max_parent_replies_count + 1
116116

0 commit comments

Comments
 (0)