mgebbe@0: # Copyright (C) 2014 by Intevation GmbH mgebbe@0: # Author: Mathias Gebbe mgebbe@0: # mgebbe@0: # This file is Free Software under the Apache License, Version 2.0 mgebbe@0: # (the "License"); and comes with ABSOLUTELY NO WARRANTY! mgebbe@0: # You may not use this file except in compliance with the License. mgebbe@0: # See LICENSE 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@0: # get host from db mgebbe@0: host = JSON.stringify(host) mgebbe@0: host = JSON.parse(host) 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@0: result = JSON.parse(result) mgebbe@0: id = result.profile.id.substr(result.profile.id.indexOf(':')+1,result.profile.id.length) mgebbe@0: Routes.updateUserDB(id,result.profile.preferredUsername,result.profile.displayName,result.profile.url,result.profile.image.url) 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@0: host = JSON.stringify(host) mgebbe@0: host = JSON.parse(host) 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@0: host = JSON.stringify(host) mgebbe@0: host = JSON.parse(host) 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@0: result = JSON.parse(result) mgebbe@0: callback null, result 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@0: # get host from db mgebbe@0: host = JSON.stringify(host) mgebbe@0: host = JSON.parse(host) 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@0: result = JSON.parse(result) mgebbe@0: callback null, result mgebbe@0: return mgebbe@0: 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@0: exports.postUser = postUser