From 20b2c49660704757600758a9ade8e6bfa8b29dc0 Mon Sep 17 00:00:00 2001 From: Jaidyn Ann <10477760+JadedCtrl@users.noreply.github.com> Date: Tue, 30 May 2023 22:50:25 -0500 Subject: [PATCH] Per-room pages listing chat messages --- spidercat.scm | 68 +++++++++++++++++++++++++++++------ templates/room-chat-item.html | 1 + templates/room-chat.html | 13 +++++++ 3 files changed, 71 insertions(+), 11 deletions(-) create mode 100644 templates/room-chat-item.html create mode 100644 templates/room-chat.html diff --git a/spidercat.scm b/spidercat.scm index bfec856..2a26c77 100644 --- a/spidercat.scm +++ b/spidercat.scm @@ -25,7 +25,7 @@ ;; Santize text for placement in HTML. -(define (sanitize-html text) +(define (html-encode-string text) (irregex-replace/all "{" (spiffy:htmlize text) "{")) @@ -55,30 +55,72 @@ (map (lambda (room) (html-from-template "templates/room-list-item.html" - `(("ROOM_TITLE" . ,room) - ("ROOM_ID" . ,room) + `(("ROOM_TITLE" . ,(html-encode-string room)) + ("ROOM_ID" . ,(uri:uri-encode-string room)) ("LAST_MESSAGE" . "nekonata: Lorem ipso facto…")))) (chatdir:channels irc-dir))))))) +;; Generate the HTML listing a room's chat messages. +(define (room-chat-html irc-dir channel) + (html-from-template + "templates/room-chat.html" + `(("ROOM_TITLE" . ,(uri:uri-decode-string channel)) + ("LIST_ITEMS" + . ,(reduce-right + string-append "" + (map (lambda (message) + (room-chat-item-html irc-dir channel message)) + (chatdir:channel-messages + irc-dir + (uri:uri-decode-string channel)))))))) + + +;; Generate the HTML for a specific message in a specific room. +;; Used to substitute {{LIST_ITEMS}} in the room-chat template. +(define (room-chat-item-html irc-dir channel message) + (let ([message-alist + (chatdir:channel-message-get irc-dir channel message)]) + (if (and (list? message-alist) + (string? (car message-alist))) + (html-from-template + "templates/room-chat-item.html" + `(("MESSAGE_SENDER" + . ,(html-encode-string + (alist-ref 'user.chat.sender (cdr message-alist)))) + ("MESSAGE_DATE" + . ,(html-encode-string + (alist-ref 'user.chat.date (cdr message-alist)))) + ("MESSAGE_TEXT" + . ,(html-encode-string + (car message-alist))))) + ""))) + + ;; Send response for a listing of joined rooms. -(define (http-get-room irc-dir #!optional request continue) +(define (http-get-rooms-list irc-dir #!optional request path) (spiffy:send-response status: 'ok body: (room-listing-html irc-dir))) +(define (http-get-room-dir irc-dir #!optional request path) + (let ([channel (third path)]) + (spiffy:send-response status: 'ok + body: (room-chat-html irc-dir channel)))) + + ;; Send response for the / index. -(define (http-get-root #!optional irc-dir request continue) +(define (http-get-root #!optional irc-dir request path) (spiffy:send-response status: 'ok body: "