Parse incoming retweets.

This commit is contained in:
Roger Braun 2017-05-04 18:42:29 +02:00
parent 5d9f3df714
commit c85998ab8a
5 changed files with 218 additions and 15 deletions

View File

@ -177,7 +177,7 @@ defmodule Pleroma.Web.ActivityPub.ActivityPub do
|> Enum.reverse
end
def announce(%User{ap_id: ap_id} = user, %Object{data: %{"id" => id}} = object) do
def announce(%User{ap_id: ap_id} = user, %Object{data: %{"id" => id}} = object, local \\ true) do
data = %{
"type" => "Announce",
"actor" => ap_id,
@ -186,7 +186,7 @@ defmodule Pleroma.Web.ActivityPub.ActivityPub do
"context" => object.data["context"]
}
{:ok, activity} = insert(data)
{:ok, activity} = insert(data, local)
announcements = [ap_id | (object.data["announcements"] || [])] |> Enum.uniq

View File

@ -25,20 +25,44 @@ defmodule Pleroma.Web.OStatus do
activities = Enum.map(entries, fn (entry) ->
{:xmlObj, :string, object_type } = :xmerl_xpath.string('string(/entry/activity:object-type[1])', entry)
{:xmlObj, :string, verb } = :xmerl_xpath.string('string(/entry/activity:verb[1])', entry)
case object_type do
'http://activitystrea.ms/schema/1.0/note' ->
with {:ok, activity} <- handle_note(entry, doc), do: activity
'http://activitystrea.ms/schema/1.0/comment' ->
with {:ok, activity} <- handle_note(entry, doc), do: activity
case verb do
'http://activitystrea.ms/schema/1.0/share' ->
with {:ok, activity, retweeted_activity} <- handle_share(entry, doc), do: [activity, retweeted_activity]
_ ->
Logger.error("Couldn't parse incoming document")
nil
case object_type do
'http://activitystrea.ms/schema/1.0/note' ->
with {:ok, activity} <- handle_note(entry, doc), do: activity
'http://activitystrea.ms/schema/1.0/comment' ->
with {:ok, activity} <- handle_note(entry, doc), do: activity
_ ->
Logger.error("Couldn't parse incoming document")
nil
end
end
end)
{:ok, activities}
end
def make_share(entry, doc, retweeted_activity) do
with {:ok, actor} <- find_make_or_update_user(doc),
%Object{} = object <- Object.get_cached_by_ap_id(retweeted_activity.data["object"]["id"]),
{:ok, activity, object} = ActivityPub.announce(actor, object, false) do
{:ok, activity}
end
end
def handle_share(entry, doc) do
with [object] <- :xmerl_xpath.string('/entry/activity:object', entry),
{:ok, retweeted_activity} <- handle_note(object, object),
{:ok, activity} <- make_share(entry, doc, retweeted_activity) do
{:ok, activity, retweeted_activity}
else
e -> {:error, e}
end
end
def get_attachments(entry) do
:xmerl_xpath.string('/entry/link[@rel="enclosure"]', entry)
|> Enum.map(fn (enclosure) ->
@ -58,13 +82,13 @@ defmodule Pleroma.Web.OStatus do
end
def handle_note(entry, doc \\ nil) do
content_html = string_from_xpath("/entry/content[1]", entry)
content_html = string_from_xpath("//content[1]", entry)
[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)
inReplyTo = string_from_xpath("//thr:in-reply-to[1]/@ref", entry)
context = (string_from_xpath("/entry/ostatus:conversation[1]", entry) || "") |> String.trim
context = (string_from_xpath("//ostatus:conversation[1]", entry) || "") |> String.trim
attachments = get_attachments(entry)
@ -82,13 +106,13 @@ 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"]', entry)
mentions = :xmerl_xpath.string('//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", entry)
id = string_from_xpath("/entry/id", entry)
date = string_from_xpath("//published", entry)
id = string_from_xpath("//id", entry)
object = %{
"id" => id,

99
test/fixtures/share-gs.xml vendored Normal file
View File

@ -0,0 +1,99 @@
<?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-05-03T08:05:41+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>
<id>tag:social.heldscal.la,2017-05-03:noticeId=2028428:objectType=note</id>
<title>lambadalambda repeated a notice by lain</title>
<content type="html">RT @&lt;a href=&quot;https://pleroma.soykaf.com/users/lain&quot; class=&quot;h-card u-url p-nickname mention&quot; title=&quot;Lain Iwakura&quot;&gt;lain&lt;/a&gt; Added returning the entries as xml... let's see if the mastodon hammering stops now.</content>
<link rel="alternate" type="text/html" href="https://social.heldscal.la/notice/2028428"/>
<activity:verb>http://activitystrea.ms/schema/1.0/share</activity:verb>
<published>2017-05-03T08:05:41+00:00</published>
<updated>2017-05-03T08:05:41+00:00</updated>
<activity:object>
<activity:object-type>http://activitystrea.ms/schema/1.0/activity</activity:object-type>
<id>https://pleroma.soykaf.com/objects/4c1bda26-902e-4525-9fcd-b9fd44925193</id>
<title></title>
<content type="html">Added returning the entries as xml... let's see if the mastodon hammering stops now.</content>
<link rel="alternate" type="text/html" href="https://pleroma.soykaf.com/objects/4c1bda26-902e-4525-9fcd-b9fd44925193"/>
<activity:verb>http://activitystrea.ms/schema/1.0/post</activity:verb>
<published>2017-05-03T08:04:44+00:00</published>
<updated>2017-05-03T08:04:44+00:00</updated>
<author>
<activity:object-type>http://activitystrea.ms/schema/1.0/person</activity:object-type>
<uri>https://pleroma.soykaf.com/users/lain</uri>
<name>lain</name>
<summary>Test account</summary>
<link rel="alternate" type="text/html" href="https://pleroma.soykaf.com/users/lain"/>
<link rel="avatar" type="image/jpeg" media:width="250" media:height="202" href="https://social.heldscal.la/avatar/43188-original-20170429171039.jpeg"/>
<link rel="avatar" type="image/jpeg" media:width="96" media:height="96" href="https://social.heldscal.la/avatar/43188-96-20170429172422.jpeg"/>
<link rel="avatar" type="image/jpeg" media:width="48" media:height="48" href="https://social.heldscal.la/avatar/43188-48-20170429172422.jpeg"/>
<link rel="avatar" type="image/jpeg" media:width="24" media:height="24" href="https://social.heldscal.la/avatar/43188-24-20170429181411.jpeg"/>
<poco:preferredUsername>lain</poco:preferredUsername>
<poco:displayName>Lain Iwakura</poco:displayName>
<poco:note>Test account</poco:note>
<statusnet:profile_info local_id="43188"></statusnet:profile_info>
</author>
<activity:object>
<activity:object-type>http://activitystrea.ms/schema/1.0/note</activity:object-type>
<id>https://pleroma.soykaf.com/objects/4c1bda26-902e-4525-9fcd-b9fd44925193</id>
<title>New note by lain</title>
<content type="html">Added returning the entries as xml... let's see if the mastodon hammering stops now.</content>
<link rel="alternate" type="text/html" href="https://pleroma.soykaf.com/objects/4c1bda26-902e-4525-9fcd-b9fd44925193"/>
<status_net notice_id="2028424"></status_net>
</activity:object>
<link rel="ostatus:conversation" href="https://social.heldscal.la/conversation/1042737"/>
<ostatus:conversation href="https://social.heldscal.la/conversation/1042737" local_id="1042737" ref="https://pleroma.soykaf.com/contexts/ede39a2b-7cf3-4fa4-8ccd-cb97431bcc22">https://pleroma.soykaf.com/contexts/ede39a2b-7cf3-4fa4-8ccd-cb97431bcc22</ostatus:conversation>
<link rel="mentioned" ostatus:object-type="http://activitystrea.ms/schema/1.0/collection" href="http://activityschema.org/collection/public"/>
<source>
<id>https://pleroma.soykaf.com/users/lain/feed.atom</id>
<title>Lain Iwakura</title>
<link rel="alternate" type="text/html" href="https://pleroma.soykaf.com/users/lain"/>
<link rel="self" type="application/atom+xml" href="https://pleroma.soykaf.com/users/lain/feed.atom"/>
<icon>https://social.heldscal.la/avatar/43188-96-20170429172422.jpeg</icon>
<updated>2017-05-03T08:04:44+00:00</updated>
</source>
</activity:object>
<link rel="ostatus:conversation" href="https://social.heldscal.la/conversation/1042737"/>
<ostatus:conversation href="https://social.heldscal.la/conversation/1042737" local_id="1042737" ref="https://pleroma.soykaf.com/contexts/ede39a2b-7cf3-4fa4-8ccd-cb97431bcc22">https://pleroma.soykaf.com/contexts/ede39a2b-7cf3-4fa4-8ccd-cb97431bcc22</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/2028428.atom"/>
<link rel="edit" type="application/atom+xml" href="https://social.heldscal.la/api/statuses/show/2028428.atom"/>
<statusnet:notice_info local_id="2028428" source="api" repeat_of="2028424"></statusnet:notice_info>
</entry>
</feed>

54
test/fixtures/share.xml vendored Normal file
View File

@ -0,0 +1,54 @@
<?xml version="1.0"?>
<entry xmlns="http://www.w3.org/2005/Atom" xmlns:thr="http://purl.org/syndication/thread/1.0" xmlns:activity="http://activitystrea.ms/spec/1.0/" xmlns:poco="http://portablecontacts.net/spec/1.0" xmlns:media="http://purl.org/syndication/atommedia" xmlns:ostatus="http://ostatus.org/schema/1.0" xmlns:mastodon="http://mastodon.social/schema/1.0">
<id>tag:mastodon.social,2017-05-03:objectId=4934452:objectType=Status</id>
<published>2017-05-03T08:21:09Z</published>
<updated>2017-05-03T08:21:09Z</updated>
<title>lambadalambda shared a status by lain@pleroma.soykaf.com</title>
<author>
<id>https://mastodon.social/users/lambadalambda</id>
<activity:object-type>http://activitystrea.ms/schema/1.0/person</activity:object-type>
<uri>https://mastodon.social/users/lambadalambda</uri>
<name>lambadalambda</name>
<email>lambadalambda@mastodon.social</email>
<link rel="alternate" type="text/html" href="https://mastodon.social/@lambadalambda"/>
<link rel="avatar" type="image/gif" media:width="120" media:height="120" href="https://files.mastodon.social/accounts/avatars/000/000/264/original/1429214160519.gif"/>
<link rel="header" type="" media:width="700" media:height="335" href="/headers/original/missing.png"/>
<poco:preferredUsername>lambadalambda</poco:preferredUsername>
<poco:displayName>Critical Value</poco:displayName>
<mastodon:scope>public</mastodon:scope>
</author>
<activity:object-type>http://activitystrea.ms/schema/1.0/activity</activity:object-type>
<activity:verb>http://activitystrea.ms/schema/1.0/share</activity:verb>
<activity:object>
<id>https://pleroma.soykaf.com/objects/4c1bda26-902e-4525-9fcd-b9fd44925193</id>
<published>2017-05-03T08:04:44Z</published>
<updated>2017-05-03T08:05:52Z</updated>
<title>New status by lain@pleroma.soykaf.com</title>
<author>
<id>https://pleroma.soykaf.com/users/lain</id>
<activity:object-type>http://activitystrea.ms/schema/1.0/person</activity:object-type>
<uri>https://pleroma.soykaf.com/users/lain</uri>
<name>lain</name>
<email>lain@pleroma.soykaf.com</email>
<summary type="html">Test account</summary>
<link rel="alternate" type="text/html" href="https://pleroma.soykaf.com/users/lain"/>
<link rel="avatar" type="image/jpeg" media:width="120" media:height="120" href="https://files.mastodon.social/accounts/avatars/000/125/902/original/6B3AFC74ACA841B24CFB94DB9044C84EDE6AFF31C71718B023D413DAED09A68E.jpeg"/>
<link rel="header" type="" media:width="700" media:height="335" href="/headers/original/missing.png"/>
<poco:preferredUsername>lain</poco:preferredUsername>
<poco:displayName>Lain Iwakura</poco:displayName>
<poco:note>Test account</poco:note>
<mastodon:scope>public</mastodon:scope>
</author>
<activity:object-type>http://activitystrea.ms/schema/1.0/note</activity:object-type>
<activity:verb>http://activitystrea.ms/schema/1.0/post</activity:verb>
<content type="html" xml:lang="en">Added returning the entries as xml... let's see if the mastodon hammering stops now.</content>
<link rel="mentioned" ostatus:object-type="http://activitystrea.ms/schema/1.0/collection" href="http://activityschema.org/collection/public"/>
<mastodon:scope>public</mastodon:scope>
<link rel="alternate" type="text/html" href=""/>
</activity:object>
<content type="html" xml:lang="en">Added returning the entries as xml... let's see if the mastodon hammering stops now.</content>
<link rel="mentioned" ostatus:object-type="http://activitystrea.ms/schema/1.0/collection" href="http://activityschema.org/collection/public"/>
<mastodon:scope>public</mastodon:scope>
<link rel="alternate" type="text/html" href="https://mastodon.social/users/lambadalambda/updates/2232660"/>
<link rel="self" type="application/atom+xml" href="https://mastodon.social/users/lambadalambda/updates/2232660.atom"/>
</entry>

View File

@ -70,6 +70,32 @@ defmodule Pleroma.Web.OStatusTest do
assert activity.data["object"]["inReplyTo"] == "tag:gs.archae.me,2017-04-30:noticeId=778260:objectType=note"
end
test "handle incoming retweets - GS, subscription" do
incoming = File.read!("test/fixtures/share-gs.xml")
{:ok, [[activity, retweeted_activity]]} = OStatus.handle_incoming(incoming)
assert activity.data["type"] == "Announce"
assert activity.data["actor"] == "https://social.heldscal.la/user/23211"
assert activity.data["object"] == retweeted_activity.data["object"]["id"]
refute activity.local
assert retweeted_activity.data["type"] == "Create"
assert retweeted_activity.data["actor"] == "https://pleroma.soykaf.com/users/lain"
refute retweeted_activity.local
end
test "handle incoming retweets - Mastodon, salmon" do
incoming = File.read!("test/fixtures/share.xml")
{:ok, [[activity, retweeted_activity]]} = OStatus.handle_incoming(incoming)
assert activity.data["type"] == "Announce"
assert activity.data["actor"] == "https://mastodon.social/users/lambadalambda"
assert activity.data["object"] == retweeted_activity.data["object"]["id"]
refute activity.local
assert retweeted_activity.data["type"] == "Create"
assert retweeted_activity.data["actor"] == "https://pleroma.soykaf.com/users/lain"
refute retweeted_activity.local
end
test "handle incoming replies" do
incoming = File.read!("test/fixtures/incoming_note_activity_answer.xml")
{:ok, [activity]} = OStatus.handle_incoming(incoming)