]> source.dussan.org Git - nextcloud-server.git/commitdiff
Move federated share notifications to ROS
authorJoas Schilling <coding@schilljs.com>
Wed, 19 Oct 2016 10:38:25 +0000 (12:38 +0200)
committerJoas Schilling <coding@schilljs.com>
Thu, 20 Oct 2016 10:14:59 +0000 (12:14 +0200)
Signed-off-by: Joas Schilling <coding@schilljs.com>
apps/federatedfilesharing/appinfo/app.php
apps/federatedfilesharing/lib/Notifier.php
lib/public/RichObjectStrings/definitions.json

index f54c47b7829ee7b0984c01661f18675380c92396..7f4e46019777a3a47e3dc588655e37dd767e88e8 100644 (file)
@@ -32,7 +32,8 @@ $app->registerSettings();
 $manager = \OC::$server->getNotificationManager();
 $manager->registerNotifier(function() {
        return new Notifier(
-               \OC::$server->getL10NFactory()
+               \OC::$server->getL10NFactory(),
+               \OC::$server->getContactsManager()
        );
 }, function() use ($l) {
        return [
index 62816ee929b7eecdec062e1b04c4ea6589605b57..2cbbea2da427dcc8506dc31c97d21374bea18daa 100644 (file)
 namespace OCA\FederatedFileSharing;
 
 
+use OC\HintException;
+use OC\Share\Helper;
+use OCP\Contacts\IManager;
+use OCP\L10N\IFactory;
 use OCP\Notification\INotification;
 use OCP\Notification\INotifier;
 
 class Notifier implements INotifier {
-       /** @var \OCP\L10N\IFactory */
+       /** @var IFactory */
        protected $factory;
+       /** @var IManager */
+       protected $contactsManager;
+       /** @var array */
+       protected $federatedContacts;
 
        /**
-        * @param \OCP\L10N\IFactory $factory
+        * @param IFactory $factory
+        * @param IManager $contactsManager
         */
-       public function __construct(\OCP\L10N\IFactory $factory) {
+       public function __construct(IFactory $factory, IManager $contactsManager) {
                $this->factory = $factory;
+               $this->contactsManager = $contactsManager;
        }
 
        /**
@@ -58,11 +68,34 @@ class Notifier implements INotifier {
                                $params = $notification->getSubjectParameters();
                                if ($params[0] !== $params[1] && $params[1] !== null) {
                                        $notification->setParsedSubject(
-                                               (string) $l->t('You received "/%3$s" as a remote share from %1$s (on behalf of %2$s)', $params)
+                                               $l->t('You received "%3$s" as a remote share from %1$s (on behalf of %2$s)', $params)
+                                       );
+                                       $notification->setRichSubject(
+                                               $l->t('You received {share} as a remote share from {user} (on behalf of {behalf})'),
+                                               [
+                                                       'share' => [
+                                                               'type' => 'pending-federated-share',
+                                                               'id' => $notification->getObjectId(),
+                                                               'name' => $params[2],
+                                                       ],
+                                                       'user' => $this->createRemoteUser($params[0]),
+                                                       'behalf' => $this->createRemoteUser($params[1]),
+                                               ]
                                        );
                                } else {
                                        $notification->setParsedSubject(
-                                               (string)$l->t('You received "/%3$s" as a remote share from %1$s', $params)
+                                               $l->t('You received "%3$s" as a remote share from %1$s', $params)
+                                       );
+                                       $notification->setRichSubject(
+                                               $l->t('You received {share} as a remote share from {user}'),
+                                               [
+                                                       'share' => [
+                                                               'type' => 'pending-federated-share',
+                                                               'id' => $notification->getObjectId(),
+                                                               'name' => $params[2],
+                                                       ],
+                                                       'user' => $this->createRemoteUser($params[0]),
+                                               ]
                                        );
                                }
 
@@ -92,4 +125,93 @@ class Notifier implements INotifier {
                                throw new \InvalidArgumentException();
                }
        }
+
+       /**
+        * @param string $cloudId
+        * @return array
+        */
+       protected function createRemoteUser($cloudId) {
+               $displayName = $cloudId;
+               try {
+                       list($user, $server) = Helper::splitUserRemote($cloudId);
+                       $displayName = $this->getDisplayName($user, $server);
+               } catch (HintException $e) {
+                       $user = $cloudId;
+                       $server = '';
+               }
+
+               return [
+                       'type' => 'user',
+                       'id' => $user,
+                       'name' => $displayName,
+                       'server' => $server,
+               ];
+       }
+
+       /**
+        * Try to find the user in the contacts
+        *
+        * @param string $user
+        * @param string $server
+        * @return string
+        * @throws \OutOfBoundsException when there is no contact for the id
+        */
+       protected function getDisplayName($user, $server) {
+               $server = strtolower(rtrim($server, '/'));
+
+               if (strpos($server, 'http://') === 0) {
+                       $server = substr($server, strlen('http://'));
+               } else if (strpos($server, 'https://') === 0) {
+                       $server = substr($server, strlen('https://'));
+               }
+
+               try {
+                       return $this->getDisplayNameFromContact($user . '@' . $server);
+               } catch (\OutOfBoundsException $e) {
+               }
+
+               try {
+                       $this->getDisplayNameFromContact($user . '@http://' . $server);
+               } catch (\OutOfBoundsException $e) {
+               }
+
+               try {
+                       $this->getDisplayNameFromContact($user . '@https://' . $server);
+               } catch (\OutOfBoundsException $e) {
+               }
+
+               return $user . '@' . $server;
+       }
+
+       /**
+        * Try to find the user in the contacts
+        *
+        * @param string $federatedCloudId
+        * @return string
+        * @throws \OutOfBoundsException when there is no contact for the id
+        */
+       protected function getDisplayNameFromContact($federatedCloudId) {
+               if (isset($this->federatedContacts[$federatedCloudId])) {
+                       if ($this->federatedContacts[$federatedCloudId] !== '') {
+                               return $this->federatedContacts[$federatedCloudId];
+                       } else {
+                               throw new \OutOfBoundsException('No contact found for federated cloud id');
+                       }
+               }
+
+               $addressBookEntries = $this->contactsManager->search($federatedCloudId, ['CLOUD']);
+               foreach ($addressBookEntries as $entry) {
+                       if (isset($entry['CLOUD'])) {
+                               foreach ($entry['CLOUD'] as $cloudID) {
+                                       if ($cloudID === $federatedCloudId) {
+                                               $this->federatedContacts[$federatedCloudId] = $entry['FN'];
+                                               return $entry['FN'];
+                                       }
+                               }
+                       }
+               }
+
+               $this->federatedContacts[$federatedCloudId] = '';
+               throw new \OutOfBoundsException('No contact found for federated cloud id');
+       }
 }
index 581572eb345420f5e9e80949d2d8d96743e1ee5d..222f6615cf48ccc1bed63e7d7faaff4bfb3f4f19 100644 (file)
       }
     }
   },
+  "pending-federated-share": {
+    "author": "Nextcloud",
+    "app": "dav",
+    "since": "9.2.0",
+    "parameters": {
+      "id": {
+        "since": "9.2.0",
+        "required": true,
+        "description": "The id used to identify the federated share on the instance",
+        "example": "42"
+      },
+      "name": {
+        "since": "9.2.0",
+        "required": true,
+        "description": "The name of the shared item which should be used in the visual representation",
+        "example": "file.txt"
+      }
+    }
+  },
   "systemtag": {
     "author": "Nextcloud",
     "app": "core",