Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
mayel committed Oct 16, 2023
1 parent 00ce037 commit fa2ca6d
Show file tree
Hide file tree
Showing 12 changed files with 95 additions and 52 deletions.
4 changes: 2 additions & 2 deletions lib/activities.ex
Original file line number Diff line number Diff line change
Expand Up @@ -1145,7 +1145,7 @@ defmodule Bonfire.Social.Activities do
"""
def all_verb_names() do
# Bonfire.Boundaries.Verbs.verbs()
case Bonfire.Common.Config.get(:verbs) do
case Bonfire.Common.Config.get(:verbs, nil, :bonfire) do
verbs when is_map(verbs) or (is_list(verbs) and verbs != []) ->
verbs
|> Enum.flat_map(fn {_key, data} ->
Expand All @@ -1154,7 +1154,7 @@ defmodule Bonfire.Social.Activities do

other ->
debug(other, ":verbs list not found in Config, fallback to :verb_names")
Bonfire.Common.Config.get!(:verb_names)
Bonfire.Common.Config.get!([:verb_names])
end
end

Expand Down
9 changes: 7 additions & 2 deletions lib/boosts.ex
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,10 @@ defmodule Bonfire.Social.Boosts do
end

def count(%{} = user, object) when is_struct(object) or is_binary(object),
do: Edges.count(__MODULE__, user, object, skip_boundary_check: true)
do: Edges.count_for_subject(__MODULE__, user, object, skip_boundary_check: true)

def count(object, _) when is_struct(object), do: Edges.count(:boost, object)
def count(object, _) when is_struct(object),
do: Edges.count(:boost, object, skip_boundary_check: true)

def date_last_boosted(%{} = user, object),
do: Edges.last_date(__MODULE__, user, object, skip_boundary_check: true)
Expand Down Expand Up @@ -131,6 +132,7 @@ defmodule Bonfire.Social.Boosts do
)

Integration.maybe_federate_and_gift_wrap_activity(booster, boost)
|> debug("maybe_federated the boost")
end
end

Expand Down Expand Up @@ -228,6 +230,9 @@ defmodule Bonfire.Social.Boosts do
pointer: e(boost, :edge, :object, nil) || e(boost, :edge, :object_id, nil)
) do
ActivityPub.announce(%{actor: booster, object: object, pointer: ulid(boost)})
else
e ->
error(e, "Could not find the federated actor or object to boost.")
end
end

Expand Down
24 changes: 13 additions & 11 deletions lib/edges.ex
Original file line number Diff line number Diff line change
Expand Up @@ -143,25 +143,17 @@ defmodule Bonfire.Social.Edges do
|> repo().exists?()
end

def count(type, subject, object, opts) do
do_query(type, subject, object, Keyword.put(opts, :preload, :skip))
|> Ecto.Query.exclude(:select)
# |> Ecto.Query.exclude(:distinct)
|> Ecto.Query.exclude(:preload)
|> Ecto.Query.exclude(:order_by)
|> select([type, edge], count(edge))
|> repo().one()
end
def count(type, filters_or_object, opts \\ [])

def count(type, object, opts) when is_struct(object) do
def count(type, object, _opts) when is_struct(object) do
field_name = maybe_to_atom("#{type}_count")

object
|> repo().maybe_preload(field_name, follow_pointers: false)
|> e(field_name, :object_count, nil)
end

def count(type, filters \\ [], opts \\ []) do
def count(type, filters, opts) when is_list(filters) and is_list(opts) do
do_query(type, filters, Keyword.put(opts, :preload, :skip))
|> Ecto.Query.exclude(:select)
# |> Ecto.Query.exclude(:distinct)
Expand All @@ -171,6 +163,16 @@ defmodule Bonfire.Social.Edges do
|> repo().one()
end

def count_for_subject(type, subject, object, opts) do
do_query(type, subject, object, Keyword.put(opts, :preload, :skip))
|> Ecto.Query.exclude(:select)
# |> Ecto.Query.exclude(:distinct)
|> Ecto.Query.exclude(:preload)
|> Ecto.Query.exclude(:order_by)
|> select([type, edge], count(edge))
|> repo().one()
end

# defp do_query(type, subject, object, opts \\ [])

defp do_query(type_context, filters, opts)
Expand Down
7 changes: 7 additions & 0 deletions lib/feed_activities.ex
Original file line number Diff line number Diff line change
Expand Up @@ -433,6 +433,13 @@ defmodule Bonfire.Social.FeedActivities do
debug(feed, "feed id(s)")
feed

itself when itself == feed_name ->
Feeds.my_feed_id(feed_name, opts) ||
(
error("FeedActivities.feed: no known feed #{inspect(feed_name)}")
nil
)

e ->
error("FeedActivities.feed: no known feed #{inspect(feed_name)} - #{inspect(e)}")

Expand Down
10 changes: 3 additions & 7 deletions lib/feeds.ex
Original file line number Diff line number Diff line change
Expand Up @@ -495,19 +495,15 @@ defmodule Bonfire.Social.Feeds do
end

defp save_box_feed(:outbox, character, feed_id) do
update_character(character, %{outbox_id: feed_id})
Characters.update(character, %{outbox_id: feed_id})
end

defp save_box_feed(:inbox, character, feed_id) do
update_character(character, %{inbox_id: feed_id})
Characters.update(character, %{inbox_id: feed_id})
end

defp save_box_feed(:notifications, character, feed_id) do
update_character(character, %{notifications_id: feed_id})
end

defp update_character(%Character{} = character, attrs) do
repo().update(Character.changeset(character, attrs, :update))
Characters.update(character, %{notifications_id: feed_id})
end

# @doc "Create a new generic feed"
Expand Down
30 changes: 17 additions & 13 deletions lib/follows.ex
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ defmodule Bonfire.Social.Follows do
object_type == Bonfire.Data.Identity.User,
do: Bonfire.Social.Graph.graph_add(user, object, Follow)

if info(opts[:incoming] != true, "Maybe outgoing follow?"),
if opts[:incoming] != true,
do: Integration.maybe_federate_and_gift_wrap_activity(user, follow),
else: {:ok, follow}

Expand Down Expand Up @@ -255,7 +255,7 @@ defmodule Bonfire.Social.Follows do
Activities.delete_by_subject_verb_object(subject, :request, object),
{:ok, follow} <- do_follow(subject, object, opts) |> debug("accept_do_follow"),
:ok <-
if(debug(opts[:incoming] != true, "Maybe outgoing accept?"),
if(opts[:incoming] != true,
do: Requests.ap_publish_activity(subject, {:accept, request}, follow),
else: :ok
) do
Expand All @@ -267,7 +267,9 @@ defmodule Bonfire.Social.Follows do
end)
end

def unfollow(user, %{} = object) do
def unfollow(user, object, opts \\ [])

def unfollow(user, %{} = object, opts) do
if following?(user, object) do
Edges.delete_by_both(user, Follow, object)
# with [_id] <- Edges.delete_by_both(user, Follow, object) do
Expand All @@ -285,8 +287,10 @@ defmodule Bonfire.Social.Follows do

Bonfire.Social.Graph.graph_remove(user, object, Follow)

if opts[:incoming] != true,
do: ap_publish_activity(user, :delete, object)

# Integration.maybe_federate(user, :unfollow, object)
ap_publish_activity(user, :delete, object)

# end
else
Expand All @@ -298,13 +302,13 @@ defmodule Bonfire.Social.Follows do
end
end

def unfollow(%{} = user, object) when is_binary(object) do
def unfollow(%{} = user, object, opts) when is_binary(object) do
with {:ok, object} <-
Bonfire.Common.Pointers.get(object,
current_user: user,
skip_boundary_check: true
) do
unfollow(user, object)
unfollow(user, object, opts)
end
end

Expand Down Expand Up @@ -539,7 +543,7 @@ defmodule Bonfire.Social.Follows do
# check if not already following
false <- following?(follower, followed),
{:ok, %Follow{} = follow} <-
follow(follower, followed, current_user: follower) do
follow(follower, followed, current_user: follower, incoming: true) do
with {:ok, _accept_activity, _adapter_object, accepted_activity} <-
ActivityPub.accept_activity(%{
actor: object,
Expand Down Expand Up @@ -616,16 +620,16 @@ defmodule Bonfire.Social.Follows do
Bonfire.Federate.ActivityPub.AdapterUtils.fetch_character_by_ap_id(follower) do
case following?(follower, followed) do
false ->
reject(follower, followed)
reject(follower, followed, incoming: true)

true ->
request =
with {:ok, request} <- reject(follower, followed) do
with {:ok, request} <- reject(follower, followed, incoming: true) do
request
|> debug("rejected previously accepted request")
end

unfollow(follower, followed)
unfollow(follower, followed, incoming: true)
|> debug("unfollow rejected follow")

{:ok, request}
Expand All @@ -640,15 +644,15 @@ defmodule Bonfire.Social.Follows do
) do
with {:ok, object} <-
Bonfire.Federate.ActivityPub.AdapterUtils.fetch_character_by_ap_id(followed_ap_id),
[id] <- unfollow(follower, object) do
[id] <- unfollow(follower, object, incoming: true) do
{:ok, id}
end
end

defp reject(follower, followed) do
defp reject(follower, followed, opts \\ []) do
with {:ok, request} <-
Requests.get(follower, Follow, followed, skip_boundary_check: true),
{:ok, request} <- ignore(request, current_user: followed) do
{:ok, request} <- ignore(request, opts ++ [current_user: followed]) do
{:ok, request}
end
end
Expand Down
26 changes: 20 additions & 6 deletions lib/graph.ex
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ defmodule Bonfire.Social.Graph do

graph_conn ->
graph_conn
|> Bolt.Sips.query("CREATE CONSTRAINT ON (n: Character) ASSERT EXISTS (n.id);
|> graph_query("CREATE CONSTRAINT ON (n: Character) ASSERT EXISTS (n.id);
CREATE CONSTRAINT ON (n: Character) ASSERT n.id IS UNIQUE;")

load_from_db()
Expand Down Expand Up @@ -90,6 +90,20 @@ defmodule Bonfire.Social.Graph do
nil
end

def graph_query(graph_conn \\ graph_conn(), query) do
case graph_conn do
nil ->
nil

graph_conn ->
Bolt.Sips.query(graph_conn, query)
end
catch
:exit, reason ->
error(reason)
nil
end

def graph_meta(_subject \\ nil) do
# TODO: put in Settings
[
Expand All @@ -112,7 +126,7 @@ defmodule Bonfire.Social.Graph do
MERGE (a)-[r:#{graph_meta[:rel_name] || type} {rank: #{graph_meta[:rank] || 1}}]->(b)
RETURN a.id, type(r), b.id;"
|> debug()
|> Bolt.Sips.query(graph_conn, ...)
|> graph_query(graph_conn, ...)
|> debug()
end
end
Expand All @@ -128,7 +142,7 @@ defmodule Bonfire.Social.Graph do
"MATCH (a: Character {id: '#{id(subject)}'})-[r:#{graph_meta[:rel_name] || type}]->(b: Character {id: '#{id(object)}'})
DELETE r;"
|> debug()
|> Bolt.Sips.query(graph_conn, ...)
|> graph_query(graph_conn, ...)
|> debug()
end
end
Expand All @@ -144,7 +158,7 @@ defmodule Bonfire.Social.Graph do
CALL nxalg.shortest_path_length(subject, object, 'rank') YIELD *
RETURN length;"
|> debug()
|> Bolt.Sips.query(graph_conn, ...)
|> graph_query(graph_conn, ...)
|> debug() do
{:ok, %{records: [[length]]}} ->
length
Expand All @@ -166,7 +180,7 @@ defmodule Bonfire.Social.Graph do
CALL nxalg.shortest_path_length(subject, NULL, 'rank') YIELD *
RETURN target.id, length ORDER BY length;"
|> debug()
|> Bolt.Sips.query(graph_conn, ...)
|> graph_query(graph_conn, ...)
|> debug() do
{:ok, %{records: [[length]]}} ->
length
Expand All @@ -184,7 +198,7 @@ defmodule Bonfire.Social.Graph do
nil

graph_conn ->
Bolt.Sips.query(graph_conn, "MATCH (n) DETACH DELETE n;")
graph_query(graph_conn, "MATCH (n) DETACH DELETE n;")
|> debug()
end
end
Expand Down
8 changes: 6 additions & 2 deletions lib/integration.ex
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,12 @@ defmodule Bonfire.Social.Integration do
}
})}
else
:ignore -> {:ok, object}
other -> other
:ignore ->
{:ok, object}

other ->
warn(other, "Unexpected result")
other
end
end

Expand Down
12 changes: 9 additions & 3 deletions lib/likes.ex
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,16 @@ defmodule Bonfire.Social.Likes do
def by_liked(object, opts \\ []) when is_map(object) or is_binary(object),
do: (opts ++ [object: object]) |> query(opts) |> repo().many()

def count(%{} = user, object),
do: Edges.count(__MODULE__, user, object, skip_boundary_check: true)
def count(filters \\ [], opts \\ [])

def count(%{} = object), do: Edges.count(:like, object)
def count(filters, opts) when is_list(filters) and is_list(opts) do
Edges.count(__MODULE__, filters, opts)
end

def count(%{} = user, object) when is_struct(object) or is_binary(object),
do: Edges.count_for_subject(__MODULE__, user, object, skip_boundary_check: true)

def count(%{} = object, _), do: Edges.count(:like, object, skip_boundary_check: true)

def like(liker, object, opts \\ [])

Expand Down
7 changes: 4 additions & 3 deletions lib/posts.ex
Original file line number Diff line number Diff line change
Expand Up @@ -249,9 +249,10 @@ defmodule Bonfire.Social.Posts do

# TODO: should we just include ALL thread participants? ^

# FIXME: use `get_preset_on_object` instead of loading them all
acls = Bonfire.Boundaries.list_object_acls(post)
# |> debug("acls")
# FIXME: use `get_preset_on_object` instead of loading them all, or at least only load the IDs
acls =
Bonfire.Boundaries.list_object_acls(post)
|> debug("post_acls")

is_public = Enum.any?(acls, fn %{id: acl_id} -> acl_id in public_acl_ids end)

Expand Down
6 changes: 4 additions & 2 deletions lib/requests.ex
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ defmodule Bonfire.Social.Requests do
# end
# end

defp do_request(requester, type, object, _opts) do
defp do_request(requester, type, object, opts) do
opts = [
boundary: "mentions",
to_circles: [id(object)],
Expand All @@ -208,7 +208,9 @@ defmodule Bonfire.Social.Requests do

case create(requester, type, object, opts) do
{:ok, request} ->
Integration.maybe_federate_and_gift_wrap_activity(requester, request)
if opts[:incoming] != true,
do: Integration.maybe_federate_and_gift_wrap_activity(requester, request),
else: {:ok, request}

e ->
warn(e)
Expand Down
4 changes: 3 additions & 1 deletion lib/threads.ex
Original file line number Diff line number Diff line change
Expand Up @@ -655,7 +655,9 @@ defmodule Bonfire.Social.Threads do
# end

@doc "Key can be for eg. :thread_id or :reply_to_id"
def ap_prepare(object, key \\ :thread_id) when is_struct(object) do
def ap_prepare(object_or_thread_or_reply_to_id, key \\ :thread_id)

def ap_prepare(object, key) when is_struct(object) do
object
|> repo().maybe_preload([
:replied
Expand Down

0 comments on commit fa2ca6d

Please sign in to comment.