Merge branch 'instance-feeds' into 'master'

Introduce instance ICS & Atom feeds (disabled by default)

See merge request framasoft/mobilizon!873
This commit is contained in:
Thomas Citharel 2021-03-26 15:50:32 +00:00
commit 380d7c56a8
9 changed files with 319 additions and 185 deletions

View file

@ -2,4 +2,5 @@
5048AE33D6269B15E21CF28C6F545AB6 5048AE33D6269B15E21CF28C6F545AB6
752C0E897CA81ACD81F4BB215FA5F8E4 752C0E897CA81ACD81F4BB215FA5F8E4
23412CF16549E4E88366DC9DECF39071 23412CF16549E4E88366DC9DECF39071
81C1F600C5809C7029EE32DE4818CD7D

View file

@ -34,6 +34,7 @@ config :mobilizon, :instance,
unconfirmed_user_grace_period_hours: 48, unconfirmed_user_grace_period_hours: 48,
activity_expire_days: 365, activity_expire_days: 365,
activity_keep_number: 100, activity_keep_number: 100,
enable_instance_feeds: false,
email_from: "noreply@localhost", email_from: "noreply@localhost",
email_reply_to: "noreply@localhost" email_reply_to: "noreply@localhost"

View file

@ -10,7 +10,7 @@ defmodule Mobilizon.Posts do
import Ecto.Query import Ecto.Query
require Logger require Logger
@post_preloads [:author, :attributed_to, :picture, :media] @post_preloads [:author, :attributed_to, :picture, :media, :tags]
import EctoEnum import EctoEnum
@ -21,6 +21,14 @@ defmodule Mobilizon.Posts do
:private :private
]) ])
def list_public_local_posts(page \\ nil, limit \\ nil) do
Post
|> filter_public()
|> filter_local()
|> preload_post_associations()
|> Page.build_page(page, limit)
end
@spec list_posts_for_stream :: Enum.t() @spec list_posts_for_stream :: Enum.t()
def list_posts_for_stream do def list_posts_for_stream do
Post Post
@ -141,10 +149,20 @@ defmodule Mobilizon.Posts do
where(query, [p], p.visibility == ^:public and not p.draft) where(query, [p], p.visibility == ^:public and not p.draft)
end end
@spec filter_local(Ecto.Query.t()) :: Ecto.Query.t()
defp filter_local(query) do
where(query, [q], q.local == true)
end
defp do_get_posts_for_group(group_id) do defp do_get_posts_for_group(group_id) do
Post Post
|> where(attributed_to_id: ^group_id) |> where(attributed_to_id: ^group_id)
|> order_by(desc: :inserted_at) |> order_by(desc: :inserted_at)
|> preload([p], [:author, :attributed_to, :picture, :media, :tags]) |> preload_post_associations()
end
@spec preload_post_associations(Ecto.Query.t(), list()) :: Ecto.Query.t()
defp preload_post_associations(query, associations \\ @post_preloads) do
preload(query, ^associations)
end end
end end

View file

@ -0,0 +1,77 @@
defmodule Mobilizon.Service.Export.Common do
@moduledoc """
Common tools for exportation
"""
alias Mobilizon.{Actors, Events, Posts, Users}
alias Mobilizon.Actors.Actor
alias Mobilizon.Events.{Event, FeedToken}
alias Mobilizon.Storage.Page
alias Mobilizon.Users.User
@spec fetch_actor_event_feed(String.t()) :: String.t()
def fetch_actor_event_feed(name) do
with %Actor{} = actor <- Actors.get_local_actor_by_name(name),
{:visibility, true} <- {:visibility, Actor.is_public_visibility?(actor)},
%Page{elements: events} <- Events.list_public_events_for_actor(actor),
%Page{elements: posts} <- Posts.get_public_posts_for_group(actor) do
{:ok, actor, events, posts}
else
err ->
{:error, err}
end
end
# Only events, not posts
@spec fetch_events_from_token(String.t()) :: String.t()
def fetch_events_from_token(token) do
with {:ok, _uuid} <- Ecto.UUID.cast(token),
%FeedToken{actor: actor, user: %User{} = user} <- Events.get_feed_token(token) do
case actor do
%Actor{} = actor ->
%{
type: :actor,
actor: actor,
events: fetch_actor_private_events(actor),
user: user,
token: token
}
nil ->
with actors <- Users.get_actors_for_user(user),
events <-
actors
|> Enum.map(&fetch_actor_private_events/1)
|> Enum.concat() do
%{type: :user, events: events, user: user, token: token, actor: nil}
end
end
end
end
@spec fetch_instance_public_content :: {:ok, list(Event.t()), list(Post.t())}
def fetch_instance_public_content do
with %Page{elements: events} <- Events.list_public_local_events(),
%Page{elements: posts} <- Posts.list_public_local_posts() do
{:ok, events, posts}
end
end
@spec fetch_actor_private_events(Actor.t()) :: list(Event.t())
def fetch_actor_private_events(%Actor{} = actor) do
actor |> fetch_identity_participations() |> participations_to_events()
end
@spec fetch_identity_participations(Actor.t()) :: Page.t()
defp fetch_identity_participations(%Actor{} = actor) do
with %Page{} = page <- Events.list_event_participations_for_actor(actor) do
page
end
end
defp participations_to_events(%Page{elements: participations}) do
participations
|> Enum.map(& &1.event_id)
|> Enum.map(&Events.get_event_with_preload!/1)
end
end

View file

@ -7,11 +7,11 @@ defmodule Mobilizon.Service.Export.Feed do
alias Atomex.{Entry, Feed} alias Atomex.{Entry, Feed}
alias Mobilizon.{Actors, Config, Events, Posts, Users}
alias Mobilizon.Actors.Actor alias Mobilizon.Actors.Actor
alias Mobilizon.Events.{Event, FeedToken} alias Mobilizon.Config
alias Mobilizon.Events.Event
alias Mobilizon.Posts.Post alias Mobilizon.Posts.Post
alias Mobilizon.Storage.Page alias Mobilizon.Service.Export.Common
alias Mobilizon.Users.User alias Mobilizon.Users.User
alias Mobilizon.Web.Endpoint alias Mobilizon.Web.Endpoint
@ -43,14 +43,57 @@ defmodule Mobilizon.Service.Export.Feed do
end end
end end
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
case Common.fetch_instance_public_content() do
{: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
@spec fetch_actor_event_feed(String.t()) :: String.t() @spec fetch_actor_event_feed(String.t()) :: String.t()
defp fetch_actor_event_feed(name) do defp fetch_actor_event_feed(name) do
with %Actor{} = actor <- Actors.get_local_actor_by_name(name), case Common.fetch_actor_event_feed(name) do
{:visibility, true} <- {:visibility, Actor.is_public_visibility?(actor)}, {:ok, actor, events, posts} ->
%Page{elements: events} <- Events.list_public_events_for_actor(actor), {:ok, build_actor_feed(actor, events, posts)}
%Page{elements: posts} <- Posts.get_public_posts_for_group(actor) do
{:ok, build_actor_feed(actor, events, posts)}
else
err -> err ->
{:error, err} {:error, err}
end end
@ -156,41 +199,15 @@ defmodule Mobilizon.Service.Export.Feed do
# Only events, not posts # Only events, not posts
@spec fetch_events_from_token(String.t()) :: String.t() @spec fetch_events_from_token(String.t()) :: String.t()
defp fetch_events_from_token(token) do defp fetch_events_from_token(token) do
with {:ok, _uuid} <- Ecto.UUID.cast(token), with %{events: events, token: token, user: user, actor: actor, type: type} <-
%FeedToken{actor: actor, user: %User{} = user} <- Events.get_feed_token(token) do Common.fetch_events_from_token(token) do
case actor do case type do
%Actor{} = actor -> :user -> {:ok, build_user_feed(events, user, token)}
events = actor |> fetch_identity_participations() |> participations_to_events() :actor -> {:ok, build_actor_feed(actor, events, [], false)}
{:ok, build_actor_feed(actor, events, [], false)}
nil ->
with actors <- Users.get_actors_for_user(user),
events <-
actors
|> Enum.map(fn actor ->
actor
|> Events.list_event_participations_for_actor()
|> participations_to_events()
end)
|> Enum.concat() do
{:ok, build_user_feed(events, user, token)}
end
end end
end end
end end
defp fetch_identity_participations(%Actor{} = actor) do
with %Page{} = page <- Events.list_event_participations_for_actor(actor) do
page
end
end
defp participations_to_events(%Page{elements: participations}) do
participations
|> Enum.map(& &1.event_id)
|> Enum.map(&Events.get_event_with_preload!/1)
end
# Build an atom feed from actor and its public events # Build an atom feed from actor and its public events
@spec build_user_feed(list(), User.t(), String.t()) :: String.t() @spec build_user_feed(list(), User.t(), String.t()) :: String.t()
defp build_user_feed(events, %User{email: email}, token) do defp build_user_feed(events, %User{email: email}, token) do

View file

@ -3,83 +3,23 @@ defmodule Mobilizon.Service.Export.ICalendar do
Export an event to iCalendar format. Export an event to iCalendar format.
""" """
alias Mobilizon.{Actors, Config, Events, Users}
alias Mobilizon.Actors.Actor alias Mobilizon.Actors.Actor
alias Mobilizon.Addresses.Address alias Mobilizon.Addresses.Address
alias Mobilizon.Events.{Event, FeedToken} alias Mobilizon.{Config, Events}
alias Mobilizon.Events.Event
alias Mobilizon.Service.Export.Common
alias Mobilizon.Service.Formatter.HTML alias Mobilizon.Service.Formatter.HTML
alias Mobilizon.Storage.Page
alias Mobilizon.Users.User
@vendor "Mobilizon #{Config.instance_version()}" @vendor "Mobilizon #{Config.instance_version()}"
@doc """
Export a public event to iCalendar format.
The event must have a visibility of `:public` or `:unlisted`
"""
@spec export_public_event(Event.t()) :: {:ok, String.t()}
def export_public_event(%Event{visibility: visibility} = event)
when visibility in [:public, :unlisted] do
export_event(event)
end
@spec export_public_event(Event.t()) :: {:error, :event_not_public}
def export_public_event(%Event{}), do: {:error, :event_not_public}
@doc """
Export an event to iCalendar format
"""
def export_event(%Event{} = event) do
{:ok, %ICalendar{events: [do_export_event(event)]} |> ICalendar.to_ics(vendor: @vendor)}
end
@spec do_export_event(Event.t()) :: ICalendar.Event.t()
defp do_export_event(%Event{} = event) do
%ICalendar.Event{
summary: event.title,
dtstart: event.begins_on,
dtstamp: event.publish_at || DateTime.utc_now(),
dtend: event.ends_on,
description: HTML.strip_tags(event.description),
uid: event.uuid,
url: event.url,
geo: Address.coords(event.physical_address),
location: Address.representation(event.physical_address),
categories: event.tags |> Enum.map(& &1.title)
}
end
@doc """
Export a public actor's events to iCalendar format.
The actor must have a visibility of `:public` or `:unlisted`, as well as the events
"""
@spec export_public_actor(Actor.t()) :: String.t()
def export_public_actor(%Actor{} = actor) do
with {:visibility, true} <- {:visibility, Actor.is_public_visibility?(actor)},
%Page{elements: events} <-
Events.list_public_events_for_actor(actor) do
{:ok, %ICalendar{events: events |> Enum.map(&do_export_event/1)} |> ICalendar.to_ics()}
end
end
@spec export_private_actor(Actor.t()) :: String.t()
def export_private_actor(%Actor{} = actor) do
with events <-
actor |> Events.list_event_participations_for_actor() |> participations_to_events() do
{:ok, %ICalendar{events: events |> Enum.map(&do_export_event/1)} |> ICalendar.to_ics()}
end
end
@doc """ @doc """
Create cache for an actor, an event or an user token Create cache for an actor, an event or an user token
""" """
def create_cache("actor_" <> name) do def create_cache("actor_" <> name) do
with %Actor{} = actor <- Actors.get_local_actor_by_name(name), case export_public_actor(name) do
{:ok, res} <- export_public_actor(actor) do {:ok, res} ->
{:commit, res} {:commit, res}
else
err -> err ->
{:ignore, err} {:ignore, err}
end end
@ -105,33 +45,96 @@ defmodule Mobilizon.Service.Export.ICalendar do
end end
end end
@spec fetch_events_from_token(String.t()) :: String.t() def create_cache("instance") do
defp fetch_events_from_token(token) do case fetch_instance_feed() do
with %FeedToken{actor: actor, user: %User{} = user} <- Events.get_feed_token(token) do {:ok, res} ->
case actor do {:commit, res}
%Actor{} = actor ->
export_private_actor(actor)
nil -> err ->
with actors <- Users.get_actors_for_user(user), {:ignore, err}
events <-
actors
|> Enum.map(fn actor ->
actor
|> Events.list_event_participations_for_actor()
|> participations_to_events()
end)
|> Enum.concat() do
{:ok,
%ICalendar{events: events |> Enum.map(&do_export_event/1)} |> ICalendar.to_ics()}
end
end
end end
end end
defp participations_to_events(%Page{elements: participations}) do @spec fetch_instance_feed :: {:ok, String.t()}
participations defp fetch_instance_feed do
|> Enum.map(& &1.event_id) case Common.fetch_instance_public_content() do
|> Enum.map(&Events.get_event_with_preload!/1) {:ok, events, _posts} ->
{:ok, %ICalendar{events: events |> Enum.map(&do_export_event/1)} |> ICalendar.to_ics()}
err ->
{:error, err}
end
end
@doc """
Export an event to iCalendar format.
"""
@spec export_event(Event.t()) :: {:ok, String.t()}
def export_event(%Event{} = event), do: {:ok, events_to_ics([event])}
@doc """
Export a public event to iCalendar format.
The event must have a visibility of `:public` or `:unlisted`
"""
@spec export_public_event(Event.t()) :: {:ok, String.t()}
def export_public_event(%Event{visibility: visibility} = event)
when visibility in [:public, :unlisted] do
{:ok, events_to_ics([event])}
end
@spec export_public_event(Event.t()) :: {:error, :event_not_public}
def export_public_event(%Event{}), do: {:error, :event_not_public}
@doc """
Export a public actor's events to iCalendar format.
The actor must have a visibility of `:public` or `:unlisted`, as well as the events
"""
@spec export_public_actor(String.t()) :: String.t()
def export_public_actor(name) do
case Common.fetch_actor_event_feed(name) do
{:ok, _actor, events, _posts} ->
{:ok, events_to_ics(events)}
err ->
{:error, err}
end
end
@spec export_private_actor(Actor.t()) :: String.t()
def export_private_actor(%Actor{} = actor) do
with events <- Common.fetch_actor_private_events(actor) do
{:ok, events_to_ics(events)}
end
end
@spec fetch_events_from_token(String.t()) :: String.t()
defp fetch_events_from_token(token) do
with %{events: events} <- Common.fetch_events_from_token(token) do
{:ok, events_to_ics(events)}
end
end
@spec events_to_ics(list(Events.t())) :: String.t()
defp events_to_ics(events) do
%ICalendar{events: events |> Enum.map(&do_export_event/1)}
|> ICalendar.to_ics(vendor: @vendor)
end
@spec do_export_event(Event.t()) :: ICalendar.Event.t()
defp do_export_event(%Event{} = event) do
%ICalendar.Event{
summary: event.title,
dtstart: event.begins_on,
dtstamp: event.publish_at || DateTime.utc_now(),
dtend: event.ends_on,
description: HTML.strip_tags(event.description),
uid: event.uuid,
url: event.url,
geo: Address.coords(event.physical_address),
location: Address.representation(event.physical_address),
categories: event.tags |> Enum.map(& &1.title)
}
end end
end end

View file

@ -5,31 +5,20 @@ defmodule Mobilizon.Web.FeedController do
use Mobilizon.Web, :controller use Mobilizon.Web, :controller
plug(:put_layout, false) plug(:put_layout, false)
action_fallback(Mobilizon.Web.FallbackController) action_fallback(Mobilizon.Web.FallbackController)
alias Mobilizon.Config
def actor(conn, %{"name" => name, "format" => "atom"}) do @formats ["ics", "atom"]
case Cachex.fetch(:feed, "actor_" <> name) do
{status, data} when status in [:commit, :ok] ->
conn
|> put_resp_content_type("application/atom+xml")
|> put_resp_header("content-disposition", "attachment; filename=\"#{name}.atom\"")
|> send_resp(200, data)
_ -> def instance(conn, %{"format" => format}) when format in @formats do
{:error, :not_found} if Config.get([:instance, :enable_instance_feeds], false) do
return_data(conn, format, "instance", Config.instance_name())
else
send_resp(conn, 401, "Instance feeds are not enabled.")
end end
end end
def actor(conn, %{"name" => name, "format" => "ics"}) do def actor(conn, %{"format" => format, "name" => name}) when format in @formats do
case Cachex.fetch(:ics, "actor_" <> name) do return_data(conn, format, "actor_" <> name, name)
{status, data} when status in [:commit, :ok] ->
conn
|> put_resp_content_type("text/calendar")
|> put_resp_header("content-disposition", "attachment; filename=\"#{name}.ics\"")
|> send_resp(200, data)
_err ->
{:error, :not_found}
end
end end
def actor(_conn, _) do def actor(_conn, _) do
@ -37,49 +26,50 @@ defmodule Mobilizon.Web.FeedController do
end end
def event(conn, %{"uuid" => uuid, "format" => "ics"}) do def event(conn, %{"uuid" => uuid, "format" => "ics"}) do
case Cachex.fetch(:ics, "event_" <> uuid) do return_data(conn, "ics", "event_" <> uuid, "event.ics")
{status, data} when status in [:commit, :ok] ->
conn
|> put_resp_content_type("text/calendar")
|> put_resp_header("content-disposition", "attachment; filename=\"event.ics\"")
|> send_resp(200, data)
_ ->
{:error, :not_found}
end
end end
def event(_conn, _) do def event(_conn, _) do
{:error, :not_found} {:error, :not_found}
end end
def going(conn, %{"token" => token, "format" => "ics"}) do def going(conn, %{"token" => token, "format" => format}) when format in @formats do
case Cachex.fetch(:ics, "token_" <> token) do return_data(conn, format, "token_" <> token, "events.#{format}")
end
def going(_conn, _) do
{:error, :not_found}
end
defp return_data(conn, "atom", type, filename) do
case Cachex.fetch(:feed, type) do
{status, data} when status in [:commit, :ok] ->
conn
|> put_resp_content_type("application/atom+xml")
|> put_resp_header(
"content-disposition",
"attachment; filename=\"#{filename}.atom\""
)
|> send_resp(200, data)
_err ->
{:error, :not_found}
end
end
defp return_data(conn, "ics", type, filename) do
case Cachex.fetch(:ics, type) do
{status, data} when status in [:commit, :ok] -> {status, data} when status in [:commit, :ok] ->
conn conn
|> put_resp_content_type("text/calendar") |> put_resp_content_type("text/calendar")
|> put_resp_header("content-disposition", "attachment; filename=\"events.ics\"") |> put_resp_header(
"content-disposition",
"attachment; filename=\"#{filename}.ics\""
)
|> send_resp(200, data) |> send_resp(200, data)
_ -> _ ->
{:error, :not_found} {:error, :not_found}
end end
end end
def going(conn, %{"token" => token, "format" => "atom"}) do
case Cachex.fetch(:feed, "token_" <> token) do
{status, data} when status in [:commit, :ok] ->
conn
|> put_resp_content_type("application/atom+xml")
|> put_resp_header("content-disposition", "attachment; filename=\"events.atom\"")
|> send_resp(200, data)
{:ignore, _} ->
{:error, :not_found}
end
end
def going(_conn, _) do
{:error, :not_found}
end
end end

View file

@ -137,6 +137,7 @@ defmodule Mobilizon.Web.Router do
get("/@:name/feed/:format", FeedController, :actor) get("/@:name/feed/:format", FeedController, :actor)
get("/events/:uuid/export/:format", FeedController, :event) get("/events/:uuid/export/:format", FeedController, :event)
get("/events/going/:token/:format", FeedController, :going) get("/events/going/:token/:format", FeedController, :going)
get("/feed/instance/:format", FeedController, :instance)
end end
## MOBILIZON ## MOBILIZON

View file

@ -6,7 +6,7 @@ defmodule Mobilizon.Service.ICalendarTest do
alias ICalendar.Value alias ICalendar.Value
alias Mobilizon.Addresses.Address alias Mobilizon.Addresses.Address
alias Mobilizon.Events.Event alias Mobilizon.Events.{Event, FeedToken}
alias Mobilizon.Service.Export.ICalendar, as: ICalendarService alias Mobilizon.Service.Export.ICalendar, as: ICalendarService
describe "export an event to ics" do describe "export an event to ics" do
@ -36,4 +36,30 @@ defmodule Mobilizon.Service.ICalendarTest do
assert {:ok, ics} == ICalendarService.export_public_event(event) assert {:ok, ics} == ICalendarService.export_public_event(event)
end end
end end
describe "export the instance's public events" do
test "succeds" do
%Event{} = event = insert(:event, title: "I'm public")
%Event{} = event2 = insert(:event, visibility: :private, title: "I'm private")
%Event{} = event3 = insert(:event, title: "Another public")
{:commit, ics} = ICalendarService.create_cache("instance")
assert ics =~ event.title
refute ics =~ event2.title
assert ics =~ event3.title
end
end
describe "export an actor's events from a token" do
test "an actor feedtoken" do
user = insert(:user)
actor = insert(:actor, user: user)
%FeedToken{token: token} = insert(:feed_token, user: user, actor: actor)
event = insert(:event)
insert(:participant, event: event, actor: actor, role: :participant)
{:commit, ics} = ICalendarService.create_cache("token_#{token}")
assert ics =~ event.title
end
end
end end