UNPKG

publication-igid

Version:

Authorization gateway relying on an auth service for providing user editing interfaces

123 lines (111 loc) 3.75 kB
// const { GeneralTransitionEngine } = require('copious-transitions') let uuid = () => { // was uuid -- may change return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, (c) => { var r = Math.random() * 16 | 0, v = c == 'x' ? r : (r & 0x3 | 0x8); return v.toString(16); }); } // ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- // class PublicationTransitionEngineClass extends GeneralTransitionEngine { // constructor() { super() } // initialize(conf,db) { // this is a client to an endpoint... check the uploaders for configuration setting up endpoin uuid = global_appwide_token super.initialize(conf,db) } // /** * publication_op * * The topic is delivered by the web api * The topic will select the pathway for publication, a `publish on path` command. * The publication on path is a message relay object destined to an endpoint server. * * @param {string} topic - a command topic * @param {object} object - the subject of publication * @returns */ async publication_op(topic,object) { if ( this.db === undefined ) return let pdb = this.db.pdb if ( pdb === undefined ) return let result = false if ( topic.indexOf("command-") ) { let cmd = topic.replace("command-","") switch ( cmd ) { case "publish" : { result = await pdb.publish_entry(object) break; } case "unpublish" : { result = await pdb.unpublish_entry(object) break; } case "delete" : { result = await pdb.delete_entry(object) break; } case "upload" : { return "OK"; // The upload action is handled in the fetch_elements method of dynamic content. } default: {} } } else { result = await pdb.publish(topic,object) } if ( result ) { if ( result.state ) return(result.state) if ( typeof result === 'string' ) { return result } if ( typeof result.status === "string" ) return result if ( result !== false ) return "OK" return "ERR" } } // /** * forward_user_asset * * @param {string} key * @param {object} info_obj * @returns string -- indicating status of operations */ async forward_user_asset(key,info_obj) { let pdb = this.db.pdb if ( pdb === undefined ) return let result = await pdb.search_one(key) if ( !!(result) ) { if ( !!(info_obj._id) ) { info_obj._id = result._id } info_obj.user_op = 'update' await pdb.update(info_obj) } else { if ( info_obj._id && info_obj._id[0] === '+' ) { info_obj._id = uuid() + info_obj._id } else { info_obj._id = uuid() + `+${info_obj.asset_type}+${info_obj.email}` } info_obj.user_op = 'create' await pdb.create(info_obj) } if ( result ) { if ( result.state ) return(result.state) if ( typeof result === 'string' ) { return result } if ( typeof result.status === "string" ) return result if ( result !== false ) return "OK" } return "ERR" } // } // module.exports = new PublicationTransitionEngineClass()