|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | + |
| 6 | +/** |
| 7 | + * Circles - Bring cloud-users closer together. |
| 8 | + * |
| 9 | + * This file is licensed under the Affero General Public License version 3 or |
| 10 | + * later. See the COPYING file. |
| 11 | + * |
| 12 | + * @author Maxence Lange <maxence@artificial-owl.com> |
| 13 | + * @copyright 2021 |
| 14 | + * @license GNU AGPL version 3 or any later version |
| 15 | + * |
| 16 | + * This program is free software: you can redistribute it and/or modify |
| 17 | + * it under the terms of the GNU Affero General Public License as |
| 18 | + * published by the Free Software Foundation, either version 3 of the |
| 19 | + * License, or (at your option) any later version. |
| 20 | + * |
| 21 | + * This program is distributed in the hope that it will be useful, |
| 22 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 23 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 24 | + * GNU Affero General Public License for more details. |
| 25 | + * |
| 26 | + * You should have received a copy of the GNU Affero General Public License |
| 27 | + * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 28 | + * |
| 29 | + */ |
| 30 | + |
| 31 | + |
| 32 | +namespace OCA\Circles\Controller; |
| 33 | + |
| 34 | + |
| 35 | +use Exception; |
| 36 | +use OCA\Circles\Model\Circle; |
| 37 | +use OCA\Circles\Service\CirclesService; |
| 38 | +use OCA\Circles\Service\MembersService; |
| 39 | +use OCP\AppFramework\Http; |
| 40 | +use OCP\AppFramework\Http\DataResponse; |
| 41 | +use OCP\AppFramework\OCSController; |
| 42 | +use OCP\IRequest; |
| 43 | +use OCP\IUserSession; |
| 44 | + |
| 45 | + |
| 46 | +/** |
| 47 | + * Class OcsApiController |
| 48 | + * |
| 49 | + * @package OCA\Circles\Controller |
| 50 | + */ |
| 51 | +class OcsApiController extends OCSController { |
| 52 | + |
| 53 | + |
| 54 | + /** @var IUserSession */ |
| 55 | + private $userSession; |
| 56 | + |
| 57 | + /** @var CirclesService */ |
| 58 | + private $circlesService; |
| 59 | + |
| 60 | + /** @var MembersService */ |
| 61 | + private $membersService; |
| 62 | + |
| 63 | + |
| 64 | + /** |
| 65 | + * OcsApiController constructor. |
| 66 | + * |
| 67 | + * @param $appName |
| 68 | + * @param IRequest $request |
| 69 | + * @param IUserSession $userSession |
| 70 | + * @param CirclesService $circlesService |
| 71 | + * @param MembersService $membersService |
| 72 | + */ |
| 73 | + public function __construct( |
| 74 | + $appName, |
| 75 | + IRequest $request, |
| 76 | + IUserSession $userSession, |
| 77 | + CirclesService $circlesService, |
| 78 | + MembersService $membersService |
| 79 | + ) { |
| 80 | + parent::__construct($appName, $request); |
| 81 | + |
| 82 | + $this->userSession = $userSession; |
| 83 | + $this->circlesService = $circlesService; |
| 84 | + $this->membersService = $membersService; |
| 85 | + } |
| 86 | + |
| 87 | + |
| 88 | + /** |
| 89 | + * @NoAdminRequired |
| 90 | + * |
| 91 | + * @return DataResponse |
| 92 | + */ |
| 93 | + public function circles(): DataResponse { |
| 94 | + $user = $this->userSession->getUser(); |
| 95 | + try { |
| 96 | + $circles = $this->circlesService->listCircles($user->getUID(), Circle::CIRCLES_ALL); |
| 97 | + |
| 98 | + $circles = array_map( |
| 99 | + function(Circle $circle) { |
| 100 | + $circle->setSettings([]); |
| 101 | + |
| 102 | + return $circle; |
| 103 | + }, $circles |
| 104 | + ); |
| 105 | + |
| 106 | + return new DataResponse(json_decode(json_encode($circles), true)); |
| 107 | + } catch (Exception $e) { |
| 108 | + return new DataResponse(['message' => $$e->getMessage()], Http::STATUS_BAD_REQUEST); |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + |
| 113 | + /** |
| 114 | + * @NoAdminRequired |
| 115 | + * |
| 116 | + * @param string $circleId |
| 117 | + * |
| 118 | + * @return DataResponse |
| 119 | + */ |
| 120 | + public function members(string $circleId): DataResponse { |
| 121 | + try { |
| 122 | + $circle = $this->circlesService->detailsCircle($circleId); |
| 123 | + $members = ($circle->getMembers() === null) ? [] : $circle->getMembers(); |
| 124 | + |
| 125 | + return new DataResponse(json_decode(json_encode($members), true)); |
| 126 | + } catch (Exception $e) { |
| 127 | + return new DataResponse(['message' => $$e->getMessage()], Http::STATUS_BAD_REQUEST); |
| 128 | + } |
| 129 | + } |
| 130 | + |
| 131 | +} |
| 132 | + |
0 commit comments