2018-10-11 17:37:39 +02:00
|
|
|
defmodule MobilizonWeb.ActivityPub.ActorView do
|
|
|
|
use MobilizonWeb, :view
|
2018-05-18 09:56:21 +02:00
|
|
|
|
2019-09-09 00:52:49 +02:00
|
|
|
alias Mobilizon.Actors
|
2018-10-11 17:37:39 +02:00
|
|
|
alias Mobilizon.Actors.Actor
|
2019-09-13 01:35:03 +02:00
|
|
|
alias Mobilizon.Events.Activity
|
2018-10-11 17:37:39 +02:00
|
|
|
alias Mobilizon.Service.ActivityPub
|
|
|
|
alias Mobilizon.Service.ActivityPub.Utils
|
2018-05-18 09:56:21 +02:00
|
|
|
|
2019-04-25 19:05:05 +02:00
|
|
|
@private_visibility_empty_collection %{elements: [], total: 0}
|
|
|
|
|
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
|
|
|
|
|
|
|
%{
|
2019-07-30 16:40:59 +02:00
|
|
|
"id" => actor.url,
|
|
|
|
"type" => to_string(actor.type),
|
|
|
|
"following" => actor.following_url,
|
|
|
|
"followers" => actor.followers_url,
|
|
|
|
"inbox" => actor.inbox_url,
|
|
|
|
"outbox" => actor.outbox_url,
|
2018-05-18 09:56:21 +02:00
|
|
|
"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
|
2019-04-25 19:05:05 +02:00
|
|
|
%{total: total, elements: following} =
|
2019-09-09 00:52:49 +02:00
|
|
|
if Actor.is_public_visibility(actor),
|
2019-09-11 03:16:37 +02:00
|
|
|
do: Actors.build_followings_for_actor(actor, page),
|
2019-04-25 19:05:05 +02:00
|
|
|
else: @private_visibility_empty_collection
|
|
|
|
|
|
|
|
following
|
|
|
|
|> collection(actor.preferred_username, :following, page, total)
|
2018-05-18 09:56:21 +02:00
|
|
|
|> Map.merge(Utils.make_json_ld_header())
|
|
|
|
end
|
|
|
|
|
|
|
|
def render("following.json", %{actor: actor}) do
|
2019-04-25 19:05:05 +02:00
|
|
|
%{total: total, elements: following} =
|
2019-09-09 00:52:49 +02:00
|
|
|
if Actor.is_public_visibility(actor),
|
2019-09-11 03:16:37 +02:00
|
|
|
do: Actors.build_followings_for_actor(actor),
|
2019-04-25 19:05:05 +02:00
|
|
|
else: @private_visibility_empty_collection
|
2018-05-18 09:56:21 +02:00
|
|
|
|
|
|
|
%{
|
2019-04-25 19:05:05 +02:00
|
|
|
"id" => Actor.build_url(actor.preferred_username, :following),
|
2018-05-18 09:56:21 +02:00
|
|
|
"type" => "OrderedCollection",
|
2019-04-25 19:05:05 +02:00
|
|
|
"totalItems" => total,
|
|
|
|
"first" => collection(following, actor.preferred_username, :following, 1, total)
|
2018-05-18 09:56:21 +02:00
|
|
|
}
|
|
|
|
|> Map.merge(Utils.make_json_ld_header())
|
|
|
|
end
|
|
|
|
|
|
|
|
def render("followers.json", %{actor: actor, page: page}) do
|
2019-04-25 19:05:05 +02:00
|
|
|
%{total: total, elements: followers} =
|
2019-09-09 00:52:49 +02:00
|
|
|
if Actor.is_public_visibility(actor),
|
2019-09-11 03:16:37 +02:00
|
|
|
do: Actors.build_followers_for_actor(actor, page),
|
2019-04-25 19:05:05 +02:00
|
|
|
else: @private_visibility_empty_collection
|
|
|
|
|
|
|
|
followers
|
|
|
|
|> collection(actor.preferred_username, :followers, page, total)
|
2018-05-18 09:56:21 +02:00
|
|
|
|> Map.merge(Utils.make_json_ld_header())
|
|
|
|
end
|
|
|
|
|
|
|
|
def render("followers.json", %{actor: actor}) do
|
2019-04-25 19:05:05 +02:00
|
|
|
%{total: total, elements: followers} =
|
2019-09-09 00:52:49 +02:00
|
|
|
if Actor.is_public_visibility(actor),
|
2019-09-11 03:16:37 +02:00
|
|
|
do: Actors.build_followers_for_actor(actor),
|
2019-04-25 19:05:05 +02:00
|
|
|
else: @private_visibility_empty_collection
|
2018-05-18 09:56:21 +02:00
|
|
|
|
|
|
|
%{
|
2019-04-25 19:05:05 +02:00
|
|
|
"id" => Actor.build_url(actor.preferred_username, :followers),
|
2018-05-18 09:56:21 +02:00
|
|
|
"type" => "OrderedCollection",
|
2019-04-25 19:05:05 +02:00
|
|
|
"totalItems" => total,
|
|
|
|
"first" => collection(followers, actor.preferred_username, :followers, 1, total)
|
2018-05-18 09:56:21 +02:00
|
|
|
}
|
|
|
|
|> Map.merge(Utils.make_json_ld_header())
|
|
|
|
end
|
|
|
|
|
|
|
|
def render("outbox.json", %{actor: actor, page: page}) do
|
2019-04-25 19:05:05 +02:00
|
|
|
%{total: total, elements: followers} =
|
2019-09-09 00:52:49 +02:00
|
|
|
if Actor.is_public_visibility(actor),
|
2019-04-25 19:05:05 +02:00
|
|
|
do: ActivityPub.fetch_public_activities_for_actor(actor, page),
|
|
|
|
else: @private_visibility_empty_collection
|
2018-05-18 09:56:21 +02:00
|
|
|
|
2019-04-25 19:05:05 +02:00
|
|
|
followers
|
|
|
|
|> collection(actor.preferred_username, :outbox, page, total)
|
|
|
|
|> Map.merge(Utils.make_json_ld_header())
|
2018-05-18 09:56:21 +02:00
|
|
|
end
|
|
|
|
|
2019-04-25 19:05:05 +02:00
|
|
|
def render("outbox.json", %{actor: actor}) do
|
|
|
|
%{total: total, elements: followers} =
|
2019-09-09 00:52:49 +02:00
|
|
|
if Actor.is_public_visibility(actor),
|
2019-04-25 19:05:05 +02:00
|
|
|
do: ActivityPub.fetch_public_activities_for_actor(actor),
|
|
|
|
else: @private_visibility_empty_collection
|
|
|
|
|
2018-05-18 09:56:21 +02:00
|
|
|
%{
|
2019-04-25 19:05:05 +02:00
|
|
|
"id" => Actor.build_url(actor.preferred_username, :outbox),
|
|
|
|
"type" => "OrderedCollection",
|
|
|
|
"totalItems" => total,
|
|
|
|
"first" => collection(followers, actor.preferred_username, :outbox, 1, total)
|
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
|
|
|
|
|
2019-04-25 19:05:05 +02:00
|
|
|
@spec collection(list(), String.t(), atom(), integer(), integer()) :: map()
|
|
|
|
defp collection(collection, preferred_username, endpoint, page, total)
|
|
|
|
when endpoint in [:followers, :following, :outbox] do
|
|
|
|
offset = (page - 1) * 10
|
2018-05-18 09:56:21 +02:00
|
|
|
|
2019-04-25 19:05:05 +02:00
|
|
|
map = %{
|
|
|
|
"id" => Actor.build_url(preferred_username, endpoint, page: page),
|
2018-05-18 09:56:21 +02:00
|
|
|
"type" => "OrderedCollectionPage",
|
2019-04-25 19:05:05 +02:00
|
|
|
"partOf" => Actor.build_url(preferred_username, endpoint),
|
|
|
|
"orderedItems" => Enum.map(collection, &item/1)
|
2018-05-18 09:56:21 +02:00
|
|
|
}
|
|
|
|
|
2019-04-25 19:05:05 +02:00
|
|
|
if offset < total do
|
|
|
|
Map.put(map, "next", Actor.build_url(preferred_username, endpoint, page: page + 1))
|
|
|
|
end
|
|
|
|
|
|
|
|
map
|
2018-05-18 09:56:21 +02:00
|
|
|
end
|
2019-04-25 19:05:05 +02:00
|
|
|
|
|
|
|
def item(%Activity{data: %{"id" => id}}), do: id
|
|
|
|
def item(%Actor{url: url}), do: url
|
2018-05-18 09:56:21 +02:00
|
|
|
end
|