Skip to content

Commit

Permalink
* synset page
Browse files Browse the repository at this point in the history
  - votes handles
  - comment handles
  - suggestion handles
* define more generic methods
related to #189
  • Loading branch information
hmuniz committed Sep 27, 2019
1 parent 4572acf commit 7290631
Show file tree
Hide file tree
Showing 3 changed files with 235 additions and 133 deletions.
138 changes: 111 additions & 27 deletions backend.lisp
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
(in-package :cl-wnbrowser)

;; generics to used as backend

(defgeneric get-synset (backend id))

(defgeneric get-suggestions (backend id))
Expand All @@ -9,7 +11,28 @@
(defgeneric add-comment (backend id doc_type text login))

(defgeneric delete-comment (backend id))
;;;

(defgeneric add-suggestion (backend id doc_type type params login))

(defgeneric delete-suggestion (backend id))

(defgeneric accept-suggestion (backend id))

(defgeneric reject-suggestion (backend id))

(defgeneric delete-vote (backend id))

(defgeneric add-vote (backend id user value))

;; ES aux

(defun now ()
"It returns the number of milliseconds elapsed since January 1,
1970, 00:00:00 UTC."
(let ((timestamp (local-time:now)))
(+
(* 1000 (local-time:timestamp-to-unix timestamp))
(local-time:timestamp-millisecond timestamp))))

(defun get-synset-word-en (id)
"Returns the FIRST entry in the word_en property for the given SYNSET-ID"
Expand All @@ -31,23 +54,6 @@ returns the first entry in word_en."
(gloss-en (car (getf synset :|gloss_en|))))
(if gloss-pt gloss-pt gloss-en)))

;;;
(defmethod get-synset ((backend (eql 'es)) id)
(let ((yason:*parse-object-as* :plist)
(yason:*parse-object-key-fn* #'make-keyword))
(getf (clesc:es/get "wn" "_doc" id) ':|_source|)))

(defmethod get-suggestions ((backend (eql 'es)) id)
(let* ((yason:*parse-object-as* :plist)
(yason:*parse-object-key-fn* #'make-keyword)
(hits (getf
(getf (clesc:es/search "suggestion" :terms `(("doc_id" ,id)
("type" "suggestion")))
:|hits|)
:|hits|))
(suggestions (mapcar (lambda (hit) (getf hit :|_source|)) hits)))
(mapcar #'sugestion+votes suggestions)))


(defun sugestion+votes (suggestion)
(let*((yason:*parse-object-as* :plist)
Expand Down Expand Up @@ -78,6 +84,24 @@ returns the first entry in word_en."
:|votes| (:|positive| ,positive :|negative| ,negative :|total| ,total
:|positive_votes| ,positive-votes :|negative_votes| ,negative-votes))))))

;;; ES backend

(defmethod get-synset ((backend (eql 'es)) id)
(let ((yason:*parse-object-as* :plist)
(yason:*parse-object-key-fn* #'make-keyword))
(getf (clesc:es/get "wn" "_doc" id) ':|_source|)))

(defmethod get-suggestions ((backend (eql 'es)) id)
(let* ((yason:*parse-object-as* :plist)
(yason:*parse-object-key-fn* #'make-keyword)
(hits (getf
(getf (clesc:es/search "suggestion" :terms `(("doc_id" ,id)
("type" "suggestion")))
:|hits|)
:|hits|))
(suggestions (mapcar (lambda (hit) (getf hit :|_source|)) hits)))
(mapcar #'sugestion+votes suggestions)))

(defmethod get-comments ((backend (eql 'es)) id)
(let* ((yason:*parse-object-as* :plist)
(yason:*parse-object-key-fn* #'make-keyword)
Expand All @@ -89,14 +113,7 @@ returns the first entry in word_en."
(comments (mapcar (lambda (hit) (getf hit :|_source|)) hits)))
comments))


(defun now ()
(let ((timestamp (local-time:now)))
(+ (* 86400000 (+ 11017 (local-time:day-of timestamp)))
(* 1000 (local-time:sec-of timestamp))
(local-time:nsec-of timestamp))))


;; comments
(defmethod add-comment ((backend (eql 'es)) synset-id doc_type text login)
(let* ((id (print-object (uuid:make-v4-uuid) nil))
(comment (alexandria:alist-hash-table
Expand All @@ -113,4 +130,71 @@ returns the first entry in word_en."
("id" . ,id)))))
(clesc:es/add "suggestion" "suggestion" comment :id id)))

(defmethod defmethod add-comment ((backend (eql 'es)) id))

(defmethod delete-comment ((backend (eql 'es)) id)
(clesc:es/delete "suggestion" "suggestion" id))

;; suggestions
(defmethod add-suggestion ((backend (eql 'es)) synset-id doc-type suggestion-type params login)
(let* ((suggestion-id (print-object (uuid:make-v4-uuid) nil))
(db "wnproposedchanges")
(provenance "web")
(type "suggestion")
(value (format nil "~a(~a)" suggestion-type params))
(suggestion (alexandria:alist-hash-table
`(("date" . ,(now))
("doc_id" . ,synset-id)
("doc_type" . ,doc-type)
("type" . ,type)
("action" . ,suggestion-type)
("params" . ,params)
("user" . ,login)
("status" . "new") ;; new accepted not-accepted committed
("provenance" . ,provenance)
("id" . ,suggestion-id)))))
(clesc:es/add "suggestion" "suggestion" suggestion :id suggestion-id)
(register-audit db "add-suggestion" synset-id type value login provenance)))

(defmethod delete-suggestion ((backend (eql 'es)) id)
(clesc:es/delete "suggestion" "suggestion" id))

;; audit
(defun register-audit (db action doc-id field value user provenance)
(let* ((audit-id (print-object (uuid:make-v4-uuid) nil))
(audit (alexandria:alist-hash-table
`(("date" . ,(now))
("db" . ,db)
("action" . ,action)
("doc_id" . ,doc-id)
("field" . ,field)
("value" . ,value)
("user" . ,user)
("provenance" . ,provenance)
("id" . ,audit-id)))))
(clesc:es/add "audit" "audit" audit :id audit-id)))


;; vote
(defmethod add-vote ((backend (eql 'es)) suggestion-id user value)
(let* ((yason:*parse-object-as* :plist)
(yason:*parse-object-key-fn* #'make-keyword)
(user-votes (getf
(getf (clesc:es/search "votes" :terms `(("suggestion_id" ,suggestion-id) ("user" ,user)))
:|hits|)
:|hits|)))
(if user-votes
'("error" "user-already-voted")
(let* ((vote-id (print-object (uuid:make-v4-uuid) nil))
(db "wnvotes")
(vote (alexandria:alist-hash-table
`(("id" . ,vote-id)
("date" . ,(now))
("suggestion_id" . ,suggestion-id)
("user" . ,user)
("value" . ,value)))))
(clesc:es/add "votes" "votes" vote :id vote-id)
(register-audit db "add-vote" suggestion-id "vote" value user "web")
`("status" "vote-added" "id" ,vote-id)))))

(defmethod delete-vote ((backend (eql 'es)) id)
(clesc:es/delete "votes" "votes" id))
130 changes: 72 additions & 58 deletions ownpt-api.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@

(in-package :cl-wnbrowser)


;; own-api aux

(defun get-search-query-plist (q drilldown limit start sort-field sort-order fl)
(remove
nil
Expand All @@ -24,60 +27,6 @@
(call-rest-method
api
:parameters (get-search-query-plist term drilldown limit start sort-field sort-order fl)))

(defun delete-suggestion (id)
(call-rest-method
(format nil "delete-suggestion/~a" (drakma:url-encode id :utf-8))
:parameters (list (cons "key" *ownpt-api-key*))))

(defun accept-suggestion (id)
(call-rest-method
(format nil "accept-suggestion/~a" (drakma:url-encode id :utf-8))
:parameters (list (cons "key" *ownpt-api-key*))))

(defun reject-suggestion (id)
(call-rest-method
(format nil "reject-suggestion/~a" (drakma:url-encode id :utf-8))
:parameters (list (cons "key" *ownpt-api-key*))))

(defmethod delete-comment ((backend (eql 'own-api)) id)
(call-rest-method
(format nil "delete-comment/~a" (drakma:url-encode id :utf-8))
:parameters (list (cons "key" *ownpt-api-key*))))

(defun add-suggestion (id doc-type type param login)
(call-rest-method
(format nil "add-suggestion/~a" (drakma:url-encode id :utf-8))
:parameters (list (cons "doc_type" doc-type)
(cons "suggestion_type" type)
(cons "params" param)
(cons "key" *ownpt-api-key*)
(cons "user" login))))

(defmethod add-comment ((backend (eql 'own-api)) id doc-type text login)
(call-rest-method
(format nil "add-comment/~a" (drakma:url-encode id :utf-8))
:parameters (list (cons "doc_type" doc-type)
(cons "text" text)
(cons "key" *ownpt-api-key*)
(cons "user" login))))

(defmethod get-suggestions ((backend (eql 'own-api)) id)
(get-docs (call-rest-method (format nil "get-suggestions/~a" id))))

(defmethod get-comments ((backend (eql 'own-api)) id)
(get-docs (call-rest-method (format nil "get-comments/~a" id))))

(defun delete-vote (id)
(call-rest-method (format nil "delete-vote/~a" id)
:parameters (list (cons "key" *ownpt-api-key*))))

(defun add-vote (id user value)
(call-rest-method (format nil "add-vote/~a" id)
:parameters (list
(cons "user" user)
(cons "value" (format nil "~a" value))
(cons "key" *ownpt-api-key*))))

(defun get-document-by-id (doctype id)
(call-rest-method (format nil "~a/~a" doctype (drakma:url-encode id :utf-8))))
Expand Down Expand Up @@ -210,9 +159,6 @@ LEX-FILE."
(mapcar (lambda (s) (getf s :|doc_id|)) (get-docs result))
nil)))

(defmethod get-synset ((backend (eql 'own-api)) id)
(get-document-by-id "synset" id))

(defun get-nomlex (id)
(get-document-by-id "nomlex" id))

Expand All @@ -233,7 +179,9 @@ LEX-FILE."
(call-rest-method "statistics"))

(defun call-rest-method/stream (method &key parameters)
"Alternative to CALL-REST-METHOD that uses a stream; this is more memory efficient, but it may cause problems if YASON:PARSE takes too long to parse the stream and the stream may be cut due to timeout."
"Alternative to CALL-REST-METHOD that uses a stream; this is more
memory efficient, but it may cause problems if YASON:PARSE takes too
long to parse the stream and the stream may be cut due to timeout."
(let* ((stream (drakma:http-request
(format nil "~a/~a" *ownpt-api-uri* method)
:parameters parameters
Expand Down Expand Up @@ -261,3 +209,69 @@ LEX-FILE."
:object-as :plist :object-key-fn #'make-keyword)))


;;; own-api backend

(defmethod get-synset ((backend (eql 'own-api)) id)
(get-document-by-id "synset" id))

(defmethod delete-suggestion ((backend (eql 'own-api)) id)
(call-rest-method
(format nil "delete-suggestion/~a" (drakma:url-encode id :utf-8))
:parameters (list (cons "key" *ownpt-api-key*))))

(defmethod accept-suggestion ((backend (eql 'own-api)) id)
(call-rest-method
(format nil "accept-suggestion/~a" (drakma:url-encode id :utf-8))
:parameters (list (cons "key" *ownpt-api-key*))))


(defmethod reject-suggestion ((backend (eql 'own-api)) id)
(call-rest-method
(format nil "reject-suggestion/~a" (drakma:url-encode id :utf-8))
:parameters (list (cons "key" *ownpt-api-key*))))


(defmethod delete-comment ((backend (eql 'own-api)) id)
(call-rest-method
(format nil "delete-comment/~a" (drakma:url-encode id :utf-8))
:parameters (list (cons "key" *ownpt-api-key*))))


(defmethod add-suggestion ((backend (eql 'own-api)) id doc-type type param login)
(call-rest-method
(format nil "add-suggestion/~a" (drakma:url-encode id :utf-8))
:parameters (list (cons "doc_type" doc-type)
(cons "suggestion_type" type)
(cons "params" param)
(cons "key" *ownpt-api-key*)
(cons "user" login))))


(defmethod add-comment ((backend (eql 'own-api)) id doc-type text login)
(call-rest-method
(format nil "add-comment/~a" (drakma:url-encode id :utf-8))
:parameters (list (cons "doc_type" doc-type)
(cons "text" text)
(cons "key" *ownpt-api-key*)
(cons "user" login))))


(defmethod get-suggestions ((backend (eql 'own-api)) id)
(get-docs (call-rest-method (format nil "get-suggestions/~a" id))))


(defmethod get-comments ((backend (eql 'own-api)) id)
(get-docs (call-rest-method (format nil "get-comments/~a" id))))


(defmethod delete-vote ((backend (eql 'own-api)) id)
(call-rest-method (format nil "delete-vote/~a" id)
:parameters (list (cons "key" *ownpt-api-key*))))


(defmethod add-vote ((backend (eql 'own-api)) id user value)
(call-rest-method (format nil "add-vote/~a" id)
:parameters (list
(cons "user" user)
(cons "value" (format nil "~a" value))
(cons "key" *ownpt-api-key*))))
Loading

0 comments on commit 7290631

Please sign in to comment.