Mercurial > pumpbridge
view src/routes.coffee @ 5:af9f5b94c019
receive tweets
author | Mathias Gebbe <mgebbe@intevation.de> |
---|---|
date | Fri, 06 Jun 2014 14:42:28 +0200 |
parents | 98a070c98982 |
children | 2123f355ab68 |
line wrap: on
line source
# Copyright (C) 2014 by Intevation GmbH # Author: Mathias Gebbe <mgebbe@intevation.de> # # This file is Free Software under the Apache License, Version 2.0; # and comes with NO WARRANTY! # See the documentation coming with pumpbridge for details. async = require("async") _ = require("underscore") PumpIOClientApp = require("pump.io-client-app") databank = require 'databank' Databank = databank.Databank DatabankObject = databank.DatabankObject RequestToken = PumpIOClientApp.RequestToken userAuth = PumpIOClientApp.userAuth userOptional = PumpIOClientApp.userOptional userRequired = PumpIOClientApp.userRequired noUser = PumpIOClientApp.noUser Usermap = require("./usermap") Google = require("./google") Facebook = require("./facebook") Twitterroutes = require("./twitterroutes") Pump = require("./pumpio") User = require("./user") Edge = require("./edge") Host = require("../node_modules/pump.io-client-app/lib/models/host") https = require("https") OAuth = require('oauth') Sync = require('./sync') addRoutes = (app, options) -> getBridge = (req, res) -> res.send 'Hello, i am the bridge you looking for!' return saveBridge = (req, res) -> pumpid = req.body.pumpid pumptoken = req.body.pumptoken pumpsecret = req.body.pumpsecret gpid = req.body.gpid gptoken = req.body.gptoken fbid = req.body.fbid fbtoken = req.body.fbtoken # create/update userdb entrys # SET THE CONNECTION BETWEENS PUMPIO AND ESN-ACCOUNT (facebook or google) #### PUMPIO STUFF #### if not (pumpid?) console.log 'no pumpio' else saveUsermap(pumpid,pumpid,pumptoken+';'+pumpsecret,pumpsecret, (err, result) -> Pump.getUser(result)) #async.waterfall [(callback) -> # searchUsermap(bridgeid,bridgeid, callback) #], (result) -> # Pump.postUser(result, 'mgebbe@io.intevation.de','Hallo Welt') #### GOOGLE STUFF #### if not (gpid?) or not (gptoken?) console.log 'no google' else async.waterfall [ (callback) -> saveUsermap(pumpid,gpid,gptoken,'', callback) (user, callback) -> Google.getRefreshToken(user,callback) (token, callback) -> saveUsermap(pumpid,gpid,token.access_token + ";" + gptoken.substr(gptoken.indexOf(';')+1,gptoken.length),token.refresh_token,callback) ], (err, result) -> # now we have a gp refresh token #console.log 'done' #### FACEBOOK STUFF #### if not (fbid?) or not (fbtoken?) console.log 'no facebook' else # get long-lived token # (token is callback function) # get the long lived token from facebook Facebook.getLongLivedToken fbtoken,(token) -> saveUsermap(pumpid,fbid,token,fbtoken, (err, result) -> console.log 'fbsave.') # go back to main page #res.send JSON.stringify(req.body.pumpobj) #res.send 'bridge update for:'+ pumpid + '<br>--> ' + gpid + ' ' + gptoken + '<br>--> ' + fbid + ' ' + fbtoken res.redirect('/') # END return # Routes app.get "/bridge", userAuth, userRequired, getBridge app.post "/bridge", userAuth, userRequired, saveBridge Twitterroutes.addRoutes(app) return # update or create usermap saveUsermap = (pumpid,esnid,esntoken, extra, callback) -> saveMap = new Usermap( id : Usermap.key(pumpid,esnid) user_pumpio : pumpid user_ESN : esnid oauth_token : esntoken extra_token : extra created : Date.now() ) saveMap.save (cb) -> #console.log 'saved.' callback null, saveMap # only create and not update usermap createUsermap = (pumpid,esnid,esntoken) -> async.waterfall [(callback) -> Usermap.create user_pumpio: pumpid user_ESN: esnid oauth_token: esntoken , callback ], (err, result) -> if err instanceof databank.AlreadyExistsError console.log err return if err console.log err return else return # update or create a user updateUserDB = (id, name, displayName, profileLink, profilePicLink) -> async.waterfall [(callback) -> saveUser = new User() saveUser.id = id saveUser.name = name saveUser.displayName = displayName saveUser.profileLink = profileLink saveUser.profilePicLink = profilePicLink saveUser.created = Date.now() saveUser.save callback ], (err, result) -> return exports.saveUsermap = saveUsermap exports.updateUserDB = updateUserDB exports.addRoutes = addRoutes