1
0
Fork 0
mirror of https://framagit.org/framasoft/mobilizon.git synced 2024-12-21 15:32:32 +00:00

Some fixes

Signed-off-by: Thomas Citharel <tcit@tcit.fr>
This commit is contained in:
Thomas Citharel 2021-10-11 17:13:57 +02:00
parent d58ca5743d
commit 62af1e72d6
No known key found for this signature in database
GPG key ID: A061B9DDE0CA0773
5 changed files with 54 additions and 27 deletions

View file

@ -18,7 +18,14 @@
"paths": { "paths": {
"@/*": ["src/*"] "@/*": ["src/*"]
}, },
"lib": ["esnext", "dom", "es2017.intl", "dom.iterable", "scripthost", "webworker"] "lib": [
"esnext",
"dom",
"es2017.intl",
"dom.iterable",
"scripthost",
"webworker"
]
}, },
"include": [ "include": [
"src/**/*.ts", "src/**/*.ts",

View file

@ -266,15 +266,9 @@ defmodule Mobilizon.GraphQL.Resolvers.Event do
%{organizer_actor_id: organizer_actor_id} = args, %{organizer_actor_id: organizer_actor_id} = args,
%{context: %{current_user: %User{} = user}} = _resolution %{context: %{current_user: %User{} = user}} = _resolution
) do ) do
if Config.only_groups_can_create_events?() and Map.get(args, :attributed_to_id) == nil do case User.owns_actor(user, organizer_actor_id) do
{:error, {:is_owned, %Actor{} = organizer_actor} ->
dgettext( if can_create_event?(args) do
"errors",
"Only groups can create events"
)}
else
case User.owns_actor(user, organizer_actor_id) do
{:is_owned, %Actor{} = organizer_actor} ->
if is_organizer_group_member?(args) do if is_organizer_group_member?(args) do
args_with_organizer = args_with_organizer =
args |> Map.put(:organizer_actor, organizer_actor) |> extract_timezone(user.id) 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" "Organizer profile doesn't have permission to create an event on behalf of this group"
)} )}
end end
else
{:error,
dgettext(
"errors",
"Only groups can create events"
)}
end
{:is_owned, nil} -> {:is_owned, nil} ->
{:error, dgettext("errors", "Organizer profile is not owned by the user")} {:error, dgettext("errors", "Organizer profile is not owned by the user")}
end
end end
end end
@ -313,6 +313,15 @@ defmodule Mobilizon.GraphQL.Resolvers.Event do
{:error, dgettext("errors", "You need to be logged-in to create events")} {:error, dgettext("errors", "You need to be logged-in to create events")}
end 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 """ @doc """
Update an event Update an event
""" """

View file

@ -10,7 +10,7 @@ defmodule Mobilizon.GraphQL.Resolvers.Group do
alias Mobilizon.Federation.ActivityPub.Actions alias Mobilizon.Federation.ActivityPub.Actions
alias Mobilizon.Federation.ActivityPub.Actor, as: ActivityPubActor alias Mobilizon.Federation.ActivityPub.Actor, as: ActivityPubActor
alias Mobilizon.GraphQL.API alias Mobilizon.GraphQL.API
alias Mobilizon.Users.User alias Mobilizon.Users.{User, UserRole}
alias Mobilizon.Web.Upload alias Mobilizon.Web.Upload
import Mobilizon.Web.Gettext import Mobilizon.Web.Gettext
@ -143,14 +143,14 @@ defmodule Mobilizon.GraphQL.Resolvers.Group do
} }
} }
) do ) do
if Config.only_admin_can_create_groups?() and not is_admin(role) do if can_create_group?(role) do
{:error, "only admins can create groups"} args =
else args
with args when is_map(args) <- |> Map.update(:preferred_username, "", &String.downcase/1)
Map.update(args, :preferred_username, "", &String.downcase/1), |> Map.put(:creator_actor, creator_actor)
args when is_map(args) <- Map.put(args, :creator_actor, creator_actor), |> Map.put(: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)}, with {:picture, args} when is_map(args) <- {:picture, save_attached_pictures(args)},
{:ok, _activity, %Actor{type: :Group} = group} <- {:ok, _activity, %Actor{type: :Group} = group} <-
API.Groups.create_group(args) do API.Groups.create_group(args) do
{:ok, group} {:ok, group}
@ -161,6 +161,8 @@ defmodule Mobilizon.GraphQL.Resolvers.Group do
{:error, err} when is_binary(err) -> {:error, err} when is_binary(err) ->
{:error, err} {:error, err}
end end
else
{:error, dgettext("errors", "Only admins can create groups")}
end end
end end
@ -168,6 +170,15 @@ defmodule Mobilizon.GraphQL.Resolvers.Group do
{:error, "You need to be logged-in to create a group"} {:error, "You need to be logged-in to create a group"}
end 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 """ @doc """
Update a group. The creator is automatically added as admin Update a group. The creator is automatically added as admin
""" """

View file

@ -60,7 +60,7 @@ defimpl Mobilizon.Service.Metadata, for: Mobilizon.Events.Event do
locale locale
) do ) do
language = build_language(language, locale) language = build_language(language, locale)
begins_on = build_begins_on(begins_on, timezone, language) begins_on = build_begins_on(begins_on, timezone)
begins_on begins_on
|> datetime_to_string(language) |> 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() @spec build_language(String.t() | nil, String.t()) :: String.t()
defp build_language(language, locale), do: language || locale defp build_language(language, locale), do: language || locale
@spec build_begins_on(DateTime.t(), String.t() | nil, String.t()) :: DateTime.t() @spec build_begins_on(DateTime.t(), String.t() | nil) :: DateTime.t()
defp build_begins_on(begins_on, timezone, language) do defp build_begins_on(begins_on, timezone) do
if timezone do if timezone do
case DateTime.shift_zone(begins_on, timezone) do case DateTime.shift_zone(begins_on, timezone) do
{:ok, begins_on} -> begins_on {:ok, begins_on} -> begins_on

View file

@ -1,3 +1,3 @@
{ {
"extends": "./js/tsconfig.json" "extends": "./js/tsconfig.json"
} }