Merge branch 'bugfix/announce-race-condition' into 'develop'
authorrinpatch <rinpatch@sdf.org>
Fri, 24 Apr 2020 16:58:24 +0000 (16:58 +0000)
committerrinpatch <rinpatch@sdf.org>
Fri, 24 Apr 2020 16:58:24 +0000 (16:58 +0000)
Announcements: Prevent race condition.

See merge request pleroma/pleroma!2423

1  2 
lib/pleroma/web/activity_pub/activity_pub.ex

index 9ec31fb031b15fb4310aa60cdbd4e87b1e3bdeef,4cce4f13c1505c492202b9d4b7743f21411129b7..61a4960a0801501e66a70e7dbb3d69999c9f604a
@@@ -438,6 -438,7 +438,7 @@@ defmodule Pleroma.Web.ActivityPub.Activ
  
    defp do_announce(user, object, activity_id, local, public) do
      with true <- is_announceable?(object, user, public),
+          object <- Object.get_by_id(object.id),
           announce_data <- make_announce_data(user, object, activity_id, public),
           {:ok, activity} <- insert(announce_data, local),
           {:ok, object} <- add_announce_to_object(activity, object),
      )
    end
  
 +  defp restrict_replies(query, %{
 +         "reply_filtering_user" => user,
 +         "reply_visibility" => "self"
 +       }) do
 +    from(
 +      [activity, object] in query,
 +      where:
 +        fragment(
 +          "?->>'inReplyTo' is null OR ? = ANY(?)",
 +          object.data,
 +          ^user.ap_id,
 +          activity.recipients
 +        )
 +    )
 +  end
 +
 +  defp restrict_replies(query, %{
 +         "reply_filtering_user" => user,
 +         "reply_visibility" => "following"
 +       }) do
 +    from(
 +      [activity, object] in query,
 +      where:
 +        fragment(
 +          "?->>'inReplyTo' is null OR ? && array_remove(?, ?) OR ? = ?",
 +          object.data,
 +          ^[user.ap_id | User.get_cached_user_friends_ap_ids(user)],
 +          activity.recipients,
 +          activity.actor,
 +          activity.actor,
 +          ^user.ap_id
 +        )
 +    )
 +  end
 +
    defp restrict_replies(query, _), do: query
  
    defp restrict_reblogs(query, %{"exclude_reblogs" => val}) when val == "true" or val == "1" do
      |> maybe_set_thread_muted_field(opts)
      |> maybe_order(opts)
      |> restrict_recipients(recipients, opts["user"])
 +    |> restrict_replies(opts)
      |> restrict_tag(opts)
      |> restrict_tag_reject(opts)
      |> restrict_tag_all(opts)
      |> restrict_media(opts)
      |> restrict_visibility(opts)
      |> restrict_thread_visibility(opts, config)
 -    |> restrict_replies(opts)
      |> restrict_reblogs(opts)
      |> restrict_pinned(opts)
      |> restrict_muted_reblogs(restrict_muted_reblogs_opts)