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

View file

@ -19,6 +19,8 @@ defmodule Mobilizon.Service.Export.Feed do
require Logger
@item_limit 500
def version, do: Config.instance_version()
@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()}
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, build_instance_feed(events, posts)}
@ -88,9 +90,9 @@ defmodule Mobilizon.Service.Export.Feed do
|> Atomex.generate_document()
end
@spec fetch_actor_event_feed(String.t()) :: String.t()
defp fetch_actor_event_feed(name) do
case Common.fetch_actor_event_feed(name) do
@spec fetch_actor_event_feed(String.t(), integer()) :: String.t()
defp fetch_actor_event_feed(name, limit \\ @item_limit) do
case Common.fetch_actor_event_feed(name, limit) do
{:ok, actor, events, posts} ->
{:ok, build_actor_feed(actor, events, posts)}
@ -198,9 +200,9 @@ defmodule Mobilizon.Service.Export.Feed do
# Only events, not posts
@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} <-
Common.fetch_events_from_token(token) do
Common.fetch_events_from_token(token, limit) do
case type do
:user -> {:ok, build_user_feed(events, user, token)}
: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.Formatter.HTML
@item_limit 500
@doc """
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()}
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, %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
"""
@spec export_public_actor(String.t()) :: String.t()
def export_public_actor(name) do
case Common.fetch_actor_event_feed(name) do
def export_public_actor(name, limit \\ @item_limit) do
case Common.fetch_actor_event_feed(name, limit) do
{:ok, _actor, events, _posts} ->
{:ok, events_to_ics(events)}
@ -101,15 +103,15 @@ defmodule Mobilizon.Service.Export.ICalendar do
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
def export_private_actor(%Actor{} = actor, limit \\ @item_limit) do
with events <- Common.fetch_actor_private_events(actor, limit) 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
defp fetch_events_from_token(token, limit \\ @item_limit) do
with %{events: events} <- Common.fetch_events_from_token(token, limit) do
{:ok, events_to_ics(events)}
end
end

View file

@ -48,6 +48,18 @@ defmodule Mobilizon.Service.ICalendarTest do
refute ics =~ event2.title
assert ics =~ event3.title
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
describe "export an actor's events from a token" do