Skip to content

Commit

Permalink
allow pages without frames (sessions) again
Browse files Browse the repository at this point in the history
  • Loading branch information
vii committed May 31, 2009
1 parent 0f2667b commit 98e0c92
Show file tree
Hide file tree
Showing 13 changed files with 208 additions and 135 deletions.
66 changes: 55 additions & 11 deletions src/blog/blog.lisp
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
(in-package #:tpd2.blog)

(defconstant +max-comment-length+ 8000)

(defstruct blog
name
admin-password-file
dir
entries
entries-table
(site (current-site))
(link-base "/")
comment-index-prefix
Expand All @@ -17,14 +20,18 @@

(my-defun blog read-in ()
(with-site ((my site))
(setf (my entries)
(sort
(iter:iter (iter:for path in (cl-fad:list-directory (my dir)))
(let ((filename (force-string path)))
(unless (or (find #\# filename) (find #\~ filename))
(setf
(my entries-table) (make-hash-table :test 'equalp)
(my entries)
(sort
(iter:iter (iter:for path in (cl-fad:list-directory (my dir)))
(let ((filename (force-string path)))
(unless (or (find #\# filename) (find #\~ filename))
(let ((entry (read-in-entry me (file-namestring filename))))
(iter:collect entry)))))
#'> :key #'entry-time))
#'> :key #'entry-time))
(loop for entry in (my entries)
do (setf (gethash (entry-index-name entry) (my entries-table)) entry))
(my set-page))
me)

Expand All @@ -36,19 +43,25 @@
(my-defun blog rss-feed-url ()
(byte-vector-cat (my link-base) "feed.rss"))

(my-defun blog post-comment-url ()
(byte-vector-cat (my link-base) "comment.form"))

(my-defun blog admin-url ()
(byte-vector-cat (my link-base) "blog-admin"))

(defmacro defpage-lambda-blog (path function &rest args)
`(defpage-lambda ,path ,function :create-frame nil ,@args))

(my-defun blog set-page ()
(with-site ((my site))
(defpage-lambda (my atom-feed-url)
(defpage-lambda-blog (my atom-feed-url)
(lambda ()
(my atom-feed)))
(defpage-lambda (my rss-feed-url)
(defpage-lambda-blog (my rss-feed-url)
(lambda ()
(my rss-feed)))

(defpage-lambda (my admin-url)
(defpage-lambda-blog (my admin-url)
(lambda (password entry-name)
(webapp "Blog administration"
(<form :method :post
Expand Down Expand Up @@ -81,9 +94,40 @@
(output-object-to-ml c)
(datastore-delete c)))))))))))

(defpage-lambda (my link-base)
(defpage-lambda-blog (my post-comment-url)
(lambda (text author entry-name keep-this-empty .javascript. http-peer-info! all-http-params!)
(let ((entry-name (force-string entry-name)))
(let ((success
(when (and
(zerop (length keep-this-empty))
text
(< (length text) +max-comment-length+)
(not (equalp
text
(ignore-errors (comment-text (first (datastore-retrieve-indexed 'comment 'entry-index-name entry-name)))))))
(let ((entry (gethash entry-name (my entries-table))))
(when entry
(make-comment
:author author
:text text
:trace-details http-peer-info!
:entry-index-name entry-name)
(channel-notify entry))
t))))
(cond
(.javascript.
(webapp-respond-ajax-body all-http-params!))
(success
(webapp "Comment accepted" (<p "Thank you.")))
(t
(webapp "Comment rejected by spam protection"
(<p "Sorry for the inconvenience. Please contact the blog owner with a description of the problem."))))))))


(defpage-lambda-blog (my link-base)
(lambda ((n (force-byte-vector 0)))
(webapp ((my name) :head-contents
(webapp ((my name)
:head-contents
(with-ml-output
(<link :rel "alternate" :type "application/atom+xml" :href (my atom-feed-url))

Expand Down
30 changes: 7 additions & 23 deletions src/blog/entry.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -61,39 +61,23 @@
(my-defun entry story-ml ()
(<div :class "blog-entry-story"
(loop for p in (my paragraphs)
do (<p (output-raw-ml p)))
(<p :class "time" "Posted " (time-string (my time)))))
do (<p (output-raw-ml p)))))

(my-defun entry comments ()
(datastore-retrieve-indexed 'comment 'entry-index-name (my index-name)))

(my-defun entry comment-ml ()
(<div :class "blog-entry-post-comment"
(let ((hidden-value (force-byte-vector (time-string))))
(html-action-form-collapsed "Post a comment"
((text nil :type <textarea)
(author "Anonymous")
(keep-this-empty nil :type :hidden)
(time hidden-value :type :hidden))

(cond ((and (zerop (length keep-this-empty)) (equalp hidden-value time))
(unless (or
(not text)
(equalp text
(ignore-errors (comment-text (first (datastore-retrieve-indexed 'comment 'entry-index-name (my index-name)))))))
(make-comment
:author author
:text text
:trace-details (frame-trace-info (webapp-frame))
:entry-index-name (my index-name))
(my 'channel-notify)))
(t
(webapp "Comment rejected by spam protection"
(<p "Sorry for the inconvenience. Please contact the blog owner with a description of the problem."))))))))
(html-action-form-collapsed ("Post a comment" :action-link (blog-post-comment-url (my blog)))
((text nil :type <textarea :reset "")
(author "Anonymous")
(entry-name (my index-name) :type :hidden)
(keep-this-empty nil :type :hidden)))))

(my-defun entry 'object-to-ml ()
(<div :class "blog-entry"
(my story-ml)
(<p :class "time" "Posted " (time-string (my time)))
(call-next-method)
(my comment-ml)))

Expand Down
5 changes: 0 additions & 5 deletions src/game/web.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -231,11 +231,6 @@
(when (my waiting-for-input)
(<span :class "turn" "'s turn")))))

(defmethod player-controller-message ((controller web-state) sender message)
(web-state-add-announcement controller
(<p :class "message" (<span :class "sender" (frame-username sender))
" " message)))

(defun css ()
(css-html-style
((".inherit" <input <a)
Expand Down
11 changes: 7 additions & 4 deletions src/http/dispatcher.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -55,15 +55,18 @@

(defvar *dispatchers* nil)

(defun do-find-dispatcher (host)
(defun find-dispatcher-go (host)
(alist-get *dispatchers* host :test 'equalp))

(defun find-dispatcher (host)
(or (do-find-dispatcher host) *default-dispatcher*))
(or (find-dispatcher-go host) *default-dispatcher*))

(defun find-or-make-dispatcher (host)
(let ((host (force-byte-vector host)))
(or (do-find-dispatcher host)
(or (find-dispatcher-go host)
(let ((it (make-dispatcher :canonical-name host)))
(push (cons host it) *dispatchers*)
it))))
it))))

(defun dispatcher-add-alias (dispatcher alias)
(setf (alist-get *dispatchers* (force-byte-vector alias)) dispatcher))
6 changes: 6 additions & 0 deletions src/packages.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,7 @@
#:build-http-response
#:respond-http
#:*default-dispatcher*
#:dispatcher-add-alias
#:find-or-make-dispatcher
#:http-parse-and-generate-response))

Expand Down Expand Up @@ -293,6 +294,11 @@
#:channel-update
#:find-channel

#:webapp-respond-ajax-body

#:http-peer-info!
#:all-http-params!

#:message-channel
#:simple-channel
#:simple-channel-body-ml
Expand Down
82 changes: 48 additions & 34 deletions src/webapp/actions.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
(action-id action)))

(defmacro page-action-lambda (&body body)
`(lambda(all-http-params)
(declare (ignorable all-http-params))
`(lambda(all-http-params!)
(declare (ignorable all-http-params!))
,@body))

(defmacro page-action-link (&body body)
Expand All @@ -34,49 +34,63 @@
,@body)))

(defmacro html-action-form-collapsed (title lambda-list &body body)
`(html-collapser (<p ,title)
(html-action-form nil ,lambda-list ,@body)))
`(html-collapser (<p ,(force-first title))
(html-action-form (nil ,@(force-rest title) :after-submit-js ((toggle-hiding this.parent-node))) ,lambda-list ,@body)))

(defmacro html-action-form (title lambda-list &body body)
`(<form
:onsubmit (js-attrib (return (async-submit-form this)))
:method :post
:action
(page-action-link
(let ,(loop for p in lambda-list collect
`(,(force-first p) (or (alist-get all-http-params ,(force-byte-vector (force-first p))
:test 'byte-vector=-fold-ascii-case)
,(second (force-list p)))))
,@body))
(<p
,title
,@(loop for nv in lambda-list collect
(destructuring-bind (name &optional value &key (type '<input))
(force-list nv)
(let ((name (force-byte-vector name)))
(ecase type
(<input
`(<input :type :text :name ,name
,@(when value `(:value ,value))))
(<textarea
`(<textarea :name ,name ,value))
(:hidden
`(<input :type :text :name ,name :value ,value :style (css-attrib :display "none")))))))
(<input :class "plain-submit" :type :submit :value ""))))
(defmacro html-action-form (title-and-options lambda-list &body body)
(destructuring-bind (title
&key (action-link
`(page-action-link
(let ,(loop for p in lambda-list collect
`(,(force-first p) (or (alist-get all-http-params! ,(force-byte-vector (force-first p))
:test 'byte-vector=-fold-ascii-case)
,(second (force-list p)))))
,@body)))
(after-submit-js))
(force-list title-and-options)
(let ((body-ml
(loop for nv in lambda-list collect
(destructuring-bind (name &optional value &key (type '<input) reset)
(force-list nv)
(let ((name (force-byte-vector name)))
(when reset
(appendf after-submit-js `((setf (slot-value (this.elements.named-item ,(force-string name)) 'value) ,(if (eq reset t) nil reset)))))
(ecase type
(<input
`(<input :type :text :name ,name
,@(when value `(:value ,value))))
(<textarea
`(<textarea :name ,name ,value))
(:hidden
`(<input :type :text :name ,name :value ,value :style (css-attrib :display "none")))))))))
`(<form
:onsubmit (js-attrib (return (let ((async-submit-success (async-submit-form this))) ,@after-submit-js async-submit-success)))
:method :post
:action ,action-link
(<p
,title
,@body-ml
(<input :class "plain-submit" :type :submit :value ""))))))

(defun find-action (id)
(and id (find id (webapp-frame-var 'actions) :key 'action-id :test 'equalp)))

(defun action-respond-body (&key .id. .channels. .javascript. all-http-params)
(defun action-respond-body (&key .id. .javascript. all-http-params!)
(with-frame-site
(awhen (find-action .id.)
(funcall (action-func it) all-http-params))
(funcall (action-func it) all-http-params!))
(if .javascript.
(channel-respond-body (channel-string-to-states .channels.))
(webapp-respond-ajax all-http-params!)
(funcall (frame-current-page (webapp-frame))))))

(defun webapp-respond-ajax-body (all-http-params!)
(let ((channels (channel-string-to-states
(alist-get all-http-params! (force-byte-vector '.channels.)
:test 'byte-vector=-fold-ascii-case))))
(channel-respond-body channels :always-body t)))

(defun register-action-page ()
(defpage-lambda +action-page-name+ #'action-respond-body :defaulting-lambda-list (.id. .channels. .javascript. all-http-params)))
(defpage-lambda +action-page-name+ #'action-respond-body :defaulting-lambda-list (.id. .javascript. all-http-params!)))


(my-defun frame 'simple-channel-body-ml ()
Expand Down
8 changes: 4 additions & 4 deletions src/webapp/channel.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@

(defgeneric channel-update (channel subscriber-state))

(defun channel-respond-page (dispatcher con done path all-http-params)
(defun channel-respond-page (dispatcher con done path all-http-params!)
(declare (ignore dispatcher path))
(apply-page-call con 'channel-respond con done (.channels.)))
(apply-page-call (:con con :function 'channel-respond :create-frame nil) con done (.channels.)))

(defun channel-string-to-states (channels)
(let ((channel-states))
Expand All @@ -38,7 +38,7 @@
channels)
channel-states))

(defun channel-respond-body (channel-states)
(defun channel-respond-body (channel-states &key always-body)
(let (at-least-one)
(let ((sendbuf
(with-ml-output
Expand All @@ -50,7 +50,7 @@
(unquote (channel-state channel)))))
(output-raw-ml (channel-update channel state))))
(output-raw-ml (js-to-string (trigger-fetch-channels))))))
(when at-least-one
(when (or at-least-one always-body)
sendbuf))))

(defun channel-respond (con done &key .channels.)
Expand Down
3 changes: 3 additions & 0 deletions src/webapp/frame.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@
(defun find-frame (id)
(gethash id *frames*))

(defun-speedy webapp-frame-available-p ()
(and (boundp '*webapp-frame*) *webapp-frame*))

(defun webapp-frame (&rest args-for-make-frame)
(unless *webapp-frame*
(setf *webapp-frame* (apply 'make-frame args-for-make-frame)))
Expand Down
4 changes: 2 additions & 2 deletions src/webapp/js-library.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,6 @@
(defun trigger-fetch-channels ()
(ps:do-set-timeout (50)
(fetch-channels)))
#- (and)
(trigger-fetch-channels)))

(trigger-fetch-channels)))

Loading

0 comments on commit 98e0c92

Please sign in to comment.