Skip to content

Commit

Permalink
Add cli tool to unsubscribe from a topic
Browse files Browse the repository at this point in the history
  • Loading branch information
cweiske committed Mar 6, 2020
1 parent 973ef8c commit c32d1b6
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 3 deletions.
66 changes: 66 additions & 0 deletions bin/unsubscribe.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
#!/usr/bin/env php
<?php
namespace phinde;
/**
* Unsubscribe from a WebSub subscription
*/
require_once __DIR__ . '/../src/init.php';

$cc = new \Console_CommandLine();
$cc->description = 'Unsubscribe from a WebSub subscription';
$cc->version = '0.0.1';
$cc->addArgument(
'url',
array(
'description' => 'URL to process',
'multiple' => false
)
);
try {
$res = $cc->parse();
} catch (\Exception $e) {
$cc->displayError($e->getMessage());
}

$subDb = new Subscriptions();

$url = $res->args['url'];
$url = Helper::addSchema($url);
$urlObj = new \Net_URL2($url);
$topic = $urlObj->getNormalizedURL();


$sub = $subDb->get($topic);
if ($sub === false) {
Log::error("No existing subscription for URL");
exit(2);
}
if ($sub->sub_status === 'unsubscribed') {
Log::info('Already unsubscribed');
exit(0);
}

$subDb->unsubscribing($sub->sub_id);

$callbackUrl = $GLOBALS['phinde']['baseurl'] . 'push-subscription.php'
. '?hub.topic=' . urlencode($topic)
. '&capkey=' . urlencode($sub->sub_capkey);
$req = new HttpRequest($sub->sub_hub, 'POST');
$req->addPostParameter('hub.callback', $callbackUrl);
$req->addPostParameter('hub.mode', 'unsubscribe');
$req->addPostParameter('hub.topic', $topic);
$req->addPostParameter('hub.lease_seconds', $sub->sub_lease_seconds);
$req->addPostParameter('hub.secret', $sub->sub_secret);
$res = $req->send();

if (intval($res->getStatus()) == 202) {
Log::info('Unsubscription initiated');
exit(0);
}

Log::error(
'Error: Unsubscription response status code was not 202 but '
. $res->getStatus()
);
Log::error($res->getBody());
?>
20 changes: 17 additions & 3 deletions src/phinde/Subscriptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -181,22 +181,36 @@ public function subscribed($subId, $leaseSeconds)
}

/**
* 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
*
Expand Down

0 comments on commit c32d1b6

Please sign in to comment.