Skip to content

Commit 3ba6412

Browse files
committed
https://github.com/bonfire-networks/bonfire-app/issues/1535
1 parent 74adda0 commit 3ba6412

File tree

4 files changed

+88
-31
lines changed

4 files changed

+88
-31
lines changed

lib/activities.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -703,7 +703,7 @@ defmodule Bonfire.Social.Activities do
703703
[tags: [:character, profile: :icon]]
704704

705705
:quote_tags ->
706-
# Quote posts
706+
# Quote posts - TODO: only preload actual posts? excluding hashtags and mentions etc
707707
[tags: [:character, :post_content, created: [creator: [:profile, :character]]]]
708708

709709
:with_subject ->

lib/acts/federate_act.ex

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -90,13 +90,13 @@ defmodule Bonfire.Social.Acts.Federate do
9090
not Social.is_local?(current_user) or not Social.is_local?(object) ->
9191
warn(current_user, "ActivityPub: Skip pushing remote object")
9292

93-
# do this anyway because we might need to create pending quote requests for local objects
94-
maybe_create_pending_quote_requests(
95-
current_user,
96-
epic.assigns[:request_quotes],
97-
object,
98-
options
99-
)
93+
# should we do this here?
94+
# maybe_create_pending_quote_requests(
95+
# current_user,
96+
# epic.assigns[:request_quotes],
97+
# object,
98+
# options
99+
# )
100100

101101
nil
102102

lib/quotes.ex

Lines changed: 71 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ defmodule Bonfire.Social.Quotes do
178178
# debug(opts, "opts")
179179

180180
with {:ok, %{edge: %{object: quoted_object, subject: quote_post}} = request} <-
181-
Requests.accept(request, opts) |> debug("accepted_quote"),
181+
Requests.accept(request, opts) |> debug("accepted_quote on #{repo()}"),
182182
quoted_creator =
183183
(quoted_creator ||
184184
e(request, :edge, :object, :created, :creator, nil) ||
@@ -218,9 +218,6 @@ defmodule Bonfire.Social.Quotes do
218218
)
219219
|> debug("published_update_for_quote_post") do
220220
{:ok, quote_post}
221-
else
222-
e ->
223-
error(e, l("An error occurred while accepting the quote request"))
224221
end
225222
end
226223

@@ -251,10 +248,13 @@ defmodule Bonfire.Social.Quotes do
251248
{:ok, ignored_request}
252249
"""
253250
def reject(request, opts) do
254-
reject(request, e(request, :edge, :subject, nil), e(request, :edge, :object, nil), opts)
251+
Requests.requested(request)
252+
|> flood("request to reject")
253+
~> reject(..., e(..., :edge, :subject, nil), e(..., :edge, :object, nil), opts)
255254
end
256255

257-
def reject(request, quote_object, quoted_object, opts) do
256+
def reject(request, quote_object, quoted_object, opts)
257+
when is_struct(quote_object) and is_struct(quoted_object) do
258258
quote_object =
259259
quote_object
260260
|> repo().maybe_preload(created: [creator: [:character]])
@@ -263,13 +263,28 @@ defmodule Bonfire.Social.Quotes do
263263
quoted_object
264264
|> repo().maybe_preload(created: [creator: [:character]])
265265

266+
quoted_creator =
267+
(e(request, :edge, :object, :created, :creator, nil) ||
268+
e(quoted_object, :created, :creator, nil) ||
269+
e(request, :edge, :object, :created, :creator_id, nil) ||
270+
e(quoted_object, :created, :creator_id, nil))
271+
|> debug("determined_quoted_creator")
272+
266273
with {:ok, request} <- Requests.ignore(request, opts) |> debug("ignored_quote_request"),
267-
{:ok, _} <-
274+
{:ok, quote_object} <-
268275
update_quote_remove(quote_object, quoted_object) |> debug("removed_quote_tag"),
269276
{:ok, _} <-
270277
federate_reject(opts[:verb], request, quote_object, quoted_object)
271-
|> debug("ap_rejected_quote_request") do
272-
{:ok, request}
278+
|> debug("ap_rejected_quote_request"),
279+
# Then send Update for the now-unauthorized quote post (only if this is a local quote_post)
280+
{:ok, quote_post} <-
281+
Social.maybe_federate_and_gift_wrap_activity(
282+
quoted_creator,
283+
quote_object,
284+
opts ++ [verb: :update]
285+
)
286+
|> debug("published_update_for_quote_post") do
287+
{:ok, quote_post}
273288
end
274289
end
275290

@@ -279,9 +294,19 @@ defmodule Bonfire.Social.Quotes do
279294
|> debug("ap_quote_object"),
280295
{:ok, quote_auth} <-
281296
ActivityPub.Object.get_cached(ap_id: ap_quote_object.data["quoteAuthorization"])
282-
|> debug("ap_quote_object"),
283-
{:ok, result} <- ActivityPub.delete(quote_auth) do
297+
|> debug("ap_quoteAuthorization"),
298+
{:ok, result} <-
299+
ActivityPub.delete(quote_auth,
300+
bcc: e(ap_quote_object, :data, "to", []) ++ e(ap_quote_object, :data, "cc", [])
301+
) do
284302
{:ok, result}
303+
else
304+
{:error, :not_found} ->
305+
debug("No quoteAuthorization found, nothing to delete")
306+
{:ok, :ignore}
307+
308+
e ->
309+
error(e, "Error while attempting to federate the rejection")
285310
end
286311
end
287312

@@ -318,6 +343,13 @@ defmodule Bonfire.Social.Quotes do
318343
local: true
319344
}) do
320345
{:ok, result}
346+
else
347+
{:error, :not_found} ->
348+
warn("No AP object found for the quote post, quoted post, or actor, so skip federation")
349+
{:ok, :ignore}
350+
351+
e ->
352+
error(e, "Error while attempting to federate the rejection")
321353
end
322354
end
323355

@@ -339,6 +371,10 @@ defmodule Bonfire.Social.Quotes do
339371
debug(quoted_object, "tags to remove from thing")
340372

341373
Bonfire.Tag.Tagged.thing_tags_remove(quote_post, quoted_object)
374+
375+
{:ok,
376+
quote_post
377+
|> repo().maybe_preload([:tags], force: true)}
342378
end
343379

344380
@doc """
@@ -364,7 +400,8 @@ defmodule Bonfire.Social.Quotes do
364400
def fetch_fresh_quote_authorization(quote_post, quoted_object \\ nil) do
365401
quoted_object = quoted_object || get_first_quoted_object(quote_post)
366402

367-
with {:ok, %{data: ap_json}} <- ActivityPub.Object.get_cached(pointer: quote_post),
403+
with {:ok, %{data: ap_json}} <-
404+
ActivityPub.Object.get_cached(pointer: quote_post) |> debug("quote_ap_json"),
368405
quote_auth_url when is_binary(quote_auth_url) <- ap_json["quoteAuthorization"],
369406
{:ok, authorization} <-
370407
ActivityPub.Federator.Fetcher.fetch_fresh_object_from_id(quote_auth_url,
@@ -405,11 +442,13 @@ defmodule Bonfire.Social.Quotes do
405442
{:error, :invalid}
406443
"""
407444
def verify_quote_authorization(quote_post, quoted_object \\ nil, authorization \\ nil) do
408-
quoted_object = quoted_object || get_first_quoted_object(quote_post)
445+
quoted_object =
446+
(quoted_object || get_first_quoted_object(quote_post))
447+
|> flood("quoted_object for verification")
409448

410449
case authorization || fetch_fresh_quote_authorization(quote_post, quoted_object) do
411450
{:ok, authorization} ->
412-
case ActivityPub.Object.deleted?(authorization) do
451+
case ActivityPub.Object.is_deleted?(authorization) do
413452
true ->
414453
{:not_authorized, "Quote authorization was revoked"}
415454

@@ -455,18 +494,31 @@ defmodule Bonfire.Social.Quotes do
455494
{:ok, quoted_actor} <- ActivityPub.Actor.get_cached(pointer: quoted_creator_id) do
456495
cond do
457496
# Check that authorization references correct objects
458-
auth_data["object"] != quote_ap_object.data["id"] ->
497+
auth_data["interactingObject"] != quote_ap_object.data["id"] ->
498+
error(
499+
auth_data["interactingObject"],
500+
"authorization object does not match quote post ID"
501+
)
502+
459503
error(quote_ap_object.data["id"], "Quote post ID does not match authorization object")
460504

461-
auth_data["context"] != quoted_ap_object.data["id"] ->
505+
auth_data["interactionTarget"] != quoted_ap_object.data["id"] ->
506+
error(
507+
auth_data["interactionTarget"],
508+
"authorization target does not match quote post ID"
509+
)
510+
462511
error(
463512
quoted_ap_object.data["id"],
464-
"Quoted object ID does not match authorization context"
513+
"Quoted object ID does not match authorization target"
465514
)
466515

467516
# Check that authorization is signed by quoted object's creator
468-
auth_data["actor"] != quoted_actor.ap_id ->
469-
error(auth_data["actor"], "Authorization actor does not match quoted object's creator")
517+
auth_data["attributedTo"] != quoted_actor.ap_id ->
518+
error(
519+
auth_data["attributedTo"],
520+
"Authorization actor does not match quoted object's creator"
521+
)
470522

471523
true ->
472524
{:ok, :valid}

lib/requests.ex

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -160,10 +160,10 @@ defmodule Bonfire.Social.Requests do
160160
iex> requested(request, current_user: me)
161161
{:ok, request}
162162
"""
163-
defp requested(request, opts \\ [])
164-
defp requested(%Request{id: _} = request, _opts), do: {:ok, request}
163+
def requested(request, opts \\ [])
164+
def requested(%Request{id: _} = request, _opts), do: {:ok, request}
165165

166-
defp requested(request, opts),
166+
def requested(request, opts),
167167
do:
168168
get(
169169
[
@@ -487,7 +487,12 @@ defmodule Bonfire.Social.Requests do
487487
ap_publish_activity(subject, {:accept_to, accept_to}, ap_object)
488488
else
489489
e ->
490-
err(e, "Could not find the request to accept #{request_id}")
490+
warn(
491+
request,
492+
"Could not find AP object for this request to accept, assuming it's local-only"
493+
)
494+
495+
:ok
491496
end
492497
end
493498

0 commit comments

Comments
 (0)