Skip to content
Snippets Groups Projects
Commit 6371f9ef authored by Robin Appelman's avatar Robin Appelman
Browse files

poc pushing of file events

parent df68d895
No related branches found
No related tags found
No related merge requests found
<?xml version="1.0"?>
<info xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://apps.nextcloud.com/schema/apps/info.xsd">
<id>push_ws</id>
<id>notify_push</id>
<name>Socket Push</name>
<summary>Push update support for desktop app</summary>
<description><![CDATA[Push update support for desktop app]]></description>
<version>0.1.0</version>
<licence>agpl</licence>
<author>Robin Appelman</author>
<namespace>PushWS</namespace>
<default_enable/>
<namespace>NotifyPush</namespace>
<types>
<filesystem/>
</types>
<bugs>https://github.com/nextcloud/push_ws/issues</bugs>
......
<?php
declare(strict_types=1);
/**
* @copyright Copyright (c) 2020 Robin Appelman <robin@icewind.nl>
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
namespace OCA\NotifyPush\AppInfo;
use OC\AppFramework\Utility\SimpleContainer;
use OC\RedisFactory;
use OCA\NotifyPush\Capabilities;
use OCA\NotifyPush\Listener;
use OCA\NotifyPush\Queue\IQueue;
use OCA\NotifyPush\Queue\NullQueue;
use OCA\NotifyPush\Queue\RedisQueue;
use OCP\AppFramework\App;
use OCP\AppFramework\Bootstrap\IBootContext;
use OCP\AppFramework\Bootstrap\IBootstrap;
use OCP\AppFramework\Bootstrap\IRegistrationContext;
use OCP\EventDispatcher\IEventDispatcher;
use OCP\Files\Cache\CacheInsertEvent;
use OCP\Files\Cache\CacheRemoveEvent;
use OCP\Files\Cache\CacheUpdateEvent;
class Application extends App implements IBootstrap {
public const APP_ID = 'notify_push';
public function __construct() {
parent::__construct(self::APP_ID);
}
public function register(IRegistrationContext $context): void {
$context->registerCapability(Capabilities::class);
$context->registerService(IQueue::class, function(SimpleContainer $c) {
/** @var RedisFactory $redisFactory */
$redisFactory = $c->get(RedisFactory::class);
if ($redisFactory->isAvailable()) {
return new RedisQueue($redisFactory->getInstance());
} else {
return new NullQueue();
}
});
}
public function boot(IBootContext $context): void {
$context->injectFn([$this, 'attachHooks']);
}
public function attachHooks(
IEventDispatcher $eventDispatcher,
Listener $listener
) {
$eventDispatcher->addListener(CacheInsertEvent::class, [$listener, 'cacheListener']);
$eventDispatcher->addListener(CacheUpdateEvent::class, [$listener, 'cacheListener']);
$eventDispatcher->addListener(CacheRemoveEvent::class, [$listener, 'cacheListener']);
}
}
<?php
declare(strict_types=1);
/**
* @copyright Copyright (c) 2020 Robin Appelman <robin@icewind.nl>
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
namespace OCA\NotifyPush;
use OCP\Capabilities\ICapability;
use OCP\IConfig;
class Capabilities implements ICapability {
private $config;
public function __construct(IConfig $config) {
$this->config = $config;
}
/**
* Return this classes capabilities
*
* @return array
*/
public function getCapabilities() {
return [
'notify_push' => [
'endpoints' => [
'websocket' => $this->config->getAppValue('notify_push', 'base_endpoint') . '/ws',
],
],
];
}
}
<?php
declare(strict_types=1);
/**
* @copyright Copyright (c) 2020 Robin Appelman <robin@icewind.nl>
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
namespace OCA\NotifyPush;
use OCA\NotifyPush\Queue\IQueue;
use OCP\Files\Cache\ICacheEvent;
class Listener {
private $queue;
public function __construct(IQueue $queue) {
$this->queue = $queue;
}
public function cacheListener(ICacheEvent $event) {
$this->queue->push('notify_file_update', [
'storage' => $event->getStorageId(),
'id' => $event->getFileId(),
]);
}
}
<?php
declare(strict_types=1);
/**
* @copyright Copyright (c) 2020 Robin Appelman <robin@icewind.nl>
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
namespace OCA\NotifyPush\Queue;
interface IQueue {
public function push(string $channel, $message);
}
<?php
declare(strict_types=1);
/**
* @copyright Copyright (c) 2020 Robin Appelman <robin@icewind.nl>
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
namespace OCA\NotifyPush\Queue;
class NullQueue implements IQueue {
public function push(string $channel, $message) {
// noop
}
}
<?php
declare(strict_types=1);
/**
* @copyright Copyright (c) 2020 Robin Appelman <robin@icewind.nl>
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
namespace OCA\NotifyPush\Queue;
class RedisQueue implements IQueue {
private $redis;
public function __construct(\Redis $redis) {
$this->redis = $redis;
}
public function push(string $channel, $message) {
$this->redis->publish($channel, json_encode($message));
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment