aboutsummaryrefslogtreecommitdiff
path: root/lisp/mastodon-tl.el
diff options
context:
space:
mode:
Diffstat (limited to 'lisp/mastodon-tl.el')
-rw-r--r--lisp/mastodon-tl.el34
1 files changed, 30 insertions, 4 deletions
diff --git a/lisp/mastodon-tl.el b/lisp/mastodon-tl.el
index 130b01f..6f53f93 100644
--- a/lisp/mastodon-tl.el
+++ b/lisp/mastodon-tl.el
@@ -5,7 +5,7 @@
;; Marty Hiatt <martianhiatus@riseup.net>
;; Maintainer: Marty Hiatt <martianhiatus@riseup.net>
;; Version: 1.0.0
-;; Package-Requires: ((emacs "27.1"))
+;; Package-Requires: ((emacs "27.1") (ts "0.3"))
;; Homepage: https://codeberg.org/martianh/mastodon.el
;; This file is not part of GNU Emacs.
@@ -32,6 +32,7 @@
;;; Code:
(require 'shr)
+(require 'ts)
(require 'thingatpt) ; for word-at-point
(require 'time-date)
(require 'cl-lib)
@@ -946,6 +947,10 @@ this just means displaying toot client."
(defun mastodon-tl--get-poll (toot)
"If TOOT includes a poll, return it as a formatted string."
(let* ((poll (mastodon-tl--field 'poll toot))
+ (expiry (mastodon-tl--field 'expires_at poll))
+ (expired-p (if (eq (mastodon-tl--field 'expired poll) :json-false) nil t))
+ (multi (mastodon-tl--field 'multiple poll))
+ (vote-count (mastodon-tl--field 'voters_count poll))
(options (mastodon-tl--field 'options poll))
(option-titles (mapcar (lambda (x)
(alist-get 'title x))
@@ -958,20 +963,41 @@ this just means displaying toot client."
(concat "\nPoll: \n\n"
(mapconcat (lambda (option)
(progn
- (format "Option %s: %s%s [%s votes].\n"
+ (format "%s: %s%s%s\n"
(setq option-counter (1+ option-counter))
- (alist-get 'title option)
+ (propertize (alist-get 'title option)
+ 'face 'success)
(make-string
(1+
(- (length longest-option)
(length (alist-get 'title
option))))
?\ )
- (alist-get 'votes_count option))))
+ ;; TODO: disambiguate no votes from hidden votes
+ (format "[%s votes]" (or (alist-get 'votes_count option)
+ "0")))))
options
"\n")
+ "\n"
+ (propertize (format "%s people | " vote-count)
+ 'face 'font-lock-comment-face)
+ (let ((str (if expired-p
+ "Poll expired."
+ (mastodon-tl--format-poll-expiry expiry))))
+ (propertize str 'face 'font-lock-comment-face))
"\n")))
+(defun mastodon-tl--format-poll-expiry (timestamp)
+ "Convert poll expiry TIMESTAMP into a descriptive string."
+ (let ((parsed (ts-human-duration
+ (ts-diff (ts-parse timestamp) (ts-now)))))
+ (cond ((> (plist-get parsed :days) 0)
+ (format "%s days, %s hours left" (plist-get parsed :days) (plist-get parsed :hours)))
+ ((> (plist-get parsed :hours) 0)
+ (format "%s hours, %s minutes left" (plist-get parsed :hours) (plist-get parsed :minutes)))
+ ((> (plist-get parsed :minutes) 0)
+ (format "%s minutes left" (plist-get parsed :minutes))))))
+
(defun mastodon-tl--poll-vote (option)
"If there is a poll at point, prompt user for OPTION to vote on it."
(interactive