@@ -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 )
0 commit comments