Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/nextcloud/admin_notifications.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJoas Schilling <coding@schilljs.com>2017-02-09 13:16:41 +0300
committerJoas Schilling <coding@schilljs.com>2017-02-09 13:16:41 +0300
commit9983a61a7fbdfe0b55e8224de5fbb92ff6e4ec93 (patch)
tree7156b9732210f5d34feddf7ca66f9aa85d75a245
parentef87c6770430da640a06d5669dc2cc2e60cc9e85 (diff)
Add an OCS endpoint to generate a notification
Signed-off-by: Joas Schilling <coding@schilljs.com>
-rw-r--r--appinfo/info.xml2
-rw-r--r--appinfo/routes.php28
-rw-r--r--lib/Controller/APIController.php107
3 files changed, 136 insertions, 1 deletions
diff --git a/appinfo/info.xml b/appinfo/info.xml
index 7563ecc..8274dba 100644
--- a/appinfo/info.xml
+++ b/appinfo/info.xml
@@ -16,7 +16,7 @@
<repository type="git">https://github.com/nextcloud/admin_notifications.git</repository>
<dependencies>
- <nextcloud min-version="12" max-version="12" />
+ <nextcloud min-version="11" max-version="12" />
</dependencies>
<types>
diff --git a/appinfo/routes.php b/appinfo/routes.php
new file mode 100644
index 0000000..7fcd45f
--- /dev/null
+++ b/appinfo/routes.php
@@ -0,0 +1,28 @@
+<?php
+/**
+ * @copyright Copyright (c) 2017 Joas Schilling <coding@schilljs.com>
+ *
+ * @author Joas Schilling <coding@schilljs.com>
+ *
+ * @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/>.
+ *
+ */
+
+return [
+ 'ocs' => [
+ ['name' => 'API#generateNotification', 'url' => '/api/v1/notifications/{userId}', 'verb' => 'POST'],
+ ],
+];
diff --git a/lib/Controller/APIController.php b/lib/Controller/APIController.php
new file mode 100644
index 0000000..e6b9266
--- /dev/null
+++ b/lib/Controller/APIController.php
@@ -0,0 +1,107 @@
+<?php
+/**
+ * @copyright Copyright (c) 2017 Joas Schilling <coding@schilljs.com>
+ *
+ * @author Joas Schilling <coding@schilljs.com>
+ *
+ * @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\AdminNotifications\Controller;
+
+use OCA\AdminNotifications\AppInfo\Application;
+use OCP\AppFramework\Http;
+use OCP\AppFramework\Http\DataResponse;
+use OCP\AppFramework\OCSController;
+use OCP\AppFramework\Utility\ITimeFactory;
+use OCP\IRequest;
+use OCP\IUser;
+use OCP\IUserManager;
+use OCP\Notification\IManager;
+
+class APIController extends OCSController {
+
+ /** @var ITimeFactory */
+ protected $timeFactory;
+
+ /** @var IUserManager */
+ protected $userManager;
+
+ /** @var IManager */
+ protected $notificationManager;
+
+ /**
+ * @param string $appName
+ * @param IRequest $request
+ * @param ITimeFactory $timeFactory
+ * @param IUserManager $userManager
+ * @param IManager $notificationManager
+ */
+ public function __construct($appName, IRequest $request, ITimeFactory $timeFactory, IUserManager $userManager, IManager $notificationManager) {
+ parent::__construct($appName, $request);
+
+ $this->timeFactory = $timeFactory;
+ $this->userManager = $userManager;
+ $this->notificationManager = $notificationManager;
+ }
+
+ /**
+ * @param string $userId
+ * @param string $shortMessage
+ * @param string $longMessage
+ * @return DataResponse
+ */
+ public function generateNotification($userId, $shortMessage, $longMessage) {
+
+ $user = $this->userManager->get($userId);
+
+ if (!$user instanceof IUser) {
+ return new DataResponse(null, Http::STATUS_NOT_FOUND);
+ }
+
+ if ($shortMessage === '' || strlen($shortMessage) > 255) {
+ return new DataResponse(null, Http::STATUS_BAD_REQUEST);
+ }
+
+ if ($longMessage !== '' && strlen($longMessage) > 4000) {
+ return new DataResponse(null, Http::STATUS_BAD_REQUEST);
+ }
+
+ $notification = $this->notificationManager->createNotification();
+ $time = $this->timeFactory->getTime();
+ $datetime = new \DateTime();
+ $datetime->setTimestamp($time);
+
+ try {
+ $notification->setApp(Application::APP_ID)
+ ->setUser($user->getUID())
+ ->setDateTime($datetime)
+ ->setObject(Application::APP_ID, dechex($time))
+ ->setSubject('ocs', [$shortMessage]);
+
+ if ($longMessage !== '') {
+ $notification->setMessage('ocs', [$longMessage]);
+ }
+
+ $this->notificationManager->notify($notification);
+ } catch (\InvalidArgumentException $e) {
+ return new DataResponse(null, Http::STATUS_INTERNAL_SERVER_ERROR);
+ }
+
+ return new DataResponse();
+ }
+}