Mercurial > pumpbridge
comparison src/pumpio.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 https = require("https") | |
10 async = require("async") | |
11 _ = require("underscore") | |
12 Routes = require("./routes") | |
13 querystring = require("querystring") | |
14 EdgeControl = require("./edgecontrol") | |
15 Edge = require("./edge") | |
16 Host = require("../node_modules/pump.io-client-app/lib/models/host") | |
17 OAuth = require('oauth') | |
18 Usermap = require("./usermap") | |
19 | |
20 | |
21 ####################################### | |
22 ##### get user info pumpio ##### | |
23 ####################################### | |
24 getUser = (user) -> | |
25 id = user.user_ESN.substr(0,user.user_ESN.indexOf('@')) | |
26 server = user.user_ESN.substr(user.user_ESN.indexOf('@')+1,user.user_ESN.length) | |
27 token = user.oauth_token.substr(0,user.oauth_token.indexOf(';')) | |
28 secret = user.oauth_token.substr(user.oauth_token.indexOf(';')+1,user.oauth_token.length) | |
29 # get tokens and ask pump.io api for userinformation then update userdb | |
30 async.waterfall [ | |
31 (callback) -> | |
32 Host.search {hostname: server}, callback | |
33 (host,callback) -> | |
34 # get host from db | |
35 host = JSON.stringify(host) | |
36 host = JSON.parse(host) | |
37 oauth = new OAuth.OAuth(host[0].request_token_endpoint, host[0].access_token_endpoint, host[0].client_id, host[0].client_secret, "1.0A", null, "HMAC-SHA1") | |
38 oauth.get "https://" + server + "/api/user/"+id, token, secret, callback | |
39 ], (err,result) -> | |
40 #console.log 'PUMP BODY:' + result | |
41 result = JSON.parse(result) | |
42 id = result.profile.id.substr(result.profile.id.indexOf(':')+1,result.profile.id.length) | |
43 Routes.updateUserDB(id,result.profile.preferredUsername,result.profile.displayName,result.profile.url,result.profile.image.url) | |
44 return | |
45 | |
46 postUser = (bridgeid, to, text, callback) -> | |
47 | |
48 bridgename = bridgeid.substr(0,bridgeid.indexOf('@')) | |
49 server = bridgeid.substr(bridgeid.indexOf('@')+1,bridgeid.length) | |
50 token=undefined | |
51 secret=undefined | |
52 | |
53 async.waterfall [ | |
54 (callback) -> | |
55 Usermap.search {id: bridgeid+'_to_'+bridgeid}, callback | |
56 (bridgeuser, callback) -> | |
57 token = bridgeuser[0].oauth_token.substr(0,bridgeuser[0].oauth_token.indexOf(';')) | |
58 secret = bridgeuser[0].oauth_token.substr(bridgeuser[0].oauth_token.indexOf(';')+1,bridgeuser[0].oauth_token.length) | |
59 Host.search {hostname: server}, callback | |
60 (host,callback) -> | |
61 # get host from db | |
62 host = JSON.stringify(host) | |
63 host = JSON.parse(host) | |
64 activity = | |
65 verb: "post" | |
66 #cc: [ | |
67 # id: "http://activityschema.org/collection/public" | |
68 # objectType: "collection" | |
69 #] | |
70 to: [ | |
71 id: "acct:" + to | |
72 objectType: "person" | |
73 ] | |
74 object: | |
75 objectType: "note" | |
76 content: text | |
77 oauth = new OAuth.OAuth(host[0].request_token_endpoint, host[0].access_token_endpoint, host[0].client_id, host[0].client_secret, "1.0A", null, "HMAC-SHA1") | |
78 oauth.post "https://" + server + "/api/user/" + bridgename + "/feed", token, secret, JSON.stringify(activity), 'application/json', callback | |
79 ], (err,result) -> | |
80 #console.log 'PUMP BODY:' + result | |
81 callback null, result | |
82 | |
83 return | |
84 | |
85 ####################################### | |
86 ##### get user pumpio feed ##### | |
87 ####################################### | |
88 getUserFeed = (user, callback) -> | |
89 id = user.user_ESN.substr(0,user.user_ESN.indexOf('@')) | |
90 server = user.user_ESN.substr(user.user_ESN.indexOf('@')+1,user.user_ESN.length) | |
91 token = user.oauth_token.substr(0,user.oauth_token.indexOf(';')) | |
92 secret = user.oauth_token.substr(user.oauth_token.indexOf(';')+1,user.oauth_token.length) | |
93 # get tokens and ask pump.io api for userinformation then update userdb | |
94 async.waterfall [ | |
95 (callback) -> | |
96 Host.search {hostname: server}, callback | |
97 (host,callback) -> | |
98 # get host from db | |
99 host = JSON.stringify(host) | |
100 host = JSON.parse(host) | |
101 oauth = new OAuth.OAuth(host[0].request_token_endpoint, host[0].access_token_endpoint, host[0].client_id, host[0].client_secret, "1.0A", null, "HMAC-SHA1") | |
102 oauth.get "https://" + server + "/api/user/"+id+"/feed", token, secret, callback | |
103 ], (err,result) -> | |
104 #console.log 'PUMP BODY:' + result | |
105 result = JSON.parse(result) | |
106 callback null, result | |
107 return | |
108 | |
109 ####################################### | |
110 ##### get user pumpio note ##### | |
111 ####################################### | |
112 getNote = (user, noteurl, callback) -> | |
113 id = user.user_ESN.substr(0,user.user_ESN.indexOf('@')) | |
114 server = user.user_ESN.substr(user.user_ESN.indexOf('@')+1,user.user_ESN.length) | |
115 token = user.oauth_token.substr(0,user.oauth_token.indexOf(';')) | |
116 secret = user.oauth_token.substr(user.oauth_token.indexOf(';')+1,user.oauth_token.length) | |
117 | |
118 # get tokens and ask pump.io api for userinformation then update userdb | |
119 async.waterfall [ | |
120 (callback) -> | |
121 Host.search {hostname: server}, callback | |
122 (host,callback) -> | |
123 # get host from db | |
124 host = JSON.stringify(host) | |
125 host = JSON.parse(host) | |
126 oauth = new OAuth.OAuth(host[0].request_token_endpoint, host[0].access_token_endpoint, host[0].client_id, host[0].client_secret, "1.0A", null, "HMAC-SHA1") | |
127 oauth.get noteurl, token, secret, callback | |
128 ], (err,result) -> | |
129 #console.log 'PUMP BODY:' + result | |
130 result = JSON.parse(result) | |
131 callback null, result | |
132 return | |
133 | |
134 isPublicActivity = (act) -> | |
135 recip = [] | |
136 _.each [ | |
137 "to" | |
138 "cc" | |
139 "bto" | |
140 "bcc" | |
141 ], (prop) -> | |
142 recip = recip.concat(act[prop]) if _.isArray(act[prop]) | |
143 return | |
144 | |
145 _.some recip, (rec) -> | |
146 rec.objectType is "collection" and rec.id is "http://activityschema.org/collection/public" | |
147 | |
148 | |
149 exports.isPublicActivity = isPublicActivity | |
150 exports.getUserFeed = getUserFeed | |
151 exports.getUser = getUser | |
152 exports.getNote = getNote | |
153 exports.postUser = postUser |