mgebbe@0: # Copyright (C) 2014 by Intevation GmbH mgebbe@0: # Author: Mathias Gebbe mgebbe@0: # mgebbe@3: # This file is Free Software under the Apache License, Version 2.0; mgebbe@3: # and comes with NO WARRANTY! mgebbe@3: # See the documentation coming with pumpbridge for details. mgebbe@0: mgebbe@0: https = require("https") mgebbe@0: async = require("async") mgebbe@0: _ = require("underscore") mgebbe@0: Routes = require("./routes") mgebbe@0: querystring = require("querystring") mgebbe@0: EdgeControl = require("./edgecontrol") mgebbe@0: Edge = require("./edge") mgebbe@0: Host = require("../node_modules/pump.io-client-app/lib/models/host") mgebbe@0: OAuth = require('oauth') mgebbe@0: Usermap = require("./usermap") mgebbe@0: mgebbe@0: mgebbe@0: ####################################### mgebbe@0: ##### get user info pumpio ##### mgebbe@0: ####################################### mgebbe@0: getUser = (user) -> mgebbe@0: id = user.user_ESN.substr(0,user.user_ESN.indexOf('@')) mgebbe@0: server = user.user_ESN.substr(user.user_ESN.indexOf('@')+1,user.user_ESN.length) mgebbe@0: token = user.oauth_token.substr(0,user.oauth_token.indexOf(';')) mgebbe@0: secret = user.oauth_token.substr(user.oauth_token.indexOf(';')+1,user.oauth_token.length) mgebbe@0: # get tokens and ask pump.io api for userinformation then update userdb mgebbe@0: async.waterfall [ mgebbe@0: (callback) -> mgebbe@0: Host.search {hostname: server}, callback mgebbe@0: (host,callback) -> mgebbe@31: return if not (host?) mgebbe@0: # get host from db mgebbe@0: host = JSON.stringify(host) mgebbe@0: host = JSON.parse(host) mgebbe@31: return if not (host[0]?) mgebbe@0: oauth = new OAuth.OAuth(host[0].request_token_endpoint, host[0].access_token_endpoint, host[0].client_id, host[0].client_secret, "1.0A", null, "HMAC-SHA1") mgebbe@0: oauth.get "https://" + server + "/api/user/"+id, token, secret, callback mgebbe@0: ], (err,result) -> mgebbe@0: #console.log 'PUMP BODY:' + result mgebbe@17: try mgebbe@17: result = JSON.parse(result) mgebbe@17: id = result.profile.id.substr(result.profile.id.indexOf(':')+1,result.profile.id.length) mgebbe@17: Routes.updateUserDB(id,result.profile.preferredUsername,result.profile.displayName,result.profile.url,result.profile.image.url) mgebbe@17: catch err mgebbe@17: console.log 'pumpio getUser error' + err mgebbe@0: return mgebbe@0: mgebbe@0: postUser = (bridgeid, to, text, callback) -> mgebbe@0: mgebbe@0: bridgename = bridgeid.substr(0,bridgeid.indexOf('@')) mgebbe@0: server = bridgeid.substr(bridgeid.indexOf('@')+1,bridgeid.length) mgebbe@0: token=undefined mgebbe@0: secret=undefined mgebbe@0: mgebbe@0: async.waterfall [ mgebbe@0: (callback) -> mgebbe@0: Usermap.search {id: bridgeid+'_to_'+bridgeid}, callback mgebbe@0: (bridgeuser, callback) -> mgebbe@0: token = bridgeuser[0].oauth_token.substr(0,bridgeuser[0].oauth_token.indexOf(';')) mgebbe@0: secret = bridgeuser[0].oauth_token.substr(bridgeuser[0].oauth_token.indexOf(';')+1,bridgeuser[0].oauth_token.length) mgebbe@0: Host.search {hostname: server}, callback mgebbe@0: (host,callback) -> mgebbe@0: # get host from db mgebbe@31: return if not (host?) mgebbe@0: host = JSON.stringify(host) mgebbe@0: host = JSON.parse(host) mgebbe@31: return if not (host[0]?) mgebbe@0: activity = mgebbe@0: verb: "post" mgebbe@0: #cc: [ mgebbe@0: # id: "http://activityschema.org/collection/public" mgebbe@0: # objectType: "collection" mgebbe@0: #] mgebbe@0: to: [ mgebbe@0: id: "acct:" + to mgebbe@0: objectType: "person" mgebbe@0: ] mgebbe@0: object: mgebbe@0: objectType: "note" mgebbe@0: content: text mgebbe@0: oauth = new OAuth.OAuth(host[0].request_token_endpoint, host[0].access_token_endpoint, host[0].client_id, host[0].client_secret, "1.0A", null, "HMAC-SHA1") mgebbe@0: oauth.post "https://" + server + "/api/user/" + bridgename + "/feed", token, secret, JSON.stringify(activity), 'application/json', callback mgebbe@0: ], (err,result) -> mgebbe@0: #console.log 'PUMP BODY:' + result mgebbe@0: callback null, result mgebbe@0: mgebbe@0: return mgebbe@0: mgebbe@0: ####################################### mgebbe@0: ##### get user pumpio feed ##### mgebbe@0: ####################################### mgebbe@0: getUserFeed = (user, callback) -> mgebbe@0: id = user.user_ESN.substr(0,user.user_ESN.indexOf('@')) mgebbe@0: server = user.user_ESN.substr(user.user_ESN.indexOf('@')+1,user.user_ESN.length) mgebbe@0: token = user.oauth_token.substr(0,user.oauth_token.indexOf(';')) mgebbe@0: secret = user.oauth_token.substr(user.oauth_token.indexOf(';')+1,user.oauth_token.length) mgebbe@0: # get tokens and ask pump.io api for userinformation then update userdb mgebbe@0: async.waterfall [ mgebbe@0: (callback) -> mgebbe@0: Host.search {hostname: server}, callback mgebbe@0: (host,callback) -> mgebbe@0: # get host from db mgebbe@31: return if not (host?) mgebbe@0: host = JSON.stringify(host) mgebbe@0: host = JSON.parse(host) mgebbe@31: return if not (host[0]?) mgebbe@0: oauth = new OAuth.OAuth(host[0].request_token_endpoint, host[0].access_token_endpoint, host[0].client_id, host[0].client_secret, "1.0A", null, "HMAC-SHA1") mgebbe@0: oauth.get "https://" + server + "/api/user/"+id+"/feed", token, secret, callback mgebbe@0: ], (err,result) -> mgebbe@0: #console.log 'PUMP BODY:' + result mgebbe@17: try mgebbe@17: result = JSON.parse(result) mgebbe@17: callback null, result mgebbe@17: catch err mgebbe@17: console.log "pump.io Error" mgebbe@17: callback null, null mgebbe@0: return mgebbe@0: mgebbe@0: ####################################### mgebbe@0: ##### get user pumpio note ##### mgebbe@0: ####################################### mgebbe@0: getNote = (user, noteurl, callback) -> mgebbe@0: id = user.user_ESN.substr(0,user.user_ESN.indexOf('@')) mgebbe@0: server = user.user_ESN.substr(user.user_ESN.indexOf('@')+1,user.user_ESN.length) mgebbe@0: token = user.oauth_token.substr(0,user.oauth_token.indexOf(';')) mgebbe@0: secret = user.oauth_token.substr(user.oauth_token.indexOf(';')+1,user.oauth_token.length) mgebbe@0: mgebbe@0: # get tokens and ask pump.io api for userinformation then update userdb mgebbe@0: async.waterfall [ mgebbe@0: (callback) -> mgebbe@0: Host.search {hostname: server}, callback mgebbe@0: (host,callback) -> mgebbe@31: return if not (host?) mgebbe@0: # get host from db mgebbe@0: host = JSON.stringify(host) mgebbe@0: host = JSON.parse(host) mgebbe@31: return if not (host[0]?) mgebbe@0: oauth = new OAuth.OAuth(host[0].request_token_endpoint, host[0].access_token_endpoint, host[0].client_id, host[0].client_secret, "1.0A", null, "HMAC-SHA1") mgebbe@0: oauth.get noteurl, token, secret, callback mgebbe@0: ], (err,result) -> mgebbe@0: #console.log 'PUMP BODY:' + result mgebbe@8: if err mgebbe@8: callback null, null mgebbe@8: else mgebbe@8: result = JSON.parse(result) mgebbe@8: callback null, result mgebbe@0: return mgebbe@0: mgebbe@30: getLikes = (user, callback) -> mgebbe@31: return if not (user?) mgebbe@30: id = user.user_ESN.substr(0,user.user_ESN.indexOf('@')) mgebbe@30: server = user.user_ESN.substr(user.user_ESN.indexOf('@')+1,user.user_ESN.length) mgebbe@30: token = user.oauth_token.substr(0,user.oauth_token.indexOf(';')) mgebbe@30: secret = user.oauth_token.substr(user.oauth_token.indexOf(';')+1,user.oauth_token.length) mgebbe@30: # https://io.intevation.de/api/user/mgebbe/favorites mgebbe@30: url = "https://" + server + "/api/user/" + id + "/favorites" mgebbe@30: # get tokens and ask pump.io api for userinformation then update userdb mgebbe@30: async.waterfall [ mgebbe@30: (callback) -> mgebbe@30: Host.search {hostname: server}, callback mgebbe@30: (host,callback) -> mgebbe@30: # get host from db mgebbe@31: return if not (host?) mgebbe@30: host = JSON.stringify(host) mgebbe@30: host = JSON.parse(host) mgebbe@31: return if not (host[0]?) mgebbe@30: #console.log JSON.stringify host mgebbe@30: oauth = new OAuth.OAuth(host[0].request_token_endpoint, host[0].access_token_endpoint, host[0].client_id, host[0].client_secret, "1.0A", null, "HMAC-SHA1") mgebbe@30: oauth.get url, token, secret, callback mgebbe@30: ], (err,result) -> mgebbe@30: if err mgebbe@30: callback null, null mgebbe@30: else mgebbe@30: callback null, (result) mgebbe@30: return mgebbe@30: mgebbe@30: mgebbe@30: mgebbe@0: isPublicActivity = (act) -> mgebbe@0: recip = [] mgebbe@0: _.each [ mgebbe@0: "to" mgebbe@0: "cc" mgebbe@0: "bto" mgebbe@0: "bcc" mgebbe@0: ], (prop) -> mgebbe@0: recip = recip.concat(act[prop]) if _.isArray(act[prop]) mgebbe@0: return mgebbe@0: mgebbe@0: _.some recip, (rec) -> mgebbe@0: rec.objectType is "collection" and rec.id is "http://activityschema.org/collection/public" mgebbe@0: mgebbe@0: mgebbe@0: exports.isPublicActivity = isPublicActivity mgebbe@0: exports.getUserFeed = getUserFeed mgebbe@0: exports.getUser = getUser mgebbe@0: exports.getNote = getNote mgebbe@30: exports.getLikes = getLikes mgebbe@0: exports.postUser = postUser