Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
mayel committed Sep 16, 2024
1 parent 444ee6b commit 5d356ea
Show file tree
Hide file tree
Showing 7 changed files with 451 additions and 39 deletions.
8 changes: 4 additions & 4 deletions lib/acts/activity_act.ex
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,8 @@ defmodule Bonfire.Social.Acts.Activity do

attrs = Keyword.get(epic.assigns[:options], attrs_key, %{})

notifications_feeds =
Feeds.reply_and_or_mentions_notifications_feeds(
notify =
Feeds.reply_and_or_mentions_to_notify(
current_user,
boundary_name,
e(changeset.changes, :post_content, :changes, :mentions, []),
Expand All @@ -86,7 +86,7 @@ defmodule Bonfire.Social.Acts.Activity do
current_user,
boundary_name,
epic.assigns,
notifications_feeds
notify[:notify_feeds]
)

# feed_ids = Feeds.target_feeds(changeset, current_user, boundary) # duplicate of `Feeds.feed_ids_to_publish`
Expand All @@ -100,7 +100,7 @@ defmodule Bonfire.Social.Acts.Activity do
)
|> Epic.assign(epic, on, ...)
|> Epic.assign(..., feeds_key, feed_ids)
|> Epic.assign(..., notify_feeds_key, notifications_feeds)
|> Epic.assign(..., notify_feeds_key, notify)

changeset.action == :delete ->
# TODO: deletion
Expand Down
7 changes: 3 additions & 4 deletions lib/acts/live_push_act.ex
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ defmodule Bonfire.Social.Acts.LivePush do
notify_feeds_key = Keyword.get(act.options, :notify_feeds, :notify_feeds)

feeds = Map.get(epic.assigns, feeds_key, [])
notify_feeds = Map.get(epic.assigns, notify_feeds_key, [])

maybe_debug(
epic,
Expand All @@ -41,11 +40,11 @@ defmodule Bonfire.Social.Acts.LivePush do
"Publishing to feeds at assign #{feeds_key}"
)

maybe_apply(Bonfire.UI.Social.LivePush, :push_activity, [
Bonfire.Social.LivePush.push_activity(
feeds,
activity,
[notify: notify_feeds]
])
notify: Map.get(epic.assigns, notify_feeds_key, [])
)
|> debug("pushed")
|> Epic.assign(epic, on, ...)
end
Expand Down
2 changes: 1 addition & 1 deletion lib/boosts.ex
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ defmodule Bonfire.Social.Boosts do
# livepush will need a list of feed IDs we published to
feed_ids = for fp <- boost.feed_publishes, do: fp.feed_id

maybe_apply(Bonfire.UI.Social.LivePush, :push_activity_object, [
maybe_apply(Bonfire.Social.LivePush, :push_activity_object, [
feed_ids,
boost,
boosted,
Expand Down
8 changes: 4 additions & 4 deletions lib/feed_activities.ex
Original file line number Diff line number Diff line change
Expand Up @@ -1408,7 +1408,7 @@ defmodule Bonfire.Social.FeedActivities do
# # debug(feed_ids)
# # |> debug("notify_to_feed_ids")
# ret = publish(subject, verb_or_activity, object, to_feeds: feed_ids)
# maybe_apply(Bonfire.UI.Social.LivePush, :notify, [
# maybe_apply(Bonfire.Social.LivePush, :notify, [
# subject, verb_or_activity, object, feed_ids
# ])
# ret
Expand Down Expand Up @@ -1583,7 +1583,7 @@ defmodule Bonfire.Social.FeedActivities do
# |> Circles.circle_ids()
|> Enum.map(fn x -> put_in_feeds(x, id(activity), false) end)

if push?, do: maybe_apply(Bonfire.UI.Social.LivePush, :push_activity, [feeds, activity])
if push?, do: maybe_apply(Bonfire.Social.LivePush, :push_activity, [feeds, activity])
end

defp put_in_feeds(feed_or_subject, activity, push?)
Expand All @@ -1592,7 +1592,7 @@ defmodule Bonfire.Social.FeedActivities do
with feed_id <- uid(feed_or_subject),
{:ok, _published} <- do_put_in_feeds(feed_id, uid(activity)) do
# push to feeds of online users
if push?, do: maybe_apply(Bonfire.UI.Social.LivePush, :push_activity, [feed_id, activity])
if push?, do: maybe_apply(Bonfire.Social.LivePush, :push_activity, [feed_id, activity])
else
e ->
error(
Expand Down Expand Up @@ -1665,7 +1665,7 @@ defmodule Bonfire.Social.FeedActivities do

defp hide_activities(fp) when is_list(fp) do
for %{id: activity, feed_id: feed_id} <- fp do
maybe_apply(Bonfire.UI.Social.LivePush, :hide_activity, [
maybe_apply(Bonfire.Social.LivePush, :hide_activity, [
feed_id,
activity
])
Expand Down
93 changes: 67 additions & 26 deletions lib/feeds.ex
Original file line number Diff line number Diff line change
Expand Up @@ -46,19 +46,19 @@ defmodule Bonfire.Social.Feeds do
> Bonfire.Social.Feeds.feed_ids_to_publish(me, "public", %{reply_to: true}, [some_feed_id])
# List of feed IDs for the provided boundary
"""
def feed_ids_to_publish(me, boundary, assigns, reply_and_or_mentions_notifications_feeds \\ nil)
def feed_ids_to_publish(me, boundary, assigns, notify_feeds \\ nil)

def feed_ids_to_publish(_me, "admins", _, _) do
admins_notifications()
|> debug("posting to admin feeds")
end

def feed_ids_to_publish(me, boundary, assigns, reply_and_or_mentions_notifications_feeds) do
def feed_ids_to_publish(me, boundary, assigns, notify_feeds) do
[
e(assigns, :reply_to, :replied, :thread, :id, nil),
maybe_my_outbox_feed_id(me, boundary),
global_feed_ids(boundary),
reply_and_or_mentions_notifications_feeds ||
notify_feeds ||
reply_and_or_mentions_notifications_feeds(
me,
boundary,
Expand Down Expand Up @@ -129,53 +129,94 @@ defmodule Bonfire.Social.Feeds do
reply_to_creator,
to_circles \\ []
) do
my_notifications = feed_id(:notifications, me)

(user_notifications_feeds([reply_to_creator], boundary) ++
user_notifications_feeds(
mentions,
boundary,
to_circles
))
|> Enums.filter_empty([])
|> Enum.uniq()
# my_notifications = feed_id(:notifications, me)

filter_reply_and_or_mentions(me, reply_to_creator, mentions)
|> users_to_notify(
boundary,
to_circles
)
|> notify_feeds()

# avoid self-notifying
|> Enum.reject(&(&1 == my_notifications))
# |> Enum.reject(&(&1 == my_notifications))
# |> debug()
end

def reply_and_or_mentions_to_notify(
me,
boundary,
mentions,
reply_to_creator,
to_circles \\ []
) do
users =
filter_reply_and_or_mentions(me, reply_to_creator, mentions)
|> debug()
|> users_to_notify(
boundary,
to_circles
)

%{
notify_feeds: notify_feeds(users),
notify_emails: notify_emails(users)
}
|> debug()
end

defp user_notifications_feeds(users, boundary, to_circles \\ []) do
defp filter_reply_and_or_mentions(me, reply_to_creator, mentions) do
my_id = Enums.id(me)

([reply_to_creator] ++ mentions)
# avoid self-notifying
|> Enum.reject(&(Enums.id(&1) == my_id))
end

defp users_to_notify(users, boundary, to_circles \\ []) do
# debug(epic, act, users, "users going in")
cond do
boundary in ["public", "mentions"] ->
users
|> debug()
|> filter_empty([])
|> repo().maybe_preload([:character])
|> Enum.map(&feed_id(:notifications, &1))
|> repo().maybe_preload([:character, :settings])

boundary == "local" ->
users
|> debug()
|> filter_empty([])
|> repo().maybe_preload([:character, :peered])
# only local
|> repo().maybe_preload([:character, :peered, :settings])
# notify only local users
|> Enum.filter(&is_local?/1)
|> Enum.map(&feed_id(:notifications, &1))

true ->
# we should only notify mentions & reply_to_creator IF they are included in the object's boundaries
# TODO: check also if they can read the object otherwise (for example, by being member of an included circle)

to_circles_ids = Enums.ids(to_circles)

users
|> filter_empty([])
|> Enum.filter(&(id(&1) in to_circles_ids))
|> repo().maybe_preload([:character])
|> debug()
# only local
|> Enum.map(&feed_id(:notifications, &1))
|> repo().maybe_preload([:character, :settings])
end
|> debug()
end

defp notify_feeds(users) do
users
|> Enum.map(&feed_id(:notifications, &1))
|> Enums.filter_empty([])
|> Enum.uniq()
end

defp notify_emails(users) do
users
|> Enum.filter(&Settings.get([:email_notifications, :reply_or_mentions], false, &1))
|> repo().maybe_preload(accounted: [account: [:email]])
|> debug()
|> Enum.map(&e(&1, :accounted, :account, :email, :email_address, nil))
|> Enums.filter_empty([])
|> Enum.uniq()
end

@doc """
Expand Down
Loading

0 comments on commit 5d356ea

Please sign in to comment.