2020-02-18 08:57:00 +01:00
|
|
|
defmodule Mobilizon.Federation.ActivityStream.Converter.Todo do
|
|
|
|
@moduledoc """
|
|
|
|
TodoList converter.
|
|
|
|
|
|
|
|
This module allows to convert todo lists from ActivityStream format to our own
|
|
|
|
internal one, and back.
|
|
|
|
"""
|
|
|
|
alias Mobilizon.Actors.Actor
|
|
|
|
alias Mobilizon.Federation.ActivityPub
|
2021-04-22 12:17:56 +02:00
|
|
|
alias Mobilizon.Federation.ActivityPub.Actor, as: ActivityPubActor
|
2020-02-18 08:57:00 +01:00
|
|
|
alias Mobilizon.Federation.ActivityStream.{Converter, Convertible}
|
|
|
|
alias Mobilizon.Todos
|
|
|
|
alias Mobilizon.Todos.{Todo, TodoList}
|
|
|
|
|
|
|
|
@behaviour Converter
|
|
|
|
|
|
|
|
defimpl Convertible, for: Todo do
|
|
|
|
alias Mobilizon.Federation.ActivityStream.Converter.Todo, as: TodoConverter
|
|
|
|
|
|
|
|
defdelegate model_to_as(todo), to: TodoConverter
|
|
|
|
end
|
|
|
|
|
|
|
|
@doc """
|
|
|
|
Convert an todo list struct to an ActivityStream representation
|
|
|
|
"""
|
|
|
|
@impl Converter
|
|
|
|
@spec model_to_as(Todo.t()) :: map
|
|
|
|
def model_to_as(
|
|
|
|
%Todo{
|
|
|
|
todo_list: %TodoList{actor: %Actor{url: group_url} = _group, url: todo_list_url},
|
|
|
|
creator: %Actor{url: creator_url}
|
|
|
|
} = todo
|
|
|
|
) do
|
|
|
|
%{
|
|
|
|
"type" => "Todo",
|
|
|
|
"actor" => creator_url,
|
|
|
|
"attributedTo" => group_url,
|
|
|
|
"id" => todo.url,
|
|
|
|
"name" => todo.title,
|
|
|
|
"status" => todo.status,
|
2020-08-14 11:32:23 +02:00
|
|
|
"todoList" => todo_list_url,
|
|
|
|
"published" => todo.published_at |> DateTime.to_iso8601()
|
2020-02-18 08:57:00 +01:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
@doc """
|
|
|
|
Converts an AP object data to our internal data structure.
|
|
|
|
"""
|
|
|
|
@impl Converter
|
2021-09-10 11:27:59 +02:00
|
|
|
@spec as_to_model_data(map) :: map() | {:error, any()}
|
2020-02-18 08:57:00 +01:00
|
|
|
def as_to_model_data(
|
|
|
|
%{"type" => "Todo", "actor" => actor_url, "todoList" => todo_list_url} = object
|
|
|
|
) do
|
2021-09-24 16:46:42 +02:00
|
|
|
case ActivityPubActor.get_or_fetch_actor_by_url(actor_url) do
|
|
|
|
{:ok, %Actor{id: creator_id} = _creator} ->
|
|
|
|
case Todos.get_todo_list_by_url(todo_list_url) do
|
|
|
|
%TodoList{id: todo_list_id} ->
|
|
|
|
%{
|
|
|
|
title: object["name"],
|
|
|
|
status: object["status"],
|
|
|
|
url: object["id"],
|
|
|
|
todo_list_id: todo_list_id,
|
|
|
|
creator_id: creator_id,
|
|
|
|
published_at: object["published"]
|
|
|
|
}
|
|
|
|
|
|
|
|
nil ->
|
|
|
|
case ActivityPub.fetch_object_from_url(todo_list_url) do
|
2021-09-28 19:40:37 +02:00
|
|
|
{:ok, _, %TodoList{}} ->
|
|
|
|
as_to_model_data(object)
|
|
|
|
|
2021-09-24 16:46:42 +02:00
|
|
|
{:ok, %TodoList{}} ->
|
|
|
|
as_to_model_data(object)
|
|
|
|
|
|
|
|
{:error, err} ->
|
|
|
|
{:error, err}
|
|
|
|
end
|
2020-02-18 08:57:00 +01:00
|
|
|
end
|
2021-09-24 16:46:42 +02:00
|
|
|
|
|
|
|
{:error, err} ->
|
|
|
|
{:error, err}
|
2020-02-18 08:57:00 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|