Skip to content

Commit 2724a31

Browse files
committed
Add Recommendations Toggle
Signed-off-by: Gary Kim <[email protected]>
1 parent 02cc12c commit 2724a31

File tree

9 files changed

+242
-12
lines changed

9 files changed

+242
-12
lines changed

appinfo/routes.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,8 @@
2828
'resources' => [
2929
'recommendation' => ['url' => 'api/recommendations'],
3030
],
31+
'routes' => [
32+
['name' => 'settings#getSettings', 'url' => '/settings', 'verb' => 'GET'],
33+
['name' => 'settings#setSetting', 'url' => '/settings/{key}', 'verb' => 'PUT'],
34+
],
3135
];

js/main.js

Lines changed: 38 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

js/main.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/Controller/RecommendationController.php

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
use OCA\Recommendations\Service\RecommendationService;
3131
use OCP\AppFramework\Controller;
3232
use OCP\AppFramework\Http\JSONResponse;
33+
use OCP\IConfig;
3334
use OCP\IRequest;
3435
use OCP\IUserSession;
3536

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

45+
/** @var IConfig */
46+
private $config;
47+
4448
public function __construct(IRequest $request,
4549
IUserSession $userSession,
46-
RecommendationService $recommendationService) {
50+
RecommendationService $recommendationService,
51+
IConfig $config) {
4752
parent::__construct(Application::APP_ID, $request);
4853
$this->userSession = $userSession;
4954
$this->recommendationService = $recommendationService;
55+
$this->config = $config;
5056
}
5157

5258
/**
@@ -58,9 +64,11 @@ public function index(): JSONResponse {
5864
if (is_null($user)) {
5965
throw new Exception("Not logged in");
6066
}
61-
67+
$response = [];
68+
$response['enabled'] = $this->config->getUserValue($user->getUID(), Application::APP_ID, 'enabled', 'true') === 'true';
69+
$response['recommendations'] = $this->recommendationService->getRecommendations($user);
6270
return new JSONResponse(
63-
$this->recommendationService->getRecommendations($user)
71+
$response
6472
);
6573
}
6674

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* @copyright Copyright (c) 2019 Gary Kim <[email protected]>
7+
*
8+
* @author 2019 Gary Kim <[email protected]>
9+
*
10+
* @license GNU AGPL version 3 or any later version
11+
*
12+
* This program is free software: you can redistribute it and/or modify
13+
* it under the terms of the GNU Affero General Public License as
14+
* published by the Free Software Foundation, either version 3 of the
15+
* License, or (at your option) any later version.
16+
*
17+
* This program is distributed in the hope that it will be useful,
18+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
19+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20+
* GNU Affero General Public License for more details.
21+
*
22+
* You should have received a copy of the GNU Affero General Public License
23+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
24+
*/
25+
26+
namespace OCA\Recommendations\Controller;
27+
use OCA\Recommendations\AppInfo\Application;
28+
use OCP\AppFramework\Controller;
29+
use OCP\AppFramework\Http;
30+
use OCP\AppFramework\Http\JSONResponse;
31+
use OCP\IConfig;
32+
use OCP\IRequest;
33+
use OCP\IUserSession;
34+
use Exception;
35+
36+
class SettingsController extends Controller {
37+
38+
/** @var IConfig */
39+
private $config;
40+
41+
/** @var IUserSession */
42+
private $userSession;
43+
44+
public function __construct($appName, IRequest $request, IConfig $config, IUserSession $userSession) {
45+
parent::__construct($appName, $request);
46+
$this->config = $config;
47+
$this->userSession = $userSession;
48+
}
49+
50+
/**
51+
* @NoAdminRequired
52+
*
53+
* @return JSONResponse
54+
* @throws Exception
55+
*/
56+
public function getSettings (): JSONResponse {
57+
$user = $this->userSession->getUser();
58+
if (is_null($user)) {
59+
throw new Exception("Not logged in");
60+
}
61+
return new JSONResponse([
62+
'enabled' => $this->config->getUserValue($user->getUID(), Application::APP_ID, 'enabled', 'true') === 'true',
63+
]);
64+
}
65+
66+
/**
67+
* @NoAdminRequired
68+
*
69+
* @param $key
70+
* @param $value
71+
* @return JSONResponse
72+
* @throws Exception
73+
*/
74+
public function setSetting (string $key, string $value): JSONResponse {
75+
$user = $this->userSession->getUser();
76+
if (is_null($user)) {
77+
throw new Exception("Not logged in");
78+
}
79+
$availableSettings = ['enabled'];
80+
if (!in_array($key, $availableSettings)) {
81+
return new JSONResponse([
82+
'message' => 'parameter does not exist',
83+
], Http::STATUS_UNPROCESSABLE_ENTITY);
84+
}
85+
$this->config->setUserValue($user->getUID(), Application::APP_ID, $key, $value);
86+
return new JSONResponse([
87+
'key' => $key,
88+
'value' => $value,
89+
]);
90+
}
91+
}

src/components/Recommendations.vue

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
-->
2121

2222
<template>
23-
<div v-if="!hidden && !loading">
23+
<div v-if="!hidden && !loading && enabled">
2424
<div id="recommendations"
2525
v-if="recommendedFiles.length > 0"
2626
class="group">
@@ -51,6 +51,11 @@
5151
recommendedFiles: [],
5252
}
5353
},
54+
computed: {
55+
enabled() {
56+
return this.$store.state.enabled
57+
}
58+
},
5459
methods: {
5560
show () {
5661
this.hidden = false;
@@ -64,9 +69,10 @@
6469
this.loading = true;
6570
6671
fetchRecommendedFiles()
67-
.then(files => {
72+
.then(data => {
6873
this.loading = false;
69-
this.recommendedFiles = files;
74+
this.recommendedFiles = data.recommendations;
75+
this.$store.state.enabled = data.enabled;
7076
})
7177
.catch(console.error.bind(this));
7278
},

src/components/Settings.vue

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<template>
2+
<div id="recommendations-setting-enabled">
3+
<input id="recommendationsEnabledToggle"
4+
v-model="enabled"
5+
class="checkbox"
6+
checked="checked"
7+
type="checkbox"
8+
name="enabled">
9+
<label for="recommendationsEnabledToggle">{{ t('recommendations', 'Recommendations enabled') }}</label>
10+
</div>
11+
</template>
12+
13+
<script>
14+
15+
export default {
16+
name: 'Settings',
17+
computed: {
18+
enabled: {
19+
get() {
20+
return this.$store.state.enabled
21+
},
22+
set(val) {
23+
this.$store.dispatch('enabled', val)
24+
},
25+
},
26+
},
27+
}
28+
</script>
29+
30+
<style scoped>
31+
32+
</style>

src/main.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,12 @@
2020
*/
2121

2222
import Vue from "vue";
23+
import Vuex from "vuex";
2324

2425
import Nextcloud from "./mixins/Nextcloud";
2526
import Recommendations from "./components/Recommendations";
27+
import Settings from './components/Settings';
28+
import store from "./store/store";
2629

2730
Vue.mixin(Nextcloud);
2831
OC.Plugins.register('OCA.Files.FileList', {
@@ -48,9 +51,15 @@ OC.Plugins.register('OCA.Files.FileList', {
4851

4952
const View = Vue.extend(Recommendations);
5053
const vm = new View({
51-
propsData: {}
54+
propsData: {},
55+
store
5256
}).$mount(this.el);
5357

58+
// register Files App Setting
59+
const settingsView = Vue.extend(Settings);
60+
settingsView.prototype.$store = store;
61+
OCA.Files.Settings.registerSetting(new OCA.Files.Settings.Setting('recommendations', settingsView));
62+
5463
fileList.$el.on('changeDirectory', data => {
5564
if (data.dir.toString() === '/') {
5665
vm.show();

src/store/store.js

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
* @copyright 2019 Gary Kim <[email protected]>
3+
*
4+
* @author 2019 Gary Kim <[email protected]>
5+
*
6+
* @license GNU AGPL version 3 or any later version
7+
*
8+
* This program is free software: you can redistribute it and/or modify
9+
* it under the terms of the GNU Affero General Public License as
10+
* published by the Free Software Foundation, either version 3 of the
11+
* License, or (at your option) any later version.
12+
*
13+
* This program is distributed in the hope that it will be useful,
14+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
15+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16+
* GNU Affero General Public License for more details.
17+
*
18+
* You should have received a copy of the GNU Affero General Public License
19+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
20+
*/
21+
22+
import Vue from 'vue';
23+
import Vuex from 'vuex';
24+
import Axios from '@nextcloud/axios'
25+
import { generateUrl } from "nextcloud-server/dist/router"
26+
27+
Vue.use(Vuex);
28+
29+
export default new Vuex.Store({
30+
state: {
31+
enabled: true,
32+
},
33+
mutations: {
34+
enabled(state, val) {
35+
state.enabled = val;
36+
},
37+
},
38+
actions: {
39+
enabled(ctx, val) {
40+
ctx.commit('enabled', val);
41+
Axios.put(generateUrl('apps/recommendations/settings/enabled'), {
42+
value: val.toString(),
43+
});
44+
},
45+
},
46+
})

0 commit comments

Comments
 (0)