annotate src/twitterroutes.coffee @ 17:a52b5b244e51

removed unnecessary underscore and replaced it with async
author Mathias Gebbe <mgebbe@intevation.de>
date Fri, 13 Jun 2014 15:18:36 +0200
parents 98a070c98982
children
rev   line source
3
98a070c98982 add Twitter support
Mathias Gebbe <mgebbe@intevation.de>
parents:
diff changeset
1 # Most of the routes in the application
98a070c98982 add Twitter support
Mathias Gebbe <mgebbe@intevation.de>
parents:
diff changeset
2 #
98a070c98982 add Twitter support
Mathias Gebbe <mgebbe@intevation.de>
parents:
diff changeset
3 # Copyright 2013, E14N (https://e14n.com/)
98a070c98982 add Twitter support
Mathias Gebbe <mgebbe@intevation.de>
parents:
diff changeset
4 # all changes Copyright 2014, Intevation GmbH (https://intevation.org)
98a070c98982 add Twitter support
Mathias Gebbe <mgebbe@intevation.de>
parents:
diff changeset
5 #
98a070c98982 add Twitter support
Mathias Gebbe <mgebbe@intevation.de>
parents:
diff changeset
6 # Licensed under the Apache License, Version 2.0 (the "License");
98a070c98982 add Twitter support
Mathias Gebbe <mgebbe@intevation.de>
parents:
diff changeset
7 # you may not use this file except in compliance with the License.
98a070c98982 add Twitter support
Mathias Gebbe <mgebbe@intevation.de>
parents:
diff changeset
8 # You may obtain a copy of the License at
98a070c98982 add Twitter support
Mathias Gebbe <mgebbe@intevation.de>
parents:
diff changeset
9 #
98a070c98982 add Twitter support
Mathias Gebbe <mgebbe@intevation.de>
parents:
diff changeset
10 # http://www.apache.org/licenses/LICENSE-2.0
98a070c98982 add Twitter support
Mathias Gebbe <mgebbe@intevation.de>
parents:
diff changeset
11 #
98a070c98982 add Twitter support
Mathias Gebbe <mgebbe@intevation.de>
parents:
diff changeset
12 # Unless required by applicable law or agreed to in writing, software
98a070c98982 add Twitter support
Mathias Gebbe <mgebbe@intevation.de>
parents:
diff changeset
13 # distributed under the License is distributed on an "AS IS" BASIS,
98a070c98982 add Twitter support
Mathias Gebbe <mgebbe@intevation.de>
parents:
diff changeset
14 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
98a070c98982 add Twitter support
Mathias Gebbe <mgebbe@intevation.de>
parents:
diff changeset
15 # See the License for the specific language governing permissions and
98a070c98982 add Twitter support
Mathias Gebbe <mgebbe@intevation.de>
parents:
diff changeset
16 # limitations under the License.
98a070c98982 add Twitter support
Mathias Gebbe <mgebbe@intevation.de>
parents:
diff changeset
17
98a070c98982 add Twitter support
Mathias Gebbe <mgebbe@intevation.de>
parents:
diff changeset
18 async = require("async")
98a070c98982 add Twitter support
Mathias Gebbe <mgebbe@intevation.de>
parents:
diff changeset
19 _ = require("underscore")
98a070c98982 add Twitter support
Mathias Gebbe <mgebbe@intevation.de>
parents:
diff changeset
20 PumpIOClientApp = require("pump.io-client-app")
98a070c98982 add Twitter support
Mathias Gebbe <mgebbe@intevation.de>
parents:
diff changeset
21 RequestToken = PumpIOClientApp.RequestToken
98a070c98982 add Twitter support
Mathias Gebbe <mgebbe@intevation.de>
parents:
diff changeset
22 userAuth = PumpIOClientApp.userAuth
98a070c98982 add Twitter support
Mathias Gebbe <mgebbe@intevation.de>
parents:
diff changeset
23 userOptional = PumpIOClientApp.userOptional
98a070c98982 add Twitter support
Mathias Gebbe <mgebbe@intevation.de>
parents:
diff changeset
24 userRequired = PumpIOClientApp.userRequired
98a070c98982 add Twitter support
Mathias Gebbe <mgebbe@intevation.de>
parents:
diff changeset
25 noUser = PumpIOClientApp.noUser
98a070c98982 add Twitter support
Mathias Gebbe <mgebbe@intevation.de>
parents:
diff changeset
26 Routes = require("./routes")
98a070c98982 add Twitter support
Mathias Gebbe <mgebbe@intevation.de>
parents:
diff changeset
27 Config = require("./config")
98a070c98982 add Twitter support
Mathias Gebbe <mgebbe@intevation.de>
parents:
diff changeset
28 config = Config.config
98a070c98982 add Twitter support
Mathias Gebbe <mgebbe@intevation.de>
parents:
diff changeset
29
98a070c98982 add Twitter support
Mathias Gebbe <mgebbe@intevation.de>
parents:
diff changeset
30 addRoutes = (app) ->
98a070c98982 add Twitter support
Mathias Gebbe <mgebbe@intevation.de>
parents:
diff changeset
31
98a070c98982 add Twitter support
Mathias Gebbe <mgebbe@intevation.de>
parents:
diff changeset
32 Twitter = require("./twitter")(config)
98a070c98982 add Twitter support
Mathias Gebbe <mgebbe@intevation.de>
parents:
diff changeset
33
98a070c98982 add Twitter support
Mathias Gebbe <mgebbe@intevation.de>
parents:
diff changeset
34 addAccount = (req, res, next) ->
98a070c98982 add Twitter support
Mathias Gebbe <mgebbe@intevation.de>
parents:
diff changeset
35 Twitter.getRequestToken req.site, (err, rt) ->
98a070c98982 add Twitter support
Mathias Gebbe <mgebbe@intevation.de>
parents:
diff changeset
36 if err
98a070c98982 add Twitter support
Mathias Gebbe <mgebbe@intevation.de>
parents:
diff changeset
37 next err
98a070c98982 add Twitter support
Mathias Gebbe <mgebbe@intevation.de>
parents:
diff changeset
38 else
98a070c98982 add Twitter support
Mathias Gebbe <mgebbe@intevation.de>
parents:
diff changeset
39 res.redirect Twitter.authorizeURL(rt)
98a070c98982 add Twitter support
Mathias Gebbe <mgebbe@intevation.de>
parents:
diff changeset
40 return
98a070c98982 add Twitter support
Mathias Gebbe <mgebbe@intevation.de>
parents:
diff changeset
41
98a070c98982 add Twitter support
Mathias Gebbe <mgebbe@intevation.de>
parents:
diff changeset
42 return
98a070c98982 add Twitter support
Mathias Gebbe <mgebbe@intevation.de>
parents:
diff changeset
43
98a070c98982 add Twitter support
Mathias Gebbe <mgebbe@intevation.de>
parents:
diff changeset
44 authorizedForTwitter = (req, res, next) ->
98a070c98982 add Twitter support
Mathias Gebbe <mgebbe@intevation.de>
parents:
diff changeset
45 hostname = "twitter.com"
98a070c98982 add Twitter support
Mathias Gebbe <mgebbe@intevation.de>
parents:
diff changeset
46 token = req.query.oauth_token
98a070c98982 add Twitter support
Mathias Gebbe <mgebbe@intevation.de>
parents:
diff changeset
47 verifier = req.query.oauth_verifier
98a070c98982 add Twitter support
Mathias Gebbe <mgebbe@intevation.de>
parents:
diff changeset
48 problem = req.query.oauth_problem
98a070c98982 add Twitter support
Mathias Gebbe <mgebbe@intevation.de>
parents:
diff changeset
49 user = req.user
98a070c98982 add Twitter support
Mathias Gebbe <mgebbe@intevation.de>
parents:
diff changeset
50 rt = undefined
98a070c98982 add Twitter support
Mathias Gebbe <mgebbe@intevation.de>
parents:
diff changeset
51 fuser = undefined
98a070c98982 add Twitter support
Mathias Gebbe <mgebbe@intevation.de>
parents:
diff changeset
52 access_token = undefined
98a070c98982 add Twitter support
Mathias Gebbe <mgebbe@intevation.de>
parents:
diff changeset
53 token_secret = undefined
98a070c98982 add Twitter support
Mathias Gebbe <mgebbe@intevation.de>
parents:
diff changeset
54 id = undefined
98a070c98982 add Twitter support
Mathias Gebbe <mgebbe@intevation.de>
parents:
diff changeset
55 object = undefined
98a070c98982 add Twitter support
Mathias Gebbe <mgebbe@intevation.de>
parents:
diff changeset
56 newUser = false
98a070c98982 add Twitter support
Mathias Gebbe <mgebbe@intevation.de>
parents:
diff changeset
57 unless token
98a070c98982 add Twitter support
Mathias Gebbe <mgebbe@intevation.de>
parents:
diff changeset
58 next new Error("No token returned.")
98a070c98982 add Twitter support
Mathias Gebbe <mgebbe@intevation.de>
parents:
diff changeset
59 return
98a070c98982 add Twitter support
Mathias Gebbe <mgebbe@intevation.de>
parents:
diff changeset
60 async.waterfall [
98a070c98982 add Twitter support
Mathias Gebbe <mgebbe@intevation.de>
parents:
diff changeset
61 (callback) ->
98a070c98982 add Twitter support
Mathias Gebbe <mgebbe@intevation.de>
parents:
diff changeset
62 RequestToken.get RequestToken.key(hostname, token), callback
98a070c98982 add Twitter support
Mathias Gebbe <mgebbe@intevation.de>
parents:
diff changeset
63 (results, callback) ->
98a070c98982 add Twitter support
Mathias Gebbe <mgebbe@intevation.de>
parents:
diff changeset
64 rt = results
98a070c98982 add Twitter support
Mathias Gebbe <mgebbe@intevation.de>
parents:
diff changeset
65 Twitter.getAccessToken req.site, rt, verifier, callback
98a070c98982 add Twitter support
Mathias Gebbe <mgebbe@intevation.de>
parents:
diff changeset
66 (token, secret, extra, callback) ->
98a070c98982 add Twitter support
Mathias Gebbe <mgebbe@intevation.de>
parents:
diff changeset
67 access_token = token
98a070c98982 add Twitter support
Mathias Gebbe <mgebbe@intevation.de>
parents:
diff changeset
68 token_secret = secret
98a070c98982 add Twitter support
Mathias Gebbe <mgebbe@intevation.de>
parents:
diff changeset
69 async.parallel [
98a070c98982 add Twitter support
Mathias Gebbe <mgebbe@intevation.de>
parents:
diff changeset
70 (callback) ->
98a070c98982 add Twitter support
Mathias Gebbe <mgebbe@intevation.de>
parents:
diff changeset
71 rt.del callback
98a070c98982 add Twitter support
Mathias Gebbe <mgebbe@intevation.de>
parents:
diff changeset
72 (callback) ->
98a070c98982 add Twitter support
Mathias Gebbe <mgebbe@intevation.de>
parents:
diff changeset
73 Twitter.whoami req.site, access_token, token_secret, callback
98a070c98982 add Twitter support
Mathias Gebbe <mgebbe@intevation.de>
parents:
diff changeset
74 ], callback
98a070c98982 add Twitter support
Mathias Gebbe <mgebbe@intevation.de>
parents:
diff changeset
75 (results, callback) ->
98a070c98982 add Twitter support
Mathias Gebbe <mgebbe@intevation.de>
parents:
diff changeset
76 object = results[1]
98a070c98982 add Twitter support
Mathias Gebbe <mgebbe@intevation.de>
parents:
diff changeset
77 object = JSON.stringify(object)
98a070c98982 add Twitter support
Mathias Gebbe <mgebbe@intevation.de>
parents:
diff changeset
78 object = JSON.parse(object)
98a070c98982 add Twitter support
Mathias Gebbe <mgebbe@intevation.de>
parents:
diff changeset
79 res.clearCookie('twitteruser')
98a070c98982 add Twitter support
Mathias Gebbe <mgebbe@intevation.de>
parents:
diff changeset
80 res.clearCookie('twitterid')
98a070c98982 add Twitter support
Mathias Gebbe <mgebbe@intevation.de>
parents:
diff changeset
81 res.cookie('twitterid',object.id, { maxAge: 900000, httpOnly: false })
98a070c98982 add Twitter support
Mathias Gebbe <mgebbe@intevation.de>
parents:
diff changeset
82 res.cookie('twitteruser',object.screen_name, { maxAge: 900000, httpOnly: false })
98a070c98982 add Twitter support
Mathias Gebbe <mgebbe@intevation.de>
parents:
diff changeset
83 Routes.saveUsermap(user.id ,object.id + "@twitter" ,access_token, token_secret, callback)
98a070c98982 add Twitter support
Mathias Gebbe <mgebbe@intevation.de>
parents:
diff changeset
84 ], (err) ->
98a070c98982 add Twitter support
Mathias Gebbe <mgebbe@intevation.de>
parents:
diff changeset
85 if err
98a070c98982 add Twitter support
Mathias Gebbe <mgebbe@intevation.de>
parents:
diff changeset
86 next err
98a070c98982 add Twitter support
Mathias Gebbe <mgebbe@intevation.de>
parents:
diff changeset
87 else
98a070c98982 add Twitter support
Mathias Gebbe <mgebbe@intevation.de>
parents:
diff changeset
88 res.redirect "/"
98a070c98982 add Twitter support
Mathias Gebbe <mgebbe@intevation.de>
parents:
diff changeset
89 return
98a070c98982 add Twitter support
Mathias Gebbe <mgebbe@intevation.de>
parents:
diff changeset
90
98a070c98982 add Twitter support
Mathias Gebbe <mgebbe@intevation.de>
parents:
diff changeset
91 return
17
a52b5b244e51 removed unnecessary underscore and replaced it with async
Mathias Gebbe <mgebbe@intevation.de>
parents: 3
diff changeset
92
3
98a070c98982 add Twitter support
Mathias Gebbe <mgebbe@intevation.de>
parents:
diff changeset
93 # Routes
98a070c98982 add Twitter support
Mathias Gebbe <mgebbe@intevation.de>
parents:
diff changeset
94 console.log "Initializing Twitter routes"
98a070c98982 add Twitter support
Mathias Gebbe <mgebbe@intevation.de>
parents:
diff changeset
95 app.get "/add-account", userAuth, userRequired, addAccount
98a070c98982 add Twitter support
Mathias Gebbe <mgebbe@intevation.de>
parents:
diff changeset
96 app.get "/authorized-for-twitter", userAuth, userRequired, authorizedForTwitter
98a070c98982 add Twitter support
Mathias Gebbe <mgebbe@intevation.de>
parents:
diff changeset
97 return
98a070c98982 add Twitter support
Mathias Gebbe <mgebbe@intevation.de>
parents:
diff changeset
98
98a070c98982 add Twitter support
Mathias Gebbe <mgebbe@intevation.de>
parents:
diff changeset
99 exports.addRoutes = addRoutes
This site is hosted by Intevation GmbH (Datenschutzerklärung und Impressum | Privacy Policy and Imprint)