2020-01-26 20:36:50 +00:00
|
|
|
defmodule Mobilizon.Web.ActivityPub.ActorView do
|
|
|
|
use Mobilizon.Web, :view
|
2018-05-18 07:56:21 +00:00
|
|
|
|
2020-07-09 15:24:28 +00:00
|
|
|
alias Mobilizon.{Actors, Discussions, Events, Posts, Resources, Todos}
|
2020-02-18 07:57:00 +00:00
|
|
|
alias Mobilizon.Actors.{Actor, Member}
|
2020-07-09 15:24:28 +00:00
|
|
|
alias Mobilizon.Discussions.Discussion
|
|
|
|
alias Mobilizon.Events.Event
|
2020-01-22 01:14:42 +00:00
|
|
|
alias Mobilizon.Federation.ActivityPub
|
|
|
|
alias Mobilizon.Federation.ActivityPub.{Activity, Utils}
|
2020-01-22 21:40:40 +00:00
|
|
|
alias Mobilizon.Federation.ActivityStream.Convertible
|
2020-07-09 15:24:28 +00:00
|
|
|
alias Mobilizon.Posts.Post
|
|
|
|
alias Mobilizon.Resources.Resource
|
|
|
|
alias Mobilizon.Storage.Page
|
|
|
|
alias Mobilizon.Todos.TodoList
|
2018-05-18 07:56:21 +00:00
|
|
|
|
2019-04-25 17:05:05 +00:00
|
|
|
@private_visibility_empty_collection %{elements: [], total: 0}
|
2020-07-09 15:24:28 +00:00
|
|
|
@json_ld_header Utils.make_json_ld_header()
|
|
|
|
@selected_member_roles ~w(creator administrator moderator member)a
|
2019-04-25 17:05:05 +00:00
|
|
|
|
2018-05-18 07:56:21 +00:00
|
|
|
def render("actor.json", %{actor: actor}) do
|
2019-12-03 10:29:51 +00:00
|
|
|
actor
|
|
|
|
|> Convertible.model_to_as()
|
2018-05-18 07:56:21 +00:00
|
|
|
|> Map.merge(Utils.make_json_ld_header())
|
|
|
|
end
|
|
|
|
|
2020-08-19 09:28:23 +00: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 15:24:28 +00: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 07:56:21 +00:00
|
|
|
end
|
|
|
|
|
2020-07-09 15:24:28 +00: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 07:56:21 +00:00
|
|
|
%{
|
2020-07-09 15:24:28 +00:00
|
|
|
"id" => Actor.build_url(preferred_username, collection),
|
|
|
|
"attributedTo" => actor_url,
|
2018-05-18 07:56:21 +00:00
|
|
|
"type" => "OrderedCollection",
|
2019-04-25 17:05:05 +00:00
|
|
|
"totalItems" => total,
|
2020-07-09 15:24:28 +00:00
|
|
|
"first" => collection(elements, preferred_username, collection, 1, total)
|
2018-05-18 07:56:21 +00:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2020-07-09 15:24:28 +00: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 07:56:21 +00:00
|
|
|
end
|
|
|
|
|
2020-07-09 15:24:28 +00:00
|
|
|
defp fetch_collection(:followers, actor, page) do
|
|
|
|
Actors.build_followers_for_actor(actor, page)
|
|
|
|
end
|
2018-05-18 07:56:21 +00:00
|
|
|
|
2020-07-09 15:24:28 +00:00
|
|
|
defp fetch_collection(:members, actor, page) do
|
|
|
|
Actors.list_members_for_group(actor, @selected_member_roles, page)
|
2018-05-18 07:56:21 +00:00
|
|
|
end
|
|
|
|
|
2020-07-09 15:24:28 +00:00
|
|
|
defp fetch_collection(:resources, actor, page) do
|
|
|
|
Resources.get_resources_for_group(actor, page)
|
|
|
|
end
|
2020-02-18 07:57:00 +00:00
|
|
|
|
2020-07-09 15:24:28 +00:00
|
|
|
defp fetch_collection(:discussions, actor, page) do
|
2020-08-27 09:53:24 +00:00
|
|
|
Discussions.find_discussions_for_actor(actor, page)
|
2020-02-18 07:57:00 +00:00
|
|
|
end
|
|
|
|
|
2020-07-09 15:24:28 +00:00
|
|
|
defp fetch_collection(:posts, actor, page) do
|
|
|
|
Posts.get_posts_for_group(actor, page)
|
|
|
|
end
|
2020-02-18 07:57:00 +00:00
|
|
|
|
2020-07-09 15:24:28 +00:00
|
|
|
defp fetch_collection(:events, actor, page) do
|
2020-10-22 14:59:04 +00:00
|
|
|
Events.list_simple_organized_events_for_group(actor, page)
|
2020-02-18 07:57:00 +00:00
|
|
|
end
|
|
|
|
|
2020-07-09 15:24:28 +00:00
|
|
|
defp fetch_collection(:todos, actor, page) do
|
|
|
|
Todos.get_todo_lists_for_group(actor, page)
|
|
|
|
end
|
2020-02-18 07:57:00 +00:00
|
|
|
|
2020-07-09 15:24:28 +00: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 07:57:00 +00:00
|
|
|
end
|
|
|
|
|
2020-07-09 15:24:28 +00:00
|
|
|
defp fetch_collection(_, _, _), do: @private_visibility_empty_collection
|
2020-02-18 07:57:00 +00:00
|
|
|
|
2020-07-09 15:24:28 +00: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 07:57:00 +00:00
|
|
|
|
2020-07-09 15:24:28 +00:00
|
|
|
defp can_get_collection?(_collection_name, %Actor{} = actor, %Actor{} = actor_applicant),
|
|
|
|
do: actor_applicant_group_member?(actor, actor_applicant)
|
2018-05-18 07:56:21 +00:00
|
|
|
|
2020-07-09 15:24:28 +00:00
|
|
|
defp can_get_collection?(_, _, _), do: false
|
2018-05-18 07:56:21 +00:00
|
|
|
|
2020-07-09 15:24:28 +00: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 17:05:05 +00:00
|
|
|
|
2020-07-09 15:24:28 +00: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 07:56:21 +00:00
|
|
|
|
2019-04-25 17:05:05 +00:00
|
|
|
@spec collection(list(), String.t(), atom(), integer(), integer()) :: map()
|
|
|
|
defp collection(collection, preferred_username, endpoint, page, total)
|
2020-07-09 15:24:28 +00:00
|
|
|
when endpoint in [
|
|
|
|
:followers,
|
|
|
|
:following,
|
|
|
|
:outbox,
|
|
|
|
:members,
|
|
|
|
:resources,
|
|
|
|
:todos,
|
|
|
|
:posts,
|
|
|
|
:events,
|
|
|
|
:discussions
|
|
|
|
] do
|
2019-04-25 17:05:05 +00:00
|
|
|
offset = (page - 1) * 10
|
2018-05-18 07:56:21 +00:00
|
|
|
|
2019-04-25 17:05:05 +00:00
|
|
|
map = %{
|
|
|
|
"id" => Actor.build_url(preferred_username, endpoint, page: page),
|
2020-02-18 07:57:00 +00:00
|
|
|
"attributedTo" => Actor.build_url(preferred_username, :page),
|
2018-05-18 07:56:21 +00:00
|
|
|
"type" => "OrderedCollectionPage",
|
2019-04-25 17:05:05 +00:00
|
|
|
"partOf" => Actor.build_url(preferred_username, endpoint),
|
|
|
|
"orderedItems" => Enum.map(collection, &item/1)
|
2018-05-18 07:56:21 +00:00
|
|
|
}
|
|
|
|
|
2021-01-12 16:12:38 +00:00
|
|
|
map =
|
|
|
|
if offset < total do
|
|
|
|
Map.put(map, "next", Actor.build_url(preferred_username, endpoint, page: page + 1))
|
|
|
|
else
|
|
|
|
map
|
|
|
|
end
|
|
|
|
|
|
|
|
map =
|
|
|
|
if offset > total do
|
|
|
|
Map.put(map, "prev", Actor.build_url(preferred_username, endpoint, page: page - 1))
|
|
|
|
else
|
|
|
|
map
|
|
|
|
end
|
2019-04-25 17:05:05 +00:00
|
|
|
|
|
|
|
map
|
2018-05-18 07:56:21 +00:00
|
|
|
end
|
2019-04-25 17:05:05 +00:00
|
|
|
|
2020-09-02 06:59:59 +00:00
|
|
|
def item(%Activity{data: data}), do: data
|
2019-04-25 17:05:05 +00:00
|
|
|
def item(%Actor{url: url}), do: url
|
2020-02-18 07:57:00 +00:00
|
|
|
def item(%Member{} = member), do: Convertible.model_to_as(member)
|
|
|
|
def item(%Resource{} = resource), do: Convertible.model_to_as(resource)
|
2020-07-09 15:24:28 +00: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 07:57:00 +00: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 07:56:21 +00:00
|
|
|
end
|