From a16da387d251edc4d1bae949146c807d217cee1f Mon Sep 17 00:00:00 2001 From: Roger Braun Date: Sat, 29 Apr 2017 21:13:21 +0200 Subject: [PATCH] Handle full incoming feeds. --- lib/pleroma/web/ostatus/ostatus.ex | 34 +++++++------ test/fixtures/ostatus_incoming_post.xml | 57 ++++++++++++++++++++++ test/web/ostatus/ostatus_test.exs | 17 +++++-- test/web/websub/websub_controller_test.exs | 2 + 4 files changed, 92 insertions(+), 18 deletions(-) create mode 100644 test/fixtures/ostatus_incoming_post.xml diff --git a/lib/pleroma/web/ostatus/ostatus.ex b/lib/pleroma/web/ostatus/ostatus.ex index 59c5d8e9e..9f85d971a 100644 --- a/lib/pleroma/web/ostatus/ostatus.ex +++ b/lib/pleroma/web/ostatus/ostatus.ex @@ -21,26 +21,32 @@ defmodule Pleroma.Web.OStatus do def handle_incoming(xml_string) do doc = parse_document(xml_string) + entries = :xmerl_xpath.string('//entry', doc) - {:xmlObj, :string, object_type } = :xmerl_xpath.string('string(/entry/activity:object-type[1])', doc) + activities = Enum.map(entries, fn (entry) -> + {:xmlObj, :string, object_type } = :xmerl_xpath.string('string(/entry/activity:object-type[1])', entry) - case object_type do - 'http://activitystrea.ms/schema/1.0/note' -> - handle_note(doc) - _ -> - Logger.error("Couldn't parse incoming document") - end + case object_type do + 'http://activitystrea.ms/schema/1.0/note' -> + {:ok, activity} = handle_note(entry, doc) + activity + _ -> + Logger.error("Couldn't parse incoming document") + nil + end + end) + {:ok, activities} end # TODO # wire up replies - def handle_note(doc) do - content_html = string_from_xpath("/entry/content[1]", doc) + def handle_note(entry, doc \\ nil) do + content_html = string_from_xpath("/entry/content[1]", entry) - uri = string_from_xpath("/entry/author/uri[1]", doc) + uri = string_from_xpath("/entry/author/uri[1]", entry) || string_from_xpath("/feed/author/uri[1]", doc) {:ok, actor} = find_or_make_user(uri) - context = string_from_xpath("/entry/ostatus:conversation[1]", doc) |> String.trim + context = string_from_xpath("/entry/ostatus:conversation[1]", entry) |> String.trim context = if String.length(context) > 0 do context else @@ -51,12 +57,12 @@ defmodule Pleroma.Web.OStatus do "https://www.w3.org/ns/activitystreams#Public" ] - mentions = :xmerl_xpath.string('/entry/link[@rel="mentioned" and @ostatus:object-type="http://activitystrea.ms/schema/1.0/person"]', doc) + mentions = :xmerl_xpath.string('/entry/link[@rel="mentioned" and @ostatus:object-type="http://activitystrea.ms/schema/1.0/person"]', entry) |> Enum.map(fn(person) -> string_from_xpath("@href", person) end) to = to ++ mentions - date = string_from_xpath("/entry/published", doc) + date = string_from_xpath("/entry/published", entry) object = %{ "type" => "Note", @@ -67,7 +73,7 @@ defmodule Pleroma.Web.OStatus do "actor" => actor.ap_id } - inReplyTo = string_from_xpath("/entry/thr:in-reply-to[1]/@href", doc) + inReplyTo = string_from_xpath("/entry/thr:in-reply-to[1]/@href", entry) object = if inReplyTo do Map.put(object, "inReplyTo", inReplyTo) diff --git a/test/fixtures/ostatus_incoming_post.xml b/test/fixtures/ostatus_incoming_post.xml new file mode 100644 index 000000000..7967e1b32 --- /dev/null +++ b/test/fixtures/ostatus_incoming_post.xml @@ -0,0 +1,57 @@ + + + GNU social + https://social.heldscal.la/api/statuses/user_timeline/23211.atom + lambadalambda timeline + Updates from lambadalambda on social.heldscal.la! + https://social.heldscal.la/avatar/23211-96-20170416114255.jpeg + 2017-04-29T18:25:38+00:00 + + http://activitystrea.ms/schema/1.0/person + https://social.heldscal.la/user/23211 + lambadalambda + Call me Deacon Blues. + + + + + + lambadalambda + Constance Variable + Call me Deacon Blues. + + Berlin + + + homepage + https://heldscal.la + true + + + + + + + + + + + + + http://activitystrea.ms/schema/1.0/note + tag:social.heldscal.la,2017-04-29:noticeId=1967725:objectType=note + New note by lambadalambda + Will it blend? + + + http://activitystrea.ms/schema/1.0/post + 2017-04-29T18:25:38+00:00 + 2017-04-29T18:25:38+00:00 + + tag:social.heldscal.la,2017-04-29:objectType=thread:nonce=3f3a9dd83acc4e35 + + + + + + diff --git a/test/web/ostatus/ostatus_test.exs b/test/web/ostatus/ostatus_test.exs index cc0975bb5..1e747c728 100644 --- a/test/web/ostatus/ostatus_test.exs +++ b/test/web/ostatus/ostatus_test.exs @@ -1,11 +1,10 @@ defmodule Pleroma.Web.OStatusTest do use Pleroma.DataCase alias Pleroma.Web.OStatus - alias Pleroma.Web.XML - test "handle incoming notes" do + test "handle incoming note - GS, Salmon" do incoming = File.read!("test/fixtures/incoming_note_activity.xml") - {:ok, activity} = OStatus.handle_incoming(incoming) + {:ok, [activity]} = OStatus.handle_incoming(incoming) assert activity.data["type"] == "Create" assert activity.data["object"]["type"] == "Note" @@ -14,9 +13,19 @@ defmodule Pleroma.Web.OStatusTest do assert "http://pleroma.example.org:4000/users/lain3" in activity.data["to"] end + test "handle incoming notes - GS, subscription" do + incoming = File.read!("test/fixtures/ostatus_incoming_post.xml") + {:ok, [activity]} = OStatus.handle_incoming(incoming) + + assert activity.data["type"] == "Create" + assert activity.data["object"]["type"] == "Note" + assert activity.data["object"]["actor"] == "https://social.heldscal.la/user/23211" + assert activity.data["object"]["content"] == "Will it blend?" + end + test "handle incoming replies" do incoming = File.read!("test/fixtures/incoming_note_activity_answer.xml") - {:ok, activity} = OStatus.handle_incoming(incoming) + {:ok, [activity]} = OStatus.handle_incoming(incoming) assert activity.data["type"] == "Create" assert activity.data["object"]["type"] == "Note" diff --git a/test/web/websub/websub_controller_test.exs b/test/web/websub/websub_controller_test.exs index 521bbb9aa..8f68248a4 100644 --- a/test/web/websub/websub_controller_test.exs +++ b/test/web/websub/websub_controller_test.exs @@ -41,6 +41,8 @@ defmodule Pleroma.Web.Websub.WebsubControllerTest do assert response(conn, 200) == "some challenge" assert websub.state == "accepted" + + # TODO valid_until end test "handles incoming feed updates", %{conn: conn} do