branch: elpa/mastodon
commit 0d1e75e7d50164cf18084ce58c84b7deac879f42
Author: marty hiatt <[email protected]>
Commit: marty hiatt <[email protected]>
add :silent arg for get-json/-async, make unread count silent. #249
---
lisp/mastodon-http.el | 24 +++++++++++++++---------
lisp/mastodon-notifications.el | 15 ++++++++++-----
lisp/mastodon-profile.el | 1 -
lisp/mastodon-tl.el | 21 ++++++++++++---------
lisp/mastodon-toot.el | 2 +-
5 files changed, 38 insertions(+), 25 deletions(-)
diff --git a/lisp/mastodon-http.el b/lisp/mastodon-http.el
index fc456925e5..16c9d787a7 100644
--- a/lisp/mastodon-http.el
+++ b/lisp/mastodon-http.el
@@ -323,30 +323,36 @@ JSON means send params as JSON data."
;; Asynchronous functions
-(defun mastodon-http--get-async (url &optional params callback &rest cbargs)
+(defun mastodon-http--get-async (url &optional params silent
+ callback &rest cbargs)
"Make GET request to URL.
Pass response buffer to CALLBACK function with args CBARGS.
-PARAMS is an alist of any extra parameters to send with the request."
+PARAMS is an alist of any extra parameters to send with the request.
+Optionally make the request SILENT."
(let ((url (mastodon-http--concat-params-to-url url params)))
(mastodon-http--authorized-request "GET"
- (url-retrieve url callback cbargs))))
+ (url-retrieve url callback cbargs silent))))
-(defun mastodon-http--get-response-async (url &optional params callback &rest
cbargs)
+(defun mastodon-http--get-response-async (url &optional params silent
+ callback &rest cbargs)
"Make GET request to URL. Call CALLBACK with http response and CBARGS.
-PARAMS is an alist of any extra parameters to send with the request."
+PARAMS is an alist of any extra parameters to send with the request.
+Optionally make the request SILENT."
(mastodon-http--get-async
url
- params
+ params silent
(lambda (status)
(when status ; for flakey servers
(apply callback (mastodon-http--process-response) cbargs)))))
-(defun mastodon-http--get-json-async (url &optional params callback &rest
cbargs)
+(defun mastodon-http--get-json-async (url &optional params silent
+ callback &rest cbargs)
"Make GET request to URL. Call CALLBACK with json-list and CBARGS.
-PARAMS is an alist of any extra parameters to send with the request."
+PARAMS is an alist of any extra parameters to send with the request.
+Optionally make the request SILENT."
(mastodon-http--get-async
url
- params
+ params silent
(lambda (status)
(when status ;; only when we actually get sth?
(apply callback (mastodon-http--process-json) cbargs)))))
diff --git a/lisp/mastodon-notifications.el b/lisp/mastodon-notifications.el
index 8f16b78aed..fb9ae58c5b 100644
--- a/lisp/mastodon-notifications.el
+++ b/lisp/mastodon-notifications.el
@@ -836,6 +836,7 @@ Status notifications are created when you call
(let* ((endpoint "notifications/unread_count")
(url (mastodon-http--api endpoint
(when mastodon-group-notifications "v2")))
+ ;; NB: sync request!:
(resp (mastodon-http--get-json url nil :silent)))
(alist-get 'count resp)))
@@ -847,9 +848,12 @@ Status notifications are created when you call
:style mastodon-notifications-alert-style
:continue t)
+(defvar mastodon-notifications-notify-shown)
+
(defun mastodon-notifications-notify (&optional count)
- "Send a desktop notification when we have unread notifications.
-Uses `notifications-notify'."
+ "Send a desktop notification when we have COUNT unread notifications.
+If COUNT is nil, fetch again from server.
+Uses `alert.el'."
(let ((count (or count (mastodon-notifications--get-unread-count))))
(when (and (> count 0)
(not mastodon-notifications-notify-shown))
@@ -1040,13 +1044,14 @@ Also nil the variable."
nil #'mastodon-notifications--update-check))))))
(defun mastodon-notifications--update-check ()
- "Function called by `mastodon-notifications--update-with-timer'.
-Calls `mastodon-tl--update'."
+ "Get unread notifications count from the server, asynchronously.
+Called by `mastodon-notifications--update-with-timer'.
+Callback is `mastodon-notifications--update-check-cb'."
(let* ((endpoint "notifications/unread_count")
(url (mastodon-http--api endpoint
(when mastodon-group-notifications "v2"))))
(mastodon-http--get-json-async
- url nil #'mastodon-notifications--update-check-cb)))
+ url nil :silent #'mastodon-notifications--update-check-cb)))
(defun mastodon-notifications--update-check-cb (resp)
"Callback functions for handling unread notifs count response RESP."
diff --git a/lisp/mastodon-profile.el b/lisp/mastodon-profile.el
index 8c947334cd..f95908dc0b 100644
--- a/lisp/mastodon-profile.el
+++ b/lisp/mastodon-profile.el
@@ -46,7 +46,6 @@
(autoload 'mastodon-auth--get-account-name "mastodon-auth.el")
(autoload 'mastodon-http--api "mastodon-http.el")
(autoload 'mastodon-http--get-json "mastodon-http.el")
-(autoload 'mastodon-http--get-json-async "mastodon-http.el")
(autoload 'mastodon-http--get-response "mastodon-http")
(autoload 'mastodon-http--patch "mastodon-http")
(autoload 'mastodon-http--patch-json "mastodon-http")
diff --git a/lisp/mastodon-tl.el b/lisp/mastodon-tl.el
index a5cbb13794..a3ef95937d 100644
--- a/lisp/mastodon-tl.el
+++ b/lisp/mastodon-tl.el
@@ -3342,11 +3342,12 @@ report the account for spam."
(mastodon-http--get-json url args)))
(defun mastodon-tl--more-json-async
- (endpoint id &optional params callback &rest cbargs)
+ (endpoint id &optional params silent callback &rest cbargs)
"Return JSON for timeline ENDPOINT before ID.
Then run CALLBACK with arguments CBARGS.
PARAMS is used to send any parameters needed to correctly update
-the current view."
+the current view.
+Optionally make the request SILENT."
(let* ((args `(("max_id" . ,(mastodon-tl--as-string id))))
(args (append args params))
(url (mastodon-http--api
@@ -3355,9 +3356,9 @@ the current view."
(string= endpoint "notifications"))
(string-suffix-p "search" endpoint))
"v2"))))
- (apply #'mastodon-http--get-json-async url args callback cbargs)))
+ (apply #'mastodon-http--get-json-async url args silent callback cbargs)))
-(defun mastodon-tl--more-json-async-offset (endpoint &optional params
+(defun mastodon-tl--more-json-async-offset (endpoint &optional params silent
callback &rest cbargs)
"Return JSON for ENDPOINT, using the \"offset\" query param.
This is used for pagination with endpoints that implement the
@@ -3366,7 +3367,8 @@ This is used for pagination with endpoints that implement
the
PARAMS are the update parameters, see
`mastodon-tl--update-params'. These (\"limit\" and \"offset\")
must be set in `mastodon-tl--buffer-spec' for pagination to work.
-Then run CALLBACK with arguments CBARGS."
+Then run CALLBACK with arguments CBARGS.
+Optionally make the request SILENT."
(let* ((params (or params (mastodon-tl--update-params)))
(limit (string-to-number
(alist-get "limit" params nil nil #'string=)))
@@ -3380,7 +3382,7 @@ Then run CALLBACK with arguments CBARGS."
"v2"))))
;; increment:
(setf (alist-get "offset" params nil nil #'string=) offset)
- (apply #'mastodon-http--get-json-async url params callback cbargs)))
+ (apply #'mastodon-http--get-json-async url params silent callback cbargs)))
(defun mastodon-tl--updated-json (endpoint id &optional params version)
"Return JSON for timeline ENDPOINT since ID.
@@ -3484,14 +3486,14 @@ and profile pages when showing followers or accounts
followed."
(mastodon-tl--search-buffer-p))
(mastodon-tl--more-json-async-offset
(mastodon-tl--endpoint)
- (mastodon-tl--update-params)
+ (mastodon-tl--update-params) nil
'mastodon-tl--more* (current-buffer) (point)))
(t ;; max_id paginate (timelines, items with ids/timestamps):
(let ((max-id (mastodon-tl--oldest-id))
(params (mastodon-tl--update-params)))
(mastodon-tl--more-json-async
(mastodon-tl--endpoint)
- max-id params
+ max-id params nil
'mastodon-tl--more*
(current-buffer) (point) nil max-id))))))
@@ -3784,7 +3786,8 @@ NO-BYLINE means just insert toot body, used for
announcements."
(if headers
#'mastodon-http--get-response-async
#'mastodon-http--get-json-async)
- url params 'mastodon-tl--init*
+ url params nil ;; not silent
+ 'mastodon-tl--init*
buffer endpoint update-function headers params hide-replies
instance no-byline)))
diff --git a/lisp/mastodon-toot.el b/lisp/mastodon-toot.el
index 463abe3496..68163d3736 100644
--- a/lisp/mastodon-toot.el
+++ b/lisp/mastodon-toot.el
@@ -352,7 +352,7 @@ property, and call BODY-FUN on them."
NO-TOOT means we are not calling from a toot buffer."
(mastodon-http--get-json-async
(mastodon-http--api "instance")
- nil
+ nil :silent
'mastodon-toot--get-max-toot-chars-callback no-toot))
(defun mastodon-toot--get-max-toot-chars-callback (json-response