link websub on status page
[phinde.git] / src / phinde / Subscriptions.php
index 4d00ab806056d56e185007433a607a2c2ac4b5b6..403f5d420e9bc3d7954316b2e93d043c398055dd 100644 (file)
@@ -37,6 +37,69 @@ class Subscriptions
         return $stmt->fetchObject();
     }
 
+    /**
+     * Remove a topic
+     *
+     * @param string $topic Topic URL
+     *
+     * @return void
+     */
+    public function remove($topic)
+    {
+        $stmt = $this->db->prepare(
+            'DELETE FROM subscriptions'
+            . ' WHERE sub_topic = :topic'
+        );
+        $stmt->execute([':topic' => $topic]);
+    }
+
+    /**
+     * Count number of subscriptions
+     *
+     * @return array Array of keys with different status, number as value
+     */
+    public function count()
+    {
+        $stmt = $this->db->prepare(
+            'SELECT COUNT(*) as count, sub_status FROM subscriptions'
+            . ' GROUP BY sub_status'
+            . ' ORDER BY sub_status'
+        );
+        $stmt->execute();
+
+        $res = [];
+        foreach ($stmt as $row) {
+            $res[$row['sub_status']] = $row['count'];
+        }
+
+        return $res;
+    }
+
+    /**
+     * Get all topics that either expired or expire soon
+     *
+     * @return \PDOStatement Result iterator
+     */
+    public function getExpiring()
+    {
+        $stmt = $this->db->prepare(
+            'SELECT * FROM subscriptions'
+            . ' WHERE'
+            . '('
+            //expire soon
+            . '  sub_status IN ("active", "expired")'
+            . '  AND DATEDIFF(sub_expires, NOW()) <= 2'
+            . ') OR ('
+            //no reaction to subscription within 10 minutes
+            . '  sub_status = "subscribing"'
+            . '  AND TIMEDIFF(NOW(), sub_created) > "00:10:00"'
+            . ')'
+        );
+        $stmt->execute();
+
+        return $stmt;
+    }
+
     /**
      * Create a new subscription entry in database.
      * Automatically generates secret, capkey and lease seconds.
@@ -47,19 +110,20 @@ class Subscriptions
      * - send subscription requests to the hub
      *
      * @param string $topic URL to subscribe to
+     * @param string $hub   URL of the hub subscribing to
      *
      * @return void
      */
-    public function create($topic)
+    public function create($topic, $hub)
     {
         $stmt = $this->db->prepare(
             'INSERT INTO subscriptions'
             . ' (sub_topic, sub_status, sub_lease_seconds, sub_expires'
-            . ', sub_secret, sub_capkey, sub_created, sub_updated'
+            . ', sub_secret, sub_capkey, sub_hub, sub_created, sub_updated'
             . ', sub_pings, sub_lastping, sub_statusmessage)'
             . ' VALUES '
             . ' (:topic, "subscribing", :lease_seconds, "0000-00-00 00:00:00"'
-            . ', :secret, :capkey, NOW(), NOW()'
+            . ', :secret, :capkey, :hub, NOW(), NOW()'
             . ', 0, "0000-00-00 00:00:00", "")'
         );
         $stmt->execute(
@@ -68,10 +132,28 @@ class Subscriptions
                 ':lease_seconds' => 86400 * 30,
                 ':secret'        => bin2hex(openssl_random_pseudo_bytes(16)),
                 ':capkey'        => bin2hex(openssl_random_pseudo_bytes(16)),
+                ':hub'           => $hub,
             ]
         );
     }
 
+    /**
+     * Renew a subscription: Set its status to "subscribing"
+     *
+     * @param integer $subId Subscription ID
+     *
+     * @return void
+     */
+    public function renew($subId)
+    {
+        $this->db->prepare(
+            'UPDATE subscriptions'
+            . ' SET sub_status  = "subscribing"'
+            . '   , sub_updated = NOW()'
+            . ' WHERE sub_id = :id'
+        )->execute([':id' => $subId]);
+    }
+
     /**
      * A subscription has been confirmed by the hub - mark it as active.
      *
@@ -99,22 +181,44 @@ class Subscriptions
     }
 
     /**
-     * Mark a subscription as "unsubscribed"
+     * Begin removal of a a subscription: Set its status to "unsubscribing"
      *
      * @param integer $subId Subscription ID
      *
      * @return void
      */
-    public function unsubscribed($subId)
+    public function unsubscribing($subId)
     {
         $this->db->prepare(
             'UPDATE subscriptions'
-            . ' SET sub_status = "unsubscribed"'
+            . ' SET sub_status  = "unsubscribing"'
             . '   , sub_updated = NOW()'
             . ' WHERE sub_id = :id'
         )->execute([':id' => $subId]);
     }
 
+    /**
+     * Mark a subscription as "unsubscribed" - delete it
+     *
+     * @param integer $subId Subscription ID
+     *
+     * @return void
+     */
+    public function unsubscribed($subId)
+    {
+        $this->db
+            ->prepare('DELETE FROM subscriptions WHERE sub_id = :id')
+            ->execute([':id' => $subId]);
+    }
+
+    /**
+     * Subscription has been cancelled/denied for some reason
+     *
+     * @param integer $subId  Subscription ID
+     * @param string  $reason Cancellation reason
+     *
+     * @return void
+     */
     public function denied($subId, $reason)
     {
         $this->db->prepare(
@@ -126,6 +230,13 @@ class Subscriptions
         )->execute([':id' => $subId, ':reason' => $reason]);
     }
 
+    /**
+     * Topic update notification has been received
+     *
+     * @param integer $subId  Subscription ID
+     *
+     * @return void
+     */
     public function pinged($subId)
     {
         $this->db->prepare(