2018-10-11 17:37:39 +02:00
|
|
|
defmodule MobilizonWeb.ActivityPub.ActorView do
|
|
|
|
use MobilizonWeb, :view
|
2018-05-18 09:56:21 +02:00
|
|
|
|
2018-10-11 17:37:39 +02:00
|
|
|
alias MobilizonWeb.ActivityPub.ActorView
|
|
|
|
alias MobilizonWeb.ActivityPub.ObjectView
|
|
|
|
alias Mobilizon.Actors.Actor
|
|
|
|
alias Mobilizon.Service.ActivityPub
|
|
|
|
alias Mobilizon.Service.ActivityPub.Utils
|
|
|
|
alias Mobilizon.Activity
|
2018-05-18 09:56:21 +02:00
|
|
|
|
|
|
|
def render("actor.json", %{actor: actor}) do
|
2018-10-11 17:37:39 +02:00
|
|
|
public_key = Mobilizon.Service.ActivityPub.Utils.pem_to_public_key_pem(actor.keys)
|
2018-05-18 09:56:21 +02:00
|
|
|
|
|
|
|
%{
|
|
|
|
"id" => actor.url,
|
|
|
|
"type" => "Person",
|
|
|
|
"following" => actor.following_url,
|
|
|
|
"followers" => actor.followers_url,
|
|
|
|
"inbox" => actor.inbox_url,
|
|
|
|
"outbox" => actor.outbox_url,
|
|
|
|
"preferredUsername" => actor.preferred_username,
|
|
|
|
"name" => actor.name,
|
|
|
|
"summary" => actor.summary,
|
|
|
|
"url" => actor.url,
|
|
|
|
"manuallyApprovesFollowers" => actor.manually_approves_followers,
|
|
|
|
"publicKey" => %{
|
|
|
|
"id" => "#{actor.url}#main-key",
|
|
|
|
"owner" => actor.url,
|
|
|
|
"publicKeyPem" => public_key
|
|
|
|
},
|
2018-11-12 09:05:31 +01:00
|
|
|
# TODO : Make have actors have an uuid
|
|
|
|
# "uuid" => actor.uuid
|
2018-05-18 09:56:21 +02:00
|
|
|
"endpoints" => %{
|
2018-07-27 10:45:35 +02:00
|
|
|
"sharedInbox" => actor.shared_inbox_url
|
|
|
|
}
|
|
|
|
# "icon" => %{
|
|
|
|
# "type" => "Image",
|
|
|
|
# "url" => User.avatar_url(actor)
|
|
|
|
# },
|
|
|
|
# "image" => %{
|
|
|
|
# "type" => "Image",
|
|
|
|
# "url" => User.banner_url(actor)
|
|
|
|
# }
|
2018-05-18 09:56:21 +02:00
|
|
|
}
|
|
|
|
|> Map.merge(Utils.make_json_ld_header())
|
|
|
|
end
|
|
|
|
|
|
|
|
def render("following.json", %{actor: actor, page: page}) do
|
2018-06-14 18:15:27 +02:00
|
|
|
actor
|
2018-11-27 17:54:54 +01:00
|
|
|
|> Actor.get_followings(page)
|
2018-06-14 18:15:27 +02:00
|
|
|
|> collection(actor.following_url, page)
|
2018-05-18 09:56:21 +02:00
|
|
|
|> Map.merge(Utils.make_json_ld_header())
|
|
|
|
end
|
|
|
|
|
|
|
|
def render("following.json", %{actor: actor}) do
|
|
|
|
following = Actor.get_followings(actor)
|
|
|
|
|
|
|
|
%{
|
|
|
|
"id" => actor.following_url,
|
|
|
|
"type" => "OrderedCollection",
|
|
|
|
"totalItems" => length(following),
|
|
|
|
"first" => collection(following, actor.following_url, 1)
|
|
|
|
}
|
|
|
|
|> Map.merge(Utils.make_json_ld_header())
|
|
|
|
end
|
|
|
|
|
|
|
|
def render("followers.json", %{actor: actor, page: page}) do
|
2018-06-14 18:15:27 +02:00
|
|
|
actor
|
2018-11-27 17:54:54 +01:00
|
|
|
|> Actor.get_followers(page)
|
2018-06-14 18:15:27 +02:00
|
|
|
|> collection(actor.followers_url, page)
|
2018-05-18 09:56:21 +02:00
|
|
|
|> Map.merge(Utils.make_json_ld_header())
|
|
|
|
end
|
|
|
|
|
|
|
|
def render("followers.json", %{actor: actor}) do
|
|
|
|
followers = Actor.get_followers(actor)
|
|
|
|
|
|
|
|
%{
|
|
|
|
"id" => actor.followers_url,
|
|
|
|
"type" => "OrderedCollection",
|
2018-11-27 17:54:54 +01:00
|
|
|
# TODO put me back
|
|
|
|
# "totalItems" => length(followers),
|
2018-05-18 09:56:21 +02:00
|
|
|
"first" => collection(followers, actor.followers_url, 1)
|
|
|
|
}
|
|
|
|
|> Map.merge(Utils.make_json_ld_header())
|
|
|
|
end
|
|
|
|
|
|
|
|
def render("outbox.json", %{actor: actor, page: page}) do
|
2018-07-27 10:45:35 +02:00
|
|
|
{page, no_page} =
|
|
|
|
if page == 0 do
|
|
|
|
{1, true}
|
|
|
|
else
|
|
|
|
{page, false}
|
|
|
|
end
|
2018-05-18 09:56:21 +02:00
|
|
|
|
2018-07-27 10:45:35 +02:00
|
|
|
{activities, total} = ActivityPub.fetch_public_activities_for_actor(actor, page)
|
2018-05-18 09:56:21 +02:00
|
|
|
|
2018-11-12 18:17:53 +01:00
|
|
|
# collection =
|
|
|
|
# Enum.map(activities, fn act ->
|
|
|
|
# {:ok, data} = Transmogrifier.prepare_outgoing(act.data)
|
|
|
|
# data
|
|
|
|
# end)
|
2018-05-18 09:56:21 +02:00
|
|
|
|
|
|
|
iri = "#{actor.url}/outbox"
|
|
|
|
|
|
|
|
page = %{
|
|
|
|
"id" => "#{iri}?page=#{page}",
|
|
|
|
"type" => "OrderedCollectionPage",
|
|
|
|
"partOf" => iri,
|
|
|
|
"totalItems" => total,
|
|
|
|
"orderedItems" => render_many(activities, ActorView, "activity.json", as: :activity),
|
|
|
|
"next" => "#{iri}?page=#{page + 1}"
|
|
|
|
}
|
|
|
|
|
|
|
|
if no_page do
|
|
|
|
%{
|
|
|
|
"id" => iri,
|
|
|
|
"type" => "OrderedCollection",
|
|
|
|
"totalItems" => total,
|
|
|
|
"first" => page
|
|
|
|
}
|
|
|
|
|> Map.merge(Utils.make_json_ld_header())
|
|
|
|
else
|
|
|
|
page |> Map.merge(Utils.make_json_ld_header())
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-11-12 18:17:53 +01:00
|
|
|
def render("activity.json", %{activity: %Activity{local: local, data: data} = activity}) do
|
2018-05-18 09:56:21 +02:00
|
|
|
%{
|
2018-11-12 18:17:53 +01:00
|
|
|
"id" => data["id"],
|
2018-07-27 10:45:35 +02:00
|
|
|
"type" =>
|
|
|
|
if local do
|
|
|
|
"Create"
|
|
|
|
else
|
|
|
|
"Announce"
|
|
|
|
end,
|
2018-08-24 11:34:00 +02:00
|
|
|
"actor" => activity.actor,
|
2018-11-12 09:05:31 +01:00
|
|
|
# Not sure if needed since this is used into outbox
|
2018-05-18 09:56:21 +02:00
|
|
|
"published" => Timex.now(),
|
2018-11-12 18:17:53 +01:00
|
|
|
"to" => activity.recipients,
|
2018-08-24 11:34:00 +02:00
|
|
|
"object" =>
|
2018-11-12 18:17:53 +01:00
|
|
|
case data["type"] do
|
|
|
|
"Event" ->
|
|
|
|
render_one(data, ObjectView, "event.json", as: :event)
|
2018-08-24 11:34:00 +02:00
|
|
|
|
2018-11-12 18:17:53 +01:00
|
|
|
"Note" ->
|
|
|
|
render_one(data, ObjectView, "comment.json", as: :comment)
|
2018-08-24 11:34:00 +02:00
|
|
|
end
|
2018-05-18 09:56:21 +02:00
|
|
|
}
|
2018-11-12 09:05:31 +01:00
|
|
|
|> Map.merge(Utils.make_json_ld_header())
|
2018-05-18 09:56:21 +02:00
|
|
|
end
|
|
|
|
|
2018-11-27 17:54:54 +01:00
|
|
|
def collection(collection, iri, page, _total \\ nil) do
|
|
|
|
items = Enum.map(collection, fn account -> account.url end)
|
2018-05-18 09:56:21 +02:00
|
|
|
|
2018-11-27 17:54:54 +01:00
|
|
|
# TODO : Add me back
|
|
|
|
# total = total || length(collection)
|
|
|
|
|
|
|
|
%{
|
2018-05-18 09:56:21 +02:00
|
|
|
"id" => "#{iri}?page=#{page}",
|
|
|
|
"type" => "OrderedCollectionPage",
|
|
|
|
"partOf" => iri,
|
2018-11-27 17:54:54 +01:00
|
|
|
# "totalItems" => total,
|
2018-05-18 09:56:21 +02:00
|
|
|
"orderedItems" => items
|
|
|
|
}
|
|
|
|
|
2018-11-27 17:54:54 +01:00
|
|
|
# if offset < total do
|
|
|
|
# Map.put(map, "next", "#{iri}?page=#{page + 1}")
|
|
|
|
# end
|
2018-05-18 09:56:21 +02:00
|
|
|
end
|
|
|
|
end
|