Add address input and refactor federation stuff

Signed-off-by: Thomas Citharel <tcit@tcit.fr>
This commit is contained in:
Thomas Citharel
2019-07-30 10:35:29 +02:00
parent bcfc26ee59
commit 5fbaf42cad
34 changed files with 729 additions and 192 deletions

View File

@@ -15,7 +15,8 @@ defmodule MobilizonWeb.API.Comments do
Creates a comment from an actor and a status
"""
@spec create_comment(String.t(), String.t(), String.t()) :: {:ok, Activity.t()} | any()
@spec create_comment(String.t(), String.t(), String.t()) ::
{:ok, Activity.t(), Comment.t()} | any()
def create_comment(
from_username,
status,

View File

@@ -2,6 +2,7 @@ defmodule MobilizonWeb.API.Events do
@moduledoc """
API for Events
"""
alias Mobilizon.Addresses
alias Mobilizon.Actors
alias Mobilizon.Actors.Actor
alias Mobilizon.Service.ActivityPub
@@ -11,7 +12,7 @@ defmodule MobilizonWeb.API.Events do
@doc """
Create an event
"""
@spec create_event(map()) :: {:ok, Activity.t()} | any()
@spec create_event(map()) :: {:ok, Activity.t(), Event.t()} | any()
def create_event(
%{
title: title,
@@ -22,10 +23,9 @@ defmodule MobilizonWeb.API.Events do
tags: tags
} = args
) do
require Logger
with %Actor{url: url} = actor <-
Actors.get_local_actor_with_everything(organizer_actor_id),
physical_address <- Map.get(args, :physical_address, nil),
title <- String.trim(title),
visibility <- Map.get(args, :visibility, :public),
picture <- Map.get(args, :picture, nil),
@@ -34,14 +34,12 @@ defmodule MobilizonWeb.API.Events do
event <-
ActivityPubUtils.make_event_data(
url,
to,
%{to: to, cc: cc},
title,
content_html,
picture,
tags,
cc,
%{begins_on: begins_on},
category
%{begins_on: begins_on, physical_address: physical_address, category: category}
) do
ActivityPub.create(%{
to: ["https://www.w3.org/ns/activitystreams#Public"],
@@ -51,4 +49,15 @@ defmodule MobilizonWeb.API.Events do
})
end
end
defp get_physical_address(address_id) when is_number(address_id),
do: Addresses.get_address!(address_id)
defp get_physical_address(address_id) when is_binary(address_id) do
with {address_id, ""} <- Integer.parse(address_id) do
get_physical_address(address_id)
end
end
defp get_physical_address(nil), do: nil
end

View File

@@ -11,7 +11,7 @@ defmodule MobilizonWeb.API.Groups do
@doc """
Create a group
"""
@spec create_group(map()) :: {:ok, Activity.t()} | any()
@spec create_group(map()) :: {:ok, Activity.t(), Group.t()} | any()
def create_group(
%{
preferred_username: title,

View File

@@ -11,7 +11,7 @@ defmodule MobilizonWeb.Resolvers.Address do
Search an address
"""
@spec search(map(), map(), map()) :: {:ok, list(Address.t())}
def search(_parent, %{query: query}, %{context: %{ip: ip}}) do
def search(_parent, %{query: query, page: _page, limit: _limit}, %{context: %{ip: ip}}) do
country = Geolix.lookup(ip) |> Map.get(:country, nil)
local_addresses = Task.async(fn -> Addresses.search_addresses(query, country: country) end)

View File

@@ -11,14 +11,10 @@ defmodule MobilizonWeb.Resolvers.Comment do
def create_comment(_parent, %{text: comment, actor_username: username}, %{
context: %{current_user: %User{} = _user}
}) do
with {:ok, %Activity{data: %{"object" => %{"type" => "Note"} = object}}} <-
with {:ok, %Activity{data: %{"object" => %{"type" => "Note"} = _object}},
%Comment{} = comment} <-
Comments.create_comment(username, comment) do
{:ok,
%Comment{
text: object["content"],
url: object["id"],
uuid: object["uuid"]
}}
{:ok, comment}
end
end

View File

@@ -3,6 +3,8 @@ defmodule MobilizonWeb.Resolvers.Event do
Handles the event-related GraphQL calls
"""
alias Mobilizon.Activity
alias Mobilizon.Addresses
alias Mobilizon.Addresses.Address
alias Mobilizon.Events
alias Mobilizon.Events.{Event, Participant}
alias Mobilizon.Media.Picture
@@ -190,25 +192,10 @@ defmodule MobilizonWeb.Resolvers.Event do
"""
def create_event(_parent, args, %{context: %{current_user: _user}} = _resolution) do
with {:ok, args} <- save_attached_picture(args),
{:ok, %Activity{data: %{"object" => %{"type" => "Event"} = object}}} <-
{:ok, args} <- save_physical_address(args),
{:ok, %Activity{data: %{"object" => %{"type" => "Event"} = _object}}, %Event{} = event} <-
MobilizonWeb.API.Events.create_event(args) do
res = %{
title: object["name"],
description: object["content"],
uuid: object["uuid"],
url: object["id"]
}
res =
if Map.has_key?(object, "attachment"),
do:
Map.put(res, :picture, %{
name: object["attachment"] |> hd() |> Map.get("name"),
url: object["attachment"] |> hd() |> Map.get("url") |> hd() |> Map.get("href")
}),
else: res
{:ok, res}
{:ok, event}
end
end
@@ -237,6 +224,25 @@ defmodule MobilizonWeb.Resolvers.Event do
@spec save_attached_picture(map()) :: {:ok, map()}
defp save_attached_picture(args), do: {:ok, args}
@spec save_physical_address(map()) :: {:ok, map()}
defp save_physical_address(%{physical_address: %{url: physical_address_url}} = args) do
with %Address{} = address <- Addresses.get_address_by_url(physical_address_url),
args <- Map.put(args, :physical_address, address) do
{:ok, args}
end
end
# @spec save_physical_address(map()) :: {:ok, map()}
# defp save_physical_address(%{physical_address: address} = args) do
# with {:ok, %Address{} = address} <- Addresses.create_address(address),
# args <- Map.put(args, :physical_address, address) do
# {:ok, args}
# end
# end
@spec save_physical_address(map()) :: {:ok, map()}
defp save_physical_address(args), do: {:ok, args}
@doc """
Delete an event
"""

View File

@@ -47,20 +47,15 @@ defmodule MobilizonWeb.Resolvers.Group do
:ok,
%Activity{
data: %{
"object" => %{"type" => "Group"} = object
"object" => %{"type" => "Group"} = _object
}
}
},
%Actor{} = group
} <-
MobilizonWeb.API.Groups.create_group(args) do
{
:ok,
%Actor{
preferred_username: object["preferredUsername"],
summary: object["summary"],
type: :Group,
# uuid: object["uuid"],
url: object["id"]
}
group
}
end

View File

@@ -14,6 +14,7 @@ defmodule MobilizonWeb.Schema.AddressType do
field(:region, :string)
field(:country, :string)
field(:description, :string)
field(:url, :string)
end
object :phone_address do
@@ -26,10 +27,25 @@ defmodule MobilizonWeb.Schema.AddressType do
field(:info, :string)
end
input_object :address_input do
# Either a full picture object
field(:geom, :point, description: "The geocoordinates for the point where this address is")
field(:floor, :string, description: "The floor this event is at")
field(:street, :string, description: "The address's street name (with number)")
field(:locality, :string, description: "The address's locality")
field(:postal_code, :string)
field(:region, :string)
field(:country, :string)
field(:description, :string)
field(:url, :string)
end
object :address_queries do
@desc "Search for an address"
field :search_address, type: list_of(:address) do
arg(:query, non_null(:string))
arg(:page, :integer, default_value: 1)
arg(:limit, :integer, default_value: 10)
resolve(&Resolvers.Address.search/3)
end

View File

@@ -22,7 +22,7 @@ defmodule MobilizonWeb.Schema.EventType do
field(:begins_on, :datetime, description: "Datetime for when the event begins")
field(:ends_on, :datetime, description: "Datetime for when the event ends")
field(:status, :event_status, description: "Status of the event")
field(:visibility, :event_visibility, description: "The event's visibility")
field(:visibility, :event_visibility, description: "The event's visibility")
field(:picture, :picture,
description: "The event's picture",
@@ -132,6 +132,7 @@ defmodule MobilizonWeb.Schema.EventType do
arg(:phone_address, :string)
arg(:organizer_actor_id, non_null(:id))
arg(:category, non_null(:string))
arg(:physical_address, :address_input)
resolve(&Event.create_event/3)
end