2019-06-21 22:01:34 +00:00
|
|
|
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 {
|
2019-06-23 17:17:35 +00:00
|
|
|
|
2019-06-21 22:01:34 +00:00
|
|
|
/*
|
|
|
|
* 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()
|
2019-06-23 17:17:35 +00:00
|
|
|
|
|
|
|
|
2019-06-21 22:01:34 +00:00
|
|
|
}
|
2019-06-23 17:17:35 +00:00
|
|
|
|
2019-06-21 22:01:34 +00:00
|
|
|
/*
|
|
|
|
* 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){
|
2019-07-29 19:11:37 +00:00
|
|
|
|
|
|
|
String.prototype.capitalize = function(){
|
|
|
|
return this.charAt(0).toUpperCase() + this.slice(1)
|
|
|
|
}
|
2019-06-23 17:17:35 +00:00
|
|
|
|
2019-06-21 22:01:34 +00:00
|
|
|
// do stuff here
|
2019-06-23 17:17:35 +00:00
|
|
|
global.devbug = {
|
2019-08-07 18:12:57 +00:00
|
|
|
version: '0.6.2',
|
2019-08-07 18:01:05 +00:00
|
|
|
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
|
|
|
|
},
|
2019-06-23 17:17:35 +00:00
|
|
|
code: {
|
2019-06-24 16:45:22 +00:00
|
|
|
node: `require("devbugjs")
|
|
|
|
dbsetup({
|
2019-08-07 18:01:05 +00:00
|
|
|
\tserver: "{{ Server URL }}", // DevBug Server URL
|
|
|
|
\tproject: "{{ Project API Key }}", // Project API Key
|
2019-06-24 14:35:49 +00:00
|
|
|
})`,
|
2019-07-24 20:30:51 +00:00
|
|
|
api: `// Send a multipart/form-data POST request to:
|
|
|
|
// http://#{_flitter.config('server.url')}/api/v1/out/<project api key>
|
2019-06-24 18:00:36 +00:00
|
|
|
|
2019-07-24 20:30:51 +00:00
|
|
|
// The form should have a single field with the name "data".
|
|
|
|
// This field should contain a valid JSON string with the following structure:
|
2019-06-24 18:00:36 +00:00
|
|
|
{
|
|
|
|
"brief": "Some preview text to be displayed. Whatever you want.",
|
|
|
|
"data": {
|
2019-07-24 20:30:51 +00:00
|
|
|
// the output data (key-pairs) goes here:
|
|
|
|
"some name": "some data",
|
|
|
|
},
|
2019-06-24 18:00:36 +00:00
|
|
|
}`,
|
2019-06-23 17:17:35 +00:00
|
|
|
php: `<?php
|
|
|
|
// ===========================================================
|
|
|
|
// DEVBUG INLINE DEBUGGING HELPER - FOR USE WITH DEVBUG SERVER
|
|
|
|
// TODO: REMOVE BEFORE COMMITTING
|
2019-08-14 15:03:19 +00:00
|
|
|
function devbug_microtime($difftime = 0){
|
|
|
|
$time = explode(' ', microtime()); return ((float)$time[1]+(float)$time[0])-$difftime; }
|
|
|
|
$GLOBALS['devbug_dev_outs'] = [];
|
|
|
|
$GLOBALS['devbug_dev_out_iters'] = [];
|
|
|
|
$GLOBALS['devbug_mark_time'] = devbug_microtime();
|
2019-06-23 17:17:35 +00:00
|
|
|
function out($key, $what, $group = null){
|
2019-08-14 15:03:19 +00:00
|
|
|
$dev_outs = $GLOBALS['devbug_dev_outs'];
|
|
|
|
if ( $group ){
|
|
|
|
if ( !array_key_exists($group, $dev_outs) ) $dev_outs[$group] = [ $key => $what ];
|
|
|
|
else $dev_outs[$group][$key] = $what;
|
|
|
|
}
|
|
|
|
else $dev_outs[$key] = $what;
|
|
|
|
$GLOBALS['devbug_dev_outs'] = $dev_outs;
|
2019-06-23 17:17:35 +00:00
|
|
|
}
|
2019-07-24 20:30:51 +00:00
|
|
|
function breakpoint($continue = false, $name = null){
|
2019-08-14 15:03:19 +00:00
|
|
|
$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,
|
|
|
|
];
|
2019-06-23 17:17:35 +00:00
|
|
|
|
2019-08-14 15:03:19 +00:00
|
|
|
// 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 outtime($key, $what, $group = null){ out(devbugtime($key), $what, $group); }
|
|
|
|
function outiter($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 outpoint($group){
|
|
|
|
$bt = debug_backtrace();
|
|
|
|
$caller = array_shift($bt);
|
|
|
|
out('point: '.$caller['file'].': '.$caller['line'], devbugtime(), $group);
|
2019-06-23 17:17:35 +00:00
|
|
|
}
|
2019-08-14 15:03:19 +00:00
|
|
|
function devbug_chop($string, $at=120){ return substr($string, 0, 30); }
|
2019-06-24 18:00:36 +00:00
|
|
|
// ===========================================================`,
|
2019-07-10 19:10:36 +00:00
|
|
|
},
|
|
|
|
permission: {
|
|
|
|
project: {
|
2019-07-24 17:05:27 +00:00
|
|
|
async edit(project, target_user){
|
2019-07-10 19:10:36 +00:00
|
|
|
if ( project.user_id === target_user.uuid ) return true
|
|
|
|
else if ( project.edit_user_ids.includes(target_user.uuid) ) return true
|
|
|
|
else return false
|
|
|
|
},
|
2019-07-24 17:05:27 +00:00
|
|
|
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){
|
2019-07-10 19:10:36 +00:00
|
|
|
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
|
|
|
|
},
|
2019-07-24 17:05:27 +00:00
|
|
|
async owns(project, target_user){
|
2019-07-10 19:10:36 +00:00
|
|
|
return (project.user_id === target_user.uuid)
|
|
|
|
}
|
2019-07-24 14:52:58 +00:00
|
|
|
},
|
|
|
|
snippet: {
|
2019-07-24 17:05:27 +00:00
|
|
|
async edit(snippet, target_user){
|
|
|
|
const project = await _flitter.model('v1:Project').findById(snippet.project_id)
|
2019-07-24 14:52:58 +00:00
|
|
|
if ( snippet.user_id === target_user.uuid ) return true
|
|
|
|
else if ( snippet.edit_user_ids.includes(target_user.uuid) ) return true
|
2019-07-24 17:05:27 +00:00
|
|
|
else if ( await devbug.permission.project.edit(project, target_user) ) return true
|
2019-07-24 14:52:58 +00:00
|
|
|
else return false
|
|
|
|
},
|
2019-07-24 17:05:27 +00:00
|
|
|
async view(snippet, target_user){
|
|
|
|
const project = await _flitter.model('v1:Project').findById(snippet.project_id)
|
2019-07-24 14:52:58 +00:00
|
|
|
if ( this.edit(snippet, target_user) ) return true
|
2019-07-24 17:05:27 +00:00
|
|
|
else if ( snippet.shared_user_ids.includes(target_user.uuid) ) return true
|
|
|
|
else if ( await devbug.permission.project.view(project, target_user) ) return true
|
2019-07-24 14:52:58 +00:00
|
|
|
else return false
|
|
|
|
},
|
2019-07-24 17:05:27 +00:00
|
|
|
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
|
2019-07-24 14:52:58 +00:00
|
|
|
}
|
2019-07-10 19:10:36 +00:00
|
|
|
}
|
|
|
|
},
|
2019-06-23 17:17:35 +00:00
|
|
|
}
|
2019-06-21 22:01:34 +00:00
|
|
|
}
|
2019-06-23 17:17:35 +00:00
|
|
|
|
2019-06-21 22:01:34 +00:00
|
|
|
name(){
|
|
|
|
return "misc"
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-06-23 17:17:35 +00:00
|
|
|
module.exports = exports = MiscUnit
|