Add Activity Pub endpoints cache

Signed-off-by: Thomas Citharel <tcit@tcit.fr>
This commit is contained in:
Thomas Citharel 2019-03-01 18:30:46 +01:00
parent b3a3001e90
commit a3ffc08e57
No known key found for this signature in database
GPG Key ID: A061B9DDE0CA0773
6 changed files with 104 additions and 43 deletions

View File

@ -377,4 +377,8 @@ defmodule Mobilizon.Actors.Actor do
name -> name name -> name
end end
end end
def clear_cache(%Actor{preferred_username: preferred_username, domain: nil}) do
Cachex.del(:activity_pub, "actor_" <> preferred_username)
end
end end

View File

@ -37,12 +37,17 @@ defmodule Mobilizon.Application do
worker( worker(
Cachex, Cachex,
[ [
:json, :activity_pub,
[ [
limit: 2500 limit: 2500,
expiration:
expiration(
default: :timer.minutes(3),
interval: :timer.seconds(15)
)
] ]
], ],
id: :cache_actor id: :cache_activity_pub
), ),
worker(Guardian.DB.Token.SweeperServer, []), worker(Guardian.DB.Token.SweeperServer, []),
worker(Mobilizon.Service.Federator, []) worker(Mobilizon.Service.Federator, [])

View File

@ -21,53 +21,96 @@ defmodule MobilizonWeb.ActivityPubController do
"application/activity+json, application/ld+json" "application/activity+json, application/ld+json"
] ]
@doc """
Show an Actor's ActivityPub representation
"""
@spec actor(Plug.Conn.t(), map()) :: Plug.Conn.t()
def actor(conn, %{"name" => name}) do def actor(conn, %{"name" => name}) do
with %Actor{} = actor <- Actors.get_local_actor_by_name(name) do
if conn |> get_req_header("accept") |> is_ap_header() do if conn |> get_req_header("accept") |> is_ap_header() do
conn |> render_ap_actor(actor) render_cached_actor(conn, name)
else else
conn conn
|> put_resp_content_type("text/html") |> put_resp_content_type("text/html")
|> send_file(200, "priv/static/index.html") |> send_file(200, "priv/static/index.html")
end end
else end
nil -> {:error, :not_found}
@spec render_cached_actor(Plug.Conn.t(), String.t()) :: Plug.Conn.t()
defp render_cached_actor(conn, name) do
case Cachex.fetch(:activity_pub, "actor_" <> name, &get_local_actor_by_name/1) do
{status, %Actor{} = actor} when status in [:ok, :commit] ->
conn
|> put_resp_header("content-type", "application/activity+json")
|> json(ActorView.render("actor.json", %{actor: actor}))
{:ignore, _} ->
{:error, :not_found}
end end
end end
defp get_local_actor_by_name("actor_" <> name) do
case Actors.get_local_actor_by_name(name) do
nil -> {:ignore, nil}
%Actor{} = actor -> {:commit, actor}
end
end
# Test if the request has an AP header
defp is_ap_header(ap_headers) do defp is_ap_header(ap_headers) do
length(@activity_pub_headers -- ap_headers) < 2 length(@activity_pub_headers -- ap_headers) < 2
end end
defp render_ap_actor(conn, %Actor{} = actor) do @doc """
conn Renders an Event ActivityPub's representation
|> put_resp_header("content-type", "application/activity+json") """
|> json(ActorView.render("actor.json", %{actor: actor})) @spec event(Plug.Conn.t(), map()) :: Plug.Conn.t()
end
def event(conn, %{"uuid" => uuid}) do def event(conn, %{"uuid" => uuid}) do
with %Event{} = event <- Events.get_event_full_by_uuid(uuid), case Cachex.fetch(:activity_pub, "event_" <> uuid, &get_event_full_by_uuid/1) do
true <- event.visibility in [:public, :unlisted] do {status, %Event{} = event} when status in [:ok, :commit] ->
conn conn
|> put_resp_header("content-type", "application/activity+json") |> put_resp_header("content-type", "application/activity+json")
|> json(ObjectView.render("event.json", %{event: event |> Utils.make_event_data()})) |> json(ObjectView.render("event.json", %{event: event |> Utils.make_event_data()}))
else
_ -> {:ignore, _} ->
{:error, :not_found} {:error, :not_found}
end end
end end
defp get_event_full_by_uuid("event_" <> uuid) do
with %Event{} = event <- Events.get_event_full_by_uuid(uuid),
true <- event.visibility in [:public, :unlisted] do
{:commit, event}
else
_ -> {:ignore, nil}
end
end
@doc """
Renders a Comment ActivityPub's representation
"""
@spec comment(Plug.Conn.t(), map()) :: Plug.Conn.t()
def comment(conn, %{"uuid" => uuid}) do def comment(conn, %{"uuid" => uuid}) do
case Cachex.fetch(:activity_pub, "comment_" <> uuid, &get_comment_full_by_uuid/1) do
{status, %Comment{} = comment} when status in [:ok, :commit] ->
conn
|> put_resp_header("content-type", "application/activity+json")
|> json(
ObjectView.render("comment.json", %{comment: comment |> Utils.make_comment_data()})
)
{:ignore, _} ->
{:error, :not_found}
end
end
defp get_comment_full_by_uuid("comment_" <> uuid) do
with %Comment{} = comment <- Events.get_comment_full_from_uuid(uuid) do with %Comment{} = comment <- Events.get_comment_full_from_uuid(uuid) do
# Comments are always public for now # Comments are always public for now
# TODO : Make comments maybe restricted # TODO : Make comments maybe restricted
# true <- comment.public do # true <- comment.public do
conn {:commit, comment}
|> put_resp_header("content-type", "application/activity+json")
|> json(ObjectView.render("comment.json", %{comment: comment |> Utils.make_comment_data()}))
else else
_ -> _ -> {:ignore, nil}
{:error, :not_found}
end end
end end

View File

@ -3,16 +3,22 @@ defmodule MobilizonWeb.ActivityPub.ObjectView do
alias Mobilizon.Service.ActivityPub.Utils alias Mobilizon.Service.ActivityPub.Utils
def render("event.json", %{event: event}) do def render("event.json", %{event: event}) do
{:ok, html, []} = Earmark.as_html(event["summary"])
event = %{ event = %{
"type" => "Event", "type" => "Event",
"actor" => event["actor"], "attributedTo" => event["actor"],
"id" => event["id"], "id" => event["id"],
"name" => event["title"], "name" => event["title"],
"category" => event["category"], "category" => event["category"],
"content" => html,
"source" => %{
"content" => event["summary"], "content" => event["summary"],
"mediaType" => "text/html" "mediaType" => "text/markdown"
# "published" => Timex.format!(event.inserted_at, "{ISO:Extended}"), },
# "updated" => Timex.format!(event.updated_at, "{ISO:Extended}") "mediaType" => "text/html",
"published" => event["publish_at"],
"updated" => event["updated_at"]
} }
Map.merge(event, Utils.make_json_ld_header()) Map.merge(event, Utils.make_json_ld_header())

View File

@ -283,16 +283,20 @@ defmodule Mobilizon.Service.ActivityPub.Utils do
@spec make_event_data(Event.t(), list(String.t())) :: map() @spec make_event_data(Event.t(), list(String.t())) :: map()
def make_event_data( def make_event_data(
%Event{title: title, organizer_actor: actor, uuid: uuid}, %Event{} = event,
to \\ ["https://www.w3.org/ns/activitystreams#Public"] to \\ ["https://www.w3.org/ns/activitystreams#Public"]
) do ) do
%{ %{
"type" => "Event", "type" => "Event",
"to" => to, "to" => to,
"title" => title, "title" => event.title,
"actor" => actor.url, "actor" => event.organizer_actor.url,
"uuid" => uuid, "uuid" => event.uuid,
"id" => "#{MobilizonWeb.Endpoint.url()}/events/#{uuid}" "category" => event.category,
"summary" => event.description,
"publish_at" => (event.publish_at || event.inserted_at) |> DateTime.to_iso8601(),
"updated_at" => event.updated_at |> DateTime.to_iso8601(),
"id" => "#{MobilizonWeb.Endpoint.url()}/events/#{event.uuid}"
} }
end end

View File

@ -72,11 +72,10 @@ defmodule Mobilizon.Service.Feed do
defp get_entry(%Event{} = event) do defp get_entry(%Event{} = event) do
with {:ok, html, []} <- Earmark.as_html(event.description) do with {:ok, html, []} <- Earmark.as_html(event.description) do
entry = entry =
Entry.new(event.url, event.inserted_at, event.title) Entry.new(event.url, event.publish_at || event.inserted_at, event.title)
|> Entry.link(event.url, rel: "alternate", type: "text/html") |> Entry.link(event.url, rel: "alternate", type: "text/html")
|> Entry.content({:cdata, html}, type: "html") |> Entry.content({:cdata, html}, type: "html")
|> Entry.published(event.publish_at || event.inserted_at)
entry = if event.publish_at, do: Entry.published(entry, event.publish_at), else: entry
# Add tags # Add tags
entry = entry =