-
Notifications
You must be signed in to change notification settings - Fork 8
/
web-service.lisp
362 lines (328 loc) · 13.2 KB
/
web-service.lisp
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
;; -*- mode: common-lisp -*-
;; Copyright (c) 2015,2016 The OpenWordNet-PT project
;; This program and the accompanying materials are made available
;; under the terms described in the LICENSE file.
(in-package :cl-wnbrowser)
(setq hunchentoot:*show-lisp-errors-p* t)
(defun get-previous (n &optional (step 10))
(- n step))
(defun get-next (n &optional (step 10))
(+ n step))
(defun get-login ()
(list :login (hunchentoot:session-value :login)))
(defun process-synset (parameters)
(if (getf parameters :error)
(cl-wnbrowser.templates:synset-invalid
(append
(get-login)
parameters))
(cl-wnbrowser.templates:synset
(append
(get-login)
parameters))))
(defun make-callback-uri (request-uri)
(let* ((redirect-uri (format nil "https://~a~a" *base-url* request-uri))
(callback-uri (format nil "https://~a/callback%3Fdestination=~a"
*base-url*
(hunchentoot:url-encode request-uri))))
callback-uri))
(defun process-nomlex (nomlex)
(cl-wnbrowser.templates:nomlex
(append (get-login) nomlex)))
(defun process-results (result)
(cl-wnbrowser.templates:result result))
(defun process-error (result)
(cl-wnbrowser.templates:searcherror result))
(hunchentoot:define-easy-handler (get-root-handler-redirector :uri "/wn") ()
(hunchentoot:redirect "/"))
(hunchentoot:define-easy-handler (get-root-handler :uri "/") ()
(cl-wnbrowser.templates:home
(append
(get-login)
(list
:info (when (equal 'own-api *backend*) (get-root))
:githubid *github-client-id*))))
(defun disable-caching ()
(hunchentoot:no-cache))
(hunchentoot:define-easy-handler (execute-search-handler :uri "/search")
(term search_field start debug limit num-pages
(fq_frame :parameter-type 'list)
(fq_word_count_pt :parameter-type 'list)
(fq_word_count_en :parameter-type 'list)
(fq_rdftype :parameter-type 'list)
(fq_lexfile :parameter-type 'list) sexp)
(disable-caching)
(setf (hunchentoot:session-value :term) term)
(setf (hunchentoot:session-value :search_field) search_field)
(if (is-synset-id term)
(hunchentoot:redirect (format nil "/synset?id=~a" term))
(multiple-value-bind
(documents num-found facets error)
(execute-search
*backend*
term
:search-field search_field
:rdf-type fq_rdftype
:lex-file fq_lexfile
:frame fq_frame
:word-count-pt fq_word_count_pt
:word-count-en fq_word_count_en
:start start :limit limit)
(let* ((start/i (if start (parse-integer start) 0))
(limit/i (if limit (parse-integer limit) 10))
(num-pages/i (if num-pages (parse-integer num-pages) 5))
(request-uri (hunchentoot:request-uri*))
(result (if error
(list :error error :term term)
(list :debug debug :term term :search_field search_field
:githubid *github-client-id*
:login (hunchentoot:session-value :login)
:callbackuri (make-callback-uri request-uri)
:returnuri request-uri
:info (when (equal 'own-api *backend*) (get-root))
:fq_frame fq_frame
:fq_rdftype fq_rdftype
:fq_lexfile fq_lexfile
:fq_word_count_pt fq_word_count_pt
:fq_word_count_en fq_word_count_en
:previous (get-previous start/i)
:next (get-next start/i limit/i)
:limit limit/i
:start start/i :numfound num-found
:facets facets :documents documents :numpages num-pages/i))))
(if (string-equal "yes" sexp)
(progn
(setf (hunchentoot:content-type*) "application/sexp")
(with-output-to-string (s)
(print result s)))
(progn
(setf (hunchentoot:content-type*) "text/html")
(if error
(process-error result)
(progn
(hunchentoot:delete-session-value :ids)
(process-results result)))))))))
(hunchentoot:define-easy-handler (search-activity-handler :uri "/search-activities")
(term start debug sf so
(fq_sum_votes :parameter-type 'list)
(fq_num_votes :parameter-type 'list)
(fq_type :parameter-type 'list)
(fq_tag :parameter-type 'list)
(fq_action :parameter-type 'list)
(fq_status :parameter-type 'list)
(fq_doc_type :parameter-type 'list)
(fq_provenance :parameter-type 'list)
(fq_user :parameter-type 'list) sexp)
(disable-caching)
(multiple-value-bind
(documents num-found facets error)
(search-activities
*backend*
term
:sum_votes fq_sum_votes
:num_votes fq_num_votes
:type fq_type
:tags fq_tag
:action fq_action
:status fq_status
:doc_type fq_doc_type
:provenance fq_provenance
:user fq_user
:start start
:limit "25" :sf sf :so so)
(let* ((start/i (if start (parse-integer start) 0))
(request-uri (hunchentoot:request-uri*))
(result (if error (list :error error :term term)
(append (get-login)
(list :debug debug
:info (when (equal 'own-api *backend*) (get-root))
:term term
:githubid *github-client-id*
:login (hunchentoot:session-value :login)
:callbackuri (make-callback-uri request-uri)
:returnuri request-uri
:fq_type fq_type
:fq_num_votes fq_num_votes
:fq_sum_votes fq_sum_votes
:fq_tag fq_tag
:fq_action fq_action
:fq_status fq_status
:fq_doc_type fq_doc_type
:fq_user fq_user
:fq_provenance fq_provenance
:previous (get-previous start/i)
:next (get-next start/i 25)
:so so
:sf sf
:start start/i :numfound num-found
:facets facets
:documents documents)))))
(if (string-equal "yes" sexp)
(progn
(setf (hunchentoot:content-type*) "application/sexp")
(with-output-to-string (s)
(print result s)))
(progn
(setf (hunchentoot:session-value :term) term)
(setf (hunchentoot:content-type*) "text/html")
(if error (process-error (list :error error :term term))
(cl-wnbrowser.templates:activities result)))))))
(hunchentoot:define-easy-handler
(get-synset-handler :uri "/synset") (id debug sexp)
(disable-caching)
(let* ((synset (get-synset *backend* id))
(suggestions (get-suggestions *backend* id))
(comments (get-comments *backend* id))
(request-uri (hunchentoot:request-uri*))
(term (hunchentoot:session-value :term))
(ids (hunchentoot:session-value :ids))
(search_field (hunchentoot:session-value :search_field)))
(if (string-equal "yes" sexp)
(progn
(setf (hunchentoot:content-type*) "application/sexp")
(with-output-to-string (s)
(print (list :comments comments :suggestions suggestions :synset synset) s)))
(progn
(when (not (string-equal (lastcar ids) id))
(setf (hunchentoot:session-value :ids) (append ids (list id))))
(setf (hunchentoot:content-type*) "text/html")
(process-synset
(append (list
:original-id id
:ids (last (hunchentoot:session-value :ids) *breadcrumb-size*)
:term term
:callbackuri (make-callback-uri request-uri)
:returnuri request-uri
:debug debug
:comments comments
:suggestions suggestions
:githubid *github-client-id*
:synset synset
:error (when (null synset) t))
synset
(list :search_field search_field)))))))
;; (hunchentoot:define-easy-handler (get-nomlex-handler
;; :uri "/wn/nomlex") (id debug term)
;; (setf (hunchentoot:content-type*) "text/html")
;; (disable-caching)
;; (let ((nomlex (get-nomlex id))
;; (term (hunchentoot:session-value :term)))
;; (process-nomlex
;; (append
;; (list :term term
;; :info (get-root)
;; :callbackuri (make-callback-uri (hunchentoot:request-uri*))
;; :debug debug
;; :githubid *github-client-id*
;; :nomlex nomlex)
;; nomlex))))
(hunchentoot:define-easy-handler
(process-suggestion-handler :uri "/process-suggestion") (id doc_type type params return-uri)
(let ((login (hunchentoot:session-value :login)))
(if login
(let ((suggestion-id (add-suggestion *backend* id doc_type type params login)))
(if (member login *vote-authorized-accounts* :test #'equal)
(add-vote *backend* suggestion-id login 1))
(hunchentoot:redirect (hunchentoot:url-decode return-uri)))
(progn
(setf (hunchentoot:content-type*) "text/html")
(format nil "invalid login")))))
(hunchentoot:define-easy-handler (delete-suggestion-handler
:uri "/delete-suggestion") (id return-uri)
(let ((login (hunchentoot:session-value :login)))
(if login
(progn
(delete-suggestion *backend* login id)
(hunchentoot:redirect (hunchentoot:url-decode return-uri)))
(progn
(setf (hunchentoot:content-type*) "text/html")
(format nil "invalid login")))))
;; (hunchentoot:define-easy-handler (accept-suggestion-handler
;; :uri "/wn/accept-suggestion") (id return-uri)
;; (let ((login (hunchentoot:session-value :login)))
;; (if login
;; (progn
;; (accept-suggestion id)
;; (hunchentoot:redirect (hunchentoot:url-decode return-uri)))
;; (progn
;; (setf (hunchentoot:content-type*) "text/html")
;; (format nil "invalid login")))))
;; (hunchentoot:define-easy-handler (reject-suggestion-handler
;; :uri "/wn/reject-suggestion") (id return-uri)
;; (let ((login (hunchentoot:session-value :login)))
;; (if login
;; (progn
;; (reject-suggestion id)
;; (hunchentoot:redirect (hunchentoot:url-decode return-uri)))
;; (progn
;; (setf (hunchentoot:content-type*) "text/html")
;; (format nil "invalid login")))))
;; comments
(hunchentoot:define-easy-handler
(process-comment-handler :uri "/process-comment") (id doc_type text return-uri)
(let ((login (hunchentoot:session-value :login)))
(if login
(progn
(add-comment *backend* id doc_type text login)
(hunchentoot:redirect (hunchentoot:url-decode return-uri)))
(progn
(setf (hunchentoot:content-type*) "text/html")
(format nil "invalid login")))))
(hunchentoot:define-easy-handler
(delete-comment-handler :uri "/delete-comment") (id return-uri)
(let ((login (hunchentoot:session-value :login)))
(if login
(progn
(delete-comment *backend* login id)
(hunchentoot:redirect (hunchentoot:url-decode return-uri)))
(progn
(setf (hunchentoot:content-type*) "text/html")
(format nil "invalid login")))))
;; votes
(hunchentoot:define-easy-handler
(vote-up-handler :uri "/vote-up") (id)
(let ((login (hunchentoot:session-value :login)))
(setf (hunchentoot:content-type*) "application/json")
(if login
(with-output-to-string (s)
(yason:encode-plist (add-vote *backend* id login 1) s))
(with-output-to-string (s)
(yason:encode-plist (list :result "not-authorized") s)))))
(hunchentoot:define-easy-handler
(vote-down-handler :uri "/vote-down") (id)
(let ((login (hunchentoot:session-value :login)))
(setf (hunchentoot:content-type*) "application/json")
(if login
(with-output-to-string (s)
(yason:encode-plist (add-vote *backend* id login -1) s))
(with-output-to-string (s)
(yason:encode-plist (list :result "not-authorized") s)))))
(hunchentoot:define-easy-handler
(delete-vote-handler :uri "/delete-vote") (id)
(let ((login (hunchentoot:session-value :login)))
(when login
(delete-vote *backend* id))
(setf (hunchentoot:content-type*) "application/json")
(with-output-to-string (s)
(yason:encode-plist (list :result "Done") s))))
;; github callback
(hunchentoot:define-easy-handler
(github-callback-handler :uri "/callback") (code destination)
(let ((access-token (get-access-token code))
(request-uri (when destination (hunchentoot:url-decode destination))))
(setf (hunchentoot:session-value :login) (get-user-login (get-user access-token)))
(if (cl-strings:starts-with request-uri "/")
(hunchentoot:redirect request-uri)
(hunchentoot:redirect "/"))))
(defun publish-static-content (dir)
(push (hunchentoot:create-folder-dispatcher-and-handler
"/st/"
(merge-pathnames #p"static/" dir))
hunchentoot:*dispatch-table*))
(defun start-server (&optional (port 4243))
(publish-static-content *basedir*)
(hunchentoot:start
(make-instance 'hunchentoot:easy-acceptor
:address "0.0.0.0"
:access-log-destination (merge-pathnames #p"wn.log" *basedir*)
:port port)))