From 018a1a390fdb72652c615c28ac36f1b9a6a84d82 Mon Sep 17 00:00:00 2001 From: Roger Braun Date: Tue, 2 May 2017 21:31:01 +0200 Subject: [PATCH] Use inReplyTo to find context. --- lib/pleroma/web/ostatus/ostatus.ex | 18 ++++++++------ test/fixtures/incoming_reply_mastodon.xml | 29 +++++++++++++++++++++++ test/web/ostatus/ostatus_test.exs | 17 +++++++++++++ 3 files changed, 57 insertions(+), 7 deletions(-) create mode 100644 test/fixtures/incoming_reply_mastodon.xml diff --git a/lib/pleroma/web/ostatus/ostatus.ex b/lib/pleroma/web/ostatus/ostatus.ex index 340228dcf..7aa1ac4ac 100644 --- a/lib/pleroma/web/ostatus/ostatus.ex +++ b/lib/pleroma/web/ostatus/ostatus.ex @@ -44,13 +44,19 @@ def handle_note(entry, doc \\ nil) do [author] = :xmerl_xpath.string('//author[1]', doc) {:ok, actor} = find_make_or_update_user(author) + inReplyTo = string_from_xpath("/entry/thr:in-reply-to[1]/@ref", entry) context = (string_from_xpath("/entry/ostatus:conversation[1]", entry) || "") |> String.trim - context = if String.length(context) > 0 do - context - else - ActivityPub.generate_context_id - end + + context = with %{data: %{"context" => context}} <- Object.get_cached_by_ap_id(inReplyTo) do + context + else _e -> + if String.length(context) > 0 do + context + else + ActivityPub.generate_context_id + end + end to = [ "https://www.w3.org/ns/activitystreams#Public" @@ -74,8 +80,6 @@ def handle_note(entry, doc \\ nil) do "actor" => actor.ap_id } - inReplyTo = string_from_xpath("/entry/thr:in-reply-to[1]/@ref", entry) - object = if inReplyTo do Map.put(object, "inReplyTo", inReplyTo) else diff --git a/test/fixtures/incoming_reply_mastodon.xml b/test/fixtures/incoming_reply_mastodon.xml new file mode 100644 index 000000000..8ee1186cc --- /dev/null +++ b/test/fixtures/incoming_reply_mastodon.xml @@ -0,0 +1,29 @@ + + + tag:mastodon.social,2017-05-02:objectId=4901603:objectType=Status + 2017-05-02T18:33:06Z + 2017-05-02T18:33:06Z + New status by lambadalambda + + https://mastodon.social/users/lambadalambda + http://activitystrea.ms/schema/1.0/person + https://mastodon.social/users/lambadalambda + lambadalambda + lambadalambda@mastodon.social + + + + lambadalambda + Critical Value + public + + http://activitystrea.ms/schema/1.0/comment + http://activitystrea.ms/schema/1.0/post + <p><span class="h-card"><a href="https://pleroma.soykaf.com/users/lain" class="u-url mention">@<span>lain</span></a></span> hey</p> + + + public + + + + diff --git a/test/web/ostatus/ostatus_test.exs b/test/web/ostatus/ostatus_test.exs index 1674edbd5..e39952807 100644 --- a/test/web/ostatus/ostatus_test.exs +++ b/test/web/ostatus/ostatus_test.exs @@ -2,6 +2,7 @@ defmodule Pleroma.Web.OStatusTest do use Pleroma.DataCase alias Pleroma.Web.OStatus alias Pleroma.Web.XML + alias Pleroma.{Object, Repo} test "don't insert create notes twice" do incoming = File.read!("test/fixtures/incoming_note_activity.xml") @@ -32,6 +33,22 @@ test "handle incoming notes - GS, subscription" do assert activity.data["object"]["content"] == "Will it blend?" end + test "handle incoming notes - Mastodon, salmon, reply" do + # It uses the context of the replied to object + Repo.insert!(%Object{ + data: %{ + "id" => "https://pleroma.soykaf.com/objects/c237d966-ac75-4fe3-a87a-d89d71a3a7a4", + "context" => "2hu" + }}) + incoming = File.read!("test/fixtures/incoming_reply_mastodon.xml") + {:ok, [activity]} = OStatus.handle_incoming(incoming) + + assert activity.data["type"] == "Create" + assert activity.data["object"]["type"] == "Note" + assert activity.data["object"]["actor"] == "https://mastodon.social/users/lambadalambda" + assert activity.data["context"] == "2hu" + end + test "handle incoming notes - GS, subscription, reply" do incoming = File.read!("test/fixtures/ostatus_incoming_reply.xml") {:ok, [activity]} = OStatus.handle_incoming(incoming)