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 +++++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 72 insertions(+), 16 deletions(-) (limited to 'lisp/mastodon-tl.el') 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." -- cgit v1.2.3