|
33 | 33 | use OCP\Contacts\IManager; |
34 | 34 | use OCP\Federation\ICloudId; |
35 | 35 | use OCP\Federation\ICloudIdManager; |
| 36 | +use OCP\IURLGenerator; |
| 37 | +use OCP\IUserManager; |
36 | 38 |
|
37 | 39 | class CloudIdManager implements ICloudIdManager { |
38 | 40 | /** @var IManager */ |
39 | 41 | private $contactsManager; |
| 42 | + /** @var IURLGenerator */ |
| 43 | + private $urlGenerator; |
| 44 | + /** @var IUserManager */ |
| 45 | + private $userManager; |
40 | 46 |
|
41 | | - public function __construct(IManager $contactsManager) { |
| 47 | + public function __construct(IManager $contactsManager, IURLGenerator $urlGenerator, IUserManager $userManager) { |
42 | 48 | $this->contactsManager = $contactsManager; |
| 49 | + $this->urlGenerator = $urlGenerator; |
| 50 | + $this->userManager = $userManager; |
43 | 51 | } |
44 | 52 |
|
45 | 53 | /** |
@@ -103,24 +111,39 @@ protected function getDisplayNameFromContact(string $cloudId): ?string { |
103 | 111 |
|
104 | 112 | /** |
105 | 113 | * @param string $user |
106 | | - * @param string $remote |
| 114 | + * @param string|null $remote |
107 | 115 | * @return CloudId |
108 | 116 | */ |
109 | | - public function getCloudId(string $user, string $remote): ICloudId { |
110 | | - // TODO check what the correct url is for remote (asking the remote) |
111 | | - $fixedRemote = $this->fixRemoteURL($remote); |
112 | | - if (strpos($fixedRemote, 'http://') === 0) { |
113 | | - $host = substr($fixedRemote, strlen('http://')); |
114 | | - } elseif (strpos($fixedRemote, 'https://') === 0) { |
115 | | - $host = substr($fixedRemote, strlen('https://')); |
| 117 | + public function getCloudId(string $user, ?string $remote): ICloudId { |
| 118 | + if ($remote === null) { |
| 119 | + $remote = rtrim($this->removeProtocolFromUrl($this->urlGenerator->getAbsoluteURL('/')), '/'); |
| 120 | + $fixedRemote = $this->fixRemoteURL($remote); |
| 121 | + $localUser = $this->userManager->get($user); |
| 122 | + $displayName = !is_null($localUser) ? $localUser->getDisplayName() : ''; |
116 | 123 | } else { |
117 | | - $host = $fixedRemote; |
| 124 | + // TODO check what the correct url is for remote (asking the remote) |
| 125 | + $fixedRemote = $this->fixRemoteURL($remote); |
| 126 | + $host = $this->removeProtocolFromUrl($fixedRemote); |
| 127 | + $displayName = $this->getDisplayNameFromContact($user . '@' . $host); |
118 | 128 | } |
119 | 129 | $id = $user . '@' . $remote; |
120 | | - $displayName = $this->getDisplayNameFromContact($user . '@' . $host); |
121 | 130 | return new CloudId($id, $user, $fixedRemote, $displayName); |
122 | 131 | } |
123 | 132 |
|
| 133 | + /** |
| 134 | + * @param string $url |
| 135 | + * @return string |
| 136 | + */ |
| 137 | + private function removeProtocolFromUrl($url) { |
| 138 | + if (strpos($url, 'https://') === 0) { |
| 139 | + return substr($url, strlen('https://')); |
| 140 | + } elseif (strpos($url, 'http://') === 0) { |
| 141 | + return substr($url, strlen('http://')); |
| 142 | + } |
| 143 | + |
| 144 | + return $url; |
| 145 | + } |
| 146 | + |
124 | 147 | /** |
125 | 148 | * Strips away a potential file names and trailing slashes: |
126 | 149 | * - http://localhost |
|
0 commit comments