Add user info gathering.

This commit is contained in:
Roger Braun 2017-04-29 17:51:59 +02:00
parent ca40dda04c
commit 69922bc724
5 changed files with 57 additions and 7 deletions

View file

@ -5,6 +5,7 @@ defmodule Pleroma.Web.OStatus do
alias Pleroma.{Repo, User, Web} alias Pleroma.{Repo, User, Web}
alias Pleroma.Web.ActivityPub.ActivityPub alias Pleroma.Web.ActivityPub.ActivityPub
alias Pleroma.Web.{WebFinger, Websub}
def feed_path(user) do def feed_path(user) do
"#{user.ap_id}/feed.atom" "#{user.ap_id}/feed.atom"
@ -134,4 +135,14 @@ defmodule Pleroma.Web.OStatus do
nil nil
end end
end end
def gather_user_info(username) do
with {:ok, webfinger_data} <- WebFinger.finger(username),
{:ok, feed_data} <- Websub.gather_feed_data(webfinger_data.topic) do
{:ok, Map.merge(webfinger_data, feed_data) |> Map.put(:fqn, username)}
else e ->
Logger.debug("Couldn't gather info for #{username}")
{:error, e}
end
end
end end

View file

@ -18,7 +18,7 @@ defmodule Pleroma.Web.WebFinger do
def webfinger(resource) do def webfinger(resource) do
host = Pleroma.Web.host host = Pleroma.Web.host
regex = ~r/acct:(?<username>\w+)@#{host}/ regex = ~r/(acct:)?(?<username>\w+)@#{host}/
case Regex.named_captures(regex, resource) do case Regex.named_captures(regex, resource) do
%{"username" => username} -> %{"username" => username} ->
user = User.get_cached_by_nickname(username) user = User.get_cached_by_nickname(username)
@ -70,7 +70,7 @@ defmodule Pleroma.Web.WebFinger do
else else
e -> e ->
Logger.debug("Couldn't finger #{account}.") Logger.debug("Couldn't finger #{account}.")
Logger.debug(e) Logger.debug(inspect(e))
{:error, e} {:error, e}
end end
end end

View file

@ -121,14 +121,24 @@ defmodule Pleroma.Web.Websub do
requester.(subscription) requester.(subscription)
end end
def discover(topic, getter \\ &HTTPoison.get/1) do def gather_feed_data(topic, getter \\ &HTTPoison.get/1) do
with {:ok, response} <- getter.(topic), with {:ok, response} <- getter.(topic),
status_code when status_code in 200..299 <- response.status_code, status_code when status_code in 200..299 <- response.status_code,
body <- response.body, body <- response.body,
doc <- XML.parse_document(body), doc <- XML.parse_document(body),
url when not is_nil(url) <- XML.string_from_xpath(~S{/feed/link[@rel="self"]/@href}, doc), uri when not is_nil(uri) <- XML.string_from_xpath("/feed/author[1]/uri", doc),
hub when not is_nil(hub) <- XML.string_from_xpath(~S{/feed/link[@rel="hub"]/@href}, doc) do hub when not is_nil(hub) <- XML.string_from_xpath(~S{/feed/link[@rel="hub"]/@href}, doc) do
{:ok, %{url: url, hub: hub}}
name = XML.string_from_xpath("/feed/author[1]/name", doc)
preferredUsername = XML.string_from_xpath("/feed/author[1]/poco:preferredUsername", doc)
displayName = XML.string_from_xpath("/feed/author[1]/poco:displayName", doc)
{:ok, %{
uri: uri,
hub: hub,
nickname: preferredUsername || name,
name: displayName || name
}}
else e -> else e ->
{:error, e} {:error, e}
end end

View file

@ -63,4 +63,26 @@ defmodule Pleroma.Web.OStatusTest do
assert user == user_again assert user == user_again
end end
end end
describe "gathering user info from a user id" do
test "it returns user info in a hash" do
user = "shp@social.heldscal.la"
# TODO: make test local
{:ok, data} = OStatus.gather_user_info(user)
expected = %{
hub: "https://social.heldscal.la/main/push/hub",
magic_key: "RSA.wQ3i9UA0qmAxZ0WTIp4a-waZn_17Ez1pEEmqmqoooRsG1_BvpmOvLN0G2tEcWWxl2KOtdQMCiPptmQObeZeuj48mdsDZ4ArQinexY2hCCTcbV8Xpswpkb8K05RcKipdg07pnI7tAgQ0VWSZDImncL6YUGlG5YN8b5TjGOwk2VG8=.AQAB",
name: "shp",
nickname: "shp",
salmon: "https://social.heldscal.la/main/salmon/user/29191",
subject: "acct:shp@social.heldscal.la",
topic: "https://social.heldscal.la/api/statuses/user_timeline/29191.atom",
uri: "https://social.heldscal.la/user/29191",
fqn: user
}
assert data == expected
end
end
end end

View file

@ -112,8 +112,15 @@ defmodule Pleroma.Web.WebsubTest do
{:ok, %{status_code: 200, body: doc}} {:ok, %{status_code: 200, body: doc}}
end end
{:ok, discovered} = Websub.discover(topic, getter) {:ok, discovered} = Websub.gather_feed_data(topic, getter)
assert %{hub: "https://mastodon.social/api/push", url: topic} == discovered expected = %{
hub: "https://mastodon.social/api/push",
uri: "https://mastodon.social/users/lambadalambda",
nickname: "lambadalambda",
name: "Critical Value"
}
assert expected == discovered
end end
test "calls the hub, requests topic" do test "calls the hub, requests topic" do