Merge branch 'feld-Logger' into 'develop'
[akkoma] / lib / pleroma / web / websub / websub.ex
index a5309ebd99428fee662b07a9b98e17633e487346..5caa8198c10efe87d3a6c5531cf6c1df5f56c544 100644 (file)
@@ -31,17 +31,26 @@ defmodule Pleroma.Web.Websub do
     do
       changeset = Changeset.change(subscription, %{state: "active"})
       Repo.update(changeset)
-    else _e ->
-      changeset = Changeset.change(subscription, %{state: "rejected"})
-      {:ok, subscription} = Repo.update(changeset)
+    else e ->
+      Logger.debug("Couldn't verify subscription")
+      Logger.debug(inspect(e))
       {:error, subscription}
     end
   end
 
-  def publish(topic, user, activity) do
+  @supported_activities [
+    "Create",
+    "Follow",
+    "Like",
+    "Announce",
+    "Undo",
+    "Delete"
+  ]
+  def publish(topic, user, %{data: %{"type" => type}} = activity) when type in @supported_activities do
     # TODO: Only send to still valid subscriptions.
     query = from sub in WebsubServerSubscription,
-    where: sub.topic == ^topic and sub.state == "active"
+      where: sub.topic == ^topic and sub.state == "active",
+      where: fragment("? > NOW()", sub.valid_until)
     subscriptions = Repo.all(query)
     Enum.each(subscriptions, fn(sub) ->
       response = user
@@ -49,21 +58,16 @@ defmodule Pleroma.Web.Websub do
       |> :xmerl.export_simple(:xmerl_xml)
       |> to_string
 
-      signature = sign(sub.secret || "", response)
-      Logger.debug(fn -> "Pushing #{topic} to #{sub.callback}" end)
-
-      Task.start(fn ->
-        with {:ok, %{status_code: code}} <- @httpoison.post(sub.callback, response, [
-          {"Content-Type", "application/atom+xml"},
-          {"X-Hub-Signature", "sha1=#{signature}"}
-        ], timeout: 10000, recv_timeout: 20000) do
-          Logger.debug(fn -> "Pushed to #{sub.callback}, code #{code}" end)
-        else e ->
-            Logger.debug(fn -> "Couldn't push to #{sub.callback}, #{inspect(e)}" end)
-        end
-      end)
+      data = %{
+        xml: response,
+        topic: topic,
+        callback: sub.callback,
+        secret: sub.secret
+      }
+      Pleroma.Web.Federator.enqueue(:publish_single_websub, data)
     end)
   end
+  def publish(_,_,_), do: ""
 
   def sign(secret, doc) do
     :crypto.hmac(:sha, secret, to_string(doc)) |> Base.encode16 |> String.downcase
@@ -94,7 +98,7 @@ defmodule Pleroma.Web.Websub do
 
       {:ok, websub}
     else {:error, reason} ->
-      Logger.debug("Couldn't create subscription.")
+      Logger.debug("Couldn't create subscription")
       Logger.debug(inspect(reason))
 
       {:error, reason}
@@ -218,12 +222,12 @@ defmodule Pleroma.Web.Websub do
     cut_off = NaiveDateTime.add(NaiveDateTime.utc_now, delta)
 
     query = from sub in WebsubClientSubscription,
-      where: sub.valid_until < ^cut_off and sub.state == "accepted"
+      where: sub.valid_until < ^cut_off
 
     subs = Repo.all(query)
 
-    Enum.map(subs, fn (sub) ->
-      request_subscription(sub)
+    Enum.each(subs, fn (sub) ->
+      Pleroma.Web.Federator.enqueue(:request_subscription, sub)
     end)
   end
 end