Merge branch 'incoming-id-fixes' into 'develop'

Assorted transmogrifier fixes

See merge request pleroma/pleroma!1831
This commit is contained in:
lain 2019-10-13 15:37:49 +00:00
commit e3b4a3e96b
5 changed files with 49 additions and 34 deletions

View file

@ -269,22 +269,21 @@ def listen(%{to: to, actor: actor, context: context, object: object} = params) d
end
end
def accept(%{to: to, actor: actor, object: object} = params) do
# only accept false as false value
local = !(params[:local] == false)
with data <- %{"to" => to, "type" => "Accept", "actor" => actor.ap_id, "object" => object},
{:ok, activity} <- insert(data, local),
:ok <- maybe_federate(activity) do
{:ok, activity}
end
def accept(params) do
accept_or_reject("Accept", params)
end
def reject(%{to: to, actor: actor, object: object} = params) do
# only accept false as false value
local = !(params[:local] == false)
def reject(params) do
accept_or_reject("Reject", params)
end
with data <- %{"to" => to, "type" => "Reject", "actor" => actor.ap_id, "object" => object},
def accept_or_reject(type, %{to: to, actor: actor, object: object} = params) do
local = Map.get(params, :local, true)
activity_id = Map.get(params, :activity_id, nil)
with data <-
%{"to" => to, "type" => type, "actor" => actor.ap_id, "object" => object}
|> Utils.maybe_put("id", activity_id),
{:ok, activity} <- insert(data, local),
:ok <- maybe_federate(activity) do
{:ok, activity}
@ -409,18 +408,24 @@ def delete(%User{ap_id: ap_id, follower_address: follower_address} = user) do
end
end
def delete(%Object{data: %{"id" => id, "actor" => actor}} = object, local \\ true) do
def delete(%Object{data: %{"id" => id, "actor" => actor}} = object, options \\ []) do
local = Keyword.get(options, :local, true)
activity_id = Keyword.get(options, :activity_id, nil)
actor = Keyword.get(options, :actor, actor)
user = User.get_cached_by_ap_id(actor)
to = (object.data["to"] || []) ++ (object.data["cc"] || [])
with {:ok, object, activity} <- Object.delete(object),
data <- %{
"type" => "Delete",
"actor" => actor,
"object" => id,
"to" => to,
"deleted_activity_id" => activity && activity.id
},
data <-
%{
"type" => "Delete",
"actor" => actor,
"object" => id,
"to" => to,
"deleted_activity_id" => activity && activity.id
}
|> maybe_put("id", activity_id),
{:ok, activity} <- insert(data, local, false),
stream_out_participations(object, user),
_ <- decrease_replies_count_if_reply(object),

View file

@ -514,7 +514,7 @@ def handle_incoming(
end
def handle_incoming(
%{"type" => "Accept", "object" => follow_object, "actor" => _actor, "id" => _id} = data,
%{"type" => "Accept", "object" => follow_object, "actor" => _actor, "id" => id} = data,
_options
) do
with actor <- Containment.get_actor(data),
@ -528,7 +528,8 @@ def handle_incoming(
type: "Accept",
actor: followed,
object: follow_activity.data["id"],
local: false
local: false,
activity_id: id
})
else
_e -> :error
@ -536,7 +537,7 @@ def handle_incoming(
end
def handle_incoming(
%{"type" => "Reject", "object" => follow_object, "actor" => _actor, "id" => _id} = data,
%{"type" => "Reject", "object" => follow_object, "actor" => _actor, "id" => id} = data,
_options
) do
with actor <- Containment.get_actor(data),
@ -550,7 +551,8 @@ def handle_incoming(
type: "Reject",
actor: followed,
object: follow_activity.data["id"],
local: false
local: false,
activity_id: id
}) do
User.unfollow(follower, followed)
@ -637,7 +639,7 @@ def handle_incoming(
# an error or a tombstone. This would allow us to verify that a deletion actually took
# place.
def handle_incoming(
%{"type" => "Delete", "object" => object_id, "actor" => actor, "id" => _id} = data,
%{"type" => "Delete", "object" => object_id, "actor" => actor, "id" => id} = data,
_options
) do
object_id = Utils.get_ap_id(object_id)
@ -646,7 +648,8 @@ def handle_incoming(
{:ok, %User{} = actor} <- User.get_or_fetch_by_ap_id(actor),
{:ok, object} <- get_obj_helper(object_id),
:ok <- Containment.contain_origin(actor.ap_id, object.data),
{:ok, activity} <- ActivityPub.delete(object, false) do
{:ok, activity} <-
ActivityPub.delete(object, local: false, activity_id: id, actor: actor.ap_id) do
{:ok, activity}
else
nil ->

View file

@ -11,7 +11,7 @@ defmodule Pleroma.Web.OStatus.DeleteHandler do
def handle_delete(entry, _doc \\ nil) do
with id <- XML.string_from_xpath("//id", entry),
%Object{} = object <- Object.normalize(id),
{:ok, delete} <- ActivityPub.delete(object, false) do
{:ok, delete} <- ActivityPub.delete(object, local: false) do
delete
end
end

View file

@ -682,6 +682,7 @@ test "it works for incoming update activities which lock the account" do
test "it works for incoming deletes" do
activity = insert(:note_activity)
deleting_user = insert(:user)
data =
File.read!("test/fixtures/mastodon-delete.json")
@ -694,11 +695,14 @@ test "it works for incoming deletes" do
data =
data
|> Map.put("object", object)
|> Map.put("actor", activity.data["actor"])
|> Map.put("actor", deleting_user.ap_id)
{:ok, %Activity{local: false}} = Transmogrifier.handle_incoming(data)
{:ok, %Activity{actor: actor, local: false, data: %{"id" => id}}} =
Transmogrifier.handle_incoming(data)
assert id == data["id"]
refute Activity.get_by_id(activity.id)
assert actor == deleting_user.ap_id
end
test "it fails for incoming deletes with spoofed origin" do
@ -905,6 +909,8 @@ test "it works for incoming accepts which were pre-accepted" do
assert activity.data["object"] == follow_activity.data["id"]
assert activity.data["id"] == accept_data["id"]
follower = User.get_cached_by_id(follower.id)
assert User.following?(follower, followed) == true
@ -1009,6 +1015,7 @@ test "it works for incoming rejects which are orphaned" do
{:ok, activity} = Transmogrifier.handle_incoming(reject_data)
refute activity.local
assert activity.data["id"] == reject_data["id"]
follower = User.get_cached_by_id(follower.id)

View file

@ -40,9 +40,9 @@ test "it returns empty result if user or status search return undefined error",
test "search", %{conn: conn} do
user = insert(:user)
user_two = insert(:user, %{nickname: "shp@shitposter.club"})
user_three = insert(:user, %{nickname: "shp@heldscal.la", name: "I love 2hu 天子"})
user_three = insert(:user, %{nickname: "shp@heldscal.la", name: "I love 2hu"})
{:ok, activity} = CommonAPI.post(user, %{"status" => "This is about 2hu private"})
{:ok, activity} = CommonAPI.post(user, %{"status" => "This is about 2hu private 天子"})
{:ok, _activity} =
CommonAPI.post(user, %{
@ -70,8 +70,8 @@ test "search", %{conn: conn} do
get(conn, "/api/v2/search", %{"q" => "天子"})
|> json_response(200)
[account] == results["accounts"]
assert account["id"] == to_string(user_three.id)
[status] = results["statuses"]
assert status["id"] == to_string(activity.id)
end
end