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