Merge branch 'increase-feeds-limits' into 'master'

Add sane limits to feeds

Closes #722

See merge request framasoft/mobilizon!1036
This commit is contained in:
Thomas Citharel 2021-08-16 14:56:48 +00:00
commit a21f287c98
4 changed files with 47 additions and 31 deletions

View file

@ -9,12 +9,12 @@ defmodule Mobilizon.Service.Export.Common do
alias Mobilizon.Storage.Page alias Mobilizon.Storage.Page
alias Mobilizon.Users.User alias Mobilizon.Users.User
@spec fetch_actor_event_feed(String.t()) :: String.t() @spec fetch_actor_event_feed(String.t(), integer()) :: String.t()
def fetch_actor_event_feed(name) do def fetch_actor_event_feed(name, limit) do
with %Actor{} = actor <- Actors.get_actor_by_name(name), with %Actor{} = actor <- Actors.get_actor_by_name(name),
{:visibility, true} <- {:visibility, Actor.is_public_visibility?(actor)}, {:visibility, true} <- {:visibility, Actor.is_public_visibility?(actor)},
%Page{elements: events} <- Events.list_public_events_for_actor(actor), %Page{elements: events} <- Events.list_public_events_for_actor(actor, 1, limit),
%Page{elements: posts} <- Posts.get_public_posts_for_group(actor) do %Page{elements: posts} <- Posts.get_public_posts_for_group(actor, 1, limit) do
{:ok, actor, events, posts} {:ok, actor, events, posts}
else else
err -> err ->
@ -23,8 +23,8 @@ defmodule Mobilizon.Service.Export.Common do
end end
# Only events, not posts # Only events, not posts
@spec fetch_events_from_token(String.t()) :: String.t() @spec fetch_events_from_token(String.t(), integer()) :: String.t()
def fetch_events_from_token(token) do def fetch_events_from_token(token, limit) do
with {:ok, uuid} <- ShortUUID.decode(token), with {:ok, uuid} <- ShortUUID.decode(token),
{:ok, _uuid} <- Ecto.UUID.cast(uuid), {:ok, _uuid} <- Ecto.UUID.cast(uuid),
%FeedToken{actor: actor, user: %User{} = user} <- Events.get_feed_token(uuid) do %FeedToken{actor: actor, user: %User{} = user} <- Events.get_feed_token(uuid) do
@ -33,7 +33,7 @@ defmodule Mobilizon.Service.Export.Common do
%{ %{
type: :actor, type: :actor,
actor: actor, actor: actor,
events: fetch_actor_private_events(actor), events: fetch_actor_private_events(actor, limit),
user: user, user: user,
token: token token: token
} }
@ -42,7 +42,7 @@ defmodule Mobilizon.Service.Export.Common do
with actors <- Users.get_actors_for_user(user), with actors <- Users.get_actors_for_user(user),
events <- events <-
actors actors
|> Enum.map(&fetch_actor_private_events/1) |> Enum.map(&fetch_actor_private_events(&1, limit))
|> Enum.concat() do |> Enum.concat() do
%{type: :user, events: events, user: user, token: token, actor: nil} %{type: :user, events: events, user: user, token: token, actor: nil}
end end
@ -50,22 +50,22 @@ defmodule Mobilizon.Service.Export.Common do
end end
end end
@spec fetch_instance_public_content :: {:ok, list(Event.t()), list(Post.t())} @spec fetch_instance_public_content(integer()) :: {:ok, list(Event.t()), list(Post.t())}
def fetch_instance_public_content do def fetch_instance_public_content(limit) do
with %Page{elements: events} <- Events.list_public_local_events(), with %Page{elements: events} <- Events.list_public_local_events(1, limit),
%Page{elements: posts} <- Posts.list_public_local_posts() do %Page{elements: posts} <- Posts.list_public_local_posts(1, limit) do
{:ok, events, posts} {:ok, events, posts}
end end
end end
@spec fetch_actor_private_events(Actor.t()) :: list(Event.t()) @spec fetch_actor_private_events(Actor.t(), integer()) :: list(Event.t())
def fetch_actor_private_events(%Actor{} = actor) do def fetch_actor_private_events(%Actor{} = actor, limit) do
actor |> fetch_identity_participations() |> participations_to_events() actor |> fetch_identity_participations(limit) |> participations_to_events()
end end
@spec fetch_identity_participations(Actor.t()) :: Page.t() @spec fetch_identity_participations(Actor.t(), integer()) :: Page.t()
defp fetch_identity_participations(%Actor{} = actor) do defp fetch_identity_participations(%Actor{} = actor, limit) do
with %Page{} = page <- Events.list_event_participations_for_actor(actor) do with %Page{} = page <- Events.list_event_participations_for_actor(actor, 1, limit) do
page page
end end
end end

View file

@ -19,6 +19,8 @@ defmodule Mobilizon.Service.Export.Feed do
require Logger require Logger
@item_limit 500
def version, do: Config.instance_version() def version, do: Config.instance_version()
@spec create_cache(String.t()) :: {:commit, String.t()} | {:ignore, any()} @spec create_cache(String.t()) :: {:commit, String.t()} | {:ignore, any()}
@ -55,7 +57,7 @@ defmodule Mobilizon.Service.Export.Feed do
@spec fetch_instance_feed :: {:ok, String.t()} @spec fetch_instance_feed :: {:ok, String.t()}
defp fetch_instance_feed do defp fetch_instance_feed do
case Common.fetch_instance_public_content() do case Common.fetch_instance_public_content(@item_limit) do
{:ok, events, posts} -> {:ok, events, posts} ->
{:ok, build_instance_feed(events, posts)} {:ok, build_instance_feed(events, posts)}
@ -88,9 +90,9 @@ defmodule Mobilizon.Service.Export.Feed do
|> Atomex.generate_document() |> Atomex.generate_document()
end end
@spec fetch_actor_event_feed(String.t()) :: String.t() @spec fetch_actor_event_feed(String.t(), integer()) :: String.t()
defp fetch_actor_event_feed(name) do defp fetch_actor_event_feed(name, limit \\ @item_limit) do
case Common.fetch_actor_event_feed(name) do case Common.fetch_actor_event_feed(name, limit) do
{:ok, actor, events, posts} -> {:ok, actor, events, posts} ->
{:ok, build_actor_feed(actor, events, posts)} {:ok, build_actor_feed(actor, events, posts)}
@ -198,9 +200,9 @@ 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, limit \\ @item_limit) do
with %{events: events, token: token, user: user, actor: actor, type: type} <- with %{events: events, token: token, user: user, actor: actor, type: type} <-
Common.fetch_events_from_token(token) do Common.fetch_events_from_token(token, limit) do
case type do case type do
:user -> {:ok, build_user_feed(events, user, token)} :user -> {:ok, build_user_feed(events, user, token)}
:actor -> {:ok, build_actor_feed(actor, events, [], false)} :actor -> {:ok, build_actor_feed(actor, events, [], false)}

View file

@ -10,6 +10,8 @@ defmodule Mobilizon.Service.Export.ICalendar do
alias Mobilizon.Service.Export.Common alias Mobilizon.Service.Export.Common
alias Mobilizon.Service.Formatter.HTML alias Mobilizon.Service.Formatter.HTML
@item_limit 500
@doc """ @doc """
Create cache for an actor, an event or an user token Create cache for an actor, an event or an user token
""" """
@ -55,7 +57,7 @@ defmodule Mobilizon.Service.Export.ICalendar do
@spec fetch_instance_feed :: {:ok, String.t()} @spec fetch_instance_feed :: {:ok, String.t()}
defp fetch_instance_feed do defp fetch_instance_feed do
case Common.fetch_instance_public_content() do case Common.fetch_instance_public_content(@item_limit) do
{:ok, events, _posts} -> {:ok, events, _posts} ->
{:ok, %ICalendar{events: events |> Enum.map(&do_export_event/1)} |> ICalendar.to_ics()} {:ok, %ICalendar{events: events |> Enum.map(&do_export_event/1)} |> ICalendar.to_ics()}
@ -90,8 +92,8 @@ defmodule Mobilizon.Service.Export.ICalendar do
The actor must have a visibility of `:public` or `:unlisted`, as well as the events The actor must have a visibility of `:public` or `:unlisted`, as well as the events
""" """
@spec export_public_actor(String.t()) :: String.t() @spec export_public_actor(String.t()) :: String.t()
def export_public_actor(name) do def export_public_actor(name, limit \\ @item_limit) do
case Common.fetch_actor_event_feed(name) do case Common.fetch_actor_event_feed(name, limit) do
{:ok, _actor, events, _posts} -> {:ok, _actor, events, _posts} ->
{:ok, events_to_ics(events)} {:ok, events_to_ics(events)}
@ -101,15 +103,15 @@ defmodule Mobilizon.Service.Export.ICalendar do
end end
@spec export_private_actor(Actor.t()) :: String.t() @spec export_private_actor(Actor.t()) :: String.t()
def export_private_actor(%Actor{} = actor) do def export_private_actor(%Actor{} = actor, limit \\ @item_limit) do
with events <- Common.fetch_actor_private_events(actor) do with events <- Common.fetch_actor_private_events(actor, limit) do
{:ok, events_to_ics(events)} {:ok, events_to_ics(events)}
end end
end end
@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, limit \\ @item_limit) do
with %{events: events} <- Common.fetch_events_from_token(token) do with %{events: events} <- Common.fetch_events_from_token(token, limit) do
{:ok, events_to_ics(events)} {:ok, events_to_ics(events)}
end end
end end

View file

@ -48,6 +48,18 @@ defmodule Mobilizon.Service.ICalendarTest do
refute ics =~ event2.title refute ics =~ event2.title
assert ics =~ event3.title assert ics =~ event3.title
end end
test "with 50 events" do
Enum.each(0..50, fn i ->
%Event{} = insert(:event, title: "Event #{i}")
end)
{:commit, ics} = ICalendarService.create_cache("instance")
Enum.each(0..50, fn i ->
assert ics =~ "Event #{i}"
end)
end
end end
describe "export an actor's events from a token" do describe "export an actor's events from a token" do