Skip to content
Snippets Groups Projects
Commit 69922bc7 authored by lain's avatar lain
Browse files

Add user info gathering.

parent ca40dda0
Branches
No related tags found
No related merge requests found
......@@ -5,6 +5,7 @@ defmodule Pleroma.Web.OStatus do
alias Pleroma.{Repo, User, Web}
alias Pleroma.Web.ActivityPub.ActivityPub
alias Pleroma.Web.{WebFinger, Websub}
def feed_path(user) do
"#{user.ap_id}/feed.atom"
......@@ -134,4 +135,14 @@ defmodule Pleroma.Web.OStatus do
nil
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
......@@ -18,7 +18,7 @@ defmodule Pleroma.Web.WebFinger do
def webfinger(resource) do
host = Pleroma.Web.host
regex = ~r/acct:(?<username>\w+)@#{host}/
regex = ~r/(acct:)?(?<username>\w+)@#{host}/
case Regex.named_captures(regex, resource) do
%{"username" => username} ->
user = User.get_cached_by_nickname(username)
......@@ -70,7 +70,7 @@ defmodule Pleroma.Web.WebFinger do
else
e ->
Logger.debug("Couldn't finger #{account}.")
Logger.debug(e)
Logger.debug(inspect(e))
{:error, e}
end
end
......
......@@ -121,14 +121,24 @@ defmodule Pleroma.Web.Websub do
requester.(subscription)
end
def discover(topic, getter \\ &HTTPoison.get/1) do
def gather_feed_data(topic, getter \\ &HTTPoison.get/1) do
with {:ok, response} <- getter.(topic),
status_code when status_code in 200..299 <- response.status_code,
body <- response.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
{: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 ->
{:error, e}
end
......
......@@ -63,4 +63,26 @@ defmodule Pleroma.Web.OStatusTest do
assert user == user_again
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
......@@ -112,8 +112,15 @@ defmodule Pleroma.Web.WebsubTest do
{:ok, %{status_code: 200, body: doc}}
end
{:ok, discovered} = Websub.discover(topic, getter)
assert %{hub: "https://mastodon.social/api/push", url: topic} == discovered
{:ok, discovered} = Websub.gather_feed_data(topic, getter)
expected = %{
hub: "https://mastodon.social/api/push",
uri: "https://mastodon.social/users/lambadalambda",
nickname: "lambadalambda",
name: "Critical Value"
}
assert expected == discovered
end
test "calls the hub, requests topic" do
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment