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: async = require("async") mgebbe@0: _ = require("underscore") mgebbe@0: PumpIOClientApp = require("pump.io-client-app") mgebbe@0: databank = require 'databank' mgebbe@0: Databank = databank.Databank mgebbe@0: DatabankObject = databank.DatabankObject mgebbe@0: RequestToken = PumpIOClientApp.RequestToken mgebbe@0: userAuth = PumpIOClientApp.userAuth mgebbe@0: userOptional = PumpIOClientApp.userOptional mgebbe@0: userRequired = PumpIOClientApp.userRequired mgebbe@0: noUser = PumpIOClientApp.noUser mgebbe@0: Usermap = require("./usermap") mgebbe@0: Google = require("./google") mgebbe@0: Facebook = require("./facebook") mgebbe@3: Twitterroutes = require("./twitterroutes") mgebbe@0: Pump = require("./pumpio") mgebbe@0: User = require("./user") mgebbe@0: Edge = require("./edge") mgebbe@0: Host = require("../node_modules/pump.io-client-app/lib/models/host") mgebbe@0: https = require("https") mgebbe@0: OAuth = require('oauth') mgebbe@0: Sync = require('./sync') mgebbe@0: mgebbe@0: addRoutes = (app, options) -> mgebbe@0: mgebbe@0: getBridge = (req, res) -> mgebbe@0: res.send 'Hello, i am the bridge you looking for!' mgebbe@0: return mgebbe@0: mgebbe@0: saveBridge = (req, res) -> mgebbe@0: mgebbe@0: pumpid = req.body.pumpid mgebbe@0: pumptoken = req.body.pumptoken mgebbe@0: pumpsecret = req.body.pumpsecret mgebbe@0: mgebbe@0: gpid = req.body.gpid mgebbe@0: gptoken = req.body.gptoken mgebbe@7: gpdelete = req.body.gpdelete mgebbe@0: mgebbe@0: fbid = req.body.fbid mgebbe@0: fbtoken = req.body.fbtoken mgebbe@7: fbdelete = req.body.fbdelete mgebbe@17: mgebbe@7: twdelete = req.body.twdelete mgebbe@0: mgebbe@0: # create/update userdb entrys mgebbe@0: # SET THE CONNECTION BETWEENS PUMPIO AND ESN-ACCOUNT (facebook or google) mgebbe@0: mgebbe@0: #### PUMPIO STUFF #### mgebbe@3: if not (pumpid?) mgebbe@0: console.log 'no pumpio' mgebbe@0: else mgebbe@0: saveUsermap(pumpid,pumpid,pumptoken+';'+pumpsecret,pumpsecret, (err, result) -> mgebbe@0: Pump.getUser(result)) mgebbe@0: mgebbe@0: #async.waterfall [(callback) -> mgebbe@0: # searchUsermap(bridgeid,bridgeid, callback) mgebbe@0: #], (result) -> mgebbe@0: # Pump.postUser(result, 'mgebbe@io.intevation.de','Hallo Welt') mgebbe@0: mgebbe@0: #### GOOGLE STUFF #### mgebbe@8: if not (gpid?) or not (gptoken?) or gpid is "" or gptoken is "" mgebbe@0: console.log 'no google' mgebbe@0: else mgebbe@0: async.waterfall [ mgebbe@0: (callback) -> mgebbe@0: saveUsermap(pumpid,gpid,gptoken,'', callback) mgebbe@0: (user, callback) -> mgebbe@0: Google.getRefreshToken(user,callback) mgebbe@0: (token, callback) -> mgebbe@3: saveUsermap(pumpid,gpid,token.access_token + ";" + gptoken.substr(gptoken.indexOf(';')+1,gptoken.length),token.refresh_token,callback) mgebbe@0: ], (err, result) -> mgebbe@0: # now we have a gp refresh token mgebbe@0: #console.log 'done' mgebbe@7: if (gpdelete?) mgebbe@7: console.log "delete gp account" mgebbe@7: async.waterfall [ mgebbe@7: (callback) -> mgebbe@7: Usermap.search {user_pumpio: gpdelete}, callback mgebbe@7: (result, callback) -> mgebbe@7: _.each result, (um) -> mgebbe@7: if um.user_ESN.indexOf('@google') isnt -1 mgebbe@7: deleteUsermap(um.id,callback) mgebbe@7: return mgebbe@7: ], (err, result) -> mgebbe@17: mgebbe@0: mgebbe@0: #### FACEBOOK STUFF #### mgebbe@8: if not (fbid?) or not (fbtoken?) or fbid is "" or fbtoken is "" mgebbe@0: console.log 'no facebook' mgebbe@0: else mgebbe@7: # get long-lived token mgebbe@7: # (token is callback function) mgebbe@7: # get the long lived token from facebook mgebbe@0: Facebook.getLongLivedToken fbtoken,(token) -> mgebbe@8: if (token?) mgebbe@8: saveUsermap(pumpid,fbid,token,fbtoken, (err, result) -> mgebbe@8: console.log 'fbsave.') mgebbe@8: mgebbe@7: if (fbdelete?) mgebbe@7: console.log "delete fb account" mgebbe@7: async.waterfall [ mgebbe@7: (callback) -> mgebbe@7: Usermap.search {user_pumpio: fbdelete}, callback mgebbe@7: (result, callback) -> mgebbe@7: _.each result, (um) -> mgebbe@7: if um.user_ESN.indexOf('@facebook') isnt -1 mgebbe@7: deleteUsermap(um.id,callback) mgebbe@7: return mgebbe@7: ], (err, result) -> mgebbe@0: mgebbe@7: if (twdelete?) mgebbe@7: console.log "delete tw account" mgebbe@7: async.waterfall [ mgebbe@7: (callback) -> mgebbe@7: Usermap.search {user_pumpio: twdelete}, callback mgebbe@7: (result, callback) -> mgebbe@7: _.each result, (um) -> mgebbe@7: if um.user_ESN.indexOf('@twitter') isnt -1 mgebbe@7: deleteUsermap(um.id,callback) mgebbe@7: return mgebbe@7: ], (err, result) -> mgebbe@0: mgebbe@0: # go back to main page mgebbe@0: #res.send JSON.stringify(req.body.pumpobj) mgebbe@0: #res.send 'bridge update for:'+ pumpid + '
--> ' + gpid + ' ' + gptoken + '
--> ' + fbid + ' ' + fbtoken mgebbe@0: res.redirect('/') mgebbe@0: mgebbe@0: # END mgebbe@0: return mgebbe@0: mgebbe@0: # Routes mgebbe@0: app.get "/bridge", userAuth, userRequired, getBridge mgebbe@0: app.post "/bridge", userAuth, userRequired, saveBridge mgebbe@3: Twitterroutes.addRoutes(app) mgebbe@0: mgebbe@0: return mgebbe@0: mgebbe@0: # update or create usermap mgebbe@0: saveUsermap = (pumpid,esnid,esntoken, extra, callback) -> mgebbe@0: saveMap = new Usermap( mgebbe@0: id : Usermap.key(pumpid,esnid) mgebbe@0: user_pumpio : pumpid mgebbe@0: user_ESN : esnid mgebbe@0: oauth_token : esntoken mgebbe@0: extra_token : extra mgebbe@0: created : Date.now() mgebbe@0: ) mgebbe@0: saveMap.save (cb) -> mgebbe@0: #console.log 'saved.' mgebbe@0: callback null, saveMap mgebbe@0: mgebbe@0: # only create and not update usermap mgebbe@0: createUsermap = (pumpid,esnid,esntoken) -> mgebbe@0: async.waterfall [(callback) -> mgebbe@0: Usermap.create mgebbe@0: user_pumpio: pumpid mgebbe@0: user_ESN: esnid mgebbe@0: oauth_token: esntoken mgebbe@0: , callback mgebbe@0: ], (err, result) -> mgebbe@0: if err instanceof databank.AlreadyExistsError mgebbe@0: console.log err mgebbe@0: return mgebbe@0: if err mgebbe@0: console.log err mgebbe@0: return mgebbe@0: else mgebbe@0: return mgebbe@0: mgebbe@0: # update or create a user mgebbe@0: updateUserDB = (id, name, displayName, profileLink, profilePicLink) -> mgebbe@0: async.waterfall [(callback) -> mgebbe@0: saveUser = new User() mgebbe@0: saveUser.id = id mgebbe@0: saveUser.name = name mgebbe@0: saveUser.displayName = displayName mgebbe@0: saveUser.profileLink = profileLink mgebbe@0: saveUser.profilePicLink = profilePicLink mgebbe@0: saveUser.created = Date.now() mgebbe@0: saveUser.save callback mgebbe@0: ], (err, result) -> mgebbe@0: return mgebbe@0: mgebbe@7: deleteUsermap = (id, callback) -> mgebbe@7: bank = Usermap.bank() mgebbe@7: bank.del Usermap.type, id, callback mgebbe@7: return mgebbe@7: mgebbe@0: exports.saveUsermap = saveUsermap mgebbe@0: exports.updateUserDB = updateUserDB mgebbe@0: exports.addRoutes = addRoutes