Archived
1
0
Disbranĉigi 0

Sort messages in output by date

This commit is contained in:
Jaidyn Ann 2023-05-30 23:46:35 -05:00
parent 20b2c49660
commit dc14111285

View File

@ -16,8 +16,9 @@
;; ;;
(import scheme (import scheme
(chicken io) (chicken string) (chicken irregex) (chicken pretty-print) (chicken io) (chicken sort) (chicken string) (chicken irregex)
srfi-1 (chicken pretty-print)
srfi-1 srfi-19
(prefix chatdir chatdir:) (prefix chatdir chatdir:)
(prefix intarweb intarweb:) (prefix intarweb intarweb:)
(prefix spiffy spiffy:) (prefix spiffy spiffy:)
@ -61,6 +62,30 @@
(chatdir:channels irc-dir))))))) (chatdir:channels irc-dir)))))))
;; Returns all of a channel's messages — in alist format, with parsed datetimes.
(define (channel-messages irc-dir channel)
(map (lambda (msg-alist)
(let ([date-str (alist-ref 'user.chat.date (cdr msg-alist))])
(append
(list (car msg-alist))
(alist-update 'user.chat.date
(string->date date-str "~Y-~m-~dT~H:~M:~S~z")
(cdr msg-alist)))))
(map (lambda (message)
(chatdir:channel-message-get irc-dir channel message))
(chatdir:channel-messages irc-dir channel))))
;; Returns all of a channel's messages, sorted in order of datetime.
(define (channel-messages-sorted irc-dir channel)
(sort
(channel-messages irc-dir channel)
(lambda (a b)
(date>? (alist-ref 'user.chat.date (cdr a))
(alist-ref 'user.chat.date (cdr b))))))
;; Generate the HTML listing a room's chat messages. ;; Generate the HTML listing a room's chat messages.
(define (room-chat-html irc-dir channel) (define (room-chat-html irc-dir channel)
(html-from-template (html-from-template
@ -71,7 +96,7 @@
string-append "" string-append ""
(map (lambda (message) (map (lambda (message)
(room-chat-item-html irc-dir channel message)) (room-chat-item-html irc-dir channel message))
(chatdir:channel-messages (channel-messages-sorted
irc-dir irc-dir
(uri:uri-decode-string channel)))))))) (uri:uri-decode-string channel))))))))
@ -79,22 +104,27 @@
;; Generate the HTML for a specific message in a specific room. ;; Generate the HTML for a specific message in a specific room.
;; Used to substitute {{LIST_ITEMS}} in the room-chat template. ;; Used to substitute {{LIST_ITEMS}} in the room-chat template.
(define (room-chat-item-html irc-dir channel message) (define (room-chat-item-html irc-dir channel message)
(let ([message-alist (if (and (list? message)
(chatdir:channel-message-get irc-dir channel message)]) (string? (car message)))
(if (and (list? message-alist) (html-from-template
(string? (car message-alist))) "templates/room-chat-item.html"
(html-from-template `(("MESSAGE_SENDER"
"templates/room-chat-item.html" . ,(html-encode-string
`(("MESSAGE_SENDER" (alist-ref 'user.chat.sender (cdr message))))
. ,(html-encode-string ("MESSAGE_DATE"
(alist-ref 'user.chat.sender (cdr message-alist)))) . ,(html-encode-string
("MESSAGE_DATE" (date->string
. ,(html-encode-string (alist-ref 'user.chat.date (cdr message))
(alist-ref 'user.chat.date (cdr message-alist)))) "~Y-~m-~d")))
("MESSAGE_TEXT" ("MESSAGE_TIME"
. ,(html-encode-string . ,(html-encode-string
(car message-alist))))) (date->string
""))) (alist-ref 'user.chat.date (cdr message))
"~H:~M:~S")))
("MESSAGE_TEXT"
. ,(html-encode-string
(car message)))))
""))
;; Send response for a listing of joined rooms. ;; Send response for a listing of joined rooms.