73 lines
4.0 KiB
JavaScript
73 lines
4.0 KiB
JavaScript
|
|
module.exports = function(RED) {
|
||
|
|
var https = require('https');
|
||
|
|
var http = require('http');
|
||
|
|
var url = require('url');
|
||
|
|
|
||
|
|
var OPERATIONS = {
|
||
|
|
'status:my': { method:'GET', path:'/ocs/v2.php/apps/user_status/api/v1/user_status' },
|
||
|
|
'status:set': { method:'PUT', path:'/ocs/v2.php/apps/user_status/api/v1/user_status/status', body:['statusType'] },
|
||
|
|
'status:predefined': { method:'GET', path:'/ocs/v2.php/apps/user_status/api/v1/predefined_statuses' },
|
||
|
|
'status:setPredefined':{ method:'PUT', path:'/ocs/v2.php/apps/user_status/api/v1/user_status/message/predefined', body:['messageId','clearAt'] },
|
||
|
|
'status:setCustom': { method:'PUT', path:'/ocs/v2.php/apps/user_status/api/v1/user_status/message/custom', body:['statusIcon','message','clearAt'] },
|
||
|
|
'status:clear': { method:'DELETE', path:'/ocs/v2.php/apps/user_status/api/v1/user_status/message' },
|
||
|
|
'status:revert': { method:'DELETE', path:'/ocs/v2.php/apps/user_status/api/v1/user_status/revert/{messageId}' },
|
||
|
|
'status:heartbeat': { method:'PUT', path:'/ocs/v2.php/apps/user_status/api/v1/heartbeat', body:['status'] },
|
||
|
|
'status:list': { method:'GET', path:'/ocs/v2.php/apps/user_status/api/v1/statuses', query:['limit','offset'] },
|
||
|
|
'status:find': { method:'GET', path:'/ocs/v2.php/apps/user_status/api/v1/statuses/{userId}' }
|
||
|
|
};
|
||
|
|
|
||
|
|
function USNode(config) {
|
||
|
|
RED.nodes.createNode(this, config);
|
||
|
|
var node = this;
|
||
|
|
this.configNode = RED.nodes.getNode(config.nextcloud);
|
||
|
|
this.operation = config.operation || 'status:my';
|
||
|
|
this.userId = config.userId; this.messageId = config.messageId;
|
||
|
|
|
||
|
|
if (!this.configNode) { node.error("No NC config node"); return; }
|
||
|
|
|
||
|
|
node.on('input', function(msg) {
|
||
|
|
var op = OPERATIONS[msg.operation || node.operation];
|
||
|
|
if (!op) { node.error("Unknown: "+(msg.operation||node.operation), msg); return; }
|
||
|
|
|
||
|
|
var baseUrl = (node.configNode.baseUrl||'').replace(/\/+$/,'');
|
||
|
|
var uname = node.configNode.credentials.username;
|
||
|
|
var pw = node.configNode.credentials.password;
|
||
|
|
var path = op.path
|
||
|
|
.replace(/\{userId\}/g, msg.userId||node.userId||'')
|
||
|
|
.replace(/\{messageId\}/g, msg.messageId||node.messageId||'');
|
||
|
|
|
||
|
|
var qp = [];
|
||
|
|
if (op.query) op.query.forEach(function(q) {
|
||
|
|
var v = msg[q]!==undefined ? msg[q] : '';
|
||
|
|
if (v!==undefined&&v!=='') qp.push(encodeURIComponent(q)+'='+encodeURIComponent(v));
|
||
|
|
});
|
||
|
|
var fu = baseUrl+path+(qp.length?'?'+qp.join('&'):'');
|
||
|
|
var pu = url.parse(fu);
|
||
|
|
|
||
|
|
var h = {'OCS-APIRequest':'true','Accept':'application/json',
|
||
|
|
'Authorization':'Basic '+Buffer.from(uname+':'+pw).toString('base64')};
|
||
|
|
|
||
|
|
var bo={};
|
||
|
|
if (op.body) op.body.forEach(function(b){
|
||
|
|
var v=msg[b]!==undefined?msg[b]:'';
|
||
|
|
if(v!==undefined&&v!=='') bo[b]=v;
|
||
|
|
});
|
||
|
|
var bs=null;
|
||
|
|
if (Object.keys(bo).length>0) {h['Content-Type']='application/json'; bs=JSON.stringify(bo);}
|
||
|
|
|
||
|
|
var o = {hostname:pu.hostname,port:pu.port||(pu.protocol==='https:'?443:80),
|
||
|
|
path:pu.path,method:op.method,headers:h,rejectUnauthorized:false};
|
||
|
|
node.log(op.method+' '+fu);
|
||
|
|
var t = pu.protocol==='https:'?https:http;
|
||
|
|
var r = t.request(o,function(res){
|
||
|
|
var b='';res.on('data',function(c){b+=c;});
|
||
|
|
res.on('end',function(){msg.statusCode=res.statusCode;msg.operation=msg.operation||node.operation;
|
||
|
|
try{msg.payload=JSON.parse(b);}catch(e){msg.payload=b;}node.send(msg);});
|
||
|
|
});
|
||
|
|
r.on('error',function(e){msg.error=e.message;node.error(op.method+' '+fu+' - '+e.message,msg);});
|
||
|
|
if(bs) r.write(bs);
|
||
|
|
r.end();
|
||
|
|
});
|
||
|
|
}
|
||
|
|
RED.nodes.registerType("userstatus", USNode);
|
||
|
|
};
|