Save remote user bio and update if we see new data.

This commit is contained in:
Roger Braun 2017-05-24 17:34:38 +02:00
parent 94e980d6b4
commit bdcf42180f
5 changed files with 36 additions and 11 deletions

View File

@ -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}

View File

@ -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}

View 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"/>

View File

@ -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

View File

@ -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"}]}
}