view src/routes.coffee @ 0:b73191efc65b

Initial import of pumpbridge (bloody bloody alpha)
author Mathias Gebbe <mgebbe@intevation.de>
date Thu, 05 Jun 2014 10:35:15 +0200
parents
children 98a070c98982
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
# (the "License"); and comes with ABSOLUTELY NO WARRANTY!
# You may not use this file except in compliance with the License.
# See LICENSE 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")
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 pumpid is "" or typeof pumpid is "undefined"
      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 gpid is "" or typeof gpid is "undefined" or gptoken is "" or typeof gptoken is "undefined"
      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.lenght),token.refresh_token,callback)
      ], (err, result) ->
        # now we have a gp refresh token
        #console.log 'done'

    #### FACEBOOK STUFF ####
    if fbid is "" or typeof fbid is "undefined" or fbtoken is "" or typeof fbtoken is "undefined"
      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

  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
This site is hosted by Intevation GmbH (Datenschutzerklärung und Impressum | Privacy Policy and Imprint)