From 62af1e72d6b24a2706e225e5073f161751de3f5f Mon Sep 17 00:00:00 2001 From: Thomas Citharel Date: Mon, 11 Oct 2021 17:13:57 +0200 Subject: [PATCH] Some fixes Signed-off-by: Thomas Citharel --- js/tsconfig.json | 9 ++++++++- lib/graphql/resolvers/event.ex | 33 +++++++++++++++++++++------------ lib/graphql/resolvers/group.ex | 29 ++++++++++++++++++++--------- lib/service/metadata/event.ex | 6 +++--- tsconfig.json | 4 ++-- 5 files changed, 54 insertions(+), 27 deletions(-) diff --git a/js/tsconfig.json b/js/tsconfig.json index 99369f90..cbc948bc 100644 --- a/js/tsconfig.json +++ b/js/tsconfig.json @@ -18,7 +18,14 @@ "paths": { "@/*": ["src/*"] }, - "lib": ["esnext", "dom", "es2017.intl", "dom.iterable", "scripthost", "webworker"] + "lib": [ + "esnext", + "dom", + "es2017.intl", + "dom.iterable", + "scripthost", + "webworker" + ] }, "include": [ "src/**/*.ts", diff --git a/lib/graphql/resolvers/event.ex b/lib/graphql/resolvers/event.ex index 3ea71707..86f56b56 100644 --- a/lib/graphql/resolvers/event.ex +++ b/lib/graphql/resolvers/event.ex @@ -266,15 +266,9 @@ defmodule Mobilizon.GraphQL.Resolvers.Event do %{organizer_actor_id: organizer_actor_id} = args, %{context: %{current_user: %User{} = user}} = _resolution ) do - if Config.only_groups_can_create_events?() and Map.get(args, :attributed_to_id) == nil do - {:error, - dgettext( - "errors", - "Only groups can create events" - )} - else - case User.owns_actor(user, organizer_actor_id) do - {:is_owned, %Actor{} = organizer_actor} -> + case User.owns_actor(user, organizer_actor_id) do + {:is_owned, %Actor{} = organizer_actor} -> + if can_create_event?(args) do if is_organizer_group_member?(args) do args_with_organizer = args |> Map.put(:organizer_actor, organizer_actor) |> extract_timezone(user.id) @@ -302,10 +296,16 @@ defmodule Mobilizon.GraphQL.Resolvers.Event do "Organizer profile doesn't have permission to create an event on behalf of this group" )} end + else + {:error, + dgettext( + "errors", + "Only groups can create events" + )} + end - {:is_owned, nil} -> - {:error, dgettext("errors", "Organizer profile is not owned by the user")} - end + {:is_owned, nil} -> + {:error, dgettext("errors", "Organizer profile is not owned by the user")} end end @@ -313,6 +313,15 @@ defmodule Mobilizon.GraphQL.Resolvers.Event do {:error, dgettext("errors", "You need to be logged-in to create events")} end + @spec can_create_event?(map()) :: boolean() + defp can_create_event?(args) do + if Config.only_groups_can_create_events?() do + Map.get(args, :attributed_to_id) != nil + else + true + end + end + @doc """ Update an event """ diff --git a/lib/graphql/resolvers/group.ex b/lib/graphql/resolvers/group.ex index f003dae5..5a4ea22e 100644 --- a/lib/graphql/resolvers/group.ex +++ b/lib/graphql/resolvers/group.ex @@ -10,7 +10,7 @@ defmodule Mobilizon.GraphQL.Resolvers.Group do alias Mobilizon.Federation.ActivityPub.Actions alias Mobilizon.Federation.ActivityPub.Actor, as: ActivityPubActor alias Mobilizon.GraphQL.API - alias Mobilizon.Users.User + alias Mobilizon.Users.{User, UserRole} alias Mobilizon.Web.Upload import Mobilizon.Web.Gettext @@ -143,14 +143,14 @@ defmodule Mobilizon.GraphQL.Resolvers.Group do } } ) do - if Config.only_admin_can_create_groups?() and not is_admin(role) do - {:error, "only admins can create groups"} - else - with 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_id, creator_actor_id), - {:picture, args} when is_map(args) <- {:picture, save_attached_pictures(args)}, + if can_create_group?(role) do + args = + args + |> Map.update(:preferred_username, "", &String.downcase/1) + |> Map.put(:creator_actor, creator_actor) + |> Map.put(:creator_actor_id, creator_actor_id) + + with {:picture, args} when is_map(args) <- {:picture, save_attached_pictures(args)}, {:ok, _activity, %Actor{type: :Group} = group} <- API.Groups.create_group(args) do {:ok, group} @@ -161,6 +161,8 @@ defmodule Mobilizon.GraphQL.Resolvers.Group do {:error, err} when is_binary(err) -> {:error, err} end + else + {:error, dgettext("errors", "Only admins can create groups")} end end @@ -168,6 +170,15 @@ defmodule Mobilizon.GraphQL.Resolvers.Group do {:error, "You need to be logged-in to create a group"} end + @spec can_create_group?(UserRole.t()) :: boolean() + defp can_create_group?(role) do + if Config.only_admin_can_create_groups?() do + is_admin(role) + else + true + end + end + @doc """ Update a group. The creator is automatically added as admin """ diff --git a/lib/service/metadata/event.ex b/lib/service/metadata/event.ex index cf47ea4f..6dede3f2 100644 --- a/lib/service/metadata/event.ex +++ b/lib/service/metadata/event.ex @@ -60,7 +60,7 @@ defimpl Mobilizon.Service.Metadata, for: Mobilizon.Events.Event do locale ) do language = build_language(language, locale) - begins_on = build_begins_on(begins_on, timezone, language) + begins_on = build_begins_on(begins_on, timezone) begins_on |> datetime_to_string(language) @@ -74,8 +74,8 @@ defimpl Mobilizon.Service.Metadata, for: Mobilizon.Events.Event do @spec build_language(String.t() | nil, String.t()) :: String.t() defp build_language(language, locale), do: language || locale - @spec build_begins_on(DateTime.t(), String.t() | nil, String.t()) :: DateTime.t() - defp build_begins_on(begins_on, timezone, language) do + @spec build_begins_on(DateTime.t(), String.t() | nil) :: DateTime.t() + defp build_begins_on(begins_on, timezone) do if timezone do case DateTime.shift_zone(begins_on, timezone) do {:ok, begins_on} -> begins_on diff --git a/tsconfig.json b/tsconfig.json index ac08cb25..5a682fcc 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -1,3 +1,3 @@ { - "extends": "./js/tsconfig.json" -} \ No newline at end of file + "extends": "./js/tsconfig.json" +}