Attachment parsing, better magic key fetching.

This commit is contained in:
Roger Braun 2017-05-03 14:26:49 +02:00
commit 8141024259
12 changed files with 161 additions and 84 deletions

View file

@ -39,6 +39,24 @@ defmodule Pleroma.Web.OStatus do
{:ok, activities}
end
def get_attachments(entry) do
:xmerl_xpath.string('/entry/link[@rel="enclosure"]', entry)
|> Enum.map(fn (enclosure) ->
with href when not is_nil(href) <- string_from_xpath("/link/@href", enclosure),
type when not is_nil(type) <- string_from_xpath("/link/@type", enclosure) do
%{
"type" => "Attachment",
"url" => [%{
"type" => "Link",
"mediaType" => type,
"href" => href
}]
}
end
end)
|> Enum.filter(&(&1))
end
def handle_note(entry, doc \\ nil) do
content_html = string_from_xpath("/entry/content[1]", entry)
@ -48,6 +66,8 @@ defmodule Pleroma.Web.OStatus do
context = (string_from_xpath("/entry/ostatus:conversation[1]", entry) || "") |> String.trim
attachments = get_attachments(entry)
context = with %{data: %{"context" => context}} <- Object.get_cached_by_ap_id(inReplyTo) do
context
else _e ->
@ -77,7 +97,8 @@ defmodule Pleroma.Web.OStatus do
"content" => content_html,
"published" => date,
"context" => context,
"actor" => actor.ap_id
"actor" => actor.ap_id,
"attachment" => attachments
}
object = if inReplyTo do
@ -124,11 +145,11 @@ defmodule Pleroma.Web.OStatus do
with {:ok, info} <- gather_user_info(uri) do
data = %{
local: false,
name: info.name,
nickname: info.nickname <> "@" <> info.host,
ap_id: info.uri,
name: info["name"],
nickname: info["nickname"] <> "@" <> info["host"],
ap_id: info["uri"],
info: info,
avatar: info.avatar
avatar: info["avatar"]
}
# TODO: Make remote user changeset
# SHould enforce fqn nickname
@ -158,8 +179,8 @@ defmodule Pleroma.Web.OStatus do
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)}
{: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}

View file

@ -33,7 +33,7 @@ defmodule Pleroma.Web.OStatus.OStatusController do
def salmon_incoming(conn, params) do
{:ok, body, _conn} = read_body(conn)
magic_key = Pleroma.Web.Salmon.fetch_magic_key(body)
{:ok, magic_key} = Pleroma.Web.Salmon.fetch_magic_key(body)
{:ok, doc} = Pleroma.Web.Salmon.decode_and_validate(magic_key, body)
Pleroma.Web.OStatus.handle_incoming(doc)

View file

@ -24,16 +24,13 @@ defmodule Pleroma.Web.Salmon do
[data, type, encoding, alg, sig]
end
# TODO rewrite in with-stile
# Make it fetch the key from the saved user if there is one
def fetch_magic_key(salmon) do
[data, _, _, _, _] = decode(salmon)
doc = XML.parse_document(data)
uri = XML.string_from_xpath("/entry/author[1]/uri", doc)
{:ok, info} = Pleroma.Web.OStatus.gather_user_info(uri)
info.magic_key
with [data, _, _, _, _] <- decode(salmon),
doc <- XML.parse_document(data),
uri when not is_nil(uri) <- XML.string_from_xpath("/entry/author[1]/uri", doc),
{:ok, %{info: %{"magic_key" => magic_key}}} <- Pleroma.Web.OStatus.find_or_make_user(uri) do
{:ok, magic_key}
end
end
def decode_and_validate(magickey, salmon) do

View file

@ -61,27 +61,7 @@ defmodule Pleroma.Web do
apply(__MODULE__, which, [])
end
def host do
settings = Application.get_env(:pleroma, Pleroma.Web.Endpoint)
settings
|> Keyword.fetch!(:url)
|> Keyword.fetch!(:host)
end
def base_url do
settings = Application.get_env(:pleroma, Pleroma.Web.Endpoint)
host = host()
protocol = settings |> Keyword.fetch!(:protocol)
port_fragment = with {:ok, protocol_info} <- settings |> Keyword.fetch(String.to_atom(protocol)),
{:ok, port} <- protocol_info |> Keyword.fetch(:port)
do
":#{port}"
else _e ->
""
end
"#{protocol}://#{host}#{port_fragment}"
Pleroma.Web.Endpoint.url
end
end

View file

@ -16,7 +16,7 @@ defmodule Pleroma.Web.WebFinger do
end
def webfinger(resource) do
host = Pleroma.Web.host
host = Pleroma.Web.Endpoint.host
regex = ~r/(acct:)?(?<username>\w+)@#{host}/
with %{"username" => username} <- Regex.named_captures(regex, resource) do
user = User.get_by_nickname(username)
@ -37,7 +37,7 @@ defmodule Pleroma.Web.WebFinger do
{
:XRD, %{xmlns: "http://docs.oasis-open.org/ns/xri/xrd-1.0"},
[
{:Subject, "acct:#{user.nickname}@#{Pleroma.Web.host}"},
{:Subject, "acct:#{user.nickname}@#{Pleroma.Web.Endpoint.host}"},
{:Alias, user.ap_id},
{:Link, %{rel: "http://schemas.google.com/g/2010#updates-from", type: "application/atom+xml", href: OStatus.feed_path(user)}},
{:Link, %{rel: "http://webfinger.net/rel/profile-page", type: "text/html", href: user.ap_id}},
@ -72,10 +72,10 @@ defmodule Pleroma.Web.WebFinger do
subject = XML.string_from_xpath("//Subject", doc)
salmon = XML.string_from_xpath(~s{//Link[@rel="salmon"]/@href}, doc)
data = %{
magic_key: magic_key,
topic: topic,
subject: subject,
salmon: salmon
"magic_key" => magic_key,
"topic" => topic,
"subject" => subject,
"salmon" => salmon
}
{:ok, data}
end

View file

@ -146,12 +146,12 @@ defmodule Pleroma.Web.Websub do
avatar = OStatus.make_avatar_object(doc)
{:ok, %{
uri: uri,
hub: hub,
nickname: preferredUsername || name,
name: displayName || name,
host: URI.parse(uri).host,
avatar: avatar
"uri" => uri,
"hub" => hub,
"nickname" => preferredUsername || name,
"name" => displayName || name,
"host" => URI.parse(uri).host,
"avatar" => avatar
}}
else e ->
{:error, e}