8000 feat(huddlz): personal sections + URL-driven filters on /huddlz by mwoods79 · Pull Request #163 · huddlz-hq/huddlz · GitHub
[go: up one dir, main page]

Skip to content
3 changes: 2 additions & 1 deletion lib/huddlz/communities.ex
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ defmodule Huddlz.Communities do
{:optional, :event_type},
{:optional, :search_latitude},
{:optional, :search_longitude},
{:optional, :distance_miles}
{:optional, :distance_miles},
{:optional, :relationship}
],
get?: false

Expand Down
6 changes: 6 additions & 0 deletions lib/huddlz/communities/huddl.ex
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,12 @@ defmodule Huddlz.Communities.Huddl do
constraints min: 5, max: 100
end

argument :relationship, :atom do
description "Restrict results to huddlz the actor hosts (creator) or is attending (RSVPed)."
allow_nil? true
constraints one_of: [:hosting, :attending]
end

pagination keyset?: true,
offset?: true,
countable: true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,13 @@ defmodule Huddlz.Communities.Huddl.Preparations.ApplySearchFilters do

@meters_per_mile 1609.344

def prepare(query, _opts, _context) do
def prepare(query, _opts, context) do
query
|> apply_text_filter()
|> apply_date_filter()
|> apply_event_type_filter()
|> apply_location_filter()
|> apply_relationship_filter(context)
end

defp apply_text_filter(query) do
Expand Down Expand Up @@ -89,4 +90,33 @@ defmodule Huddlz.Communities.Huddl.Preparations.ApplySearchFilters do
query
end
end

defp apply_relationship_filter(query, %{actor: nil}) do
# An anonymous actor cannot host or attend anything; force an empty result
# rather than ignoring the relationship filter (which would silently broaden
# the query and leak unrelated huddlz to API consumers).
case Ash.Query.get_argument(query, :relationship) do
relationship when relationship in [:hosting, :attending] ->
Ash.Query.filter(query, false)

_ ->
query
end
end

defp apply_relationship_filter(query, %{actor: actor}) do
case Ash.Query.get_argument(query, :relationship) do
:hosting ->
Ash.Query.filter(query, creator_id == ^actor.id)

:attending ->
Ash.Query.filter(
query,
exists(attendees, user_id == ^actor.id) and creator_id != ^actor.id
)

_ ->
query
end
end
end
Loading
Loading
0