8000 feat(api): report or block huddl organizers and group owners by mwoods79 · Pull Request #707 · huddlz-hq/huddlz · GitHub
[go: up one dir, main page]

Skip to content
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
4 changes: 4 additions & 0 deletions GLOSSARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@ _Avoid_: Bot, integration, app
**Account report**:
A confirmed member's request for administrators to review an account they can already see, including the organizer of a public huddl without joining its group or RSVPing, for spam or another concern. A report does not itself suspend the account; its details and the reporter's identity are available only to huddlz staff for review.

**Block**:
A person's private choice to stop seeing huddlz someone else organizes, made from a huddl whose organizer they can see. Blocked organizers' huddlz leave that person's discovery and group lists; huddlz they already RSVPed to and links they open still work. The blocked person is not told and loses nothing they could see before. A block is not a report.
_Avoid_: Mute, ban

## Communities and places

**Group**:
Expand Down
9 changes: 9 additions & 0 deletions lib/huddlz/accounts.ex
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,20 @@ defmodule Huddlz.Accounts do

resource Huddlz.Accounts.AccountReport do
define :report_account, action: :report
define :report_huddl_organizer, action: :report_organizer, args: [:huddl_id]
define :report_group_owner, action: :report_group, args: [:group_id]
define :get_account_report, action: :read, get_by: [:id]
define :count_account_reports, action: :count_queue, args: [:handled]
define :list_account_reports, action: :queue, args: [{:optional, :handled}]
define :mark_report_handled, action: :mark_handled
define :reopen_report, action: :reopen
end

resource Huddlz.Accounts.UserBlock do
define :block_huddl_organizer, action: :block_organizer, args: [:huddl_id]
define :block_group_owner, action: :block_group_owner, args: [:group_id]
define :list_blocks, action: :mine
define :unblock, action: :unblock
end
end
end
49 changes: 49 additions & 0 deletions lib/huddlz/accounts/account_report.ex
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ defmodule Huddlz.Accounts.AccountReport do
routes do
base "/account_reports"
post :report
route :post, "/organizer", :report_organizer
route :post, "/group", :report_group
end
end

Expand Down Expand Up @@ -84,6 +86,48 @@ defmodule Huddlz.Accounts.AccountReport do
return_skipped_upsert? true
end

action :report_organizer do
description """
Report the organizer of a huddl the actor can see, by naming the
huddl. It follows the same rules as reporting the account directly
and returns nothing about the account.
"""

argument :huddl_id, :uuid do
description "The huddl whose organizer to report"
allow_nil? false
end

argument :reason, Huddlz.Accounts.AccountReport.Reason

argument :details, :string do
constraints max_length: 1000, trim?: true
end

run Huddlz.Accounts.AccountReport.Actions.ReportOrganizer
end

action :report_group do
description """
Report the owner of a group the actor can see, by naming the group.
It follows the same rules as reporting the account directly and
returns nothing about the account.
"""

argument :group_id, :uuid do
description "The group whose owner to report"
allow_nil? false
end

argument :reason, Huddlz.Accounts.AccountReport.Reason

argument :details, :string do
constraints max_length: 1000, trim?: true
end

run Huddlz.Accounts.AccountReport.Actions.ReportGroup
end

read :queue do
description "The administrators' queue: open reports, or handled ones, newest first, expired ones gone"

Expand Down Expand Up @@ -137,6 +181,11 @@ defmodule Huddlz.Accounts.AccountReport do
end

policies do
policy action([:report_organizer, :report_group]) do
description "Signed-in people may ask; reporting t 10BC8 he account itself decides the rest"
authorize_if actor_present()
end

policy action(:report) do
description "Confirmed members report accounts they can already see, never themselves"
forbid_unless Huddlz.Accounts.Checks.ConfirmedActor
Expand Down
38 changes: 38 additions & 0 deletions lib/huddlz/accounts/account_report/actions/report_group.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
defmodule Huddlz.Accounts.AccountReport.Actions.ReportGroup do
@moduledoc """
Reports the owner of a group through the ordinary `:report` action,
so the same rules decide who may report whom. The reporter names only
the group and learns nothing about the account: the action returns no
report.
"""

use Ash.Resource.Actions.Implementation

alias Ash.Error.Query.NotFound
alias Huddlz.Accounts
alias Huddlz.Accounts.GroupOwner
alias Huddlz.Communities.Group

@impl true
def run(input, _opts, %{actor: actor}) do
group_id = input.arguments.group_id

with {:ok, owner_id} <- GroupOwner.fetch(group_id, actor),
{:ok, _report} <-
Accounts.report_account(
%{
reported_user_id: owner_id,
reason: Map.get(input.arguments, :reason),
details: Map.get(input.arguments, :details),
source_type: :group,
source_id: group_id
},
actor: actor
) do
:ok
else
:error -> {:error, NotFound.exception(resource: Group)}
{:error, error} -> {:error, error}
end
end
end
38 changes: 38 additions & 0 deletions lib/huddlz/accounts/account_report/actions/report_organizer.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
defmodule Huddlz.Accounts.AccountReport.Actions.ReportOrganizer do
@moduledoc """
Reports the organizer of a huddl through the ordinary `:report` action,
so the same rules decide who may report whom. The reporter names only
the huddl and learns nothing about the account: the action returns no
report.
"""

use Ash.Resource.Actions.Implementation

alias Ash.Error.Query.NotFound
alias Huddlz.Accounts
alias Huddlz.Accounts.HuddlOrganizer
alias Huddlz.Communities.Huddl

@impl true
def run(input, _opts, %{actor: actor}) do
huddl_id = input.arguments.huddl_id

with {:ok, organizer_id} <- HuddlOrganizer.fetch(huddl_id, actor),
{:ok, _report} <-
Accounts.report_account(
%{
reported_user_id: organizer_id,
reason: Map.get(input.arguments, :reason),
details: Map.get(input.arguments, :details),
source_type: :huddl,
source_id: huddl_id
},
actor: actor
) do
:ok
else
:error -> {:error, NotFound.exception(resource: Huddl)}
{:error, error} -> {:error, error}
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ defmodule Huddlz.Accounts.AccountReport.Checks.ReporterCanSeeAccount do
@moduledoc """
Reporting grants no new visibility: the reporter must already meet the
account on an ordinary member surface. That is a group they both belong
to, or a huddl they both go to, or a huddl the reporter goes to that
the account organizes, or a public huddl whose organizer they can see.
Nobody reports themselves.
to, a huddl they both go to, a huddl the reporter goes to that the
account organizes, a public huddl whose organizer they can see, or a
public group the account owns. Nobody reports themselves.
"""

use Ash.Policy.SimpleCheck

alias Huddlz.Communities.{GroupMember, Huddl, HuddlAttendee}
alias Huddlz.Communities.{Group, GroupMember, Huddl, HuddlAttendee}

require Ash.Query

Expand All @@ -21,17 +21,18 @@ defmodule Huddlz.Accounts.AccountReport.Checks.ReporterCanSeeAccount do
case Ash.Changeset.get_attribute(changeset, :reported_user_id) do
nil -> false
^actor_id -> false
reported_id -> visible?(actor_id, reported_id)
reported_id -> visible?(actor_id, reported_id, changeset)
end
end

def match?(_actor, _context, _opts), do: false

defp visible?(actor_id, reported_id) do
defp visible?(actor_id, reported_id, changeset) do
shares_group?(actor_id, reported_id) or
shares_huddl?(actor_id, reported_id) or
organizes_attended_huddl?(actor_id, reported_id) or
organizes_public_huddl?(reported_id)
organizes_public_huddl?(reported_id) or
owns_reported_public_group?(reported_id, changeset)
end

defp shares_group?(actor_id, reported_id) do
Expand Down Expand Up @@ -61,4 +62,16 @@ defmodule Huddlz.Accounts.AccountReport.Checks.ReporterCanSeeAccount do
|> Ash.Query.filter(creator_id == ^reported_id)
|> Ash.exists?()
end

defp owns_reported_public_group?(reported_id, changeset) do
with :group <- Ash.Changeset.get_attribute(changeset, :source_type),
group_id when is_binary(group_id) <- Ash.Changeset.get_attribute(changeset, :source_id) do
Group
|> Ash.Query.for_read(:read, %{}, actor: nil)
|> Ash.Query.filter(id == ^group_id and owner_id == ^reported_id)
|> Ash.exists?()
else
_ -> false
end
end
end
26 changes: 26 additions & 0 deletions lib/huddlz/accounts/group_owner.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
defmodule Huddlz.Accounts.GroupOwner do
@moduledoc """
Who owns a group the actor can see. Apps that never learn a person's
identity, like the iOS app, report and block "the owner of this group",
and this lookup turns that into the account behind it without putting
the account in any response.
"""

alias Huddlz.Communities.Group

require Ash.Query

@doc "The group owner's id, or `:error` when the actor cannot see the group."
def fetch(group_id, actor) when is_binary(group_id) do
Group
|> Ash.Query.for_read(:read, %{}, actor: actor)
|> Ash.Query.filter(id == ^group_id)
|> Ash.read_one()
|> case do
{:ok, %Group{owner_id: owner_id}} -> {:ok, owner_id}
_ -> :error
end
end

def fetch(_group_id, _actor), do: :error
end
26 changes: 26 additions & 0 deletions lib/huddlz/accounts/huddl_organizer.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
defmodule Huddlz.Accounts.HuddlOrganizer do
@moduledoc """
Who organized a huddl the actor can see. Apps that never learn a
person's identity, like the iOS app, report and block "the organizer of
this huddl", and this lookup turns that into the account behind it
without putting the account in any response.
"""

alias Huddlz.Communities.Huddl

require Ash.Query

@doc "The organizer's id, or `:error` when the actor cannot see the huddl."
def fetch(huddl_id, actor) when is_binary(huddl_id) do
Huddl
|> Ash.Query.for_read(:read, %{}, actor: actor)
|> Ash.Query.filter(id == ^huddl_id)
|> Ash.read_one()
|> case do
{:ok, %Huddl{creator_id: creator_id}} -> {:ok, creator_id}
_ -> :error
end
end

def fetch(_huddl_id, _actor), do: :error
end
Loading
0