From d6b3af9d776c224015f9e9d8a4d858acae6f8560 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Julius=20H=C3=A4rtl?= <jus@bitgrid.net>
Date: Fri, 6 Sep 2019 13:47:11 +0200
Subject: [PATCH] Load checks from the backend
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Signed-off-by: Julius Härtl <jus@bitgrid.net>
---
 apps/workflowengine/src/components/Check.vue  |  6 +--
 apps/workflowengine/src/components/Rule.vue   |  2 +-
 apps/workflowengine/src/services/Operation.js | 38 -------------------
 apps/workflowengine/src/store.js              | 26 ++++++++-----
 apps/workflowengine/src/workflowengine.js     | 25 +++++++++++-
 5 files changed, 44 insertions(+), 53 deletions(-)
 delete mode 100644 apps/workflowengine/src/services/Operation.js

diff --git a/apps/workflowengine/src/components/Check.vue b/apps/workflowengine/src/components/Check.vue
index bd3c471fc3c..e521dffbb29 100644
--- a/apps/workflowengine/src/components/Check.vue
+++ b/apps/workflowengine/src/components/Check.vue
@@ -50,9 +50,9 @@ export default {
 		}
 	},
 	computed: {
-		...mapState({
-			Checks: (state) => state.plugins.checks
-		}),
+		Checks() {
+			return this.$store.getters.getChecksForEntity(this.rule.entity)
+		},
 		operators() {
 			if (!this.currentOption) { return [] }
 			return this.Checks[this.currentOption.class].operators
diff --git a/apps/workflowengine/src/components/Rule.vue b/apps/workflowengine/src/components/Rule.vue
index fab79109516..36616e9910a 100644
--- a/apps/workflowengine/src/components/Rule.vue
+++ b/apps/workflowengine/src/components/Rule.vue
@@ -1,5 +1,5 @@
 <template>
-	<div class="section rule" :style="{ borderLeftColor: operation.color }">
+	<div class="section rule" :style="{ borderLeftColor: operation.color || '' }">
 		<!-- TODO: icon-confirm -->
 		<div class="trigger">
 			<p>
diff --git a/apps/workflowengine/src/services/Operation.js b/apps/workflowengine/src/services/Operation.js
deleted file mode 100644
index 58c20d6db14..00000000000
--- a/apps/workflowengine/src/services/Operation.js
+++ /dev/null
@@ -1,38 +0,0 @@
-const ALL_CHECKS = [
-	'OCA\\WorkflowEngine\\Check\\FileMimeType',
-	'OCA\\WorkflowEngine\\Check\\FileName',
-	'OCA\\WorkflowEngine\\Check\\FileSize',
-	'OCA\\WorkflowEngine\\Check\\FileSystemTags',
-	'OCA\\WorkflowEngine\\Check\\RequestRemoteAddress',
-	'OCA\\WorkflowEngine\\Check\\RequestTime',
-	'OCA\\WorkflowEngine\\Check\\RequestURL',
-	'OCA\\WorkflowEngine\\Check\\RequestUserAgent',
-	'OCA\\WorkflowEngine\\Check\\UserGroupMembership'
-]
-
-const Operators = {}
-/**
- * Extend operators for testing
- */
-
-Operators['OCA\\TestExample\\Operation1'] = {
-	id: 'OCA\\TestExample\\Operation1',
-	name: 'Rename file',
-	description: '🚧 For UI mocking only',
-	iconClass: 'icon-address-white',
-	color: 'var(--color-success)',
-	operation: 'deny'
-}
-Operators['OCA\\TestExample\\Operation2'] = {
-	id: 'OCA\\TestExample\\Operation2',
-	name: 'Notify me',
-	description: '🚧 For UI mocking only',
-	iconClass: 'icon-comment-white',
-	color: 'var(--color-warning)',
-	operation: 'deny'
-}
-
-export {
-	Operators,
-	ALL_CHECKS
-}
diff --git a/apps/workflowengine/src/store.js b/apps/workflowengine/src/store.js
index a6af51b65d8..75e53149ea2 100644
--- a/apps/workflowengine/src/store.js
+++ b/apps/workflowengine/src/store.js
@@ -24,7 +24,6 @@ import Vue from 'vue'
 import Vuex from 'vuex'
 import axios from 'nextcloud-axios'
 import { getApiUrl } from './helpers/api'
-import { ALL_CHECKS } from './services/Operation'
 import confirmPassword from 'nextcloud-password-confirmation'
 
 Vue.use(Vuex)
@@ -40,13 +39,7 @@ const store = new Vuex.Store({
 			operators: {}
 		}),
 
-		entities: OCP.InitialState.loadState('workflowengine', 'entities').map(entity => {
-			return {
-				...entity,
-				// TODO: move to backend data once checks are provided
-				checks: [...ALL_CHECKS]
-			}
-		}),
+		entities: OCP.InitialState.loadState('workflowengine', 'entities'),
 		events: OCP.InitialState.loadState('workflowengine', 'entities')
 			.map((entity) => entity.events.map(event => {
 				return {
@@ -54,7 +47,8 @@ const store = new Vuex.Store({
 					entity,
 					...event
 				}
-			})).flat()
+			})).flat(),
+		checks: OCP.InitialState.loadState('workflowengine', 'checks')
 	},
 	mutations: {
 		addRule(state, rule) {
@@ -149,6 +143,20 @@ const store = new Vuex.Store({
 		},
 		getEventsForOperation(state) {
 			return (operation) => state.events
+		},
+		/**
+		 * Return all available checker plugins for a given entity class
+		 */
+		getChecksForEntity(state) {
+			return (entity) => {
+				return state.checks
+					.filter((check) => check.supportedEntities.indexOf(entity) > -1 || check.supportedEntities.length === 0)
+					.map((check) => state.plugins.checks[check.id])
+					.reduce((obj, item) => {
+						obj[item.class] = item
+						return obj
+					}, {})
+			}
 		}
 	}
 })
diff --git a/apps/workflowengine/src/workflowengine.js b/apps/workflowengine/src/workflowengine.js
index 6f96f454262..e05ac0a5053 100644
--- a/apps/workflowengine/src/workflowengine.js
+++ b/apps/workflowengine/src/workflowengine.js
@@ -4,7 +4,6 @@ import store from './store'
 
 import Settings from './components/Workflow'
 import FileValues from './components/Values/file'
-import {Operators} from './services/Operation';
 
 /**
  * A plugin for displaying a custom value field for checks
@@ -63,7 +62,29 @@ window.OCA.WorkflowEngine = Object.assign({}, OCA.WorkflowEngine, {
 
 // Register shipped checks for file entity
 FileValues.forEach((checkPlugin) => window.OCA.WorkflowEngine.registerCheck(checkPlugin))
-Object.values(Operators).forEach((operatorPlugin) => window.OCA.WorkflowEngine.registerOperator(operatorPlugin))
+
+/**
+ * FIXME: remove before merge as this is for UI testing only
+ */
+const demo = [
+	{
+		id: 'OCA\\TestExample\\Operation1',
+		name: 'Rename file',
+		description: '🚧 For UI mocking only',
+		iconClass: 'icon-address-white',
+		color: 'var(--color-success)',
+		operation: 'deny'
+	},
+	{
+		id: 'OCA\\TestExample\\Operation2',
+		name: 'Notify me',
+		description: '🚧 For UI mocking only',
+		iconClass: 'icon-comment-white',
+		color: 'var(--color-warning)',
+		operation: 'deny'
+	}
+]
+demo.forEach((operatorPlugin) => window.OCA.WorkflowEngine.registerOperator(operatorPlugin))
 
 Vue.use(Vuex)
 Vue.prototype.t = t
-- 
GitLab