Skip to content
This repository was archived by the owner on Nov 8, 2022. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions lib/groupher_server/cms/abuse_report.ex
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,18 @@ defmodule GroupherServer.CMS.AbuseReport do
import Ecto.Changeset

alias GroupherServer.{Accounts, CMS}
alias CMS.{ArticleComment, Embeds, Post, Job}
alias CMS.{ArticleComment, Embeds, Post, Job, Repo}

# @required_fields ~w(article_comment_id user_id recived_user_id)a
@optional_fields ~w(article_comment_id post_id job_id account_id operate_user_id deal_with is_closed report_cases_count)a
@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
@update_fields ~w(operate_user_id deal_with is_closed report_cases_count)a

@type t :: %AbuseReport{}
schema "abuse_reports" do
belongs_to(:article_comment, ArticleComment, foreign_key: :article_comment_id)
belongs_to(:post, Post, foreign_key: :post_id)
belongs_to(:job, Job, foreign_key: :job_id)
belongs_to(:repo, Repo, foreign_key: :repo_id)
belongs_to(:account, Accounts.User, foreign_key: :account_id)

embeds_many(:report_cases, Embeds.AbuseReportCase, on_replace: :delete)
Expand Down
11 changes: 7 additions & 4 deletions lib/groupher_server/cms/cms.ex
Original file line number Diff line number Diff line change
Expand Up @@ -143,8 +143,6 @@ defmodule GroupherServer.CMS do

defdelegate fold_article_comment(comment_id, user), to: ArticleCommentAction
defdelegate unfold_article_comment(comment_id, user), to: ArticleCommentAction
defdelegate report_article_comment(comment_id, user), to: ArticleCommentAction
defdelegate unreport_article_comment(comment_id, user), to: ArticleCommentAction

defdelegate emotion_to_comment(comment_id, args, user), to: ArticleCommentEmotion
defdelegate undo_emotion_to_comment(comment_id, args, user), to: ArticleCommentEmotion
Expand All @@ -161,8 +159,13 @@ defmodule GroupherServer.CMS do
defdelegate list_comments(thread, content_id, filters), to: CommentCURD
defdelegate list_comments_participators(thread, content_id, filters), to: CommentCURD

# report
defdelegate create_report(type, content_id, args, user), to: AbuseReport
# TODO: move report to abuse report module
defdelegate create_report(type, content_id, reason, attr, user), to: AbuseReport
defdelegate report_article(thread, article_id, reason, attr, user), to: AbuseReport
defdelegate undo_report_article(thread, article_id, user), to: AbuseReport
defdelegate list_reports(type, content_id, filter), to: AbuseReport
defdelegate report_article_comment(comment_id, reason, attr, user), to: ArticleCommentAction
defdelegate undo_report_article_comment(comment_id, user), to: AbuseReport

# Passport CURD
defdelegate stamp_passport(rules, user), to: PassportCURD
Expand Down
136 changes: 117 additions & 19 deletions lib/groupher_server/cms/delegates/abuse_report.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,58 +3,152 @@ defmodule GroupherServer.CMS.Delegate.AbuseReport do
CURD and operations for article comments
"""
import Ecto.Query, warn: false
# import Helper.Utils, only: [done: 1]
import Helper.Utils, only: [done: 1, strip_struct: 1]

import GroupherServer.CMS.Helper.Matcher2
# import ShortMaps
import ShortMaps

alias Helper.{ORM}
alias Helper.ORM
alias Helper.QueryBuilder
alias GroupherServer.{Accounts, CMS, Repo}

alias Accounts.User
alias CMS.{AbuseReport, Embeds}

# alias Accounts.User
alias Ecto.Multi

def create_report(type, content_id, %{reason: reason}, %User{} = user) do
@doc """
list paged reports for both comment and article
"""
def list_reports(type, content_id, %{page: page, size: size} = filter) do
with {:ok, info} <- match(type) do
query = from(r in AbuseReport, where: field(r, ^info.foreign_key) == ^content_id)

query
|> QueryBuilder.filter_pack(filter)
|> ORM.paginater(~m(page size)a)
|> done()
end
end

@doc """
report article content
"""
def report_article(thread, article_id, reason, attr, %User{} = user) do
with {:ok, info} <- match(thread),
{:ok, article} <- ORM.find(info.model, article_id) do
Multi.new()
|> Multi.run(:create_abuse_report, fn _, _ ->
create_report(thread, article_id, reason, attr, user)
end)
|> Multi.run(:update_report_flag, fn _, _ ->
update_report_meta(info, article, true)
end)
|> Repo.transaction()
|> result()
end
end

def undo_report_article_comment(comment_id, %User{} = user) do
undo_report_article(:article_comment, comment_id, user)
end

@doc """
undo report article content
"""
def undo_report_article(thread, article_id, %User{} = user) do
with {:ok, info} <- match(thread),
{:ok, article} <- ORM.find(info.model, article_id) do
Multi.new()
|> Multi.run(:delete_abuse_report, fn _, _ ->
delete_report(thread, article_id, user)
end)
|> Multi.run(:update_report_flag, fn _, _ ->
update_report_meta(info, article, false)
end)
|> Repo.transaction()
|> result()
end
end

def create_report(type, content_id, reason, attr, %User{} = user) do
with {:ok, info} <- match(type),
{:ok, report} <- not_reported_before(info, content_id, user) do
case report do
nil ->
updated_report_cases = [
report_cases = [
%{
reason: reason,
additional_reason: "additional_reason",
attr: attr,
user: %{login: user.login, nickname: user.nickname}
}
]

args =
%{report_cases_count: 1, report_cases: updated_report_cases}
%{report_cases_count: 1, report_cases: report_cases}
|> Map.put(info.foreign_key, content_id)

AbuseReport |> ORM.create(args)

_ ->
updated_report_cases =
user = %{login: user.login, nickname: user.nickname}

report_cases =
report.report_cases
|> List.insert_at(
length(report.report_cases),
%Embeds.AbuseReportCase{
reason: reason,
additional_reason: "additional_reason",
user: %{login: user.login, nickname: user.nickname}
}
%Embeds.AbuseReportCase{reason: reason, attr: attr, user: user}
)

report
|> Ecto.Changeset.change(%{report_cases_count: length(updated_report_cases)})
|> Ecto.Changeset.put_embed(:report_cases, updated_report_cases)
|> Ecto.Changeset.change(%{report_cases_count: length(report_cases)})
|> Ecto.Changeset.put_embed(:report_cases, report_cases)
|> Repo.update()
end
end
end

defp delete_report(thread, content_id, %User{} = user) do
with {:ok, info} <- match(thread),
{:error, _} <- not_reported_before(info, content_id, user),
{:ok, report} = ORM.find_by(AbuseReport, Map.put(%{}, info.foreign_key, content_id)) do
case length(report.report_cases) do
1 ->
ORM.delete(report)

_ ->
report_cases = report.report_cases |> Enum.reject(&(&1.user.login == user.login))

report
|> Ecto.Changeset.change(%{report_cases_count: length(report_cases)})
|> Ecto.Changeset.put_embed(:report_cases, report_cases)
|> Repo.update()
end
end
end

# update is_reported flag and reported_count in mete for article or comment
defp update_report_meta(info, content, is_reported) do
case ORM.find_by(AbuseReport, Map.put(%{}, info.foreign_key, content.id)) do
{:ok, record} ->
reported_count = record.report_cases |> length
meta = content.meta |> Map.merge(%{reported_count: reported_count}) |> strip_struct

content
|> Ecto.Changeset.change(%{is_reported: is_reported})
|> Ecto.Changeset.put_embed(:meta, meta)
|> Repo.update()

{:error, _} ->
meta = content.meta |> Map.merge(%{reported_count: 0}) |> strip_struct

content
|> Ecto.Changeset.change(%{is_reported: false})
|> Ecto.Changeset.put_embed(:meta, meta)
|> Repo.update()
end
end

defp not_reported_before(info, content_id, %User{login: login}) do
query = from(r in AbuseReport, where: field(r, ^info.foreign_key) == ^content_id)

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

if not reported_before,
do: {:ok, report},
else: {:error, "#{login} already reported"}
if not reported_before, do: {:ok, report}, else: {:error, "#{login} already reported"}
end
end

defp result({:ok, %{update_report_flag: result}}), do: result |> done()

defp result({:error, _, result, _steps}) do
{:error, result}
end
end
21 changes: 7 additions & 14 deletions lib/groupher_server/cms/delegates/article_comment_action.ex
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ defmodule GroupherServer.CMS.Delegate.ArticleCommentAction do
end
end

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

@doc "fold a comment"
@doc "unfold a comment"
def unfold_article_comment(comment_id, %User{} = _user) do
with {:ok, comment} <- ORM.find(ArticleComment, comment_id) do
comment |> ORM.update(%{is_folded: false})
end
end

@doc "fold a comment"
def report_article_comment(comment_id, %User{} = user) do
with {:ok, comment} <-
ORM.find(ArticleComment, comment_id) do
@doc "report a comment"
def report_article_comment(comment_id, reason, attr, %User{} = user) do
with {:ok, comment} <- ORM.find(ArticleComment, comment_id) do
Multi.new()
|> Multi.run(:create_abuse_report, fn _, _ ->
CMS.create_report(:article_comment, comment_id, %{reason: "todo fucked"}, user)
CMS.create_report(:article_comment, comment_id, reason, attr, user)
end)
|> Multi.run(:update_report_flag, fn _, _ ->
# TODO: update report count in meta
ORM.update(comment, %{is_reported: true})
end)
|> Multi.run(:fold_comment_report_too_many, fn _, %{create_abuse_report: abuse_report} ->
Expand All @@ -121,14 +122,6 @@ defmodule GroupherServer.CMS.Delegate.ArticleCommentAction do
end
end

@doc "fold a comment"
def unreport_article_comment(comment_id, %User{} = _user) do
with {:ok, comment} <-
ORM.find(ArticleComment, comment_id) do
comment |> ORM.update(%{is_reported: false})
end
end

@doc "reply to exsiting comment"
def reply_article_comment(comment_id, content, %User{} = user) do
with {:ok, target_comment} <-
Expand Down
6 changes: 6 additions & 0 deletions lib/groupher_server/cms/delegates/article_operation.ex
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,12 @@ defmodule GroupherServer.CMS.Delegate.ArticleOperation do
end
end

########
########
########
########
########

@doc """
trash / untrash articles
"""
Expand Down
6 changes: 4 additions & 2 deletions lib/groupher_server/cms/embeds/abuse_report_case.ex
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,19 @@ defmodule GroupherServer.CMS.Embeds.AbuseReportCase do
alias GroupherServer.CMS
alias CMS.Embeds

@optional_fields [:reason, :attr]

embedded_schema do
field(:reason, :string)
field(:additional_reason, :string)
field(:attr, :string)
embeds_one(:user, Embeds.User, on_replace: :delete)

timestamps(type: :utc_datetime)
end

def changeset(struct, params) do
struct
|> cast(params, [:reason, :additional_reason])
|> cast(params, @optional_fields)
|> cast_embed(:user, required: true, with: &Embeds.User.changeset/2)
end
end
6 changes: 4 additions & 2 deletions lib/groupher_server/cms/embeds/article_comment_meta.ex
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,15 @@ defmodule GroupherServer.CMS.Embeds.ArticleCommentMeta do

import Ecto.Changeset

@optional_fields ~w(is_article_author_upvoted is_solution report_count is_reply_to_others)a
@optional_fields ~w(is_article_author_upvoted is_solution report_count is_reply_to_others reported_count)a

@default_meta %{
is_article_author_upvoted: false,
is_solution: false,
is_reply_to_others: false,
report_count: 0,
upvoted_user_ids: []
upvoted_user_ids: [],
reported_count: 0
}

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

field(:upvoted_user_ids, {:array, :integer}, default: [])
field(:reported_count, :integer, default: 0)
end

def changeset(struct, params) do
Expand Down
8 changes: 4 additions & 4 deletions lib/groupher_server/cms/embeds/article_meta.ex
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,16 @@ defmodule GroupherServer.CMS.Embeds.ArticleMeta do
use Accessible
import Ecto.Changeset

@optional_fields ~w(is_edited is_comment_locked is_reported upvoted_user_ids collected_user_ids viewed_user_ids reported_user_ids)a
@optional_fields ~w(is_edited is_comment_locked upvoted_user_ids collected_user_ids viewed_user_ids reported_user_ids reported_count)a

@default_meta %{
is_edited: false,
is_comment_locked: false,
is_reported: false,
upvoted_user_ids: [],
collected_user_ids: [],
viewed_user_ids: [],
reported_user_ids: []
reported_user_ids: [],
reported_count: 0
}

@doc "for test usage"
Expand All @@ -24,12 +24,12 @@ defmodule GroupherServer.CMS.Embeds.ArticleMeta do
embedded_schema do
field(:is_edited, :boolean, default: false)
field(:is_comment_locked, :boolean, default: false)
field(:is_reported, :boolean, default: false)
# reaction history
field(:upvoted_user_ids, {:array, :integer}, default: [])
field(:collected_user_ids, {:array, :integer}, default: [])
field(:viewed_user_ids, {:array, :integer}, default: [])
field(:reported_user_ids, {:array, :integer}, default: [])
field(:reported_count, :integer, default: 0)
end

def changeset(struct, params) do
Expand Down
3 changes: 2 additions & 1 deletion lib/groupher_server/cms/job.ex
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ defmodule GroupherServer.CMS.Job do

@timestamps_opts [type: :utc_datetime_usec]
@required_fields ~w(title company company_logo body digest length)a
@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
@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

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

has_many(:upvotes, {"article_upvotes", ArticleUpvote})
field(:upvotes_count, :integer, default: 0)
Expand Down
Loading