2019-03-06 16:07:42 +00:00
|
|
|
defmodule Mobilizon.Service.Export.Feed do
|
2019-03-01 11:57:22 +00:00
|
|
|
@moduledoc """
|
2019-09-22 14:26:23 +00:00
|
|
|
Serve Atom Syndication Feeds.
|
2019-03-01 11:57:22 +00:00
|
|
|
"""
|
|
|
|
|
2020-01-26 20:36:50 +00:00
|
|
|
import Mobilizon.Web.Gettext
|
2019-09-22 14:26:23 +00:00
|
|
|
|
|
|
|
alias Atomex.{Entry, Feed}
|
|
|
|
|
2019-03-01 11:57:22 +00:00
|
|
|
alias Mobilizon.Actors.Actor
|
2021-03-26 14:40:10 +00:00
|
|
|
alias Mobilizon.Config
|
|
|
|
alias Mobilizon.Events.Event
|
2021-01-19 10:24:21 +00:00
|
|
|
alias Mobilizon.Posts.Post
|
2021-03-26 14:40:10 +00:00
|
|
|
alias Mobilizon.Service.Export.Common
|
2019-09-22 14:26:23 +00:00
|
|
|
alias Mobilizon.Users.User
|
|
|
|
|
2020-12-15 16:17:42 +00:00
|
|
|
alias Mobilizon.Web.Endpoint
|
2020-01-26 20:36:50 +00:00
|
|
|
alias Mobilizon.Web.Router.Helpers, as: Routes
|
2019-09-22 14:26:23 +00:00
|
|
|
|
2019-03-08 11:25:06 +00:00
|
|
|
require Logger
|
2019-03-01 11:57:22 +00:00
|
|
|
|
2021-08-16 14:15:52 +00:00
|
|
|
@item_limit 500
|
|
|
|
|
2021-01-19 10:24:21 +00:00
|
|
|
def version, do: Config.instance_version()
|
2019-03-01 11:57:22 +00:00
|
|
|
|
|
|
|
@spec create_cache(String.t()) :: {:commit, String.t()} | {:ignore, any()}
|
|
|
|
def create_cache("actor_" <> name) do
|
2019-07-23 16:06:22 +00:00
|
|
|
case fetch_actor_event_feed(name) do
|
|
|
|
{:ok, res} ->
|
|
|
|
{:commit, res}
|
|
|
|
|
2019-03-01 11:57:22 +00:00
|
|
|
err ->
|
|
|
|
{:ignore, err}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-03-08 11:25:06 +00:00
|
|
|
@spec create_cache(String.t()) :: {:commit, String.t()} | {:ignore, any()}
|
|
|
|
def create_cache("token_" <> token) do
|
2019-07-23 16:06:22 +00:00
|
|
|
case fetch_events_from_token(token) do
|
|
|
|
{:ok, res} ->
|
|
|
|
{:commit, res}
|
|
|
|
|
2019-03-08 11:25:06 +00:00
|
|
|
err ->
|
|
|
|
{:ignore, err}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-03-26 14:40:10 +00:00
|
|
|
def create_cache("instance") do
|
|
|
|
case fetch_instance_feed() do
|
|
|
|
{:ok, res} ->
|
|
|
|
{:commit, res}
|
|
|
|
|
|
|
|
err ->
|
|
|
|
{:ignore, err}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
@spec fetch_instance_feed :: {:ok, String.t()}
|
|
|
|
defp fetch_instance_feed do
|
2021-08-16 14:15:52 +00:00
|
|
|
case Common.fetch_instance_public_content(@item_limit) do
|
2021-03-26 14:40:10 +00:00
|
|
|
{:ok, events, posts} ->
|
|
|
|
{:ok, build_instance_feed(events, posts)}
|
|
|
|
|
|
|
|
err ->
|
|
|
|
{:error, err}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# Build an atom feed from the whole instance and its public events and posts
|
|
|
|
@spec build_instance_feed(list(), list()) :: String.t()
|
|
|
|
defp build_instance_feed(events, posts) do
|
|
|
|
self_url = Endpoint.url()
|
|
|
|
|
|
|
|
title =
|
|
|
|
gettext("Public feed for %{instance}",
|
|
|
|
instance: Config.instance_name()
|
|
|
|
)
|
|
|
|
|
|
|
|
# Title uses default instance language
|
|
|
|
self_url
|
|
|
|
|> Feed.new(
|
|
|
|
DateTime.utc_now(),
|
|
|
|
title
|
|
|
|
)
|
|
|
|
|> Feed.link(self_url, rel: "self")
|
|
|
|
|> Feed.link(self_url, rel: "alternate")
|
|
|
|
|> Feed.generator(Config.instance_name(), uri: Endpoint.url(), version: version())
|
|
|
|
|> Feed.entries(Enum.map(events ++ posts, &get_entry/1))
|
|
|
|
|> Feed.build()
|
|
|
|
|> Atomex.generate_document()
|
|
|
|
end
|
|
|
|
|
2021-08-16 14:15:52 +00:00
|
|
|
@spec fetch_actor_event_feed(String.t(), integer()) :: String.t()
|
|
|
|
defp fetch_actor_event_feed(name, limit \\ @item_limit) do
|
|
|
|
case Common.fetch_actor_event_feed(name, limit) do
|
2021-03-26 14:40:10 +00:00
|
|
|
{:ok, actor, events, posts} ->
|
|
|
|
{:ok, build_actor_feed(actor, events, posts)}
|
|
|
|
|
2021-09-24 14:46:42 +00:00
|
|
|
{:error, err} ->
|
2019-03-01 11:57:22 +00:00
|
|
|
{:error, err}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-01-19 10:24:21 +00:00
|
|
|
# Build an atom feed from actor and its public events and posts
|
2021-09-24 14:46:42 +00:00
|
|
|
@spec build_actor_feed(Actor.t(), list(Event.t()), list(Post.t()), boolean()) :: String.t()
|
2021-01-19 10:24:21 +00:00
|
|
|
defp build_actor_feed(%Actor{} = actor, events, posts, public \\ true) do
|
2019-03-01 11:57:22 +00:00
|
|
|
display_name = Actor.display_name(actor)
|
2019-09-22 11:41:24 +00:00
|
|
|
|
|
|
|
self_url =
|
|
|
|
Endpoint
|
|
|
|
|> Routes.feed_url(:actor, actor.preferred_username, "atom")
|
|
|
|
|> URI.decode()
|
2019-03-01 11:57:22 +00:00
|
|
|
|
2019-03-08 11:25:06 +00:00
|
|
|
title =
|
|
|
|
if public,
|
2021-01-19 10:24:21 +00:00
|
|
|
do:
|
|
|
|
gettext("%{actor}'s public events feed on %{instance}",
|
|
|
|
actor: display_name,
|
|
|
|
instance: Config.instance_name()
|
|
|
|
),
|
|
|
|
else:
|
|
|
|
gettext("%{actor}'s private events feed on %{instance}",
|
|
|
|
actor: display_name,
|
|
|
|
instance: Config.instance_name()
|
|
|
|
)
|
2019-03-08 11:25:06 +00:00
|
|
|
|
2019-03-01 11:57:22 +00:00
|
|
|
# Title uses default instance language
|
|
|
|
feed =
|
2019-09-22 11:41:24 +00:00
|
|
|
self_url
|
|
|
|
|> Feed.new(
|
2019-03-01 11:57:22 +00:00
|
|
|
DateTime.utc_now(),
|
2021-01-19 10:24:21 +00:00
|
|
|
title
|
2019-03-01 11:57:22 +00:00
|
|
|
)
|
|
|
|
|> Feed.author(display_name, uri: actor.url)
|
|
|
|
|> Feed.link(self_url, rel: "self")
|
|
|
|
|> Feed.link(actor.url, rel: "alternate")
|
2021-01-19 10:24:21 +00:00
|
|
|
|> Feed.generator(Config.instance_name(), uri: Endpoint.url(), version: version())
|
|
|
|
|> Feed.entries(Enum.map(events ++ posts, &get_entry/1))
|
2019-03-01 11:57:22 +00:00
|
|
|
|
2019-05-22 12:12:11 +00:00
|
|
|
feed =
|
|
|
|
if actor.avatar do
|
2020-12-15 16:17:42 +00:00
|
|
|
feed |> Feed.icon(actor.avatar.url)
|
2019-05-22 12:12:11 +00:00
|
|
|
else
|
|
|
|
feed
|
|
|
|
end
|
2019-03-01 11:57:22 +00:00
|
|
|
|
|
|
|
feed =
|
2019-05-22 12:12:11 +00:00
|
|
|
if actor.banner do
|
2020-12-15 16:17:42 +00:00
|
|
|
feed |> Feed.logo(actor.banner.url)
|
2019-05-22 12:12:11 +00:00
|
|
|
else
|
|
|
|
feed
|
|
|
|
end
|
2019-03-01 11:57:22 +00:00
|
|
|
|
|
|
|
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
|
2019-03-21 19:23:42 +00:00
|
|
|
description = event.description || ""
|
|
|
|
|
2019-09-26 14:49:49 +00:00
|
|
|
entry =
|
2019-09-22 11:41:24 +00:00
|
|
|
event.url
|
|
|
|
|> Entry.new(event.publish_at || event.inserted_at, event.title)
|
2019-09-26 14:49:49 +00:00
|
|
|
|> Entry.link(event.url, rel: "alternate", type: "text/html")
|
|
|
|
|> Entry.content({:cdata, description}, type: "html")
|
|
|
|
|> Entry.published(event.publish_at || event.inserted_at)
|
|
|
|
|
|
|
|
# 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)
|
2019-03-01 11:57:22 +00:00
|
|
|
end
|
2019-03-08 11:25:06 +00:00
|
|
|
|
2021-01-19 10:24:21 +00:00
|
|
|
@spec get_entry(Post.t()) :: any()
|
|
|
|
defp get_entry(%Post{} = post) do
|
|
|
|
body = post.body || ""
|
|
|
|
|
|
|
|
entry =
|
|
|
|
post.url
|
|
|
|
|> Entry.new(post.publish_at || post.inserted_at, post.title)
|
|
|
|
|> Entry.link(post.url, rel: "alternate", type: "text/html")
|
|
|
|
|> Entry.content({:cdata, body}, type: "html")
|
|
|
|
|> Entry.published(post.publish_at || post.inserted_at)
|
|
|
|
|> Entry.author(post.author.name || post.author.preferred_username)
|
|
|
|
|
|
|
|
# Add tags
|
|
|
|
entry =
|
|
|
|
post.tags
|
|
|
|
|> Enum.uniq()
|
|
|
|
|> Enum.reduce(entry, fn tag, acc -> Entry.category(acc, tag.slug, label: tag.title) end)
|
|
|
|
|
|
|
|
Entry.build(entry)
|
|
|
|
end
|
|
|
|
|
|
|
|
# Only events, not posts
|
2021-09-24 14:46:42 +00:00
|
|
|
@spec fetch_events_from_token(String.t(), integer()) :: {:ok, String.t()} | {:error, atom()}
|
2021-08-16 14:15:52 +00:00
|
|
|
defp fetch_events_from_token(token, limit \\ @item_limit) do
|
2021-09-24 14:46:42 +00:00
|
|
|
case Common.fetch_events_from_token(token, limit) do
|
|
|
|
%{events: events, token: token, user: user, actor: actor, type: type} ->
|
|
|
|
case type do
|
|
|
|
:user -> {:ok, build_user_feed(events, user, token)}
|
|
|
|
:actor -> {:ok, build_actor_feed(actor, events, [], false)}
|
|
|
|
end
|
|
|
|
|
|
|
|
{:error, err} ->
|
|
|
|
{:error, err}
|
2019-03-08 11:25:06 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-10-15 19:18:03 +00:00
|
|
|
# Build an atom feed from actor and its public events
|
2021-09-24 14:46:42 +00:00
|
|
|
@spec build_user_feed(list(Event.t()), User.t(), String.t()) :: String.t()
|
2019-03-08 11:25:06 +00:00
|
|
|
defp build_user_feed(events, %User{email: email}, token) do
|
2019-09-22 11:41:24 +00:00
|
|
|
self_url = Endpoint |> Routes.feed_url(:going, token, "atom") |> URI.decode()
|
2019-03-08 11:25:06 +00:00
|
|
|
|
|
|
|
# Title uses default instance language
|
2019-09-22 11:41:24 +00:00
|
|
|
self_url
|
2021-01-19 10:24:21 +00:00
|
|
|
|> Feed.new(
|
|
|
|
DateTime.utc_now(),
|
|
|
|
gettext("Feed for %{email} on %{instance}",
|
|
|
|
email: email,
|
|
|
|
instance: Config.instance_name()
|
|
|
|
)
|
|
|
|
)
|
2019-03-08 11:25:06 +00:00
|
|
|
|> Feed.link(self_url, rel: "self")
|
2021-01-19 10:24:21 +00:00
|
|
|
|> Feed.generator(Config.instance_name(), uri: Endpoint.url(), version: version())
|
2019-03-08 11:25:06 +00:00
|
|
|
|> Feed.entries(Enum.map(events, &get_entry/1))
|
|
|
|
|> Feed.build()
|
|
|
|
|> Atomex.generate_document()
|
|
|
|
end
|
2019-03-01 11:57:22 +00:00
|
|
|
end
|