summaryrefslogtreecommitdiffstats
path: root/apps/federatedfilesharing
diff options
context:
space:
mode:
authorMorris Jobke <hey@morrisjobke.de>2016-10-27 15:30:58 +0200
committerGitHub <noreply@github.com>2016-10-27 15:30:58 +0200
commitd4969abc9d306b5b5539f4e7722fc5ee1c722082 (patch)
treeb495bf61e890e8aed244e9cff50fde4e9c38ca86 /apps/federatedfilesharing
parentba2b274fccdeb06ea9ddba0590644653e073d934 (diff)
parentced3aeacb1aadfe592894ea666f0aa8777932597 (diff)
downloadnextcloud-server-d4969abc9d306b5b5539f4e7722fc5ee1c722082.tar.gz
nextcloud-server-d4969abc9d306b5b5539f4e7722fc5ee1c722082.zip
Merge pull request #1800 from nextcloud/nextcloud-rich-object-strings
Nextcloud rich object strings
Diffstat (limited to 'apps/federatedfilesharing')
-rw-r--r--apps/federatedfilesharing/appinfo/app.php3
-rw-r--r--apps/federatedfilesharing/lib/Notifier.php132
2 files changed, 129 insertions, 6 deletions
diff --git a/apps/federatedfilesharing/appinfo/app.php b/apps/federatedfilesharing/appinfo/app.php
index f54c47b7829..7f4e4601977 100644
--- a/apps/federatedfilesharing/appinfo/app.php
+++ b/apps/federatedfilesharing/appinfo/app.php
@@ -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 [
diff --git a/apps/federatedfilesharing/lib/Notifier.php b/apps/federatedfilesharing/lib/Notifier.php
index 62816ee929b..2cbbea2da42 100644
--- a/apps/federatedfilesharing/lib/Notifier.php
+++ b/apps/federatedfilesharing/lib/Notifier.php
@@ -24,18 +24,28 @@
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');
+ }
}