Remove unneccesary subscriptions on update.
authorlain <lain@soykaf.club>
Sat, 24 Feb 2018 16:36:02 +0000 (17:36 +0100)
committerlain <lain@soykaf.club>
Sat, 24 Feb 2018 16:36:02 +0000 (17:36 +0100)
lib/pleroma/web/activity_pub/transmogrifier.ex
test/web/activity_pub/transmogrifier_test.exs

index b64b69565457095baabc39741b1b0a5766d9a1ab..2e5ca70fd63465684aedae6faaba8a0c73448fb1 100644 (file)
@@ -204,6 +204,8 @@ defmodule Pleroma.Web.ActivityPub.Transmogrifier do
     update: [set: [following: fragment("array_replace(?,?,?)", u.following, ^old_follower_address, ^user.follower_address)]]
     Repo.update_all(q, [])
 
+    maybe_retire_websub(user.ap_id)
+
     # Only do this for recent activties, don't go through the whole db.
     since = (Repo.aggregate(Activity, :max, :id) || 0) - 100_000
     q  = from a in Activity,
@@ -236,4 +238,13 @@ defmodule Pleroma.Web.ActivityPub.Transmogrifier do
       e -> e
     end
   end
+
+  def maybe_retire_websub(ap_id) do
+    # some sanity checks
+    if is_binary(ap_id) && (String.length(ap_id) > 8) do
+      q = from ws in Pleroma.Web.Websub.WebsubClientSubscription,
+        where: fragment("? like ?", ws.topic, ^"#{ap_id}%")
+      Repo.delete_all(q)
+    end
+  end
 end
index 297d48f425ec8dd3d09129436249b0f410cf5c7c..2e500d11dee9e216eb2424f2bc3b031f3217d7aa 100644 (file)
@@ -4,6 +4,8 @@ defmodule Pleroma.Web.ActivityPub.TransmogrifierTest do
   alias Pleroma.Activity
   alias Pleroma.User
   alias Pleroma.Repo
+  alias Pleroma.Web.Websub.WebsubClientSubscription
+  alias Pleroma.Web.Websub.WebsubServerSubscription
   import Ecto.Query
 
   import Pleroma.Factory
@@ -216,4 +218,32 @@ defmodule Pleroma.Web.ActivityPub.TransmogrifierTest do
       refute "..." in user_two.following
     end
   end
+
+  describe "maybe_retire_websub" do
+    test "it deletes all websub client subscripitions with the user as topic" do
+      subscription = %WebsubClientSubscription{topic: "https://niu.moe/users/rye.atom"}
+      {:ok, ws} = Repo.insert(subscription)
+
+      subscription = %WebsubClientSubscription{topic: "https://niu.moe/users/pasty.atom"}
+      {:ok, ws2} = Repo.insert(subscription)
+
+      Transmogrifier.maybe_retire_websub("https://niu.moe/users/rye")
+
+      refute Repo.get(WebsubClientSubscription, ws.id)
+      assert Repo.get(WebsubClientSubscription, ws2.id)
+    end
+
+    test "it deletes all websub server subscriptions with the server as callback" do
+      subscription = %WebsubClientSubscription{topic: "https://niu.moe/users/rye.atom"}
+      {:ok, ws} = Repo.insert(subscription)
+
+      subscription = %WebsubClientSubscription{topic: "https://niu.moe/users/pasty.atom"}
+      {:ok, ws2} = Repo.insert(subscription)
+
+      Transmogrifier.maybe_retire_websub("https://niu.moe/users/rye")
+
+      refute Repo.get(WebsubClientSubscription, ws.id)
+      assert Repo.get(WebsubClientSubscription, ws2.id)
+    end
+  end
 end