Fix software design suggestions
This commit is contained in:
committed by
Thomas Citharel
parent
aed824f1aa
commit
0a0d07cf38
@@ -13,7 +13,7 @@ defmodule Mobilizon.Service.ActivityPub do
|
||||
alias Mobilizon.{Actors, Config, Events}
|
||||
alias Mobilizon.Actors.{Actor, Follower}
|
||||
alias Mobilizon.Events.{Comment, Event, Participant}
|
||||
alias Mobilizon.Service.ActivityPub.{Activity, Convertible, Transmogrifier}
|
||||
alias Mobilizon.Service.ActivityPub.{Activity, Converter, Convertible, Relay, Transmogrifier}
|
||||
alias Mobilizon.Service.{Federator, WebFinger}
|
||||
alias Mobilizon.Service.HTTPSignatures.Signature
|
||||
|
||||
@@ -535,7 +535,8 @@ defmodule Mobilizon.Service.ActivityPub do
|
||||
|
||||
if public && !is_delete_activity?(activity) && Config.get([:instance, :allow_relay]) do
|
||||
Logger.info(fn -> "Relaying #{activity.data["id"]} out" end)
|
||||
Mobilizon.Service.ActivityPub.Relay.publish(activity)
|
||||
|
||||
Relay.publish(activity)
|
||||
end
|
||||
|
||||
followers =
|
||||
@@ -694,7 +695,7 @@ defmodule Mobilizon.Service.ActivityPub do
|
||||
%Activity{
|
||||
recipients: ["https://www.w3.org/ns/activitystreams#Public"],
|
||||
actor: event.organizer_actor.url,
|
||||
data: event |> Mobilizon.Service.ActivityPub.Converters.Event.model_to_as(),
|
||||
data: Converter.Event.model_to_as(event),
|
||||
local: local
|
||||
}
|
||||
end
|
||||
@@ -705,7 +706,7 @@ defmodule Mobilizon.Service.ActivityPub do
|
||||
%Activity{
|
||||
recipients: ["https://www.w3.org/ns/activitystreams#Public"],
|
||||
actor: comment.actor.url,
|
||||
data: comment |> Mobilizon.Service.ActivityPub.Converters.Comment.model_to_as(),
|
||||
data: Converter.Comment.model_to_as(comment),
|
||||
local: local
|
||||
}
|
||||
end
|
||||
|
||||
@@ -1,16 +1,11 @@
|
||||
defmodule Mobilizon.Service.ActivityPub.Converter do
|
||||
@moduledoc """
|
||||
Converter behaviour
|
||||
Converter behaviour.
|
||||
|
||||
This module allows to convert from ActivityStream format to our own internal one, and back
|
||||
This module allows to convert from ActivityStream format to our own internal
|
||||
one, and back.
|
||||
"""
|
||||
@callback as_to_model_data(map()) :: map()
|
||||
@callback model_to_as(struct()) :: map()
|
||||
end
|
||||
|
||||
defprotocol Mobilizon.Service.ActivityPub.Convertible do
|
||||
@type activitystreams :: map()
|
||||
|
||||
@spec model_to_as(t) :: activitystreams
|
||||
def model_to_as(convertible)
|
||||
@callback as_to_model_data(map) :: map
|
||||
@callback model_to_as(struct) :: map
|
||||
end
|
||||
|
||||
@@ -1,21 +1,27 @@
|
||||
defmodule Mobilizon.Service.ActivityPub.Converters.Actor do
|
||||
defmodule Mobilizon.Service.ActivityPub.Converter.Actor do
|
||||
@moduledoc """
|
||||
Actor converter
|
||||
Actor converter.
|
||||
|
||||
This module allows to convert events from ActivityStream format to our own
|
||||
internal one, and back.
|
||||
"""
|
||||
|
||||
alias Mobilizon.Actors.Actor, as: ActorModel
|
||||
alias Mobilizon.Service.ActivityPub.Converter
|
||||
alias Mobilizon.Service.ActivityPub.{Converter, Convertible}
|
||||
|
||||
@behaviour Converter
|
||||
|
||||
defimpl Convertible, for: ActorModel do
|
||||
alias Mobilizon.Service.ActivityPub.Converter.Actor, as: ActorConverter
|
||||
|
||||
defdelegate model_to_as(actor), to: ActorConverter
|
||||
end
|
||||
|
||||
@doc """
|
||||
Converts an AP object data to our internal data structure
|
||||
Converts an AP object data to our internal data structure.
|
||||
"""
|
||||
@impl Converter
|
||||
@spec as_to_model_data(map()) :: map()
|
||||
@spec as_to_model_data(map) :: map
|
||||
def as_to_model_data(object) do
|
||||
avatar =
|
||||
object["icon"]["url"] &&
|
||||
@@ -45,10 +51,10 @@ defmodule Mobilizon.Service.ActivityPub.Converters.Actor do
|
||||
end
|
||||
|
||||
@doc """
|
||||
Convert an actor struct to an ActivityStream representation
|
||||
Convert an actor struct to an ActivityStream representation.
|
||||
"""
|
||||
@impl Converter
|
||||
@spec model_to_as(ActorModel.t()) :: map()
|
||||
@spec model_to_as(ActorModel.t()) :: map
|
||||
def model_to_as(%ActorModel{} = actor) do
|
||||
%{
|
||||
"type" => Atom.to_string(actor.type),
|
||||
@@ -65,9 +71,3 @@ defmodule Mobilizon.Service.ActivityPub.Converters.Actor do
|
||||
}
|
||||
end
|
||||
end
|
||||
|
||||
defimpl Mobilizon.Service.ActivityPub.Convertible, for: Mobilizon.Actors.Actor do
|
||||
alias Mobilizon.Service.ActivityPub.Converters.Actor, as: ActorConverter
|
||||
|
||||
defdelegate model_to_as(actor), to: ActorConverter
|
||||
end
|
||||
@@ -1,11 +1,9 @@
|
||||
defmodule Mobilizon.Service.ActivityPub.Converters.Address do
|
||||
defmodule Mobilizon.Service.ActivityPub.Converter.Address do
|
||||
@moduledoc """
|
||||
Address converter.
|
||||
|
||||
This module allows to convert reports from ActivityStream format to our own
|
||||
internal one, and back.
|
||||
|
||||
Note: Reports are named Flag in AS.
|
||||
"""
|
||||
|
||||
alias Mobilizon.Addresses.Address, as: AddressModel
|
||||
@@ -14,10 +12,10 @@ defmodule Mobilizon.Service.ActivityPub.Converters.Address do
|
||||
@behaviour Converter
|
||||
|
||||
@doc """
|
||||
Converts an AP object data to our internal data structure
|
||||
Converts an AP object data to our internal data structure.
|
||||
"""
|
||||
@impl Converter
|
||||
@spec as_to_model_data(map()) :: map()
|
||||
@spec as_to_model_data(map) :: map
|
||||
def as_to_model_data(object) do
|
||||
res = %{
|
||||
"description" => object["name"],
|
||||
@@ -50,10 +48,10 @@ defmodule Mobilizon.Service.ActivityPub.Converters.Address do
|
||||
end
|
||||
|
||||
@doc """
|
||||
Convert an event struct to an ActivityStream representation
|
||||
Convert an event struct to an ActivityStream representation.
|
||||
"""
|
||||
@impl Converter
|
||||
@spec model_to_as(AddressModel.t()) :: map()
|
||||
@spec model_to_as(AddressModel.t()) :: map
|
||||
def model_to_as(%AddressModel{} = address) do
|
||||
res = %{
|
||||
"type" => "Place",
|
||||
@@ -1,4 +1,4 @@
|
||||
defmodule Mobilizon.Service.ActivityPub.Converters.Comment do
|
||||
defmodule Mobilizon.Service.ActivityPub.Converter.Comment do
|
||||
@moduledoc """
|
||||
Comment converter.
|
||||
|
||||
@@ -10,19 +10,26 @@ defmodule Mobilizon.Service.ActivityPub.Converters.Comment do
|
||||
alias Mobilizon.Events.Comment, as: CommentModel
|
||||
alias Mobilizon.Events.Event
|
||||
alias Mobilizon.Service.ActivityPub
|
||||
alias Mobilizon.Service.ActivityPub.Converter
|
||||
alias Mobilizon.Service.ActivityPub.{Converter, Convertible}
|
||||
|
||||
require Logger
|
||||
|
||||
@behaviour Converter
|
||||
|
||||
defimpl Convertible, for: CommentModel do
|
||||
alias Mobilizon.Service.ActivityPub.Converter.Comment, as: CommentConverter
|
||||
|
||||
defdelegate model_to_as(comment), to: CommentConverter
|
||||
end
|
||||
|
||||
@doc """
|
||||
Converts an AP object data to our internal data structure
|
||||
Converts an AP object data to our internal data structure.
|
||||
"""
|
||||
@impl Converter
|
||||
@spec as_to_model_data(map()) :: map()
|
||||
@spec as_to_model_data(map) :: map
|
||||
def as_to_model_data(object) do
|
||||
{:ok, %Actor{id: actor_id}} = ActivityPub.get_or_fetch_by_url(object["actor"])
|
||||
|
||||
Logger.debug("Inserting full comment")
|
||||
Logger.debug(inspect(object))
|
||||
|
||||
@@ -65,6 +72,7 @@ defmodule Mobilizon.Service.ActivityPub.Converters.Comment do
|
||||
end
|
||||
else
|
||||
Logger.debug("No parent object for this comment")
|
||||
|
||||
data
|
||||
end
|
||||
|
||||
@@ -75,7 +83,7 @@ defmodule Mobilizon.Service.ActivityPub.Converters.Comment do
|
||||
Make an AS comment object from an existing `Comment` structure.
|
||||
"""
|
||||
@impl Converter
|
||||
@spec model_to_as(CommentModel.t()) :: map()
|
||||
@spec model_to_as(CommentModel.t()) :: map
|
||||
def model_to_as(%CommentModel{} = comment) do
|
||||
object = %{
|
||||
"type" => "Note",
|
||||
@@ -94,9 +102,3 @@ defmodule Mobilizon.Service.ActivityPub.Converters.Comment do
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
defimpl Mobilizon.Service.ActivityPub.Convertible, for: Mobilizon.Events.Comment do
|
||||
alias Mobilizon.Service.ActivityPub.Converters.Comment, as: CommentConverter
|
||||
|
||||
defdelegate model_to_as(comment), to: CommentConverter
|
||||
end
|
||||
@@ -1,4 +1,4 @@
|
||||
defmodule Mobilizon.Service.ActivityPub.Converters.Event do
|
||||
defmodule Mobilizon.Service.ActivityPub.Converter.Event do
|
||||
@moduledoc """
|
||||
Event converter.
|
||||
|
||||
@@ -10,20 +10,27 @@ defmodule Mobilizon.Service.ActivityPub.Converters.Event do
|
||||
alias Mobilizon.Actors.Actor
|
||||
alias Mobilizon.Addresses.Address
|
||||
alias Mobilizon.Events.Event, as: EventModel
|
||||
alias Mobilizon.Events.Tag
|
||||
alias Mobilizon.Events.{EventOptions, Tag}
|
||||
alias Mobilizon.Media.Picture
|
||||
alias Mobilizon.Service.ActivityPub.{Converter, Utils}
|
||||
alias Mobilizon.Service.ActivityPub.Converters.Address, as: AddressConverter
|
||||
alias Mobilizon.Service.ActivityPub.{Converter, Convertible, Utils}
|
||||
alias Mobilizon.Service.ActivityPub.Converter.Address, as: AddressConverter
|
||||
alias Mobilizon.Service.ActivityPub.Converter.Picture, as: PictureConverter
|
||||
|
||||
require Logger
|
||||
|
||||
@behaviour Converter
|
||||
|
||||
defimpl Convertible, for: EventModel do
|
||||
alias Mobilizon.Service.ActivityPub.Converter.Event, as: EventConverter
|
||||
|
||||
defdelegate model_to_as(event), to: EventConverter
|
||||
end
|
||||
|
||||
@doc """
|
||||
Converts an AP object data to our internal data structure
|
||||
Converts an AP object data to our internal data structure.
|
||||
"""
|
||||
@impl Converter
|
||||
@spec as_to_model_data(map()) :: map()
|
||||
@spec as_to_model_data(map) :: map
|
||||
def as_to_model_data(object) do
|
||||
Logger.debug("event as_to_model_data")
|
||||
Logger.debug(inspect(object))
|
||||
@@ -71,15 +78,57 @@ defmodule Mobilizon.Service.ActivityPub.Converters.Event do
|
||||
|
||||
{:ok, Map.put(entity, "options", options)}
|
||||
else
|
||||
err ->
|
||||
{:error, err}
|
||||
error ->
|
||||
{:error, error}
|
||||
end
|
||||
end
|
||||
|
||||
@doc """
|
||||
Convert an event struct to an ActivityStream representation.
|
||||
"""
|
||||
@impl Converter
|
||||
@spec model_to_as(EventModel.t()) :: map
|
||||
def model_to_as(%EventModel{} = event) do
|
||||
to =
|
||||
if event.visibility == :public,
|
||||
do: ["https://www.w3.org/ns/activitystreams#Public"],
|
||||
else: [event.organizer_actor.followers_url]
|
||||
|
||||
res = %{
|
||||
"type" => "Event",
|
||||
"to" => to,
|
||||
"cc" => [],
|
||||
"attributedTo" => event.organizer_actor.url,
|
||||
"name" => event.title,
|
||||
"actor" => event.organizer_actor.url,
|
||||
"uuid" => event.uuid,
|
||||
"category" => event.category,
|
||||
"content" => event.description,
|
||||
"publish_at" => (event.publish_at || event.inserted_at) |> date_to_string(),
|
||||
"updated_at" => event.updated_at |> date_to_string(),
|
||||
"mediaType" => "text/html",
|
||||
"startTime" => event.begins_on |> date_to_string(),
|
||||
"endTime" => event.ends_on |> date_to_string(),
|
||||
"tag" => event.tags |> build_tags(),
|
||||
"id" => event.url,
|
||||
"url" => event.url
|
||||
}
|
||||
|
||||
res =
|
||||
if is_nil(event.physical_address),
|
||||
do: res,
|
||||
else: Map.put(res, "location", AddressConverter.model_to_as(event.physical_address))
|
||||
|
||||
if is_nil(event.picture),
|
||||
do: res,
|
||||
else: Map.put(res, "attachment", [PictureConverter.model_to_as(event.picture)])
|
||||
end
|
||||
|
||||
# Get only elements that we have in EventOptions
|
||||
@spec get_options(map) :: map
|
||||
defp get_options(object) do
|
||||
keys =
|
||||
Mobilizon.Events.EventOptions
|
||||
EventOptions
|
||||
|> struct
|
||||
|> Map.keys()
|
||||
|> List.delete(:__struct__)
|
||||
@@ -91,6 +140,7 @@ defmodule Mobilizon.Service.ActivityPub.Converters.Event do
|
||||
end)
|
||||
end
|
||||
|
||||
@spec get_address(map | binary | nil) :: integer | nil
|
||||
defp get_address(address_url) when is_bitstring(address_url) do
|
||||
get_address(%{"id" => address_url})
|
||||
end
|
||||
@@ -115,8 +165,9 @@ defmodule Mobilizon.Service.ActivityPub.Converters.Event do
|
||||
|
||||
defp get_address(nil), do: nil
|
||||
|
||||
@spec do_get_address(map) :: integer | nil
|
||||
defp do_get_address(map) do
|
||||
map = Mobilizon.Service.ActivityPub.Converters.Address.as_to_model_data(map)
|
||||
map = Mobilizon.Service.ActivityPub.Converter.Address.as_to_model_data(map)
|
||||
|
||||
case Addresses.create_address(map) do
|
||||
{:ok, %Address{id: address_id}} ->
|
||||
@@ -127,6 +178,7 @@ defmodule Mobilizon.Service.ActivityPub.Converters.Event do
|
||||
end
|
||||
end
|
||||
|
||||
@spec fetch_tags([String.t()]) :: [String.t()]
|
||||
defp fetch_tags(tags) do
|
||||
Logger.debug("fetching tags")
|
||||
|
||||
@@ -141,6 +193,7 @@ defmodule Mobilizon.Service.ActivityPub.Converters.Event do
|
||||
end)
|
||||
end
|
||||
|
||||
@spec build_tags([String.t()]) :: String.t()
|
||||
defp build_tags(tags) do
|
||||
Enum.map(tags, fn %Tag{} = tag ->
|
||||
%{
|
||||
@@ -203,12 +256,7 @@ defmodule Mobilizon.Service.ActivityPub.Converters.Event do
|
||||
else: Map.put(res, "attachment", [Utils.make_picture_data(event.picture)])
|
||||
end
|
||||
|
||||
@spec date_to_string(DateTime.t() | nil) :: String.t()
|
||||
defp date_to_string(nil), do: nil
|
||||
defp date_to_string(date), do: DateTime.to_iso8601(date)
|
||||
end
|
||||
|
||||
defimpl Mobilizon.Service.ActivityPub.Convertible, for: Mobilizon.Events.Event do
|
||||
alias Mobilizon.Service.ActivityPub.Converters.Event, as: EventConverter
|
||||
|
||||
defdelegate model_to_as(event), to: EventConverter
|
||||
defp date_to_string(%DateTime{} = date), do: DateTime.to_iso8601(date)
|
||||
end
|
||||
@@ -1,4 +1,4 @@
|
||||
defmodule Mobilizon.Service.ActivityPub.Converters.Flag do
|
||||
defmodule Mobilizon.Service.ActivityPub.Converter.Flag do
|
||||
@moduledoc """
|
||||
Flag converter.
|
||||
|
||||
@@ -18,10 +18,10 @@ defmodule Mobilizon.Service.ActivityPub.Converters.Flag do
|
||||
@behaviour Converter
|
||||
|
||||
@doc """
|
||||
Converts an AP object data to our internal data structure
|
||||
Converts an AP object data to our internal data structure.
|
||||
"""
|
||||
@impl Converter
|
||||
@spec as_to_model_data(map()) :: map()
|
||||
@spec as_to_model_data(map) :: map
|
||||
def as_to_model_data(object) do
|
||||
with params <- as_to_model(object) do
|
||||
%{
|
||||
@@ -35,6 +35,21 @@ defmodule Mobilizon.Service.ActivityPub.Converters.Flag do
|
||||
end
|
||||
end
|
||||
|
||||
@doc """
|
||||
Convert an event struct to an ActivityStream representation
|
||||
"""
|
||||
@impl Converter
|
||||
@spec model_to_as(EventModel.t()) :: map
|
||||
def model_to_as(%Report{} = report) do
|
||||
%{
|
||||
"type" => "Flag",
|
||||
"to" => ["https://www.w3.org/ns/activitystreams#Public"],
|
||||
"actor" => report.reporter.url,
|
||||
"id" => report.url
|
||||
}
|
||||
end
|
||||
|
||||
@spec as_to_model(map) :: map
|
||||
def as_to_model(%{"object" => objects} = object) do
|
||||
with {:ok, %Actor{} = reporter} <- Actors.get_actor_by_url(object["actor"]),
|
||||
%Actor{} = reported <-
|
||||
@@ -74,18 +89,4 @@ defmodule Mobilizon.Service.ActivityPub.Converters.Flag do
|
||||
}
|
||||
end
|
||||
end
|
||||
|
||||
@doc """
|
||||
Convert an event struct to an ActivityStream representation
|
||||
"""
|
||||
@impl Converter
|
||||
@spec model_to_as(EventModel.t()) :: map()
|
||||
def model_to_as(%Report{} = report) do
|
||||
%{
|
||||
"type" => "Flag",
|
||||
"to" => ["https://www.w3.org/ns/activitystreams#Public"],
|
||||
"actor" => report.reporter.url,
|
||||
"id" => report.url
|
||||
}
|
||||
end
|
||||
end
|
||||
31
lib/service/activity_pub/converter/participant.ex
Normal file
31
lib/service/activity_pub/converter/participant.ex
Normal file
@@ -0,0 +1,31 @@
|
||||
defmodule Mobilizon.Service.ActivityPub.Converter.Participant do
|
||||
@moduledoc """
|
||||
Participant converter.
|
||||
|
||||
This module allows to convert reports from ActivityStream format to our own
|
||||
internal one, and back.
|
||||
"""
|
||||
|
||||
alias Mobilizon.Events.Participant, as: ParticipantModel
|
||||
|
||||
alias Mobilizon.Service.ActivityPub.Convertible
|
||||
|
||||
defimpl Convertible, for: ParticipantModel do
|
||||
alias Mobilizon.Service.ActivityPub.Converter.Participant, as: ParticipantConverter
|
||||
|
||||
defdelegate model_to_as(participant), to: ParticipantConverter
|
||||
end
|
||||
|
||||
@doc """
|
||||
Convert an event struct to an ActivityStream representation.
|
||||
"""
|
||||
@spec model_to_as(ParticipantModel.t()) :: map
|
||||
def model_to_as(%ParticipantModel{} = participant) do
|
||||
%{
|
||||
"type" => "Join",
|
||||
"id" => participant.url,
|
||||
"actor" => participant.actor.url,
|
||||
"object" => participant.event.url
|
||||
}
|
||||
end
|
||||
end
|
||||
28
lib/service/activity_pub/converter/picture.ex
Normal file
28
lib/service/activity_pub/converter/picture.ex
Normal file
@@ -0,0 +1,28 @@
|
||||
defmodule Mobilizon.Service.ActivityPub.Converter.Picture do
|
||||
@moduledoc """
|
||||
Picture converter.
|
||||
|
||||
This module allows to convert events from ActivityStream format to our own
|
||||
internal one, and back.
|
||||
"""
|
||||
|
||||
alias Mobilizon.Media.Picture, as: PictureModel
|
||||
|
||||
@doc """
|
||||
Convert a picture struct to an ActivityStream representation.
|
||||
"""
|
||||
@spec model_to_as(PictureModel.t()) :: map
|
||||
def model_to_as(%PictureModel{file: file}) do
|
||||
%{
|
||||
"type" => "Document",
|
||||
"url" => [
|
||||
%{
|
||||
"type" => "Link",
|
||||
"mediaType" => file.content_type,
|
||||
"href" => file.url
|
||||
}
|
||||
],
|
||||
"name" => file.name
|
||||
}
|
||||
end
|
||||
end
|
||||
@@ -1,31 +0,0 @@
|
||||
defmodule Mobilizon.Service.ActivityPub.Converters.Participant do
|
||||
@moduledoc """
|
||||
Participant converter.
|
||||
|
||||
This module allows to convert reports from ActivityStream format to our own
|
||||
internal one, and back.
|
||||
|
||||
Note: Reports are named Flag in AS.
|
||||
"""
|
||||
|
||||
alias Mobilizon.Events.Participant, as: ParticipantModel
|
||||
|
||||
@doc """
|
||||
Convert an event struct to an ActivityStream representation
|
||||
"""
|
||||
@spec model_to_as(ParticipantModel.t()) :: map()
|
||||
def model_to_as(%ParticipantModel{} = participant) do
|
||||
%{
|
||||
"type" => "Join",
|
||||
"id" => participant.url,
|
||||
"actor" => participant.actor.url,
|
||||
"object" => participant.event.url
|
||||
}
|
||||
end
|
||||
|
||||
defimpl Mobilizon.Service.ActivityPub.Convertible, for: Mobilizon.Events.Participant do
|
||||
alias Mobilizon.Service.ActivityPub.Converters.Participant, as: ParticipantConverter
|
||||
|
||||
defdelegate model_to_as(event), to: ParticipantConverter
|
||||
end
|
||||
end
|
||||
10
lib/service/activity_pub/convertible.ex
Normal file
10
lib/service/activity_pub/convertible.ex
Normal file
@@ -0,0 +1,10 @@
|
||||
defprotocol Mobilizon.Service.ActivityPub.Convertible do
|
||||
@moduledoc """
|
||||
Convertible protocol.
|
||||
"""
|
||||
|
||||
@type activity_streams :: map
|
||||
|
||||
@spec model_to_as(t) :: activity_streams
|
||||
def model_to_as(convertible)
|
||||
end
|
||||
@@ -11,7 +11,7 @@ defmodule Mobilizon.Service.ActivityPub.Relay do
|
||||
alias Mobilizon.Actors
|
||||
alias Mobilizon.Actors.Actor
|
||||
alias Mobilizon.Service.ActivityPub
|
||||
alias Mobilizon.Service.ActivityPub.Activity
|
||||
alias Mobilizon.Service.ActivityPub.{Activity, Transmogrifier}
|
||||
|
||||
alias MobilizonWeb.API.Follows
|
||||
|
||||
@@ -72,9 +72,7 @@ defmodule Mobilizon.Service.ActivityPub.Relay do
|
||||
def publish(%Activity{data: %{"object" => object}} = _activity) do
|
||||
with %Actor{id: actor_id} = actor <- get_actor(),
|
||||
{:ok, object} <-
|
||||
Mobilizon.Service.ActivityPub.Transmogrifier.fetch_obj_helper_as_activity_streams(
|
||||
object
|
||||
) do
|
||||
Transmogrifier.fetch_obj_helper_as_activity_streams(object) do
|
||||
ActivityPub.announce(actor, object, "#{object["id"]}/announces/#{actor_id}", true, false)
|
||||
else
|
||||
e ->
|
||||
|
||||
@@ -13,7 +13,7 @@ defmodule Mobilizon.Service.ActivityPub.Transmogrifier do
|
||||
alias Mobilizon.Events
|
||||
alias Mobilizon.Events.{Comment, Event, Participant}
|
||||
alias Mobilizon.Service.ActivityPub
|
||||
alias Mobilizon.Service.ActivityPub.{Utils, Visibility}
|
||||
alias Mobilizon.Service.ActivityPub.{Converter, Convertible, Utils, Visibility}
|
||||
|
||||
require Logger
|
||||
|
||||
@@ -122,7 +122,7 @@ defmodule Mobilizon.Service.ActivityPub.Transmogrifier do
|
||||
def handle_incoming(%{"id" => ""}), do: :error
|
||||
|
||||
def handle_incoming(%{"type" => "Flag"} = data) do
|
||||
with params <- Mobilizon.Service.ActivityPub.Converters.Flag.as_to_model(data) do
|
||||
with params <- Converter.Flag.as_to_model(data) do
|
||||
params = %{
|
||||
reporter_url: params["reporter"].url,
|
||||
reported_actor_url: params["reported"].url,
|
||||
@@ -868,7 +868,7 @@ defmodule Mobilizon.Service.ActivityPub.Transmogrifier do
|
||||
|
||||
def fetch_obj_helper_as_activity_streams(object) do
|
||||
with {:ok, object} <- fetch_obj_helper(object) do
|
||||
{:ok, Mobilizon.Service.ActivityPub.Convertible.model_to_as(object)}
|
||||
{:ok, Convertible.model_to_as(object)}
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -16,7 +16,8 @@ defmodule Mobilizon.Service.ActivityPub.Utils do
|
||||
alias Mobilizon.Events.{Comment, Event}
|
||||
alias Mobilizon.Media.Picture
|
||||
alias Mobilizon.Reports.Report
|
||||
alias Mobilizon.Service.ActivityPub.{Activity, Converters}
|
||||
alias Mobilizon.Service.ActivityPub.{Activity, Converter}
|
||||
alias Mobilizon.Service.Federator
|
||||
alias Mobilizon.Storage.Repo
|
||||
|
||||
alias MobilizonWeb.{Email, Endpoint}
|
||||
@@ -64,7 +65,8 @@ defmodule Mobilizon.Service.ActivityPub.Utils do
|
||||
_ -> 5
|
||||
end
|
||||
|
||||
Mobilizon.Service.Federator.enqueue(:publish, activity, priority)
|
||||
Federator.enqueue(:publish, activity, priority)
|
||||
|
||||
:ok
|
||||
end
|
||||
|
||||
@@ -119,7 +121,7 @@ defmodule Mobilizon.Service.ActivityPub.Utils do
|
||||
def insert_full_object(%{"object" => %{"type" => "Event"} = object_data, "type" => "Create"})
|
||||
when is_map(object_data) do
|
||||
with {:ok, object_data} <-
|
||||
Converters.Event.as_to_model_data(object_data),
|
||||
Converter.Event.as_to_model_data(object_data),
|
||||
{:ok, %Event{} = event} <- Events.create_event(object_data) do
|
||||
{:ok, event}
|
||||
end
|
||||
@@ -139,7 +141,7 @@ defmodule Mobilizon.Service.ActivityPub.Utils do
|
||||
"""
|
||||
def insert_full_object(%{"object" => %{"type" => "Note"} = object_data, "type" => "Create"})
|
||||
when is_map(object_data) do
|
||||
with data <- Converters.Comment.as_to_model_data(object_data),
|
||||
with data <- Converter.Comment.as_to_model_data(object_data),
|
||||
{:ok, %Comment{} = comment} <- Events.create_comment(data) do
|
||||
{:ok, comment}
|
||||
else
|
||||
@@ -155,7 +157,7 @@ defmodule Mobilizon.Service.ActivityPub.Utils do
|
||||
"""
|
||||
def insert_full_object(%{"type" => "Flag"} = object_data)
|
||||
when is_map(object_data) do
|
||||
with data <- Converters.Flag.as_to_model_data(object_data),
|
||||
with data <- Converter.Flag.as_to_model_data(object_data),
|
||||
{:ok, %Report{} = report} <- Reports.create_report(data) do
|
||||
Enum.each(Users.list_moderators(), fn moderator ->
|
||||
moderator
|
||||
@@ -187,7 +189,7 @@ defmodule Mobilizon.Service.ActivityPub.Utils do
|
||||
when is_map(object_data) do
|
||||
with {:event_not_found, %Event{} = event} <-
|
||||
{:event_not_found, Events.get_event_by_url(event_url)},
|
||||
{:ok, object_data} <- Converters.Event.as_to_model_data(object_data),
|
||||
{:ok, object_data} <- Converter.Event.as_to_model_data(object_data),
|
||||
{:ok, %Event{} = event} <- Events.update_event(event, object_data) do
|
||||
{:ok, event}
|
||||
end
|
||||
@@ -199,7 +201,7 @@ defmodule Mobilizon.Service.ActivityPub.Utils do
|
||||
})
|
||||
when is_map(object_data) and type_actor in @actor_types do
|
||||
with {:ok, %Actor{} = actor} <- Actors.get_actor_by_url(actor_url),
|
||||
object_data <- Converters.Actor.as_to_model_data(object_data),
|
||||
object_data <- Converter.Actor.as_to_model_data(object_data),
|
||||
{:ok, %Actor{} = actor} <- Actors.update_actor(actor, object_data) do
|
||||
{:ok, actor}
|
||||
end
|
||||
@@ -245,21 +247,10 @@ defmodule Mobilizon.Service.ActivityPub.Utils do
|
||||
end
|
||||
|
||||
@doc """
|
||||
Convert a picture model into an AS Link representation
|
||||
Convert a picture model into an AS Link representation.
|
||||
"""
|
||||
# TODO: Move me to Mobilizon.Service.ActivityPub.Converters
|
||||
def make_picture_data(%Picture{file: file} = _picture) do
|
||||
%{
|
||||
"type" => "Document",
|
||||
"url" => [
|
||||
%{
|
||||
"type" => "Link",
|
||||
"mediaType" => file.content_type,
|
||||
"href" => file.url
|
||||
}
|
||||
],
|
||||
"name" => file.name
|
||||
}
|
||||
def make_picture_data(%Picture{} = picture) do
|
||||
Converter.Picture.model_to_as(picture)
|
||||
end
|
||||
|
||||
@doc """
|
||||
@@ -268,7 +259,7 @@ defmodule Mobilizon.Service.ActivityPub.Utils do
|
||||
def make_picture_data(picture) when is_map(picture) do
|
||||
with {:ok, %{"url" => [%{"href" => url, "mediaType" => content_type}], "size" => size}} <-
|
||||
MobilizonWeb.Upload.store(picture.file),
|
||||
{:ok, %Picture{file: _file} = pic} <-
|
||||
{:ok, %Picture{file: _file} = picture} <-
|
||||
Mobilizon.Media.create_picture(%{
|
||||
"file" => %{
|
||||
"url" => url,
|
||||
@@ -278,7 +269,7 @@ defmodule Mobilizon.Service.ActivityPub.Utils do
|
||||
},
|
||||
"actor_id" => picture.actor_id
|
||||
}) do
|
||||
make_picture_data(pic)
|
||||
Converter.Picture.model_to_as(picture)
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
Reference in New Issue
Block a user