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

Commit f4a606b

Browse files
committed
refactor(abuse-report): wip
1 parent 2c5aa42 commit f4a606b

Some content is hidden

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

57 files changed

+422
-181
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/delegates/abuse_report.ex

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ defmodule GroupherServer.CMS.Delegate.AbuseReport do
7979
report_cases = [
8080
%{
8181
reason: reason,
82-
additional_reason: attr,
82+
attr: attr,
8383
user: %{login: user.login, nickname: user.nickname}
8484
}
8585
]
@@ -91,15 +91,13 @@ defmodule GroupherServer.CMS.Delegate.AbuseReport do
9191
AbuseReport |> ORM.create(args)
9292

9393
_ ->
94+
user = %{login: user.login, nickname: user.nickname}
95+
9496
report_cases =
9597
report.report_cases
9698
|> List.insert_at(
9799
length(report.report_cases),
98-
%Embeds.AbuseReportCase{
99-
reason: reason,
100-
additional_reason: attr,
101-
user: %{login: user.login, nickname: user.nickname}
102-
}
100+
%Embeds.AbuseReportCase{reason: reason, attr: attr, user: user}
103101
)
104102

105103
report
@@ -151,11 +149,6 @@ defmodule GroupherServer.CMS.Delegate.AbuseReport do
151149
end
152150
end
153151

154-
##############
155-
##############
156-
##############
157-
##############
158-
159152
defp not_reported_before(info, content_id, %User{login: login}) do
160153
query = from(r in AbuseReport, where: field(r, ^info.foreign_key) == ^content_id)
161154

@@ -176,8 +169,7 @@ defmodule GroupherServer.CMS.Delegate.AbuseReport do
176169
end
177170
end
178171

179-
defp result({:ok, %{create_abuse_report: result}}), do: result |> done()
180-
defp result({:ok, %{delete_abuse_report: result}}), do: result |> done()
172+
defp result({:ok, %{update_report_flag: result}}), do: result |> done()
181173

182174
defp result({:error, _, result, _steps}) do
183175
{:error, result}

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_web/resolvers/cms_resolver.ex

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -94,11 +94,16 @@ defmodule GroupherServerWeb.Resolvers.CMS do
9494
set_community_flags(community_id, thread, id, %{trash: false})
9595
end
9696

97+
def report_article(_root, ~m(thread id reason attr)a, %{context: %{cur_user: user}}) do
98+
CMS.report_article(thread, id, reason, attr, user)
99+
end
100+
101+
def undo_report_article(_root, ~m(thread id)a, %{context: %{cur_user: user}}) do
102+
CMS.undo_report_article(thread, id, user)
103+
end
104+
97105
# TODO: report contents
98-
# def report_content(_root, ~m(id thread community_id)a, _info),
99106
# do: set_community_flags(community_id, thread, id, %{report: true})
100-
101-
# def undo_report_content(_root, ~m(id thread community_id)a, _info),
102107
# do: set_community_flags(community_id, thread, id, %{report: false})
103108

104109
defp set_community_flags(community_id, thread, id, flag) do

lib/groupher_server_web/schema/Helper/mutations.ex

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,4 +124,28 @@ defmodule GroupherServerWeb.Schema.Helper.Mutations do
124124
end
125125
end
126126
end
127+
128+
defmacro article_report_mutation(thread) do
129+
quote do
130+
@desc unquote("report a #{thread}")
131+
field unquote(:"report_#{thread}"), unquote(thread) do
132+
arg(:id, non_null(:id))
133+
arg(:reason, non_null(:string))
134+
arg(:attr, :string, default_value: "")
135+
arg(:thread, unquote(:"#{thread}_thread"), default_value: unquote(thread))
136+
137+
middleware(M.Authorize, :login)
138+
resolve(&R.CMS.report_article/3)
139+
end
140+
141+
@desc unquote("undo report a #{thread}")
142+
field unquote(:"undo_report_#{thread}"), unquote(thread) do
143+
arg(:id, non_null(:id))
144+
arg(:thread, unquote(:"#{thread}_thread"), default_value: unquote(thread))
145+
146+
middleware(M.Authorize, :login)
147+
resolve(&R.CMS.undo_report_article/3)
148+
end
149+
end
150+
end
127151
end

lib/groupher_server_web/schema/cms/cms_types.ex

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,8 @@ defmodule GroupherServerWeb.Schema.CMS.Types do
6868
end)
6969
end
7070

71+
field(:is_reported, :boolean)
72+
7173
article_comments_fields()
7274
viewer_has_state_fields()
7375
# upvoted_count
@@ -109,6 +111,7 @@ defmodule GroupherServerWeb.Schema.CMS.Types do
109111
field(:finance, :string)
110112
field(:scale, :string)
111113

114+
field(:is_reported, :boolean)
112115
# comments_count
113116
# comments_participators
114117
article_comments_fields()
@@ -153,6 +156,7 @@ defmodule GroupherServerWeb.Schema.CMS.Types do
153156
field(:origial_community, :community, resolve: dataloader(CMS, :origial_community))
154157
field(:communities, list_of(:community), resolve: dataloader(CMS, :communities))
155158

159+
field(:is_reported, :boolean)
156160
viewer_has_state_fields()
157161
# comments_count
158162
# comments_participators

lib/groupher_server_web/schema/cms/mutations/job.ex

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ defmodule GroupherServerWeb.Schema.CMS.Mutations.Job do
7676
article_trash_mutation(:job)
7777
article_delete_mutation(:job)
7878
article_emotion_mutation(:job)
79+
article_report_mutation(:job)
7980
#############
8081
end
8182
end

lib/groupher_server_web/schema/cms/mutations/post.ex

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ defmodule GroupherServerWeb.Schema.CMS.Mutations.Post do
5050
article_trash_mutation(:post)
5151
article_delete_mutation(:post)
5252
article_emotion_mutation(:post)
53+
article_report_mutation(:post)
5354
#############
5455
end
5556
end

lib/groupher_server_web/schema/cms/mutations/repo.ex

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ defmodule GroupherServerWeb.Schema.CMS.Mutations.Repo do
7474
article_pin_mutation(:repo)
7575
article_trash_mutation(:repo)
7676
article_delete_mutation(:repo)
77+
article_report_mutation(:repo)
7778
#############
7879
end
7980
end
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
defmodule GroupherServer.Repo.Migrations.AddRepoToReport do
2+
use Ecto.Migration
3+
4+
def change do
5+
alter table(:abuse_reports) do
6+
add(:repo_id, references(:cms_repos, on_delete: :delete_all))
7+
end
8+
end
9+
end

0 commit comments

Comments
 (0)