Refactoring of Events context
This commit is contained in:
@@ -65,7 +65,10 @@ defmodule Mobilizon do
|
||||
{Cachex, :start_link,
|
||||
[
|
||||
name,
|
||||
Keyword.merge(cachex_options(limit, default, interval), fallback_options(fallback))
|
||||
Keyword.merge(
|
||||
cachex_options(limit, default, interval),
|
||||
fallback_options(fallback)
|
||||
)
|
||||
]}
|
||||
}
|
||||
end
|
||||
|
||||
@@ -45,6 +45,7 @@ defmodule Mobilizon.Actors do
|
||||
:creator
|
||||
])
|
||||
|
||||
@public_visibility [:public, :unlisted]
|
||||
@administrator_roles [:creator, :administrator]
|
||||
|
||||
@doc false
|
||||
@@ -836,7 +837,7 @@ defmodule Mobilizon.Actors do
|
||||
from(
|
||||
a in Actor,
|
||||
where: a.type == ^:Group,
|
||||
where: a.visibility in ^[:public, :unlisted]
|
||||
where: a.visibility in ^@public_visibility
|
||||
)
|
||||
end
|
||||
|
||||
|
||||
@@ -3,11 +3,19 @@ defmodule Mobilizon.Events.Activity do
|
||||
Represents an activity.
|
||||
"""
|
||||
|
||||
@type t :: %__MODULE__{
|
||||
data: String.t(),
|
||||
local: boolean,
|
||||
actor: Actor.t(),
|
||||
recipients: [String.t()]
|
||||
# notifications: [???]
|
||||
}
|
||||
|
||||
defstruct [
|
||||
:data,
|
||||
:local,
|
||||
:actor,
|
||||
:recipients,
|
||||
:notifications
|
||||
:recipients
|
||||
# :notifications
|
||||
]
|
||||
end
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -73,7 +73,7 @@ defmodule MobilizonWeb.API.Reports do
|
||||
|
||||
defp get_report_comments(%Actor{id: actor_id}, comment_ids) do
|
||||
{:get_report_comments,
|
||||
Events.get_all_comments_by_actor_and_ids(actor_id, comment_ids) |> Enum.map(& &1.url)}
|
||||
Events.list_comments_by_actor_and_ids(actor_id, comment_ids) |> Enum.map(& &1.url)}
|
||||
end
|
||||
|
||||
defp get_report_comments(_, _), do: {:get_report_comments, nil}
|
||||
|
||||
@@ -68,7 +68,7 @@ defmodule MobilizonWeb.API.Search do
|
||||
end
|
||||
|
||||
true ->
|
||||
{:ok, Events.find_and_count_events_by_name(search, page, limit)}
|
||||
{:ok, Events.build_events_by_name(search, page, limit)}
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
@@ -17,12 +17,12 @@ defmodule MobilizonWeb.PageController do
|
||||
end
|
||||
|
||||
def event(conn, %{"uuid" => uuid}) do
|
||||
{status, event} = Events.get_cached_event_full_by_uuid(uuid)
|
||||
{status, event} = Events.get_cached_public_event_by_uuid_with_preload(uuid)
|
||||
render_or_error(conn, &ok_status_and_is_visible?/2, status, :event, event)
|
||||
end
|
||||
|
||||
def comment(conn, %{"uuid" => uuid}) do
|
||||
{status, comment} = Events.get_cached_comment_full_by_uuid(uuid)
|
||||
{status, comment} = Events.get_cached_comment_by_uuid_with_preload(uuid)
|
||||
render_or_error(conn, &ok_status_and_is_visible?/2, status, :comment, comment)
|
||||
end
|
||||
|
||||
|
||||
@@ -25,7 +25,7 @@ defmodule MobilizonWeb.Resolvers.Event do
|
||||
end
|
||||
|
||||
def find_event(_parent, %{uuid: uuid}, _resolution) do
|
||||
case Mobilizon.Events.get_event_full_by_uuid(uuid) do
|
||||
case Mobilizon.Events.get_public_event_by_uuid_with_preload(uuid) do
|
||||
nil ->
|
||||
{:error, "Event with UUID #{uuid} not found"}
|
||||
|
||||
@@ -58,14 +58,14 @@ defmodule MobilizonWeb.Resolvers.Event do
|
||||
) do
|
||||
# We get the organizer's next public event
|
||||
events =
|
||||
[Events.get_actor_upcoming_public_event(organizer_actor, uuid)]
|
||||
[Events.get_upcoming_public_event_for_actor(organizer_actor, uuid)]
|
||||
|> Enum.filter(&is_map/1)
|
||||
|
||||
# We find similar events with the same tags
|
||||
# uniq_by : It's possible event_from_same_actor is inside events_from_tags
|
||||
events =
|
||||
events
|
||||
|> Enum.concat(Events.find_similar_events_by_common_tags(tags, @number_of_related_events))
|
||||
|> Enum.concat(Events.list_events_by_tags(tags, @number_of_related_events))
|
||||
|> uniq_events()
|
||||
|
||||
# TODO: We should use tag_relations to find more appropriate events
|
||||
@@ -104,7 +104,7 @@ defmodule MobilizonWeb.Resolvers.Event do
|
||||
) do
|
||||
with {:is_owned, %Actor{} = actor} <- User.owns_actor(user, actor_id),
|
||||
{:has_event, {:ok, %Event{} = event}} <-
|
||||
{:has_event, Mobilizon.Events.get_event_full(event_id)},
|
||||
{:has_event, Mobilizon.Events.get_event_with_preload(event_id)},
|
||||
{:error, :participant_not_found} <- Mobilizon.Events.get_participant(event_id, actor_id),
|
||||
{:ok, _activity, participant} <- MobilizonWeb.API.Participations.join(event, actor),
|
||||
participant <-
|
||||
@@ -141,7 +141,7 @@ defmodule MobilizonWeb.Resolvers.Event do
|
||||
) do
|
||||
with {:is_owned, %Actor{} = actor} <- User.owns_actor(user, actor_id),
|
||||
{:has_event, {:ok, %Event{} = event}} <-
|
||||
{:has_event, Mobilizon.Events.get_event_full(event_id)},
|
||||
{:has_event, Mobilizon.Events.get_event_with_preload(event_id)},
|
||||
{:ok, _activity, _participant} <- MobilizonWeb.API.Participations.leave(event, actor) do
|
||||
{:ok, %{event: %{id: event_id}, actor: %{id: actor_id}}}
|
||||
else
|
||||
@@ -200,7 +200,7 @@ defmodule MobilizonWeb.Resolvers.Event do
|
||||
) do
|
||||
# See https://github.com/absinthe-graphql/absinthe/issues/490
|
||||
with args <- Map.put(args, :options, args[:options] || %{}),
|
||||
{:ok, %Event{} = event} <- Mobilizon.Events.get_event_full(event_id),
|
||||
{:ok, %Event{} = event} <- Mobilizon.Events.get_event_with_preload(event_id),
|
||||
{:is_owned, %Actor{} = organizer_actor} <-
|
||||
User.owns_actor(user, event.organizer_actor_id),
|
||||
{:ok, args} <- save_attached_picture(args),
|
||||
|
||||
@@ -33,7 +33,7 @@ defmodule MobilizonWeb.Resolvers.Tag do
|
||||
# """
|
||||
# def get_related_tags(_parent, %{tag_id: tag_id}, _resolution) do
|
||||
# with %Tag{} = tag <- Mobilizon.Events.get_tag!(tag_id),
|
||||
# tags <- Mobilizon.Events.tag_neighbors(tag) do
|
||||
# tags <- Mobilizon.Events.list_tag_neighbors(tag) do
|
||||
# {:ok, tags}
|
||||
# end
|
||||
# end
|
||||
@@ -42,7 +42,7 @@ defmodule MobilizonWeb.Resolvers.Tag do
|
||||
Retrieve the list of related tags for a parent tag
|
||||
"""
|
||||
def get_related_tags(%Tag{} = tag, _args, _resolution) do
|
||||
with tags <- Mobilizon.Events.tag_neighbors(tag) do
|
||||
with tags <- Mobilizon.Events.list_tag_neighbors(tag) do
|
||||
{:ok, tags}
|
||||
end
|
||||
end
|
||||
|
||||
@@ -84,10 +84,10 @@ defmodule Mobilizon.Service.ActivityPub do
|
||||
{:ok, _activity, %{url: object_url} = _object} <- Transmogrifier.handle_incoming(params) do
|
||||
case data["type"] do
|
||||
"Event" ->
|
||||
{:ok, Events.get_event_full_by_url!(object_url)}
|
||||
{:ok, Events.get_public_event_by_url_with_preload!(object_url)}
|
||||
|
||||
"Note" ->
|
||||
{:ok, Events.get_comment_full_from_url!(object_url)}
|
||||
{:ok, Events.get_comment_from_url_with_preload!(object_url)}
|
||||
|
||||
"Actor" ->
|
||||
{:ok, Actors.get_actor_by_url!(object_url, true)}
|
||||
@@ -97,10 +97,10 @@ defmodule Mobilizon.Service.ActivityPub do
|
||||
end
|
||||
else
|
||||
{:existing_event, %Event{url: event_url}} ->
|
||||
{:ok, Events.get_event_full_by_url!(event_url)}
|
||||
{:ok, Events.get_public_event_by_url_with_preload!(event_url)}
|
||||
|
||||
{:existing_comment, %Comment{url: comment_url}} ->
|
||||
{:ok, Events.get_comment_full_from_url!(comment_url)}
|
||||
{:ok, Events.get_comment_from_url_with_preload!(comment_url)}
|
||||
|
||||
{:existing_actor, {:ok, %Actor{url: actor_url}}} ->
|
||||
{:ok, Actors.get_actor_by_url!(actor_url, true)}
|
||||
@@ -682,8 +682,8 @@ defmodule Mobilizon.Service.ActivityPub do
|
||||
"""
|
||||
@spec fetch_public_activities_for_actor(Actor.t(), integer(), integer()) :: map()
|
||||
def fetch_public_activities_for_actor(%Actor{} = actor, page \\ 1, limit \\ 10) do
|
||||
{:ok, events, total_events} = Events.get_public_events_for_actor(actor, page, limit)
|
||||
{:ok, comments, total_comments} = Events.get_public_comments_for_actor(actor, page, limit)
|
||||
{:ok, events, total_events} = Events.list_public_events_for_actor(actor, page, limit)
|
||||
{:ok, comments, total_comments} = Events.list_public_comments_for_actor(actor, page, limit)
|
||||
|
||||
event_activities = Enum.map(events, &event_to_activity/1)
|
||||
|
||||
|
||||
@@ -45,7 +45,7 @@ defmodule Mobilizon.Service.Export.Feed do
|
||||
defp fetch_actor_event_feed(name) do
|
||||
with %Actor{} = actor <- Actors.get_local_actor_by_name(name),
|
||||
{:visibility, true} <- {:visibility, Actor.is_public_visibility(actor)},
|
||||
{:ok, events, _count} <- Events.get_public_events_for_actor(actor) do
|
||||
{:ok, events, _count} <- Events.list_public_events_for_actor(actor) do
|
||||
{:ok, build_actor_feed(actor, events)}
|
||||
else
|
||||
err ->
|
||||
|
||||
@@ -45,7 +45,7 @@ defmodule Mobilizon.Service.Export.ICalendar do
|
||||
@spec export_public_actor(Actor.t()) :: String.t()
|
||||
def export_public_actor(%Actor{} = actor) do
|
||||
with true <- Actor.is_public_visibility(actor),
|
||||
{:ok, events, _} <- Events.get_public_events_for_actor(actor) do
|
||||
{:ok, events, _} <- Events.list_public_events_for_actor(actor) do
|
||||
{:ok, %ICalendar{events: events |> Enum.map(&do_export_event/1)} |> ICalendar.to_ics()}
|
||||
end
|
||||
end
|
||||
@@ -74,7 +74,7 @@ defmodule Mobilizon.Service.Export.ICalendar do
|
||||
Create cache for an actor
|
||||
"""
|
||||
def create_cache("event_" <> uuid) do
|
||||
with %Event{} = event <- Events.get_event_full_by_uuid(uuid),
|
||||
with %Event{} = event <- Events.get_public_event_by_uuid_with_preload(uuid),
|
||||
{:ok, res} <- export_public_event(event) do
|
||||
{:commit, res}
|
||||
else
|
||||
|
||||
Reference in New Issue
Block a user