Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Add Recommendations Toggle
Signed-off-by: Gary Kim <[email protected]>
  • Loading branch information
gary-kim committed Mar 31, 2020
commit ee4ddf8cc97cc32a196700835f707356b1dd3e82
4 changes: 4 additions & 0 deletions appinfo/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,8 @@
'resources' => [
'recommendation' => ['url' => 'api/recommendations'],
],
'routes' => [
['name' => 'settings#getSettings', 'url' => '/settings', 'verb' => 'GET'],
['name' => 'settings#setSetting', 'url' => '/settings/{key}', 'verb' => 'PUT'],
],
];
39 changes: 30 additions & 9 deletions js/main.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion js/main.js.map

Large diffs are not rendered by default.

16 changes: 13 additions & 3 deletions lib/Controller/RecommendationController.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
use OCA\Recommendations\Service\RecommendationService;
use OCP\AppFramework\Controller;
use OCP\AppFramework\Http\JSONResponse;
use OCP\IConfig;
use OCP\IRequest;
use OCP\IUserSession;

Expand All @@ -41,12 +42,17 @@ class RecommendationController extends Controller {
/** @var RecommendationService */
private $recommendationService;

/** @var IConfig */
private $config;

public function __construct(IRequest $request,
IUserSession $userSession,
RecommendationService $recommendationService) {
RecommendationService $recommendationService,
IConfig $config) {
parent::__construct(Application::APP_ID, $request);
$this->userSession = $userSession;
$this->recommendationService = $recommendationService;
$this->config = $config;
}

/**
Expand All @@ -58,9 +64,13 @@ public function index(): JSONResponse {
if (is_null($user)) {
throw new Exception("Not logged in");
}

$response = [];
$response['enabled'] = $this->config->getUserValue($user->getUID(), Application::APP_ID, 'enabled', 'true') === 'true';
if ($response['enabled']) {
$response['recommendations'] = $this->recommendationService->getRecommendations($user);
}
return new JSONResponse(
$this->recommendationService->getRecommendations($user)
$response
);
}

Expand Down
95 changes: 95 additions & 0 deletions lib/Controller/SettingsController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
<?php

declare(strict_types=1);

/**
* @copyright Copyright (c) 2019 Gary Kim <[email protected]>
*
* @author 2019 Gary Kim <[email protected]>
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

namespace OCA\Recommendations\Controller;
use OCA\Recommendations\AppInfo\Application;
use OCP\AppFramework\Controller;
use OCP\AppFramework\Http;
use OCP\AppFramework\Http\JSONResponse;
use OCP\IConfig;
use OCP\IRequest;
use OCP\IUser;
use OCP\IUserSession;
use Exception;

class SettingsController extends Controller {

/** @var IConfig */
private $config;

/** @var IUserSession */
private $userSession;

public function __construct($appName,
IRequest $request,
IConfig $config,
IUserSession $userSession) {
parent::__construct($appName, $request);
$this->config = $config;
$this->userSession = $userSession;
}

/**
* @NoAdminRequired
*
* @return JSONResponse
* @throws Exception
*/
public function getSettings(): JSONResponse {
$user = $this->userSession->getUser();
if (!$user instanceof IUser) {
throw new Exception("Not logged in");
}
return new JSONResponse([
'enabled' => $this->config->getUserValue($user->getUID(), Application::APP_ID, 'enabled', 'true') === 'true',
]);
}

/**
* @NoAdminRequired
*
* @param $key
* @param $value
* @return JSONResponse
* @throws Exception
*/
public function setSetting(string $key, string $value): JSONResponse {
$user = $this->userSession->getUser();
if (!$user instanceof IUser) {
throw new Exception("Not logged in");
}
$availableSettings = ['enabled'];
if (!in_array($key, $availableSettings)) {
return new JSONResponse([
'message' => 'parameter does not exist',
], Http::STATUS_UNPROCESSABLE_ENTITY);
}
$this->config->setUserValue($user->getUID(), Application::APP_ID, $key, $value);
return new JSONResponse([
'key' => $key,
'value' => $value,
]);
}
}
Loading