From bdcf42180fa67e43f13584d5e19f26d751199d03 Mon Sep 17 00:00:00 2001 From: Roger Braun Date: Wed, 24 May 2017 17:34:38 +0200 Subject: [PATCH] Save remote user bio and update if we see new data. --- lib/pleroma/web/ostatus/ostatus.ex | 31 ++++++++++++++++++++++-------- lib/pleroma/web/websub/websub.ex | 4 +++- test/fixtures/lambadalambda.atom | 2 +- test/web/ostatus/ostatus_test.exs | 9 ++++++++- test/web/websub/websub_test.exs | 1 + 5 files changed, 36 insertions(+), 11 deletions(-) diff --git a/lib/pleroma/web/ostatus/ostatus.ex b/lib/pleroma/web/ostatus/ostatus.ex index 02fc273cf..9300de16e 100644 --- a/lib/pleroma/web/ostatus/ostatus.ex +++ b/lib/pleroma/web/ostatus/ostatus.ex @@ -228,16 +228,30 @@ def handle_note(entry, doc \\ nil) 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 @@ def make_user(uri) 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} diff --git a/lib/pleroma/web/websub/websub.ex b/lib/pleroma/web/websub/websub.ex index e46e0a2ce..a683f6da4 100644 --- a/lib/pleroma/web/websub/websub.ex +++ b/lib/pleroma/web/websub/websub.ex @@ -156,6 +156,7 @@ def gather_feed_data(topic, getter \\ &@httpoison.get/1) 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 @@ def gather_feed_data(topic, getter \\ &@httpoison.get/1) do "nickname" => preferredUsername || name, "name" => displayName || name, "host" => URI.parse(uri).host, - "avatar" => avatar + "avatar" => avatar, + "bio" => bio }} else e -> {:error, e} diff --git a/test/fixtures/lambadalambda.atom b/test/fixtures/lambadalambda.atom index 35e506420..964a416f7 100644 --- a/test/fixtures/lambadalambda.atom +++ b/test/fixtures/lambadalambda.atom @@ -11,7 +11,7 @@ https://mastodon.social/users/lambadalambda lambadalambda lambadalambda@mastodon.social - + a cool dude. diff --git a/test/web/ostatus/ostatus_test.exs b/test/web/ostatus/ostatus_test.exs index 6599b00b3..e40fae78c 100644 --- a/test/web/ostatus/ostatus_test.exs +++ b/test/web/ostatus/ostatus_test.exs @@ -233,6 +233,7 @@ test "tries to use the information in poco fields" 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 @@ test "find_make_or_update_user takes an author element and returns an updated us 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 @@ test "find_make_or_update_user takes an author element and returns an updated us [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 @@ test "it returns user info in a hash" 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 @@ test "it works with the uri" 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 diff --git a/test/web/websub/websub_test.exs b/test/web/websub/websub_test.exs index aaed9226e..1ca573d66 100644 --- a/test/web/websub/websub_test.exs +++ b/test/web/websub/websub_test.exs @@ -120,6 +120,7 @@ test "discovers the hub and canonical url" 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"}]} }