Refactor Atom feed caching
Signed-off-by: Thomas Citharel <tcit@tcit.fr> Fixes Fix tests Fix tests
This commit is contained in:
@@ -3,6 +3,7 @@ defmodule Mobilizon.Application do
|
||||
The Mobilizon application
|
||||
"""
|
||||
use Application
|
||||
import Cachex.Spec
|
||||
|
||||
# See https://hexdocs.pm/elixir/Application.html
|
||||
# for more information on OTP Applications
|
||||
@@ -17,7 +18,32 @@ defmodule Mobilizon.Application do
|
||||
supervisor(MobilizonWeb.Endpoint, []),
|
||||
# Start your own worker by calling: Mobilizon.Worker.start_link(arg1, arg2, arg3)
|
||||
# worker(Mobilizon.Worker, [arg1, arg2, arg3]),
|
||||
worker(Cachex, [:mobilizon, []]),
|
||||
worker(
|
||||
Cachex,
|
||||
[
|
||||
:feed,
|
||||
[
|
||||
limit: 2500,
|
||||
expiration:
|
||||
expiration(
|
||||
default: :timer.minutes(60),
|
||||
interval: :timer.seconds(60)
|
||||
),
|
||||
fallback: fallback(default: &Mobilizon.Service.Feed.create_cache/1)
|
||||
]
|
||||
],
|
||||
id: :cache_feed
|
||||
),
|
||||
worker(
|
||||
Cachex,
|
||||
[
|
||||
:json,
|
||||
[
|
||||
limit: 2500
|
||||
]
|
||||
],
|
||||
id: :cache_actor
|
||||
),
|
||||
worker(Guardian.DB.Token.SweeperServer, []),
|
||||
worker(Mobilizon.Service.Federator, [])
|
||||
]
|
||||
|
||||
@@ -21,10 +21,6 @@ defmodule MobilizonWeb.ActivityPubController do
|
||||
"application/activity+json, application/ld+json"
|
||||
]
|
||||
|
||||
def actor(conn, %{"name" => _name, "_format" => "atom"} = params) do
|
||||
MobilizonWeb.FeedController.actor(conn, params)
|
||||
end
|
||||
|
||||
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
|
||||
|
||||
@@ -4,102 +4,17 @@ defmodule MobilizonWeb.FeedController do
|
||||
"""
|
||||
use MobilizonWeb, :controller
|
||||
|
||||
alias Mobilizon.Actors
|
||||
alias Mobilizon.Actors.Actor
|
||||
alias Mobilizon.Events
|
||||
alias Mobilizon.Events.Event
|
||||
alias Atomex.{Feed, Entry}
|
||||
import MobilizonWeb.Gettext
|
||||
|
||||
@version Mix.Project.config()[:version]
|
||||
def version(), do: @version
|
||||
|
||||
def actor(conn, %{"name" => name, "_format" => format}) when format in ["atom"] do
|
||||
name = String.replace_suffix(name, ".atom", "")
|
||||
|
||||
def actor(conn, %{"name" => name, "format" => "atom"}) do
|
||||
with {status, data} when status in [:ok, :commit] <-
|
||||
Cachex.fetch(:mobilizon, "actor_" <> format <> "_" <> name, &create_cache/1) do
|
||||
Cachex.fetch(:feed, "actor_" <> name) do
|
||||
conn
|
||||
|> put_resp_content_type("application/atom+xml")
|
||||
|> send_resp(200, data)
|
||||
else
|
||||
_err ->
|
||||
send_resp(conn, 404, "Not found")
|
||||
end
|
||||
end
|
||||
|
||||
@spec create_cache(String.t()) :: {:commit, String.t()} | {:ignore, any()}
|
||||
defp create_cache(key) do
|
||||
with ["actor", type, name] <- String.split(key, "_", parts: 3),
|
||||
{:ok, res} <- fetch_actor_event_feed(type, name) do
|
||||
{:commit, res}
|
||||
else
|
||||
err ->
|
||||
{:ignore, err}
|
||||
end
|
||||
end
|
||||
|
||||
@spec fetch_actor_event_feed(String.t(), String.t()) :: String.t()
|
||||
defp fetch_actor_event_feed(type, name) do
|
||||
with %Actor{} = actor <- Actors.get_local_actor_by_name(name),
|
||||
{:ok, events, _count} <- Events.get_public_events_for_actor(actor) do
|
||||
{:ok, build_actor_feed(actor, events, type)}
|
||||
else
|
||||
err ->
|
||||
{:error, err}
|
||||
end
|
||||
end
|
||||
|
||||
@spec build_actor_feed(Actor.t(), list(), String.t()) :: String.t()
|
||||
defp build_actor_feed(%Actor{} = actor, events, type) do
|
||||
display_name = Actor.display_name(actor)
|
||||
|
||||
# Title uses default instance language
|
||||
feed =
|
||||
Feed.new(
|
||||
actor.url <> ".rss",
|
||||
DateTime.utc_now(),
|
||||
gettext("%{actor}'s public events feed", actor: display_name)
|
||||
)
|
||||
|> Feed.author(display_name, uri: actor.url)
|
||||
|> Feed.link(actor.url <> "." <> type, rel: "self")
|
||||
|> Feed.link(actor.url, rel: "alternate")
|
||||
|> Feed.generator("Mobilizon", uri: "https://joinmobilizon.org", version: version())
|
||||
|> Feed.entries(Enum.map(events, &get_entry/1))
|
||||
|
||||
feed = if actor.avatar_url, do: Feed.icon(feed, actor.avatar_url), else: feed
|
||||
|
||||
feed =
|
||||
if actor.banner_url,
|
||||
do: Feed.logo(feed, actor.banner_url),
|
||||
else: feed
|
||||
|
||||
feed
|
||||
|> Feed.build()
|
||||
|> Atomex.generate_document()
|
||||
end
|
||||
|
||||
defp get_entry(%Event{} = event) do
|
||||
with {:ok, html, []} <- Earmark.as_html(event.description) do
|
||||
entry =
|
||||
Entry.new(event.url, event.inserted_at, event.title)
|
||||
|> Entry.link(event.url, rel: "alternate", type: "text/html")
|
||||
|> Entry.content({:cdata, html}, type: "html")
|
||||
|
||||
entry = if event.publish_at, do: Entry.published(entry, event.publish_at), else: entry
|
||||
|
||||
# Add tags
|
||||
entry =
|
||||
event.tags
|
||||
|> Enum.map(& &1.title)
|
||||
|> Enum.uniq()
|
||||
|> Enum.reduce(entry, fn tag, acc -> Entry.category(acc, tag) end)
|
||||
|
||||
Entry.build(entry)
|
||||
else
|
||||
{:error, _html, error_messages} ->
|
||||
require Logger
|
||||
Logger.error("Unable to produce HTML for Markdown", details: inspect(error_messages))
|
||||
conn
|
||||
|> put_resp_content_type("text/html")
|
||||
|> send_file(404, "priv/static/index.html")
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -22,9 +22,8 @@ defmodule MobilizonWeb.Router do
|
||||
plug(:accepts, ["activity-json", "html"])
|
||||
end
|
||||
|
||||
pipeline :activity_pub_rss do
|
||||
plug(TrailingFormatPlug)
|
||||
plug(:accepts, ["activity-json", "html", "atom"])
|
||||
pipeline :rss do
|
||||
plug(:accepts, ["atom", "html"])
|
||||
end
|
||||
|
||||
pipeline :browser do
|
||||
@@ -57,14 +56,15 @@ defmodule MobilizonWeb.Router do
|
||||
end
|
||||
|
||||
scope "/", MobilizonWeb do
|
||||
pipe_through(:activity_pub_rss)
|
||||
pipe_through(:rss)
|
||||
|
||||
get("/@:name", ActivityPubController, :actor)
|
||||
get("/@:name/feed/:format", FeedController, :actor)
|
||||
end
|
||||
|
||||
scope "/", MobilizonWeb do
|
||||
pipe_through(:activity_pub)
|
||||
|
||||
get("/@:name", ActivityPubController, :actor)
|
||||
get("/@:name/outbox", ActivityPubController, :outbox)
|
||||
get("/@:name/following", ActivityPubController, :following)
|
||||
get("/@:name/followers", ActivityPubController, :followers)
|
||||
|
||||
94
lib/service/feed.ex
Normal file
94
lib/service/feed.ex
Normal file
@@ -0,0 +1,94 @@
|
||||
defmodule Mobilizon.Service.Feed do
|
||||
@moduledoc """
|
||||
Serve Atom Syndication Feeds
|
||||
"""
|
||||
|
||||
alias Mobilizon.Actors
|
||||
alias Mobilizon.Actors.Actor
|
||||
alias Mobilizon.Events
|
||||
alias Mobilizon.Events.Event
|
||||
alias Atomex.{Feed, Entry}
|
||||
import MobilizonWeb.Gettext
|
||||
alias MobilizonWeb.Router.Helpers, as: Routes
|
||||
alias MobilizonWeb.Endpoint
|
||||
|
||||
@version Mix.Project.config()[:version]
|
||||
def version(), do: @version
|
||||
|
||||
@spec create_cache(String.t()) :: {:commit, String.t()} | {:ignore, any()}
|
||||
def create_cache("actor_" <> name) do
|
||||
with {:ok, res} <- fetch_actor_event_feed(name) do
|
||||
{:commit, res}
|
||||
else
|
||||
err ->
|
||||
{:ignore, err}
|
||||
end
|
||||
end
|
||||
|
||||
@spec fetch_actor_event_feed(String.t()) :: String.t()
|
||||
defp fetch_actor_event_feed(name) do
|
||||
with %Actor{} = actor <- Actors.get_local_actor_by_name(name),
|
||||
{:ok, events, _count} <- Events.get_public_events_for_actor(actor) do
|
||||
{:ok, build_actor_feed(actor, events)}
|
||||
else
|
||||
err ->
|
||||
{:error, err}
|
||||
end
|
||||
end
|
||||
|
||||
# Build an atom feed from actor and it's public events
|
||||
@spec build_actor_feed(Actor.t(), list()) :: String.t()
|
||||
defp build_actor_feed(%Actor{} = actor, events) do
|
||||
display_name = Actor.display_name(actor)
|
||||
self_url = Routes.feed_url(Endpoint, :actor, actor.preferred_username, "atom") |> URI.decode()
|
||||
|
||||
# Title uses default instance language
|
||||
feed =
|
||||
Feed.new(
|
||||
self_url,
|
||||
DateTime.utc_now(),
|
||||
gettext("%{actor}'s public events feed", actor: display_name)
|
||||
)
|
||||
|> Feed.author(display_name, uri: actor.url)
|
||||
|> Feed.link(self_url, rel: "self")
|
||||
|> Feed.link(actor.url, rel: "alternate")
|
||||
|> Feed.generator("Mobilizon", uri: "https://joinmobilizon.org", version: version())
|
||||
|> Feed.entries(Enum.map(events, &get_entry/1))
|
||||
|
||||
feed = if actor.avatar_url, do: Feed.icon(feed, actor.avatar_url), else: feed
|
||||
|
||||
feed =
|
||||
if actor.banner_url,
|
||||
do: Feed.logo(feed, actor.banner_url),
|
||||
else: feed
|
||||
|
||||
feed
|
||||
|> Feed.build()
|
||||
|> Atomex.generate_document()
|
||||
end
|
||||
|
||||
# Create an entry for the Atom feed
|
||||
@spec get_entry(Event.t()) :: any()
|
||||
defp get_entry(%Event{} = event) do
|
||||
with {:ok, html, []} <- Earmark.as_html(event.description) do
|
||||
entry =
|
||||
Entry.new(event.url, event.inserted_at, event.title)
|
||||
|> Entry.link(event.url, rel: "alternate", type: "text/html")
|
||||
|> Entry.content({:cdata, html}, type: "html")
|
||||
|
||||
entry = if event.publish_at, do: Entry.published(entry, event.publish_at), else: entry
|
||||
|
||||
# Add tags
|
||||
entry =
|
||||
event.tags
|
||||
|> Enum.uniq()
|
||||
|> Enum.reduce(entry, fn tag, acc -> Entry.category(acc, tag.slug, label: tag.title) end)
|
||||
|
||||
Entry.build(entry)
|
||||
else
|
||||
{:error, _html, error_messages} ->
|
||||
require Logger
|
||||
Logger.error("Unable to produce HTML for Markdown", details: inspect(error_messages))
|
||||
end
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user