comparison 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
comparison
equal deleted inserted replaced
-1:000000000000 0:b73191efc65b
1 # Copyright (C) 2014 by Intevation GmbH
2 # Author: Mathias Gebbe <mgebbe@intevation.de>
3 #
4 # This file is Free Software under the Apache License, Version 2.0
5 # (the "License"); and comes with ABSOLUTELY NO WARRANTY!
6 # You may not use this file except in compliance with the License.
7 # See LICENSE for details.
8
9 async = require("async")
10 _ = require("underscore")
11 PumpIOClientApp = require("pump.io-client-app")
12 databank = require 'databank'
13 Databank = databank.Databank
14 DatabankObject = databank.DatabankObject
15 RequestToken = PumpIOClientApp.RequestToken
16 userAuth = PumpIOClientApp.userAuth
17 userOptional = PumpIOClientApp.userOptional
18 userRequired = PumpIOClientApp.userRequired
19 noUser = PumpIOClientApp.noUser
20 Usermap = require("./usermap")
21 Google = require("./google")
22 Facebook = require("./facebook")
23 Pump = require("./pumpio")
24 User = require("./user")
25 Edge = require("./edge")
26 Host = require("../node_modules/pump.io-client-app/lib/models/host")
27 https = require("https")
28 OAuth = require('oauth')
29 Sync = require('./sync')
30
31 addRoutes = (app, options) ->
32
33 getBridge = (req, res) ->
34 res.send 'Hello, i am the bridge you looking for!'
35 return
36
37 saveBridge = (req, res) ->
38
39 pumpid = req.body.pumpid
40 pumptoken = req.body.pumptoken
41 pumpsecret = req.body.pumpsecret
42
43 gpid = req.body.gpid
44 gptoken = req.body.gptoken
45
46 fbid = req.body.fbid
47 fbtoken = req.body.fbtoken
48
49 # create/update userdb entrys
50 # SET THE CONNECTION BETWEENS PUMPIO AND ESN-ACCOUNT (facebook or google)
51
52
53 #### PUMPIO STUFF ####
54 if pumpid is "" or typeof pumpid is "undefined"
55 console.log 'no pumpio'
56 else
57 saveUsermap(pumpid,pumpid,pumptoken+';'+pumpsecret,pumpsecret, (err, result) ->
58 Pump.getUser(result))
59
60 #async.waterfall [(callback) ->
61 # searchUsermap(bridgeid,bridgeid, callback)
62 #], (result) ->
63 # Pump.postUser(result, 'mgebbe@io.intevation.de','Hallo Welt')
64
65
66
67 #### GOOGLE STUFF ####
68 if gpid is "" or typeof gpid is "undefined" or gptoken is "" or typeof gptoken is "undefined"
69 console.log 'no google'
70 else
71 async.waterfall [
72 (callback) ->
73 saveUsermap(pumpid,gpid,gptoken,'', callback)
74 (user, callback) ->
75 Google.getRefreshToken(user,callback)
76 (token, callback) ->
77 saveUsermap(pumpid,gpid,token.access_token+";"+gptoken.substr(gptoken.indexOf(';')+1 ,gptoken.lenght),token.refresh_token,callback)
78 ], (err, result) ->
79 # now we have a gp refresh token
80 #console.log 'done'
81
82 #### FACEBOOK STUFF ####
83 if fbid is "" or typeof fbid is "undefined" or fbtoken is "" or typeof fbtoken is "undefined"
84 console.log 'no facebook'
85 else
86 # get long-lived token
87 # (token is callback function)
88 # get the long lived token from facebook
89 Facebook.getLongLivedToken fbtoken,(token) ->
90 saveUsermap(pumpid,fbid,token,fbtoken, (err, result) ->
91 console.log 'fbsave.')
92
93
94 # go back to main page
95 #res.send JSON.stringify(req.body.pumpobj)
96 #res.send 'bridge update for:'+ pumpid + '<br>--> ' + gpid + ' ' + gptoken + '<br>--> ' + fbid + ' ' + fbtoken
97 res.redirect('/')
98
99 # END
100 return
101
102 # Routes
103 app.get "/bridge", userAuth, userRequired, getBridge
104 app.post "/bridge", userAuth, userRequired, saveBridge
105
106 return
107
108 # update or create usermap
109 saveUsermap = (pumpid,esnid,esntoken, extra, callback) ->
110 saveMap = new Usermap(
111 id : Usermap.key(pumpid,esnid)
112 user_pumpio : pumpid
113 user_ESN : esnid
114 oauth_token : esntoken
115 extra_token : extra
116 created : Date.now()
117 )
118 saveMap.save (cb) ->
119 #console.log 'saved.'
120 callback null, saveMap
121
122 # only create and not update usermap
123 createUsermap = (pumpid,esnid,esntoken) ->
124 async.waterfall [(callback) ->
125 Usermap.create
126 user_pumpio: pumpid
127 user_ESN: esnid
128 oauth_token: esntoken
129 , callback
130 ], (err, result) ->
131 if err instanceof databank.AlreadyExistsError
132 console.log err
133 return
134 if err
135 console.log err
136 return
137 else
138 return
139
140 # update or create a user
141 updateUserDB = (id, name, displayName, profileLink, profilePicLink) ->
142 async.waterfall [(callback) ->
143 saveUser = new User()
144 saveUser.id = id
145 saveUser.name = name
146 saveUser.displayName = displayName
147 saveUser.profileLink = profileLink
148 saveUser.profilePicLink = profilePicLink
149 saveUser.created = Date.now()
150 saveUser.save callback
151 ], (err, result) ->
152 return
153
154 exports.saveUsermap = saveUsermap
155 exports.updateUserDB = updateUserDB
156 exports.addRoutes = addRoutes
This site is hosted by Intevation GmbH (Datenschutzerklärung und Impressum | Privacy Policy and Imprint)