Save remote user bio and update if we see new data.
authorRoger Braun <roger@rogerbraun.net>
Wed, 24 May 2017 15:34:38 +0000 (17:34 +0200)
committerRoger Braun <roger@rogerbraun.net>
Wed, 24 May 2017 15:34:38 +0000 (17:34 +0200)
lib/pleroma/web/ostatus/ostatus.ex
lib/pleroma/web/websub/websub.ex
test/fixtures/lambadalambda.atom
test/web/ostatus/ostatus_test.exs
test/web/websub/websub_test.exs

index 02fc273cf7eabecfc23a5a1a54c1b23d64bfed75..9300de16e01540ef3608ad1875405e2771ccb301 100644 (file)
@@ -228,16 +228,30 @@ defmodule Pleroma.Web.OStatus do
     end
   end
 
+  def maybe_update(doc, user) do
+    old_data = %{
+      avatar: user.avatar,
+      bio: user.bio,
+      name: user.name
+    }
+
+    with false <- user.local,
+         avatar <- make_avatar_object(doc),
+         bio when not is_nil(bio) <- string_from_xpath("//author[1]/summary", doc),
+         name when not is_nil(name) <- string_from_xpath("//author[1]/poco:displayName", doc),
+         new_data <- %{avatar: avatar, name: name, bio: bio},
+         false <- new_data == old_data do
+      change = Ecto.Changeset.change(user, new_data)
+      Repo.update(change)
+    else e ->
+      {:ok, user}
+    end
+  end
+
   def find_make_or_update_user(doc) do
     uri = string_from_xpath("//author/uri[1]", doc)
     with {:ok, user} <- find_or_make_user(uri) do
-      avatar = make_avatar_object(doc)
-      if !user.local && user.avatar != avatar do
-        change = Ecto.Changeset.change(user, %{avatar: avatar})
-        Repo.update(change)
-      else
-        {:ok, user}
-      end
+      maybe_update(doc, user)
     end
   end
 
@@ -261,7 +275,8 @@ defmodule Pleroma.Web.OStatus do
         nickname: info["nickname"] <> "@" <> info["host"],
         ap_id: info["uri"],
         info: info,
-        avatar: info["avatar"]
+        avatar: info["avatar"],
+        bio: info["bio"]
       }
       with %User{} = user <- User.get_by_ap_id(data.ap_id) do
         {:ok, user}
index e46e0a2ce19082c9e3f718f367d1f5912e8cae75..a683f6da40de022e443630f8adb279471f019431 100644 (file)
@@ -156,6 +156,7 @@ defmodule Pleroma.Web.Websub do
       preferredUsername = XML.string_from_xpath("/feed/author[1]/poco:preferredUsername", doc)
       displayName = XML.string_from_xpath("/feed/author[1]/poco:displayName", doc)
       avatar = OStatus.make_avatar_object(doc)
+      bio = XML.string_from_xpath("/feed/author[1]/summary", doc)
 
       {:ok, %{
         "uri" => uri,
@@ -163,7 +164,8 @@ defmodule Pleroma.Web.Websub do
         "nickname" => preferredUsername || name,
         "name" => displayName || name,
         "host" => URI.parse(uri).host,
-        "avatar" => avatar
+        "avatar" => avatar,
+        "bio" => bio
       }}
     else e ->
       {:error, e}
index 35e506420f241dc6e741ab2ded4096b33acdc6b6..964a416f72a65801ca75bb179cd19630dba10c52 100644 (file)
@@ -11,7 +11,7 @@
     <uri>https://mastodon.social/users/lambadalambda</uri>
     <name>lambadalambda</name>
     <email>lambadalambda@mastodon.social</email>
-    <summary></summary>
+    <summary>a cool dude.</summary>
     <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?1492379244"/>
     <link rel="header" type="" media:width="700" media:height="335" href="/headers/original/missing.png"/>
index 6599b00b3e1a2991e6c404fdae9ad3f79ba016e8..e40fae78cc3a1d22f5386ab7cc6a998b236f0db5 100644 (file)
@@ -233,6 +233,7 @@ defmodule Pleroma.Web.OStatusTest do
       assert user.local == false
       assert user.info["uri"] == uri
       assert user.ap_id == uri
+      assert user.bio == "Call me Deacon Blues."
       assert user.avatar["type"] == "Image"
 
       {:ok, user_again} = OStatus.find_or_make_user(uri)
@@ -244,7 +245,9 @@ defmodule Pleroma.Web.OStatusTest do
       uri = "https://social.heldscal.la/user/23211"
 
       {:ok, user} = OStatus.find_or_make_user(uri)
-      change = Ecto.Changeset.change(user, %{avatar: nil})
+      old_name = user.name
+      old_bio = user.bio
+      change = Ecto.Changeset.change(user, %{avatar: nil, bio: nil, old_name: nil})
 
       {:ok, user} = Repo.update(change)
       refute user.avatar
@@ -253,6 +256,8 @@ defmodule Pleroma.Web.OStatusTest do
       [author] = :xmerl_xpath.string('//author[1]', doc)
       {:ok, user} = OStatus.find_make_or_update_user(author)
       assert user.avatar["type"] == "Image"
+      assert user.name == old_name
+      assert user.bio == old_bio
 
       {:ok, user_again} = OStatus.find_make_or_update_user(author)
       assert user_again == user
@@ -277,6 +282,7 @@ defmodule Pleroma.Web.OStatusTest do
         "uri" => "https://social.heldscal.la/user/29191",
         "host" => "social.heldscal.la",
         "fqn" => user,
+        "bio" => "cofe",
         "avatar" => %{"type" => "Image", "url" => [%{"href" => "https://social.heldscal.la/avatar/29191-original-20170421154949.jpeg", "mediaType" => "image/jpeg", "type" => "Link"}]}
       }
       assert data == expected
@@ -299,6 +305,7 @@ defmodule Pleroma.Web.OStatusTest do
         "uri" => "https://social.heldscal.la/user/29191",
         "host" => "social.heldscal.la",
         "fqn" => user,
+        "bio" => "cofe",
         "avatar" => %{"type" => "Image", "url" => [%{"href" => "https://social.heldscal.la/avatar/29191-original-20170421154949.jpeg", "mediaType" => "image/jpeg", "type" => "Link"}]}
       }
       assert data == expected
index aaed9226ef19aa1e86af3d80aa03c8a39c515360..1ca573d6600bf49b00eda84deadad111fc3be6b3 100644 (file)
@@ -120,6 +120,7 @@ defmodule Pleroma.Web.WebsubTest do
       "nickname" => "lambadalambda",
       "name" => "Critical Value",
       "host" => "mastodon.social",
+      "bio" => "a cool dude.",
       "avatar" => %{"type" => "Image", "url" => [%{"href" => "https://files.mastodon.social/accounts/avatars/000/000/264/original/1429214160519.gif?1492379244", "mediaType" => "image/gif", "type" => "Link"}]}
     }