mirror of
				https://github.com/zadam/trilium.git
				synced 2025-11-04 13:39:01 +01:00 
			
		
		
		
	added #runOnInstance and #runAtHour
This commit is contained in:
		
							parent
							
								
									052f584bf4
								
							
						
					
					
						commit
						9c679aef20
					
				
							
								
								
									
										14
									
								
								package-lock.json
									
									
									
										generated
									
									
									
								
							
							
						
						
									
										14
									
								
								package-lock.json
									
									
									
										generated
									
									
									
								
							@ -65,7 +65,7 @@
 | 
				
			|||||||
      },
 | 
					      },
 | 
				
			||||||
      "devDependencies": {
 | 
					      "devDependencies": {
 | 
				
			||||||
        "cross-env": "7.0.3",
 | 
					        "cross-env": "7.0.3",
 | 
				
			||||||
        "electron": "13.0.0-beta.11",
 | 
					        "electron": "13.0.0-beta.12",
 | 
				
			||||||
        "electron-builder": "22.10.5",
 | 
					        "electron-builder": "22.10.5",
 | 
				
			||||||
        "electron-packager": "15.2.0",
 | 
					        "electron-packager": "15.2.0",
 | 
				
			||||||
        "electron-rebuild": "2.3.5",
 | 
					        "electron-rebuild": "2.3.5",
 | 
				
			||||||
@ -3312,9 +3312,9 @@
 | 
				
			|||||||
      }
 | 
					      }
 | 
				
			||||||
    },
 | 
					    },
 | 
				
			||||||
    "node_modules/electron": {
 | 
					    "node_modules/electron": {
 | 
				
			||||||
      "version": "13.0.0-beta.11",
 | 
					      "version": "13.0.0-beta.12",
 | 
				
			||||||
      "resolved": "https://registry.npmjs.org/electron/-/electron-13.0.0-beta.11.tgz",
 | 
					      "resolved": "https://registry.npmjs.org/electron/-/electron-13.0.0-beta.12.tgz",
 | 
				
			||||||
      "integrity": "sha512-hSKu4n3BIznAgnmDmURFJ7Xe7BO5B2DKw8g+J8LLE/PLverIH1qBwVzHL/oWlMLxjPCCKd8WARNX7L8X/bOJHA==",
 | 
					      "integrity": "sha512-Ly+60Sgb5PITxRWA15XrW46GXSajOO8w+BHft07TZKKuicyGN+vqZAhR8HvvOtm9nGmVrrWsi430R9t4zQL8kw==",
 | 
				
			||||||
      "dev": true,
 | 
					      "dev": true,
 | 
				
			||||||
      "hasInstallScript": true,
 | 
					      "hasInstallScript": true,
 | 
				
			||||||
      "dependencies": {
 | 
					      "dependencies": {
 | 
				
			||||||
@ -12984,9 +12984,9 @@
 | 
				
			|||||||
      }
 | 
					      }
 | 
				
			||||||
    },
 | 
					    },
 | 
				
			||||||
    "electron": {
 | 
					    "electron": {
 | 
				
			||||||
      "version": "13.0.0-beta.11",
 | 
					      "version": "13.0.0-beta.12",
 | 
				
			||||||
      "resolved": "https://registry.npmjs.org/electron/-/electron-13.0.0-beta.11.tgz",
 | 
					      "resolved": "https://registry.npmjs.org/electron/-/electron-13.0.0-beta.12.tgz",
 | 
				
			||||||
      "integrity": "sha512-hSKu4n3BIznAgnmDmURFJ7Xe7BO5B2DKw8g+J8LLE/PLverIH1qBwVzHL/oWlMLxjPCCKd8WARNX7L8X/bOJHA==",
 | 
					      "integrity": "sha512-Ly+60Sgb5PITxRWA15XrW46GXSajOO8w+BHft07TZKKuicyGN+vqZAhR8HvvOtm9nGmVrrWsi430R9t4zQL8kw==",
 | 
				
			||||||
      "dev": true,
 | 
					      "dev": true,
 | 
				
			||||||
      "requires": {
 | 
					      "requires": {
 | 
				
			||||||
        "@electron/get": "^1.0.1",
 | 
					        "@electron/get": "^1.0.1",
 | 
				
			||||||
 | 
				
			|||||||
@ -78,7 +78,7 @@
 | 
				
			|||||||
  },
 | 
					  },
 | 
				
			||||||
  "devDependencies": {
 | 
					  "devDependencies": {
 | 
				
			||||||
    "cross-env": "7.0.3",
 | 
					    "cross-env": "7.0.3",
 | 
				
			||||||
    "electron": "13.0.0-beta.11",
 | 
					    "electron": "13.0.0-beta.12",
 | 
				
			||||||
    "electron-builder": "22.10.5",
 | 
					    "electron-builder": "22.10.5",
 | 
				
			||||||
    "electron-packager": "15.2.0",
 | 
					    "electron-packager": "15.2.0",
 | 
				
			||||||
    "electron-rebuild": "2.3.5",
 | 
					    "electron-rebuild": "2.3.5",
 | 
				
			||||||
 | 
				
			|||||||
@ -303,6 +303,14 @@ class Note extends Entity {
 | 
				
			|||||||
        return this.getAttributes(LABEL, name);
 | 
					        return this.getAttributes(LABEL, name);
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    /**
 | 
				
			||||||
 | 
					     * @param {string} [name] - label name to filter
 | 
				
			||||||
 | 
					     * @returns {string[]} all note's label values, including inherited ones
 | 
				
			||||||
 | 
					     */
 | 
				
			||||||
 | 
					    getLabelValues(name) {
 | 
				
			||||||
 | 
					        return this.getLabels(name).map(l => l.value);
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    /**
 | 
					    /**
 | 
				
			||||||
     * @param {string} [name] - label name to filter
 | 
					     * @param {string} [name] - label name to filter
 | 
				
			||||||
     * @returns {Attribute[]} all note's labels (attributes with type label), excluding inherited ones
 | 
					     * @returns {Attribute[]} all note's labels (attributes with type label), excluding inherited ones
 | 
				
			||||||
@ -311,6 +319,14 @@ class Note extends Entity {
 | 
				
			|||||||
        return this.getOwnedAttributes(LABEL, name);
 | 
					        return this.getOwnedAttributes(LABEL, name);
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    /**
 | 
				
			||||||
 | 
					     * @param {string} [name] - label name to filter
 | 
				
			||||||
 | 
					     * @returns {string[]} all note's label values, excluding inherited ones
 | 
				
			||||||
 | 
					     */
 | 
				
			||||||
 | 
					    getOwnedLabelValues(name) {
 | 
				
			||||||
 | 
					        return this.getOwnedAttributes(LABEL, name).map(l => l.value);
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    /**
 | 
					    /**
 | 
				
			||||||
     * @param {string} [name] - relation name to filter
 | 
					     * @param {string} [name] - relation name to filter
 | 
				
			||||||
     * @returns {Attribute[]} all note's relations (attributes with type relation), including inherited ones
 | 
					     * @returns {Attribute[]} all note's relations (attributes with type relation), including inherited ones
 | 
				
			||||||
 | 
				
			|||||||
@ -182,9 +182,11 @@ const ATTR_HELP = {
 | 
				
			|||||||
                <ul>
 | 
					                <ul>
 | 
				
			||||||
                    <li>frontendStartup - when Trilium frontend starts up (or is refreshed).</li>
 | 
					                    <li>frontendStartup - when Trilium frontend starts up (or is refreshed).</li>
 | 
				
			||||||
                    <li>backendStartup - when Trilium backend starts up</li>
 | 
					                    <li>backendStartup - when Trilium backend starts up</li>
 | 
				
			||||||
                    <li>hourly - run once an hour</li>
 | 
					                    <li>hourly - run once an hour. You can use additional label <code>runAtHours</code> to specify at which hour.</li>
 | 
				
			||||||
                    <li>daily - run once a day</li>
 | 
					                    <li>daily - run once a day</li>
 | 
				
			||||||
                </ul>`,
 | 
					                </ul>`,
 | 
				
			||||||
 | 
					        "runOnInstance": "Define which trilium instance should run this on. Default to all instances.",
 | 
				
			||||||
 | 
					        "runAtHour": "On which hour should this run. Should be used together with <code>#run=hourly</code>",
 | 
				
			||||||
        "disableInclusion": "scripts with this label won't be included into parent script execution.",
 | 
					        "disableInclusion": "scripts with this label won't be included into parent script execution.",
 | 
				
			||||||
        "sorted": "keeps child notes sorted by title alphabetically",
 | 
					        "sorted": "keeps child notes sorted by title alphabetically",
 | 
				
			||||||
        "hidePromotedAttributes": "Hide promoted attributes on this note",
 | 
					        "hidePromotedAttributes": "Hide promoted attributes on this note",
 | 
				
			||||||
 | 
				
			|||||||
@ -25,6 +25,8 @@ const BUILTIN_ATTRIBUTES = [
 | 
				
			|||||||
    { type: 'label', name: 'iconClass' },
 | 
					    { type: 'label', name: 'iconClass' },
 | 
				
			||||||
    { type: 'label', name: 'keyboardShortcut' },
 | 
					    { type: 'label', name: 'keyboardShortcut' },
 | 
				
			||||||
    { type: 'label', name: 'run', isDangerous: true },
 | 
					    { type: 'label', name: 'run', isDangerous: true },
 | 
				
			||||||
 | 
					    { type: 'label', name: 'runOnInstance', isDangerous: false },
 | 
				
			||||||
 | 
					    { type: 'label', name: 'runAtHours', isDangerous: false },
 | 
				
			||||||
    { type: 'label', name: 'customRequestHandler', isDangerous: true },
 | 
					    { type: 'label', name: 'customRequestHandler', isDangerous: true },
 | 
				
			||||||
    { type: 'label', name: 'customResourceProvider', isDangerous: true },
 | 
					    { type: 'label', name: 'customResourceProvider', isDangerous: true },
 | 
				
			||||||
    { type: 'label', name: 'widget', isDangerous: true },
 | 
					    { type: 'label', name: 'widget', isDangerous: true },
 | 
				
			||||||
 | 
				
			|||||||
@ -2,22 +2,45 @@ const scriptService = require('./script');
 | 
				
			|||||||
const repository = require('./repository');
 | 
					const repository = require('./repository');
 | 
				
			||||||
const cls = require('./cls');
 | 
					const cls = require('./cls');
 | 
				
			||||||
const sqlInit = require('./sql_init');
 | 
					const sqlInit = require('./sql_init');
 | 
				
			||||||
 | 
					const config = require('./config');
 | 
				
			||||||
 | 
					const log = require('./log');
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					function getRunAtHours(note) {
 | 
				
			||||||
 | 
					    try {
 | 
				
			||||||
 | 
					        return note.getLabelValues('runAtHour').map(hour => parseInt(hour));
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					    catch (e) {
 | 
				
			||||||
 | 
					        log.error(`Could not parse runAtHour for note ${note.noteId}: ${e.message}`);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        return [];
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
function runNotesWithLabel(runAttrValue) {
 | 
					function runNotesWithLabel(runAttrValue) {
 | 
				
			||||||
    const notes = repository.getEntities(`
 | 
					    const notes = repository.getEntities(`
 | 
				
			||||||
        SELECT notes.* 
 | 
					        SELECT notes.* 
 | 
				
			||||||
        FROM notes 
 | 
					        FROM notes 
 | 
				
			||||||
          JOIN attributes ON attributes.noteId = notes.noteId
 | 
					        JOIN attributes ON attributes.noteId = notes.noteId
 | 
				
			||||||
                           AND attributes.isDeleted = 0
 | 
					                       AND attributes.isDeleted = 0
 | 
				
			||||||
                           AND attributes.type = 'label'
 | 
					                       AND attributes.type = 'label'
 | 
				
			||||||
                           AND attributes.name = 'run' 
 | 
					                       AND attributes.name = 'run' 
 | 
				
			||||||
                           AND attributes.value = ? 
 | 
					                       AND attributes.value = ? 
 | 
				
			||||||
        WHERE
 | 
					        WHERE
 | 
				
			||||||
          notes.type = 'code'
 | 
					          notes.type = 'code'
 | 
				
			||||||
          AND notes.isDeleted = 0`, [runAttrValue]);
 | 
					          AND notes.isDeleted = 0`, [runAttrValue]);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    const instanceName = config.General ? config.General.instanceName : null;
 | 
				
			||||||
 | 
					    const currentHours = new Date().getHours();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    for (const note of notes) {
 | 
					    for (const note of notes) {
 | 
				
			||||||
        scriptService.executeNoteNoException(note, { originEntity: note });
 | 
					        const runOnInstances = note.getLabelValues('runOnInstance');
 | 
				
			||||||
 | 
					        const runAtHours = getRunAtHours(note);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        if ((runOnInstances.length === 0 || runOnInstances.includes(instanceName))
 | 
				
			||||||
 | 
					            && (runAtHours.length === 0 || runAtHours.includes(currentHours))
 | 
				
			||||||
 | 
					        ) {
 | 
				
			||||||
 | 
					            scriptService.executeNoteNoException(note, {originEntity: note});
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user