From 2e8e4311bd191c1d1018e6ade438a3476cef271b Mon Sep 17 00:00:00 2001 From: Mayel de Borniol Date: Sun, 1 Sep 2024 15:26:59 +0100 Subject: [PATCH] https://github.com/bonfire-networks/bonfire-app/issues/831 --- lib/activities.ex | 22 ++++++++++++++++++---- lib/feed_activities.ex | 34 ++++++++++++++++++++-------------- lib/post_contents.ex | 4 ++-- lib/threads.ex | 6 ++++++ 4 files changed, 46 insertions(+), 20 deletions(-) diff --git a/lib/activities.ex b/lib/activities.ex index 9067b72..441fd72 100644 --- a/lib/activities.ex +++ b/lib/activities.ex @@ -783,10 +783,20 @@ defmodule Bonfire.Social.Activities do do: query |> proload( - activity: [labelled: {"labelled_", [:post_content, :media, subject: [:profile]]}] + activity: [ + labelled: + {"labelled_", + [ + :post_content + # :media, + # subject: [:profile] + ]} + ] ), else: query + # NOTE: media attached to the label should be loaded separately as there can be several + # proload query, activity: [:media] # FYI: proloading media only queries one attachment :with_seen -> query_preload_seen(query, opts) @@ -851,9 +861,7 @@ defmodule Bonfire.Social.Activities do [:media, :sensitive] :maybe_with_labelled -> - if Extend.module_enabled?(Bonfire.Label, opts), - do: [labelled: [:post_content, :media, subject: [:profile]]], - else: [] + maybe_with_labelled() :with_seen -> subquery = subquery_preload_seen(opts) @@ -862,6 +870,12 @@ defmodule Bonfire.Social.Activities do end end + def maybe_with_labelled do + if Extend.extension_enabled?(:bonfire_label), + do: [labelled: [:post_content, :media, subject: [:profile]]], + else: [] + end + defp maybe_preload_reply_to(opts) do # If the root replied to anything, fetch that and its creator too. e.g. # * Alice's post that replied to Bob's post diff --git a/lib/feed_activities.ex b/lib/feed_activities.ex index ce2b5d2..ea29c90 100644 --- a/lib/feed_activities.ex +++ b/lib/feed_activities.ex @@ -370,18 +370,6 @@ defmodule Bonfire.Social.FeedActivities do result end - # TODO: where best to do these postloads? and try to optimise into one call - |> Bonfire.Common.Needles.Preload.maybe_preload_nested_pointers( - [activity: [replied: [:reply_to]]], - opts - ) - |> Bonfire.Common.Needles.Preload.maybe_preload_nested_pointers( - [activity: [:object]], - opts - ) - - # run post-preloads to follow pointers and catch anything else missing - TODO: only follow some pointers - # |> Activities.activity_preloads(e(opts, :preload, :feed), opts |> Keyword.put_new(:follow_pointers, true)) end end @@ -707,7 +695,25 @@ defmodule Bonfire.Social.FeedActivities do :edges, edges |> maybe_dedup_feed_objects(opts) - # |> Activities.activity_preloads(post_preloads, opts) + # TODO: where best to do these postloads? and try to optimise into one call + + |> Bonfire.Common.Needles.Preload.maybe_preload_nested_pointers( + [activity: [replied: [:reply_to]]], + opts + ) + |> Bonfire.Common.Needles.Preload.maybe_preload_nested_pointers( + [activity: [:object]], + opts + ) + |> repo().maybe_preload( + # FIXME: this should happen in `Activities.activity_preloads` + [activity: Activities.maybe_with_labelled()], + opts |> Keyword.put_new(:follow_pointers, false) + ) + + # run post-preloads to follow pointers and catch anything else missing - TODO: only follow some pointers + # |> Activities.activity_preloads(e(opts, :preload, :feed), opts |> Keyword.put_new(:follow_pointers, true)) + # |> Activities.activity_preloads(e(opts, :preload, :feed), opts |> Keyword.put_new(:follow_pointers, false)) ) end @@ -959,7 +965,7 @@ defmodule Bonfire.Social.FeedActivities do debug("include labelling for all") [] else - debug("do not include labelling") + debug("do not include labelling as activities") [:label] end ++ if opts[:include_flags] == :moderators and diff --git a/lib/post_contents.ex b/lib/post_contents.ex index 79f0fdc..c5eb800 100644 --- a/lib/post_contents.ex +++ b/lib/post_contents.ex @@ -124,13 +124,13 @@ defmodule Bonfire.Social.PostContents do #Ecto.Changeset<...> """ def cast(changeset, attrs, creator, boundary, opts) do - has_images = is_list(attrs[:uploaded_media]) and length(attrs[:uploaded_media]) > 0 + has_media = not is_nil(e(attrs, :uploaded_media, nil) || e(attrs, :links, nil)) changeset |> repo().maybe_preload(:post_content) |> Changeset.cast(%{post_content: maybe_prepare_contents(attrs, creator, boundary, opts)}, []) |> Changeset.cast_assoc(:post_content, - required: !has_images, + required: !has_media, with: &changeset/2 # with: (if changeset.action==:upsert, do: &changeset_update/2, else: &changeset/2) ) diff --git a/lib/threads.ex b/lib/threads.ex index 395034f..54ebad9 100644 --- a/lib/threads.ex +++ b/lib/threads.ex @@ -583,8 +583,14 @@ defmodule Bonfire.Social.Threads do # preloaded after so we can get more than 1 |> repo().maybe_preload( # :pinned, + # FIXME: this should happen via `Activities.activity_preloads` activity: [:media] ) + |> repo().maybe_preload( + # FIXME: this should happen via `Activities.activity_preloads` + [activity: Activities.maybe_with_labelled()], + opts |> Keyword.put_new(:follow_pointers, false) + ) # |> repo().many # without pagination # |> debug("thread")