Commit dd12cf72 authored by lain's avatar lain
Browse files

Fix cws.

parent 932e0e87
......@@ -136,9 +136,13 @@ def following?(%User{} = follower, %User{} = followed) do
Enum.member?(follower.following, User.ap_followers(followed))
end
def get_by_ap_id(ap_id) do
Repo.get_by(User, ap_id: ap_id)
end
def get_cached_by_ap_id(ap_id) do
key = "ap_id:#{ap_id}"
Cachex.get!(:user_cache, key, fallback: fn(_) -> Repo.get_by(User, ap_id: ap_id) end)
Cachex.get!(:user_cache, key, fallback: fn(_) -> get_by_ap_id(ap_id) end)
end
def get_cached_by_nickname(nickname) do
......
......@@ -81,11 +81,11 @@ def make_favorite(entry, doc, favorited_activity) do
end
def get_or_try_fetching(entry) do
Logger.debug("Trying to fetch entry")
Logger.debug("Trying to get entry from db")
with id when not is_nil(id) <- string_from_xpath("//activity:object[1]/id", entry),
%Activity{} = activity <- Activity.get_create_activity_by_object_ap_id(id) do
{:ok, activity}
else _e ->
else e ->
Logger.debug("Couldn't get, will try to fetch")
with href when not is_nil(href) <- string_from_xpath("//activity:object[1]/link[@type=\"text/html\"]/@href", entry),
{:ok, [favorited_activity]} <- fetch_activity_from_html_url(href) do
......@@ -126,7 +126,7 @@ def get_content(entry) do
base_content = string_from_xpath("//content", entry)
with scope when not is_nil(scope) <- string_from_xpath("//mastodon:scope", entry),
cw when not is_nil(cw) <- string_from_xpath("//summary", entry) do
cw when not is_nil(cw) <- string_from_xpath("/*/summary", entry) do
"<span class='mastodon-cw'>#{cw}</span><br>#{base_content}"
else _e -> base_content
end
......@@ -297,7 +297,7 @@ def fetch_activity_from_html_url(url) do
with {:ok, %{body: body}} <- @httpoison.get(url, [], follow_redirect: true),
{:ok, atom_url} <- get_atom_url(body),
{:ok, %{status_code: code, body: body}} when code in 200..299 <- @httpoison.get(atom_url, [], follow_redirect: true) do
Logger.debug("Got #{url}, handling...")
Logger.debug("Got document from #{url}, handling...")
handle_incoming(body)
else e -> Logger.debug("Couldn't get #{url}: #{inspect(e)}")
end
......
<?xml version="1.0"?>
<feed 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>https://mastodon.social/users/lambadalambda.atom</id>
<title>Critical Value</title>
<subtitle></subtitle>
<updated>2017-04-16T21:47:25Z</updated>
<logo>https://files.mastodon.social/accounts/avatars/000/000/264/original/1429214160519.gif</logo>
<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>
<link rel="alternate" type="text/html" href="https://mastodon.social/@lambadalambda"/>
<link rel="self" type="application/atom+xml" href="https://mastodon.social/users/lambadalambda.atom"/>
<link rel="hub" href="https://mastodon.social/api/push"/>
<link rel="salmon" href="https://mastodon.social/api/salmon/264"/>
<entry>
<id>tag:mastodon.social,2017-05-11:objectId=5647963:objectType=Status</id>
<published>2017-05-11T10:23:15Z</published>
<updated>2017-05-11T10:23:15Z</updated>
<title>lambadalambda shared a status by Skruyb@mamot.fr</title>
<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>tag:mamot.fr,2017-05-10:objectId=1294943:objectType=Status</id>
<published>2017-05-10T17:31:44Z</published>
<updated>2017-05-10T17:31:45Z</updated>
<title>New status by Skruyb@mamot.fr</title>
<author>
<id>https://mamot.fr/users/Skruyb</id>
<activity:object-type>http://activitystrea.ms/schema/1.0/person</activity:object-type>
<uri>https://mamot.fr/users/Skruyb</uri>
<name>Skruyb</name>
<email>Skruyb@mamot.fr</email>
<summary type="html">&lt;p&gt;Fr and En.&lt;br&gt;Posts will disappear on a regular basis.&lt;/p&gt;</summary>
<link rel="alternate" type="text/html" href="https://mamot.fr/@Skruyb"/>
<link rel="avatar" type="image/jpeg" media:width="120" media:height="120" href="https://files.mastodon.social/accounts/avatars/000/106/282/original/d95dbcfc76f77f4c.jpg"/>
<link rel="header" type="image/jpeg" media:width="700" media:height="335" href="https://files.mastodon.social/accounts/headers/000/106/282/original/c1aabdf5c97eb875.jpg"/>
<poco:preferredUsername>Skruyb</poco:preferredUsername>
<poco:displayName>The 7th Son</poco:displayName>
<poco:note>Fr and En.Posts will disappear on a regular basis.</poco:note>
<mastodon:scope>public</mastodon:scope>
</author>
<activity:object-type>http://activitystrea.ms/schema/1.0/comment</activity:object-type>
<activity:verb>http://activitystrea.ms/schema/1.0/post</activity:verb>
<summary xml:lang="it">Hey.</summary>
<content type="html" xml:lang="it">&lt;p&gt;&lt;span class="h-card"&gt;&lt;a href="https://mastodon.social/@lambadalambda"&gt;@&lt;span&gt;lambadalambda&lt;/span&gt;&lt;/a&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;Hey!!!&lt;/p&gt;</content>
<link rel="mentioned" ostatus:object-type="http://activitystrea.ms/schema/1.0/person" href="https://mastodon.social/users/lambadalambda"/>
<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://mamot.fr/users/Skruyb/updates/176041"/>
<thr:in-reply-to ref="tag:mastodon.social,2017-05-10:objectId=5582979:objectType=Status" href="https://mastodon.social/@lambadalambda/5582979"/>
</activity:object>
<content type="html" xml:lang="en">&lt;p&gt;&lt;span class="h-card"&gt;&lt;a href="https://mastodon.social/@lambadalambda"&gt;@&lt;span&gt;lambadalambda&lt;/span&gt;&lt;/a&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;Hey!!!&lt;/p&gt;</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/2325225"/>
<link rel="self" type="application/atom+xml" href="https://mastodon.social/users/lambadalambda/updates/2325225.atom"/>
</entry>
</feed>
<?xml version="1.0"?>
<XRD xmlns="http://docs.oasis-open.org/ns/xri/xrd-1.0">
<Subject>acct:Skruyb@mamot.fr</Subject>
<Alias>https://mamot.fr/@Skruyb</Alias>
<Alias>https://mamot.fr/users/Skruyb</Alias>
<Link rel="http://webfinger.net/rel/profile-page" type="text/html" href="https://mamot.fr/@Skruyb"/>
<Link rel="http://schemas.google.com/g/2010#updates-from" type="application/atom+xml" href="https://mamot.fr/users/Skruyb.atom"/>
<Link rel="salmon" href="https://mamot.fr/api/salmon/26213"/>
<Link rel="magic-public-key" href="data:application/magic-public-key,RSA.oguRI4WsZfm-6wne1Y-_y8-8QmWFOrpqfyLemzc_bX-qCttWb-TXjZhE3iv-yg79i9qcJhCKvmTKwscJTcjr0Jp2KsGTLJrUodsR0zYAhYkugdv6EJHvbJVA0bXZKCk5XieGvBn-t7xQC5mclbEgQs58kY3cavJ70b5XFFAWeDloga9G3E2TMygnuzQ3oZlxBa6b3HzjY3RxB3bYirCZLV_bipPGHkI7xr7TDV7fnGTjRm9IUz6UOboixT5UIRvZ6EZ_rCJaMIFXj47bmwj74pxiNlughYpmSLhI0Ka7hofoYmZ7CbUEfBWIzfgEiB-SO99tACy8pDwyQ5d9P7pt0w==.AQAB"/>
<Link rel="http://ostatus.org/schema/1.0/subscribe" template="https://mamot.fr/authorize_follow?acct={uri}"/>
</XRD>
......@@ -135,6 +135,20 @@ def get("https://pawoo.net/users/pekorino.atom", _, _) do
}}
end
def get("https://mamot.fr/.well-known/webfinger", [Accept: "application/xrd+xml"], [params: [resource: "https://mamot.fr/users/Skruyb"]]) do
{:ok, %Response{
status_code: 200,
body: File.read!("test/fixtures/httpoison_mock/skruyb@mamot.fr.atom")
}}
end
def get("https://mamot.fr/users/Skruyb.atom", _, _) do
{:ok, %Response{
status_code: 200,
body: File.read!("test/fixtures/httpoison_mock/https___mamot.fr_users_Skruyb.atom")
}}
end
def get(url, body, headers) do
{:error, "Not implemented the mock response for get #{inspect(url)}, #{inspect(body)}, #{inspect(headers)}"}
end
......
......@@ -72,6 +72,13 @@ test "handle incoming notes - Mastodon, with CW" do
assert String.contains?(activity.data["object"]["content"], "technologic")
end
test "handle incoming retweets - Mastodon, with CW" do
incoming = File.read!("test/fixtures/cw_retweet.xml")
{:ok, [[activity, retweeted_activity]]} = OStatus.handle_incoming(incoming)
assert String.contains?(retweeted_activity.data["object"]["content"], "Hey.")
end
test "handle incoming notes - GS, subscription, reply" do
incoming = File.read!("test/fixtures/ostatus_incoming_reply.xml")
{:ok, [activity]} = OStatus.handle_incoming(incoming)
......@@ -99,6 +106,7 @@ test "handle incoming retweets - GS, subscription" do
refute retweeted_activity.local
assert retweeted_activity.data["object"]["announcement_count"] == 1
assert String.contains?(retweeted_activity.data["object"]["content"], "mastodon")
refute String.contains?(retweeted_activity.data["object"]["content"], "Test account")
end
test "handle incoming retweets - GS, subscription - local message" do
......@@ -138,6 +146,7 @@ test "handle incoming retweets - Mastodon, salmon" do
assert retweeted_activity.data["type"] == "Create"
assert retweeted_activity.data["actor"] == "https://pleroma.soykaf.com/users/lain"
refute retweeted_activity.local
refute String.contains?(retweeted_activity.data["object"]["content"], "Test account")
end
test "handle incoming favorites - GS, websub" do
......@@ -192,8 +201,8 @@ test "handle incoming follows" do
assert activity.data["object"] == "https://pawoo.net/users/pekorino"
refute activity.local
follower = User.get_cached_by_ap_id(activity.data["actor"])
followed = User.get_cached_by_ap_id(activity.data["object"])
follower = User.get_by_ap_id(activity.data["actor"])
followed = User.get_by_ap_id(activity.data["object"])
assert User.following?(follower, followed)
end
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment