Skip to content
This repository has been archived by the owner on Jan 21, 2022. It is now read-only.

Commit

Permalink
Generalizations for notifications (#174)
Browse files Browse the repository at this point in the history
* Added generalizations needed for adding notifications #37

- Made generalizations to toot and bylines to enable notifications.
- Fixed tl tests to account for the new function arguments.
- fixed whitespace
  • Loading branch information
alexjgriffith authored and jdenen committed Aug 10, 2018
1 parent 5617739 commit a7c5f0b
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 30 deletions.
56 changes: 36 additions & 20 deletions lisp/mastodon-tl.el
Original file line number Diff line number Diff line change
Expand Up @@ -328,8 +328,14 @@ E.g. this could return something like \"1 min ago\", \"yesterday\", etc.
TIME-STAMP is assumed to be in the past."
(car (mastodon-tl--relative-time-details timestamp current-time)))

(defun mastodon-tl--byline (toot)
"Generate byline for TOOT."
(defun mastodon-tl--byline (toot author-byline action-byline)
"Generate byline for TOOT.
AUTHOR-BYLINE is function for adding the author portion of
the byline that takes one variable.
ACTION-BYLINE is a function for adding an action, such as boosting
favouriting and following to the byline. It also takes a single function. By default
it is `mastodon-tl--byline-boosted'"
(let ((id (cdr (assoc 'id toot)))
(parsed-time (date-to-time (mastodon-tl--field 'created_at toot)))
(faved (equal 't (mastodon-tl--field 'favourited toot)))
Expand All @@ -343,8 +349,8 @@ TIME-STAMP is assumed to be in the past."
(when faved
(format "(%s) "
(propertize "F" 'face 'mastodon-boost-fave-face)))
(mastodon-tl--byline-author toot)
(mastodon-tl--byline-boosted toot)
(funcall author-byline toot)
(funcall action-byline toot)
" "
;; TODO: Once we have a view for toot (responses etc.) make
;; this a tab stop and attach an action.
Expand Down Expand Up @@ -535,26 +541,36 @@ message is a link which unhides/hides the main body."
(mastodon-tl--render-text content toot)
(mastodon-tl--media toot))))

(defun mastodon-tl--toot (toot)
"Display TOOT content and byline."
(insert
(concat
;; remove trailing whitespace
(defun mastodon-tl--insert-status (toot body author-byline action-byline)
"Display the content and byline of a timeline element.
(replace-regexp-in-string
"[\t\n ]*\\'" ""
(if (mastodon-tl--has-spoiler toot)
(mastodon-tl--spoiler toot)
(mastodon-tl--content toot)))
(mastodon-tl--byline toot)
"\n\n")))
BODY will form the section of the toot above the byline.
AUTHOR-BYLINE is an optional function for adding the author portion of
the byline that takes one variable. By default it is `mastodon-tl--byline-author'
ACTION-BYLINE is also an optional function for adding an action, such as boosting
favouriting and following to the byline. It also takes a single function. By default
it is `mastodon-tl--byline-boosted'"
(insert
body
(mastodon-tl--byline toot author-byline action-byline)
"\n\n"))

(defun mastodon-tl--toot(toot)
"Formats TOOT and insertes it into the buffer."
(mastodon-tl--insert-status
toot
(replace-regexp-in-string
"[\t\n ]*\\'" ""
(if (mastodon-tl--has-spoiler toot)
(mastodon-tl--spoiler toot)
(mastodon-tl--content toot)))
'mastodon-tl--byline-author
'mastodon-tl--byline-boosted))

(defun mastodon-tl--timeline (toots)
"Display each toot in TOOTS."
(mapc 'mastodon-tl--toot toots)
(goto-char (point-min))
(while (search-forward "\n\n\n | " nil t)
(replace-match "\n | "))
(when mastodon-tl--display-media-p
(mastodon-media--inline-images)))

Expand Down Expand Up @@ -653,10 +669,10 @@ webapp"
(toot (mastodon-tl--property 'toot-json))
(context (mastodon-http--get-json url)))
(when (member (cdr (assoc 'type toot)) '("reblog" "favourite"))
(setq toot (cdr(assoc 'status toot))))
(setq toot (cdr (assoc 'status toot))))
(if (> (+ (length (cdr (assoc 'ancestors context)))
(length (cdr (assoc 'descendants context))))
0)
0)
(with-output-to-temp-buffer buffer
(switch-to-buffer buffer)
(mastodon-mode)
Expand Down
43 changes: 33 additions & 10 deletions test/mastodon-tl-tests.el
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,9 @@ a string or a numeric."
(mock (format-time-string mastodon-toot-timestamp-format '(22782 21551)) => "2999-99-99 00:11:22")

(should (string= (substring-no-properties
(mastodon-tl--byline mastodon-tl-test-base-toot))
(mastodon-tl--byline mastodon-tl-test-base-toot
'mastodon-tl--byline-author
'mastodon-tl--byline-boosted))
"
| Account 42 (@acct42@example.space) 2999-99-99 00:11:22
------------")))))
Expand All @@ -272,7 +274,9 @@ a string or a numeric."
(mock (format-time-string mastodon-toot-timestamp-format '(22782 21551)) => "2999-99-99 00:11:22")

(should (string= (substring-no-properties
(mastodon-tl--byline mastodon-tl-test-base-toot))
(mastodon-tl--byline mastodon-tl-test-base-toot
'mastodon-tl--byline-author
'mastodon-tl--byline-boosted))
"
| Account 42 (@acct42@example.space) 2999-99-99 00:11:22
------------")))))
Expand All @@ -286,7 +290,10 @@ a string or a numeric."
(mock (date-to-time timestamp) => '(22782 21551))
(mock (format-time-string mastodon-toot-timestamp-format '(22782 21551)) => "2999-99-99 00:11:22")

(should (string= (substring-no-properties (mastodon-tl--byline toot))
(should (string= (substring-no-properties
(mastodon-tl--byline toot
'mastodon-tl--byline-author
'mastodon-tl--byline-boosted))
"
| (B) Account 42 (@acct42@example.space) 2999-99-99 00:11:22
------------")))))
Expand All @@ -300,7 +307,10 @@ a string or a numeric."
(mock (date-to-time timestamp) => '(22782 21551))
(mock (format-time-string mastodon-toot-timestamp-format '(22782 21551)) => "2999-99-99 00:11:22")

(should (string= (substring-no-properties (mastodon-tl--byline toot))
(should (string= (substring-no-properties
(mastodon-tl--byline toot
'mastodon-tl--byline-author
'mastodon-tl--byline-boosted))
"
| (F) Account 42 (@acct42@example.space) 2999-99-99 00:11:22
------------")))))
Expand All @@ -315,7 +325,10 @@ a string or a numeric."
(mock (date-to-time timestamp) => '(22782 21551))
(mock (format-time-string mastodon-toot-timestamp-format '(22782 21551)) => "2999-99-99 00:11:22")

(should (string= (substring-no-properties (mastodon-tl--byline toot))
(should (string= (substring-no-properties
(mastodon-tl--byline toot
'mastodon-tl--byline-author
'mastodon-tl--byline-boosted))
"
| (B) (F) Account 42 (@acct42@example.space) 2999-99-99 00:11:22
------------")))))
Expand All @@ -335,7 +348,10 @@ a string or a numeric."
(mock (date-to-time original-timestamp) => '(3 4))
(mock (format-time-string mastodon-toot-timestamp-format '(3 4)) => "original time")

(should (string= (substring-no-properties (mastodon-tl--byline toot))
(should (string= (substring-no-properties
(mastodon-tl--byline toot
'mastodon-tl--byline-author
'mastodon-tl--byline-boosted))
"
| Account 42 (@acct42@example.space) Boosted Account 43 (@acct43@example.space) original time
------------")))))
Expand All @@ -356,8 +372,10 @@ a string or a numeric."
(mock (date-to-time original-timestamp) => '(3 4))
(mock (format-time-string mastodon-toot-timestamp-format '(3 4)) => "original time")

(should (string= (substring-no-properties (mastodon-tl--byline toot))
"
(should (string= (substring-no-properties
(mastodon-tl--byline toot
'mastodon-tl--byline-author
'mastodon-tl--byline-boosted))"
| Account 42 (@acct42@example.space) Boosted Account 43 (@acct43@example.space) original time
------------")))))

Expand All @@ -376,7 +394,10 @@ a string or a numeric."
(mock (date-to-time original-timestamp) => '(3 4))
(mock (format-time-string mastodon-toot-timestamp-format '(3 4)) => "original time")

(should (string= (substring-no-properties (mastodon-tl--byline toot))
(should (string= (substring-no-properties
(mastodon-tl--byline toot
'mastodon-tl--byline-author
'mastodon-tl--byline-boosted))
"
| (B) (F) Account 42 (@acct42@example.space) Boosted Account 43 (@acct43@example.space) original time
------------")))))
Expand All @@ -390,7 +411,9 @@ a string or a numeric."
(mock (current-time) => '(22782 22000))
(mock (format-time-string mastodon-toot-timestamp-format '(22782 21551)) => "2999-99-99 00:11:22")

(let* ((formatted-string (mastodon-tl--byline mastodon-tl-test-base-toot))
(let* ((formatted-string (mastodon-tl--byline mastodon-tl-test-base-toot
'mastodon-tl--byline-author
'mastodon-tl--byline-boosted))
(timestamp-start (string-match "2999-99-99" formatted-string))
(properties (text-properties-at timestamp-start formatted-string)))
(should (equal '(22782 21551) (plist-get properties 'timestamp)))
Expand Down

0 comments on commit a7c5f0b

Please sign in to comment.