Refactoring of Events context
This commit is contained in:
parent
6372599493
commit
ac77a7d28a
|
@ -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
|
||||
|
|
|
@ -4,6 +4,7 @@ defmodule Mobilizon.EventsTest do
|
|||
import Mobilizon.Factory
|
||||
|
||||
alias Mobilizon.Events
|
||||
alias Mobilizon.Storage.Page
|
||||
|
||||
@event_valid_attrs %{
|
||||
begins_on: "2010-04-17 14:00:00Z",
|
||||
|
@ -47,29 +48,29 @@ defmodule Mobilizon.EventsTest do
|
|||
refute Ecto.assoc_loaded?(Events.get_event!(event.id).organizer_actor)
|
||||
end
|
||||
|
||||
test "get_event_full!/1 returns the event with given id", %{event: event} do
|
||||
assert Events.get_event_full!(event.id).organizer_actor.preferred_username ==
|
||||
test "get_event_with_preload!/1 returns the event with given id", %{event: event} do
|
||||
assert Events.get_event_with_preload!(event.id).organizer_actor.preferred_username ==
|
||||
event.organizer_actor.preferred_username
|
||||
|
||||
assert Events.get_event_full!(event.id).participants == []
|
||||
assert Events.get_event_with_preload!(event.id).participants == []
|
||||
end
|
||||
|
||||
test "find_and_count_events_by_name/1 returns events for a given name", %{
|
||||
test "build_events_by_name/1 returns events for a given name", %{
|
||||
event: %Event{title: title} = event
|
||||
} do
|
||||
assert title == hd(Events.find_and_count_events_by_name(event.title).elements).title
|
||||
assert title == hd(Events.build_events_by_name(event.title).elements).title
|
||||
|
||||
%Event{} = event2 = insert(:event, title: "Special event")
|
||||
|
||||
assert event2.title ==
|
||||
Events.find_and_count_events_by_name("Special").elements |> hd() |> Map.get(:title)
|
||||
Events.build_events_by_name("Special").elements |> hd() |> Map.get(:title)
|
||||
|
||||
assert event2.title ==
|
||||
Events.find_and_count_events_by_name(" Special ").elements
|
||||
Events.build_events_by_name(" Special ").elements
|
||||
|> hd()
|
||||
|> Map.get(:title)
|
||||
|
||||
assert %{elements: [], total: 0} == Events.find_and_count_events_by_name("")
|
||||
assert %Page{elements: [], total: 0} == Events.build_events_by_name("")
|
||||
end
|
||||
|
||||
test "find_close_events/3 returns events in the area" do
|
||||
|
@ -127,19 +128,15 @@ defmodule Mobilizon.EventsTest do
|
|||
assert_raise Ecto.NoResultsError, fn -> Events.get_event!(event.id) end
|
||||
end
|
||||
|
||||
test "change_event/1 returns a event changeset", %{event: event} do
|
||||
assert %Ecto.Changeset{} = Events.change_event(event)
|
||||
end
|
||||
|
||||
test "get_public_events_for_actor/1", %{actor: actor, event: event} do
|
||||
assert {:ok, [event_found], 1} = Events.get_public_events_for_actor(actor)
|
||||
test "list_public_events_for_actor/1", %{actor: actor, event: event} do
|
||||
assert {:ok, [event_found], 1} = Events.list_public_events_for_actor(actor)
|
||||
assert event_found.title == event.title
|
||||
end
|
||||
|
||||
test "get_public_events_for_actor/3", %{actor: actor, event: event} do
|
||||
test "list_public_events_for_actor/3", %{actor: actor, event: event} do
|
||||
event1 = insert(:event, organizer_actor: actor)
|
||||
|
||||
case Events.get_public_events_for_actor(actor, 1, 10) do
|
||||
case Events.list_public_events_for_actor(actor, 1, 10) do
|
||||
{:ok, events_found, 2} ->
|
||||
event_ids = MapSet.new(events_found |> Enum.map(& &1.id))
|
||||
assert event_ids == MapSet.new([event.id, event1.id])
|
||||
|
@ -149,10 +146,10 @@ defmodule Mobilizon.EventsTest do
|
|||
end
|
||||
end
|
||||
|
||||
test "get_public_events_for_actor/3 with limited results", %{actor: actor, event: event} do
|
||||
test "list_public_events_for_actor/3 with limited results", %{actor: actor, event: event} do
|
||||
event1 = insert(:event, organizer_actor: actor)
|
||||
|
||||
case Events.get_public_events_for_actor(actor, 1, 1) do
|
||||
case Events.list_public_events_for_actor(actor, 1, 1) do
|
||||
{:ok, [%Event{id: event_found_id}], 2} ->
|
||||
assert event_found_id in [event.id, event1.id]
|
||||
|
||||
|
@ -229,11 +226,6 @@ defmodule Mobilizon.EventsTest do
|
|||
assert {:ok, %Tag{}} = Events.delete_tag(tag)
|
||||
assert_raise Ecto.NoResultsError, fn -> Events.get_tag!(tag.id) end
|
||||
end
|
||||
|
||||
test "change_tag/1 returns a tag changeset" do
|
||||
tag = insert(:tag)
|
||||
assert %Ecto.Changeset{} = Events.change_tag(tag)
|
||||
end
|
||||
end
|
||||
|
||||
describe "tags_relations" do
|
||||
|
@ -272,7 +264,7 @@ defmodule Mobilizon.EventsTest do
|
|||
assert {:ok, %TagRelation{}} = Events.delete_tag_relation(tag_relation)
|
||||
end
|
||||
|
||||
test "tag_neighbors/2 return the connected tags for a given tag", %{
|
||||
test "list_tag_neighbors/2 return the connected tags for a given tag", %{
|
||||
tag1: %Tag{} = tag1,
|
||||
tag2: %Tag{} = tag2
|
||||
} do
|
||||
|
@ -307,7 +299,7 @@ defmodule Mobilizon.EventsTest do
|
|||
}} = Events.create_tag_relation(%{tag_id: tag1.id, link_id: tag1.id})
|
||||
|
||||
# The order is preserved, since tag4 has one more relation than tag2
|
||||
assert [tag4, tag2] == Events.tag_neighbors(tag1)
|
||||
assert [tag4, tag2] == Events.list_tag_neighbors(tag1)
|
||||
end
|
||||
end
|
||||
|
||||
|
@ -383,10 +375,6 @@ defmodule Mobilizon.EventsTest do
|
|||
test "delete_participant/1 deletes the participant", %{participant: participant} do
|
||||
assert {:ok, %Participant{}} = Events.delete_participant(participant)
|
||||
end
|
||||
|
||||
test "change_participant/1 returns a participant changeset", %{participant: participant} do
|
||||
assert %Ecto.Changeset{} = Events.change_participant(participant)
|
||||
end
|
||||
end
|
||||
|
||||
describe "sessions" do
|
||||
|
@ -481,11 +469,6 @@ defmodule Mobilizon.EventsTest do
|
|||
assert {:ok, %Session{}} = Events.delete_session(session)
|
||||
assert_raise Ecto.NoResultsError, fn -> Events.get_session!(session.id) end
|
||||
end
|
||||
|
||||
test "change_session/1 returns a session changeset" do
|
||||
session = insert(:session)
|
||||
assert %Ecto.Changeset{} = Events.change_session(session)
|
||||
end
|
||||
end
|
||||
|
||||
describe "tracks" do
|
||||
|
@ -548,11 +531,6 @@ defmodule Mobilizon.EventsTest do
|
|||
assert {:ok, %Track{}} = Events.delete_track(track)
|
||||
assert_raise Ecto.NoResultsError, fn -> Events.get_track!(track.id) end
|
||||
end
|
||||
|
||||
test "change_track/1 returns a track changeset" do
|
||||
track = insert(:track)
|
||||
assert %Ecto.Changeset{} = Events.change_track(track)
|
||||
end
|
||||
end
|
||||
|
||||
describe "comments" do
|
||||
|
@ -616,10 +594,5 @@ defmodule Mobilizon.EventsTest do
|
|||
assert {:ok, %Comment{}} = Events.delete_comment(comment)
|
||||
assert_raise Ecto.NoResultsError, fn -> Events.get_comment!(comment.id) end
|
||||
end
|
||||
|
||||
test "change_comment/1 returns a comment changeset" do
|
||||
comment = insert(:comment)
|
||||
assert %Ecto.Changeset{} = Events.change_comment(comment)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -113,7 +113,7 @@ defmodule Mobilizon.Service.ActivityPub.ActivityPubTest do
|
|||
# TODO: The delete activity it relayed and fetched once again (and then not found /o\)
|
||||
test "it creates a delete activity and deletes the original event" do
|
||||
event = insert(:event)
|
||||
event = Events.get_event_full_by_url!(event.url)
|
||||
event = Events.get_public_event_by_url_with_preload!(event.url)
|
||||
{:ok, delete, _} = ActivityPub.delete(event)
|
||||
|
||||
assert delete.data["type"] == "Delete"
|
||||
|
@ -125,7 +125,7 @@ defmodule Mobilizon.Service.ActivityPub.ActivityPubTest do
|
|||
|
||||
test "it creates a delete activity and deletes the original comment" do
|
||||
comment = insert(:comment)
|
||||
comment = Events.get_comment_full_from_url!(comment.url)
|
||||
comment = Events.get_comment_from_url_with_preload!(comment.url)
|
||||
{:ok, delete, _} = ActivityPub.delete(comment)
|
||||
|
||||
assert delete.data["type"] == "Delete"
|
||||
|
|
|
@ -46,13 +46,13 @@ defmodule MobilizonWeb.API.SearchTest do
|
|||
|
||||
test "search events" do
|
||||
with_mock Events,
|
||||
find_and_count_events_by_name: fn "toto", 1, 10 ->
|
||||
build_events_by_name: fn "toto", 1, 10 ->
|
||||
%Page{total: 1, elements: [%Event{title: "super toto event"}]}
|
||||
end do
|
||||
assert {:ok, %{total: 1, elements: [%Event{title: "super toto event"}]}} =
|
||||
Search.search_events("toto", 1, 10)
|
||||
|
||||
assert_called(Events.find_and_count_events_by_name("toto", 1, 10))
|
||||
assert_called(Events.build_events_by_name("toto", 1, 10))
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -197,7 +197,9 @@ defmodule MobilizonWeb.Resolvers.ReportResolverTest do
|
|||
assert json_response(res, 200)["data"]["reports"]
|
||||
|> Enum.map(fn report -> Map.get(report, "id") end)
|
||||
|> Enum.sort() ==
|
||||
Enum.map([report_1_id, report_2_id, report_3_id], &to_string/1)
|
||||
[report_1_id, report_2_id, report_3_id]
|
||||
|> Enum.map(&to_string/1)
|
||||
|> Enum.sort()
|
||||
|
||||
query = """
|
||||
{
|
||||
|
|
|
@ -39,9 +39,7 @@ defmodule MobilizonWeb.Resolvers.TagResolverTest do
|
|||
|> Enum.map(fn tag -> tag["slug"] end)
|
||||
|> MapSet.new() ==
|
||||
[tag2, tag3]
|
||||
|> Enum.map(fn
|
||||
tag -> tag.slug
|
||||
end)
|
||||
|> Enum.map(fn tag -> tag.slug end)
|
||||
|> MapSet.new()
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Reference in a new issue