Skip to content

Commit

Permalink
wip - change prob not needed or soon to be outdated
Browse files Browse the repository at this point in the history
  • Loading branch information
ivanminutillo committed Jan 10, 2025
1 parent bf84229 commit 83a30b7
Showing 1 changed file with 49 additions and 31 deletions.
80 changes: 49 additions & 31 deletions lib/feed_loader.ex
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ defmodule Bonfire.Social.FeedLoader do
## Examples
iex> %{edges: _, page_info: %Paginator.PageInfo{}} = Bonfire.Social.FeedActivities.feed(:explore)
iex> %{edges: _, page_info: %Paginator.PageInfo{}} = Bonfire.Social.FeedActivities.feed(%{feed_name: :explore})
> Bonfire.Social.FeedActivities.feed("feed123", [])
Expand Down Expand Up @@ -235,13 +235,31 @@ defmodule Bonfire.Social.FeedLoader do
end
end

# def feed_filtered(other, filters, opts) do
# e = l("Not a recognised feed to query, return explore feed (with any provided filters)")
# debug(other, e)
# # raise e
# query_extras(filters, opts)
# |> paginate_and_boundarise_feed(filters, opts)
# |> prepare_feed(filters, opts)
# end

def feed_filtered(other, filters, opts) do
e = l("Not a recognised feed to query, return explore feed (with any provided filters)")
debug(other, e)
# raise e
query_extras(filters, opts)
|> paginate_and_boundarise_feed(filters, opts)
|> prepare_feed(filters, opts)
case other do
:custom ->
# For custom feeds, directly use the filters without looking up presets
query_extras(filters, opts)
|> paginate_and_boundarise_feed(filters, opts)
|> prepare_feed(filters, opts)

_ ->
e = l("Not a recognised feed to query, return explore feed (with any provided filters)")
debug(other, e)
# raise e
query_extras(filters, opts)
|> paginate_and_boundarise_feed(filters, opts)
|> prepare_feed(filters, opts)
end
end

@doc """
Expand All @@ -255,7 +273,7 @@ defmodule Bonfire.Social.FeedLoader do
iex> query = Ecto.Query.from(f in Bonfire.Data.Social.FeedPublish)
iex> %{edges: _, page_info: %{}} = feed_paginated([], base_query: query)
"""
def feed_paginated(filters \\ %{}, opts \\ []) do
opts = to_options(opts)
Expand Down Expand Up @@ -660,7 +678,7 @@ defmodule Bonfire.Social.FeedLoader do
fetcher_user_id = "1ACT1V1TYPVBREM0TESFETCHER"

cond do
# TODO: make local and remote filters instead
# TODO: make local and remote filters instead
:local in feed_ids or local_feed_id in feed_ids ->
debug("local feed")

Expand Down Expand Up @@ -927,7 +945,7 @@ defmodule Bonfire.Social.FeedLoader do
# # |> debug("as atoms")
# # |> maybe_filter(query, ...)

# true ->
# true ->
warn(filters, "no supported filters defined")
query
# end
Expand Down Expand Up @@ -994,7 +1012,7 @@ defmodule Bonfire.Social.FeedLoader do
(
dump(
Enum.map(feed, fn fi ->
# e(fi, :activity, :object, nil) ||
# e(fi, :activity, :object, nil) ||
e(fi, :activity, :object, :post_content, nil) ||
e(fi, :activity, nil) || fi
end),
Expand Down Expand Up @@ -1142,30 +1160,30 @@ defmodule Bonfire.Social.FeedLoader do
# 1: Retrieve a preset feed without parameters
iex> {:ok, %{feed_name: :local, exclude_activity_types: [:like]}} = preset_feed_filters(:local, [])
# 1: Retrieve a preset feed without parameters
iex> {:ok, %{feed_name: :local, exclude_activity_types: [:like]}} =preset_feed_filters(:local, [])
# 2: Retrieve a preset feed with parameters
iex> {:ok, %{subjects: "alice"}} = preset_feed_filters(:user_activities, [by: "alice"])
# 3: Feed not found (error case)
iex> preset_feed_filters("unknown_feed", [])
{:error, :not_found}
# 4: Preset feed with parameterized filters
iex> {:ok, %{activity_types: :like, subjects: %{id: "alice"}}} = preset_feed_filters(:liked_by_me, current_user: %{id: "alice"})
# 5: Feed with `current_user_required` should check for current user
iex> {:ok, %{feed_name: :messages}} = preset_feed_filters(:messages, current_user: %{id: "alice"})
# 6: Feed with `current_user_required` and no current user
iex> preset_feed_filters(:messages, [])
** (Bonfire.Fail.Auth) You need to log in first.
** (Bonfire.Fail.Auth) You need to log in first.
# 7: Custom feed with additional parameters
iex> {:ok, %{activity_types: :follow, objects: "alice"}} = preset_feed_filters(:user_followers, [by: "alice"])
"""
@spec preset_feed_filters(String.t(), map()) :: {:ok, map()} | {:error, atom()}
def preset_feed_filters(name, opts \\ []) do
Expand All @@ -1189,9 +1207,9 @@ defmodule Bonfire.Social.FeedLoader do
debug(presets, "Feed `#{name}` not found")
{:error, :not_found}

# %{admin_required: true} = alias when not user.is_admin ->
# %{admin_required: true} = alias when not user.is_admin ->
# {:error, :unauthorized} # TODO
# %{mod_required: true} = alias when not user.is_moderator ->
# %{mod_required: true} = alias when not user.is_moderator ->
# {:error, :unauthorized} # TODO
%{current_user_required: true} = feed_def ->
if current_user_required!(opts), do: {:ok, feed_def}
Expand Down Expand Up @@ -1280,7 +1298,7 @@ defmodule Bonfire.Social.FeedLoader do
# Failing with `:current_user_required` parameter if we have no current user
iex> replace_parameters(:current_user_required, %{}, current_user: nil)
** (Bonfire.Fail.Auth) You need to log in first.
** (Bonfire.Fail.Auth) You need to log in first.
# Handling a parameter that is in the opts
iex> replace_parameters(:type, %{}, type: "post")
Expand All @@ -1301,7 +1319,7 @@ defmodule Bonfire.Social.FeedLoader do
# # Handling a string key parameter that is in the opts - FIXME
# iex> replace_parameters("type", %{}, type: "post")
# "post"
# Handling a parameter that is not in the opts
iex> replace_parameters(:unknown, %{}, current_user: "bob")
:unknown
Expand Down Expand Up @@ -1436,25 +1454,25 @@ defmodule Bonfire.Social.FeedLoader do
iex> matches_filter?(%{types: "*"}, %{types: "post"})
true
iex> matches_filter?(%{types: ["post", "comment"]}, %{types: ["comment", "reaction"]})
true
iex> matches_filter?(%{types: "post"}, %{types: ["comment", "post"]})
true
iex> matches_filter?(%{types: :post}, %{types: ["comment", "post"]})
true
iex> matches_filter?(%{types: "post"}, %{types: [:comment, :post]})
true
iex> matches_filter?(%{types: ["post"]}, %{types: "post"})
true
iex> matches_filter?(%{types: "post"}, %{types: "comment"})
false
iex> matches_filter?(%{types: :post}, %{types: "post"})
true
"""
Expand All @@ -1472,7 +1490,7 @@ defmodule Bonfire.Social.FeedLoader do
rule_value == "*" ->
true

# Direct match
# Direct match
filter_value == rule_value ->
true

Expand Down Expand Up @@ -1566,7 +1584,7 @@ defmodule Bonfire.Social.FeedLoader do
:with_verb
]
# With unknown key
# With unknown key
iex> map_activity_preloads([:unknown_key])
[:unknown_key]
Expand Down

0 comments on commit 83a30b7

Please sign in to comment.