defmodule Pleroma.Web.Salmon do
+ @httpoison Application.get_env(:pleroma, :httpoison)
+
use Bitwise
alias Pleroma.Web.XML
alias Pleroma.Web.OStatus.ActivityRepresenter
{:xmlObj, :string, encoding} = :xmerl_xpath.string('string(//me:encoding[1])', doc)
{:xmlObj, :string, type} = :xmerl_xpath.string('string(//me:data[1]/@type)', doc)
-
{:ok, data} = Base.url_decode64(to_string(data), ignore: :whitespace)
{:ok, sig} = Base.url_decode64(to_string(sig), ignore: :whitespace)
alg = to_string(alg)
[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
|> Enum.map(&Base.url_encode64/1)
|> Enum.join(".")
- signature = :public_key.sign(signed_text, :sha256, private_key) |> to_string |> Base.url_encode64
- doc_base64= doc |> Base.url_encode64
+ signature = signed_text
+ |> :public_key.sign(:sha256, private_key)
+ |> to_string
+ |> Base.url_encode64
+
+ doc_base64 = doc
+ |> Base.url_encode64
# Don't need proper xml building, these strings are safe to leave unescaped
salmon = """
defp send_to_user(_,_,_), do: nil
- def publish(user, activity, poster \\ &HTTPoison.post/3)
+ def publish(user, activity, poster \\ &@httpoison.post/3)
def publish(%{info: %{"keys" => keys}} = user, activity, poster) do
feed = ActivityRepresenter.to_simple_form(activity, user, true)
|> ActivityRepresenter.wrap_with_entry
remote_users(activity)
|> Enum.each(fn(remote_user) ->
- Logger.debug("sending salmon to #{remote_user.ap_id}")
- send_to_user(remote_user, feed, poster)
+ Task.start(fn ->
+ Logger.debug(fn -> "sending salmon to #{remote_user.ap_id}" end)
+ send_to_user(remote_user, feed, poster)
+ end)
end)
end
end
- def publish(%{id: id}, _, _), do: Logger.debug("Keys missing for user #{id}")
+ def publish(%{id: id}, _, _), do: Logger.debug(fn -> "Keys missing for user #{id}" end)
end