3434use OCP \Contacts \IManager ;
3535use OCP \EventDispatcher \Event ;
3636use OCP \EventDispatcher \IEventDispatcher ;
37+ use OCP \Federation \CloudId ;
3738use OCP \Federation \ICloudId ;
3839use OCP \Federation \ICloudIdManager ;
40+ use OCP \Federation \ICloudIdResolver ;
3941use OCP \ICache ;
4042use OCP \ICacheFactory ;
4143use OCP \IURLGenerator ;
@@ -52,6 +54,8 @@ class CloudIdManager implements ICloudIdManager {
5254 private ICache $ memCache ;
5355 /** @var array[] */
5456 private array $ cache = [];
57+ /** @var ICloudIdResolver[] */
58+ private array $ cloudIdResolvers = [];
5559
5660 public function __construct (
5761 IManager $ contactsManager ,
@@ -65,7 +69,7 @@ public function __construct(
6569 $ this ->userManager = $ userManager ;
6670 $ this ->memCache = $ cacheFactory ->createDistributed ('cloud_id_ ' );
6771 $ eventDispatcher ->addListener (UserChangedEvent::class, [$ this , 'handleUserEvent ' ]);
68- $ eventDispatcher ->addListener (CardUpdatedEvent::class, [$ this , 'handleCardEvent ' ]);
72+ $ eventDispatcher ->addListener (CardUpdatedEvent::class, [$ this , 'handleCardEvent ' ]);
6973 }
7074
7175 public function handleUserEvent (Event $ event ): void {
@@ -100,6 +104,12 @@ public function handleCardEvent(Event $event): void {
100104 */
101105 public function resolveCloudId (string $ cloudId ): ICloudId {
102106 // TODO magic here to get the url and user instead of just splitting on @
107+
108+ foreach ($ this ->cloudIdResolvers as $ resolver ) {
109+ if ($ resolver ->isValidCloudId ($ cloudId )) {
110+ return $ resolver ->resolveCloudId ($ cloudId );
111+ }
112+ }
103113
104114 if (!$ this ->isValidCloudId ($ cloudId )) {
105115 throw new \InvalidArgumentException ('Invalid cloud id ' );
@@ -246,6 +256,28 @@ protected function fixRemoteURL(string $remote): string {
246256 * @return bool
247257 */
248258 public function isValidCloudId (string $ cloudId ): bool {
259+ foreach ($ this ->cloudIdResolvers as $ resolver ) {
260+ if ($ resolver ->isValidCloudId ($ cloudId )) {
261+ return true ;
262+ }
263+ }
264+
249265 return strpos ($ cloudId , '@ ' ) !== false ;
250266 }
267+
268+ /**
269+ * @param ICloudIdResolver $resolver
270+ */
271+ public function registerCloudIdResolver (ICloudIdResolver $ resolver ) {
272+ array_unshift ($ this ->cloudIdResolvers , $ resolver );
273+ }
274+
275+ /**
276+ * @param ICloudIdResolver $resolver
277+ */
278+ public function unregisterCloudIdResolver (ICloudIdResolver $ resolver ) {
279+ if (($ key = array_search ($ resolver , $ this ->cloudIdResolvers )) !== false ) {
280+ array_splice ($ this ->cloudIdResolvers , $ key , 1 );
281+ }
282+ }
251283}
0 commit comments