annotate src/twitter.coffee @ 3:98a070c98982

add Twitter support
author Mathias Gebbe <mgebbe@intevation.de>
date Thu, 05 Jun 2014 18:02:25 +0200
parents
children f352c74a6a5b
rev   line source
3
98a070c98982 add Twitter support
Mathias Gebbe <mgebbe@intevation.de>
parents:
diff changeset
1 # twitter.js
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 # data object representing twitter.com
98a070c98982 add Twitter support
Mathias Gebbe <mgebbe@intevation.de>
parents:
diff changeset
4 #
98a070c98982 add Twitter support
Mathias Gebbe <mgebbe@intevation.de>
parents:
diff changeset
5 # Copyright 2013, E14N (https://e14n.com/)
98a070c98982 add Twitter support
Mathias Gebbe <mgebbe@intevation.de>
parents:
diff changeset
6 # all changes Copyright 2014, Intevation GmbH (https://intevation.org)
98a070c98982 add Twitter support
Mathias Gebbe <mgebbe@intevation.de>
parents:
diff changeset
7 #
98a070c98982 add Twitter support
Mathias Gebbe <mgebbe@intevation.de>
parents:
diff changeset
8 # Licensed under the Apache License, Version 2.0 (the "License");
98a070c98982 add Twitter support
Mathias Gebbe <mgebbe@intevation.de>
parents:
diff changeset
9 # you may not use this file except in compliance with the License.
98a070c98982 add Twitter support
Mathias Gebbe <mgebbe@intevation.de>
parents:
diff changeset
10 # You may obtain a copy of the License at
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 # http://www.apache.org/licenses/LICENSE-2.0
98a070c98982 add Twitter support
Mathias Gebbe <mgebbe@intevation.de>
parents:
diff changeset
13 #
98a070c98982 add Twitter support
Mathias Gebbe <mgebbe@intevation.de>
parents:
diff changeset
14 # Unless required by applicable law or agreed to in writing, software
98a070c98982 add Twitter support
Mathias Gebbe <mgebbe@intevation.de>
parents:
diff changeset
15 # distributed under the License is distributed on an "AS IS" BASIS,
98a070c98982 add Twitter support
Mathias Gebbe <mgebbe@intevation.de>
parents:
diff changeset
16 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
98a070c98982 add Twitter support
Mathias Gebbe <mgebbe@intevation.de>
parents:
diff changeset
17 # See the License for the specific language governing permissions and
98a070c98982 add Twitter support
Mathias Gebbe <mgebbe@intevation.de>
parents:
diff changeset
18 # limitations under the License.
98a070c98982 add Twitter support
Mathias Gebbe <mgebbe@intevation.de>
parents:
diff changeset
19
98a070c98982 add Twitter support
Mathias Gebbe <mgebbe@intevation.de>
parents:
diff changeset
20 _ = require("underscore")
98a070c98982 add Twitter support
Mathias Gebbe <mgebbe@intevation.de>
parents:
diff changeset
21 async = require("async")
98a070c98982 add Twitter support
Mathias Gebbe <mgebbe@intevation.de>
parents:
diff changeset
22 OAuth = require("oauth").OAuth
98a070c98982 add Twitter support
Mathias Gebbe <mgebbe@intevation.de>
parents:
diff changeset
23 PumpIOClientApp = require("pump.io-client-app")
98a070c98982 add Twitter support
Mathias Gebbe <mgebbe@intevation.de>
parents:
diff changeset
24 DatabankObject = require("databank").DatabankObject
98a070c98982 add Twitter support
Mathias Gebbe <mgebbe@intevation.de>
parents:
diff changeset
25 RequestToken = PumpIOClientApp.RequestToken
98a070c98982 add Twitter support
Mathias Gebbe <mgebbe@intevation.de>
parents:
diff changeset
26 Usermap = require("./usermap")
98a070c98982 add Twitter support
Mathias Gebbe <mgebbe@intevation.de>
parents:
diff changeset
27 Pump = require("./pumpio")
98a070c98982 add Twitter support
Mathias Gebbe <mgebbe@intevation.de>
parents:
diff changeset
28 ToESN = require("./toESN")
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
98a070c98982 add Twitter support
Mathias Gebbe <mgebbe@intevation.de>
parents:
diff changeset
31 module.exports = (config) ->
98a070c98982 add Twitter support
Mathias Gebbe <mgebbe@intevation.de>
parents:
diff changeset
32 client_id = config.twclient_id
98a070c98982 add Twitter support
Mathias Gebbe <mgebbe@intevation.de>
parents:
diff changeset
33 client_secret = config.twclient_secret
98a070c98982 add Twitter support
Mathias Gebbe <mgebbe@intevation.de>
parents:
diff changeset
34 request_token_endpoint = "https://api.twitter.com/oauth/request_token"
98a070c98982 add Twitter support
Mathias Gebbe <mgebbe@intevation.de>
parents:
diff changeset
35 access_token_endpoint = "https://api.twitter.com/oauth/access_token"
98a070c98982 add Twitter support
Mathias Gebbe <mgebbe@intevation.de>
parents:
diff changeset
36 authorization_endpoint = "https://api.twitter.com/oauth/authorize"
98a070c98982 add Twitter support
Mathias Gebbe <mgebbe@intevation.de>
parents:
diff changeset
37 whoami_endpoint = "https://api.twitter.com/1.1/account/verify_credentials.json"
98a070c98982 add Twitter support
Mathias Gebbe <mgebbe@intevation.de>
parents:
diff changeset
38 hostname = "twitter.com"
98a070c98982 add Twitter support
Mathias Gebbe <mgebbe@intevation.de>
parents:
diff changeset
39
98a070c98982 add Twitter support
Mathias Gebbe <mgebbe@intevation.de>
parents:
diff changeset
40 Twitter =
98a070c98982 add Twitter support
Mathias Gebbe <mgebbe@intevation.de>
parents:
diff changeset
41 getRequestToken: (site, callback) ->
98a070c98982 add Twitter support
Mathias Gebbe <mgebbe@intevation.de>
parents:
diff changeset
42 oa = Twitter.getOAuth(site)
98a070c98982 add Twitter support
Mathias Gebbe <mgebbe@intevation.de>
parents:
diff changeset
43 async.waterfall [
98a070c98982 add Twitter support
Mathias Gebbe <mgebbe@intevation.de>
parents:
diff changeset
44 (callback) ->
98a070c98982 add Twitter support
Mathias Gebbe <mgebbe@intevation.de>
parents:
diff changeset
45 oa.getOAuthRequestToken callback
98a070c98982 add Twitter support
Mathias Gebbe <mgebbe@intevation.de>
parents:
diff changeset
46 (token, secret, other, callback) ->
98a070c98982 add Twitter support
Mathias Gebbe <mgebbe@intevation.de>
parents:
diff changeset
47 RequestToken.create
98a070c98982 add Twitter support
Mathias Gebbe <mgebbe@intevation.de>
parents:
diff changeset
48 token: token
98a070c98982 add Twitter support
Mathias Gebbe <mgebbe@intevation.de>
parents:
diff changeset
49 secret: secret
98a070c98982 add Twitter support
Mathias Gebbe <mgebbe@intevation.de>
parents:
diff changeset
50 hostname: hostname
98a070c98982 add Twitter support
Mathias Gebbe <mgebbe@intevation.de>
parents:
diff changeset
51 , callback
98a070c98982 add Twitter support
Mathias Gebbe <mgebbe@intevation.de>
parents:
diff changeset
52 ], callback
98a070c98982 add Twitter support
Mathias Gebbe <mgebbe@intevation.de>
parents:
diff changeset
53 return
98a070c98982 add Twitter support
Mathias Gebbe <mgebbe@intevation.de>
parents:
diff changeset
54
98a070c98982 add Twitter support
Mathias Gebbe <mgebbe@intevation.de>
parents:
diff changeset
55 authorizeURL: (rt) ->
98a070c98982 add Twitter support
Mathias Gebbe <mgebbe@intevation.de>
parents:
diff changeset
56 separator = undefined
98a070c98982 add Twitter support
Mathias Gebbe <mgebbe@intevation.de>
parents:
diff changeset
57 if _.contains(authorization_endpoint, "?")
98a070c98982 add Twitter support
Mathias Gebbe <mgebbe@intevation.de>
parents:
diff changeset
58 separator = "&"
98a070c98982 add Twitter support
Mathias Gebbe <mgebbe@intevation.de>
parents:
diff changeset
59 else
98a070c98982 add Twitter support
Mathias Gebbe <mgebbe@intevation.de>
parents:
diff changeset
60 separator = "?"
98a070c98982 add Twitter support
Mathias Gebbe <mgebbe@intevation.de>
parents:
diff changeset
61 authorization_endpoint + separator + "oauth_token=" + rt.token
98a070c98982 add Twitter support
Mathias Gebbe <mgebbe@intevation.de>
parents:
diff changeset
62
98a070c98982 add Twitter support
Mathias Gebbe <mgebbe@intevation.de>
parents:
diff changeset
63 getAccessToken: (site, rt, verifier, callback) ->
98a070c98982 add Twitter support
Mathias Gebbe <mgebbe@intevation.de>
parents:
diff changeset
64 oa = Twitter.getOAuth(site)
98a070c98982 add Twitter support
Mathias Gebbe <mgebbe@intevation.de>
parents:
diff changeset
65 oa.getOAuthAccessToken rt.token, rt.secret, verifier, callback
98a070c98982 add Twitter support
Mathias Gebbe <mgebbe@intevation.de>
parents:
diff changeset
66 return
98a070c98982 add Twitter support
Mathias Gebbe <mgebbe@intevation.de>
parents:
diff changeset
67
98a070c98982 add Twitter support
Mathias Gebbe <mgebbe@intevation.de>
parents:
diff changeset
68 whoami: (site, token, secret, callback) ->
98a070c98982 add Twitter support
Mathias Gebbe <mgebbe@intevation.de>
parents:
diff changeset
69 oa = Twitter.getOAuth(site)
98a070c98982 add Twitter support
Mathias Gebbe <mgebbe@intevation.de>
parents:
diff changeset
70 async.waterfall [(callback) ->
98a070c98982 add Twitter support
Mathias Gebbe <mgebbe@intevation.de>
parents:
diff changeset
71 oa.get whoami_endpoint, token, secret, callback
98a070c98982 add Twitter support
Mathias Gebbe <mgebbe@intevation.de>
parents:
diff changeset
72 return
98a070c98982 add Twitter support
Mathias Gebbe <mgebbe@intevation.de>
parents:
diff changeset
73 ], (err, doc, response) ->
98a070c98982 add Twitter support
Mathias Gebbe <mgebbe@intevation.de>
parents:
diff changeset
74 obj = undefined
98a070c98982 add Twitter support
Mathias Gebbe <mgebbe@intevation.de>
parents:
diff changeset
75 if err
98a070c98982 add Twitter support
Mathias Gebbe <mgebbe@intevation.de>
parents:
diff changeset
76 callback err, null
98a070c98982 add Twitter support
Mathias Gebbe <mgebbe@intevation.de>
parents:
diff changeset
77 else
98a070c98982 add Twitter support
Mathias Gebbe <mgebbe@intevation.de>
parents:
diff changeset
78 try
98a070c98982 add Twitter support
Mathias Gebbe <mgebbe@intevation.de>
parents:
diff changeset
79 obj = JSON.parse(doc)
98a070c98982 add Twitter support
Mathias Gebbe <mgebbe@intevation.de>
parents:
diff changeset
80 callback null, obj
98a070c98982 add Twitter support
Mathias Gebbe <mgebbe@intevation.de>
parents:
diff changeset
81 catch e
98a070c98982 add Twitter support
Mathias Gebbe <mgebbe@intevation.de>
parents:
diff changeset
82 callback e, null
98a070c98982 add Twitter support
Mathias Gebbe <mgebbe@intevation.de>
parents:
diff changeset
83 return
98a070c98982 add Twitter support
Mathias Gebbe <mgebbe@intevation.de>
parents:
diff changeset
84
98a070c98982 add Twitter support
Mathias Gebbe <mgebbe@intevation.de>
parents:
diff changeset
85 return
98a070c98982 add Twitter support
Mathias Gebbe <mgebbe@intevation.de>
parents:
diff changeset
86
98a070c98982 add Twitter support
Mathias Gebbe <mgebbe@intevation.de>
parents:
diff changeset
87 sync: (user) ->
98a070c98982 add Twitter support
Mathias Gebbe <mgebbe@intevation.de>
parents:
diff changeset
88 me = user.user_pumpio
98a070c98982 add Twitter support
Mathias Gebbe <mgebbe@intevation.de>
parents:
diff changeset
89 id = user.user_ESN.substr(0,user.user_ESN.indexOf('@'))
98a070c98982 add Twitter support
Mathias Gebbe <mgebbe@intevation.de>
parents:
diff changeset
90 token = user.oauth_token
98a070c98982 add Twitter support
Mathias Gebbe <mgebbe@intevation.de>
parents:
diff changeset
91 secret = user.extra_token
98a070c98982 add Twitter support
Mathias Gebbe <mgebbe@intevation.de>
parents:
diff changeset
92 util = require("util")
98a070c98982 add Twitter support
Mathias Gebbe <mgebbe@intevation.de>
parents:
diff changeset
93 twitter = require("twitter")
98a070c98982 add Twitter support
Mathias Gebbe <mgebbe@intevation.de>
parents:
diff changeset
94 twit = new twitter(
98a070c98982 add Twitter support
Mathias Gebbe <mgebbe@intevation.de>
parents:
diff changeset
95 consumer_key: client_id
98a070c98982 add Twitter support
Mathias Gebbe <mgebbe@intevation.de>
parents:
diff changeset
96 consumer_secret: client_secret
98a070c98982 add Twitter support
Mathias Gebbe <mgebbe@intevation.de>
parents:
diff changeset
97 access_token_key: token
98a070c98982 add Twitter support
Mathias Gebbe <mgebbe@intevation.de>
parents:
diff changeset
98 access_token_secret: secret
98a070c98982 add Twitter support
Mathias Gebbe <mgebbe@intevation.de>
parents:
diff changeset
99 )
98a070c98982 add Twitter support
Mathias Gebbe <mgebbe@intevation.de>
parents:
diff changeset
100
98a070c98982 add Twitter support
Mathias Gebbe <mgebbe@intevation.de>
parents:
diff changeset
101 #twit.verifyCredentials((data) ->
98a070c98982 add Twitter support
Mathias Gebbe <mgebbe@intevation.de>
parents:
diff changeset
102 # console.log util.inspect(data)
98a070c98982 add Twitter support
Mathias Gebbe <mgebbe@intevation.de>
parents:
diff changeset
103 # return
98a070c98982 add Twitter support
Mathias Gebbe <mgebbe@intevation.de>
parents:
diff changeset
104 #).updateStatus "Test tweet from pumpbridge using " + twitter.VERSION, (data) ->
98a070c98982 add Twitter support
Mathias Gebbe <mgebbe@intevation.de>
parents:
diff changeset
105 # console.log util.inspect(data)
98a070c98982 add Twitter support
Mathias Gebbe <mgebbe@intevation.de>
parents:
diff changeset
106 # return
98a070c98982 add Twitter support
Mathias Gebbe <mgebbe@intevation.de>
parents:
diff changeset
107
98a070c98982 add Twitter support
Mathias Gebbe <mgebbe@intevation.de>
parents:
diff changeset
108 # GET PUBLIC PUMP POSTS AND POST THEM
98a070c98982 add Twitter support
Mathias Gebbe <mgebbe@intevation.de>
parents:
diff changeset
109 async.waterfall [
98a070c98982 add Twitter support
Mathias Gebbe <mgebbe@intevation.de>
parents:
diff changeset
110 (callback) ->
98a070c98982 add Twitter support
Mathias Gebbe <mgebbe@intevation.de>
parents:
diff changeset
111 Usermap.search {id: me + '_to_' + me}, callback
98a070c98982 add Twitter support
Mathias Gebbe <mgebbe@intevation.de>
parents:
diff changeset
112 (user, callback) ->
98a070c98982 add Twitter support
Mathias Gebbe <mgebbe@intevation.de>
parents:
diff changeset
113 Pump.getUserFeed(user[0],callback)
98a070c98982 add Twitter support
Mathias Gebbe <mgebbe@intevation.de>
parents:
diff changeset
114 (feed, callback) ->
98a070c98982 add Twitter support
Mathias Gebbe <mgebbe@intevation.de>
parents:
diff changeset
115 async.eachSeries feed.items, ((post, callback) ->
98a070c98982 add Twitter support
Mathias Gebbe <mgebbe@intevation.de>
parents:
diff changeset
116 # do for each post
98a070c98982 add Twitter support
Mathias Gebbe <mgebbe@intevation.de>
parents:
diff changeset
117 if (post.verb is "post" or post.verb is "share") and (post.object.objectType is "note" or post.object.objectType is "image") and (Pump.isPublicActivity(post)) and (typeof post.object.deleted is "undefined")
98a070c98982 add Twitter support
Mathias Gebbe <mgebbe@intevation.de>
parents:
diff changeset
118 ToESN.search {uid: post.object.id + "@twitter"}, (err, result) ->
98a070c98982 add Twitter support
Mathias Gebbe <mgebbe@intevation.de>
parents:
diff changeset
119 if result.length is 0
98a070c98982 add Twitter support
Mathias Gebbe <mgebbe@intevation.de>
parents:
diff changeset
120 text = post.object.content.replace(/<(?:.|\n)*?>/gm, '')
98a070c98982 add Twitter support
Mathias Gebbe <mgebbe@intevation.de>
parents:
diff changeset
121 status = ""
98a070c98982 add Twitter support
Mathias Gebbe <mgebbe@intevation.de>
parents:
diff changeset
122 if text.length <= 140
98a070c98982 add Twitter support
Mathias Gebbe <mgebbe@intevation.de>
parents:
diff changeset
123 status = text
98a070c98982 add Twitter support
Mathias Gebbe <mgebbe@intevation.de>
parents:
diff changeset
124 else
98a070c98982 add Twitter support
Mathias Gebbe <mgebbe@intevation.de>
parents:
diff changeset
125 status = text.substr(0, 140 - (post.object.url.length + 2)) + ".." + post.object.url
98a070c98982 add Twitter support
Mathias Gebbe <mgebbe@intevation.de>
parents:
diff changeset
126 twit.verifyCredentials((data) ->
98a070c98982 add Twitter support
Mathias Gebbe <mgebbe@intevation.de>
parents:
diff changeset
127 #console.log util.inspect(data)
98a070c98982 add Twitter support
Mathias Gebbe <mgebbe@intevation.de>
parents:
diff changeset
128 return
98a070c98982 add Twitter support
Mathias Gebbe <mgebbe@intevation.de>
parents:
diff changeset
129 ).updateStatus status, (data) ->
98a070c98982 add Twitter support
Mathias Gebbe <mgebbe@intevation.de>
parents:
diff changeset
130 async.waterfall [
98a070c98982 add Twitter support
Mathias Gebbe <mgebbe@intevation.de>
parents:
diff changeset
131 (callback) ->
98a070c98982 add Twitter support
Mathias Gebbe <mgebbe@intevation.de>
parents:
diff changeset
132 savePost = new ToESN()
98a070c98982 add Twitter support
Mathias Gebbe <mgebbe@intevation.de>
parents:
diff changeset
133 savePost.uid = post.object.id + "@twitter"
98a070c98982 add Twitter support
Mathias Gebbe <mgebbe@intevation.de>
parents:
diff changeset
134 savePost.sourceUser = post.actor.id
98a070c98982 add Twitter support
Mathias Gebbe <mgebbe@intevation.de>
parents:
diff changeset
135 savePost.sourcePost = post.object.id
98a070c98982 add Twitter support
Mathias Gebbe <mgebbe@intevation.de>
parents:
diff changeset
136 savePost.targetUser = user.user_ESN
98a070c98982 add Twitter support
Mathias Gebbe <mgebbe@intevation.de>
parents:
diff changeset
137 savePost.targetPost = data.id
98a070c98982 add Twitter support
Mathias Gebbe <mgebbe@intevation.de>
parents:
diff changeset
138 savePost.recipientUser = 'public'
98a070c98982 add Twitter support
Mathias Gebbe <mgebbe@intevation.de>
parents:
diff changeset
139 savePost.updated = Date.now()
98a070c98982 add Twitter support
Mathias Gebbe <mgebbe@intevation.de>
parents:
diff changeset
140 savePost.save callback
98a070c98982 add Twitter support
Mathias Gebbe <mgebbe@intevation.de>
parents:
diff changeset
141 ], (err, result) ->
98a070c98982 add Twitter support
Mathias Gebbe <mgebbe@intevation.de>
parents:
diff changeset
142 #console.log util.inspect(data)
98a070c98982 add Twitter support
Mathias Gebbe <mgebbe@intevation.de>
parents:
diff changeset
143 return
98a070c98982 add Twitter support
Mathias Gebbe <mgebbe@intevation.de>
parents:
diff changeset
144 callback null, 'done'
98a070c98982 add Twitter support
Mathias Gebbe <mgebbe@intevation.de>
parents:
diff changeset
145 ), (err) ->
98a070c98982 add Twitter support
Mathias Gebbe <mgebbe@intevation.de>
parents:
diff changeset
146 callback null, 'done'
98a070c98982 add Twitter support
Mathias Gebbe <mgebbe@intevation.de>
parents:
diff changeset
147 ],(err, result) ->
98a070c98982 add Twitter support
Mathias Gebbe <mgebbe@intevation.de>
parents:
diff changeset
148 #console.log 'done.'
98a070c98982 add Twitter support
Mathias Gebbe <mgebbe@intevation.de>
parents:
diff changeset
149
98a070c98982 add Twitter support
Mathias Gebbe <mgebbe@intevation.de>
parents:
diff changeset
150 return
98a070c98982 add Twitter support
Mathias Gebbe <mgebbe@intevation.de>
parents:
diff changeset
151
98a070c98982 add Twitter support
Mathias Gebbe <mgebbe@intevation.de>
parents:
diff changeset
152 getOAuth: (site) ->
98a070c98982 add Twitter support
Mathias Gebbe <mgebbe@intevation.de>
parents:
diff changeset
153 new OAuth(request_token_endpoint, access_token_endpoint, client_id, client_secret, "1.0", site.url("/authorized-for-twitter"), "HMAC-SHA1", null, # nonce size; use default
98a070c98982 add Twitter support
Mathias Gebbe <mgebbe@intevation.de>
parents:
diff changeset
154 "User-Agent": site.userAgent()
98a070c98982 add Twitter support
Mathias Gebbe <mgebbe@intevation.de>
parents:
diff changeset
155 )
98a070c98982 add Twitter support
Mathias Gebbe <mgebbe@intevation.de>
parents:
diff changeset
156
98a070c98982 add Twitter support
Mathias Gebbe <mgebbe@intevation.de>
parents:
diff changeset
157 Twitter
This site is hosted by Intevation GmbH (Datenschutzerklärung und Impressum | Privacy Policy and Imprint)