You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
devbug/app/MiscUnit.js

211 lines
7.7 KiB

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){
// do stuff here
global.devbug = {
version: '0.2.0',
code: {
node: `require("devbugjs")
dbsetup({
\tserver: "https://CHANGEME:8000/", // DevBug Server URL
\tproject: "CHANGEME", // Project API Key
})`,
api: `Send a multipart/form-data POST request to:
http(s)://<devbug url>/api/v1/out/<project api key>
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
}
}`,
php: `<?php
// ===========================================================
// DEVBUG INLINE DEBUGGING HELPER - FOR USE WITH DEVBUG SERVER
// TODO: REMOVE BEFORE COMMITTING
$dev_outs = [];
function out($key, $what, $group = null){
\tglobal $dev_outs;
\tif ( $group ){
\t\tif ( !array_key_exists($group, $dev_outs) ){
\t\t\t$dev_outs[$group] = [ $key => $what ];
\t\t}
\t\telse {
\t\t\t$dev_outs[$group][$key] = $what;
\t\t}
\t}
\telse {
\t\t$dev_outs[$key] = $what;
\t}
}
function breakpoint($html = false, $name = null){
\tglobal $dev_outs;
\t$devbug = "http://CHANGEME:8000/";
\t$project_api_key = "CHANGEME";
\t$bt = debug_backtrace();
\t$caller = array_shift($bt);
\tif ( !$html ){
\t\tvar_dump([($item ? $item : $caller), 'outs' => $dev_outs]);
\t}
\telse {
\t\techo "<pre><code>";
\t\tvar_dump([($item ? $item : $caller), 'outs' => $dev_outs]);
\t\techo "</code></pre>";
\t}
\t// Send to devbug server
\t$ch = curl_init();
\t$url = $devbug.'api/v1/out/'.$project_api_key;
\tvar_dump($url);
\tcurl_setopt($ch, CURLOPT_URL, $url);
\tcurl_setopt($ch, CURLOPT_POST, 1);
\tcurl_setopt($ch, CURLOPT_POSTFIELDS, [
\t 'data' => json_encode([
\t 'brief' => ($name ? $name : 'Breakpoint').': '.$caller['file'].': '.$caller['line'],
\t 'data' => $dev_outs,
\t ])
\t]);
\tcurl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
\t
\t$odata = curl_exec($ch);
\texit();
}
// ===========================================================`,
js: `// ===========================================================
// DEVBUG INLINE DEBUGGING HELPER - FOR USE WITH DEVBUG SERVER
// TODO: REMOVE BEFORE COMMITTING
let outs = {}
const devbug_url = 'http://CHANGEME:8000/'
const project_api_key = 'CHANGEME'
const out = (key, what, group=false) => {
if ( group ){
if ( Object.keys(outs).includes(group) ) outs[group][key] = what
else outs[group] = {}; outs[group][key] = where
}
else {
outs[key] = what
}
}
const breakpoint = (html = false, name = null) => {
var e = new Error();
(function() {
// Load the script
var script = document.createElement("SCRIPT");
script.src = 'https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js';
script.type = 'text/javascript';
script.onload = () => {
var $ = window.jQuery;
$(() => {
e = e.stack.split('at'); var caller = '';
if ( e.length < 3 ) e = e.stack.split('@')
if ( e.length > 2 ) caller = e[2].trim()
else if ( e.length > 1 ) caller = e[1]
else caller = 'Unable to determine stacktrace'
var data = new FormData();
data.append('data', JSON.stringify({brief: (name ? name : 'Breakpoint: ')+caller,data: outs}))
$.ajax({
url: devbug_url+'api/v1/out/'+project_api_key,
data: data, cache: false, contentType: false, processData: false, method: 'POST', type: 'POST',
success: (res) => { console.log('DevBug POST Completed'); console.log(res) }
})
});
};
document.getElementsByTagName("head")[0].appendChild(script);
})();
}
if ( window ) window.out = out; window.breakpoint = breakpoint;
// ===========================================================`
},
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