Correct active state websub name.
[akkoma] / lib / pleroma / web / websub / websub.ex
index 7bdb778ad0621eb4608aab2a3328eb4058b21ece..2276ddeec822cebad1179f56334f4b5475541d83 100644 (file)
@@ -51,10 +51,12 @@ defmodule Pleroma.Web.Websub do
       signature = sign(sub.secret || "", response)
       Logger.debug(fn -> "Pushing to #{sub.callback}" end)
 
-      @httpoison.post(sub.callback, response, [
-            {"Content-Type", "application/atom+xml"},
-            {"X-Hub-Signature", "sha1=#{signature}"}
-          ])
+      Task.start(fn ->
+        @httpoison.post(sub.callback, response, [
+              {"Content-Type", "application/atom+xml"},
+              {"X-Hub-Signature", "sha1=#{signature}"}
+            ])
+      end)
     end)
   end
 
@@ -124,7 +126,7 @@ defmodule Pleroma.Web.Websub do
     topic = subscribed.info["topic"]
     # FIXME: Race condition, use transactions
     {:ok, subscription} = with subscription when not is_nil(subscription) <- Repo.get_by(WebsubClientSubscription, topic: topic) do
-      subscribers = [subscriber.ap_id, subscription.subscribers] |> Enum.uniq
+      subscribers = [subscriber.ap_id | subscription.subscribers] |> Enum.uniq
       change = Ecto.Changeset.change(subscription, %{subscribers: subscribers})
       Repo.update(change)
     else _e ->
@@ -202,4 +204,19 @@ defmodule Pleroma.Web.Websub do
       {:error, websub}
     end
   end
+
+  def refresh_subscriptions(delta \\ 60 * 60 * 24) do
+    Logger.debug("Refreshing subscriptions")
+
+    cut_off = NaiveDateTime.add(NaiveDateTime.utc_now, delta)
+
+    query = from sub in WebsubClientSubscription,
+      where: sub.valid_until < ^cut_off and sub.state == "accepted"
+
+    subs = Repo.all(query)
+
+    Enum.map(subs, fn (sub) ->
+      request_subscription(sub)
+    end)
+  end
 end