2021-03-26 15:40:10 +01:00
|
|
|
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
|
|
|
|
|
2021-08-16 16:15:52 +02:00
|
|
|
@spec fetch_actor_event_feed(String.t(), integer()) :: String.t()
|
|
|
|
def fetch_actor_event_feed(name, limit) do
|
2021-03-29 18:23:03 +02:00
|
|
|
with %Actor{} = actor <- Actors.get_actor_by_name(name),
|
2021-03-26 15:40:10 +01:00
|
|
|
{:visibility, true} <- {:visibility, Actor.is_public_visibility?(actor)},
|
2021-08-16 16:15:52 +02:00
|
|
|
%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
|
2021-03-26 15:40:10 +01:00
|
|
|
{:ok, actor, events, posts}
|
|
|
|
else
|
|
|
|
err ->
|
|
|
|
{:error, err}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# Only events, not posts
|
2021-08-16 16:15:52 +02:00
|
|
|
@spec fetch_events_from_token(String.t(), integer()) :: String.t()
|
|
|
|
def fetch_events_from_token(token, limit) do
|
2021-03-26 19:01:55 +01:00
|
|
|
with {:ok, uuid} <- ShortUUID.decode(token),
|
|
|
|
{:ok, _uuid} <- Ecto.UUID.cast(uuid),
|
|
|
|
%FeedToken{actor: actor, user: %User{} = user} <- Events.get_feed_token(uuid) do
|
2021-03-26 15:40:10 +01:00
|
|
|
case actor do
|
|
|
|
%Actor{} = actor ->
|
|
|
|
%{
|
|
|
|
type: :actor,
|
|
|
|
actor: actor,
|
2021-08-16 16:15:52 +02:00
|
|
|
events: fetch_actor_private_events(actor, limit),
|
2021-03-26 15:40:10 +01:00
|
|
|
user: user,
|
|
|
|
token: token
|
|
|
|
}
|
|
|
|
|
|
|
|
nil ->
|
|
|
|
with actors <- Users.get_actors_for_user(user),
|
|
|
|
events <-
|
|
|
|
actors
|
2021-08-16 16:15:52 +02:00
|
|
|
|> Enum.map(&fetch_actor_private_events(&1, limit))
|
2021-03-26 15:40:10 +01:00
|
|
|
|> Enum.concat() do
|
|
|
|
%{type: :user, events: events, user: user, token: token, actor: nil}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-08-16 16:15:52 +02:00
|
|
|
@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
|
2021-03-26 15:40:10 +01:00
|
|
|
{:ok, events, posts}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-08-16 16:15:52 +02:00
|
|
|
@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()
|
2021-03-26 15:40:10 +01:00
|
|
|
end
|
|
|
|
|
2021-08-16 16:15:52 +02:00
|
|
|
@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
|
2021-03-26 15:40:10 +01:00
|
|
|
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
|