2020-01-22 21:40:40 +00:00
|
|
|
defmodule Mobilizon.Federation.ActivityStream.Converter.Flag do
|
2019-07-23 11:49:22 +00:00
|
|
|
@moduledoc """
|
2019-09-22 14:26:23 +00:00
|
|
|
Flag converter.
|
2019-07-23 11:49:22 +00:00
|
|
|
|
2019-09-22 14:26:23 +00:00
|
|
|
This module allows to convert reports from ActivityStream format to our own
|
|
|
|
internal one, and back.
|
2019-07-23 11:49:22 +00:00
|
|
|
|
|
|
|
Note: Reports are named Flag in AS.
|
|
|
|
"""
|
2019-09-22 14:26:23 +00:00
|
|
|
|
2019-07-23 11:49:22 +00:00
|
|
|
alias Mobilizon.Actors.Actor
|
2023-06-05 16:32:29 +00:00
|
|
|
alias Mobilizon.Discussions.Comment
|
2019-07-23 11:49:22 +00:00
|
|
|
alias Mobilizon.Events.Event
|
|
|
|
alias Mobilizon.Reports.Report
|
2020-01-22 01:14:42 +00:00
|
|
|
|
2023-06-05 16:32:29 +00:00
|
|
|
alias Mobilizon.Federation.ActivityPub
|
2021-04-22 10:17:56 +00:00
|
|
|
alias Mobilizon.Federation.ActivityPub.Actor, as: ActivityPubActor
|
2020-01-22 01:14:42 +00:00
|
|
|
alias Mobilizon.Federation.ActivityPub.Relay
|
2020-01-22 21:40:40 +00:00
|
|
|
alias Mobilizon.Federation.ActivityStream.{Converter, Convertible}
|
2019-07-23 11:49:22 +00:00
|
|
|
|
|
|
|
@behaviour Converter
|
|
|
|
|
2019-11-15 17:36:47 +00:00
|
|
|
defimpl Convertible, for: Report do
|
2020-01-22 21:40:40 +00:00
|
|
|
alias Mobilizon.Federation.ActivityStream.Converter.Flag, as: FlagConverter
|
2019-11-15 17:36:47 +00:00
|
|
|
|
|
|
|
defdelegate model_to_as(report), to: FlagConverter
|
|
|
|
end
|
|
|
|
|
2019-07-23 11:49:22 +00:00
|
|
|
@doc """
|
2019-09-22 16:29:13 +00:00
|
|
|
Converts an AP object data to our internal data structure.
|
2019-07-23 11:49:22 +00:00
|
|
|
"""
|
|
|
|
@impl Converter
|
2019-09-22 16:29:13 +00:00
|
|
|
@spec as_to_model_data(map) :: map
|
2019-07-23 11:49:22 +00: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,
|
2023-06-05 16:32:29 +00:00
|
|
|
"events" => params["events"],
|
2019-07-23 11:49:22 +00:00
|
|
|
"comments" => params["comments"]
|
|
|
|
}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-09-22 16:29:13 +00:00
|
|
|
@doc """
|
|
|
|
Convert an event struct to an ActivityStream representation
|
|
|
|
"""
|
|
|
|
@impl Converter
|
2019-11-15 17:36:47 +00:00
|
|
|
@spec model_to_as(Report.t()) :: map
|
2019-09-22 16:29:13 +00:00
|
|
|
def model_to_as(%Report{} = report) do
|
2023-06-05 16:32:29 +00:00
|
|
|
object =
|
|
|
|
[report.reported.url] ++
|
|
|
|
Enum.map(report.comments, fn comment -> comment.url end) ++
|
|
|
|
Enum.map(report.events, & &1.url)
|
2019-11-15 17:36:47 +00:00
|
|
|
|
2019-09-22 16:29:13 +00:00
|
|
|
%{
|
|
|
|
"type" => "Flag",
|
2019-12-03 10:29:51 +00:00
|
|
|
"actor" => Relay.get_actor().url,
|
2019-11-15 17:36:47 +00:00
|
|
|
"id" => report.url,
|
|
|
|
"content" => report.content,
|
|
|
|
"object" => object
|
2019-09-22 16:29:13 +00:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
@spec as_to_model(map) :: map
|
2019-07-23 11:49:22 +00:00
|
|
|
def as_to_model(%{"object" => objects} = object) do
|
2021-04-22 10:17:56 +00:00
|
|
|
with {:ok, %Actor{} = reporter} <-
|
|
|
|
ActivityPubActor.get_or_fetch_actor_by_url(object["actor"]),
|
2022-04-18 14:59:07 +00:00
|
|
|
%Actor{} = reported <- find_reported(objects),
|
2023-06-05 16:32:29 +00:00
|
|
|
%{events: events, comments: comments} <- find_events_and_comments(objects) do
|
2019-07-23 11:49:22 +00:00
|
|
|
%{
|
|
|
|
"reporter" => reporter,
|
|
|
|
"uri" => object["id"],
|
|
|
|
"content" => object["content"],
|
|
|
|
"reported" => reported,
|
2023-06-05 16:32:29 +00:00
|
|
|
"events" => events,
|
2019-07-23 11:49:22 +00:00
|
|
|
"comments" => comments
|
|
|
|
}
|
|
|
|
end
|
|
|
|
end
|
2022-04-18 14:59:07 +00:00
|
|
|
|
|
|
|
@spec find_reported(list(String.t())) :: Actor.t() | nil
|
|
|
|
defp find_reported(objects) do
|
|
|
|
Enum.reduce_while(objects, nil, fn url, _ ->
|
|
|
|
case ActivityPubActor.get_or_fetch_actor_by_url(url) do
|
|
|
|
{:ok, %Actor{} = actor} ->
|
|
|
|
{:halt, actor}
|
|
|
|
|
|
|
|
_ ->
|
|
|
|
{:cont, nil}
|
|
|
|
end
|
|
|
|
end)
|
|
|
|
end
|
|
|
|
|
2023-06-05 16:32:29 +00:00
|
|
|
defp find_events_and_comments(objects) do
|
2022-04-18 14:59:07 +00:00
|
|
|
objects
|
2023-06-05 16:32:29 +00:00
|
|
|
|> Enum.map(&ActivityPub.fetch_object_from_url/1)
|
|
|
|
|> Enum.reduce(%{comments: [], events: []}, fn res, acc ->
|
|
|
|
case res do
|
|
|
|
{:ok, %Event{} = event} ->
|
|
|
|
Map.put(acc, :events, [event | acc.events])
|
2022-04-18 14:59:07 +00:00
|
|
|
|
2023-06-05 16:32:29 +00:00
|
|
|
{:ok, %Comment{} = comment} ->
|
|
|
|
Map.put(acc, :comments, [comment | acc.comments])
|
2022-04-18 14:59:07 +00:00
|
|
|
|
|
|
|
_ ->
|
2023-06-05 16:32:29 +00:00
|
|
|
acc
|
2022-04-18 14:59:07 +00:00
|
|
|
end
|
|
|
|
end)
|
|
|
|
end
|
2019-07-23 11:49:22 +00:00
|
|
|
end
|