3131
3232namespace OCA \Circles ;
3333
34- use OCA \Circles \Tools \Exceptions \InvalidItemException ;
3534use OCA \Circles \Exceptions \CircleNotFoundException ;
3635use OCA \Circles \Exceptions \ContactAddressBookNotFoundException ;
3736use OCA \Circles \Exceptions \ContactFormatException ;
5958use OCA \Circles \Model \Membership ;
6059use OCA \Circles \Model \Probes \CircleProbe ;
6160use OCA \Circles \Service \CircleService ;
61+ use OCA \Circles \Service \ConfigService ;
6262use OCA \Circles \Service \FederatedUserService ;
6363use OCA \Circles \Service \MemberService ;
6464use OCA \Circles \Service \MembershipService ;
65- use OCP \ IUserSession ;
65+ use OCA \ Circles \ Tools \ Exceptions \ InvalidItemException ;
6666
6767/**
6868 * Class CirclesManager
7272class CirclesManager {
7373
7474
75- /** @var CirclesQueryHelper */
76- private $ circlesQueryHelper ;
77-
7875 /** @var FederatedUserService */
7976 private $ federatedUserService ;
8077
@@ -87,27 +84,36 @@ class CirclesManager {
8784 /** @var MembershipService */
8885 private $ membershipService ;
8986
87+ /** @var ConfigService */
88+ private $ configService ;
89+
90+ /** @var CirclesQueryHelper */
91+ private $ circlesQueryHelper ;
92+
9093
9194 /**
9295 * CirclesManager constructor.
9396 *
94- * @param IUserSession $userSession
9597 * @param FederatedUserService $federatedUserService
9698 * @param CircleService $circleService
9799 * @param MemberService $memberService
100+ * @param MembershipService $membershipService
101+ * @param ConfigService $configService
98102 * @param CirclesQueryHelper $circlesQueryHelper
99103 */
100104 public function __construct (
101105 FederatedUserService $ federatedUserService ,
102106 CircleService $ circleService ,
103107 MemberService $ memberService ,
104108 MembershipService $ membershipService ,
109+ ConfigService $ configService ,
105110 CirclesQueryHelper $ circlesQueryHelper
106111 ) {
107112 $ this ->federatedUserService = $ federatedUserService ;
108113 $ this ->circleService = $ circleService ;
109114 $ this ->memberService = $ memberService ;
110115 $ this ->membershipService = $ membershipService ;
116+ $ this ->configService = $ configService ;
111117 $ this ->circlesQueryHelper = $ circlesQueryHelper ;
112118 }
113119
@@ -136,6 +142,29 @@ public function getFederatedUser(string $federatedId, int $type = Member::TYPE_S
136142 return $ this ->federatedUserService ->getFederatedUser ($ federatedId , $ type );
137143 }
138144
145+ /**
146+ * @param string $userId
147+ *
148+ * @return FederatedUser
149+ * @throws CircleNotFoundException
150+ * @throws FederatedItemException
151+ * @throws FederatedUserException
152+ * @throws FederatedUserNotFoundException
153+ * @throws InvalidIdException
154+ * @throws MemberNotFoundException
155+ * @throws OwnerNotFoundException
156+ * @throws RemoteInstanceException
157+ * @throws RemoteNotFoundException
158+ * @throws RemoteResourceNotFoundException
159+ * @throws RequestBuilderException
160+ * @throws SingleCircleNotFoundException
161+ * @throws UnknownRemoteException
162+ * @throws UserTypeNotFoundException
163+ */
164+ public function getLocalFederatedUser (string $ userId ): FederatedUser {
165+ return $ this ->getFederatedUser ($ userId , Member::TYPE_USER );
166+ }
167+
139168
140169 /**
141170 * @throws FederatedUserNotFoundException
@@ -161,6 +190,22 @@ public function startSuperSession(): void {
161190 }
162191
163192
193+ /**
194+ * @param string $appId
195+ * @param int $appSerial
196+ *
197+ * @throws ContactAddressBookNotFoundException
198+ * @throws ContactFormatException
199+ * @throws ContactNotFoundException
200+ * @throws FederatedUserException
201+ * @throws InvalidIdException
202+ * @throws RequestBuilderException
203+ * @throws SingleCircleNotFoundException
204+ */
205+ public function startAppSession (string $ appId , int $ appSerial = Member::APP_DEFAULT ): void {
206+ $ this ->federatedUserService ->setLocalCurrentApp ($ appId , $ appSerial );
207+ }
208+
164209 /**
165210 * $userId - userId to emulate as initiator (can be empty)
166211 * $userType - specify if userIs not a singleId
@@ -304,6 +349,66 @@ public function getCircle(string $singleId, ?CircleProbe $probe = null): Circle
304349 }
305350
306351
352+ /**
353+ * @param Circle $circle
354+ *
355+ * @throws CircleNotFoundException
356+ * @throws FederatedEventException
357+ * @throws FederatedItemException
358+ * @throws InitiatorNotConfirmedException
359+ * @throws InitiatorNotFoundException
360+ * @throws OwnerNotFoundException
361+ * @throws RemoteInstanceException
362+ * @throws RemoteNotFoundException
363+ * @throws RemoteResourceNotFoundException
364+ * @throws RequestBuilderException
365+ * @throws UnknownRemoteException
366+ */
367+ public function updateConfig (Circle $ circle ): void {
368+ $ this ->circleService ->updateConfig ($ circle ->getSingleId (), $ circle ->getConfig ());
369+ }
370+
371+
372+ /**
373+ * @param string $circleId
374+ * @param bool $enabled
375+ *
376+ * @throws CircleNotFoundException
377+ * @throws FederatedEventException
378+ * @throws FederatedItemException
379+ * @throws FederatedUserException
380+ * @throws InitiatorNotConfirmedException
381+ * @throws InitiatorNotFoundException
382+ * @throws OwnerNotFoundException
383+ * @throws RemoteInstanceException
384+ * @throws RemoteNotFoundException
385+ * @throws RemoteResourceNotFoundException
386+ * @throws RequestBuilderException
387+ * @throws UnknownRemoteException
388+ */
389+ public function flagAsAppManaged (string $ circleId , bool $ enabled = true ): void {
390+ $ this ->federatedUserService ->confirmSuperSession ();
391+ $ this ->federatedUserService ->setOwnerAsCurrentUser ($ circleId );
392+
393+ $ probe = new CircleProbe ();
394+ $ probe ->includeSystemCircles ();
395+
396+ $ localCircle = $ this ->circleService ->getCircle ($ circleId , $ probe );
397+ if (!$ this ->configService ->isLocalInstance ($ localCircle ->getInstance ())) {
398+ throw new CircleNotFoundException ('This Circle is not managed from this instance ' );
399+ }
400+
401+ $ config = $ localCircle ->getConfig ();
402+ if ($ enabled ) {
403+ $ config |= Circle::CFG_APP ;
404+ } else {
405+ $ config &= ~Circle::CFG_APP ;
406+ }
407+
408+ $ this ->circleService ->updateConfig ($ circleId , $ config );
409+ }
410+
411+
307412 /**
308413 * @param string $circleId
309414 * @param FederatedUser $federatedUser
0 commit comments