From 626e0e3ba4f435ea1c13507f2d4f66aee96aa8f8 Mon Sep 17 00:00:00 2001 From: Alexander Griffith Date: Thu, 11 May 2017 19:38:16 -0400 Subject: Fix for #84 changed more-json in mastodon-tl-tests to recieve an endpont more evocative names and fixed whitespace replaced remaining end eith endpoint-plist added basic unit tests for endpoint selection replaced equalp with equal in tests --- lisp/mastodon-tl.el | 88 ++++++++++++++++++++++++++++++++++++++--------- test/mastodon-tl-tests.el | 46 ++++++++++++++++++++++++- 2 files changed, 117 insertions(+), 17 deletions(-) diff --git a/lisp/mastodon-tl.el b/lisp/mastodon-tl.el index b00622b..2361b93 100644 --- a/lisp/mastodon-tl.el +++ b/lisp/mastodon-tl.el @@ -224,22 +224,72 @@ also render the html" (replace-regexp "\n\n\n | " "\n | " nil (point-min) (point-max)) (mastodon-media--inline-images)) -(defun mastodon-tl--more-json (timeline id) - "Return JSON for TIMELINE before ID." - (let ((url (mastodon-http--api (concat "timelines/" - timeline - "?max_id=" - (number-to-string id))))) +(defun mastodon-tl--get-endpoint (buffer-string) + "Match BUFFER-STRING against buffer-names. + +Returns the matching plist" + (let* ((mastodon-buffer-endpoints ;; can be made a global var + '((buffer-name "home" endpoint "timelines/home" + update-function mastodon-tl--timeline) + (buffer-name "local" endpoint "timelines/public?local=true" + update-function mastodon-tl--timeline) + (buffer-name "federated" endpoint "timelines/public" + update-function mastodon-tl--timeline) + (buffer-name "public?local=true" + endpoint "timelines/public?local=true" + update-function mastodon-tl--timeline) + (buffer-name "public" + endpoint "timelines/public" + update-function mastodon-tl--timeline) + (buffer-name "tag/.*" + endpoint (lambda (x) (concat "timelines/" x)) + update-function mastodon-tl--timeline + endpoint-function) + (buffer-name "notifications" + endpoint "notifications" + update-function + mastodon-notifications--notifications))) + (match-endpoint-string + (lambda (endpoint-plist) + (when (string-match + (plist-get endpoint-plist 'buffer-name) buffer-string) + (unless (functionp + (plist-get endpoint-plist 'update-function)) + (error + (format "%s not a function" + (plist-get endpoint-plist 'update-function)))) + (cond + ((stringp (plist-get endpoint-plist 'endpoint)) + endpoint-plist) + ((functionp (plist-get endpoint-plist 'endpoint)) + (plist-put endpoint-plist 'endpoint + (funcall (plist-get endpoint-plist 'endpoint) + buffer-string))) + (t (error + (format "%s not a string or function" + (plist-get endpoint-plist 'endpoint)))))))) + (endpoint (remove-if 'null + (mapcar match-endpoint-string + mastodon-buffer-endpoints)))) + (or (car endpoint) + (error "%s is not a valid mastodon endpoint" string)))) + +(defun mastodon-tl--more-json (endpoint id) + "Return JSON for timeline ENDPOINT before ID." + (let* ((url (mastodon-http--api (concat + endpoint + "?max_id=" + (number-to-string id))))) (mastodon-http--get-json url))) ;; TODO ;; Look into the JSON returned here by Local -(defun mastodon-tl--updated-json (timeline id) - "Return JSON for TIMELINE since ID." - (let ((url (mastodon-http--api (concat "timelines/" - timeline - "?since_id=" - (number-to-string id))))) +(defun mastodon-tl--updated-json (endpoint id) + "Return JSON for timeline ENDPOINT since ID." + (let ((url (mastodon-http--api (concat + endpoint + "?since_id=" + (number-to-string id))))) (mastodon-http--get-json url))) (defun mastodon-tl--property (prop &optional backward) @@ -284,13 +334,16 @@ Move forward (down) the timeline unless BACKWARD is non-nil." (interactive) (let* ((point-before (point)) (tl (mastodon-tl--timeline-name)) + (endpoint-plist (mastodon-tl--get-endpoint tl)) + (endpoint (plist-get endpoint-plist 'endpoint)) + (update-function (plist-get endpoint-plist 'update-function)) (id (mastodon-tl--oldest-id)) - (json (mastodon-tl--more-json tl id))) + (json (mastodon-tl--more-json endpoint id))) (when json (with-current-buffer (current-buffer) (let ((inhibit-read-only t)) (goto-char (point-max)) - (mastodon-tl--timeline json) + (funcall update-function json) (goto-char point-before) (mastodon-tl--goto-next-toot)))))) @@ -298,13 +351,16 @@ Move forward (down) the timeline unless BACKWARD is non-nil." "Update timeline with new toots." (interactive) (let* ((tl (mastodon-tl--timeline-name)) + (endpoint-plist (mastodon-tl--get-endpoint tl)) + (endpoint (plist-get endpoint-plist 'endpoint)) + (update-function (plist-get endpoint-plist 'update-function)) (id (mastodon-tl--newest-id)) - (json (mastodon-tl--updated-json tl id))) + (json (mastodon-tl--updated-json endpoint id))) (when json (with-current-buffer (current-buffer) (let ((inhibit-read-only t)) (goto-char (point-min)) - (mastodon-tl--timeline json)))))) + (funcall update-function json)))))) (defun mastodon-tl--get (timeline) "Display TIMELINE in buffer." diff --git a/test/mastodon-tl-tests.el b/test/mastodon-tl-tests.el index a91d6d5..3fb2c3b 100644 --- a/test/mastodon-tl-tests.el +++ b/test/mastodon-tl-tests.el @@ -101,7 +101,7 @@ (let ((mastodon-instance-url "https://instance.url")) (with-mock (mock (mastodon-http--get-json "https://instance.url/api/v1/timelines/foo?max_id=12345")) - (mastodon-tl--more-json "foo" 12345)))) + (mastodon-tl--more-json "timelines/foo" 12345)))) (ert-deftest mastodon-tl--byline-regular () "Should format the regular toot correctly." @@ -236,3 +236,47 @@ | (B) (F) Account 42 (@acct42@example.space) Boosted Account 43 (@acct43@example.space) original time ------------"))))) +(ert-deftest mastodon-tl--endpoint-notifications () + "Should return the appropriate endpoint string and update function format + +notifications." + (should + (and + (equal + 'mastodon-notifications--notifications + (plist-get (mastodon-tl--get-endpoint "notifications") + 'update-function)) + (equal + "notifications" + (plist-get (mastodon-tl--get-endpoint "notifications") + 'endpoint))))) + +(ert-deftest mastodon-tl--endpoint-tag () + "Should return the appropriate endpoint string and update function for + +tags." + (should + (and + (equal + 'mastodon-tl--timeline + (plist-get (mastodon-tl--get-endpoint "tag/test") + 'update-function)) + (equal + "timelines/tag/test" + (plist-get (mastodon-tl--get-endpoint "tag/test") + 'endpoint))))) + +(ert-deftest mastodon-tl--endpoint-local () + "Should return the appropriate endpoint string and update function for + +the local timeline." + (should + (and + (equal + 'mastodon-tl--timeline + (plist-get (mastodon-tl--get-endpoint "public?local=true") + 'update-function)) + (equal + "timelines/public?local=true" + (plist-get (mastodon-tl--get-endpoint "public?local=true") + 'endpoint))))) -- cgit v1.2.3