2019-03-06 17:07:42 +01:00
|
|
|
defmodule Mobilizon.Service.Export.Feed do
|
2019-03-01 12:57:22 +01:00
|
|
|
@moduledoc """
|
2019-09-22 16:26:23 +02:00
|
|
|
Serve Atom Syndication Feeds.
|
2019-03-01 12:57:22 +01:00
|
|
|
"""
|
|
|
|
|
2020-01-26 21:36:50 +01:00
|
|
|
import Mobilizon.Web.Gettext
|
2019-09-22 16:26:23 +02:00
|
|
|
|
|
|
|
alias Atomex.{Entry, Feed}
|
|
|
|
|
|
|
|
alias Mobilizon.{Actors, Events, Users}
|
2019-03-01 12:57:22 +01:00
|
|
|
alias Mobilizon.Actors.Actor
|
2019-03-08 12:25:06 +01:00
|
|
|
alias Mobilizon.Events.{Event, FeedToken}
|
2020-06-11 19:13:21 +02:00
|
|
|
alias Mobilizon.Storage.Page
|
2019-09-22 16:26:23 +02:00
|
|
|
alias Mobilizon.Users.User
|
|
|
|
|
2020-01-26 21:36:50 +01:00
|
|
|
alias Mobilizon.Web.{Endpoint, MediaProxy}
|
|
|
|
alias Mobilizon.Web.Router.Helpers, as: Routes
|
2019-09-22 16:26:23 +02:00
|
|
|
|
2019-03-08 12:25:06 +01:00
|
|
|
require Logger
|
2019-03-01 12:57:22 +01:00
|
|
|
|
|
|
|
@version Mix.Project.config()[:version]
|
2019-09-22 16:26:23 +02:00
|
|
|
def version, do: @version
|
2019-03-01 12:57:22 +01:00
|
|
|
|
|
|
|
@spec create_cache(String.t()) :: {:commit, String.t()} | {:ignore, any()}
|
|
|
|
def create_cache("actor_" <> name) do
|
2019-07-23 18:06:22 +02:00
|
|
|
case fetch_actor_event_feed(name) do
|
|
|
|
{:ok, res} ->
|
|
|
|
{:commit, res}
|
|
|
|
|
2019-03-01 12:57:22 +01:00
|
|
|
err ->
|
|
|
|
{:ignore, err}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-03-08 12:25:06 +01:00
|
|
|
@spec create_cache(String.t()) :: {:commit, String.t()} | {:ignore, any()}
|
|
|
|
def create_cache("token_" <> token) do
|
2019-07-23 18:06:22 +02:00
|
|
|
case fetch_events_from_token(token) do
|
|
|
|
{:ok, res} ->
|
|
|
|
{:commit, res}
|
|
|
|
|
2019-03-08 12:25:06 +01:00
|
|
|
err ->
|
|
|
|
{:ignore, err}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-03-01 12:57:22 +01: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-09-09 00:52:49 +02:00
|
|
|
{:visibility, true} <- {:visibility, Actor.is_public_visibility(actor)},
|
2019-09-16 02:07:44 +02:00
|
|
|
{:ok, events, _count} <- Events.list_public_events_for_actor(actor) do
|
2019-03-01 12:57:22 +01:00
|
|
|
{:ok, build_actor_feed(actor, events)}
|
|
|
|
else
|
|
|
|
err ->
|
|
|
|
{:error, err}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-10-15 21:18:03 +02:00
|
|
|
# Build an atom feed from actor and its public events
|
2019-03-08 12:25:06 +01:00
|
|
|
@spec build_actor_feed(Actor.t(), list(), boolean()) :: String.t()
|
|
|
|
defp build_actor_feed(%Actor{} = actor, events, public \\ true) do
|
2019-03-01 12:57:22 +01:00
|
|
|
display_name = Actor.display_name(actor)
|
2019-09-22 13:41:24 +02:00
|
|
|
|
|
|
|
self_url =
|
|
|
|
Endpoint
|
|
|
|
|> Routes.feed_url(:actor, actor.preferred_username, "atom")
|
|
|
|
|> URI.decode()
|
2019-03-01 12:57:22 +01:00
|
|
|
|
2019-03-08 12:25:06 +01:00
|
|
|
title =
|
|
|
|
if public,
|
|
|
|
do: "%{actor}'s public events feed on Mobilizon",
|
|
|
|
else: "%{actor}'s private events feed on Mobilizon"
|
|
|
|
|
2019-03-01 12:57:22 +01:00
|
|
|
# Title uses default instance language
|
|
|
|
feed =
|
2019-09-22 13:41:24 +02:00
|
|
|
self_url
|
|
|
|
|> Feed.new(
|
2019-03-01 12:57:22 +01:00
|
|
|
DateTime.utc_now(),
|
2020-01-26 21:36:50 +01:00
|
|
|
Gettext.gettext(Mobilizon.Web.Gettext, title, actor: display_name)
|
2019-03-01 12:57:22 +01: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 14:12:11 +02:00
|
|
|
feed =
|
|
|
|
if actor.avatar do
|
2019-05-28 10:51:02 +02:00
|
|
|
feed |> Feed.icon(actor.avatar.url |> MediaProxy.url())
|
2019-05-22 14:12:11 +02:00
|
|
|
else
|
|
|
|
feed
|
|
|
|
end
|
2019-03-01 12:57:22 +01:00
|
|
|
|
|
|
|
feed =
|
2019-05-22 14:12:11 +02:00
|
|
|
if actor.banner do
|
2019-05-28 10:51:02 +02:00
|
|
|
feed |> Feed.logo(actor.banner.url |> MediaProxy.url())
|
2019-05-22 14:12:11 +02:00
|
|
|
else
|
|
|
|
feed
|
|
|
|
end
|
2019-03-01 12:57:22 +01: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 20:23:42 +01:00
|
|
|
description = event.description || ""
|
|
|
|
|
2019-09-26 16:49:49 +02:00
|
|
|
entry =
|
2019-09-22 13:41:24 +02:00
|
|
|
event.url
|
|
|
|
|> Entry.new(event.publish_at || event.inserted_at, event.title)
|
2019-09-26 16:49:49 +02: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 12:57:22 +01:00
|
|
|
end
|
2019-03-08 12:25:06 +01:00
|
|
|
|
|
|
|
@spec fetch_events_from_token(String.t()) :: String.t()
|
|
|
|
defp fetch_events_from_token(token) do
|
2019-05-22 14:12:11 +02:00
|
|
|
with {:ok, _uuid} <- Ecto.UUID.cast(token),
|
|
|
|
%FeedToken{actor: actor, user: %User{} = user} <- Events.get_feed_token(token) do
|
2019-03-08 12:25:06 +01:00
|
|
|
case actor do
|
|
|
|
%Actor{} = actor ->
|
2019-09-26 16:38:58 +02:00
|
|
|
events = actor |> fetch_identity_participations() |> participations_to_events()
|
2019-03-08 12:25:06 +01:00
|
|
|
{:ok, build_actor_feed(actor, events, false)}
|
|
|
|
|
|
|
|
nil ->
|
|
|
|
with actors <- Users.get_actors_for_user(user),
|
|
|
|
events <-
|
|
|
|
actors
|
2019-09-26 16:38:58 +02:00
|
|
|
|> Enum.map(fn actor ->
|
|
|
|
actor
|
|
|
|
|> Events.list_event_participations_for_actor()
|
|
|
|
|> participations_to_events()
|
|
|
|
end)
|
2019-03-08 12:25:06 +01:00
|
|
|
|> Enum.concat() do
|
|
|
|
{:ok, build_user_feed(events, user, token)}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-09-26 16:38:58 +02:00
|
|
|
defp fetch_identity_participations(%Actor{} = actor) do
|
2020-06-11 19:13:21 +02:00
|
|
|
with %Page{} = page <- Events.list_event_participations_for_actor(actor) do
|
|
|
|
page
|
2019-03-08 12:25:06 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-06-11 19:13:21 +02:00
|
|
|
defp participations_to_events(%Page{elements: participations}) do
|
2019-09-26 16:38:58 +02:00
|
|
|
participations
|
|
|
|
|> Enum.map(& &1.event_id)
|
|
|
|
|> Enum.map(&Events.get_event_with_preload!/1)
|
|
|
|
end
|
|
|
|
|
2019-10-15 21:18:03 +02:00
|
|
|
# Build an atom feed from actor and its public events
|
2019-03-08 12:25:06 +01:00
|
|
|
@spec build_user_feed(list(), User.t(), String.t()) :: String.t()
|
|
|
|
defp build_user_feed(events, %User{email: email}, token) do
|
2019-09-22 13:41:24 +02:00
|
|
|
self_url = Endpoint |> Routes.feed_url(:going, token, "atom") |> URI.decode()
|
2019-03-08 12:25:06 +01:00
|
|
|
|
|
|
|
# Title uses default instance language
|
2019-09-22 13:41:24 +02:00
|
|
|
self_url
|
|
|
|
|> Feed.new(DateTime.utc_now(), gettext("Feed for %{email} on Mobilizon", email: email))
|
2019-03-08 12:25:06 +01:00
|
|
|
|> 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 12:57:22 +01:00
|
|
|
end
|