2018-11-06 09:30:27 +00:00
|
|
|
defmodule MobilizonWeb.Resolvers.Event do
|
2019-01-03 13:59:59 +00:00
|
|
|
@moduledoc """
|
|
|
|
Handles the event-related GraphQL calls
|
|
|
|
"""
|
2018-11-07 15:09:28 +00:00
|
|
|
alias Mobilizon.Service.ActivityPub
|
2018-12-14 16:41:55 +00:00
|
|
|
alias Mobilizon.Activity
|
2018-11-12 22:30:47 +00:00
|
|
|
alias Mobilizon.Actors
|
2018-12-14 16:41:55 +00:00
|
|
|
alias Mobilizon.Events.Event
|
2018-11-07 15:09:28 +00:00
|
|
|
|
2018-12-21 10:45:55 +00:00
|
|
|
# We limit the max number of events that can be retrieved
|
|
|
|
@event_max_limit 100
|
|
|
|
|
|
|
|
def list_events(_parent, %{page: page, limit: limit}, _resolution)
|
|
|
|
when limit < @event_max_limit do
|
2018-12-14 10:23:36 +00:00
|
|
|
{:ok, Mobilizon.Events.list_events(page, limit)}
|
2018-11-06 09:30:27 +00:00
|
|
|
end
|
|
|
|
|
2018-12-21 11:21:56 +00:00
|
|
|
def list_events(_parent, %{page: _page, limit: _limit}, _resolution) do
|
2018-12-21 10:45:55 +00:00
|
|
|
{:error, :events_max_limit_reached}
|
|
|
|
end
|
|
|
|
|
2018-11-06 09:30:27 +00:00
|
|
|
def find_event(_parent, %{uuid: uuid}, _resolution) do
|
|
|
|
case Mobilizon.Events.get_event_full_by_uuid(uuid) do
|
|
|
|
nil ->
|
|
|
|
{:error, "Event with UUID #{uuid} not found"}
|
|
|
|
|
|
|
|
event ->
|
|
|
|
{:ok, event}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-11-12 22:30:47 +00:00
|
|
|
@doc """
|
|
|
|
List participant for event (separate request)
|
|
|
|
"""
|
2018-11-06 09:30:27 +00:00
|
|
|
def list_participants_for_event(_parent, %{uuid: uuid}, _resolution) do
|
|
|
|
{:ok, Mobilizon.Events.list_participants_for_event(uuid)}
|
|
|
|
end
|
|
|
|
|
2018-11-12 22:30:47 +00:00
|
|
|
@doc """
|
|
|
|
List participants for event (through an event request)
|
|
|
|
"""
|
2018-12-14 10:23:36 +00:00
|
|
|
def list_participants_for_event(%{uuid: uuid}, %{page: page, limit: limit}, _resolution) do
|
|
|
|
{:ok, Mobilizon.Events.list_participants_for_event(uuid, page, limit)}
|
2018-11-12 22:30:47 +00:00
|
|
|
end
|
|
|
|
|
2018-11-06 09:30:27 +00:00
|
|
|
@doc """
|
|
|
|
Search events by title
|
|
|
|
"""
|
|
|
|
def search_events(_parent, %{search: search, page: page, limit: limit}, _resolution) do
|
|
|
|
{:ok, Mobilizon.Events.find_events_by_name(search, page, limit)}
|
|
|
|
end
|
|
|
|
|
|
|
|
@doc """
|
|
|
|
Search events and actors by title
|
|
|
|
"""
|
|
|
|
def search_events_and_actors(_parent, %{search: search, page: page, limit: limit}, _resolution) do
|
2018-11-12 22:30:47 +00:00
|
|
|
search = String.trim(search)
|
2018-11-07 15:09:42 +00:00
|
|
|
|
2018-11-06 09:30:27 +00:00
|
|
|
found =
|
2018-11-07 15:09:28 +00:00
|
|
|
case String.contains?(search, "@") do
|
|
|
|
true ->
|
|
|
|
with {:ok, actor} <- ActivityPub.find_or_make_actor_from_nickname(search) do
|
|
|
|
actor
|
|
|
|
else
|
|
|
|
{:error, _err} ->
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
|
|
|
|
_ ->
|
|
|
|
Mobilizon.Events.find_events_by_name(search, page, limit) ++
|
|
|
|
Mobilizon.Actors.find_actors_by_username_or_name(search, page, limit)
|
|
|
|
end
|
2018-11-06 09:30:27 +00:00
|
|
|
|
|
|
|
require Logger
|
|
|
|
Logger.debug(inspect(found))
|
|
|
|
{:ok, found}
|
|
|
|
end
|
|
|
|
|
2018-12-14 16:41:55 +00:00
|
|
|
@doc """
|
|
|
|
Create an event
|
|
|
|
"""
|
2018-11-06 09:30:27 +00:00
|
|
|
def create_event(_parent, args, %{context: %{current_user: user}}) do
|
2018-12-14 16:41:55 +00:00
|
|
|
with {:ok, %Activity{data: %{"object" => %{"type" => "Event"} = object}}} <-
|
|
|
|
args
|
|
|
|
# Set default organizer_actor_id if none set
|
|
|
|
|> Map.update(
|
|
|
|
:organizer_actor_username,
|
|
|
|
Actors.get_actor_for_user(user).preferred_username,
|
|
|
|
& &1
|
|
|
|
)
|
|
|
|
|> MobilizonWeb.API.Events.create_event() do
|
|
|
|
{:ok,
|
|
|
|
%Event{
|
|
|
|
title: object["name"],
|
|
|
|
description: object["content"],
|
|
|
|
uuid: object["uuid"],
|
|
|
|
url: object["id"]
|
|
|
|
}}
|
|
|
|
end
|
2018-11-06 09:30:27 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def create_event(_parent, _args, _resolution) do
|
|
|
|
{:error, "You need to be logged-in to create events"}
|
|
|
|
end
|
|
|
|
end
|