function registerEventHooks() { // Hook into the "saved" event to publish to PuSH when an entity is saved \Idno\Core\site()->addEventHook('saved', function (\Idno\Core\Event $event) { if ($object = $event->data()['object']) { /* @var \Idno\Common\Entity $object */ if ($object->isPublic()) { $url = $object->getURL(); \Idno\Core\PubSubHubbub::publish($url); } } }); }
function registerEventHooks() { // Hook into the "saved" event to publish to PuSH when an entity is saved \Idno\Core\Idno::site()->addEventHook('saved', function (\Idno\Core\Event $event) { $eventdata = $event->data(); if ($object = $eventdata['object']) { /* @var \Idno\Common\Entity $object */ if ($object instanceof \Idno\Entities\ActivityStreamPost) { if ($object->isPublic()) { \Idno\Core\PubSubHubbub::publish($object); } } } }); // Add PuSH headers to the top of the page \Idno\Core\Idno::site()->addEventHook('page/head', function (Event $event) { if (!empty(site()->config()->hub)) { $eventdata = $event->data(); header('Link: <' . site()->config()->hub . '>; rel="hub"', false); header('Link: <' . site()->template()->getCurrentURL() . '>; rel="self"', false); } }); // When we follow a user, try and subscribe to their hub \Idno\Core\Idno::site()->addEventHook('follow', function (\Idno\Core\Event $event) { $eventdata = $event->data(); $user = $eventdata['user']; $eventdata = $event->data(); $following = $eventdata['following']; if ($user instanceof \Idno\Entities\User && $following instanceof \Idno\Entities\User) { $url = $following->getURL(); // Find self reference from profile url if ($feed = $this->findSelf($url)) { $following->pubsub_self = $feed; if ($hubs = $this->discoverHubs($url)) { $pending = unserialize($user->pubsub_pending); if (!$pending) { $pending = new \stdClass(); } if (!is_array($pending->subscribe)) { $pending->subscribe = array(); } $pending->subscribe[] = $following->getUUID(); $user->pubsub_pending = serialize($pending); $user->save(); $following->pubsub_hub = $hubs[0]; $following->save(); $return = \Idno\Core\Webservice::post($following->pubsub_hub, array('hub.callback' => \Idno\Core\Idno::site()->config->url . 'pubsub/callback/' . $user->getID() . '/' . $following->getID(), 'hub.mode' => 'subscribe', 'hub.verify' => 'async', 'hub.topic' => $feed)); \Idno\Core\Idno::site()->logging->log("Pubsub: " . print_r($return, true)); } else { \Idno\Core\Idno::site()->logging->log("Pubsub: No hubs found"); } } } }); // Send unfollow notification to their hub \Idno\Core\Idno::site()->addEventHook('unfollow', function (\Idno\Core\Event $event) { $eventdata = $event->data(); $user = $eventdata['user']; $eventdata = $event->data(); $following = $eventdata['following']; if ($user instanceof \Idno\Entities\User && $following instanceof \Idno\Entities\User) { $url = $following->getURL(); $pending = unserialize($user->pubsub_pending); if (!$pending) { $pending = new \stdClass(); } if (!is_array($pending->subscribe)) { $pending->unsubscribe = array(); } $pending->unsubscribe[] = $following->getID(); $user->pubsub_pending = serialize($pending); $user->save(); $return = \Idno\Core\Webservice::post($following->pubsub_hub, array('hub.callback' => \Idno\Core\Idno::site()->config->url . 'pubsub/callback/' . $user->getID() . '/' . $following->getID(), 'hub.mode' => 'unsubscribe', 'hub.verify' => 'async', 'hub.topic' => $following->pubsub_self)); \Idno\Core\Idno::site()->logging->log("Pubsub: " . print_r($return, true)); } }); }