/
facebook.ss
214 lines (198 loc) · 8.33 KB
/
facebook.ss
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
#lang scheme/base
;; A PLT Scheme interface to the Facebook API.
;; NOT READY FOR USE YET! Just a preliminary checkin...
;; (One reason this isn't ready is that you have to hack dherman's json.plt planet lib
;; to get this to work.)
(require (file "settings.scm")
(file "util.scm")
"contract-lp.ss"
"web-export.ss"
(file "web-support.scm")
"page.scm"
"form.scm"
"util.scm"
"js.scm"
(only-in (planet "json.ss" ("dherman" "json.plt" 1 (= 1))) (read read-json)))
(provide facebook-fn
define-facebook-required-login-page
facebook-require-login
facebook-form
facebook-strict-error
;; facebook-create-object (via contract)
;; facebook-complex-val (via contract)
;; facebook-session-key (via contract)
;; facebook-error (via contract)
)
;; MMM this design prevents us from running multiple Facebook apps off the same
;; web server.
(declare-setting *FB_API_KEY*)
(declare-setting *FB_SECRET_KEY*)
(declare-setting *FB_API_VERSION* "1.0")
(declare-setting *FB_API_URL* "http://api.facebook.com/restserver.php")
;; API notes: All API calls must have a method, api_key and sig parameter. Other
;; parameters are optional or required depending on the particular method. I think "v"
;; (api version) is always required too.
(define-syntax facebook
(syntax-rules ()
((_ method)
'foo)
((_ method keyword val rst ...)
(keyword->string 'keyword))))
;;
;; if val-for-key is non-#f, it should be a symbol corresponding to a JSON hash table
;; key. This function will return #f if the JSON result is not a hash, or does not
;; contain a value for that key. When val-for-key is #f, we simply return the
;; generated JSON object.
;;
(define (facebook-fn method-str (bindings '())
#:val-for-key (val-for-key #f)
#:pass-session-from-req (req #f))
(let* ((sys-bindings `((api_key . ,(setting *FB_API_KEY*))
(call_id . ,(number->string (current-milliseconds)))
(method . ,(string-append "facebook." method-str))
(format . "JSON")
(v . ,(setting *FB_API_VERSION*))))
(augmented-sys-bindings (if req
(alist-cons 'session_key (facebook-session-key req)
sys-bindings)
sys-bindings))
(sans-sig (sort (append bindings augmented-sys-bindings)
(match-lambda* ((list (list-rest k1 v1) (list-rest k2 v2))
(string<=? (symbol->string k1)
(symbol->string k2))))))
(sig (md5-string (fold-right (match-lambda* ((list (list-rest k v) acc)
(string-append (format "~A=~A" k v)
acc)))
(setting *FB_SECRET_KEY*)
sans-sig)))
(json-result (get-url (url+query (setting *FB_API_URL*)
(alist-cons 'sig sig sans-sig))
read-json)))
(if (and (hash? json-result) val-for-key)
(hash-ref json-result val-for-key #f)
json-result)))
;;
;; define-facebook-required-login-page
;;
;; Note that all page keyword args are potentially valid except for #:body-wrap, since we
;; use that to get the "required login" functionality. Also, we force a #:blank #t.
;;
;; on-login-url (if given) should be a URL relative to your callback URL.
;; E.g., if your callback URL is http://myownserver.com/myfbapp/, then the default is to
;; redirect to the top-level (i.e., passing ""). If you wanted to go to
;; http://myownserver.com/myfbapp/foo/bar
;; you should use "foo/bar". (Note you shouldn't use a leading slash for on-login-url.)
;;
;; Note in the settings for your app on facebook.com, make sure your callback URL ends in
;; a slash!
;;
(define-syntax define-facebook-required-login-page
(syntax-rules (=>)
((_ (page-name req args ...) => on-login-url
keywords-and-body ...)
(define-page (page-name req args ...)
#:blank #t
#:body-wrap (lambda (body) (facebook-require-login on-login-url body))
keywords-and-body ...))
((_ (page-name req args ...)
keywords-and-body ...)
(define-facebook-required-login-page (page-name req args ...) => ""
keywords-and-body ...))))
(define (facebook-require-login on-login-url . body)
`(fb:if-is-app-user
,@body
(fb:else
(fb:redirect
((url ,(format
"http://www.facebook.com/login.php?v=~A&api_key=~A&next=~A&canvas="
(setting *FB_API_VERSION*)
(setting *FB_API_KEY*)
on-login-url)))))))
;;
;; facebook-session-key
;;
(provide/contract (facebook-session-key (-> request? (or/c #f string?))))
;;
(define (facebook-session-key req)
(let ((binds (request-bindings req)))
(or (assoc-val 'fb_sig_session_key binds)
(aand (assoc-val 'auth_token binds)
(facebook-fn "auth.getSession" `((auth_token . ,it))
#:val-for-key 'session_key)))))
;;
;; facebook-error
;;
;; returns #f (if the given json result isn't an error) and o/w returns the error msg.
;;
(provide/contract (facebook-error (-> any/c (or/c #f string?))))
;;
(define (facebook-error json-result)
(and (hash? json-result)
(hash-ref json-result 'error_msg #f)))
;;
;; facebook-uid
;;
(provide/contract (facebook-uid (-> request? (or/c #f string?))))
;;
(define (facebook-uid req)
(assoc-val 'fb_sig_user (request-bindings req)))
;;
;; facebook-form
;;
;; Same interface to the standard form function. A few keyword args are set appropriately
;; for Facebook, though.
;;
(define facebook-form
(make-keyword-procedure
(lambda (kws kw-vals . reg-args)
(call-with-keyword-override form
kws kw-vals
(list '#:action) (list "")
reg-args))))
;;
;; facebook-complex-val
;;
;; The Facebook API sometimes takes parameters of type "complex". This is just a JSON
;; object. This function takes standard Scheme bindings and turns them into an appropriate
;; "complex" JSON object.
;;
(provide/contract (facebook-complex-val (-> (listof (cons/c symbol? any/c)) string?)))
;;
(define (facebook-complex-val bindings)
(js-hash (hash-hash-map (alist->hash bindings) (lambda (k v) (js-quote v)))))
;;
;; facebook-create-object
;;
;; Each key in bindings must have already been created as a property of the given
;; type.
;; Returns the newly created object id (as determined by Facebook).
;;
(provide/contract (facebook-create-object (->* (symbol?
(listof (cons/c symbol? string?))
request?)
(#:association
(or/c #f string?)
#:associate-existing-id-to-fresh
(or/c #f string?))
string?)))
;;
(define (facebook-create-object type bindings req
#:association (association #f)
#:associate-existing-id-to-fresh (from-id #f))
(let ((obj-id (facebook-fn "data.createObject"
`((obj_type . ,(symbol->string type))
(properties . ,(facebook-complex-val bindings))))))
(aif (facebook-error obj-id)
(e it)
(let ((obj-id (number->string obj-id)))
(when association
(let ((assoc-result (facebook-fn "data.setAssociation"
`((name . ,association)
(obj_id1 . ,from-id)
(obj_id2 . ,obj-id))
#:pass-session-from-req req)))
(awhen (facebook-error assoc-result)
(e it))))
obj-id))))
(define (facebook-strict-error fb-fn-result)
(awhen (facebook-error fb-fn-result) (e it)))