Extract note handler.
[akkoma] / lib / pleroma / web / websub / websub.ex
index a5abc303c1feb1e2f533b13a863d3fa3664ad6f1..e46e0a2ce19082c9e3f718f367d1f5912e8cae75 100644 (file)
@@ -39,6 +39,7 @@ defmodule Pleroma.Web.Websub do
   end
 
   def publish(topic, user, activity) do
+    # TODO: Only send to still valid subscriptions.
     query = from sub in WebsubServerSubscription,
     where: sub.topic == ^topic and sub.state == "active"
     subscriptions = Repo.all(query)
@@ -126,7 +127,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 ->
@@ -204,4 +205,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