Some refactoring.

This commit is contained in:
Roger Braun 2017-05-05 12:07:38 +02:00
parent c48c381e90
commit 7269c51f3a
8 changed files with 36 additions and 31 deletions

View file

@ -6,24 +6,24 @@ defmodule Pleroma.Web.Federator do
@websub Application.get_env(:pleroma, :websub)
def handle(:publish, activity) do
Logger.debug("Running publish for #{activity.data["id"]}")
Logger.debug(fn -> "Running publish for #{activity.data["id"]}" end)
with actor when not is_nil(actor) <- User.get_cached_by_ap_id(activity.data["actor"]) do
Logger.debug("Sending #{activity.data["id"]} out via websub")
Logger.debug(fn -> "Sending #{activity.data["id"]} out via websub" end)
Pleroma.Web.Websub.publish(Pleroma.Web.OStatus.feed_path(actor), actor, activity)
{:ok, actor} = WebFinger.ensure_keys_present(actor)
Logger.debug("Sending #{activity.data["id"]} out via salmon")
Logger.debug(fn -> "Sending #{activity.data["id"]} out via salmon" end)
Pleroma.Web.Salmon.publish(actor, activity)
end
end
def handle(:verify_websub, websub) do
Logger.debug("Running websub verification for #{websub.id} (#{websub.topic}, #{websub.callback})")
Logger.debug(fn -> "Running websub verification for #{websub.id} (#{websub.topic}, #{websub.callback})" end)
@websub.verify(websub)
end
def handle(type, payload) do
Logger.debug("Unknown task: #{type}")
Logger.debug(fn -> "Unknown task: #{type}" end)
{:error, "Don't know what do do with this"}
end

View file

@ -3,7 +3,7 @@ defmodule Pleroma.Web.OStatus.ActivityRepresenter do
alias Pleroma.Web.OStatus.UserRepresenter
require Logger
defp get_in_reply_to(%{"object" => %{ "inReplyTo" => in_reply_to}}) do
defp get_in_reply_to(%{"object" => %{"inReplyTo" => in_reply_to}}) do
[{:"thr:in-reply-to", [ref: to_charlist(in_reply_to)], []}]
end

View file

@ -24,8 +24,8 @@ defmodule Pleroma.Web.OStatus do
entries = :xmerl_xpath.string('//entry', doc)
activities = Enum.map(entries, fn (entry) ->
{:xmlObj, :string, object_type } = :xmerl_xpath.string('string(/entry/activity:object-type[1])', entry)
{:xmlObj, :string, verb } = :xmerl_xpath.string('string(/entry/activity:verb[1])', entry)
{:xmlObj, :string, object_type} = :xmerl_xpath.string('string(/entry/activity:object-type[1])', entry)
{:xmlObj, :string, verb} = :xmerl_xpath.string('string(/entry/activity:verb[1])', entry)
case verb do
'http://activitystrea.ms/schema/1.0/share' ->
@ -206,7 +206,7 @@ defmodule Pleroma.Web.OStatus do
{: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}")
Logger.debug(fn -> "Couldn't gather info for #{username}" end)
{:error, e}
end
end

View file

@ -48,7 +48,8 @@ defmodule Pleroma.Web.OStatus.OStatusController do
activity = Activity.get_create_activity_by_object_ap_id(id)
user = User.get_cached_by_ap_id(activity.data["actor"])
response = ActivityRepresenter.to_simple_form(activity, user, true)
response = activity
|> ActivityRepresenter.to_simple_form(user, true)
|> ActivityRepresenter.wrap_with_entry
|> :xmerl.export_simple(:xmerl_xml)
|> to_string

View file

@ -101,8 +101,13 @@ defmodule Pleroma.Web.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 = """
@ -143,11 +148,11 @@ defmodule Pleroma.Web.Salmon do
remote_users(activity)
|> Enum.each(fn(remote_user) ->
Logger.debug("sending salmon to #{remote_user.ap_id}")
Logger.debug(fn -> "sending salmon to #{remote_user.ap_id}" end)
send_to_user(remote_user, feed, poster)
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

View file

@ -41,7 +41,7 @@ defmodule Pleroma.Web.TwitterAPI.TwitterAPI do
Enum.join([text | attachment_text], "<br>")
end
def create_status(user = %User{}, data = %{"status" => status}) do
def create_status(%User{} = user, %{"status" => status} = data) do
attachments = attachments_from_ids(data["media_ids"])
context = ActivityPub.generate_context_id
mentions = parse_mentions(status)
@ -122,8 +122,7 @@ defmodule Pleroma.Web.TwitterAPI.TwitterAPI do
statuses <- activities |> activities_to_statuses(%{for: user})
do
statuses
else e ->
IO.inspect(e)
else _e ->
[]
end
end
@ -135,26 +134,26 @@ defmodule Pleroma.Web.TwitterAPI.TwitterAPI do
end
def follow(%User{} = follower, params) do
with { :ok, %User{} = followed } <- get_user(params),
{ :ok, follower } <- User.follow(follower, followed),
{ :ok, activity } <- ActivityPub.insert(%{
with {:ok, %User{} = followed} <- get_user(params),
{:ok, follower} <- User.follow(follower, followed),
{:ok, activity} <- ActivityPub.insert(%{
"type" => "Follow",
"actor" => follower.ap_id,
"object" => followed.ap_id,
"published" => make_date()
})
do
{ :ok, follower, followed, activity }
{:ok, follower, followed, activity}
else
err -> err
end
end
def unfollow(%User{} = follower, params) do
with { :ok, %User{} = unfollowed } <- get_user(params),
{ :ok, follower } <- User.unfollow(follower, unfollowed)
with {:ok, %User{} = unfollowed} <- get_user(params),
{:ok, follower} <- User.unfollow(follower, unfollowed)
do
{ :ok, follower, unfollowed}
{:ok, follower, unfollowed}
else
err -> err
end
@ -269,14 +268,14 @@ defmodule Pleroma.Web.TwitterAPI.TwitterAPI do
def get_user(user \\ nil, params) do
case params do
%{ "user_id" => user_id } ->
%{"user_id" => user_id} ->
case target = Repo.get(User, user_id) do
nil ->
{:error, "No user with such user_id"}
_ ->
{:ok, target}
end
%{ "screen_name" => nickname } ->
%{"screen_name" => nickname} ->
case target = Repo.get_by(User, nickname: nickname) do
nil ->
{:error, "No user with such screen_name"}

View file

@ -102,8 +102,8 @@ defmodule Pleroma.Web.WebFinger do
{:ok, data}
else
e ->
Logger.debug("Couldn't finger #{account}.")
Logger.debug(inspect(e))
Logger.debug(fn -> "Couldn't finger #{account}." end)
Logger.debug(fn -> inspect(e) end)
{:error, e}
end
end

View file

@ -49,7 +49,7 @@ defmodule Pleroma.Web.Websub do
|> to_string
signature = sign(sub.secret || "", response)
Logger.debug("Pushing to #{sub.callback}")
Logger.debug(fn -> "Pushing to #{sub.callback}" end)
HTTPoison.post(sub.callback, response, [
{"Content-Type", "application/atom+xml"},
@ -196,8 +196,8 @@ defmodule Pleroma.Web.Websub do
change = Ecto.Changeset.change(websub, %{state: "rejected"})
{:ok, websub} = Repo.update(change)
Logger.debug("Couldn't confirm subscription: #{inspect(websub)}")
Logger.debug("error: #{inspect(e)}")
Logger.debug(fn -> "Couldn't confirm subscription: #{inspect(websub)}" end)
Logger.debug(fn -> "error: #{inspect(e)}" end)
{:error, websub}
end