77
88namespace OCA \Deck \Service ;
99
10- use OC \ Federation \ CloudIdManager ;
10+ use Exception ;
1111use OCA \Deck \Db \Acl ;
1212use OCA \Deck \Db \Assignment ;
1313use OCA \Deck \Db \Board ;
@@ -56,10 +56,10 @@ public function getExternalStacksFromRemote(Board $localBoard):DataResponse {
5656 return new DataResponse ($ this ->LocalizeRemoteStacks ($ ocs , $ localBoard ));
5757 }
5858
59- public function localizeRemoteUser (Board $ localBoard , array $ user ): array | User | FederatedUser | null {
59+ public function localizeRemoteUser (Board $ localBoard , array $ user ): array | User | FederatedUser | null {
6060 // skip invalid users
6161 if (!$ user ['uid ' ]) {
62- return null ;;
62+ return null ;
6363 }
6464 // if it's already a valid cloud id the user originates from a third instance and we pass it as is
6565 if ($ this ->cloudIdManager ->isValidCloudId ($ user ['uid ' ])) {
@@ -75,13 +75,13 @@ public function localizeRemoteUser(Board $localBoard, array $user): array | User
7575 if ($ owner instanceof FederatedUser) {
7676 return new FederatedUser ($ this ->cloudIdManager ->getCloudId ($ user ['uid ' ], $ owner ->getCloudId ()->getRemote ()));
7777 }
78- return null ;
78+ throw new Exception ( ' Owner of the board must be a federated user to localize remote users ' ) ;
7979 }
8080
8181 public function LocalizeRemoteStacks (array $ stacks , Board $ localBoard ) {
8282 foreach ($ stacks as $ i => $ stack ) {
8383 $ stack ['boardId ' ] = $ localBoard ->getId ();
84- foreach ($ stack ['cards ' ] as $ j => $ card ) {
84+ foreach ($ stack ['cards ' ] as $ j => $ card ) {
8585 $ stack ['cards ' ][$ j ]['assignedUsers ' ] = array_map (function ($ assignment ) use ($ localBoard ) {
8686 $ assignment ['participant ' ] = $ this ->localizeRemoteUser ($ localBoard , $ assignment ['participant ' ]);
8787 return $ assignment ;
@@ -207,24 +207,13 @@ public function assignUserOnRemote(Board $localBoard, int $cardId, string $userI
207207 $ this ->configService ->ensureFederationEnabled ();
208208
209209 $ ownerCloudId = $ this ->cloudIdManager ->resolveCloudId ($ localBoard ->getOwner ());
210+ list ($ resolvedUserId , $ resolvedType ) = $ this ->resolveUserIdForRemote ($ userId , $ ownerCloudId );
210211
211- if ($ this ->cloudIdManager ->isValidCloudId ($ userId )) {
212- $ cloudId = $ this ->cloudIdManager ->resolveCloudId ($ userId );
213- // assignee's origin is the same as the board owner's origin: send as local user
214- if ($ cloudId ->getRemote () === $ ownerCloudId ->getRemote ()) {
215- $ userId = $ cloudId ->getUser ();
216- $ type = Assignment::TYPE_USER ;
217- }
218- } else {
219- // local user for us = remote user for remote
220- $ userId = $ this ->cloudIdManager ->getCloudId ($ userId , null )->getId ();
221- $ type = Assignment::TYPE_REMOTE ;
222- }
223212 $ shareToken = $ localBoard ->getShareToken ();
224213 $ url = $ ownerCloudId ->getRemote () . '/ocs/v2.php/apps/deck/api/v1.0/cards/ ' . $ cardId . '/assign ' ;
225214 $ resp = $ this ->proxy ->post ($ ownerCloudId ->getId (), $ shareToken , $ url , [
226- 'userId ' => $ userId ,
227- 'type ' => $ type ,
215+ 'userId ' => $ resolvedUserId ,
216+ 'type ' => $ resolvedType ,
228217 'boardId ' => $ localBoard ->getExternalId (),
229218 ]);
230219 $ result = $ this ->proxy ->getOcsData ($ resp );
@@ -234,6 +223,38 @@ public function assignUserOnRemote(Board $localBoard, int $cardId, string $userI
234223 return $ result ;
235224 }
236225
226+ public function unAssignUserOnRemote (Board $ localBoard , int $ cardId , string $ userId , int $ type = 0 ): array {
227+ $ this ->configService ->ensureFederationEnabled ();
228+
229+ $ ownerCloudId = $ this ->cloudIdManager ->resolveCloudId ($ localBoard ->getOwner ());
230+ list ($ resolvedUserId , $ resolvedType ) = $ this ->resolveUserIdForRemote ($ userId , $ ownerCloudId );
231+
232+ $ shareToken = $ localBoard ->getShareToken ();
233+ $ url = $ ownerCloudId ->getRemote () . '/ocs/v2.php/apps/deck/api/v1.0/cards/ ' . $ cardId . '/unassign ' ;
234+ $ resp = $ this ->proxy ->put ($ ownerCloudId ->getId (), $ shareToken , $ url , [
235+ 'userId ' => $ resolvedUserId ,
236+ 'type ' => $ resolvedType ,
237+ 'boardId ' => $ localBoard ->getExternalId (),
238+ ]);
239+ return $ this ->proxy ->getOcsData ($ resp );
240+ }
241+ /**
242+ * Resolves the userId and type before sending it to remote instances
243+ * @param string $userId
244+ * @param $ownerCloudId
245+ * @return array [userId, type]
246+ */
247+ private function resolveUserIdForRemote (string $ userId , $ ownerCloudId ): array {
248+ if ($ this ->cloudIdManager ->isValidCloudId ($ userId )) {
249+ $ cloudId = $ this ->cloudIdManager ->resolveCloudId ($ userId );
250+ // assignee's origin is the same as the board owner's origin: send as local user
251+ if ($ cloudId ->getRemote () === $ ownerCloudId ->getRemote ()) {
252+ return [$ cloudId ->getUser (), Assignment::TYPE_USER ];
253+ }
254+ }
255+ // local user for us = remote user for remote
256+ return [$ this ->cloudIdManager ->getCloudId ($ userId , null )->getId (), Assignment::TYPE_REMOTE ];
257+ }
237258
238259 public function createStackOnRemote (
239260 Board $ localBoard ,
0 commit comments