2021-08-13 11:22:04 +02:00
|
|
|
defmodule Mobilizon.GraphQL.Resolvers.Event.Utils do
|
|
|
|
@moduledoc """
|
|
|
|
Tools to test permission on events
|
|
|
|
"""
|
|
|
|
|
|
|
|
alias Mobilizon.Actors.Actor
|
2022-05-03 11:40:16 +02:00
|
|
|
alias Mobilizon.Events
|
2021-08-13 11:22:04 +02:00
|
|
|
alias Mobilizon.Events.Event
|
2022-05-03 11:40:16 +02:00
|
|
|
alias Mobilizon.Federation.ActivityPub.{Permission, Relay}
|
2021-09-10 11:27:59 +02:00
|
|
|
import Mobilizon.Service.Guards, only: [is_valid_string: 1]
|
2021-08-13 11:22:04 +02:00
|
|
|
|
2021-10-04 18:59:41 +02:00
|
|
|
@spec can_event_be_updated_by?(Event.t(), Actor.t()) ::
|
2021-09-10 11:27:59 +02:00
|
|
|
boolean
|
2021-08-13 11:22:04 +02:00
|
|
|
def can_event_be_updated_by?(
|
|
|
|
%Event{attributed_to: %Actor{type: :Group}} = event,
|
|
|
|
%Actor{} = actor_member
|
|
|
|
) do
|
|
|
|
Permission.can_update_group_object?(actor_member, event)
|
|
|
|
end
|
|
|
|
|
|
|
|
def can_event_be_updated_by?(
|
|
|
|
%Event{} = event,
|
|
|
|
%Actor{id: actor_member_id}
|
|
|
|
) do
|
|
|
|
Event.can_be_managed_by?(event, actor_member_id)
|
|
|
|
end
|
|
|
|
|
2021-10-04 18:59:41 +02:00
|
|
|
@spec can_event_be_deleted_by?(Event.t(), Actor.t()) ::
|
2021-09-10 11:27:59 +02:00
|
|
|
boolean
|
2021-08-13 11:22:04 +02:00
|
|
|
def can_event_be_deleted_by?(
|
2021-09-10 11:27:59 +02:00
|
|
|
%Event{attributed_to: %Actor{type: :Group}, id: event_id, url: event_url} = event,
|
2021-08-13 11:22:04 +02:00
|
|
|
%Actor{} = actor_member
|
2021-09-10 11:27:59 +02:00
|
|
|
)
|
|
|
|
when is_valid_string(event_id) and is_valid_string(event_url) do
|
2021-08-13 11:22:04 +02:00
|
|
|
Permission.can_delete_group_object?(actor_member, event)
|
|
|
|
end
|
|
|
|
|
|
|
|
def can_event_be_deleted_by?(%Event{} = event, %Actor{id: actor_member_id}) do
|
|
|
|
Event.can_be_managed_by?(event, actor_member_id)
|
|
|
|
end
|
2021-11-08 18:46:04 +01:00
|
|
|
|
|
|
|
@spec check_event_access?(Event.t()) :: boolean()
|
|
|
|
def check_event_access?(%Event{local: true}), do: true
|
|
|
|
|
|
|
|
def check_event_access?(%Event{url: url}) do
|
2022-05-03 11:40:16 +02:00
|
|
|
relay_actor = Relay.get_actor()
|
|
|
|
Events.check_if_event_has_instance_follow(url, relay_actor.id)
|
2021-11-08 18:46:04 +01:00
|
|
|
end
|
2021-08-13 11:22:04 +02:00
|
|
|
end
|