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

158 lines
5.9 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.5.1',
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://#{_flitter.config('server.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:
"some name": "some data",
},
}`,
php: `<?php
// ===========================================================
// DEVBUG INLINE DEBUGGING HELPER - FOR USE WITH DEVBUG SERVER
// TODO: REMOVE BEFORE COMMITTING
$dev_outs = [];
function out($key, $what, $group = null){
global $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;
}
}
function breakpoint($continue = false, $name = null){
global $dev_outs;
$devbug = "http://localhost:8000/";
$project_api_key = "CHANGEME";
$bt = debug_backtrace();
$caller = array_shift($bt);
// 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(); }
}
// ===========================================================`,
},
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