Absinthe middleware actor provider

Signed-off-by: Thomas Citharel <tcit@tcit.fr>
This commit is contained in:
Thomas Citharel 2021-09-10 11:35:32 +02:00
parent ae97339353
commit 55e7696230
No known key found for this signature in database
GPG key ID: A061B9DDE0CA0773
29 changed files with 483 additions and 442 deletions

View file

@ -0,0 +1,40 @@
defmodule Mobilizon.GraphQL.Middleware.CurrentActorProvider do
@moduledoc """
Absinthe Error Handler
"""
alias Mobilizon.Actors.Actor
alias Mobilizon.Users
alias Mobilizon.Users.User
@behaviour Absinthe.Middleware
@impl Absinthe.Middleware
@spec call(Absinthe.Resolution.t(), any) :: Absinthe.Resolution.t()
def call(
%Absinthe.Resolution{context: %{current_user: %User{id: user_id} = user} = context} =
resolution,
_config
) do
case Cachex.fetch(:default_actors, to_string(user_id), fn -> default(user) end) do
{status, %Actor{} = current_actor} when status in [:ok, :commit] ->
context = Map.put(context, :current_actor, current_actor)
%Absinthe.Resolution{resolution | context: context}
{_, nil} ->
resolution
end
end
def call(%Absinthe.Resolution{} = resolution, _config), do: resolution
@spec default(User.t()) :: {:commit, Actor.t()} | {:ignore, nil}
defp default(%User{} = user) do
case Users.get_actor_for_user(user) do
%Actor{} = actor ->
{:commit, actor}
nil ->
{:ignore, nil}
end
end
end

View file

@ -4,7 +4,7 @@ defmodule Mobilizon.GraphQL.Resolvers.Activity do
""" """
import Mobilizon.Users.Guards import Mobilizon.Users.Guards
alias Mobilizon.{Activities, Actors, Users} alias Mobilizon.{Activities, Actors}
alias Mobilizon.Actors.Actor alias Mobilizon.Actors.Actor
alias Mobilizon.Service.Activity.Utils alias Mobilizon.Service.Activity.Utils
alias Mobilizon.Storage.Page alias Mobilizon.Storage.Page
@ -13,10 +13,8 @@ defmodule Mobilizon.GraphQL.Resolvers.Activity do
require Logger require Logger
def group_activity(%Actor{type: :Group, id: group_id}, %{page: page, limit: limit} = args, %{ def group_activity(%Actor{type: :Group, id: group_id}, %{page: page, limit: limit} = args, %{
context: %{current_user: %User{role: role} = user} context: %{current_user: %User{role: role}, current_actor: %Actor{id: actor_id}}
}) do }) do
case Users.get_actor_for_user(user) do
%Actor{id: actor_id} = _actor ->
if Actors.is_member?(actor_id, group_id) or is_moderator(role) do if Actors.is_member?(actor_id, group_id) or is_moderator(role) do
%Page{total: total, elements: elements} = %Page{total: total, elements: elements} =
Activities.list_group_activities_for_member( Activities.list_group_activities_for_member(
@ -33,10 +31,6 @@ defmodule Mobilizon.GraphQL.Resolvers.Activity do
else else
{:error, :unauthorized} {:error, :unauthorized}
end end
nil ->
{:error, :user_actor_not_found}
end
end end
def group_activity(_, _, _) do def group_activity(_, _, _) do

View file

@ -4,7 +4,7 @@ defmodule Mobilizon.GraphQL.Resolvers.Actor do
""" """
import Mobilizon.Users.Guards import Mobilizon.Users.Guards
alias Mobilizon.{Actors, Admin, Users} alias Mobilizon.{Actors, Admin}
alias Mobilizon.Actors.Actor alias Mobilizon.Actors.Actor
alias Mobilizon.Federation.ActivityPub alias Mobilizon.Federation.ActivityPub
alias Mobilizon.Service.Workers.Background alias Mobilizon.Service.Workers.Background
@ -32,12 +32,14 @@ defmodule Mobilizon.GraphQL.Resolvers.Actor do
end end
def suspend_profile(_parent, %{id: id}, %{ def suspend_profile(_parent, %{id: id}, %{
context: %{current_user: %User{role: role} = user} context: %{
current_user: %User{role: role},
current_actor: %Actor{} = moderator_actor
}
}) })
when is_moderator(role) do when is_moderator(role) do
with {:moderator_actor, %Actor{} = moderator_actor} <- case Actors.get_actor_with_preload(id) do
{:moderator_actor, Users.get_actor_for_user(user)}, %Actor{suspended: false} = actor ->
%Actor{suspended: false} = actor <- Actors.get_actor_with_preload(id) do
case actor do case actor do
# Suspend a group on this instance # Suspend a group on this instance
%Actor{type: :Group, domain: nil} -> %Actor{type: :Group, domain: nil} ->
@ -56,15 +58,9 @@ defmodule Mobilizon.GraphQL.Resolvers.Actor do
%Actor{domain: nil} -> %Actor{domain: nil} ->
{:error, dgettext("errors", "No remote profile found with this ID")} {:error, dgettext("errors", "No remote profile found with this ID")}
end end
else
{:moderator_actor, nil} ->
{:error, dgettext("errors", "No profile found for the moderator user")}
%Actor{suspended: true} -> %Actor{suspended: true} ->
{:error, dgettext("errors", "Profile already suspended")} {:error, dgettext("errors", "Profile already suspended")}
{:error, _} ->
{:error, dgettext("errors", "Error while performing background task")}
end end
end end
@ -73,12 +69,13 @@ defmodule Mobilizon.GraphQL.Resolvers.Actor do
end end
def unsuspend_profile(_parent, %{id: id}, %{ def unsuspend_profile(_parent, %{id: id}, %{
context: %{current_user: %User{role: role} = user} context: %{
current_user: %User{role: role},
current_actor: %Actor{} = moderator_actor
}
}) })
when is_moderator(role) do when is_moderator(role) do
with {:moderator_actor, %Actor{} = moderator_actor} <- with %Actor{suspended: true} = actor <-
{:moderator_actor, Users.get_actor_for_user(user)},
%Actor{suspended: true} = actor <-
Actors.get_actor_with_preload(id, true), Actors.get_actor_with_preload(id, true),
{:delete_tombstones, {_, nil}} <- {:delete_tombstones, {_, nil}} <-
{:delete_tombstones, Mobilizon.Tombstone.delete_actor_tombstones(id)}, {:delete_tombstones, Mobilizon.Tombstone.delete_actor_tombstones(id)},

View file

@ -3,7 +3,7 @@ defmodule Mobilizon.GraphQL.Resolvers.Comment do
Handles the comment-related GraphQL calls. Handles the comment-related GraphQL calls.
""" """
alias Mobilizon.{Actors, Admin, Discussions, Events, Users} alias Mobilizon.{Actors, Admin, Discussions, Events}
alias Mobilizon.Actors.Actor alias Mobilizon.Actors.Actor
alias Mobilizon.Discussions.Comment, as: CommentModel alias Mobilizon.Discussions.Comment, as: CommentModel
alias Mobilizon.Events.{Event, EventOptions} alias Mobilizon.Events.{Event, EventOptions}
@ -23,12 +23,11 @@ defmodule Mobilizon.GraphQL.Resolvers.Comment do
%{event_id: event_id} = args, %{event_id: event_id} = args,
%{ %{
context: %{ context: %{
current_user: %User{} = user current_actor: %Actor{id: actor_id}
} }
} }
) do ) do
with %Actor{id: actor_id} <- Users.get_actor_for_user(user), with {:find_event,
{:find_event,
{:ok, {:ok,
%Event{ %Event{
options: %EventOptions{comment_moderation: comment_moderation}, options: %EventOptions{comment_moderation: comment_moderation},
@ -59,12 +58,11 @@ defmodule Mobilizon.GraphQL.Resolvers.Comment do
%{text: text, comment_id: comment_id}, %{text: text, comment_id: comment_id},
%{ %{
context: %{ context: %{
current_user: %User{} = user current_actor: %Actor{id: actor_id}
} }
} }
) do ) do
with {:actor, %Actor{id: actor_id} = _actor} <- {:actor, Users.get_actor_for_user(user)}, with %CommentModel{actor_id: comment_actor_id} = comment <-
%CommentModel{actor_id: comment_actor_id} = comment <-
Mobilizon.Discussions.get_comment_with_preload(comment_id), Mobilizon.Discussions.get_comment_with_preload(comment_id),
true <- actor_id == comment_actor_id, true <- actor_id == comment_actor_id,
{:ok, _, %CommentModel{} = comment} <- Comments.update_comment(comment, %{text: text}) do {:ok, _, %CommentModel{} = comment} <- Comments.update_comment(comment, %{text: text}) do
@ -81,13 +79,13 @@ defmodule Mobilizon.GraphQL.Resolvers.Comment do
%{comment_id: comment_id}, %{comment_id: comment_id},
%{ %{
context: %{ context: %{
current_user: %User{role: role} = user current_user: %User{role: role},
current_actor: %Actor{id: actor_id} = actor
} }
} }
) do ) do
with {:actor, %Actor{id: actor_id} = actor} <- {:actor, Users.get_actor_for_user(user)}, case Discussions.get_comment_with_preload(comment_id) do
%CommentModel{deleted_at: nil} = comment <- %CommentModel{deleted_at: nil} = comment ->
Discussions.get_comment_with_preload(comment_id) do
cond do cond do
{:comment_can_be_managed, true} == CommentModel.can_be_managed_by(comment, actor_id) -> {:comment_can_be_managed, true} == CommentModel.can_be_managed_by(comment, actor_id) ->
do_delete_comment(comment, actor) do_delete_comment(comment, actor)
@ -103,9 +101,12 @@ defmodule Mobilizon.GraphQL.Resolvers.Comment do
true -> true ->
{:error, dgettext("errors", "You cannot delete this comment")} {:error, dgettext("errors", "You cannot delete this comment")}
end end
else
%CommentModel{deleted_at: deleted_at} when not is_nil(deleted_at) -> %CommentModel{deleted_at: deleted_at} when not is_nil(deleted_at) ->
{:error, dgettext("errors", "Comment is already deleted")} {:error, dgettext("errors", "Comment is already deleted")}
nil ->
{:error, dgettext("errors", "Comment not found")}
end end
end end

View file

@ -3,7 +3,7 @@ defmodule Mobilizon.GraphQL.Resolvers.Discussion do
Handles the group-related GraphQL calls. Handles the group-related GraphQL calls.
""" """
alias Mobilizon.{Actors, Discussions, Users} alias Mobilizon.{Actors, Discussions}
alias Mobilizon.Actors.Actor alias Mobilizon.Actors.Actor
alias Mobilizon.Discussions.{Comment, Discussion} alias Mobilizon.Discussions.{Comment, Discussion}
alias Mobilizon.Federation.ActivityPub alias Mobilizon.Federation.ActivityPub
@ -17,12 +17,11 @@ defmodule Mobilizon.GraphQL.Resolvers.Discussion do
%{page: page, limit: limit}, %{page: page, limit: limit},
%{ %{
context: %{ context: %{
current_user: %User{} = user current_actor: %Actor{id: actor_id}
} }
} }
) do ) do
with {:actor, %Actor{id: actor_id} = _actor} <- {:actor, Users.get_actor_for_user(user)}, with {:member, true} <- {:member, Actors.is_member?(actor_id, group_id)},
{:member, true} <- {:member, Actors.is_member?(actor_id, group_id)},
{:ok, %Actor{type: :Group} = group} <- Actors.get_group_by_actor_id(group_id) do {:ok, %Actor{type: :Group} = group} <- Actors.get_group_by_actor_id(group_id) do
{:ok, Discussions.find_discussions_for_actor(group, page, limit)} {:ok, Discussions.find_discussions_for_actor(group, page, limit)}
else else
@ -37,11 +36,10 @@ defmodule Mobilizon.GraphQL.Resolvers.Discussion do
def get_discussion(_parent, %{id: id}, %{ def get_discussion(_parent, %{id: id}, %{
context: %{ context: %{
current_user: %User{} = user current_actor: %Actor{id: creator_id}
} }
}) do }) do
with {:actor, %Actor{id: creator_id} = _actor} <- {:actor, Users.get_actor_for_user(user)}, with %Discussion{actor_id: actor_id} = discussion <-
%Discussion{actor_id: actor_id} = discussion <-
Discussions.get_discussion(id), Discussions.get_discussion(id),
{:member, true} <- {:member, Actors.is_member?(creator_id, actor_id)} do {:member, true} <- {:member, Actors.is_member?(creator_id, actor_id)} do
{:ok, discussion} {:ok, discussion}
@ -50,11 +48,10 @@ defmodule Mobilizon.GraphQL.Resolvers.Discussion do
def get_discussion(_parent, %{slug: slug}, %{ def get_discussion(_parent, %{slug: slug}, %{
context: %{ context: %{
current_user: %User{} = user current_actor: %Actor{id: creator_id}
} }
}) do }) do
with {:actor, %Actor{id: creator_id} = _actor} <- {:actor, Users.get_actor_for_user(user)}, with %Discussion{actor_id: actor_id} = discussion <-
%Discussion{actor_id: actor_id} = discussion <-
Discussions.get_discussion_by_slug(slug), Discussions.get_discussion_by_slug(slug),
{:member, true} <- {:member, Actors.is_member?(creator_id, actor_id)} do {:member, true} <- {:member, Actors.is_member?(creator_id, actor_id)} do
{:ok, discussion} {:ok, discussion}
@ -89,12 +86,11 @@ defmodule Mobilizon.GraphQL.Resolvers.Discussion do
%{title: title, text: text, actor_id: group_id}, %{title: title, text: text, actor_id: group_id},
%{ %{
context: %{ context: %{
current_user: %User{} = user current_actor: %Actor{id: creator_id}
} }
} }
) do ) do
with {:actor, %Actor{id: creator_id} = _actor} <- {:actor, Users.get_actor_for_user(user)}, with {:member, true} <- {:member, Actors.is_member?(creator_id, group_id)},
{:member, true} <- {:member, Actors.is_member?(creator_id, group_id)},
{:ok, _activity, %Discussion{} = discussion} <- {:ok, _activity, %Discussion{} = discussion} <-
Comments.create_discussion(%{ Comments.create_discussion(%{
title: title, title: title,
@ -120,12 +116,11 @@ defmodule Mobilizon.GraphQL.Resolvers.Discussion do
%{text: text, discussion_id: discussion_id}, %{text: text, discussion_id: discussion_id},
%{ %{
context: %{ context: %{
current_user: %User{} = user current_actor: %Actor{id: creator_id}
} }
} }
) do ) do
with {:actor, %Actor{id: creator_id} = _actor} <- {:actor, Users.get_actor_for_user(user)}, with {:no_discussion,
{:no_discussion,
%Discussion{ %Discussion{
actor_id: actor_id, actor_id: actor_id,
last_comment: %Comment{ last_comment: %Comment{
@ -161,12 +156,11 @@ defmodule Mobilizon.GraphQL.Resolvers.Discussion do
%{title: title, discussion_id: discussion_id}, %{title: title, discussion_id: discussion_id},
%{ %{
context: %{ context: %{
current_user: %User{} = user current_actor: %Actor{id: creator_id}
} }
} }
) do ) do
with {:actor, %Actor{id: creator_id} = _actor} <- {:actor, Users.get_actor_for_user(user)}, with {:no_discussion, %Discussion{actor_id: actor_id} = discussion} <-
{:no_discussion, %Discussion{actor_id: actor_id} = discussion} <-
{:no_discussion, Discussions.get_discussion(discussion_id)}, {:no_discussion, Discussions.get_discussion(discussion_id)},
{:member, true} <- {:member, Actors.is_member?(creator_id, actor_id)}, {:member, true} <- {:member, Actors.is_member?(creator_id, actor_id)},
{:ok, _activity, %Discussion{} = discussion} <- {:ok, _activity, %Discussion{} = discussion} <-
@ -187,11 +181,11 @@ defmodule Mobilizon.GraphQL.Resolvers.Discussion do
def delete_discussion(_parent, %{discussion_id: discussion_id}, %{ def delete_discussion(_parent, %{discussion_id: discussion_id}, %{
context: %{ context: %{
current_user: %User{} = user current_user: %User{},
current_actor: %Actor{id: creator_id} = actor
} }
}) do }) do
with {:actor, %Actor{id: creator_id} = actor} <- {:actor, Users.get_actor_for_user(user)}, with {:no_discussion, %Discussion{actor_id: actor_id} = discussion} <-
{:no_discussion, %Discussion{actor_id: actor_id} = discussion} <-
{:no_discussion, Discussions.get_discussion(discussion_id)}, {:no_discussion, Discussions.get_discussion(discussion_id)},
{:member, true} <- {:member, Actors.is_member?(creator_id, actor_id)}, {:member, true} <- {:member, Actors.is_member?(creator_id, actor_id)},
{:ok, _activity, %Discussion{} = discussion} <- {:ok, _activity, %Discussion{} = discussion} <-

View file

@ -3,7 +3,7 @@ defmodule Mobilizon.GraphQL.Resolvers.Event do
Handles the event-related GraphQL calls. Handles the event-related GraphQL calls.
""" """
alias Mobilizon.{Actors, Admin, Events, Users} alias Mobilizon.{Actors, Admin, Events}
alias Mobilizon.Actors.Actor alias Mobilizon.Actors.Actor
alias Mobilizon.Config alias Mobilizon.Config
alias Mobilizon.Events.{Event, EventParticipantStats} alias Mobilizon.Events.{Event, EventParticipantStats}
@ -24,11 +24,12 @@ defmodule Mobilizon.GraphQL.Resolvers.Event do
def organizer_for_event( def organizer_for_event(
%Event{attributed_to_id: attributed_to_id, organizer_actor_id: organizer_actor_id}, %Event{attributed_to_id: attributed_to_id, organizer_actor_id: organizer_actor_id},
_args, _args,
%{context: %{current_user: %User{role: user_role} = user}} = _resolution %{
context: %{current_user: %User{role: user_role}, current_actor: %Actor{id: actor_id}}
} = _resolution
) )
when not is_nil(attributed_to_id) do when not is_nil(attributed_to_id) do
with %Actor{id: group_id} <- Actors.get_actor(attributed_to_id), with %Actor{id: group_id} <- Actors.get_actor(attributed_to_id),
%Actor{id: actor_id} <- Users.get_actor_for_user(user),
{:member, true} <- {:member, true} <-
{:member, Actors.is_member?(actor_id, group_id) or is_moderator(user_role)}, {:member, Actors.is_member?(actor_id, group_id) or is_moderator(user_role)},
%Actor{} = actor <- Actors.get_actor(organizer_actor_id) do %Actor{} = actor <- Actors.get_actor(organizer_actor_id) do
@ -77,10 +78,8 @@ defmodule Mobilizon.GraphQL.Resolvers.Event do
defp find_private_event( defp find_private_event(
_parent, _parent,
%{uuid: uuid}, %{uuid: uuid},
%{context: %{current_user: %User{} = user}} = _resolution %{context: %{current_actor: %Actor{} = profile}} = _resolution
) do ) do
%Actor{} = profile = Users.get_actor_for_user(user)
case Events.get_event_by_uuid_with_preload(uuid) do case Events.get_event_by_uuid_with_preload(uuid) do
# Event attributed to group # Event attributed to group
%Event{attributed_to: %Actor{}} = event -> %Event{attributed_to: %Actor{}} = event ->
@ -136,12 +135,10 @@ defmodule Mobilizon.GraphQL.Resolvers.Event do
def list_participants_for_event( def list_participants_for_event(
%Event{id: event_id} = event, %Event{id: event_id} = event,
%{page: page, limit: limit, roles: roles}, %{page: page, limit: limit, roles: roles},
%{context: %{current_user: %User{} = user}} = _resolution %{context: %{current_actor: %Actor{} = actor}} = _resolution
) do ) do
with %Actor{} = actor <- Users.get_actor_for_user(user),
# Check that moderator has right # Check that moderator has right
{:event_can_be_managed, true} <- if can_event_be_updated_by?(event, actor) do
{:event_can_be_managed, can_event_be_updated_by?(event, actor)} do
roles = roles =
case roles do case roles do
nil -> nil ->
@ -160,7 +157,6 @@ defmodule Mobilizon.GraphQL.Resolvers.Event do
participants = Events.list_participants_for_event(event_id, roles, page, limit) participants = Events.list_participants_for_event(event_id, roles, page, limit)
{:ok, participants} {:ok, participants}
else else
{:event_can_be_managed, _} ->
{:error, {:error,
dgettext("errors", "Provided profile doesn't have moderator permissions on this event")} dgettext("errors", "Provided profile doesn't have moderator permissions on this event")}
end end
@ -290,13 +286,12 @@ defmodule Mobilizon.GraphQL.Resolvers.Event do
def update_event( def update_event(
_parent, _parent,
%{event_id: event_id} = args, %{event_id: event_id} = args,
%{context: %{current_user: %User{} = user}} = _resolution %{context: %{current_user: %User{} = user, current_actor: %Actor{} = actor}} = _resolution
) do ) do
# See https://github.com/absinthe-graphql/absinthe/issues/490 # See https://github.com/absinthe-graphql/absinthe/issues/490
args = Map.put(args, :options, args[:options] || %{}) args = Map.put(args, :options, args[:options] || %{})
with {:ok, %Event{} = event} <- Events.get_event_with_preload(event_id), with {:ok, %Event{} = event} <- Events.get_event_with_preload(event_id),
%Actor{} = actor <- Users.get_actor_for_user(user),
{:ok, args} <- verify_profile_change(args, event, user, actor), {:ok, args} <- verify_profile_change(args, event, user, actor),
{:event_can_be_managed, true} <- {:event_can_be_managed, true} <-
{:event_can_be_managed, can_event_be_updated_by?(event, actor)}, {:event_can_be_managed, can_event_be_updated_by?(event, actor)},
@ -335,10 +330,15 @@ defmodule Mobilizon.GraphQL.Resolvers.Event do
def delete_event( def delete_event(
_parent, _parent,
%{event_id: event_id}, %{event_id: event_id},
%{context: %{current_user: %User{role: role} = user}} %{
context: %{
current_user: %User{role: role},
current_actor: %Actor{id: actor_id} = actor
}
}
) do ) do
with {:ok, %Event{local: is_local} = event} <- Events.get_event_with_preload(event_id), case Events.get_event_with_preload(event_id) do
%Actor{id: actor_id} = actor <- Users.get_actor_for_user(user) do {:ok, %Event{local: is_local} = event} ->
cond do cond do
{:event_can_be_managed, true} == {:event_can_be_managed, true} ==
{:event_can_be_managed, can_event_be_deleted_by?(event, actor)} -> {:event_can_be_managed, can_event_be_deleted_by?(event, actor)} ->
@ -355,7 +355,7 @@ defmodule Mobilizon.GraphQL.Resolvers.Event do
true -> true ->
{:error, dgettext("errors", "You cannot delete this event")} {:error, dgettext("errors", "You cannot delete this event")}
end end
else
{:error, :event_not_found} -> {:error, :event_not_found} ->
{:error, dgettext("errors", "Event not found")} {:error, dgettext("errors", "Event not found")}
end end

View file

@ -4,7 +4,7 @@ defmodule Mobilizon.GraphQL.Resolvers.Followers do
""" """
import Mobilizon.Users.Guards import Mobilizon.Users.Guards
alias Mobilizon.{Actors, Users} alias Mobilizon.Actors
alias Mobilizon.Actors.{Actor, Follower} alias Mobilizon.Actors.{Actor, Follower}
alias Mobilizon.Federation.ActivityPub alias Mobilizon.Federation.ActivityPub
alias Mobilizon.Storage.Page alias Mobilizon.Storage.Page
@ -16,17 +16,16 @@ defmodule Mobilizon.GraphQL.Resolvers.Followers do
%{page: page, limit: limit} = args, %{page: page, limit: limit} = args,
%{ %{
context: %{ context: %{
current_user: %User{role: user_role} = user current_user: %User{role: user_role},
current_actor: %Actor{id: actor_id}
} }
} }
) do ) do
with {:actor, %Actor{id: actor_id} = _actor} <- {:actor, Users.get_actor_for_user(user)}, if Actors.is_moderator?(actor_id, group_id) or is_moderator(user_role) do
{:member, true} <-
{:member, Actors.is_moderator?(actor_id, group_id) or is_moderator(user_role)} do
{:ok, {:ok,
Actors.list_paginated_followers_for_actor(group, Map.get(args, :approved), page, limit)} Actors.list_paginated_followers_for_actor(group, Map.get(args, :approved), page, limit)}
else else
_ -> {:error, :unauthorized} {:error, :unauthorized}
end end
end end
@ -35,11 +34,10 @@ defmodule Mobilizon.GraphQL.Resolvers.Followers do
@spec update_follower(any(), map(), map()) :: {:ok, Follower.t()} | {:error, any()} @spec update_follower(any(), map(), map()) :: {:ok, Follower.t()} | {:error, any()}
def update_follower(_, %{id: follower_id, approved: approved}, %{ def update_follower(_, %{id: follower_id, approved: approved}, %{
context: %{ context: %{
current_user: %User{} = user current_actor: %Actor{id: actor_id}
} }
}) do }) do
with {:actor, %Actor{id: actor_id} = _actor} <- {:actor, Users.get_actor_for_user(user)}, with %Follower{target_actor: %Actor{type: :Group, id: group_id}} = follower <-
%Follower{target_actor: %Actor{type: :Group, id: group_id}} = follower <-
Actors.get_follower(follower_id), Actors.get_follower(follower_id),
{:member, true} <- {:member, true} <-
{:member, Actors.is_moderator?(actor_id, group_id)}, {:member, Actors.is_moderator?(actor_id, group_id)},

View file

@ -4,7 +4,7 @@ defmodule Mobilizon.GraphQL.Resolvers.Group do
""" """
import Mobilizon.Users.Guards import Mobilizon.Users.Guards
alias Mobilizon.{Actors, Events, Users} alias Mobilizon.{Actors, Events}
alias Mobilizon.Actors.{Actor, Member} alias Mobilizon.Actors.{Actor, Member}
alias Mobilizon.Federation.ActivityPub alias Mobilizon.Federation.ActivityPub
alias Mobilizon.Federation.ActivityPub.Actor, as: ActivityPubActor alias Mobilizon.Federation.ActivityPub.Actor, as: ActivityPubActor
@ -23,13 +23,12 @@ defmodule Mobilizon.GraphQL.Resolvers.Group do
%{preferred_username: name} = args, %{preferred_username: name} = args,
%{ %{
context: %{ context: %{
current_user: %User{} = user current_actor: %Actor{id: actor_id}
} }
} }
) do ) do
with {:group, {:ok, %Actor{id: group_id, suspended: false} = group}} <- with {:group, {:ok, %Actor{id: group_id, suspended: false} = group}} <-
{:group, ActivityPubActor.find_or_make_group_from_nickname(name)}, {:group, ActivityPubActor.find_or_make_group_from_nickname(name)},
{:actor, %Actor{id: actor_id} = _actor} <- {:actor, Users.get_actor_for_user(user)},
{:member, true} <- {:member, Actors.is_member?(actor_id, group_id)} do {:member, true} <- {:member, Actors.is_member?(actor_id, group_id)} do
{:ok, group} {:ok, group}
else else
@ -119,12 +118,11 @@ defmodule Mobilizon.GraphQL.Resolvers.Group do
args, args,
%{ %{
context: %{ context: %{
current_user: user current_actor: %Actor{id: creator_actor_id} = creator_actor
} }
} }
) do ) do
with %Actor{id: creator_actor_id} = creator_actor <- Users.get_actor_for_user(user), with args when is_map(args) <- Map.update(args, :preferred_username, "", &String.downcase/1),
args when is_map(args) <- Map.update(args, :preferred_username, "", &String.downcase/1),
args when is_map(args) <- Map.put(args, :creator_actor, creator_actor), args when is_map(args) <- Map.put(args, :creator_actor, creator_actor),
args when is_map(args) <- Map.put(args, :creator_actor_id, creator_actor_id), args when is_map(args) <- Map.put(args, :creator_actor_id, creator_actor_id),
{:picture, args} when is_map(args) <- {:picture, save_attached_pictures(args)}, {:picture, args} when is_map(args) <- {:picture, save_attached_pictures(args)},
@ -152,12 +150,11 @@ defmodule Mobilizon.GraphQL.Resolvers.Group do
%{id: group_id} = args, %{id: group_id} = args,
%{ %{
context: %{ context: %{
current_user: %User{} = user current_actor: %Actor{} = updater_actor
} }
} }
) do ) do
with %Actor{} = updater_actor <- Users.get_actor_for_user(user), with {:administrator, true} <-
{:administrator, true} <-
{:administrator, Actors.is_administrator?(updater_actor.id, group_id)}, {:administrator, Actors.is_administrator?(updater_actor.id, group_id)},
args when is_map(args) <- Map.put(args, :updater_actor, updater_actor), args when is_map(args) <- Map.put(args, :updater_actor, updater_actor),
{:picture, args} when is_map(args) <- {:picture, save_attached_pictures(args)}, {:picture, args} when is_map(args) <- {:picture, save_attached_pictures(args)},
@ -188,12 +185,11 @@ defmodule Mobilizon.GraphQL.Resolvers.Group do
%{group_id: group_id}, %{group_id: group_id},
%{ %{
context: %{ context: %{
current_user: user current_actor: %Actor{id: actor_id} = actor
} }
} }
) do ) do
with %Actor{id: actor_id} = actor <- Users.get_actor_for_user(user), with {:ok, %Actor{} = group} <- Actors.get_group_by_actor_id(group_id),
{:ok, %Actor{} = group} <- Actors.get_group_by_actor_id(group_id),
{:ok, %Member{} = member} <- Actors.get_member(actor_id, group.id), {:ok, %Member{} = member} <- Actors.get_member(actor_id, group.id),
{:is_admin, true} <- {:is_admin, Member.is_administrator(member)}, {:is_admin, true} <- {:is_admin, Member.is_administrator(member)},
{:ok, _activity, group} <- ActivityPub.delete(group, actor, true) do {:ok, _activity, group} <- ActivityPub.delete(group, actor, true) do
@ -219,10 +215,9 @@ defmodule Mobilizon.GraphQL.Resolvers.Group do
Join an existing group Join an existing group
""" """
def join_group(_parent, %{group_id: group_id} = args, %{ def join_group(_parent, %{group_id: group_id} = args, %{
context: %{current_user: %User{} = user} context: %{current_actor: %Actor{} = actor}
}) do }) do
with %Actor{} = actor <- Users.get_actor_for_user(user), with {:ok, %Actor{type: :Group} = group} <-
{:ok, %Actor{type: :Group} = group} <-
Actors.get_group_by_actor_id(group_id), Actors.get_group_by_actor_id(group_id),
{:error, :member_not_found} <- Actors.get_member(actor.id, group.id), {:error, :member_not_found} <- Actors.get_member(actor.id, group.id),
{:is_able_to_join, true} <- {:is_able_to_join, Member.can_be_joined(group)}, {:is_able_to_join, true} <- {:is_able_to_join, Member.can_be_joined(group)},
@ -253,12 +248,11 @@ defmodule Mobilizon.GraphQL.Resolvers.Group do
%{group_id: group_id}, %{group_id: group_id},
%{ %{
context: %{ context: %{
current_user: %User{} = user current_actor: %Actor{} = actor
} }
} }
) do ) do
with {:actor, %Actor{} = actor} <- {:actor, Users.get_actor_for_user(user)}, with {:group, %Actor{type: :Group} = group} <- {:group, Actors.get_actor(group_id)},
{:group, %Actor{type: :Group} = group} <- {:group, Actors.get_actor(group_id)},
{:ok, _activity, %Member{} = member} <- ActivityPub.leave(group, actor, true) do {:ok, _activity, %Member{} = member} <- ActivityPub.leave(group, actor, true) do
{:ok, member} {:ok, member}
else else
@ -286,13 +280,12 @@ defmodule Mobilizon.GraphQL.Resolvers.Group do
} = args, } = args,
%{ %{
context: %{ context: %{
current_user: %User{role: user_role} = user current_user: %User{role: user_role},
current_actor: %Actor{id: actor_id}
} }
} }
) do ) do
with {:actor, %Actor{id: actor_id} = _actor} <- {:actor, Users.get_actor_for_user(user)}, if Actors.is_member?(actor_id, group_id) or is_moderator(user_role) do
{:member, true} <-
{:member, Actors.is_member?(actor_id, group_id) or is_moderator(user_role)} do
# TODO : Handle public / restricted to group members events # TODO : Handle public / restricted to group members events
{:ok, {:ok,
Events.list_organized_events_for_group( Events.list_organized_events_for_group(
@ -304,7 +297,6 @@ defmodule Mobilizon.GraphQL.Resolvers.Group do
limit limit
)} )}
else else
{:member, false} ->
find_events_for_group(group, args, nil) find_events_for_group(group, args, nil)
end end
end end

View file

@ -4,7 +4,7 @@ defmodule Mobilizon.GraphQL.Resolvers.Media do
""" """
alias Mobilizon.Actors.Actor alias Mobilizon.Actors.Actor
alias Mobilizon.{Medias, Users} alias Mobilizon.Medias
alias Mobilizon.Medias.Media alias Mobilizon.Medias.Media
alias Mobilizon.Users.User alias Mobilizon.Users.User
import Mobilizon.Web.Gettext import Mobilizon.Web.Gettext
@ -44,10 +44,9 @@ defmodule Mobilizon.GraphQL.Resolvers.Media do
def upload_media( def upload_media(
_parent, _parent,
%{file: %Plug.Upload{} = file} = args, %{file: %Plug.Upload{} = file} = args,
%{context: %{current_user: %User{} = user}} %{context: %{current_actor: %Actor{id: actor_id}}}
) do ) do
with %Actor{id: actor_id} <- Users.get_actor_for_user(user), with {:ok,
{:ok,
%{ %{
name: _name, name: _name,
url: url, url: url,

View file

@ -4,7 +4,7 @@ defmodule Mobilizon.GraphQL.Resolvers.Member do
""" """
import Mobilizon.Users.Guards import Mobilizon.Users.Guards
alias Mobilizon.{Actors, Users} alias Mobilizon.Actors
alias Mobilizon.Actors.{Actor, Member} alias Mobilizon.Actors.{Actor, Member}
alias Mobilizon.Federation.ActivityPub alias Mobilizon.Federation.ActivityPub
alias Mobilizon.Federation.ActivityPub.Actor, as: ActivityPubActor alias Mobilizon.Federation.ActivityPub.Actor, as: ActivityPubActor
@ -21,12 +21,10 @@ defmodule Mobilizon.GraphQL.Resolvers.Member do
%Actor{id: group_id} = group, %Actor{id: group_id} = group,
%{page: page, limit: limit, roles: roles}, %{page: page, limit: limit, roles: roles},
%{ %{
context: %{current_user: %User{role: user_role} = user} context: %{current_user: %User{role: user_role}, current_actor: %Actor{id: actor_id}}
} = _resolution } = _resolution
) do ) do
with %Actor{id: actor_id} <- Users.get_actor_for_user(user), if Actors.is_member?(actor_id, group_id) or is_moderator(user_role) do
{:member, true} <-
{:member, Actors.is_member?(actor_id, group_id) or is_moderator(user_role)} do
roles = roles =
case roles do case roles do
"" -> "" ->
@ -42,13 +40,11 @@ defmodule Mobilizon.GraphQL.Resolvers.Member do
%Page{} = page = Actors.list_members_for_group(group, roles, page, limit) %Page{} = page = Actors.list_members_for_group(group, roles, page, limit)
{:ok, page} {:ok, page}
else else
{:member, false} ->
# Actor is not member of group, fallback to public # Actor is not member of group, fallback to public
with %Page{} = page <- Actors.list_members_for_group(group) do %Page{} = page = Actors.list_members_for_group(group)
{:ok, %Page{page | elements: []}} {:ok, %Page{page | elements: []}}
end end
end end
end
def find_members_for_group(%Actor{} = group, _args, _resolution) do def find_members_for_group(%Actor{} = group, _args, _resolution) do
with %Page{} = page <- Actors.list_members_for_group(group) do with %Page{} = page <- Actors.list_members_for_group(group) do
@ -59,10 +55,9 @@ defmodule Mobilizon.GraphQL.Resolvers.Member do
def invite_member( def invite_member(
_parent, _parent,
%{group_id: group_id, target_actor_username: target_actor_username}, %{group_id: group_id, target_actor_username: target_actor_username},
%{context: %{current_user: %User{} = user}} %{context: %{current_actor: %Actor{id: actor_id} = actor}}
) do ) do
with %Actor{id: actor_id} = actor <- Users.get_actor_for_user(user), with {:ok, %Actor{} = group} <- Actors.get_group_by_actor_id(group_id),
{:ok, %Actor{} = group} <- Actors.get_group_by_actor_id(group_id),
{:has_rights_to_invite, {:ok, %Member{role: role}}} {:has_rights_to_invite, {:ok, %Member{role: role}}}
when role in [:moderator, :administrator, :creator] <- when role in [:moderator, :administrator, :creator] <-
{:has_rights_to_invite, Actors.get_member(actor_id, group_id)}, {:has_rights_to_invite, Actors.get_member(actor_id, group_id)},
@ -97,9 +92,10 @@ defmodule Mobilizon.GraphQL.Resolvers.Member do
end end
end end
def accept_invitation(_parent, %{id: member_id}, %{context: %{current_user: %User{} = user}}) do def accept_invitation(_parent, %{id: member_id}, %{
with %Actor{id: actor_id} <- Users.get_actor_for_user(user), context: %{current_actor: %Actor{id: actor_id}}
%Member{actor: %Actor{id: member_actor_id}} = member <- }) do
with %Member{actor: %Actor{id: member_actor_id}} = member <-
Actors.get_member(member_id), Actors.get_member(member_id),
{:is_same_actor, true} <- {:is_same_actor, member_actor_id == actor_id}, {:is_same_actor, true} <- {:is_same_actor, member_actor_id == actor_id},
{:ok, _activity, %Member{} = member} <- {:ok, _activity, %Member{} = member} <-
@ -115,9 +111,10 @@ defmodule Mobilizon.GraphQL.Resolvers.Member do
end end
end end
def reject_invitation(_parent, %{id: member_id}, %{context: %{current_user: %User{} = user}}) do def reject_invitation(_parent, %{id: member_id}, %{
with %Actor{id: actor_id} <- Users.get_actor_for_user(user), context: %{current_actor: %Actor{id: actor_id}}
{:invitation_exists, %Member{actor: %Actor{id: member_actor_id}} = member} <- }) do
with {:invitation_exists, %Member{actor: %Actor{id: member_actor_id}} = member} <-
{:invitation_exists, Actors.get_member(member_id)}, {:invitation_exists, Actors.get_member(member_id)},
{:is_same_actor, true} <- {:is_same_actor, member_actor_id == actor_id}, {:is_same_actor, true} <- {:is_same_actor, member_actor_id == actor_id},
{:ok, _activity, %Member{} = member} <- {:ok, _activity, %Member{} = member} <-
@ -137,10 +134,9 @@ defmodule Mobilizon.GraphQL.Resolvers.Member do
end end
def update_member(_parent, %{member_id: member_id, role: role}, %{ def update_member(_parent, %{member_id: member_id, role: role}, %{
context: %{current_user: %User{} = user} context: %{current_actor: %Actor{} = moderator}
}) do }) do
with %Actor{} = moderator <- Users.get_actor_for_user(user), with %Member{} = member <- Actors.get_member(member_id),
%Member{} = member <- Actors.get_member(member_id),
{:ok, _activity, %Member{} = member} <- {:ok, _activity, %Member{} = member} <-
ActivityPub.update(member, %{role: role}, true, %{moderator: moderator}) do ActivityPub.update(member, %{role: role}, true, %{moderator: moderator}) do
{:ok, member} {:ok, member}
@ -161,10 +157,9 @@ defmodule Mobilizon.GraphQL.Resolvers.Member do
do: {:error, "You must be logged-in to update a member"} do: {:error, "You must be logged-in to update a member"}
def remove_member(_parent, %{member_id: member_id, group_id: group_id}, %{ def remove_member(_parent, %{member_id: member_id, group_id: group_id}, %{
context: %{current_user: %User{} = user} context: %{current_actor: %Actor{id: moderator_id} = moderator}
}) do }) do
with %Actor{id: moderator_id} = moderator <- Users.get_actor_for_user(user), with %Member{role: role} = member when role != :rejected <- Actors.get_member(member_id),
%Member{role: role} = member when role != :rejected <- Actors.get_member(member_id),
%Actor{type: :Group} = group <- Actors.get_actor(group_id), %Actor{type: :Group} = group <- Actors.get_actor(group_id),
{:has_rights_to_remove, {:ok, %Member{role: role}}} {:has_rights_to_remove, {:ok, %Member{role: role}}}
when role in [:moderator, :administrator, :creator] <- when role in [:moderator, :administrator, :creator] <-

View file

@ -2,7 +2,7 @@ defmodule Mobilizon.GraphQL.Resolvers.Participant do
@moduledoc """ @moduledoc """
Handles the participation-related GraphQL calls. Handles the participation-related GraphQL calls.
""" """
alias Mobilizon.{Actors, Config, Crypto, Events, Users} alias Mobilizon.{Actors, Config, Crypto, Events}
alias Mobilizon.Actors.Actor alias Mobilizon.Actors.Actor
alias Mobilizon.Events.{Event, Participant} alias Mobilizon.Events.{Event, Participant}
alias Mobilizon.GraphQL.API.Participations alias Mobilizon.GraphQL.API.Participations
@ -225,14 +225,12 @@ defmodule Mobilizon.GraphQL.Resolvers.Participant do
%{id: participation_id, role: new_role}, %{id: participation_id, role: new_role},
%{ %{
context: %{ context: %{
current_user: user current_actor: %Actor{} = moderator_actor
} }
} }
) do ) do
# Check that moderator provided is rightly authenticated
with %Actor{} = moderator_actor <- Users.get_actor_for_user(user),
# Check that participation already exists # Check that participation already exists
{:has_participation, %Participant{role: old_role, event_id: event_id} = participation} <- with {:has_participation, %Participant{role: old_role, event_id: event_id} = participation} <-
{:has_participation, Events.get_participant(participation_id)}, {:has_participation, Events.get_participant(participation_id)},
{:same_role, false} <- {:same_role, new_role == old_role}, {:same_role, false} <- {:same_role, new_role == old_role},
# Check that moderator has right # Check that moderator has right

View file

@ -91,8 +91,14 @@ defmodule Mobilizon.GraphQL.Resolvers.Person do
@doc """ @doc """
Returns the current actor for the currently logged-in user Returns the current actor for the currently logged-in user
""" """
def get_current_person(_parent, _args, %{context: %{current_user: user}}) do @spec get_current_person(any, any, Absinthe.Resolution.t()) ::
{:ok, Users.get_actor_for_user(user)} {:error, :unauthenticated} | {:ok, Actor.t()}
def get_current_person(_parent, _args, %{context: %{current_actor: %Actor{} = actor}}) do
{:ok, actor}
end
def get_current_person(_parent, _args, %{context: %{current_user: %User{}}}) do
{:error, :no_current_person}
end end
def get_current_person(_parent, _args, _resolution) do def get_current_person(_parent, _args, _resolution) do
@ -102,6 +108,8 @@ defmodule Mobilizon.GraphQL.Resolvers.Person do
@doc """ @doc """
Returns the list of identities for the logged-in user Returns the list of identities for the logged-in user
""" """
@spec identities(any, any, Absinthe.Resolution.t()) ::
{:error, :unauthenticated} | {:ok, list(Actor.t())}
def identities(_parent, _args, %{context: %{current_user: user}}) do def identities(_parent, _args, %{context: %{current_user: user}}) do
{:ok, Users.get_actors_for_user(user)} {:ok, Users.get_actors_for_user(user)}
end end
@ -148,21 +156,24 @@ defmodule Mobilizon.GraphQL.Resolvers.Person do
require Logger require Logger
args = Map.put(args, :user_id, user.id) args = Map.put(args, :user_id, user.id)
with {:find_actor, %Actor{} = actor} <- case owned_actor(user, id) do
{:find_actor, Actors.get_actor(id)}, {:ok, %Actor{} = actor} ->
{:is_owned, %Actor{}} <- User.owns_actor(user, actor.id), case save_attached_pictures(args) do
{:picture, args} when is_map(args) <- {:picture, save_attached_pictures(args)}, args when is_map(args) ->
{:ok, _activity, %Actor{} = actor} <- ActivityPub.update(actor, args, true) do case ActivityPub.update(actor, args, true) do
{:ok, _activity, %Actor{} = actor} ->
{:ok, actor} {:ok, actor}
else
{:picture, {:error, :file_too_large}} -> {:error, err} ->
{:error, err}
end
{:error, :file_too_large} ->
{:error, dgettext("errors", "The provided picture is too heavy")} {:error, dgettext("errors", "The provided picture is too heavy")}
end
{:find_actor, nil} -> {:error, err} ->
{:error, dgettext("errors", "Profile not found")} {:error, err}
{:is_owned, nil} ->
{:error, dgettext("errors", "Profile is not owned by authenticated user")}
end end
end end
@ -176,27 +187,22 @@ defmodule Mobilizon.GraphQL.Resolvers.Person do
def delete_person( def delete_person(
_parent, _parent,
%{id: id} = _args, %{id: id} = _args,
%{context: %{current_user: user}} = _resolution %{context: %{current_user: %User{} = user}} = _resolution
) do ) do
with {:find_actor, %Actor{} = actor} <- case owned_actor(user, id) do
{:find_actor, Actors.get_actor(id)}, {:ok, %Actor{} = actor} ->
{:is_owned, %Actor{}} <- User.owns_actor(user, actor.id), if last_identity?(user) do
{:last_identity, false} <- {:last_identity, last_identity?(user)},
{:last_admin, false} <- {:last_admin, last_admin_of_a_group?(actor.id)},
{:ok, actor} <- Actors.delete_actor(actor) do
{:ok, actor}
else
{:find_actor, nil} ->
{:error, dgettext("errors", "Profile not found")}
{:last_identity, true} ->
{:error, dgettext("errors", "Cannot remove the last identity of a user")} {:error, dgettext("errors", "Cannot remove the last identity of a user")}
else
{:last_admin, true} -> if last_admin_of_a_group?(actor.id) do
{:error, dgettext("errors", "Cannot remove the last administrator of a group")} {:error, dgettext("errors", "Cannot remove the last administrator of a group")}
else
Actors.delete_actor(actor)
end
end
{:is_owned, nil} -> {:error, err} ->
{:error, dgettext("errors", "Profile is not owned by authenticated user")} {:error, err}
end end
end end
@ -204,20 +210,50 @@ defmodule Mobilizon.GraphQL.Resolvers.Person do
{:error, :unauthenticated} {:error, :unauthenticated}
end end
@spec owned_actor(User.t(), integer() | String.t()) :: {:error, String.t()} | {:ok, Actor.t()}
defp owned_actor(%User{} = user, actor_id) do
with {:find_actor, %Actor{} = actor} <-
{:find_actor, Actors.get_actor(actor_id)},
{:is_owned, %Actor{}} <- User.owns_actor(user, actor.id) do
{:ok, actor}
else
{:find_actor, nil} ->
{:error, dgettext("errors", "Profile not found")}
{:is_owned, nil} ->
{:error, dgettext("errors", "Profile is not owned by authenticated user")}
end
end
defp last_identity?(user) do defp last_identity?(user) do
length(Users.get_actors_for_user(user)) <= 1 length(Users.get_actors_for_user(user)) <= 1
end end
@spec save_attached_pictures(map()) :: map() | {:error, any()}
defp save_attached_pictures(args) do defp save_attached_pictures(args) do
with args when is_map(args) <- save_attached_picture(args, :avatar), case save_attached_picture(args, :avatar) do
args when is_map(args) <- save_attached_picture(args, :banner) do {:error, err} ->
{:error, err}
args when is_map(args) ->
case save_attached_picture(args, :banner) do
{:error, err} ->
{:error, err}
args when is_map(args) ->
args args
end end
end end
end
@spec save_attached_picture(map(), :avatar | :banner) :: map() | {:error, any}
defp save_attached_picture(args, key) do defp save_attached_picture(args, key) do
if Map.has_key?(args, key) && !is_nil(args[key][:media]) do if Map.has_key?(args, key) && !is_nil(args[key][:media]) do
with media when is_map(media) <- save_picture(args[key][:media], key) do case save_picture(args[key][:media], key) do
{:error, err} ->
{:error, err}
media when is_map(media) ->
Map.put(args, key, media) Map.put(args, key, media)
end end
else else
@ -225,10 +261,14 @@ defmodule Mobilizon.GraphQL.Resolvers.Person do
end end
end end
@spec save_picture(map(), :avatar | :banner) :: {:ok, map()} | {:error, any()}
defp save_picture(media, key) do defp save_picture(media, key) do
with {:ok, %{name: name, url: url, content_type: content_type, size: size}} <- case Upload.store(media.file, type: key, description: media.alt) do
Upload.store(media.file, type: key, description: media.alt) do {:ok, %{name: name, url: url, content_type: content_type, size: size}} ->
%{"name" => name, "url" => url, "content_type" => content_type, "size" => size} %{"name" => name, "url" => url, "content_type" => content_type, "size" => size}
{:error, err} ->
{:error, err}
end end
end end
@ -237,30 +277,37 @@ defmodule Mobilizon.GraphQL.Resolvers.Person do
""" """
def register_person(_parent, args, _resolution) do def register_person(_parent, args, _resolution) do
# When registering, email is assumed confirmed (unlike changing email) # When registering, email is assumed confirmed (unlike changing email)
with {:ok, %User{} = user} <- Users.get_user_by_email(args.email, unconfirmed: false), case Users.get_user_by_email(args.email, unconfirmed: false) do
user_actor <- Users.get_actor_for_user(user), {:ok, %User{} = user} ->
no_actor <- is_nil(user_actor), if is_nil(Users.get_actor_for_user(user)) do
{:no_actor, true} <- {:no_actor, no_actor}, # No profile yet, we can create one
args <- Map.update(args, :preferred_username, "", &String.downcase/1), case prepare_args(args, user) do
args <- Map.put(args, :user_id, user.id), args when is_map(args) ->
{:picture, args} when is_map(args) <- {:picture, save_attached_pictures(args)}, Actors.new_person(args, true)
{:ok, %Actor{} = new_person} <- Actors.new_person(args, true) do
{:ok, new_person} {:error, :file_too_large} ->
else
{:picture, {:error, :file_too_large}} ->
{:error, dgettext("errors", "The provided picture is too heavy")} {:error, dgettext("errors", "The provided picture is too heavy")}
{:error, _err} ->
{:error, dgettext("errors", "Error while uploading pictures")}
end
else
{:error, dgettext("errors", "You already have a profile for this user")}
end
{:error, :user_not_found} -> {:error, :user_not_found} ->
{:error, dgettext("errors", "No user with this email was found")} {:error, dgettext("errors", "No user with this email was found")}
{:no_actor, _} ->
{:error, dgettext("errors", "You already have a profile for this user")}
{:error, %Ecto.Changeset{} = e} ->
{:error, e}
end end
end end
@spec prepare_args(map(), User.t()) :: map() | {:error, any()}
defp prepare_args(args, %User{} = user) do
args
|> Map.update(:preferred_username, "", &String.downcase/1)
|> Map.put(:user_id, user.id)
|> save_attached_pictures()
end
@doc """ @doc """
Returns the participations, optionally restricted to an event Returns the participations, optionally restricted to an event
""" """
@ -269,17 +316,13 @@ defmodule Mobilizon.GraphQL.Resolvers.Person do
%{event_id: event_id}, %{event_id: event_id},
%{context: %{current_user: %User{} = user}} %{context: %{current_user: %User{} = user}}
) do ) do
with {:can_get_participations, true} <- if user_can_access_person_details?(person, user) do
{:can_get_participations, user_can_access_person_details?(person, user)}, case Events.get_participant(event_id, actor_id) do
{:no_participant, {:ok, %Participant{} = participant}} <- {:ok, %Participant{} = participant} -> {:ok, %Page{elements: [participant], total: 1}}
{:no_participant, Events.get_participant(event_id, actor_id)} do {:error, :participant_not_found} -> {:ok, %Page{elements: [], total: 0}}
{:ok, %Page{elements: [participant], total: 1}} end
else else
{:is_owned, nil} -> {:error, :unauthorized}
{:error, dgettext("errors", "Profile is not owned by authenticated user")}
{:no_participant, _} ->
{:ok, %Page{elements: [], total: 0}}
end end
end end

View file

@ -4,7 +4,7 @@ defmodule Mobilizon.GraphQL.Resolvers.Post do
""" """
import Mobilizon.Users.Guards import Mobilizon.Users.Guards
alias Mobilizon.{Actors, Posts, Users} alias Mobilizon.{Actors, Posts}
alias Mobilizon.Actors.Actor alias Mobilizon.Actors.Actor
alias Mobilizon.Federation.ActivityPub alias Mobilizon.Federation.ActivityPub
alias Mobilizon.Federation.ActivityPub.{Permission, Utils} alias Mobilizon.Federation.ActivityPub.{Permission, Utils}
@ -27,12 +27,12 @@ defmodule Mobilizon.GraphQL.Resolvers.Post do
%{page: page, limit: limit} = args, %{page: page, limit: limit} = args,
%{ %{
context: %{ context: %{
current_user: %User{role: user_role} = user current_user: %User{role: user_role},
current_actor: %Actor{id: actor_id}
} }
} = _resolution } = _resolution
) do ) do
with %Actor{id: actor_id} <- Users.get_actor_for_user(user), with {:member, true} <-
{:member, true} <-
{:member, Actors.is_member?(actor_id, group_id) or is_moderator(user_role)}, {:member, Actors.is_member?(actor_id, group_id) or is_moderator(user_role)},
%Page{} = page <- Posts.get_posts_for_group(group, page, limit) do %Page{} = page <- Posts.get_posts_for_group(group, page, limit) do
{:ok, page} {:ok, page}
@ -65,13 +65,12 @@ defmodule Mobilizon.GraphQL.Resolvers.Post do
%{slug: slug}, %{slug: slug},
%{ %{
context: %{ context: %{
current_user: %User{role: user_role} = user current_user: %User{role: user_role},
current_actor: %Actor{} = current_profile
} }
} = _resolution } = _resolution
) do ) do
with {:current_actor, %Actor{} = current_profile} <- with {:post, %Post{attributed_to: %Actor{}} = post} <-
{:current_actor, Users.get_actor_for_user(user)},
{:post, %Post{attributed_to: %Actor{}} = post} <-
{:post, Posts.get_post_by_slug_with_preloads(slug)}, {:post, Posts.get_post_by_slug_with_preloads(slug)},
{:member, true} <- {:member, true} <-
{:member, {:member,
@ -107,12 +106,11 @@ defmodule Mobilizon.GraphQL.Resolvers.Post do
%{attributed_to_id: group_id} = args, %{attributed_to_id: group_id} = args,
%{ %{
context: %{ context: %{
current_user: %User{} = user current_actor: %Actor{id: actor_id}
} }
} = _resolution } = _resolution
) do ) do
with %Actor{id: actor_id} <- Users.get_actor_for_user(user), with {:member, true} <- {:member, Actors.is_member?(actor_id, group_id)},
{:member, true} <- {:member, Actors.is_member?(actor_id, group_id)},
%Actor{} = group <- Actors.get_actor(group_id), %Actor{} = group <- Actors.get_actor(group_id),
args <- args <-
Map.update(args, :picture, nil, fn picture -> Map.update(args, :picture, nil, fn picture ->
@ -147,12 +145,11 @@ defmodule Mobilizon.GraphQL.Resolvers.Post do
%{id: id} = args, %{id: id} = args,
%{ %{
context: %{ context: %{
current_user: %User{} = user current_actor: %Actor{id: actor_id, url: actor_url}
} }
} = _resolution } = _resolution
) do ) do
with {:uuid, {:ok, _uuid}} <- {:uuid, Ecto.UUID.cast(id)}, with {:uuid, {:ok, _uuid}} <- {:uuid, Ecto.UUID.cast(id)},
%Actor{id: actor_id, url: actor_url} <- Users.get_actor_for_user(user),
{:post, %Post{attributed_to: %Actor{id: group_id} = group} = post} <- {:post, %Post{attributed_to: %Actor{id: group_id} = group} = post} <-
{:post, Posts.get_post_with_preloads(id)}, {:post, Posts.get_post_with_preloads(id)},
args <- args <-
@ -185,12 +182,11 @@ defmodule Mobilizon.GraphQL.Resolvers.Post do
%{id: post_id}, %{id: post_id},
%{ %{
context: %{ context: %{
current_user: %User{} = user current_actor: %Actor{id: actor_id} = actor
} }
} = _resolution } = _resolution
) do ) do
with {:uuid, {:ok, _uuid}} <- {:uuid, Ecto.UUID.cast(post_id)}, with {:uuid, {:ok, _uuid}} <- {:uuid, Ecto.UUID.cast(post_id)},
%Actor{id: actor_id} = actor <- Users.get_actor_for_user(user),
{:post, %Post{attributed_to: %Actor{id: group_id}} = post} <- {:post, %Post{attributed_to: %Actor{id: group_id}} = post} <-
{:post, Posts.get_post_with_preloads(post_id)}, {:post, Posts.get_post_with_preloads(post_id)},
{:member, true} <- {:member, Actors.is_member?(actor_id, group_id)}, {:member, true} <- {:member, Actors.is_member?(actor_id, group_id)},

View file

@ -5,7 +5,7 @@ defmodule Mobilizon.GraphQL.Resolvers.Report do
import Mobilizon.Users.Guards import Mobilizon.Users.Guards
alias Mobilizon.{Actors, Config, Reports, Users} alias Mobilizon.{Actors, Config, Reports}
alias Mobilizon.Actors.Actor alias Mobilizon.Actors.Actor
alias Mobilizon.Reports.{Note, Report} alias Mobilizon.Reports.{Note, Report}
alias Mobilizon.Users.User alias Mobilizon.Users.User
@ -47,13 +47,12 @@ defmodule Mobilizon.GraphQL.Resolvers.Report do
def create_report( def create_report(
_parent, _parent,
args, args,
%{context: %{current_user: %User{} = user}} = _resolution %{context: %{current_actor: %Actor{id: reporter_id}}} = _resolution
) do ) do
with %Actor{id: reporter_id} <- Users.get_actor_for_user(user), case args |> Map.put(:reporter_id, reporter_id) |> API.Reports.report() do
{:ok, _, %Report{} = report} <- {:ok, _, %Report{} = report} ->
args |> Map.put(:reporter_id, reporter_id) |> API.Reports.report() do
{:ok, report} {:ok, report}
else
_error -> _error ->
{:error, dgettext("errors", "Error while saving report")} {:error, dgettext("errors", "Error while saving report")}
end end
@ -84,11 +83,10 @@ defmodule Mobilizon.GraphQL.Resolvers.Report do
def update_report( def update_report(
_parent, _parent,
%{report_id: report_id, status: status}, %{report_id: report_id, status: status},
%{context: %{current_user: %User{role: role} = user}} %{context: %{current_user: %User{role: role}, current_actor: %Actor{} = actor}}
) )
when is_moderator(role) do when is_moderator(role) do
with %Actor{} = actor <- Users.get_actor_for_user(user), with %Report{} = report <- Mobilizon.Reports.get_report(report_id),
%Report{} = report <- Mobilizon.Reports.get_report(report_id),
{:ok, %Report{} = report} <- API.Reports.update_report_status(actor, report, status) do {:ok, %Report{} = report} <- API.Reports.update_report_status(actor, report, status) do
{:ok, report} {:ok, report}
else else
@ -104,11 +102,10 @@ defmodule Mobilizon.GraphQL.Resolvers.Report do
def create_report_note( def create_report_note(
_parent, _parent,
%{report_id: report_id, content: content}, %{report_id: report_id, content: content},
%{context: %{current_user: %User{role: role} = user}} %{context: %{current_user: %User{role: role}, current_actor: %Actor{id: moderator_id}}}
) )
when is_moderator(role) do when is_moderator(role) do
with %Actor{id: moderator_id} <- Users.get_actor_for_user(user), with %Report{} = report <- Reports.get_report(report_id),
%Report{} = report <- Reports.get_report(report_id),
%Actor{} = moderator <- Actors.get_local_actor_with_preload(moderator_id), %Actor{} = moderator <- Actors.get_local_actor_with_preload(moderator_id),
{:ok, %Note{} = note} <- API.Reports.create_report_note(report, moderator, content) do {:ok, %Note{} = note} <- API.Reports.create_report_note(report, moderator, content) do
{:ok, note} {:ok, note}
@ -118,11 +115,10 @@ defmodule Mobilizon.GraphQL.Resolvers.Report do
def delete_report_note( def delete_report_note(
_parent, _parent,
%{note_id: note_id}, %{note_id: note_id},
%{context: %{current_user: %User{role: role} = user}} %{context: %{current_user: %User{role: role}, current_actor: %Actor{id: moderator_id}}}
) )
when is_moderator(role) do when is_moderator(role) do
with %Actor{id: moderator_id} <- Users.get_actor_for_user(user), with %Note{} = note <- Reports.get_note(note_id),
%Note{} = note <- Reports.get_note(note_id),
%Actor{} = moderator <- Actors.get_local_actor_with_preload(moderator_id), %Actor{} = moderator <- Actors.get_local_actor_with_preload(moderator_id),
{:ok, %Note{} = note} <- API.Reports.delete_report_note(note, moderator) do {:ok, %Note{} = note} <- API.Reports.delete_report_note(note, moderator) do
{:ok, %{id: note.id}} {:ok, %{id: note.id}}

View file

@ -3,7 +3,7 @@ defmodule Mobilizon.GraphQL.Resolvers.Resource do
Handles the resources-related GraphQL calls Handles the resources-related GraphQL calls
""" """
alias Mobilizon.{Actors, Resources, Users} alias Mobilizon.{Actors, Resources}
alias Mobilizon.Actors.Actor alias Mobilizon.Actors.Actor
alias Mobilizon.Federation.ActivityPub alias Mobilizon.Federation.ActivityPub
alias Mobilizon.Resources.Resource alias Mobilizon.Resources.Resource
@ -26,12 +26,11 @@ defmodule Mobilizon.GraphQL.Resolvers.Resource do
%{page: page, limit: limit}, %{page: page, limit: limit},
%{ %{
context: %{ context: %{
current_user: %User{} = user current_actor: %Actor{id: actor_id}
} }
} = _resolution } = _resolution
) do ) do
with %Actor{id: actor_id} <- Users.get_actor_for_user(user), with {:member, true} <- {:member, Actors.is_member?(actor_id, group_id)},
{:member, true} <- {:member, Actors.is_member?(actor_id, group_id)},
%Page{} = page <- Resources.get_resources_for_group(group, page, limit) do %Page{} = page <- Resources.get_resources_for_group(group, page, limit) do
{:ok, page} {:ok, page}
else else
@ -53,12 +52,11 @@ defmodule Mobilizon.GraphQL.Resolvers.Resource do
%{page: page, limit: limit}, %{page: page, limit: limit},
%{ %{
context: %{ context: %{
current_user: %User{} = user current_actor: %Actor{id: actor_id}
} }
} = _resolution } = _resolution
) do ) do
with %Actor{id: actor_id} <- Users.get_actor_for_user(user), with {:member, true} <- {:member, Actors.is_member?(actor_id, group_id)},
{:member, true} <- {:member, Actors.is_member?(actor_id, group_id)},
%Page{} = page <- Resources.get_resources_for_folder(parent, page, limit) do %Page{} = page <- Resources.get_resources_for_folder(parent, page, limit) do
{:ok, page} {:ok, page}
end end
@ -72,13 +70,11 @@ defmodule Mobilizon.GraphQL.Resolvers.Resource do
%{path: path, username: username}, %{path: path, username: username},
%{ %{
context: %{ context: %{
current_user: %User{} = user current_actor: %Actor{id: actor_id}
} }
} = _resolution } = _resolution
) do ) do
with {:current_actor, %Actor{id: actor_id}} <- with {:group, %Actor{id: group_id}} <- {:group, Actors.get_actor_by_name(username, :Group)},
{:current_actor, Users.get_actor_for_user(user)},
{:group, %Actor{id: group_id}} <- {:group, Actors.get_actor_by_name(username, :Group)},
{:member, true} <- {:member, Actors.is_member?(actor_id, group_id)}, {:member, true} <- {:member, Actors.is_member?(actor_id, group_id)},
{:resource, %Resource{} = resource} <- {:resource, %Resource{} = resource} <-
{:resource, Resources.get_resource_by_group_and_path_with_preloads(group_id, path)} do {:resource, Resources.get_resource_by_group_and_path_with_preloads(group_id, path)} do
@ -99,12 +95,11 @@ defmodule Mobilizon.GraphQL.Resolvers.Resource do
%{actor_id: group_id} = args, %{actor_id: group_id} = args,
%{ %{
context: %{ context: %{
current_user: %User{} = user current_actor: %Actor{id: actor_id}
} }
} = _resolution } = _resolution
) do ) do
with %Actor{id: actor_id} <- Users.get_actor_for_user(user), with {:member, true} <- {:member, Actors.is_member?(actor_id, group_id)},
{:member, true} <- {:member, Actors.is_member?(actor_id, group_id)},
parent <- get_eventual_parent(args), parent <- get_eventual_parent(args),
{:own_check, true} <- {:own_check, check_resource_owned_by_group(parent, group_id)}, {:own_check, true} <- {:own_check, check_resource_owned_by_group(parent, group_id)},
{:ok, _, %Resource{} = resource} <- {:ok, _, %Resource{} = resource} <-
@ -138,12 +133,11 @@ defmodule Mobilizon.GraphQL.Resolvers.Resource do
%{id: resource_id} = args, %{id: resource_id} = args,
%{ %{
context: %{ context: %{
current_user: %User{} = user current_actor: %Actor{id: actor_id, url: actor_url}
} }
} = _resolution } = _resolution
) do ) do
with %Actor{id: actor_id, url: actor_url} <- Users.get_actor_for_user(user), with {:resource, %Resource{actor_id: group_id} = resource} <-
{:resource, %Resource{actor_id: group_id} = resource} <-
{:resource, Resources.get_resource_with_preloads(resource_id)}, {:resource, Resources.get_resource_with_preloads(resource_id)},
{:member, true} <- {:member, Actors.is_member?(actor_id, group_id)}, {:member, true} <- {:member, Actors.is_member?(actor_id, group_id)},
{:ok, _, %Resource{} = resource} <- {:ok, _, %Resource{} = resource} <-
@ -167,12 +161,11 @@ defmodule Mobilizon.GraphQL.Resolvers.Resource do
%{id: resource_id}, %{id: resource_id},
%{ %{
context: %{ context: %{
current_user: %User{} = user current_actor: %Actor{id: actor_id} = actor
} }
} = _resolution } = _resolution
) do ) do
with %Actor{id: actor_id} = actor <- Users.get_actor_for_user(user), with {:resource, %Resource{parent_id: _parent_id, actor_id: group_id} = resource} <-
{:resource, %Resource{parent_id: _parent_id, actor_id: group_id} = resource} <-
{:resource, Resources.get_resource_with_preloads(resource_id)}, {:resource, Resources.get_resource_with_preloads(resource_id)},
{:member, true} <- {:member, Actors.is_member?(actor_id, group_id)}, {:member, true} <- {:member, Actors.is_member?(actor_id, group_id)},
{:ok, _, %Resource{} = resource} <- {:ok, _, %Resource{} = resource} <-

View file

@ -3,12 +3,11 @@ defmodule Mobilizon.GraphQL.Resolvers.Todos do
Handles the todos related GraphQL calls Handles the todos related GraphQL calls
""" """
alias Mobilizon.{Actors, Todos, Users} alias Mobilizon.{Actors, Todos}
alias Mobilizon.Actors.Actor alias Mobilizon.Actors.Actor
alias Mobilizon.Federation.ActivityPub alias Mobilizon.Federation.ActivityPub
alias Mobilizon.Storage.Page alias Mobilizon.Storage.Page
alias Mobilizon.Todos.{Todo, TodoList} alias Mobilizon.Todos.{Todo, TodoList}
alias Mobilizon.Users.User
import Mobilizon.Web.Gettext import Mobilizon.Web.Gettext
require Logger require Logger
@ -22,11 +21,10 @@ defmodule Mobilizon.GraphQL.Resolvers.Todos do
%Actor{id: group_id} = group, %Actor{id: group_id} = group,
_args, _args,
%{ %{
context: %{current_user: %User{} = user} context: %{current_actor: %Actor{id: actor_id}}
} = _resolution } = _resolution
) do ) do
with %Actor{id: actor_id} <- Users.get_actor_for_user(user), with {:member, true} <- {:member, Actors.is_member?(actor_id, group_id)},
{:member, true} <- {:member, Actors.is_member?(actor_id, group_id)},
%Page{} = page <- Todos.get_todo_lists_for_group(group) do %Page{} = page <- Todos.get_todo_lists_for_group(group) do
{:ok, page} {:ok, page}
else else
@ -45,11 +43,10 @@ defmodule Mobilizon.GraphQL.Resolvers.Todos do
%TodoList{actor_id: group_id} = todo_list, %TodoList{actor_id: group_id} = todo_list,
_args, _args,
%{ %{
context: %{current_user: %User{} = user} context: %{current_actor: %Actor{id: actor_id}}
} = _resolution } = _resolution
) do ) do
with %Actor{id: actor_id} <- Users.get_actor_for_user(user), with {:member, true} <- {:member, Actors.is_member?(actor_id, group_id)},
{:member, true} <- {:member, Actors.is_member?(actor_id, group_id)},
%Page{} = page <- Todos.get_todos_for_todo_list(todo_list) do %Page{} = page <- Todos.get_todos_for_todo_list(todo_list) do
{:ok, page} {:ok, page}
else else
@ -62,11 +59,10 @@ defmodule Mobilizon.GraphQL.Resolvers.Todos do
_parent, _parent,
%{id: todo_list_id}, %{id: todo_list_id},
%{ %{
context: %{current_user: %User{} = user} context: %{current_actor: %Actor{id: actor_id}}
} = _resolution } = _resolution
) do ) do
with {:actor, %Actor{id: actor_id}} <- {:actor, Users.get_actor_for_user(user)}, with {:todo, %TodoList{actor_id: group_id} = todo} <-
{:todo, %TodoList{actor_id: group_id} = todo} <-
{:todo, Todos.get_todo_list(todo_list_id)}, {:todo, Todos.get_todo_list(todo_list_id)},
{:member, true} <- {:member, Actors.is_member?(actor_id, group_id)} do {:member, true} <- {:member, Actors.is_member?(actor_id, group_id)} do
{:ok, todo} {:ok, todo}
@ -86,11 +82,10 @@ defmodule Mobilizon.GraphQL.Resolvers.Todos do
_parent, _parent,
%{group_id: group_id} = args, %{group_id: group_id} = args,
%{ %{
context: %{current_user: %User{} = user} context: %{current_actor: %Actor{id: actor_id}}
} = _resolution } = _resolution
) do ) do
with {:actor, %Actor{id: actor_id} = _actor} <- {:actor, Users.get_actor_for_user(user)}, with {:member, true} <- {:member, Actors.is_member?(actor_id, group_id)},
{:member, true} <- {:member, Actors.is_member?(actor_id, group_id)},
{:ok, _, %TodoList{} = todo_list} <- {:ok, _, %TodoList{} = todo_list} <-
ActivityPub.create(:todo_list, Map.put(args, :actor_id, group_id), true, %{}) do ActivityPub.create(:todo_list, Map.put(args, :actor_id, group_id), true, %{}) do
{:ok, todo_list} {:ok, todo_list}
@ -153,11 +148,10 @@ defmodule Mobilizon.GraphQL.Resolvers.Todos do
_parent, _parent,
%{id: todo_id}, %{id: todo_id},
%{ %{
context: %{current_user: %User{} = user} context: %{current_actor: %Actor{id: actor_id}}
} = _resolution } = _resolution
) do ) do
with {:actor, %Actor{id: actor_id}} <- {:actor, Users.get_actor_for_user(user)}, with {:todo, %Todo{todo_list_id: todo_list_id} = todo} <-
{:todo, %Todo{todo_list_id: todo_list_id} = todo} <-
{:todo, Todos.get_todo(todo_id)}, {:todo, Todos.get_todo(todo_id)},
{:todo_list, %TodoList{actor_id: group_id}} <- {:todo_list, %TodoList{actor_id: group_id}} <-
{:todo_list, Todos.get_todo_list(todo_list_id)}, {:todo_list, Todos.get_todo_list(todo_list_id)},
@ -179,11 +173,10 @@ defmodule Mobilizon.GraphQL.Resolvers.Todos do
_parent, _parent,
%{todo_list_id: todo_list_id} = args, %{todo_list_id: todo_list_id} = args,
%{ %{
context: %{current_user: %User{} = user} context: %{current_actor: %Actor{id: actor_id}}
} = _resolution } = _resolution
) do ) do
with {:actor, %Actor{id: actor_id} = _actor} <- {:actor, Users.get_actor_for_user(user)}, with {:todo_list, %TodoList{actor_id: group_id} = _todo_list} <-
{:todo_list, %TodoList{actor_id: group_id} = _todo_list} <-
{:todo_list, Todos.get_todo_list(todo_list_id)}, {:todo_list, Todos.get_todo_list(todo_list_id)},
{:member, true} <- {:member, Actors.is_member?(actor_id, group_id)}, {:member, true} <- {:member, Actors.is_member?(actor_id, group_id)},
{:ok, _, %Todo{} = todo} <- {:ok, _, %Todo{} = todo} <-
@ -205,11 +198,10 @@ defmodule Mobilizon.GraphQL.Resolvers.Todos do
_parent, _parent,
%{id: todo_id} = args, %{id: todo_id} = args,
%{ %{
context: %{current_user: %User{} = user} context: %{current_actor: %Actor{id: actor_id}}
} = _resolution } = _resolution
) do ) do
with {:actor, %Actor{id: actor_id} = _actor} <- {:actor, Users.get_actor_for_user(user)}, with {:todo, %Todo{todo_list_id: todo_list_id} = todo} <-
{:todo, %Todo{todo_list_id: todo_list_id} = todo} <-
{:todo, Todos.get_todo(todo_id)}, {:todo, Todos.get_todo(todo_id)},
{:todo_list, %TodoList{actor_id: group_id}} <- {:todo_list, %TodoList{actor_id: group_id}} <-
{:todo_list, Todos.get_todo_list(todo_list_id)}, {:todo_list, Todos.get_todo_list(todo_list_id)},

View file

@ -33,8 +33,7 @@ defmodule Mobilizon.GraphQL.Resolvers.User do
""" """
@spec get_current_user(any, map(), Absinthe.Resolution.t()) :: @spec get_current_user(any, map(), Absinthe.Resolution.t()) ::
{:error, :unauthenticated} | {:ok, Mobilizon.Users.User.t()} {:error, :unauthenticated} | {:ok, Mobilizon.Users.User.t()}
def get_current_user(_parent, _args, %{context: %{current_user: %User{} = user} = context}) do def get_current_user(_parent, _args, %{context: %{current_user: %User{} = user}}) do
# Logger.error(inspect(context))
{:ok, user} {:ok, user}
end end
@ -204,9 +203,10 @@ defmodule Mobilizon.GraphQL.Resolvers.User do
""" """
@spec validate_user(map(), %{token: String.t()}, map()) :: {:ok, map()} | {:error, String.t()} @spec validate_user(map(), %{token: String.t()}, map()) :: {:ok, map()} | {:error, String.t()}
def validate_user(_parent, %{token: token}, _resolution) do def validate_user(_parent, %{token: token}, _resolution) do
with {:check_confirmation_token, {:ok, %User{} = user}} <- case Email.User.check_confirmation_token(token) do
{:check_confirmation_token, Email.User.check_confirmation_token(token)}, {:ok, %User{} = user} ->
{:get_actor, actor} <- {:get_actor, Users.get_actor_for_user(user)} do actor = Users.get_actor_for_user(user)
{:ok, %{access_token: access_token, refresh_token: refresh_token}} = {:ok, %{access_token: access_token, refresh_token: refresh_token}} =
Authenticator.generate_tokens(user) Authenticator.generate_tokens(user)
@ -216,11 +216,14 @@ defmodule Mobilizon.GraphQL.Resolvers.User do
refresh_token: refresh_token, refresh_token: refresh_token,
user: Map.put(user, :default_actor, actor) user: Map.put(user, :default_actor, actor)
}} }}
else
error ->
Logger.info("Unable to validate user with token #{token}")
Logger.debug(inspect(error))
{:error, :invalid_token} ->
Logger.info("Invalid token #{token} to validate user")
{:error, dgettext("errors", "Unable to validate user")}
{:error, %Ecto.Changeset{} = err} ->
Logger.info("Unable to validate user with token #{token}")
Logger.debug(inspect(err))
{:error, dgettext("errors", "Unable to validate user")} {:error, dgettext("errors", "Unable to validate user")}
end end
end end
@ -280,8 +283,17 @@ defmodule Mobilizon.GraphQL.Resolvers.User do
{:ok, tokens} = Authenticator.authenticate(email, password) {:ok, tokens} = Authenticator.authenticate(email, password)
{:ok, Map.put(tokens, :user, user)} {:ok, Map.put(tokens, :user, user)}
{:error, error} -> {:error, %Ecto.Changeset{errors: [password: {"registration.error.password_too_short", _}]}} ->
{:error, error} {:error,
gettext(
"The password you have choosen is too short. Please make sure your password contains at least 6 charaters."
)}
{:error, _err} ->
{:error,
gettext(
"The token you provided is invalid. Make sure that the URL is exactly the one provided inside the email you got."
)}
end end
end end
@ -289,12 +301,12 @@ defmodule Mobilizon.GraphQL.Resolvers.User do
def change_default_actor( def change_default_actor(
_parent, _parent,
%{preferred_username: username}, %{preferred_username: username},
%{context: %{current_user: user}} %{context: %{current_user: %User{id: user_id} = user}}
) do ) do
with %Actor{id: actor_id} <- Actors.get_local_actor_by_name(username), with %Actor{id: actor_id} = actor <- Actors.get_local_actor_by_name(username),
{:user_actor, true} <- {:user_actor, true} <-
{:user_actor, actor_id in Enum.map(Users.get_actors_for_user(user), & &1.id)}, {:user_actor, actor_id in Enum.map(Users.get_actors_for_user(user), & &1.id)},
%User{} = user <- Users.update_user_default_actor(user.id, actor_id) do %User{} = user <- Users.update_user_default_actor(user_id, actor) do
{:ok, user} {:ok, user}
else else
{:user_actor, _} -> {:user_actor, _} ->
@ -459,19 +471,17 @@ defmodule Mobilizon.GraphQL.Resolvers.User do
end end
def delete_account(_parent, %{user_id: user_id}, %{ def delete_account(_parent, %{user_id: user_id}, %{
context: %{current_user: %User{role: role} = moderator_user} context: %{
current_user: %User{role: role},
current_actor: %Actor{} = moderator_actor
}
}) })
when is_moderator(role) do when is_moderator(role) do
with {:moderator_actor, %Actor{} = moderator_actor} <- with %User{disabled: false} = user <- Users.get_user(user_id),
{:moderator_actor, Users.get_actor_for_user(moderator_user)},
%User{disabled: false} = user <- Users.get_user(user_id),
{:ok, %User{}} <- {:ok, %User{}} <-
do_delete_account(%User{} = user, actor_performing: Relay.get_actor()) do do_delete_account(%User{} = user, actor_performing: Relay.get_actor()) do
Admin.log_action(moderator_actor, "delete", user) Admin.log_action(moderator_actor, "delete", user)
else else
{:moderator_actor, nil} ->
{:error, dgettext("errors", "No profile found for the moderator user")}
%User{disabled: true} -> %User{disabled: true} ->
{:error, dgettext("errors", "User already disabled")} {:error, dgettext("errors", "User already disabled")}
end end

View file

@ -20,7 +20,7 @@ defmodule Mobilizon.GraphQL.Schema do
alias Mobilizon.Actors.{Actor, Follower, Member} alias Mobilizon.Actors.{Actor, Follower, Member}
alias Mobilizon.Discussions.Comment alias Mobilizon.Discussions.Comment
alias Mobilizon.Events.{Event, Participant} alias Mobilizon.Events.{Event, Participant}
alias Mobilizon.GraphQL.Middleware.ErrorHandler alias Mobilizon.GraphQL.Middleware.{CurrentActorProvider, ErrorHandler}
alias Mobilizon.GraphQL.Schema alias Mobilizon.GraphQL.Schema
alias Mobilizon.Storage.Repo alias Mobilizon.Storage.Repo
@ -195,7 +195,7 @@ defmodule Mobilizon.GraphQL.Schema do
end end
def middleware(middleware, _field, %{identifier: type}) when type in [:query, :mutation] do def middleware(middleware, _field, %{identifier: type}) when type in [:query, :mutation] do
middleware ++ [ErrorHandler] [CurrentActorProvider] ++ middleware ++ [ErrorHandler]
end end
def middleware(middleware, _field, _object) do def middleware(middleware, _field, _object) do

View file

@ -59,6 +59,7 @@ defmodule Mobilizon do
cachex_spec(:config, 10, 60, 60), cachex_spec(:config, 10, 60, 60),
cachex_spec(:rich_media_cache, 10, 60, 60), cachex_spec(:rich_media_cache, 10, 60, 60),
cachex_spec(:activity_pub, 2500, 3, 15), cachex_spec(:activity_pub, 2500, 3, 15),
cachex_spec(:default_actors, 2500, 3, 15),
%{ %{
id: :cache_key_value, id: :cache_key_value,
start: {Cachex, :start_link, [:key_value]} start: {Cachex, :start_link, [:key_value]}

View file

@ -739,6 +739,8 @@ defmodule Mobilizon.Events do
""" """
@spec get_participant(integer) :: Participant.t() | nil @spec get_participant(integer) :: Participant.t() | nil
@spec get_participant(integer | String.t(), integer | String.t(), map()) ::
{:ok, Participant.t()} | {:error, :participant_not_found}
def get_participant(participant_id) do def get_participant(participant_id) do
Participant Participant
|> where([p], p.id == ^participant_id) |> where([p], p.id == ^participant_id)
@ -749,8 +751,6 @@ defmodule Mobilizon.Events do
@doc """ @doc """
Gets a single participation for an event and actor. Gets a single participation for an event and actor.
""" """
@spec get_participant(integer | String.t(), integer | String.t(), map()) ::
{:ok, Participant.t()} | {:error, :participant_not_found}
def get_participant(event_id, actor_id, params \\ %{}) def get_participant(event_id, actor_id, params \\ %{})
# This one if to check someone doesn't go to the same event twice # This one if to check someone doesn't go to the same event twice

View file

@ -117,7 +117,7 @@ defmodule Mobilizon.Users do
@doc """ @doc """
Get an user by its reset password token. Get an user by its reset password token.
""" """
@spec get_user_by_reset_password_token(String.t()) :: Actor.t() @spec get_user_by_reset_password_token(String.t()) :: Actor.t() | nil
def get_user_by_reset_password_token(token) do def get_user_by_reset_password_token(token) do
token token
|> user_by_reset_password_token_query() |> user_by_reset_password_token_query()
@ -263,22 +263,26 @@ defmodule Mobilizon.Users do
Updates user's default actor. Updates user's default actor.
Raises `Ecto.NoResultsError` if the user does not exist. Raises `Ecto.NoResultsError` if the user does not exist.
""" """
@spec update_user_default_actor(integer | String.t(), integer | String.t()) :: User.t() @spec update_user_default_actor(integer | String.t(), Actor.t() | nil) :: User.t()
def update_user_default_actor(user_id, actor_id) do def update_user_default_actor(user_id, actor) do
with _ <- actor_id = if is_nil(actor), do: nil, else: actor.id
user_id user_id
|> update_user_default_actor_query(actor_id) |> update_user_default_actor_query(actor_id)
|> Repo.update_all([]) do |> Repo.update_all([])
Cachex.put(:default_actors, to_string(user_id), actor)
user_id user_id
|> get_user!() |> get_user!()
|> Repo.preload([:default_actor]) |> Repo.preload([:default_actor])
end end
end
@doc """ @doc """
Returns the list of users. Returns the list of users.
""" """
@spec list_users(String.t(), integer | nil, integer | nil, atom | nil, atom | nil) :: Page.t() @spec list_users(String.t(), integer | nil, integer | nil, atom | nil, atom | nil) ::
Page.t(User.t())
def list_users(email \\ "", page \\ nil, limit \\ nil, sort \\ nil, direction \\ nil) def list_users(email \\ "", page \\ nil, limit \\ nil, sort \\ nil, direction \\ nil)
def list_users("", page, limit, sort, direction) do def list_users("", page, limit, sort, direction) do
@ -596,7 +600,7 @@ defmodule Mobilizon.Users do
from(a in Actor, where: a.user_id == ^user_id) from(a in Actor, where: a.user_id == ^user_id)
end end
@spec update_user_default_actor_query(integer | String.t(), integer | String.t()) :: @spec update_user_default_actor_query(integer | String.t(), integer | String.t() | nil) ::
Ecto.Query.t() Ecto.Query.t()
defp update_user_default_actor_query(user_id, actor_id) do defp update_user_default_actor_query(user_id, actor_id) do
from( from(

View file

@ -197,13 +197,12 @@ defmodule Mobilizon.Service.ActorSuspension do
defp reset_default_actor_id(%Actor{type: :Person, user: %User{id: user_id} = user, id: actor_id}) do defp reset_default_actor_id(%Actor{type: :Person, user: %User{id: user_id} = user, id: actor_id}) do
Logger.debug("reset_default_actor_id") Logger.debug("reset_default_actor_id")
new_actor_id = new_actor =
user user
|> Users.get_actors_for_user() |> Users.get_actors_for_user()
|> Enum.map(& &1.id) |> Enum.find(&(&1.id !== actor_id))
|> Enum.find(&(&1 !== actor_id))
{:ok, Users.update_user_default_actor(user_id, new_actor_id)} {:ok, Users.update_user_default_actor(user_id, new_actor)}
rescue rescue
_e in Ecto.NoResultsError -> _e in Ecto.NoResultsError ->
{:error, :user_not_found} {:error, :user_not_found}

View file

@ -7,7 +7,7 @@ defmodule Mobilizon.Web.Email.User do
import Bamboo.Phoenix import Bamboo.Phoenix
import Mobilizon.Web.Gettext, only: [gettext: 1, gettext: 2] import Mobilizon.Web.Gettext, only: [gettext: 2]
alias Mobilizon.{Config, Crypto, Users} alias Mobilizon.{Config, Crypto, Users}
alias Mobilizon.Storage.Repo alias Mobilizon.Storage.Repo
@ -57,20 +57,26 @@ defmodule Mobilizon.Web.Email.User do
|> render(:password_reset) |> render(:password_reset)
end end
@spec check_confirmation_token(String.t()) :: {:ok, User.t()} | {:error, :invalid_token} @spec check_confirmation_token(String.t()) ::
{:ok, User.t()} | {:error, :invalid_token | Ecto.Changeset.t()}
def check_confirmation_token(token) when is_binary(token) do def check_confirmation_token(token) when is_binary(token) do
with %User{} = user <- Users.get_user_by_activation_token(token), case Users.get_user_by_activation_token(token) do
{:ok, %User{} = user} <- %User{} = user ->
Users.update_user(user, %{ case Users.update_user(user, %{
confirmed_at: DateTime.utc_now() |> DateTime.truncate(:second), confirmed_at: DateTime.utc_now() |> DateTime.truncate(:second),
confirmation_sent_at: nil, confirmation_sent_at: nil,
confirmation_token: nil, confirmation_token: nil,
email: user.unconfirmed_email || user.email email: user.unconfirmed_email || user.email
}) do }) do
{:ok, %User{} = user} ->
Logger.info("User #{user.email} has been confirmed") Logger.info("User #{user.email} has been confirmed")
{:ok, user} {:ok, user}
else
_err -> {:error, %Ecto.Changeset{} = err} ->
{:error, err}
end
nil ->
{:error, :invalid_token} {:error, :invalid_token}
end end
end end
@ -98,30 +104,20 @@ defmodule Mobilizon.Web.Email.User do
Check that the provided token is correct and update provided password Check that the provided token is correct and update provided password
""" """
@spec check_reset_password_token(String.t(), String.t()) :: @spec check_reset_password_token(String.t(), String.t()) ::
{:ok, User.t()} | {:error, String.t()} {:ok, User.t()} | {:error, :user_not_found | Ecto.Changeset.t()}
def check_reset_password_token(password, token) do def check_reset_password_token(password, token) do
with %User{} = user <- Users.get_user_by_reset_password_token(token), case Users.get_user_by_reset_password_token(token) do
{:ok, %User{} = user} <- %User{} = user ->
Repo.update( user
User.password_reset_changeset(user, %{ |> User.password_reset_changeset(%{
"password" => password, "password" => password,
"reset_password_sent_at" => nil, "reset_password_sent_at" => nil,
"reset_password_token" => nil "reset_password_token" => nil
}) })
) do |> Repo.update()
{:ok, user}
else
{:error, %Ecto.Changeset{errors: [password: {"registration.error.password_too_short", _}]}} ->
{:error,
gettext(
"The password you have choosen is too short. Please make sure your password contains at least 6 charaters."
)}
_err -> nil ->
{:error, {:error, :user_not_found}
gettext(
"The token you provided is invalid. Make sure that the URL is exactly the one provided inside the email you got."
)}
end end
end end

View file

@ -170,7 +170,7 @@ defmodule Mobilizon.GraphQL.Resolvers.CommentTest do
# Change the current actor for user # Change the current actor for user
actor2 = insert(:actor, user: user) actor2 = insert(:actor, user: user)
Mobilizon.Users.update_user_default_actor(user.id, actor2.id) Mobilizon.Users.update_user_default_actor(user.id, actor2)
res = res =
conn conn
@ -183,7 +183,7 @@ defmodule Mobilizon.GraphQL.Resolvers.CommentTest do
assert hd(res["errors"])["message"] == assert hd(res["errors"])["message"] ==
"You cannot delete this comment" "You cannot delete this comment"
Mobilizon.Users.update_user_default_actor(user.id, actor.id) Mobilizon.Users.update_user_default_actor(user.id, actor)
res = res =
conn conn

View file

@ -448,7 +448,7 @@ defmodule Mobilizon.GraphQL.Resolvers.DiscussionTest do
# # Change the current actor for user # # Change the current actor for user
# actor2 = insert(:actor, user: user) # actor2 = insert(:actor, user: user)
# Mobilizon.Users.update_user_default_actor(user.id, actor2.id) # Mobilizon.Users.update_user_default_actor(user.id, actor2)
# res = # res =
# conn # conn
@ -461,7 +461,7 @@ defmodule Mobilizon.GraphQL.Resolvers.DiscussionTest do
# assert hd(res["errors"])["message"] == # assert hd(res["errors"])["message"] ==
# "You cannot delete this comment" # "You cannot delete this comment"
# Mobilizon.Users.update_user_default_actor(user.id, actor.id) # Mobilizon.Users.update_user_default_actor(user.id, actor)
# res = # res =
# conn # conn

View file

@ -1225,7 +1225,7 @@ defmodule Mobilizon.Web.Resolvers.EventTest do
%Actor{} = administrator_actor = insert(:actor, user: user) %Actor{} = administrator_actor = insert(:actor, user: user)
insert(:member, parent: group, actor: administrator_actor, role: :administrator) insert(:member, parent: group, actor: administrator_actor, role: :administrator)
%Actor{id: not_member_actor_id} = insert(:actor, user: user) %Actor{id: not_member_actor_id} = not_member_actor = insert(:actor, user: user)
%Event{} = %Event{} =
event = insert(:event, attributed_to: group, organizer_actor: administrator_actor) event = insert(:event, attributed_to: group, organizer_actor: administrator_actor)
@ -1235,7 +1235,7 @@ defmodule Mobilizon.Web.Resolvers.EventTest do
|> Map.put(:attributed_to_id, "#{group_id}") |> Map.put(:attributed_to_id, "#{group_id}")
|> Map.put(:eventId, to_string(event.id)) |> Map.put(:eventId, to_string(event.id))
Users.update_user_default_actor(user.id, member_not_approved_actor_id) Users.update_user_default_actor(user.id, member_not_approved_actor)
res = res =
conn conn
@ -1250,7 +1250,7 @@ defmodule Mobilizon.Web.Resolvers.EventTest do
assert hd(res["errors"])["message"] == assert hd(res["errors"])["message"] ==
"This profile doesn't have permission to update an event on behalf of this group" "This profile doesn't have permission to update an event on behalf of this group"
Users.update_user_default_actor(user.id, not_member_actor_id) Users.update_user_default_actor(user.id, not_member_actor)
res = res =
conn conn
@ -1265,7 +1265,7 @@ defmodule Mobilizon.Web.Resolvers.EventTest do
assert hd(res["errors"])["message"] == assert hd(res["errors"])["message"] ==
"This profile doesn't have permission to update an event on behalf of this group" "This profile doesn't have permission to update an event on behalf of this group"
Users.update_user_default_actor(user.id, member_actor_id) Users.update_user_default_actor(user.id, member_actor)
res = res =
conn conn
@ -1280,7 +1280,7 @@ defmodule Mobilizon.Web.Resolvers.EventTest do
assert hd(res["errors"])["message"] == assert hd(res["errors"])["message"] ==
"This profile doesn't have permission to update an event on behalf of this group" "This profile doesn't have permission to update an event on behalf of this group"
Users.update_user_default_actor(user.id, moderator_actor_id) Users.update_user_default_actor(user.id, moderator_actor)
res = res =
conn conn

View file

@ -423,7 +423,7 @@ defmodule Mobilizon.GraphQL.Resolvers.MemberTest do
} do } do
user = insert(:user) user = insert(:user)
actor = insert(:actor, user: user) actor = insert(:actor, user: user)
Mobilizon.Users.update_user_default_actor(user.id, actor.id) Mobilizon.Users.update_user_default_actor(user.id, actor)
%Member{id: member_id} = %Member{id: member_id} =
insert(:member, %{actor: target_actor, parent: group, role: :member}) insert(:member, %{actor: target_actor, parent: group, role: :member})
@ -449,7 +449,7 @@ defmodule Mobilizon.GraphQL.Resolvers.MemberTest do
group: group, group: group,
target_actor: target_actor target_actor: target_actor
} do } do
Mobilizon.Users.update_user_default_actor(user.id, actor.id) Mobilizon.Users.update_user_default_actor(user.id, actor)
insert(:member, actor: actor, parent: group, role: :administrator) insert(:member, actor: actor, parent: group, role: :administrator)
%Member{id: member_id} = %Member{id: member_id} =
@ -504,7 +504,7 @@ defmodule Mobilizon.GraphQL.Resolvers.MemberTest do
actor: actor, actor: actor,
group: group group: group
} do } do
Mobilizon.Users.update_user_default_actor(user.id, actor.id) Mobilizon.Users.update_user_default_actor(user.id, actor)
%Member{id: member_id} = insert(:member, actor: actor, parent: group, role: :administrator) %Member{id: member_id} = insert(:member, actor: actor, parent: group, role: :administrator)
res = res =

View file

@ -873,6 +873,7 @@ defmodule Mobilizon.GraphQL.Resolvers.UserTest do
test "test refresh_token/3 with an appropriate token", context do test "test refresh_token/3 with an appropriate token", context do
user = insert(:user) user = insert(:user)
insert(:actor, user: user)
{:ok, refresh_token} = Authenticator.generate_refresh_token(user) {:ok, refresh_token} = Authenticator.generate_refresh_token(user)
mutation = """ mutation = """

View file

@ -12,7 +12,9 @@ defmodule Mobilizon.Service.ActorSuspensionTest do
describe "suspend a person" do describe "suspend a person" do
setup do setup do
%Actor{} = actor = insert(:actor) user = insert(:user)
%Actor{} = actor = insert(:actor, user: user)
insert(:actor, user: user)
%Comment{} = comment = insert(:comment, actor: actor) %Comment{} = comment = insert(:comment, actor: actor)
%Event{} = event = insert(:event, organizer_actor: actor) %Event{} = event = insert(:event, organizer_actor: actor)