const Unit = require('libflitter/Unit') /* * The Miscellaneous Unit * ------------------------------------------------------------- * This is a Unit file where you should make any modifications that * your application may need such as custom Express add-ons. Changes * here are loaded after the core Flitter units, but before the other * units (secondary, custom, error, and App), so they are available to * other units in the stack. */ class MiscUnit extends Unit { /* * Initializes the unit class. * This is called OUTSIDE of a Flitter context, * so no other contexts are available. Modifications here * take place before any part of Flitter is loaded or available. */ constructor(){ super() } /* * Initialize the actual Unit. * This is where most of the changes will go. * This is provided an instance of the Express app * so any custom changes can be made. The core Flitter * contexts are available here, so things like config(), * model(), etc. work. */ async go(app, context){ String.prototype.capitalize = function(){ return this.charAt(0).toUpperCase() + this.slice(1) } // do stuff here global.devbug = { version: '0.7.0', get_inline: function(type, project){ let code = this.code[type]; if (!code) return false code = code.replace(/{{ Project API Key }}/g, project.uuid).replace(/{{ Server URL }}/g, _flitter.config('server.url')) return code }, code: { node: `require("devbugjs") dbsetup({ \tserver: "{{ Server URL }}", // DevBug Server URL \tproject: "{{ Project API Key }}", // Project API Key })`, api: `// Send a multipart/form-data POST request to: // http://#{_flitter.config('server.url')}/api/v1/out/ // The form should have a single field with the name "data". // This field should contain a valid JSON string with the following structure: { "brief": "Some preview text to be displayed. Whatever you want.", "data": { // the output data (key-pairs) goes here: "some name": "some data", }, }`, php: ` $what ]; else $dev_outs[$group][$key] = $what; } else $dev_outs[$key] = $what; $GLOBALS['devbug_dev_outs'] = $dev_outs; } function breakpoint($continue = false, $name = null){ $dev_outs = $GLOBALS['devbug_dev_outs']; $devbug = "{{ Server URL }}/"; $project_api_key = "{{ Project API Key }}"; $bt = debug_backtrace(); $caller = array_shift($bt); $time = devbug_microtime(); $dev_outs['DevBug Summary'] = [ 'Start Time' => $GLOBALS['devbug_mark_time'], 'Breakpoint Time' => $time, 'Total Execution Time' => $time - $GLOBALS['devbug_mark_time'], 'Breakpoint Stacktrace' => $caller, ]; // Send to devbug server $ch = curl_init(); $url = $devbug.'api/v1/out/'.$project_api_key; curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, [ 'data' => json_encode([ 'brief' => ($name ? $name : 'Breakpoint').': '.$caller['file'].': '.$caller['line'], 'data' => $dev_outs, ]) ]); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); $odata = curl_exec($ch); if ( !$continue ) { exit(); } } function devbugtime($prefix = 'time: '){ $mt = explode(' ', microtime()); $mt = $mt[1].' '.$mt[0]; return $prefix.$mt; } function outt($key, $what, $group = null){ out(devbugtime($key), $what, $group); } function outi($key, $what, $group = null){ $keyname = $group ? $key.$group : $key; if ( !$GLOBALS['devbug_dev_out_iters'][$keyname] ) $GLOBALS['devbug_dev_out_iters'][$keyname] = 0; out($key.'_'.$GLOBALS['devbug_dev_out_iters'][$keyname], $what, $group); $GLOBALS['devbug_dev_out_iters'][$keyname]++; } function point(){ if ( !$GLOBALS['devbug_current_bench'] ) benchmark(); $bt = debug_backtrace(); $caller = array_shift($bt); out('point: '.$caller['file'].': '.$caller['line'], devbugtime(), $GLOBALS['devbug_current_bench']); } function benchmark($name = "DevBug"){ $GLOBALS['devbug_current_bench'] = "Benchmark: ".$name; point(); } function benchmarki($name = "DevBug"){ if ( !$GLOBALS['devbug_bench_iters'][$name] ) $GLOBALS['devbug_bench_iters'][$name] = 1; benchmark($name.'('.$GLOBALS['devbug_bench_iters'][$name].')'); $GLOBALS['devbug_bench_iters'][$name]++; } function devbug_chop($string, $at=120){ return substr($string, 0, 30); } // ===========================================================`, }, permission: { project: { async edit(project, target_user){ if ( project.user_id === target_user.uuid ) return true else if ( project.edit_user_ids.includes(target_user.uuid) ) return true else return false }, sync_edit(project, target_user){ if ( project.user_id === target_user.uuid ) return true else if ( project.edit_user_ids.includes(target_user.uuid) ) return true else return false }, async view(project, target_user){ if ( project.user_id === target_user.uuid ) return true else if ( project.shared_user_ids.includes(target_user.uuid) ) return true else if ( project.edit_user_ids.includes(target_user.uuid) ) return true else return false }, async owns(project, target_user){ return (project.user_id === target_user.uuid) } }, snippet: { async edit(snippet, target_user){ const project = await _flitter.model('v1:Project').findById(snippet.project_id) if ( snippet.user_id === target_user.uuid ) return true else if ( snippet.edit_user_ids.includes(target_user.uuid) ) return true else if ( await devbug.permission.project.edit(project, target_user) ) return true else return false }, async view(snippet, target_user){ const project = await _flitter.model('v1:Project').findById(snippet.project_id) if ( this.edit(snippet, target_user) ) return true else if ( snippet.shared_user_ids.includes(target_user.uuid) ) return true else if ( await devbug.permission.project.view(project, target_user) ) return true else return false }, async owns(snippet, target_user){ const project = await _flitter.model('v1:Project').findById(snippet.project_id) if ( snippet.user_id === target_user.uuid ) return true else if ( await devbug.permission.project.owns(project, target_user) ) return true else return false } } }, } } name(){ return "misc" } } module.exports = exports = MiscUnit