aboutsummaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorRoger Braun <roger@rogerbraun.net>2017-05-03 14:26:49 +0200
committerRoger Braun <roger@rogerbraun.net>2017-05-03 14:26:49 +0200
commit8141024259ee4bebd58d6ecd963f181aad420846 (patch)
tree916d1f7f6d25869733c1a3453bb946c6f55b250c /lib
parent16afea399d330c28de05c77649fe0540598ee8ec (diff)
downloadpleroma-8141024259ee4bebd58d6ecd963f181aad420846.tar.gz
Attachment parsing, better magic key fetching.
Diffstat (limited to 'lib')
-rw-r--r--lib/pleroma/web/ostatus/ostatus.ex35
-rw-r--r--lib/pleroma/web/ostatus/ostatus_controller.ex2
-rw-r--r--lib/pleroma/web/salmon/salmon.ex15
-rw-r--r--lib/pleroma/web/web.ex22
-rw-r--r--lib/pleroma/web/web_finger/web_finger.ex12
-rw-r--r--lib/pleroma/web/websub/websub.ex12
6 files changed, 48 insertions, 50 deletions
diff --git a/lib/pleroma/web/ostatus/ostatus.ex b/lib/pleroma/web/ostatus/ostatus.ex
index 7aa1ac4ac..f81751a25 100644
--- a/lib/pleroma/web/ostatus/ostatus.ex
+++ b/lib/pleroma/web/ostatus/ostatus.ex
@@ -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}
diff --git a/lib/pleroma/web/ostatus/ostatus_controller.ex b/lib/pleroma/web/ostatus/ostatus_controller.ex
index c6700ae78..1f2dedd30 100644
--- a/lib/pleroma/web/ostatus/ostatus_controller.ex
+++ b/lib/pleroma/web/ostatus/ostatus_controller.ex
@@ -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)
diff --git a/lib/pleroma/web/salmon/salmon.ex b/lib/pleroma/web/salmon/salmon.ex
index b4f214d46..f02cb11dc 100644
--- a/lib/pleroma/web/salmon/salmon.ex
+++ b/lib/pleroma/web/salmon/salmon.ex
@@ -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
diff --git a/lib/pleroma/web/web.ex b/lib/pleroma/web/web.ex
index a81e3e6e1..ee7ee78e9 100644
--- a/lib/pleroma/web/web.ex
+++ b/lib/pleroma/web/web.ex
@@ -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
diff --git a/lib/pleroma/web/web_finger/web_finger.ex b/lib/pleroma/web/web_finger/web_finger.ex
index d16bdd982..5fa69c2c8 100644
--- a/lib/pleroma/web/web_finger/web_finger.ex
+++ b/lib/pleroma/web/web_finger/web_finger.ex
@@ -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
diff --git a/lib/pleroma/web/websub/websub.ex b/lib/pleroma/web/websub/websub.ex
index 546bfb5a4..e32fc8817 100644
--- a/lib/pleroma/web/websub/websub.ex
+++ b/lib/pleroma/web/websub/websub.ex
@@ -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}