2020-01-26 21:36:50 +01:00
|
|
|
defmodule Mobilizon.Web.ActivityPub.ActorView do
|
|
|
|
use Mobilizon.Web, :view
|
2018-05-18 09:56:21 +02:00
|
|
|
|
2020-07-09 17:24:28 +02:00
|
|
|
alias Mobilizon.{Actors, Discussions, Events, Posts, Resources, Todos}
|
2020-02-18 08:57:00 +01:00
|
|
|
alias Mobilizon.Actors.{Actor, Member}
|
2020-07-09 17:24:28 +02:00
|
|
|
alias Mobilizon.Discussions.Discussion
|
|
|
|
alias Mobilizon.Events.Event
|
2020-01-22 02:14:42 +01:00
|
|
|
alias Mobilizon.Federation.ActivityPub
|
|
|
|
alias Mobilizon.Federation.ActivityPub.{Activity, Utils}
|
2020-01-22 22:40:40 +01:00
|
|
|
alias Mobilizon.Federation.ActivityStream.Convertible
|
2020-07-09 17:24:28 +02:00
|
|
|
alias Mobilizon.Posts.Post
|
|
|
|
alias Mobilizon.Resources.Resource
|
|
|
|
alias Mobilizon.Storage.Page
|
|
|
|
alias Mobilizon.Todos.TodoList
|
2018-05-18 09:56:21 +02:00
|
|
|
|
2019-04-25 19:05:05 +02:00
|
|
|
@private_visibility_empty_collection %{elements: [], total: 0}
|
2020-07-09 17:24:28 +02:00
|
|
|
@json_ld_header Utils.make_json_ld_header()
|
|
|
|
@selected_member_roles ~w(creator administrator moderator member)a
|
2019-04-25 19:05:05 +02:00
|
|
|
|
2018-05-18 09:56:21 +02:00
|
|
|
def render("actor.json", %{actor: actor}) do
|
2019-12-03 11:29:51 +01:00
|
|
|
actor
|
|
|
|
|> Convertible.model_to_as()
|
2018-05-18 09:56:21 +02:00
|
|
|
|> Map.merge(Utils.make_json_ld_header())
|
|
|
|
end
|
|
|
|
|
2020-08-19 11:28:23 +02:00
|
|
|
def render("member.json", %{member: %Member{} = member}) do
|
|
|
|
member
|
|
|
|
|> Convertible.model_to_as()
|
|
|
|
|> Map.merge(Utils.make_json_ld_header())
|
|
|
|
end
|
|
|
|
|
2020-07-09 17:24:28 +02:00
|
|
|
@doc """
|
|
|
|
Render an actor collection
|
|
|
|
"""
|
|
|
|
@spec render(String.t(), map()) :: map()
|
|
|
|
def render(view_name, %{actor: %Actor{} = actor} = args) do
|
|
|
|
is_root? = is_nil(Map.get(args, :page))
|
|
|
|
page = Map.get(args, :page, 1)
|
|
|
|
collection_name = String.trim_trailing(view_name, ".json")
|
|
|
|
collection_name = String.to_existing_atom(collection_name)
|
|
|
|
|
|
|
|
%{total: total, elements: elements} =
|
|
|
|
if can_get_collection?(collection_name, actor, Map.get(args, :actor_applicant)),
|
|
|
|
do: fetch_collection(collection_name, actor, page),
|
|
|
|
else: default_collection(collection_name, actor, page)
|
|
|
|
|
|
|
|
collection =
|
|
|
|
if is_root? do
|
|
|
|
root_collection(elements, actor, collection_name, total)
|
|
|
|
else
|
|
|
|
collection(elements, actor.preferred_username, collection_name, page, total)
|
|
|
|
end
|
|
|
|
|
|
|
|
Map.merge(collection, @json_ld_header)
|
2018-05-18 09:56:21 +02:00
|
|
|
end
|
|
|
|
|
2020-07-09 17:24:28 +02:00
|
|
|
@spec root_collection(Enum.t(), Actor.t(), atom(), integer()) :: map()
|
|
|
|
defp root_collection(
|
|
|
|
elements,
|
|
|
|
%Actor{preferred_username: preferred_username, url: actor_url},
|
|
|
|
collection,
|
|
|
|
total
|
|
|
|
) do
|
2018-05-18 09:56:21 +02:00
|
|
|
%{
|
2020-07-09 17:24:28 +02:00
|
|
|
"id" => Actor.build_url(preferred_username, collection),
|
|
|
|
"attributedTo" => actor_url,
|
2018-05-18 09:56:21 +02:00
|
|
|
"type" => "OrderedCollection",
|
2019-04-25 19:05:05 +02:00
|
|
|
"totalItems" => total,
|
2020-07-09 17:24:28 +02:00
|
|
|
"first" => collection(elements, preferred_username, collection, 1, total)
|
2018-05-18 09:56:21 +02:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2020-07-09 17:24:28 +02:00
|
|
|
@spec fetch_collection(atom(), Actor.t(), integer()) :: Page.t()
|
|
|
|
defp fetch_collection(:following, actor, page) do
|
|
|
|
Actors.build_followings_for_actor(actor, page)
|
2018-05-18 09:56:21 +02:00
|
|
|
end
|
|
|
|
|
2020-07-09 17:24:28 +02:00
|
|
|
defp fetch_collection(:followers, actor, page) do
|
|
|
|
Actors.build_followers_for_actor(actor, page)
|
|
|
|
end
|
2018-05-18 09:56:21 +02:00
|
|
|
|
2020-07-09 17:24:28 +02:00
|
|
|
defp fetch_collection(:members, actor, page) do
|
|
|
|
Actors.list_members_for_group(actor, @selected_member_roles, page)
|
2018-05-18 09:56:21 +02:00
|
|
|
end
|
|
|
|
|
2020-07-09 17:24:28 +02:00
|
|
|
defp fetch_collection(:resources, actor, page) do
|
|
|
|
Resources.get_resources_for_group(actor, page)
|
|
|
|
end
|
2020-02-18 08:57:00 +01:00
|
|
|
|
2020-07-09 17:24:28 +02:00
|
|
|
defp fetch_collection(:discussions, actor, page) do
|
|
|
|
Discussions.find_discussions_for_actor(actor.id, page)
|
2020-02-18 08:57:00 +01:00
|
|
|
end
|
|
|
|
|
2020-07-09 17:24:28 +02:00
|
|
|
defp fetch_collection(:posts, actor, page) do
|
|
|
|
Posts.get_posts_for_group(actor, page)
|
|
|
|
end
|
2020-02-18 08:57:00 +01:00
|
|
|
|
2020-07-09 17:24:28 +02:00
|
|
|
defp fetch_collection(:events, actor, page) do
|
|
|
|
Events.list_organized_events_for_group(actor, page)
|
2020-02-18 08:57:00 +01:00
|
|
|
end
|
|
|
|
|
2020-07-09 17:24:28 +02:00
|
|
|
defp fetch_collection(:todos, actor, page) do
|
|
|
|
Todos.get_todo_lists_for_group(actor, page)
|
|
|
|
end
|
2020-02-18 08:57:00 +01:00
|
|
|
|
2020-07-09 17:24:28 +02:00
|
|
|
@spec fetch_collection(atom(), Actor.t(), integer()) :: %{total: integer(), elements: Enum.t()}
|
|
|
|
defp fetch_collection(:outbox, actor, page) do
|
|
|
|
ActivityPub.fetch_public_activities_for_actor(actor, page)
|
2020-02-18 08:57:00 +01:00
|
|
|
end
|
|
|
|
|
2020-07-09 17:24:28 +02:00
|
|
|
defp fetch_collection(_, _, _), do: @private_visibility_empty_collection
|
2020-02-18 08:57:00 +01:00
|
|
|
|
2020-07-09 17:24:28 +02:00
|
|
|
@spec can_get_collection?(atom(), Actor.t(), Actor.t()) :: boolean()
|
|
|
|
# Outbox only contains public activities
|
|
|
|
defp can_get_collection?(collection, %Actor{visibility: visibility} = _actor, _actor_applicant)
|
|
|
|
when visibility in [:public, :unlisted] and collection in [:outbox, :followers, :following],
|
|
|
|
do: true
|
2020-02-18 08:57:00 +01:00
|
|
|
|
2020-07-09 17:24:28 +02:00
|
|
|
defp can_get_collection?(_collection_name, %Actor{} = actor, %Actor{} = actor_applicant),
|
|
|
|
do: actor_applicant_group_member?(actor, actor_applicant)
|
2018-05-18 09:56:21 +02:00
|
|
|
|
2020-07-09 17:24:28 +02:00
|
|
|
defp can_get_collection?(_, _, _), do: false
|
2018-05-18 09:56:21 +02:00
|
|
|
|
2020-07-09 17:24:28 +02:00
|
|
|
# Posts and events allows to browse public content
|
|
|
|
defp default_collection(:posts, %Actor{} = actor, page),
|
|
|
|
do: Posts.get_public_posts_for_group(actor, page)
|
2019-04-25 19:05:05 +02:00
|
|
|
|
2020-07-09 17:24:28 +02:00
|
|
|
defp default_collection(:events, %Actor{} = actor, page),
|
|
|
|
do: Events.list_public_events_for_actor(actor, page)
|
|
|
|
|
|
|
|
defp default_collection(_, _, _), do: @private_visibility_empty_collection
|
2018-05-18 09:56:21 +02:00
|
|
|
|
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)
|
2020-07-09 17:24:28 +02:00
|
|
|
when endpoint in [
|
|
|
|
:followers,
|
|
|
|
:following,
|
|
|
|
:outbox,
|
|
|
|
:members,
|
|
|
|
:resources,
|
|
|
|
:todos,
|
|
|
|
:posts,
|
|
|
|
:events,
|
|
|
|
:discussions
|
|
|
|
] do
|
2019-04-25 19:05:05 +02:00
|
|
|
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),
|
2020-02-18 08:57:00 +01:00
|
|
|
"attributedTo" => Actor.build_url(preferred_username, :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
|
2020-02-18 08:57:00 +01:00
|
|
|
def item(%Member{} = member), do: Convertible.model_to_as(member)
|
|
|
|
def item(%Resource{} = resource), do: Convertible.model_to_as(resource)
|
2020-07-09 17:24:28 +02:00
|
|
|
def item(%Discussion{} = discussion), do: Convertible.model_to_as(discussion)
|
|
|
|
def item(%Post{} = post), do: Convertible.model_to_as(post)
|
|
|
|
def item(%Event{} = event), do: Convertible.model_to_as(event)
|
|
|
|
def item(%TodoList{} = todo_list), do: Convertible.model_to_as(todo_list)
|
2020-02-18 08:57:00 +01:00
|
|
|
|
|
|
|
defp actor_applicant_group_member?(%Actor{}, nil), do: false
|
|
|
|
|
|
|
|
defp actor_applicant_group_member?(%Actor{id: group_id}, %Actor{id: actor_applicant_id}),
|
|
|
|
do:
|
|
|
|
Actors.get_member(actor_applicant_id, group_id, [
|
|
|
|
:member,
|
|
|
|
:moderator,
|
|
|
|
:administrator,
|
|
|
|
:creator
|
|
|
|
]) != {:error, :member_not_found}
|
2018-05-18 09:56:21 +02:00
|
|
|
end
|