41
lib/web/cache/activity_pub.ex
vendored
41
lib/web/cache/activity_pub.ex
vendored
@@ -3,11 +3,12 @@ defmodule Mobilizon.Web.Cache.ActivityPub do
|
||||
ActivityPub related cache.
|
||||
"""
|
||||
|
||||
alias Mobilizon.{Actors, Conversations, Events, Resources, Todos, Tombstone}
|
||||
alias Mobilizon.{Actors, Discussions, Events, Posts, Resources, Todos, Tombstone}
|
||||
alias Mobilizon.Actors.Actor
|
||||
alias Mobilizon.Conversations.Comment
|
||||
alias Mobilizon.Discussions.{Comment, Discussion}
|
||||
alias Mobilizon.Events.Event
|
||||
alias Mobilizon.Federation.ActivityPub.Relay
|
||||
alias Mobilizon.Posts.Post
|
||||
alias Mobilizon.Resources.Resource
|
||||
alias Mobilizon.Todos.{Todo, TodoList}
|
||||
alias Mobilizon.Web.Endpoint
|
||||
@@ -61,7 +62,7 @@ defmodule Mobilizon.Web.Cache.ActivityPub do
|
||||
{:commit, Comment.t()} | {:ignore, nil}
|
||||
def get_comment_by_uuid_with_preload(uuid) do
|
||||
Cachex.fetch(@cache, "comment_" <> uuid, fn "comment_" <> uuid ->
|
||||
case Conversations.get_comment_from_uuid_with_preload(uuid) do
|
||||
case Discussions.get_comment_from_uuid_with_preload(uuid) do
|
||||
%Comment{} = comment ->
|
||||
{:commit, comment}
|
||||
|
||||
@@ -88,6 +89,40 @@ defmodule Mobilizon.Web.Cache.ActivityPub do
|
||||
end)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Gets a post by its slug, with all associations loaded.
|
||||
"""
|
||||
@spec get_post_by_slug_with_preload(String.t()) ::
|
||||
{:commit, Post.t()} | {:ignore, nil}
|
||||
def get_post_by_slug_with_preload(slug) do
|
||||
Cachex.fetch(@cache, "post_" <> slug, fn "post_" <> slug ->
|
||||
case Posts.get_post_by_slug_with_preloads(slug) do
|
||||
%Post{} = post ->
|
||||
{:commit, post}
|
||||
|
||||
nil ->
|
||||
{:ignore, nil}
|
||||
end
|
||||
end)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Gets a discussion by its slug, with all associations loaded.
|
||||
"""
|
||||
@spec get_discussion_by_slug_with_preload(String.t()) ::
|
||||
{:commit, Discussion.t()} | {:ignore, nil}
|
||||
def get_discussion_by_slug_with_preload(slug) do
|
||||
Cachex.fetch(@cache, "discussion_" <> slug, fn "discussion_" <> slug ->
|
||||
case Discussions.get_discussion_by_slug(slug) do
|
||||
%Discussion{} = discussion ->
|
||||
{:commit, discussion}
|
||||
|
||||
nil ->
|
||||
{:ignore, nil}
|
||||
end
|
||||
end)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Gets a todo list by its UUID, with all associations loaded.
|
||||
"""
|
||||
|
||||
2
lib/web/cache/cache.ex
vendored
2
lib/web/cache/cache.ex
vendored
@@ -23,5 +23,7 @@ defmodule Mobilizon.Web.Cache do
|
||||
defdelegate get_resource_by_uuid_with_preload(uuid), to: ActivityPub
|
||||
defdelegate get_todo_list_by_uuid_with_preload(uuid), to: ActivityPub
|
||||
defdelegate get_todo_by_uuid_with_preload(uuid), to: ActivityPub
|
||||
defdelegate get_post_by_slug_with_preload(slug), to: ActivityPub
|
||||
defdelegate get_discussion_by_slug_with_preload(slug), to: ActivityPub
|
||||
defdelegate get_relay, to: ActivityPub
|
||||
end
|
||||
|
||||
@@ -14,6 +14,7 @@ defmodule Mobilizon.Web.ActivityPubController do
|
||||
|
||||
alias Mobilizon.Web.ActivityPub.ActorView
|
||||
alias Mobilizon.Web.Cache
|
||||
alias Plug.Conn
|
||||
|
||||
require Logger
|
||||
|
||||
@@ -33,96 +34,40 @@ defmodule Mobilizon.Web.ActivityPubController do
|
||||
end
|
||||
end
|
||||
|
||||
def following(conn, %{"name" => name, "page" => page}) do
|
||||
with {page, ""} <- Integer.parse(page),
|
||||
%Actor{} = actor <- Actors.get_local_actor_by_name_with_preload(name) do
|
||||
conn
|
||||
|> put_resp_header("content-type", "application/activity+json")
|
||||
|> json(ActorView.render("following.json", %{actor: actor, page: page}))
|
||||
end
|
||||
def following(conn, args) do
|
||||
actor_collection(conn, "following", args)
|
||||
end
|
||||
|
||||
def following(conn, %{"name" => name}) do
|
||||
with %Actor{} = actor <- Actors.get_local_actor_by_name_with_preload(name) do
|
||||
conn
|
||||
|> put_resp_header("content-type", "application/activity+json")
|
||||
|> json(ActorView.render("following.json", %{actor: actor}))
|
||||
end
|
||||
def followers(conn, args) do
|
||||
actor_collection(conn, "followers", args)
|
||||
end
|
||||
|
||||
def followers(conn, %{"name" => name, "page" => page}) do
|
||||
with {page, ""} <- Integer.parse(page),
|
||||
%Actor{} = actor <- Actors.get_local_actor_by_name_with_preload(name) do
|
||||
conn
|
||||
|> put_resp_header("content-type", "application/activity+json")
|
||||
|> json(ActorView.render("followers.json", %{actor: actor, page: page}))
|
||||
end
|
||||
def members(conn, args) do
|
||||
actor_collection(conn, "members", args)
|
||||
end
|
||||
|
||||
def followers(conn, %{"name" => name}) do
|
||||
with %Actor{} = actor <- Actors.get_local_actor_by_name_with_preload(name) do
|
||||
conn
|
||||
|> put_resp_header("content-type", "application/activity+json")
|
||||
|> json(ActorView.render("followers.json", %{actor: actor}))
|
||||
end
|
||||
def resources(conn, args) do
|
||||
actor_collection(conn, "resources", args)
|
||||
end
|
||||
|
||||
def members(conn, %{"name" => name, "page" => page}) do
|
||||
with {page, ""} <- Integer.parse(page),
|
||||
%Actor{} = group <- Actors.get_local_actor_by_name_with_preload(name) do
|
||||
conn
|
||||
|> put_resp_header("content-type", "application/activity+json")
|
||||
|> json(
|
||||
ActorView.render("members.json", %{
|
||||
group: group,
|
||||
page: page,
|
||||
actor_applicant: Map.get(conn.assigns, :actor)
|
||||
})
|
||||
)
|
||||
end
|
||||
def posts(conn, args) do
|
||||
actor_collection(conn, "posts", args)
|
||||
end
|
||||
|
||||
def members(conn, %{"name" => name}) do
|
||||
with %Actor{} = group <- Actors.get_local_actor_by_name_with_preload(name) do
|
||||
conn
|
||||
|> put_resp_header("content-type", "application/activity+json")
|
||||
|> json(
|
||||
ActorView.render("members.json", %{
|
||||
group: group,
|
||||
actor_applicant: Map.get(conn.assigns, :actor)
|
||||
})
|
||||
)
|
||||
end
|
||||
def todos(conn, args) do
|
||||
actor_collection(conn, "todos", args)
|
||||
end
|
||||
|
||||
def resources(conn, %{"name" => name}) do
|
||||
with %Actor{} = group <- Actors.get_local_actor_by_name_with_preload(name) do
|
||||
conn
|
||||
|> put_resp_header("content-type", "application/activity+json")
|
||||
|> json(
|
||||
ActorView.render("resources.json", %{
|
||||
group: group,
|
||||
actor_applicant: Map.get(conn.assigns, :actor)
|
||||
})
|
||||
)
|
||||
end
|
||||
def events(conn, args) do
|
||||
actor_collection(conn, "events", args)
|
||||
end
|
||||
|
||||
def outbox(conn, %{"name" => name, "page" => page}) do
|
||||
with {page, ""} <- Integer.parse(page),
|
||||
%Actor{} = actor <- Actors.get_local_actor_by_name(name) do
|
||||
conn
|
||||
|> put_resp_header("content-type", "application/activity+json")
|
||||
|> json(ActorView.render("outbox.json", %{actor: actor, page: page}))
|
||||
end
|
||||
def discussions(conn, args) do
|
||||
actor_collection(conn, "discussions", args)
|
||||
end
|
||||
|
||||
def outbox(conn, %{"name" => name}) do
|
||||
with %Actor{} = actor <- Actors.get_local_actor_by_name(name) do
|
||||
conn
|
||||
|> put_resp_header("content-type", "application/activity+json")
|
||||
|> json(ActorView.render("outbox.json", %{actor: actor}))
|
||||
end
|
||||
def outbox(conn, args) do
|
||||
actor_collection(conn, "outbox", args)
|
||||
end
|
||||
|
||||
# TODO: Ensure that this inbox is a recipient of the message
|
||||
@@ -178,4 +123,34 @@ defmodule Mobilizon.Web.ActivityPubController do
|
||||
|> put_status(500)
|
||||
|> json("Unknown Error")
|
||||
end
|
||||
|
||||
@spec actor_collection(Conn.t(), String.t(), map()) :: Conn.t()
|
||||
|
||||
defp actor_collection(conn, collection, %{"name" => name, "page" => page}) do
|
||||
with {page, ""} <- Integer.parse(page),
|
||||
%Actor{} = actor <- Actors.get_local_actor_by_name_with_preload(name) do
|
||||
conn
|
||||
|> put_resp_header("content-type", "application/activity+json")
|
||||
|> json(
|
||||
ActorView.render("#{collection}.json", %{
|
||||
actor: actor,
|
||||
page: page,
|
||||
actor_applicant: Map.get(conn.assigns, :actor)
|
||||
})
|
||||
)
|
||||
end
|
||||
end
|
||||
|
||||
defp actor_collection(conn, collection, %{"name" => name}) do
|
||||
with %Actor{} = actor <- Actors.get_local_actor_by_name_with_preload(name) do
|
||||
conn
|
||||
|> put_resp_header("content-type", "application/activity+json")
|
||||
|> json(
|
||||
ActorView.render("#{collection}.json", %{
|
||||
actor: actor,
|
||||
actor_applicant: Map.get(conn.assigns, :actor)
|
||||
})
|
||||
)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -4,7 +4,7 @@ defmodule Mobilizon.Web.PageController do
|
||||
"""
|
||||
use Mobilizon.Web, :controller
|
||||
|
||||
alias Mobilizon.Conversations.Comment
|
||||
alias Mobilizon.Discussions.Comment
|
||||
alias Mobilizon.Events.Event
|
||||
alias Mobilizon.Federation.ActivityPub
|
||||
alias Mobilizon.Tombstone
|
||||
@@ -40,14 +40,36 @@ defmodule Mobilizon.Web.PageController do
|
||||
render_or_error(conn, &checks?/3, status, :resource, resource)
|
||||
end
|
||||
|
||||
def resources(conn, %{"name" => _name}) do
|
||||
case get_format(conn) do
|
||||
"html" ->
|
||||
render(conn, :index)
|
||||
@spec post(Plug.Conn.t(), map()) :: Plug.Conn.t() | {:error, :not_found}
|
||||
def post(conn, %{"slug" => slug}) do
|
||||
{status, post} = Cache.get_post_by_slug_with_preload(slug)
|
||||
render_or_error(conn, &checks?/3, status, :post, post)
|
||||
end
|
||||
|
||||
"activity-json" ->
|
||||
ActivityPubController.call(conn, :resources)
|
||||
end
|
||||
@spec discussion(Plug.Conn.t(), map()) :: Plug.Conn.t() | {:error, :not_found}
|
||||
def discussion(conn, %{"slug" => slug}) do
|
||||
{status, discussion} = Cache.get_discussion_by_slug_with_preload(slug)
|
||||
render_or_error(conn, &checks?/3, status, :discussion, discussion)
|
||||
end
|
||||
|
||||
def resources(conn, %{"name" => _name}) do
|
||||
handle_collection_route(conn, :resources)
|
||||
end
|
||||
|
||||
def posts(conn, %{"name" => _name}) do
|
||||
handle_collection_route(conn, :posts)
|
||||
end
|
||||
|
||||
def discussions(conn, %{"name" => _name}) do
|
||||
handle_collection_route(conn, :discussions)
|
||||
end
|
||||
|
||||
def events(conn, %{"name" => _name}) do
|
||||
handle_collection_route(conn, :events)
|
||||
end
|
||||
|
||||
def todos(conn, %{"name" => _name}) do
|
||||
handle_collection_route(conn, :todos)
|
||||
end
|
||||
|
||||
@spec todo_list(Plug.Conn.t(), map) :: {:error, :not_found} | Plug.Conn.t()
|
||||
@@ -71,6 +93,16 @@ defmodule Mobilizon.Web.PageController do
|
||||
end
|
||||
end
|
||||
|
||||
defp handle_collection_route(conn, collection) do
|
||||
case get_format(conn) do
|
||||
"html" ->
|
||||
render(conn, :index)
|
||||
|
||||
"activity-json" ->
|
||||
ActivityPubController.call(conn, collection)
|
||||
end
|
||||
end
|
||||
|
||||
defp render_or_error(conn, check_fn, status, object_type, object) do
|
||||
case check_fn.(conn, status, object) do
|
||||
true ->
|
||||
|
||||
@@ -82,12 +82,4 @@ defmodule Mobilizon.Web.Email.Event do
|
||||
|> Email.Event.event_updated(actor, old_event, event, diff, locale)
|
||||
|> Email.Mailer.deliver_later()
|
||||
end
|
||||
|
||||
defp send_notification_for_event_update_to_participant(user, old_event, new_event, diff) do
|
||||
require Logger
|
||||
Logger.error(inspect(user))
|
||||
Logger.error(inspect(old_event))
|
||||
Logger.error(inspect(new_event))
|
||||
Logger.error(inspect(diff))
|
||||
end
|
||||
end
|
||||
|
||||
@@ -84,7 +84,6 @@ defmodule Mobilizon.Web.ReverseProxy do
|
||||
| {:redirect_on_failure, boolean}
|
||||
|
||||
@hackney Application.get_env(:mobilizon, :hackney, :hackney)
|
||||
@httpoison Application.get_env(:mobilizon, :httpoison, HTTPoison)
|
||||
|
||||
@default_hackney_options []
|
||||
|
||||
@@ -108,7 +107,6 @@ defmodule Mobilizon.Web.ReverseProxy do
|
||||
hackney_opts =
|
||||
@default_hackney_options
|
||||
|> Keyword.merge(Keyword.get(opts, :http, []))
|
||||
|> @httpoison.process_request_options()
|
||||
|
||||
req_headers = build_req_headers(conn.req_headers, opts)
|
||||
|
||||
|
||||
@@ -87,17 +87,23 @@ defmodule Mobilizon.Web.Router do
|
||||
get("/resource/:uuid", PageController, :resource, as: "resource")
|
||||
get("/todo-list/:uuid", PageController, :todo_list, as: "todo_list")
|
||||
get("/todo/:uuid", PageController, :todo, as: "todo")
|
||||
get("/@:name/todos", PageController, :todos)
|
||||
get("/@:name/resources", PageController, :resources)
|
||||
get("/@:name/posts", PageController, :posts)
|
||||
get("/@:name/discussions", PageController, :discussions)
|
||||
get("/@:name/events", PageController, :events)
|
||||
get("/p/:slug", PageController, :post)
|
||||
get("/@:name/c/:slug", PageController, :discussion)
|
||||
end
|
||||
|
||||
scope "/", Mobilizon.Web do
|
||||
pipe_through(:activity_pub)
|
||||
pipe_through(:activity_pub_signature)
|
||||
|
||||
get("/@:name/outbox", ActivityPubController, :outbox)
|
||||
get("/@:name/following", ActivityPubController, :following)
|
||||
get("/@:name/followers", ActivityPubController, :followers)
|
||||
get("/@:name/members", ActivityPubController, :members)
|
||||
get("/@:name/todo-lists", ActivityPubController, :todo_lists)
|
||||
end
|
||||
|
||||
scope "/", Mobilizon.Web do
|
||||
|
||||
@@ -64,7 +64,7 @@ defmodule Mobilizon.Web.Upload do
|
||||
}
|
||||
defstruct [:id, :name, :tempfile, :content_type, :path, :size]
|
||||
|
||||
@spec store(source, options :: [option()]) :: {:ok, Map.t()} | {:error, any()}
|
||||
@spec store(source, options :: [option()]) :: {:ok, map()} | {:error, any()}
|
||||
def store(upload, opts \\ []) do
|
||||
opts = get_opts(opts)
|
||||
|
||||
|
||||
@@ -37,7 +37,7 @@ defmodule Mobilizon.Web.Upload.Uploader do
|
||||
|
||||
@callback remove_file(file_spec()) :: :ok | {:ok, file_spec()} | {:error, String.t()}
|
||||
|
||||
@callback http_callback(Plug.Conn.t(), Map.t()) ::
|
||||
@callback http_callback(Plug.Conn.t(), map()) ::
|
||||
{:ok, Plug.Conn.t()}
|
||||
| {:ok, Plug.Conn.t(), file_spec()}
|
||||
| {:error, Plug.Conn.t(), String.t()}
|
||||
|
||||
@@ -1,16 +1,21 @@
|
||||
defmodule Mobilizon.Web.ActivityPub.ActorView do
|
||||
use Mobilizon.Web, :view
|
||||
|
||||
alias Mobilizon.Actors
|
||||
alias Mobilizon.{Actors, Discussions, Events, Posts, Resources, Todos}
|
||||
alias Mobilizon.Actors.{Actor, Member}
|
||||
alias Mobilizon.Resources
|
||||
alias Mobilizon.Resources.Resource
|
||||
|
||||
alias Mobilizon.Discussions.Discussion
|
||||
alias Mobilizon.Events.Event
|
||||
alias Mobilizon.Federation.ActivityPub
|
||||
alias Mobilizon.Federation.ActivityPub.{Activity, Utils}
|
||||
alias Mobilizon.Federation.ActivityStream.Convertible
|
||||
alias Mobilizon.Posts.Post
|
||||
alias Mobilizon.Resources.Resource
|
||||
alias Mobilizon.Storage.Page
|
||||
alias Mobilizon.Todos.TodoList
|
||||
|
||||
@private_visibility_empty_collection %{elements: [], total: 0}
|
||||
@json_ld_header Utils.make_json_ld_header()
|
||||
@selected_member_roles ~w(creator administrator moderator member)a
|
||||
|
||||
def render("actor.json", %{actor: actor}) do
|
||||
actor
|
||||
@@ -18,145 +23,120 @@ defmodule Mobilizon.Web.ActivityPub.ActorView do
|
||||
|> Map.merge(Utils.make_json_ld_header())
|
||||
end
|
||||
|
||||
def render("following.json", %{actor: actor, page: page}) do
|
||||
%{total: total, elements: following} =
|
||||
if Actor.is_public_visibility(actor),
|
||||
do: Actors.build_followings_for_actor(actor, page),
|
||||
else: @private_visibility_empty_collection
|
||||
@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)
|
||||
|
||||
following
|
||||
|> collection(actor.preferred_username, :following, page, total)
|
||||
|> Map.merge(Utils.make_json_ld_header())
|
||||
%{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)
|
||||
end
|
||||
|
||||
def render("following.json", %{actor: actor}) do
|
||||
%{total: total, elements: following} =
|
||||
if Actor.is_public_visibility(actor),
|
||||
do: Actors.build_followings_for_actor(actor),
|
||||
else: @private_visibility_empty_collection
|
||||
|
||||
@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
|
||||
%{
|
||||
"id" => Actor.build_url(actor.preferred_username, :following),
|
||||
"id" => Actor.build_url(preferred_username, collection),
|
||||
"attributedTo" => actor_url,
|
||||
"type" => "OrderedCollection",
|
||||
"totalItems" => total,
|
||||
"first" => collection(following, actor.preferred_username, :following, 1, total)
|
||||
"first" => collection(elements, preferred_username, collection, 1, total)
|
||||
}
|
||||
|> Map.merge(Utils.make_json_ld_header())
|
||||
end
|
||||
|
||||
def render("followers.json", %{actor: actor, page: page}) do
|
||||
%{total: total, elements: followers} =
|
||||
if Actor.is_public_visibility(actor),
|
||||
do: Actors.build_followers_for_actor(actor, page),
|
||||
else: @private_visibility_empty_collection
|
||||
|
||||
followers
|
||||
|> collection(actor.preferred_username, :followers, page, total)
|
||||
|> Map.merge(Utils.make_json_ld_header())
|
||||
@spec fetch_collection(atom(), Actor.t(), integer()) :: Page.t()
|
||||
defp fetch_collection(:following, actor, page) do
|
||||
Actors.build_followings_for_actor(actor, page)
|
||||
end
|
||||
|
||||
def render("followers.json", %{actor: actor}) do
|
||||
%{total: total, elements: followers} =
|
||||
if Actor.is_public_visibility(actor),
|
||||
do: Actors.build_followers_for_actor(actor),
|
||||
else: @private_visibility_empty_collection
|
||||
|
||||
%{
|
||||
"id" => actor.followers_url,
|
||||
"type" => "OrderedCollection",
|
||||
"totalItems" => total,
|
||||
"first" => collection(followers, actor.preferred_username, :followers, 1, total)
|
||||
}
|
||||
|> Map.merge(Utils.make_json_ld_header())
|
||||
defp fetch_collection(:followers, actor, page) do
|
||||
Actors.build_followers_for_actor(actor, page)
|
||||
end
|
||||
|
||||
def render("members.json", %{group: group, page: page, actor_applicant: actor_applicant}) do
|
||||
%{total: total, elements: members} =
|
||||
if Actor.is_public_visibility(group) ||
|
||||
actor_applicant_group_member?(group, actor_applicant),
|
||||
do: Actors.list_members_for_group(group, page),
|
||||
else: @private_visibility_empty_collection
|
||||
|
||||
members
|
||||
|> collection(group.preferred_username, :members, page, total)
|
||||
|> Map.merge(Utils.make_json_ld_header())
|
||||
defp fetch_collection(:members, actor, page) do
|
||||
Actors.list_members_for_group(actor, @selected_member_roles, page)
|
||||
end
|
||||
|
||||
def render("members.json", %{group: group, actor_applicant: actor_applicant}) do
|
||||
%{total: total, elements: members} =
|
||||
if Actor.is_public_visibility(group) ||
|
||||
actor_applicant_group_member?(group, actor_applicant),
|
||||
do: Actors.list_members_for_group(group),
|
||||
else: @private_visibility_empty_collection
|
||||
|
||||
%{
|
||||
"id" => group.url,
|
||||
"attributedTo" => group.url,
|
||||
"type" => "OrderedCollection",
|
||||
"totalItems" => total,
|
||||
"first" => collection(members, group.preferred_username, :members, 1, total)
|
||||
}
|
||||
|> Map.merge(Utils.make_json_ld_header())
|
||||
defp fetch_collection(:resources, actor, page) do
|
||||
Resources.get_resources_for_group(actor, page)
|
||||
end
|
||||
|
||||
def render("resources.json", %{group: group, page: page, actor_applicant: actor_applicant}) do
|
||||
%{total: total, elements: resources} =
|
||||
if Actor.is_public_visibility(group) ||
|
||||
actor_applicant_group_member?(group, actor_applicant),
|
||||
do: Resources.get_top_level_resources_for_group(group),
|
||||
else: @private_visibility_empty_collection
|
||||
|
||||
resources
|
||||
|> collection(group.preferred_username, :resources, page, total)
|
||||
|> Map.merge(Utils.make_json_ld_header())
|
||||
defp fetch_collection(:discussions, actor, page) do
|
||||
Discussions.find_discussions_for_actor(actor.id, page)
|
||||
end
|
||||
|
||||
def render("resources.json", %{group: group, actor_applicant: actor_applicant}) do
|
||||
%{total: total, elements: resources} =
|
||||
if Actor.is_public_visibility(group) ||
|
||||
actor_applicant_group_member?(group, actor_applicant),
|
||||
do: Resources.get_top_level_resources_for_group(group),
|
||||
else: @private_visibility_empty_collection
|
||||
|
||||
%{
|
||||
"id" => group.resources_url,
|
||||
"attributedTo" => group.url,
|
||||
"type" => "OrderedCollection",
|
||||
"totalItems" => total,
|
||||
"first" => collection(resources, group.preferred_username, :resources, 1, total)
|
||||
}
|
||||
|> Map.merge(Utils.make_json_ld_header())
|
||||
defp fetch_collection(:posts, actor, page) do
|
||||
Posts.get_posts_for_group(actor, page)
|
||||
end
|
||||
|
||||
def render("outbox.json", %{actor: actor, page: page}) do
|
||||
%{total: total, elements: followers} =
|
||||
if Actor.is_public_visibility(actor),
|
||||
do: ActivityPub.fetch_public_activities_for_actor(actor, page),
|
||||
else: @private_visibility_empty_collection
|
||||
|
||||
followers
|
||||
|> collection(actor.preferred_username, :outbox, page, total)
|
||||
|> Map.merge(Utils.make_json_ld_header())
|
||||
defp fetch_collection(:events, actor, page) do
|
||||
Events.list_organized_events_for_group(actor, page)
|
||||
end
|
||||
|
||||
def render("outbox.json", %{actor: actor}) do
|
||||
%{total: total, elements: followers} =
|
||||
if Actor.is_public_visibility(actor),
|
||||
do: ActivityPub.fetch_public_activities_for_actor(actor),
|
||||
else: @private_visibility_empty_collection
|
||||
|
||||
%{
|
||||
"id" => Actor.build_url(actor.preferred_username, :outbox),
|
||||
"type" => "OrderedCollection",
|
||||
"totalItems" => total,
|
||||
"first" => collection(followers, actor.preferred_username, :outbox, 1, total)
|
||||
}
|
||||
|> Map.merge(Utils.make_json_ld_header())
|
||||
defp fetch_collection(:todos, actor, page) do
|
||||
Todos.get_todo_lists_for_group(actor, page)
|
||||
end
|
||||
|
||||
@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)
|
||||
end
|
||||
|
||||
defp fetch_collection(_, _, _), do: @private_visibility_empty_collection
|
||||
|
||||
@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
|
||||
|
||||
defp can_get_collection?(_collection_name, %Actor{} = actor, %Actor{} = actor_applicant),
|
||||
do: actor_applicant_group_member?(actor, actor_applicant)
|
||||
|
||||
defp can_get_collection?(_, _, _), do: false
|
||||
|
||||
# Posts and events allows to browse public content
|
||||
defp default_collection(:posts, %Actor{} = actor, page),
|
||||
do: Posts.get_public_posts_for_group(actor, page)
|
||||
|
||||
defp default_collection(:events, %Actor{} = actor, page),
|
||||
do: Events.list_public_events_for_actor(actor, page)
|
||||
|
||||
defp default_collection(_, _, _), do: @private_visibility_empty_collection
|
||||
|
||||
@spec collection(list(), String.t(), atom(), integer(), integer()) :: map()
|
||||
defp collection(collection, preferred_username, endpoint, page, total)
|
||||
when endpoint in [:followers, :following, :outbox, :members, :resources, :todos] do
|
||||
when endpoint in [
|
||||
:followers,
|
||||
:following,
|
||||
:outbox,
|
||||
:members,
|
||||
:resources,
|
||||
:todos,
|
||||
:posts,
|
||||
:events,
|
||||
:discussions
|
||||
] do
|
||||
offset = (page - 1) * 10
|
||||
|
||||
map = %{
|
||||
@@ -178,6 +158,10 @@ defmodule Mobilizon.Web.ActivityPub.ActorView do
|
||||
def item(%Actor{url: url}), do: url
|
||||
def item(%Member{} = member), do: Convertible.model_to_as(member)
|
||||
def item(%Resource{} = resource), do: Convertible.model_to_as(resource)
|
||||
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)
|
||||
|
||||
defp actor_applicant_group_member?(%Actor{}, nil), do: false
|
||||
|
||||
|
||||
@@ -4,24 +4,30 @@ defmodule Mobilizon.Web.JsonLD.ObjectView do
|
||||
alias Mobilizon.Actors.Actor
|
||||
alias Mobilizon.Addresses.Address
|
||||
alias Mobilizon.Events.Event
|
||||
|
||||
alias Mobilizon.Posts.Post
|
||||
alias Mobilizon.Web.JsonLD.ObjectView
|
||||
alias Mobilizon.Web.MediaProxy
|
||||
|
||||
def render("event.json", %{event: %Event{} = event}) do
|
||||
# TODO: event.description is actually markdown!
|
||||
organizer = %{
|
||||
"@type" => if(event.organizer_actor.type == :Group, do: "Organization", else: "Person"),
|
||||
"name" => Actor.display_name(event.organizer_actor)
|
||||
}
|
||||
|
||||
json_ld = %{
|
||||
"@context" => "https://schema.org",
|
||||
"@type" => "Event",
|
||||
"name" => event.title,
|
||||
"description" => event.description,
|
||||
"performer" => %{
|
||||
"@type" =>
|
||||
if(event.organizer_actor.type == :Group, do: "PerformingGroup", else: "Person"),
|
||||
"name" => Actor.display_name(event.organizer_actor)
|
||||
},
|
||||
"location" => render_one(event.physical_address, ObjectView, "place.json", as: :address)
|
||||
# We assume for now performer == organizer
|
||||
"performer" => organizer,
|
||||
"organizer" => organizer,
|
||||
"location" => render_one(event.physical_address, ObjectView, "place.json", as: :address),
|
||||
"eventStatus" =>
|
||||
if(event.status == :cancelled,
|
||||
do: "https://schema.org/EventCancelled",
|
||||
else: "https://schema.org/EventScheduled"
|
||||
)
|
||||
}
|
||||
|
||||
json_ld =
|
||||
@@ -62,4 +68,18 @@ defmodule Mobilizon.Web.JsonLD.ObjectView do
|
||||
end
|
||||
|
||||
def render("place.json", nil), do: %{}
|
||||
|
||||
def render("post.json", %{post: %Post{} = post}) do
|
||||
%{
|
||||
"@context" => "https://schema.org",
|
||||
"@type" => "Article",
|
||||
"name" => post.title,
|
||||
"author" => %{
|
||||
"@type" => "Organization",
|
||||
"name" => Actor.display_name(post.attributed_to)
|
||||
},
|
||||
"datePublished" => post.publish_at,
|
||||
"dateModified" => post.updated_at
|
||||
}
|
||||
end
|
||||
end
|
||||
|
||||
@@ -6,7 +6,7 @@ defmodule Mobilizon.Web.PageView do
|
||||
use Mobilizon.Web, :view
|
||||
|
||||
alias Mobilizon.Actors.Actor
|
||||
alias Mobilizon.Conversations.Comment
|
||||
alias Mobilizon.Discussions.{Comment, Discussion}
|
||||
alias Mobilizon.Events.Event
|
||||
alias Mobilizon.Resources.Resource
|
||||
alias Mobilizon.Tombstone
|
||||
@@ -42,6 +42,12 @@ defmodule Mobilizon.Web.PageView do
|
||||
|> Map.merge(Utils.make_json_ld_header())
|
||||
end
|
||||
|
||||
def render("discussion.activity-json", %{conn: %{assigns: %{object: %Discussion{} = resource}}}) do
|
||||
resource
|
||||
|> Convertible.model_to_as()
|
||||
|> Map.merge(Utils.make_json_ld_header())
|
||||
end
|
||||
|
||||
def render("resource.activity-json", %{conn: %{assigns: %{object: %Resource{} = resource}}}) do
|
||||
resource
|
||||
|> Convertible.model_to_as()
|
||||
@@ -49,12 +55,15 @@ defmodule Mobilizon.Web.PageView do
|
||||
end
|
||||
|
||||
def render(page, %{object: object, conn: conn} = _assigns)
|
||||
when page in ["actor.html", "event.html", "comment.html"] do
|
||||
when page in ["actor.html", "event.html", "comment.html", "post.html"] do
|
||||
locale = get_locale(conn)
|
||||
tags = object |> Metadata.build_tags(locale)
|
||||
inject_tags(tags, locale)
|
||||
end
|
||||
|
||||
# Discussions are private, no need to embed metadata
|
||||
def render("discussion.html", params), do: render("index.html", params)
|
||||
|
||||
def render("index.html", %{conn: conn}) do
|
||||
tags = Instance.build_tags()
|
||||
inject_tags(tags, get_locale(conn))
|
||||
|
||||
@@ -5,7 +5,7 @@ defmodule Mobilizon.Web.Views.Utils do
|
||||
|
||||
alias Mobilizon.Service.Metadata.Utils, as: MetadataUtils
|
||||
|
||||
@spec inject_tags(List.t(), String.t()) :: {:safe, String.t()}
|
||||
@spec inject_tags(Enum.t(), String.t()) :: {:safe, String.t()}
|
||||
def inject_tags(tags, locale \\ "en") do
|
||||
with {:ok, index_content} <- File.read(index_file_path()) do
|
||||
do_replacements(index_content, MetadataUtils.stringify_tags(tags), locale)
|
||||
|
||||
Reference in New Issue
Block a user