From: Roger Braun Date: Wed, 10 May 2017 16:44:06 +0000 (+0200) Subject: Refresh subscriptions. X-Git-Url: https://git.squeep.com/?a=commitdiff_plain;h=2e753e8cd740d540eae7badf5c48197d3914b5f5;p=akkoma Refresh subscriptions. --- diff --git a/lib/pleroma/web/federator/federator.ex b/lib/pleroma/web/federator/federator.ex index ab3313de1..fbf6ea6be 100644 --- a/lib/pleroma/web/federator/federator.ex +++ b/lib/pleroma/web/federator/federator.ex @@ -1,7 +1,7 @@ defmodule Pleroma.Web.Federator do use GenServer alias Pleroma.User - alias Pleroma.Web.WebFinger + alias Pleroma.Web.{WebFinger, Websub} require Logger @websub Application.get_env(:pleroma, :websub) @@ -9,14 +9,27 @@ defmodule Pleroma.Web.Federator do @max_jobs 10 def start_link do + spawn(fn -> + Process.sleep(1000 * 60 * 1) # 10 minutes + enqueue(:refresh_subscriptions, nil) + end) GenServer.start_link(__MODULE__, {:sets.new(), :queue.new()}, name: __MODULE__) end + def handle(:refresh_subscriptions, _) do + Logger.debug("Federator running refresh subscriptions") + Websub.refresh_subscriptions() + spawn(fn -> + Process.sleep(1000 * 60 * 60) # 60 minutes + enqueue(:refresh_subscriptions, nil) + end) + end + def handle(:publish, activity) do 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(fn -> "Sending #{activity.data["id"]} out via websub" end) - Pleroma.Web.Websub.publish(Pleroma.Web.OStatus.feed_path(actor), actor, activity) + Websub.publish(Pleroma.Web.OStatus.feed_path(actor), actor, activity) {:ok, actor} = WebFinger.ensure_keys_present(actor) Logger.debug(fn -> "Sending #{activity.data["id"]} out via salmon" end) diff --git a/lib/pleroma/web/websub/websub.ex b/lib/pleroma/web/websub/websub.ex index 7c8efa917..5468758fd 100644 --- a/lib/pleroma/web/websub/websub.ex +++ b/lib/pleroma/web/websub/websub.ex @@ -204,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 == "active" + + subs = Repo.all(query) + + Enum.map(subs, fn (sub) -> + request_subscription(sub) + end) + end end diff --git a/test/web/websub/websub_test.exs b/test/web/websub/websub_test.exs index 48774dc69..6f228bccc 100644 --- a/test/web/websub/websub_test.exs +++ b/test/web/websub/websub_test.exs @@ -7,7 +7,7 @@ end defmodule Pleroma.Web.WebsubTest do use Pleroma.DataCase alias Pleroma.Web.Websub - alias Pleroma.Web.Websub.WebsubServerSubscription + alias Pleroma.Web.Websub.{WebsubServerSubscription, WebsubClientSubscription} import Pleroma.Factory alias Pleroma.Web.Router.Helpers @@ -174,4 +174,18 @@ defmodule Pleroma.Web.WebsubTest do signed = Websub.sign("secret", [["て"], ['す']]) end + + describe "renewing subscriptions" do + test "it renews subscriptions that have less than a day of time left" do + day = 60 * 60 * 24 + now = NaiveDateTime.utc_now + still_good = insert(:websub_client_subscription, %{valid_until: NaiveDateTime.add(now, 2 * day), topic: "http://example.org/still_good", state: "active"}) + needs_refresh = insert(:websub_client_subscription, %{valid_until: NaiveDateTime.add(now, day - 100), topic: "http://example.org/needs_refresh", state: "active"}) + + refresh = Websub.refresh_subscriptions() + + assert still_good == Repo.get(WebsubClientSubscription, still_good.id) + refute needs_refresh == Repo.get(WebsubClientSubscription, needs_refresh.id) + end + end end