Skip to content

Commit e90021c

Browse files
committed
Move filtering to javascript
Signed-off-by: Julius Härtl <jus@bitgrid.net>
1 parent 28d6ada commit e90021c

4 files changed

Lines changed: 79 additions & 96 deletions

File tree

settings/Controller/AppSettingsController.php

Lines changed: 44 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,9 @@ class AppSettingsController extends Controller {
8181
/** @var ILogger */
8282
private $logger;
8383

84+
/** @var array */
85+
private $allApps = [];
86+
8487
/**
8588
* @param string $appName
8689
* @param IRequest $request
@@ -197,82 +200,58 @@ private function getAllCategories() {
197200
return $formattedCategories;
198201
}
199202

203+
private function fetchApps() {
204+
$appClass = new \OC_App();
205+
$apps = $appClass->listAllApps();
206+
foreach ($apps as $app) {
207+
$app['installed'] = true;
208+
$this->allApps[$app['id']] = $app;
209+
}
210+
211+
$apps = $this->getAppsForCategory('');
212+
foreach ($apps as $app) {
213+
$app['appstore'] = true;
214+
if (!array_key_exists($app['id'], $this->allApps)) {
215+
$this->allApps[$app['id']] = $app;
216+
} else {
217+
$this->allApps[$app['id']] = array_merge($this->allApps[$app['id']], $app);
218+
}
219+
}
220+
221+
// add bundle information
222+
$bundles = $this->bundleFetcher->getBundles();
223+
foreach($bundles as $bundle) {
224+
foreach($bundle->getAppIdentifiers() as $identifier) {
225+
foreach($this->allApps as &$app) {
226+
if($app['id'] === $identifier) {
227+
$app['bundleId'] = $bundle->getIdentifier();
228+
continue;
229+
}
230+
}
231+
}
232+
}
233+
}
234+
235+
private function getAllApps() {
236+
return $this->allApps;
237+
}
200238
/**
201239
* Get all available apps in a category
202240
*
203241
* @param string $category
204242
* @return JSONResponse
205243
* @throws \Exception
206244
*/
207-
public function listApps($category = ''): JSONResponse {
208-
$appClass = new \OC_App();
245+
public function listApps(): JSONResponse {
209246

210-
switch ($category) {
211-
case 'installed':
212-
$apps = $appClass->listAllApps();
213-
break;
214-
case 'updates':
215-
$apps = $this->getAppsWithUpdates();
216-
break;
217-
case 'enabled':
218-
$apps = $appClass->listAllApps();
219-
$apps = array_filter($apps, function ($app) {
220-
return $app['active'];
221-
});
222-
break;
223-
case 'disabled':
224-
$apps = $appClass->listAllApps();
225-
$apps = array_filter($apps, function ($app) {
226-
return !$app['active'];
227-
});
228-
break;
229-
case 'app-bundles':
230-
$bundles = $this->bundleFetcher->getBundles();
231-
$apps = [];
232-
$installedApps = $appClass->listAllApps();
233-
$appstoreApps = $this->getAppsForCategory();
234-
foreach($bundles as $bundle) {
235-
foreach($bundle->getAppIdentifiers() as $identifier) {
236-
$alreadyMatched = false;
237-
foreach($installedApps as $app) {
238-
if($app['id'] === $identifier) {
239-
$app['bundleId'] = $bundle->getIdentifier();
240-
$apps[] = $app;
241-
$alreadyMatched = true;
242-
continue;
243-
}
244-
}
245-
if (!$alreadyMatched) {
246-
foreach ($appstoreApps as $app) {
247-
if ($app['id'] === $identifier) {
248-
$app['bundleId'] = $bundle->getIdentifier();
249-
$apps[] = $app;
250-
continue;
251-
}
252-
}
253-
}
254-
}
255-
}
256-
break;
257-
default:
258-
$apps = $this->getAppsForCategory($category);
259-
break;
260-
}
261-
262-
263-
// Fetch all apps from appstore
264-
$allAppStoreApps = [];
265-
$fetchedApps = $this->appFetcher->get();
266-
foreach ($fetchedApps as $app) {
267-
$allAppStoreApps[$app['id']] = $app;
268-
}
247+
$this->fetchApps();
248+
$apps = $this->getAllApps();
269249

270250
$dependencyAnalyzer = new DependencyAnalyzer(new Platform($this->config), $this->l10n);
271251

272252
// Extend existing app details
273-
$apps = array_map(function($appData) use ($allAppStoreApps, $dependencyAnalyzer) {
274-
$appstoreData = $allAppStoreApps[$appData['id']];
275-
$appData['appstoreData'] = $appstoreData;
253+
$apps = array_map(function($appData) use ($dependencyAnalyzer) {
254+
$appstoreData = $appData['appstoreData'];
276255
$appData['screenshot'] = isset($appstoreData['screenshots'][0]['url']) ? 'https://usercontent.apps.nextcloud.com/'.base64_encode($appstoreData['screenshots'][0]['url']) : '';
277256

278257
$newVersion = $this->installer->isUpdateAvailable($appData['id']);
@@ -310,7 +289,7 @@ public function listApps($category = ''): JSONResponse {
310289
}
311290

312291
/**
313-
* Get all apps for a category
292+
* Get all apps for a category from the app store
314293
*
315294
* @param string $requestedCategory
316295
* @return array

settings/src/components/appList.vue

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,15 +80,45 @@ export default {
8080
return this.$store.getters.loading('list');
8181
},
8282
apps() {
83-
return this.$store.getters.getApps
83+
let apps = this.$store.getters.getAllApps
8484
.filter(app => app.name.toLowerCase().search(this.search.toLowerCase()) !== -1)
85+
.sort(function (a, b) {
86+
if (a.active !== b.active) {
87+
return (a.active ? -1 : 1)
88+
}
89+
if (a.update !== b.update) {
90+
return (a.update ? -1 : 1)
91+
}
92+
return OC.Util.naturalSortCompare(a.name, b.name);
93+
});
94+
95+
if (this.category === 'installed') {
96+
return apps.filter(app => app.installed);
97+
}
98+
if (this.category === 'enabled') {
99+
return apps.filter(app => app.active);
100+
}
101+
if (this.category === 'disabled') {
102+
return apps.filter(app => !app.active);
103+
}
104+
if (this.category === 'app-bundles') {
105+
return apps.filter(app => app.bundles);
106+
}
107+
if (this.category === 'updates') {
108+
return apps.filter(app => app.update);
109+
}
110+
// filter app store categories
111+
return apps.filter(app => {
112+
return app.appstore && app.category !== undefined &&
113+
(app.category === this.category || app.category.indexOf(this.category) > -1);
114+
});
85115
},
86116
bundles() {
87117
return this.$store.getters.getServerData.bundles;
88118
},
89119
bundleApps() {
90120
return function(bundle) {
91-
return this.$store.getters.getApps
121+
return this.$store.getters.getAllApps
92122
.filter(app => app.bundleId === bundle);
93123
}
94124
},

settings/src/store/apps.js

Lines changed: 2 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -145,17 +145,6 @@ const getters = {
145145
getCategories(state) {
146146
return state.categories;
147147
},
148-
getApps(state) {
149-
return state.apps.concat([]).sort(function (a, b) {
150-
if (a.active !== b.active) {
151-
return (a.active ? -1 : 1)
152-
}
153-
if (a.update !== b.update) {
154-
return (a.update ? -1 : 1)
155-
}
156-
return OC.Util.naturalSortCompare(a.name, b.name);
157-
});
158-
},
159148
getAllApps(state) {
160149
return state.allApps;
161150
},
@@ -279,23 +268,12 @@ const actions = {
279268
}).catch((error) => context.commit('API_FAILURE', { appId, error }));
280269
},
281270

282-
getApps(context, { category }) {
283-
context.commit('startLoading', 'list');
284-
return api.get(OC.generateUrl(`settings/apps/list?category=${category}`))
285-
.then((response) => {
286-
context.commit('setApps', response.data.apps);
287-
context.commit('stopLoading', 'list');
288-
return true;
289-
})
290-
.catch((error) => context.commit('API_FAILURE', error))
291-
},
292-
293271
getAllApps(context) {
294-
context.commit('startLoading', 'all');
272+
context.commit('startLoading', 'list');
295273
return api.get(OC.generateUrl(`settings/apps/list`))
296274
.then((response) => {
297275
context.commit('setAllApps', response.data.apps);
298-
context.commit('stopLoading', 'all');
276+
context.commit('stopLoading', 'list');
299277
return true;
300278
})
301279
.catch((error) => context.commit('API_FAILURE', error))

settings/src/views/Apps.vue

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,6 @@ export default {
6969
},
7070
beforeMount() {
7171
this.$store.dispatch('getCategories');
72-
this.$store.dispatch('getApps', {category: this.category});
7372
this.$store.dispatch('getAllApps');
7473
this.$store.dispatch('getGroups');
7574
this.$store.commit('setUpdateCount', this.$store.getters.getServerData.updateCount)
@@ -89,11 +88,8 @@ export default {
8988
}
9089
},
9190
watch: {
92-
// watch url change and group select
9391
category: function (val, old) {
94-
this.$store.commit('resetApps');
9592
this.setSearch('');
96-
this.$store.dispatch('getApps', { category: this.category });
9793
}
9894
},
9995
computed: {
@@ -110,7 +106,7 @@ export default {
110106
return this.$store.getters.getCategories;
111107
},
112108
apps() {
113-
return this.$store.getters.getApps;
109+
return this.$store.getters.getAllApps;
114110
},
115111
updateCount() {
116112
return this.$store.getters.getUpdateCount;

0 commit comments

Comments
 (0)