Handle full incoming feeds.

This commit is contained in:
Roger Braun 2017-04-29 21:13:21 +02:00
parent 20015b4b67
commit a16da387d2
4 changed files with 92 additions and 18 deletions

View File

@ -21,26 +21,32 @@ def salmon_path(user) do
def handle_incoming(xml_string) do def handle_incoming(xml_string) do
doc = parse_document(xml_string) 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 case object_type do
'http://activitystrea.ms/schema/1.0/note' -> 'http://activitystrea.ms/schema/1.0/note' ->
handle_note(doc) {:ok, activity} = handle_note(entry, doc)
activity
_ -> _ ->
Logger.error("Couldn't parse incoming document") Logger.error("Couldn't parse incoming document")
nil
end end
end)
{:ok, activities}
end end
# TODO # TODO
# wire up replies # wire up replies
def handle_note(doc) do def handle_note(entry, doc \\ nil) do
content_html = string_from_xpath("/entry/content[1]", doc) 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) {: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 = if String.length(context) > 0 do
context context
else else
@ -51,12 +57,12 @@ def handle_note(doc) do
"https://www.w3.org/ns/activitystreams#Public" "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) |> Enum.map(fn(person) -> string_from_xpath("@href", person) end)
to = to ++ mentions to = to ++ mentions
date = string_from_xpath("/entry/published", doc) date = string_from_xpath("/entry/published", entry)
object = %{ object = %{
"type" => "Note", "type" => "Note",
@ -67,7 +73,7 @@ def handle_note(doc) do
"actor" => actor.ap_id "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 object = if inReplyTo do
Map.put(object, "inReplyTo", inReplyTo) Map.put(object, "inReplyTo", inReplyTo)

57
test/fixtures/ostatus_incoming_post.xml vendored Normal file
View File

@ -0,0 +1,57 @@
<?xml version="1.0" encoding="UTF-8"?>
<feed xml:lang="en-US" xmlns="http://www.w3.org/2005/Atom" xmlns:thr="http://purl.org/syndication/thread/1.0" xmlns:georss="http://www.georss.org/georss" xmlns:activity="http://activitystrea.ms/spec/1.0/" xmlns:media="http://purl.org/syndication/atommedia" xmlns:poco="http://portablecontacts.net/spec/1.0" xmlns:ostatus="http://ostatus.org/schema/1.0" xmlns:statusnet="http://status.net/schema/api/1/">
<generator uri="https://gnu.io/social" version="1.0.2-dev">GNU social</generator>
<id>https://social.heldscal.la/api/statuses/user_timeline/23211.atom</id>
<title>lambadalambda timeline</title>
<subtitle>Updates from lambadalambda on social.heldscal.la!</subtitle>
<logo>https://social.heldscal.la/avatar/23211-96-20170416114255.jpeg</logo>
<updated>2017-04-29T18:25:38+00:00</updated>
<author>
<activity:object-type>http://activitystrea.ms/schema/1.0/person</activity:object-type>
<uri>https://social.heldscal.la/user/23211</uri>
<name>lambadalambda</name>
<summary>Call me Deacon Blues.</summary>
<link rel="alternate" type="text/html" href="https://social.heldscal.la/lambadalambda"/>
<link rel="avatar" type="image/jpeg" media:width="236" media:height="236" href="https://social.heldscal.la/avatar/23211-original-20170416114255.jpeg"/>
<link rel="avatar" type="image/jpeg" media:width="96" media:height="96" href="https://social.heldscal.la/avatar/23211-96-20170416114255.jpeg"/>
<link rel="avatar" type="image/jpeg" media:width="48" media:height="48" href="https://social.heldscal.la/avatar/23211-48-20170416114255.jpeg"/>
<link rel="avatar" type="image/jpeg" media:width="24" media:height="24" href="https://social.heldscal.la/avatar/23211-24-20170416114257.jpeg"/>
<poco:preferredUsername>lambadalambda</poco:preferredUsername>
<poco:displayName>Constance Variable</poco:displayName>
<poco:note>Call me Deacon Blues.</poco:note>
<poco:address>
<poco:formatted>Berlin</poco:formatted>
</poco:address>
<poco:urls>
<poco:type>homepage</poco:type>
<poco:value>https://heldscal.la</poco:value>
<poco:primary>true</poco:primary>
</poco:urls>
<followers url="https://social.heldscal.la/lambadalambda/subscribers"></followers>
<statusnet:profile_info local_id="23211"></statusnet:profile_info>
</author>
<link href="https://social.heldscal.la/lambadalambda" rel="alternate" type="text/html"/>
<link href="https://social.heldscal.la/main/sup" rel="http://api.friendfeed.com/2008/03#sup" type="application/json"/>
<link href="https://social.heldscal.la/main/push/hub" rel="hub"/>
<link href="https://social.heldscal.la/main/salmon/user/23211" rel="salmon"/>
<link href="https://social.heldscal.la/main/salmon/user/23211" rel="http://salmon-protocol.org/ns/salmon-replies"/>
<link href="https://social.heldscal.la/main/salmon/user/23211" rel="http://salmon-protocol.org/ns/salmon-mention"/>
<link href="https://social.heldscal.la/api/statuses/user_timeline/23211.atom" rel="self" type="application/atom+xml"/>
<entry>
<activity:object-type>http://activitystrea.ms/schema/1.0/note</activity:object-type>
<id>tag:social.heldscal.la,2017-04-29:noticeId=1967725:objectType=note</id>
<title>New note by lambadalambda</title>
<content type="html">Will it blend?</content>
<link rel="alternate" type="text/html" href="https://social.heldscal.la/notice/1967725"/>
<status_net notice_id="1967725"></status_net>
<activity:verb>http://activitystrea.ms/schema/1.0/post</activity:verb>
<published>2017-04-29T18:25:38+00:00</published>
<updated>2017-04-29T18:25:38+00:00</updated>
<link rel="ostatus:conversation" href="https://social.heldscal.la/conversation/1007861"/>
<ostatus:conversation href="https://social.heldscal.la/conversation/1007861" local_id="1007861" ref="tag:social.heldscal.la,2017-04-29:objectType=thread:nonce=3f3a9dd83acc4e35">tag:social.heldscal.la,2017-04-29:objectType=thread:nonce=3f3a9dd83acc4e35</ostatus:conversation>
<link rel="mentioned" ostatus:object-type="http://activitystrea.ms/schema/1.0/collection" href="http://activityschema.org/collection/public"/>
<link rel="self" type="application/atom+xml" href="https://social.heldscal.la/api/statuses/show/1967725.atom"/>
<link rel="edit" type="application/atom+xml" href="https://social.heldscal.la/api/statuses/show/1967725.atom"/>
<statusnet:notice_info local_id="1967725" source="Pleroma FE"></statusnet:notice_info>
</entry>
</feed>

View File

@ -1,11 +1,10 @@
defmodule Pleroma.Web.OStatusTest do defmodule Pleroma.Web.OStatusTest do
use Pleroma.DataCase use Pleroma.DataCase
alias Pleroma.Web.OStatus 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") 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["type"] == "Create"
assert activity.data["object"]["type"] == "Note" assert activity.data["object"]["type"] == "Note"
@ -14,9 +13,19 @@ test "handle incoming notes" do
assert "http://pleroma.example.org:4000/users/lain3" in activity.data["to"] assert "http://pleroma.example.org:4000/users/lain3" in activity.data["to"]
end 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 test "handle incoming replies" do
incoming = File.read!("test/fixtures/incoming_note_activity_answer.xml") 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["type"] == "Create"
assert activity.data["object"]["type"] == "Note" assert activity.data["object"]["type"] == "Note"

View File

@ -41,6 +41,8 @@ test "websub subscription confirmation", %{conn: conn} do
assert response(conn, 200) == "some challenge" assert response(conn, 200) == "some challenge"
assert websub.state == "accepted" assert websub.state == "accepted"
# TODO valid_until
end end
test "handles incoming feed updates", %{conn: conn} do test "handles incoming feed updates", %{conn: conn} do