diff --git a/appinfo/routes.php b/appinfo/routes.php index f272a5d0..2a11dd07 100644 --- a/appinfo/routes.php +++ b/appinfo/routes.php @@ -15,4 +15,8 @@ ['name' => 'recommendation#index', 'url' => '/api/recommendations', 'verb' => 'GET'], ['name' => 'recommendation#always', 'url' => '/api/recommendations/always', 'verb' => 'GET'], ], + 'ocs' => [ + ['name' => 'recommendation_api#index', 'url' => '/api/v1/recommendations', 'verb' => 'GET'], + ['name' => 'recommendation_api#always', 'url' => '/api/v1/recommendations/always', 'verb' => 'GET'], + ], ]; diff --git a/lib/Controller/RecommendationApiController.php b/lib/Controller/RecommendationApiController.php new file mode 100644 index 00000000..7c6fa9db --- /dev/null +++ b/lib/Controller/RecommendationApiController.php @@ -0,0 +1,91 @@ +. + */ + +namespace OCA\Recommendations\Controller; + +use OCA\Recommendations\AppInfo\Application; +use OCA\Recommendations\Service\RecommendationService; +use OCP\AppFramework\OCSController; +use OCP\AppFramework\Http\DataResponse; +use OCP\IConfig; +use OCP\IRequest; +use OCP\IUserManager; + +class RecommendationApiController extends OCSController { + + /** @var IConfig */ + private $config; + + /** @var IUserManager */ + private $userManager; + + /** @var RecommendationService */ + private $service; + + public function __construct($appName, IRequest $request, IConfig $config, + IUserManager $userManager, + RecommendationService $service, $userId) { + parent::__construct($appName, $request, 'GET'); + $this->config = $config; + $this->service = $service; + $this->userManager = $userManager; + $this->userId = $userId; + } + + /** + * @CORS + * @NoCSRFRequired + * @NoAdminRequired + */ + public function index(): DataResponse { + $user = $this->userManager->get($this->userId); + + $response = []; + $response['enabled'] = $this->config->getUserValue($user->getUID(), Application::APP_ID, 'enabled', 'true') === 'true'; + + if ($response['enabled']) { + $response['recommendations'] = $this->service->getRecommendations($user); + } + + return new DataResponse( + $response + ); + } + + /** + * @CORS + * @NoCSRFRequired + * @NoAdminRequired + */ + public function always(): DataResponse { + $user = $this->userManager->get($this->userId); + + $response = [ + 'enabled' => $this->config->getUserValue($user->getUID(), Application::APP_ID, 'enabled', 'true') === 'true', + 'recommendations' => $this->service->getRecommendations($user), + ]; + + return new DataResponse( + $response + ); + } +}