Commit 102455bf authored by lain's avatar lain
Browse files

Add avatar updating from incoming messages.

parent 945b4b55
......@@ -90,6 +90,19 @@ def handle_note(entry, doc \\ nil) do
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.avatar != avatar do
change = Ecto.Changeset.change(user, %{avatar: avatar})
Repo.update(change)
else
{:ok, user}
end
end
end
def find_or_make_user(uri) do
query = from user in User,
where: user.local == false and fragment("? @> ?", user.info, ^%{uri: uri})
......@@ -121,8 +134,8 @@ def make_user(uri) do
# TODO: Just takes the first one for now.
def make_avatar_object(author_doc) do
href = string_from_xpath("/feed/author[1]/link[@rel=\"avatar\"]/@href", author_doc)
type = string_from_xpath("/feed/author[1]/link[@rel=\"avatar\"]/@type", author_doc)
href = string_from_xpath("//author[1]/link[@rel=\"avatar\"]/@href", author_doc)
type = string_from_xpath("//author[1]/link[@rel=\"avatar\"]/@type", author_doc)
if href do
%{
......
This diff is collapsed.
defmodule Pleroma.Web.OStatusTest do
use Pleroma.DataCase
alias Pleroma.Web.OStatus
alias Pleroma.Web.XML
test "don't insert create notes twice" do
incoming = File.read!("test/fixtures/incoming_note_activity.xml")
......@@ -71,6 +72,25 @@ test "tries to use the information in poco fields" do
assert user == user_again
end
test "find_make_or_update_user takes an author element and returns an updated user" do
# TODO make test local
uri = "https://social.heldscal.la/user/23211"
{:ok, user} = OStatus.find_or_make_user(uri)
change = Ecto.Changeset.change(user, %{avatar: nil})
{:ok, user} = Repo.update(change)
refute user.avatar
doc = XML.parse_document(File.read!("test/fixtures/23211.atom"))
[author] = :xmerl_xpath.string('//author[1]', doc)
{:ok, user} = OStatus.find_make_or_update_user(author)
assert user.avatar["type"] == "Image"
{:ok, user_again} = OStatus.find_make_or_update_user(author)
assert user_again == user
end
end
describe "gathering user info from a user id" do
......
Supports Markdown
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