userSession = $userSession; $this->federatedUserService = $federatedUserService; $this->circleService = $circleService; $this->memberService = $memberService; $this->membershipService = $membershipService; $this->searchService = $searchService; $this->configService = $configService; $this->setup('app', 'circles'); } /** * @param string $emulated * @param string $name * @param bool $personal * @param bool $local * * @return DataResponse * @throws OCSException */ public function create( string $emulated, string $name, bool $personal = false, bool $local = false, ): DataResponse { try { $this->setLocalFederatedUser($emulated); $circle = $this->circleService->create($name, null, $personal, $local); return new DataResponse($this->serializeArray($circle)); } catch (Exception $e) { $this->e( $e, [ 'emulated' => $emulated, 'name' => $name, 'members' => $personal, 'local' => $local ] ); throw new OCSException($e->getMessage(), (int)$e->getCode()); } } /** * @param string $emulated * @param string $circleId * * @return DataResponse * @throws OCSException */ public function destroy(string $emulated, string $circleId): DataResponse { try { $this->setLocalFederatedUser($emulated); $circle = $this->circleService->destroy($circleId); return new DataResponse($this->serializeArray($circle)); } catch (Exception $e) { $this->e($e, ['emulated' => $emulated, 'circleId' => $circleId]); throw new OCSException($e->getMessage(), (int)$e->getCode()); } } /** * @param string $emulated * @param string $circleId * @param string $userId * @param int $type * * @return DataResponse * @throws OCSException */ public function memberAdd(string $emulated, string $circleId, string $userId, int $type): DataResponse { try { $this->setLocalFederatedUser($emulated); // exception in Contact if ($type === Member::TYPE_CONTACT) { $currentUser = $this->federatedUserService->getCurrentUser(); if (!$this->configService->isLocalInstance($currentUser->getInstance())) { throw new OCSException('works only from local instance', 404); } $userId = $currentUser->getUserId() . '/' . $userId; } $federatedUser = $this->federatedUserService->generateFederatedUser($userId, $type); $result = $this->memberService->addMember($circleId, $federatedUser); return new DataResponse($this->serializeArray($result)); } catch (Exception $e) { $this->e( $e, [ 'emulated' => $emulated, 'circleId' => $circleId, 'userId' => $userId, 'type' => $type ] ); throw new OCSException($e->getMessage(), (int)$e->getCode()); } } /** * @param string $emulated * @param string $circleId * @param string $memberId * @param string|int $level * * @return DataResponse * @throws OCSException */ public function memberLevel(string $emulated, string $circleId, string $memberId, $level): DataResponse { try { $this->setLocalFederatedUser($emulated); if (is_int($level)) { $level = Member::parseLevelInt($level); } else { $level = Member::parseLevelString($level); } $this->memberService->getMemberById($memberId, $circleId); $result = $this->memberService->memberLevel($memberId, $level); return new DataResponse($this->serializeArray($result)); } catch (Exception $e) { $this->e( $e, [ 'emulated' => $emulated, 'circleId' => $circleId, 'memberId' => $memberId, 'level' => $level ] ); throw new OCSException($e->getMessage(), (int)$e->getCode()); } } /** * @param string $emulated * @param int $limit * @param int $offset * @return DataResponse * @throws OCSException */ public function circles(string $emulated, int $limit = -1, int $offset = 0): DataResponse { try { $this->setLocalFederatedUser($emulated); $probe = new CircleProbe(); $probe->filterHiddenCircles() ->filterBackendCircles() ->addDetail(BasicProbe::DETAILS_POPULATION) ->setItemsLimit($limit) ->setItemsOffset($offset); return new DataResponse($this->serializeArray($this->circleService->getCircles($probe))); } catch (Exception $e) { $this->e($e, ['emulated' => $emulated]); throw new OCSException($e->getMessage(), (int)$e->getCode()); } } /** * @param string $emulated * @param string $circleId * * @return DataResponse * @throws OCSException */ public function circleDetails(string $emulated, string $circleId): DataResponse { try { $this->setLocalFederatedUser($emulated); $probe = new CircleProbe(); $probe->includeNonVisibleCircles(); return new DataResponse($this->serialize($this->circleService->getCircle($circleId, $probe))); } catch (Exception $e) { $this->e($e, ['emulated' => $emulated, 'circleId' => $circleId]); throw new OCSException($e->getMessage(), (int)$e->getCode()); } } /** * @param string $emulated * @param string $circleId * * @return DataResponse * @throws OCSException */ public function circleJoin(string $emulated, string $circleId): DataResponse { try { $this->setLocalFederatedUser($emulated); $result = $this->circleService->circleJoin($circleId); return new DataResponse($this->serializeArray($result)); } catch (Exception $e) { $this->e($e, ['emulated' => $emulated, 'circleId' => $circleId]); throw new OCSException($e->getMessage(), (int)$e->getCode()); } } /** * @param string $emulated * @param string $circleId * * @return DataResponse * @throws OCSException */ public function circleLeave(string $emulated, string $circleId): DataResponse { try { $this->setLocalFederatedUser($emulated); $result = $this->circleService->circleLeave($circleId); return new DataResponse($this->serializeArray($result)); } catch (Exception $e) { $this->e($e, ['emulated' => $emulated, 'circleId' => $circleId]); throw new OCSException($e->getMessage(), (int)$e->getCode()); } } /** * @param string $emulated * @param string $circleId * @param string $memberId * * @return DataResponse * @throws OCSException */ public function memberConfirm(string $emulated, string $circleId, string $memberId): DataResponse { try { $this->setLocalFederatedUser($emulated); $member = $this->memberService->getMemberById($memberId, $circleId); $federatedUser = new FederatedUser(); $federatedUser->importFromIFederatedUser($member); $result = $this->memberService->addMember($circleId, $federatedUser); return new DataResponse($this->serializeArray($result)); } catch (Exception $e) { $this->e($e, ['emulated' => $emulated, 'circleId' => $circleId, 'memberId' => $memberId]); throw new OCSException($e->getMessage(), (int)$e->getCode()); } } /** * @param string $emulated * @param string $circleId * @param string $memberId * * @return DataResponse * @throws OCSException */ public function memberRemove(string $emulated, string $circleId, string $memberId): DataResponse { try { $this->setLocalFederatedUser($emulated); $this->memberService->getMemberById($memberId, $circleId); $result = $this->memberService->removeMember($memberId); return new DataResponse($this->serializeArray($result)); } catch (Exception $e) { $this->e($e, ['emulated' => $emulated, 'circleId' => $circleId, 'memberId' => $memberId]); throw new OCSException($e->getMessage(), (int)$e->getCode()); } } /** * @param string $emulated * @param string $circleId * * @return DataResponse * @throws OCSException */ public function members(string $emulated, string $circleId): DataResponse { try { $this->setLocalFederatedUser($emulated); return new DataResponse($this->serializeArray($this->memberService->getMembers($circleId))); } catch (Exception $e) { $this->e($e, ['emulated' => $emulated, 'circleId' => $circleId]); throw new OCSException($e->getMessage(), (int)$e->getCode()); } } /** * @param string $emulated * @param string $circleId * @param string $value * * @return DataResponse * @throws OCSException */ public function editName(string $emulated, string $circleId, string $value): DataResponse { try { $this->setLocalFederatedUser($emulated); $outcome = $this->circleService->updateName($circleId, $value); return new DataResponse($this->serializeArray($outcome)); } catch (Exception $e) { $this->e($e, ['emulated' => $emulated, 'circleId' => $circleId, 'value' => $value]); throw new OCSException($e->getMessage(), (int)$e->getCode()); } } /** * @param string $emulated * @param string $circleId * @param string $value * * @return DataResponse * @throws OCSException */ public function editDescription(string $emulated, string $circleId, string $value): DataResponse { try { $this->setLocalFederatedUser($emulated); $outcome = $this->circleService->updateDescription($circleId, $value); return new DataResponse($this->serializeArray($outcome)); } catch (Exception $e) { $this->e($e, ['emulated' => $emulated, 'circleId' => $circleId, 'value' => $value]); throw new OCSException($e->getMessage(), (int)$e->getCode()); } } /** * @param string $emulated * @param string $circleId * @param string $setting * @param string|null $value * * @return DataResponse * @throws OCSException */ public function editSetting(string $emulated, string $circleId, string $setting, ?string $value = null): DataResponse { try { $this->setLocalFederatedUser($emulated); $outcome = $this->circleService->updateSetting($circleId, $setting, $value); return new DataResponse($this->serializeArray($outcome)); } catch (Exception $e) { $this->e($e, ['circleId' => $circleId, 'setting' => $setting, 'value' => $value]); throw new OCSException($e->getMessage(), (int)$e->getCode()); } } /** * @param string $emulated * @param string $circleId * @param int $value * * @return DataResponse * @throws OCSException */ public function editConfig(string $emulated, string $circleId, int $value): DataResponse { try { $this->setLocalFederatedUser($emulated); $outcome = $this->circleService->updateConfig($circleId, $value); return new DataResponse($this->serializeArray($outcome)); } catch (Exception $e) { $this->e($e, ['emulated' => $emulated, 'circleId' => $circleId, 'value' => $value]); throw new OCSException($e->getMessage(), (int)$e->getCode()); } } /** * @param string $emulated * @param string $circleId * @param string $singleId * * @return DataResponse * @throws OCSException */ public function link(string $emulated, string $circleId, string $singleId): DataResponse { try { $this->setLocalFederatedUser($emulated); $membership = $this->membershipService->getMembership($circleId, $singleId, true); return new DataResponse($this->serialize($membership)); } catch (Exception $e) { $this->e($e, ['emulated' => $emulated, 'circleId' => $circleId, 'singleId' => $singleId]); throw new OCSException($e->getMessage(), (int)$e->getCode()); } } /** * @param string $emulated * * @throws FederatedUserException * @throws FederatedUserNotFoundException * @throws InvalidIdException * @throws RequestBuilderException * @throws SingleCircleNotFoundException * @throws ContactAddressBookNotFoundException * @throws ContactFormatException * @throws ContactNotFoundException */ private function setLocalFederatedUser(string $emulated): void { $user = $this->userSession->getUser(); $this->federatedUserService->setCurrentPatron($user->getUID()); $this->federatedUserService->setLocalCurrentUserId($emulated); } }