Even more fixes

Signed-off-by: Thomas Citharel <tcit@tcit.fr>
This commit is contained in:
Thomas Citharel
2020-06-15 19:41:11 +02:00
parent 2e85a4a3d3
commit ef6a1a21ac
17 changed files with 213 additions and 67 deletions

View File

@@ -111,6 +111,13 @@ defmodule Mobilizon.GraphQL.Resolvers.Admin do
}
end
defp transform_action_log(User, :delete, %ActionLog{changes: changes}) do
%{
action: :user_deletion,
object: convert_changes_to_struct(User, changes)
}
end
# Changes are stored as %{"key" => "value"} so we need to convert them back as struct
defp convert_changes_to_struct(struct, %{"report_id" => _report_id} = changes) do
with data <- for({key, val} <- changes, into: %{}, do: {String.to_atom(key), val}),

View File

@@ -110,6 +110,29 @@ defmodule Mobilizon.GraphQL.Resolvers.Event do
{:ok, stats.participant + stats.moderator + stats.administrator + stats.creator}
end
def stats_participants(
%Event{participant_stats: %EventParticipantStats{} = stats, id: event_id} = _event,
_args,
%{context: %{current_user: %User{id: user_id} = _user}} = _resolution
) do
if Events.is_user_moderator_for_event?(user_id, event_id) do
stats =
Map.put(
stats,
:going,
stats.participant + stats.moderator + stats.administrator + stats.creator
)
{:ok, stats}
else
{:ok, %EventParticipantStats{}}
end
end
def stats_participants(_event, _args, _resolution) do
{:ok, %EventParticipantStats{}}
end
@doc """
List related events
"""

View File

@@ -5,7 +5,7 @@ defmodule Mobilizon.GraphQL.Resolvers.User do
import Mobilizon.Users.Guards
alias Mobilizon.{Actors, Config, Events, Users}
alias Mobilizon.{Actors, Admin, Config, Events, Users}
alias Mobilizon.Actors.Actor
alias Mobilizon.Crypto
alias Mobilizon.Federation.ActivityPub
@@ -390,11 +390,20 @@ defmodule Mobilizon.GraphQL.Resolvers.User do
end
def delete_account(_parent, %{user_id: user_id}, %{
context: %{current_user: %User{role: role}}
context: %{current_user: %User{role: role} = moderator_user}
})
when is_moderator(role) do
with %User{} = user <- Users.get_user(user_id) do
do_delete_account(%User{} = user)
with {:moderator_actor, %Actor{} = moderator_actor} <-
{:moderator_actor, Users.get_actor_for_user(moderator_user)},
%User{disabled: false} = user <- Users.get_user(user_id),
{:ok, %User{}} <- do_delete_account(%User{} = user) do
Admin.log_action(moderator_actor, "delete", user)
else
{:moderator_actor, nil} ->
{:error, "No actor found for the moderator user"}
%User{disabled: true} ->
{:error, "User already disabled"}
end
end

View File

@@ -9,6 +9,7 @@ defmodule Mobilizon.GraphQL.Schema.AdminType do
alias Mobilizon.Conversations.Comment
alias Mobilizon.Events.Event
alias Mobilizon.Reports.{Note, Report}
alias Mobilizon.Users.User
alias Mobilizon.GraphQL.Resolvers.Admin
@@ -32,6 +33,7 @@ defmodule Mobilizon.GraphQL.Schema.AdminType do
value(:event_update)
value(:actor_suspension)
value(:actor_unsuspension)
value(:user_deletion)
end
@desc "The objects that can be in an action log"
@@ -54,6 +56,9 @@ defmodule Mobilizon.GraphQL.Schema.AdminType do
%Actor{type: "Person"}, _ ->
:person
%User{}, _ ->
:user
_, _ ->
nil
end)

View File

@@ -63,7 +63,10 @@ defmodule Mobilizon.GraphQL.Schema.EventType do
field(:draft, :boolean, description: "Whether or not the event is a draft")
field(:participant_stats, :participant_stats)
field(:participant_stats, :participant_stats,
description: "Statistics on the event",
resolve: &Event.stats_participants/3
)
field(:participants, :paginated_participant_list, description: "The event's participants") do
arg(:page, :integer, default_value: 1)
@@ -121,11 +124,7 @@ defmodule Mobilizon.GraphQL.Schema.EventType do
end
object :participant_stats do
field(:going, :integer,
description: "The number of approved participants",
resolve: &Event.stats_participants_going/3
)
field(:going, :integer, description: "The number of approved participants")
field(:not_approved, :integer, description: "The number of not approved participants")
field(:not_confirmed, :integer, description: "The number of not confirmed participants")
field(:rejected, :integer, description: "The number of rejected participants")

View File

@@ -15,7 +15,8 @@ defmodule Mobilizon.GraphQL.Schema.UserType do
@desc "A local user of Mobilizon"
object :user do
field(:id, non_null(:id), description: "The user's ID")
interfaces([:action_log_object])
field(:id, :id, description: "The user's ID")
field(:email, non_null(:string), description: "The user's email")
field(:actors, non_null(list_of(:person)),

View File

@@ -425,6 +425,16 @@ defmodule Mobilizon.Events do
|> Repo.all()
end
@spec is_user_moderator_for_event?(integer | String.t(), integer | String.t()) :: boolean
def is_user_moderator_for_event?(user_id, event_id) do
Participant
|> join(:inner, [p], a in Actor, on: p.actor_id == a.id)
|> where([p, _a], p.event_id == ^event_id)
|> where([_p, a], a.user_id == ^user_id)
|> where([p, _a], p.role == ^:creator)
|> Repo.exists?()
end
@doc """
Finds close events to coordinates.
Radius is in meters and defaults to 50km.
@@ -741,7 +751,8 @@ defmodule Mobilizon.Events do
|> Repo.one()
end
@default_participant_roles [:participant, :moderator, :administrator, :creator]
@moderator_roles [:moderator, :administrator, :creator]
@default_participant_roles [:participant] ++ @moderator_roles
@doc """
Returns the list of participants for an event.
@@ -810,7 +821,7 @@ defmodule Mobilizon.Events do
where:
p.event_id == ^event_id and
p.actor_id ==
^actor_id and p.role in ^[:moderator, :administrator, :creator]
^actor_id and p.role in ^@moderator_roles
)
) == nil)
end