Add backend and endpoints for Feed Tokens
Closes #19 #86 #87 Signed-off-by: Thomas Citharel <tcit@tcit.fr>
This commit is contained in:
@@ -3,14 +3,17 @@ defmodule Mobilizon.Service.Export.Feed do
|
||||
Serve Atom Syndication Feeds
|
||||
"""
|
||||
|
||||
alias Mobilizon.Users.User
|
||||
alias Mobilizon.Users
|
||||
alias Mobilizon.Actors
|
||||
alias Mobilizon.Actors.Actor
|
||||
alias Mobilizon.Events
|
||||
alias Mobilizon.Events.Event
|
||||
alias Mobilizon.Events.{Event, FeedToken}
|
||||
alias Atomex.{Feed, Entry}
|
||||
import MobilizonWeb.Gettext
|
||||
alias MobilizonWeb.Router.Helpers, as: Routes
|
||||
alias MobilizonWeb.Endpoint
|
||||
require Logger
|
||||
|
||||
@version Mix.Project.config()[:version]
|
||||
def version(), do: @version
|
||||
@@ -25,6 +28,16 @@ defmodule Mobilizon.Service.Export.Feed do
|
||||
end
|
||||
end
|
||||
|
||||
@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
|
||||
|
||||
@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),
|
||||
@@ -37,17 +50,22 @@ defmodule Mobilizon.Service.Export.Feed do
|
||||
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
|
||||
@spec build_actor_feed(Actor.t(), list(), boolean()) :: String.t()
|
||||
defp build_actor_feed(%Actor{} = actor, events, public \\ true) do
|
||||
display_name = Actor.display_name(actor)
|
||||
self_url = Routes.feed_url(Endpoint, :actor, actor.preferred_username, "atom") |> URI.decode()
|
||||
|
||||
title =
|
||||
if public,
|
||||
do: "%{actor}'s public events feed on Mobilizon",
|
||||
else: "%{actor}'s private events feed on Mobilizon"
|
||||
|
||||
# Title uses default instance language
|
||||
feed =
|
||||
Feed.new(
|
||||
self_url,
|
||||
DateTime.utc_now(),
|
||||
gettext("%{actor}'s public events feed", actor: display_name)
|
||||
Gettext.gettext(MobilizonWeb.Gettext, title, actor: display_name)
|
||||
)
|
||||
|> Feed.author(display_name, uri: actor.url)
|
||||
|> Feed.link(self_url, rel: "self")
|
||||
@@ -86,8 +104,51 @@ defmodule Mobilizon.Service.Export.Feed do
|
||||
Entry.build(entry)
|
||||
else
|
||||
{:error, _html, error_messages} ->
|
||||
require Logger
|
||||
Logger.error("Unable to produce HTML for Markdown", details: inspect(error_messages))
|
||||
end
|
||||
end
|
||||
|
||||
@spec fetch_events_from_token(String.t()) :: String.t()
|
||||
defp fetch_events_from_token(token) do
|
||||
with %FeedToken{actor: actor, user: %User{} = user} <- Events.get_feed_token(token) do
|
||||
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
|
||||
end
|
||||
|
||||
@@ -3,10 +3,12 @@ defmodule Mobilizon.Service.Export.ICalendar do
|
||||
Export an event to iCalendar format
|
||||
"""
|
||||
|
||||
alias Mobilizon.Events.Event
|
||||
alias Mobilizon.Events.{Event, FeedToken}
|
||||
alias Mobilizon.Events
|
||||
alias Mobilizon.Actors.Actor
|
||||
alias Mobilizon.Actors
|
||||
alias Mobilizon.Users.User
|
||||
alias Mobilizon.Users
|
||||
|
||||
@doc """
|
||||
Export a public event to iCalendar format.
|
||||
@@ -47,6 +49,13 @@ defmodule Mobilizon.Service.Export.ICalendar do
|
||||
end
|
||||
end
|
||||
|
||||
@spec export_private_actor(Actor.t()) :: String.t()
|
||||
def export_private_actor(%Actor{} = actor) do
|
||||
with events <- Events.list_event_participations_for_actor(actor) do
|
||||
{:ok, %ICalendar{events: events |> Enum.map(&do_export_event/1)} |> ICalendar.to_ics()}
|
||||
end
|
||||
end
|
||||
|
||||
@doc """
|
||||
Create cache for an actor
|
||||
"""
|
||||
@@ -72,4 +81,36 @@ defmodule Mobilizon.Service.Export.ICalendar do
|
||||
{:ignore, err}
|
||||
end
|
||||
end
|
||||
|
||||
@doc """
|
||||
Create cache for an actor
|
||||
"""
|
||||
def create_cache("token_" <> token) do
|
||||
with {:ok, res} <- fetch_events_from_token(token) do
|
||||
{:commit, res}
|
||||
else
|
||||
err ->
|
||||
{:ignore, err}
|
||||
end
|
||||
end
|
||||
|
||||
@spec fetch_events_from_token(String.t()) :: String.t()
|
||||
defp fetch_events_from_token(token) do
|
||||
with %FeedToken{actor: actor, user: %User{} = user} <- Events.get_feed_token(token) do
|
||||
case actor do
|
||||
%Actor{} = actor ->
|
||||
export_private_actor(actor)
|
||||
|
||||
nil ->
|
||||
with actors <- Users.get_actors_for_user(user),
|
||||
events <-
|
||||
actors
|
||||
|> Enum.map(&Events.list_event_participations_for_actor/1)
|
||||
|> Enum.concat() do
|
||||
{:ok,
|
||||
%ICalendar{events: events |> Enum.map(&do_export_event/1)} |> ICalendar.to_ics()}
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user