2019-03-06 16:07:42 +00:00
|
|
|
defmodule Mobilizon.Service.Export.Feed do
|
2019-03-01 11:57:22 +00:00
|
|
|
@moduledoc """
|
|
|
|
Serve Atom Syndication Feeds
|
|
|
|
"""
|
|
|
|
|
2019-03-08 11:25:06 +00:00
|
|
|
alias Mobilizon.Users.User
|
|
|
|
alias Mobilizon.Users
|
2019-03-01 11:57:22 +00:00
|
|
|
alias Mobilizon.Actors
|
|
|
|
alias Mobilizon.Actors.Actor
|
|
|
|
alias Mobilizon.Events
|
2019-03-08 11:25:06 +00:00
|
|
|
alias Mobilizon.Events.{Event, FeedToken}
|
2019-03-01 11:57:22 +00:00
|
|
|
alias Atomex.{Feed, Entry}
|
|
|
|
import MobilizonWeb.Gettext
|
|
|
|
alias MobilizonWeb.Router.Helpers, as: Routes
|
|
|
|
alias MobilizonWeb.Endpoint
|
2019-05-28 08:51:02 +00:00
|
|
|
alias MobilizonWeb.MediaProxy
|
2019-03-08 11:25:06 +00:00
|
|
|
require Logger
|
2019-03-01 11:57:22 +00:00
|
|
|
|
|
|
|
@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
|
|
|
|
|
2019-03-08 11:25:06 +00:00
|
|
|
@spec create_cache(String.t()) :: {:commit, String.t()} | {:ignore, any()}
|
|
|
|
def create_cache("token_" <> token) do
|
|
|
|
with {:ok, res} <- fetch_events_from_token(token) do
|
|
|
|
{:commit, res}
|
|
|
|
else
|
|
|
|
err ->
|
|
|
|
{:ignore, err}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-03-01 11:57:22 +00:00
|
|
|
@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),
|
2019-04-25 17:05:05 +00:00
|
|
|
{:visibility, true} <- {:visibility, Actor.public_visibility?(actor)},
|
2019-03-01 11:57:22 +00:00
|
|
|
{: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
|
2019-03-08 11:25:06 +00:00
|
|
|
@spec build_actor_feed(Actor.t(), list(), boolean()) :: String.t()
|
|
|
|
defp build_actor_feed(%Actor{} = actor, events, public \\ true) do
|
2019-03-01 11:57:22 +00:00
|
|
|
display_name = Actor.display_name(actor)
|
|
|
|
self_url = Routes.feed_url(Endpoint, :actor, actor.preferred_username, "atom") |> URI.decode()
|
|
|
|
|
2019-03-08 11:25:06 +00:00
|
|
|
title =
|
|
|
|
if public,
|
|
|
|
do: "%{actor}'s public events feed on Mobilizon",
|
|
|
|
else: "%{actor}'s private events feed on Mobilizon"
|
|
|
|
|
2019-03-01 11:57:22 +00:00
|
|
|
# Title uses default instance language
|
|
|
|
feed =
|
|
|
|
Feed.new(
|
|
|
|
self_url,
|
|
|
|
DateTime.utc_now(),
|
2019-03-08 11:25:06 +00:00
|
|
|
Gettext.gettext(MobilizonWeb.Gettext, title, actor: display_name)
|
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")
|
|
|
|
|> Feed.generator("Mobilizon", uri: "https://joinmobilizon.org", version: version())
|
|
|
|
|> Feed.entries(Enum.map(events, &get_entry/1))
|
|
|
|
|
2019-05-22 12:12:11 +00:00
|
|
|
feed =
|
|
|
|
if actor.avatar do
|
2019-05-28 08:51:02 +00:00
|
|
|
feed |> Feed.icon(actor.avatar.url |> MediaProxy.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
|
2019-05-28 08:51:02 +00:00
|
|
|
feed |> Feed.logo(actor.banner.url |> MediaProxy.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 || ""
|
|
|
|
|
|
|
|
with {:ok, html, []} <- Earmark.as_html(description) do
|
2019-03-01 11:57:22 +00:00
|
|
|
entry =
|
2019-03-01 17:30:46 +00:00
|
|
|
Entry.new(event.url, event.publish_at || event.inserted_at, event.title)
|
2019-03-01 11:57:22 +00:00
|
|
|
|> Entry.link(event.url, rel: "alternate", type: "text/html")
|
|
|
|
|> Entry.content({:cdata, html}, type: "html")
|
2019-03-01 17:30:46 +00:00
|
|
|
|> Entry.published(event.publish_at || event.inserted_at)
|
2019-03-01 11:57:22 +00:00
|
|
|
|
|
|
|
# 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} ->
|
|
|
|
Logger.error("Unable to produce HTML for Markdown", details: inspect(error_messages))
|
|
|
|
end
|
|
|
|
end
|
2019-03-08 11:25:06 +00:00
|
|
|
|
|
|
|
@spec fetch_events_from_token(String.t()) :: String.t()
|
|
|
|
defp fetch_events_from_token(token) do
|
2019-05-22 12:12:11 +00:00
|
|
|
with {:ok, _uuid} <- Ecto.UUID.cast(token),
|
|
|
|
%FeedToken{actor: actor, user: %User{} = user} <- Events.get_feed_token(token) do
|
2019-03-08 11:25:06 +00:00
|
|
|
case actor do
|
|
|
|
%Actor{} = actor ->
|
|
|
|
events = fetch_identity_going_to_events(actor)
|
|
|
|
{:ok, build_actor_feed(actor, events, false)}
|
|
|
|
|
|
|
|
nil ->
|
|
|
|
with actors <- Users.get_actors_for_user(user),
|
|
|
|
events <-
|
|
|
|
actors
|
|
|
|
|> Enum.map(&Events.list_event_participations_for_actor/1)
|
|
|
|
|> Enum.concat() do
|
|
|
|
{:ok, build_user_feed(events, user, token)}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
defp fetch_identity_going_to_events(%Actor{} = actor) do
|
|
|
|
with events <- Events.list_event_participations_for_actor(actor) do
|
|
|
|
events
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# Build an atom feed from actor and it's public events
|
|
|
|
@spec build_user_feed(list(), User.t(), String.t()) :: String.t()
|
|
|
|
defp build_user_feed(events, %User{email: email}, token) do
|
|
|
|
self_url = Routes.feed_url(Endpoint, :going, token, "atom") |> URI.decode()
|
|
|
|
|
|
|
|
# Title uses default instance language
|
|
|
|
Feed.new(
|
|
|
|
self_url,
|
|
|
|
DateTime.utc_now(),
|
|
|
|
gettext("Feed for %{email} on Mobilizon", email: email)
|
|
|
|
)
|
|
|
|
|> Feed.link(self_url, rel: "self")
|
|
|
|
|> Feed.generator("Mobilizon", uri: "https://joinmobilizon.org", version: version())
|
|
|
|
|> Feed.entries(Enum.map(events, &get_entry/1))
|
|
|
|
|> Feed.build()
|
|
|
|
|> Atomex.generate_document()
|
|
|
|
end
|
2019-03-01 11:57:22 +00:00
|
|
|
end
|