2021-03-26 14:40:10 +00: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}
|
2021-09-24 14:46:42 +00:00
|
|
|
alias Mobilizon.Posts.Post
|
2021-03-26 14:40:10 +00:00
|
|
|
alias Mobilizon.Storage.Page
|
|
|
|
alias Mobilizon.Users.User
|
|
|
|
|
2021-09-24 14:46:42 +00:00
|
|
|
@spec fetch_actor_event_feed(String.t(), integer()) ::
|
|
|
|
{:ok, Actor.t(), [Event.t()], [Post.t()]}
|
|
|
|
| {:error, :actor_not_public | :actor_not_found}
|
2021-08-16 14:15:52 +00:00
|
|
|
def fetch_actor_event_feed(name, limit) do
|
2021-09-24 14:46:42 +00:00
|
|
|
case Actors.get_actor_by_name(name) do
|
|
|
|
%Actor{} = actor ->
|
|
|
|
if Actor.is_public_visibility?(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)
|
|
|
|
{:ok, actor, events, posts}
|
|
|
|
else
|
|
|
|
{:error, :actor_not_public}
|
|
|
|
end
|
|
|
|
|
|
|
|
nil ->
|
|
|
|
{:error, :actor_not_found}
|
2021-03-26 14:40:10 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-09-24 14:46:42 +00:00
|
|
|
@typep token_feed_data :: %{
|
|
|
|
type: :actor | :user,
|
|
|
|
actor: Actor.t() | nil,
|
|
|
|
user: User.t(),
|
|
|
|
events: [Event.t()],
|
|
|
|
token: String.t()
|
|
|
|
}
|
|
|
|
|
2021-03-26 14:40:10 +00:00
|
|
|
# Only events, not posts
|
2021-09-24 14:46:42 +00:00
|
|
|
@spec fetch_events_from_token(String.t(), integer()) ::
|
|
|
|
token_feed_data | {:error, :bad_token | :token_not_found}
|
2021-08-16 14:15:52 +00:00
|
|
|
def fetch_events_from_token(token, limit) do
|
2021-09-24 14:46:42 +00:00
|
|
|
case uuid_from_token(token) do
|
|
|
|
{:ok, uuid} ->
|
|
|
|
case Events.get_feed_token(uuid) do
|
|
|
|
nil ->
|
|
|
|
{:error, :token_not_found}
|
|
|
|
|
|
|
|
%FeedToken{actor: actor, user: %User{} = user} ->
|
|
|
|
produce_actor_feed(actor, user, token, limit)
|
|
|
|
end
|
|
|
|
|
|
|
|
{:error, :bad_token} ->
|
|
|
|
{:error, :bad_token}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
@spec uuid_from_token(String.t()) :: {:ok, String.t()} | {:error, :bad_token}
|
|
|
|
defp uuid_from_token(token) do
|
|
|
|
case ShortUUID.decode(token) do
|
|
|
|
{:ok, uuid} ->
|
|
|
|
case Ecto.UUID.cast(uuid) do
|
|
|
|
{:ok, _uuid} ->
|
|
|
|
{:ok, uuid}
|
|
|
|
|
|
|
|
:error ->
|
|
|
|
{:error, :bad_token}
|
|
|
|
end
|
|
|
|
|
|
|
|
{:error, _err} ->
|
|
|
|
{:error, :bad_token}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
@spec produce_actor_feed(Actor.t() | nil, User.t(), String.t(), integer()) :: token_feed_data
|
|
|
|
defp produce_actor_feed(%Actor{} = actor, %User{} = user, token, limit) do
|
|
|
|
%{
|
|
|
|
type: :actor,
|
|
|
|
actor: actor,
|
|
|
|
events: fetch_actor_private_events(actor, limit),
|
|
|
|
user: user,
|
|
|
|
token: token
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
defp produce_actor_feed(nil, %User{} = user, token, limit) do
|
|
|
|
with actors <- Users.get_actors_for_user(user),
|
|
|
|
events <-
|
|
|
|
actors
|
|
|
|
|> Enum.map(&fetch_actor_private_events(&1, limit))
|
|
|
|
|> Enum.concat() do
|
|
|
|
%{type: :user, events: events, user: user, token: token, actor: nil}
|
2021-03-26 14:40:10 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-08-16 14:15:52 +00:00
|
|
|
@spec fetch_instance_public_content(integer()) :: {:ok, list(Event.t()), list(Post.t())}
|
|
|
|
def fetch_instance_public_content(limit) do
|
2021-09-24 14:46:42 +00:00
|
|
|
%Page{elements: events} = Events.list_public_local_events(1, limit)
|
|
|
|
%Page{elements: posts} = Posts.list_public_local_posts(1, limit)
|
|
|
|
{:ok, events, posts}
|
2021-03-26 14:40:10 +00:00
|
|
|
end
|
|
|
|
|
2021-08-16 14:15:52 +00:00
|
|
|
@spec fetch_actor_private_events(Actor.t(), integer()) :: list(Event.t())
|
2021-10-05 13:29:06 +00:00
|
|
|
defp fetch_actor_private_events(%Actor{} = actor, limit) do
|
2021-08-16 14:15:52 +00:00
|
|
|
actor |> fetch_identity_participations(limit) |> participations_to_events()
|
2021-03-26 14:40:10 +00:00
|
|
|
end
|
|
|
|
|
2022-04-07 16:37:44 +00:00
|
|
|
@spec fetch_identity_participations(Actor.t(), integer()) :: Page.t(Participant.t())
|
2021-08-16 14:15:52 +00:00
|
|
|
defp fetch_identity_participations(%Actor{} = actor, limit) do
|
|
|
|
with %Page{} = page <- Events.list_event_participations_for_actor(actor, 1, limit) do
|
2021-03-26 14:40:10 +00: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
|