summaryrefslogtreecommitdiff
path: root/lisp/service/messages-service.lisp
blob: 9e2a4194d79520e5290b88807956b27d2bd04d82 (plain)
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
;;; -*- Mode: Lisp; Syntax: ANSI-Common-Lisp; Base: 10 -*-
(declaim (optimize (speed 0) (safety 3) (debug 3)))

(in-package :bogenherr)

(defclass messages-service (auth-service)
  ((title :initarg :title
          :initform nil
          :accessor title)
   (form :initarg :form
         :initform nil
         :accessor form))
  (:documentation ""))

(defclass messages/view-service (messages-service)
  ((results :initarg :results
            :initform nil
            :accessor results)
   (location-p :initform nil))
  (:documentation ""))

(defclass messages/mark-service (messages-service)
  ((location-p :initform nil))
  (:documentation ""))

(defun messages-json ()
  (with-auth (instance messages-service "messages-view")
    (setf (title instance) "Messages Administration")
    (setf (form instance) (make-form "messages-select-mode-form"
                                     nil
                                     nil
                                     `((:label "View Unread" :field-type "button" :onclick "on_messages_mode_clicked('unread')")
                                       (:label "View Read" :field-type "button" :onclick "on_messages_mode_clicked('read')"))))))

(defun messages-view-json (read)
  (with-auth (instance messages/view-service "messages-view")
    (when read (setf (session-value :messages-read) read))
    (with-bogenherr-database
      (let ((contact-pkg (make-instance 'contact-pkg)))
        (setf (results instance) (get-contact-us-posts contact-pkg
                                                       (id (get-user))
                                                       (string= (session-value :messages-read) "read")))))
    (sanitize-rest-json instance)
    (loop for result in (results instance)
          do (sanitize-json result))))

(defun messages-mark-json (read id)
  (with-auth (instance messages/mark-service "messages-view")
    (with-bogenherr-database
      (handler-case
          (let ((contact-pkg (make-instance 'contact-pkg)))
            (mark-contact-us-post contact-pkg id (id (get-user)) (string= read "read"))
            (setf (message instance) (format nil "Message marked ~a successfully." read)))
        (error (e)
          (declare (ignore e))
          (setf (errormsg instance) (format nil "Error marking message ~a." read)))))))

(define-endpoint ("/messages" :method :get) () messages-json)
(define-endpoint ("/messages/view" :method :post) (&post (read :parameter-type 'string)) messages-view-json read)
(define-endpoint ("/messages/mark" :method :post) (&post (read :parameter-type 'string) (id :parameter-type 'integer)) messages-mark-json read id)