Skip to content

Commit

Permalink
https://github.com/bonfire-networks/bonfire-app/issues/903
Browse files Browse the repository at this point in the history
  • Loading branch information
mayel committed Mar 5, 2024
1 parent 9526bf3 commit bd005c9
Show file tree
Hide file tree
Showing 8 changed files with 108 additions and 74 deletions.
84 changes: 65 additions & 19 deletions lib/benchmark.ex
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ defmodule Bonfire.UI.Social.Benchmark do
Benchee,
:run,
[
current_user_approaches_feed(),
# current_user_approaches_feed(),
# md_lib_feed(),
# some_feed_queries(),
some_feed_queries(),
[
parallel: 1,
warmup: 2,
Expand Down Expand Up @@ -155,8 +155,10 @@ defmodule Bonfire.UI.Social.Benchmark do
:run,
[
%{
"fetch feed page with activities" => fn -> get(conn, "/feed/local") end,
# "query & render entire feed page with activities" => fn -> live(conn, "/feed/local") end, # NOPE: LiveView helpers can only be invoked from the test process
# "query & render feed component with activities (default preloads)" => fn ->
# live_feed()
# end,
# "query & render feed component with activities (using all async preloads)" => fn ->
# live_feed(live_update_many_preloads: :async)
# end,
Expand All @@ -178,24 +180,65 @@ defmodule Bonfire.UI.Social.Benchmark do
# "query & render feed component with activities (skipping feed preloads)" => fn ->
# live_feed(feed_live_update_many_preloads: :skip)
# end,
"render feed component with already queried activities (skipping preloads)" => fn ->
render_feed(feed.edges, feed_live_update_many_preloads: :skip)
end,
"render feed component with already queried activities (inline preloads)" => fn ->
render_feed(feed.edges, feed_live_update_many_preloads: :inline)
end,
"render feed component with already queried activities (async actions preloads)" =>
fn ->
render_feed(feed.edges, feed_live_update_many_preloads: :async_actions)
end,
"render feed component with already queried activities (async preloads)" => fn ->
render_feed(feed.edges, feed_live_update_many_preloads: :async)
end
# "render feed component with already queried activities (default preloads)" => fn ->
# render_feed(feed.edges)
# end,
# "render feed component with already queried activities (skipping preloads)" => fn ->
# render_feed(feed.edges, feed_live_update_many_preloads: :skip)
# end,
# "render feed component with already queried activities (inline preloads)" => fn ->
# render_feed(feed.edges, feed_live_update_many_preloads: :inline)
# end,
# "render feed component with already queried activities (async actions preloads)" =>
# fn ->
# render_feed(feed.edges, feed_live_update_many_preloads: :async_actions)
# end,
# "render feed component with already queried activities (async preloads)" => fn ->
# render_feed(feed.edges, feed_live_update_many_preloads: :async)
# end,
# "render feed component with already queried activities (skip activity component)" => fn ->
# render_feed(feed.edges, hide_activities: "component")
# end,
# "render feed component with already queried activities (hide activity component)" => fn ->
# render_feed(feed.edges, hide_activities: "all")
# end,
# "render feed component with already queried activities (skip subject)" => fn ->
# render_feed(feed.edges, hide_activities: "subject")
# end,
# "render feed component with already queried activities (skip note)" => fn ->
# render_feed(feed.edges, hide_activities: "note")
# end,
# "render feed component with already queried activities (skip media)" => fn ->
# render_feed(feed.edges, hide_activities: "media")
# end,
# "render feed component with already queried activities (skip actions)" => fn ->
# render_feed(feed.edges, hide_activities: "actions")
# end,
# "render feed component with already queried activities (skip dynamic)" => fn ->
# render_feed(feed.edges, hide_activities: "dynamic")
# end,
"fetch home page with activities" => fn -> get(conn, "/") end,
"fetch feed page with activities" => fn -> get(conn, "/feed/local") end
# "fetch feed page with (skipped) activities" => fn ->
# get(conn, "/feed/local?&hide_activities=component")
# end,
# "fetch feed page with (not rendered) activities" => fn ->
# get(conn, "/feed/local?&hide_activities=all")
# end,
# "fetch feed page with not rendered subject" => fn ->
# get(conn, "/feed/local?&hide_activities=subject")
# end,
# "fetch feed page with not rendered note" => fn ->
# get(conn, "/feed/local?&hide_activities=note")
# end,
# "fetch feed page with not rendered media" => fn ->
# get(conn, "/feed/local?&hide_activities=media")
# end,
# "fetch feed page with not rendered actions" => fn ->
# get(conn, "/feed/local?&hide_activities=actions")
# end,
# "fetch feed page with not rendered dynamic component" => fn ->
# get(conn, "/feed/local?&hide_activities=dynamic")
# end
},
[
Expand Down Expand Up @@ -259,9 +302,12 @@ defmodule Bonfire.UI.Social.Benchmark do
Process.put(:live_update_many_preloads, opts[:live_update_many_preloads])
Process.put(:feed_live_update_many_preloads, opts[:feed_live_update_many_preloads])

Bonfire.UI.Common.Testing.Helpers.render_stateful(Bonfire.UI.Social.FeedLive, %{
feed: feed
})
Bonfire.UI.Common.Testing.Helpers.render_stateful(
Bonfire.UI.Social.FeedLive,
Enum.into(opts, %{
feed: feed
})
)
end

if Config.get(:env) == :prod do
Expand Down
1 change: 1 addition & 0 deletions lib/components/activity/actions/actions_live.ex
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ defmodule Bonfire.UI.Social.Activity.ActionsLive do
prop my_like, :any, default: nil
prop my_bookmark, :any, default: nil

# @decorate time()
def update_many(assigns_sockets) do
assigns_sockets
|> LiveHandler.actions_update_many(caller_module: __MODULE__)
Expand Down
61 changes: 21 additions & 40 deletions lib/components/activity/actions/actions_live.sface
Original file line number Diff line number Diff line change
Expand Up @@ -55,30 +55,30 @@
is_remote={@is_remote}
/>
{#else}
<StatelessComponent
module={maybe_component(Bonfire.UI.Social.Activity.ReplyLive, @__context__)}
object_id={id(@object)}
object_type={@object_type}
object_boundary={@object_boundary}
permalink={@permalink}
thread_mode={@thread_mode}
reply_count={if @showing_within == :thread and @thread_mode != :flat,
do:
e(@activity, :replied, :direct_replies_count, nil) ||
e(@object, :replied, :direct_replies_count, 0),
else: @reply_count}
showing_within={@showing_within}
hide_reply={@hide_reply}
viewing_main_object={@viewing_main_object}
activity_component_id={@activity_component_id}
event_target={@myself}
class="flex btn btn-sm btn-ghost btn-circle text-base-content/70"
/>

{#if LiveHandler.feed_live_update_many_preloads?() == :async_actions}
{#if @object_boundary || !socket_connected?(@__context__)}
<!-- LOAD ALL AS IF THEY WERE STATELESS COMPONENTS, since we preload data in this component -->

<StatelessComponent
module={maybe_component(Bonfire.UI.Social.Activity.ReplyLive, @__context__)}
object_id={id(@object)}
object_type={@object_type}
object_boundary={@object_boundary}
permalink={@permalink}
thread_mode={@thread_mode}
reply_count={if @showing_within == :thread and @thread_mode != :flat,
do:
e(@activity, :replied, :direct_replies_count, nil) ||
e(@object, :replied, :direct_replies_count, 0),
else: @reply_count}
showing_within={@showing_within}
hide_reply={@hide_reply}
viewing_main_object={@viewing_main_object}
activity_component_id={@activity_component_id}
event_target={@myself}
class="flex btn btn-sm btn-ghost btn-circle text-base-content/70"
/>

<StatelessComponent
:if={@showing_within != :messages}
module={maybe_component(Bonfire.UI.Reactions.BoostActionLive, @__context__)}
Expand Down Expand Up @@ -146,31 +146,12 @@
{#else}
<!-- ACTIONS SKELETON GOES HERE -->
<div class="skeleton w-6 h-6 rounded-full shrink-0" />
<div class="skeleton w-6 h-6 ml-1 rounded-full shrink-0" />
<!-- <div class="skeleton w-6 h-6 ml-1 rounded-full shrink-0" /> -->
<div class="skeleton w-6 h-6 ml-1 rounded-full shrink-0" />
<div class="skeleton w-6 h-6 ml-1 rounded-full shrink-0" />
<div class="skeleton w-6 h-6 ml-1 rounded-full shrink-0" />
{/if}
{#else}
<StatefulComponent
module={maybe_component(Bonfire.UI.Social.Activity.ReplyLive, @__context__)}
object_id={id(@object)}
object_type={@object_type}
object_boundary={@object_boundary}
permalink={@permalink}
thread_mode={@thread_mode}
reply_count={if @showing_within == :thread and @thread_mode != :flat,
do:
e(@activity, :replied, :direct_replies_count, nil) ||
e(@object, :replied, :direct_replies_count, 0),
else: @reply_count}
showing_within={@showing_within}
hide_reply={@hide_reply}
viewing_main_object={@viewing_main_object}
activity_component_id={@activity_component_id}
class="flex btn btn-sm btn-ghost btn-circle text-base-content/70"
/>

<StatefulComponent
module={maybe_component(Bonfire.UI.Reactions.BoostActionLive, @__context__)}
:if={@showing_within != :messages}
Expand Down
4 changes: 2 additions & 2 deletions lib/components/activity/activity_live.ex
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ defmodule Bonfire.UI.Social.ActivityLive do
prop hide_actions, :any, default: false
prop activity_loaded_preloads, :list, default: []

# @decorate time()
@decorate time()
def update_many(assigns_sockets) do
assigns_sockets
|> LiveHandler.activity_update_many(caller_module: __MODULE__)
Expand Down Expand Up @@ -715,7 +715,7 @@ defmodule Bonfire.UI.Social.ActivityLive do
Bonfire.UI.Moderation.FlaggedActionsLive
]}
{#if @hide_activity != "actions" and @hide_actions != true}
{#if LiveHandler.feed_live_update_many_preloads?() == :async_actions}
{#if socket_connected?(@__context__) && LiveHandler.feed_live_update_many_preloads?() == :async_actions}
<StatefulComponent
id={"#{@activity_component_id}_actions"}
module={component}
Expand Down
2 changes: 1 addition & 1 deletion lib/components/activity/media/media_live.sface
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
{#if e(m.metadata, "wikibase", "itemType", nil) in ["journalArticle"] or
e(m.metadata, "crossref", "DOI", nil) || e(m.metadata, "oembed", "DOI", nil) ||
e(m.metadata, "other", "citation_doi", nil) || e(m.metadata, "other", "citation_doi", nil) ||
e(m.metadata, "json_ld", "@type", nil) in ["ScholarlyArticle"]}
e(m.metadata, "json_ld", "@type", nil) in ["ScholarlyArticle", "Dataset"]}
<Bonfire.UI.Social.Activity.AcademicPaperLive
media={m}
metadata={Enums.deep_merge_reduce([
Expand Down
24 changes: 14 additions & 10 deletions lib/components/feeds/feed_live.ex
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ defmodule Bonfire.UI.Social.FeedLive do
prop loading, :boolean, default: true
prop preload, :atom, default: :feed
prop cache_strategy, :any, default: nil
prop hide_activities, :any, default: nil

prop feedback_title, :string, default: nil
prop feedback_message, :string, default: nil
Expand Down Expand Up @@ -68,8 +69,6 @@ defmodule Bonfire.UI.Social.FeedLive do
|> stream_configure(:feed, dom_id: &stream_id("fa", &1))
|> stream(:feed, [])
|> assign(
feed: nil,
hide_activities: nil,
feed_count: nil,
hide_fresh: 0,
cute_gif: maybe_cute_gif()
Expand Down Expand Up @@ -117,6 +116,9 @@ defmodule Bonfire.UI.Social.FeedLive do
# end
end

@decorate time()
def update(assigns, socket)

def update(%{insert_stream: %{feed: entries}} = assigns, socket) do
debug("feed stream is being poured into")

Expand Down Expand Up @@ -207,7 +209,7 @@ defmodule Bonfire.UI.Social.FeedLive do

maybe_subscribe(socket)

{:ok, socket}
ok_socket(socket)
end

def update(%{feed: nil, feed_filters: empty_feed_filters} = assigns, socket)
Expand All @@ -227,7 +229,7 @@ defmodule Bonfire.UI.Social.FeedLive do

maybe_subscribe(socket)

{:ok, socket}
ok_socket(socket)
end

def update(%{feed: nil} = assigns, socket) do
Expand All @@ -247,7 +249,7 @@ defmodule Bonfire.UI.Social.FeedLive do

maybe_subscribe(socket)

{:ok, socket}
ok_socket(socket)
end

def update(%{feed: :loading} = assigns, socket) do
Expand All @@ -264,7 +266,7 @@ defmodule Bonfire.UI.Social.FeedLive do

def update(_assigns, socket) do
warn("No feed loaded")
{:ok, socket}
ok_socket(socket)
end

defp ok_socket(socket) do
Expand All @@ -273,8 +275,10 @@ defmodule Bonfire.UI.Social.FeedLive do
{:ok,
socket
|> assign(
feed_component_id: socket.assigns.id,
hide_activities: socket.assigns[:__context__][:current_params]["hide_activities"]
feed_component_id: socket.assigns[:id],
hide_activities:
socket.assigns[:hide_activities] ||
socket.assigns[:__context__][:current_params]["hide_activities"]
)}
end

Expand Down Expand Up @@ -439,10 +443,10 @@ defmodule Bonfire.UI.Social.FeedLive do
# socket
# )do

# {:ok,
# ok_socket(
# socket
# |> assign(
# hide_fresh: 0
# )}
# ))
# end
end
2 changes: 2 additions & 0 deletions lib/live_handlers/feeds_live_handler.ex
Original file line number Diff line number Diff line change
Expand Up @@ -1156,6 +1156,7 @@ defmodule Bonfire.Social.Feeds.LiveHandler do
end
end

@decorate time()
def activity_update_many(assigns_sockets, opts) do
feed_live_update_many_preloads = feed_live_update_many_preloads?()

Expand Down Expand Up @@ -1200,6 +1201,7 @@ defmodule Bonfire.Social.Feeds.LiveHandler do
end
end

@decorate time()
def actions_update_many(assigns_sockets, opts) do
feed_live_update_many_preloads = feed_live_update_many_preloads?()

Expand Down
4 changes: 2 additions & 2 deletions lib/social_routes.ex
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ defmodule Bonfire.UI.Social.Routes do
scope "/", Bonfire.UI.Social do
pipe_through(:browser)

live("/feed", FeedsLive, as: :feed)
live("/feed/local", FeedsLive, :local, as: :feed)
# live("/local", Feeds.LocalLive, as: :local)
# live("/federation", Feeds.FederationLive, as: :federation)
# live("/federation/:type", Feeds.FederationLive, as: :federation)
Expand All @@ -23,8 +25,6 @@ defmodule Bonfire.UI.Social.Routes do
scope "/", Bonfire.UI.Social do
pipe_through(:browser)
pipe_through(:user_required)
live("/feed", FeedsLive, as: :feed)
live("/feed/local", FeedsLive, :local, as: :feed)
live("/feed/fediverse", FeedsLive, :fediverse, as: :feed)
# live("/feed/explore", FeedsLive, :explore, as: :explore)
live("/feed/:tab", FeedsLive, as: :feed)
Expand Down

0 comments on commit bd005c9

Please sign in to comment.