Mercurial > pumpbridge
comparison src/facebook.coffee @ 7:2123f355ab68
go back interval or 15 min, delete credentials, layout
author | Mathias Gebbe <mgebbe@intevation.de> |
---|---|
date | Wed, 11 Jun 2014 02:32:53 +0200 |
parents | f352c74a6a5b |
children | 6fd0a307850f |
comparison
equal
deleted
inserted
replaced
6:a94ac7c3b182 | 7:2123f355ab68 |
---|---|
44 getFriends(user) | 44 getFriends(user) |
45 getPages(user) | 45 getPages(user) |
46 getUser(user) | 46 getUser(user) |
47 getStream user,callback | 47 getStream user,callback |
48 ], (err, posts) -> | 48 ], (err, posts) -> |
49 return if not (posts?) | 49 return if not (posts?) or posts.length is 0 |
50 #console.log JSON.stringify(posts) # all posts from network (100) | 50 #console.log JSON.stringify(posts) # all posts from network (100) |
51 async.eachSeries posts, ((post, callback) -> | 51 async.eachSeries posts, ((post, callback) -> |
52 | 52 |
53 async.waterfall [ | 53 async.waterfall [ |
54 (callback) -> | 54 (callback) -> |
55 FromESN.search {uid: post.id + "@facebook_to_" + me, recipientUser: me}, callback | 55 FromESN.search {uid: post.id + "@facebook_to_" + me, recipientUser: me}, callback |
56 (result, callback) -> | 56 (result, callback) -> |
57 return if result.length isnt 0 or post.from.id == id | 57 return if result.length isnt 0 or post.from.id is id |
58 #console.log "postid: " + post.from.id + "id:" + id | 58 #console.log "postid: " + post.from.id + "id:" + id |
59 # if this is your own post return!!! | 59 # if this is your own post return!!! |
60 getPublicUserInfo(post.from.id , callback) | 60 getPublicUserInfo(post.from.id , callback) |
61 (pubuser, callback) -> | 61 (pubuser, callback) -> |
62 pubuser = pubuser | 62 pubuser = pubuser |
65 Sync.postParser post, dbuser[0], 'facebook', callback | 65 Sync.postParser post, dbuser[0], 'facebook', callback |
66 (parsed, callback) -> | 66 (parsed, callback) -> |
67 Pump.postUser bridgeid, me, parsed, callback | 67 Pump.postUser bridgeid, me, parsed, callback |
68 (pumppost, callback) -> | 68 (pumppost, callback) -> |
69 pumppost = JSON.parse(pumppost) | 69 pumppost = JSON.parse(pumppost) |
70 if (post.actions[1].link?) | |
71 postlink = post.actions[1].link | |
72 else | |
73 postlink = post.actions[0].link | |
70 FromESN.create | 74 FromESN.create |
71 postid: post.id + "@facebook" | 75 postid: post.id + "@facebook" |
72 sourceUser: post.from.id | 76 sourceUser: post.from.id |
73 sourcePost: post.actions[0].link | 77 sourcePost: postlink |
74 pumpPost: pumppost.object.id | 78 pumpPost: pumppost.object.id |
75 recipientUser: me | 79 recipientUser: me |
76 created: Date.now() | 80 created: Date.now() |
77 , callback | 81 , callback |
78 ], (err, result) -> | 82 ], (err, result) -> |
131 (callback) -> | 135 (callback) -> |
132 Usermap.search {id: me + '_to_' + me}, callback | 136 Usermap.search {id: me + '_to_' + me}, callback |
133 (user, callback) -> | 137 (user, callback) -> |
134 Pump.getUserFeed(user[0],callback) | 138 Pump.getUserFeed(user[0],callback) |
135 (feed, callback) -> | 139 (feed, callback) -> |
140 interval = config.interval | |
141 if not (interval?) | |
142 interval = 15 * 60 * 1000 # 900 000 ms (15min) | |
143 ti = new Date().getTime() - interval | |
136 async.eachSeries feed.items, ((post, callback) -> | 144 async.eachSeries feed.items, ((post, callback) -> |
137 # do for each post | 145 # do for each post |
138 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 not (post.object.deleted?) | 146 ts = Date.parse(post.updated) |
147 if (ts >= ti and 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 not (post.object.deleted?) | |
139 postStream(user,post) | 148 postStream(user,post) |
140 callback null, 'done' | 149 callback null, 'done' |
141 ), (err) -> | 150 ), (err) -> |
142 callback null, 'done' | 151 callback null, 'done' |
143 ],(err, result) -> | 152 ],(err, result) -> |
234 ####################################### | 243 ####################################### |
235 ###### get facebook stream ###### | 244 ###### get facebook stream ###### |
236 ####################################### | 245 ####################################### |
237 getStream = (user,callback) -> | 246 getStream = (user,callback) -> |
238 token = user.oauth_token | 247 token = user.oauth_token |
239 | 248 interval = config.interval |
249 if not (interval?) | |
250 interval = 15 * 60 * 1000 # 900 000 ms (15min) | |
251 ts = Math.round(((new Date()).getTime() - interval)/1000) | |
252 | |
240 FB.setAccessToken token | 253 FB.setAccessToken token |
241 | 254 |
242 FB.api "me/home?limit=25&", | 255 FB.api "me/home?limit=25&?since="+ts, |
243 fields: [ | 256 fields: [ |
244 "id" | 257 "id" |
245 "type" | 258 "type" |
246 "from" | 259 "from" |
247 "privacy" | 260 "privacy" |
269 ####################################### | 282 ####################################### |
270 ###### post facebook stream ###### | 283 ###### post facebook stream ###### |
271 ####################################### | 284 ####################################### |
272 postStream = (user, post) -> | 285 postStream = (user, post) -> |
273 token = user.oauth_token | 286 token = user.oauth_token |
287 text = "" | |
274 | 288 |
275 FB.setAccessToken token | 289 FB.setAccessToken token |
276 | 290 |
277 return if not post.object.content? or post.object.content is "" | 291 return if not post.object.content? or post.object.content is "" |
278 ToESN.search {uid: post.object.id + "@facebook"}, (err, result) -> | 292 ToESN.search {uid: post.object.id + "@facebook"}, (err, result) -> |
279 if result.length is 0 | 293 if result.length is 0 |
280 body = post.object.content.replace(/<(?:.|\n)*?>/gm, '') + " " + post.object.url | 294 body = post.object.content.replace(/<(?:.|\n)*?>/gm, '') + " " + post.object.url |
281 | 295 if post.verb is "share" |
296 text = post.object.author.url + " wrotes:" | |
282 FB.api "me/feed", "post", | 297 FB.api "me/feed", "post", |
283 message: body | 298 message: text + body |
284 , (res) -> | 299 , (res) -> |
285 if not res or res.error | 300 if not res or res.error |
286 console.log (if not res then "error occurred" else res.error) | 301 console.log (if not res then "error occurred" else res.error) |
287 return | 302 return |
288 | 303 |