2020-01-22 22:40:40 +01:00
|
|
|
defmodule Mobilizon.Federation.ActivityStream.Converter.Flag do
|
2019-07-23 13:49:22 +02:00
|
|
|
@moduledoc """
|
2019-09-22 16:26:23 +02:00
|
|
|
Flag converter.
|
2019-07-23 13:49:22 +02:00
|
|
|
|
2019-09-22 16:26:23 +02:00
|
|
|
This module allows to convert reports from ActivityStream format to our own
|
|
|
|
internal one, and back.
|
2019-07-23 13:49:22 +02:00
|
|
|
|
|
|
|
Note: Reports are named Flag in AS.
|
|
|
|
"""
|
2019-09-22 16:26:23 +02:00
|
|
|
|
2019-07-23 13:49:22 +02:00
|
|
|
alias Mobilizon.Actors.Actor
|
2020-07-09 17:24:28 +02:00
|
|
|
alias Mobilizon.Discussions
|
2019-07-23 13:49:22 +02:00
|
|
|
alias Mobilizon.Events
|
|
|
|
alias Mobilizon.Events.Event
|
|
|
|
alias Mobilizon.Reports.Report
|
2020-01-22 02:14:42 +01:00
|
|
|
|
2021-04-22 12:17:56 +02:00
|
|
|
alias Mobilizon.Federation.ActivityPub.Actor, as: ActivityPubActor
|
2020-01-22 02:14:42 +01:00
|
|
|
alias Mobilizon.Federation.ActivityPub.Relay
|
2020-01-22 22:40:40 +01:00
|
|
|
alias Mobilizon.Federation.ActivityStream.{Converter, Convertible}
|
2019-07-23 13:49:22 +02:00
|
|
|
|
|
|
|
@behaviour Converter
|
|
|
|
|
2019-11-15 18:36:47 +01:00
|
|
|
defimpl Convertible, for: Report do
|
2020-01-22 22:40:40 +01:00
|
|
|
alias Mobilizon.Federation.ActivityStream.Converter.Flag, as: FlagConverter
|
2019-11-15 18:36:47 +01:00
|
|
|
|
|
|
|
defdelegate model_to_as(report), to: FlagConverter
|
|
|
|
end
|
|
|
|
|
2019-07-23 13:49:22 +02:00
|
|
|
@doc """
|
2019-09-22 18:29:13 +02:00
|
|
|
Converts an AP object data to our internal data structure.
|
2019-07-23 13:49:22 +02:00
|
|
|
"""
|
|
|
|
@impl Converter
|
2019-09-22 18:29:13 +02:00
|
|
|
@spec as_to_model_data(map) :: map
|
2019-07-23 13:49:22 +02:00
|
|
|
def as_to_model_data(object) do
|
|
|
|
with params <- as_to_model(object) do
|
|
|
|
%{
|
|
|
|
"reporter_id" => params["reporter"].id,
|
|
|
|
"uri" => params["uri"],
|
|
|
|
"content" => params["content"],
|
|
|
|
"reported_id" => params["reported"].id,
|
|
|
|
"event_id" => (!is_nil(params["event"]) && params["event"].id) || nil,
|
|
|
|
"comments" => params["comments"]
|
|
|
|
}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-09-22 18:29:13 +02:00
|
|
|
@doc """
|
|
|
|
Convert an event struct to an ActivityStream representation
|
|
|
|
"""
|
|
|
|
@impl Converter
|
2019-11-15 18:36:47 +01:00
|
|
|
@spec model_to_as(Report.t()) :: map
|
2019-09-22 18:29:13 +02:00
|
|
|
def model_to_as(%Report{} = report) do
|
2019-11-15 18:36:47 +01:00
|
|
|
object = [report.reported.url] ++ Enum.map(report.comments, fn comment -> comment.url end)
|
|
|
|
|
|
|
|
object = if report.event, do: object ++ [report.event.url], else: object
|
|
|
|
|
2019-09-22 18:29:13 +02:00
|
|
|
%{
|
|
|
|
"type" => "Flag",
|
2019-12-03 11:29:51 +01:00
|
|
|
"actor" => Relay.get_actor().url,
|
2019-11-15 18:36:47 +01:00
|
|
|
"id" => report.url,
|
|
|
|
"content" => report.content,
|
|
|
|
"object" => object
|
2019-09-22 18:29:13 +02:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
@spec as_to_model(map) :: map
|
2019-07-23 13:49:22 +02:00
|
|
|
def as_to_model(%{"object" => objects} = object) do
|
2021-04-22 12:17:56 +02:00
|
|
|
with {:ok, %Actor{} = reporter} <-
|
|
|
|
ActivityPubActor.get_or_fetch_actor_by_url(object["actor"]),
|
2019-07-23 13:49:22 +02:00
|
|
|
%Actor{} = reported <-
|
|
|
|
Enum.reduce_while(objects, nil, fn url, _ ->
|
2021-04-22 12:17:56 +02:00
|
|
|
case ActivityPubActor.get_or_fetch_actor_by_url(url) do
|
2019-07-23 18:06:22 +02:00
|
|
|
{:ok, %Actor{} = actor} ->
|
|
|
|
{:halt, actor}
|
|
|
|
|
|
|
|
_ ->
|
|
|
|
{:cont, nil}
|
2019-07-23 13:49:22 +02:00
|
|
|
end
|
|
|
|
end),
|
|
|
|
event <-
|
|
|
|
Enum.reduce_while(objects, nil, fn url, _ ->
|
2019-07-23 18:06:22 +02:00
|
|
|
case Events.get_event_by_url(url) do
|
|
|
|
%Event{} = event ->
|
|
|
|
{:halt, event}
|
|
|
|
|
|
|
|
_ ->
|
|
|
|
{:cont, nil}
|
2019-07-23 13:49:22 +02:00
|
|
|
end
|
|
|
|
end),
|
|
|
|
|
2019-12-03 11:29:51 +01:00
|
|
|
# Remove the reported actor and the event from the object list.
|
2019-07-23 13:49:22 +02:00
|
|
|
comments <-
|
|
|
|
Enum.filter(objects, fn url ->
|
|
|
|
!(url == reported.url || (!is_nil(event) && event.url == url))
|
|
|
|
end),
|
2020-07-09 17:24:28 +02:00
|
|
|
comments <- Enum.map(comments, &Discussions.get_comment_from_url/1) do
|
2019-07-23 13:49:22 +02:00
|
|
|
%{
|
|
|
|
"reporter" => reporter,
|
|
|
|
"uri" => object["id"],
|
|
|
|
"content" => object["content"],
|
|
|
|
"reported" => reported,
|
|
|
|
"event" => event,
|
|
|
|
"comments" => comments
|
|
|
|
}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|