-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Node Events *********** ``node_event`` - events about File `Nodes` Supported event sub-types: * NodeCreatedEvent * NodeTouchedEvent * NodeWrittenEvent * NodeDeletedEvent * NodeRenamedEvent * NodeCopiedEvent Signed-off-by: Alexander Piskun <bigcat88@icloud.com>
- Loading branch information
Showing
17 changed files
with
718 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
.. _events_listener: | ||
|
||
=============== | ||
Events Listener | ||
=============== | ||
|
||
This API allows you to listen to `Nextcloud events <https://docs.nextcloud.com/server/latest/developer_manual/basics/events.html#events>`_ | ||
|
||
Currently only **limited** numbers of events are supported. | ||
|
||
Please let us know if there are any specific event we should add support to. | ||
|
||
.. note:: | ||
|
||
Unlike PHP events, all information from events comes to ExApp **asynchronously**, more like a notification system | ||
to no slow down the server. | ||
|
||
Register | ||
^^^^^^^^ | ||
|
||
OCS endpoint: ``POST /apps/app_api/api/v1/events_listener`` | ||
|
||
Params | ||
****** | ||
|
||
.. code-block:: json | ||
{ | ||
"eventType": "node_event", | ||
"actionHandler": "/action_handler_route" | ||
"eventSubtypes": [], | ||
} | ||
.. note:: ``eventSubtypes`` is an optional parameter, when it is not specified all event subtypes will be propagated to ExApp. | ||
|
||
Url in ``actionHandler`` is relative to the ExApp root, starting slash is not required. | ||
|
||
Unregister | ||
^^^^^^^^^^ | ||
|
||
OCS endpoint: ``DELETE /apps/app_api/api/v1/events_listener`` | ||
|
||
Params | ||
****** | ||
|
||
To unregister EventsListener, you just need to provide an `eventType` of the registered EventsListener: | ||
|
||
.. code-block:: json | ||
{ | ||
"eventType": "node_event" | ||
} | ||
Event payload | ||
^^^^^^^^^^^^^ | ||
|
||
.. code-block:: json | ||
{ | ||
"event_type": "node_event", | ||
"event_subtype": "NodeCreatedEvent", | ||
"event_data": "associative array depending on `event_subtype`" | ||
} | ||
Events types | ||
^^^^^^^^^^^^ | ||
|
||
Node Events | ||
*********** | ||
|
||
``node_event`` - events about File `Nodes` | ||
|
||
Supported event sub-types: | ||
* ``NodeCreatedEvent`` | ||
* ``NodeTouchedEvent`` | ||
* ``NodeWrittenEvent`` | ||
* ``NodeDeletedEvent`` | ||
* ``NodeRenamedEvent`` | ||
* ``NodeCopiedEvent`` | ||
|
||
For all Node events ``event_data`` contain key **target** which has the same format like in :ref:`FileActionsMenu payload <node_info>` | ||
|
||
For ``NodeCopiedEvent`` and ``NodeRenamedEvent`` there is also a ``source`` key in the same format. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace OCA\AppAPI\Controller; | ||
|
||
use OCA\AppAPI\AppInfo\Application; | ||
use OCA\AppAPI\Attribute\AppAPIAuth; | ||
use OCA\AppAPI\Service\ExAppEventsListenerService; | ||
use OCP\AppFramework\Http; | ||
use OCP\AppFramework\Http\Attribute\NoCSRFRequired; | ||
use OCP\AppFramework\Http\Attribute\PublicPage; | ||
use OCP\AppFramework\Http\DataResponse; | ||
use OCP\AppFramework\OCSController; | ||
use OCP\IRequest; | ||
|
||
class EventsListenerController extends OCSController { | ||
protected $request; | ||
|
||
public function __construct( | ||
IRequest $request, | ||
private readonly ExAppEventsListenerService $service, | ||
) { | ||
parent::__construct(Application::APP_ID, $request); | ||
|
||
$this->request = $request; | ||
} | ||
|
||
#[NoCSRFRequired] | ||
#[PublicPage] | ||
#[AppAPIAuth] | ||
public function registerListener(string $eventType, string $actionHandler, array $eventSubtypes = []): DataResponse { | ||
$listener = $this->service->registerEventsListener( | ||
$this->request->getHeader('EX-APP-ID'), $eventType, $actionHandler, $eventSubtypes); | ||
if ($listener === null) { | ||
return new DataResponse([], Http::STATUS_BAD_REQUEST); | ||
} | ||
return new DataResponse(); | ||
} | ||
|
||
#[NoCSRFRequired] | ||
#[PublicPage] | ||
#[AppAPIAuth] | ||
public function unregisterListener(string $eventType): DataResponse { | ||
$unregistered = $this->service->unregisterEventsListener($this->request->getHeader('EX-APP-ID'), $eventType); | ||
if (!$unregistered) { | ||
return new DataResponse([], Http::STATUS_NOT_FOUND); | ||
} | ||
return new DataResponse(); | ||
} | ||
|
||
#[AppAPIAuth] | ||
#[PublicPage] | ||
#[NoCSRFRequired] | ||
public function getListener(string $eventType): DataResponse { | ||
$result = $this->service->getEventsListener($this->request->getHeader('EX-APP-ID'), $eventType); | ||
if (!$result) { | ||
return new DataResponse([], Http::STATUS_NOT_FOUND); | ||
} | ||
return new DataResponse($result, Http::STATUS_OK); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace OCA\AppAPI\Db; | ||
|
||
use JsonSerializable; | ||
use OCP\AppFramework\Db\Entity; | ||
|
||
/** | ||
* Class ExAppEventsListener | ||
* | ||
* @package OCA\AppAPI\Db | ||
* | ||
* @method string getAppid() | ||
* @method string getEventType() | ||
* @method array getEventSubtypes() | ||
* @method string getActionHandler() | ||
* @method void setAppid(string $appid) | ||
* @method void setEventType(string $eventType) | ||
* @method void setEventSubtypes(array $eventSubtypes) | ||
* @method void setActionHandler(string $actionHandler) | ||
*/ | ||
class ExAppEventsListener extends Entity implements JsonSerializable { | ||
protected $appid; | ||
protected $eventType; | ||
protected $eventSubtypes; | ||
protected $icon; | ||
protected $actionHandler; | ||
|
||
/** | ||
* @param array $params | ||
*/ | ||
public function __construct(array $params = []) { | ||
$this->addType('appid', 'string'); | ||
$this->addType('eventType', 'string'); | ||
$this->addType('eventSubtypes', 'json'); | ||
$this->addType('actionHandler', 'string'); | ||
|
||
if (isset($params['id'])) { | ||
$this->setId($params['id']); | ||
} | ||
if (isset($params['appid'])) { | ||
$this->setAppid($params['appid']); | ||
} | ||
if (isset($params['event_type'])) { | ||
$this->setEventType($params['event_type']); | ||
} | ||
if (isset($params['event_subtypes'])) { | ||
$this->setEventSubtypes($params['event_subtypes']); | ||
} | ||
if (isset($params['action_handler'])) { | ||
$this->setActionHandler($params['action_handler']); | ||
} | ||
} | ||
|
||
public function jsonSerialize(): array { | ||
return [ | ||
'id' => $this->getId(), | ||
'appid' => $this->getAppid(), | ||
'event_type' => $this->getEventType(), | ||
'event_subtypes' => $this->getEventSubtypes(), | ||
'action_handler' => $this->getActionHandler(), | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace OCA\AppAPI\Db; | ||
|
||
use OCP\AppFramework\Db\DoesNotExistException; | ||
use OCP\AppFramework\Db\MultipleObjectsReturnedException; | ||
use OCP\AppFramework\Db\QBMapper; | ||
use OCP\DB\Exception; | ||
use OCP\DB\QueryBuilder\IQueryBuilder; | ||
use OCP\IDBConnection; | ||
|
||
/** | ||
* @template-extends QBMapper<ExAppEventsListener> | ||
*/ | ||
class ExAppEventsListenerMapper extends QBMapper { | ||
public function __construct(IDBConnection $db) { | ||
parent::__construct($db, 'ex_event_handlers'); | ||
} | ||
|
||
/** | ||
* @throws Exception | ||
*/ | ||
public function findAllEnabled(): array { | ||
$qb = $this->db->getQueryBuilder(); | ||
$result = $qb->select('exs.*') | ||
->from($this->tableName, 'exs') | ||
->innerJoin('exs', 'ex_apps', 'exa', 'exa.appid = exs.appid') | ||
->where( | ||
$qb->expr()->eq('exa.enabled', $qb->createNamedParameter(1, IQueryBuilder::PARAM_INT)) | ||
) | ||
->executeQuery(); | ||
return $result->fetchAll(); | ||
} | ||
|
||
public function removeByAppIdEventType(string $appId, string $eventType): bool { | ||
$qb = $this->db->getQueryBuilder(); | ||
$qb->delete($this->tableName) | ||
->where( | ||
$qb->expr()->eq('appid', $qb->createNamedParameter($appId, IQueryBuilder::PARAM_STR)), | ||
$qb->expr()->eq('event_type', $qb->createNamedParameter($eventType, IQueryBuilder::PARAM_STR)) | ||
); | ||
try { | ||
$result = $qb->executeStatement(); | ||
if ($result) { | ||
return true; | ||
} | ||
} catch (Exception) { | ||
} | ||
return false; | ||
} | ||
|
||
/** | ||
* @throws Exception | ||
*/ | ||
public function removeAllByAppId(string $appId): int { | ||
$qb = $this->db->getQueryBuilder(); | ||
$qb->delete($this->tableName) | ||
->where( | ||
$qb->expr()->eq('appid', $qb->createNamedParameter($appId, IQueryBuilder::PARAM_STR)) | ||
); | ||
return $qb->executeStatement(); | ||
} | ||
|
||
/** | ||
* @param string $appId | ||
* @param string $eventType | ||
* | ||
* @return ExAppEventsListener | ||
* @throws Exception | ||
* @throws MultipleObjectsReturnedException if more than one result | ||
* @throws DoesNotExistException if not found | ||
*/ | ||
public function findByAppIdEventType(string $appId, string $eventType): ExAppEventsListener { | ||
$qb = $this->db->getQueryBuilder(); | ||
$qb->select('*') | ||
->from($this->tableName) | ||
->where( | ||
$qb->expr()->eq('appid', $qb->createNamedParameter($appId, IQueryBuilder::PARAM_STR)), | ||
$qb->expr()->eq('event_type', $qb->createNamedParameter($eventType, IQueryBuilder::PARAM_STR)) | ||
); | ||
return $this->findEntity($qb); | ||
} | ||
} |
Oops, something went wrong.