devbug/app/MiscUnit.js

158 lines
5.9 KiB
JavaScript
Raw Normal View History

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-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-07-24 20:30:51 +00:00
version: '0.5.1',
2019-06-23 17:17:35 +00:00
code: {
2019-06-24 16:45:22 +00:00
node: `require("devbugjs")
dbsetup({
2019-06-24 14:35:49 +00:00
\tserver: "https://CHANGEME:8000/", // DevBug Server URL
\tproject: "CHANGEME", // Project API Key
})`,
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
$dev_outs = [];
function out($key, $what, $group = null){
2019-07-24 20:30:51 +00:00
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;
}
2019-06-23 17:17:35 +00:00
}
2019-07-24 20:30:51 +00:00
function breakpoint($continue = false, $name = null){
global $dev_outs;
$devbug = "http://localhost:8000/";
$project_api_key = "CHANGEME";
$bt = debug_backtrace();
$caller = array_shift($bt);
2019-06-23 17:17:35 +00:00
2019-07-24 20:30:51 +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,
])
]);
2019-06-23 17:17:35 +00:00
2019-07-24 20:30:51 +00:00
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
2019-06-23 17:17:35 +00:00
2019-07-24 20:30:51 +00:00
$odata = curl_exec($ch);
2019-06-23 17:17:35 +00:00
2019-07-24 20:30:51 +00:00
if ( !$continue ) { exit(); }
2019-06-23 17:17:35 +00:00
}
2019-06-24 18:00:36 +00:00
// ===========================================================`,
},
permission: {
project: {
2019-07-24 17:05:27 +00:00
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
},
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){
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){
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-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