-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathgh-notify.el
1427 lines (1235 loc) · 56.3 KB
/
gh-notify.el
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
;;; gh-notify.el --- A veneer for Magit/Forge GitHub notifications -*- lexical-binding: t -*-
;; Copyright (C) 2021 [email protected]
;; 2020 [email protected]
;;
;; All rights reserved
;; Modified: 2024-04-10
;; Version: 2.1.0
;; Author: Bas Alberts <[email protected]>
;; xristos <[email protected]>
;;
;; Maintainer: Bas Alberts <[email protected]>
;; URL: https://github.com/anticomputer/gh-notify
;; Package-Requires: ((emacs "29.1") (magit "3.3.0") (forge "0.4.0"))
;; Keywords: comm
;; Redistribution and use in source and binary forms, with or without
;; modification, are permitted provided that the following conditions
;; are met:
;;
;; * Redistributions of source code must retain the above copyright
;; notice, this list of conditions and the following disclaimer.
;;
;; * Redistributions in binary form must reproduce the above
;; copyright notice, this list of conditions and the following
;; disclaimer in the documentation and/or other materials
;; provided with the distribution.
;;
;; THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
;; AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
;; IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
;; ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
;; LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
;; CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
;; SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
;; INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
;; CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
;; ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
;; POSSIBILITY OF SUCH DAMAGE.
;; This project includes code modified from:
;;
;; Magit/Forge (https://github.com/magit/forge)
;; Copyright (C) 2018-2021 Jonas Bernoulli
;;
;; Magit/Forge modifications are subject to the following license terms:
;;
;; Forge is free software; you can redistribute it and/or modify it
;; under the terms of the GNU General Public License as published by
;; the Free Software Foundation; either version 3, or (at your option)
;; any later version.
;;
;; Forge is distributed in the hope that it will be useful, but WITHOUT
;; ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
;; or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
;; License for more details.
;;
;; You should have received a copy of the GNU General Public License
;; along with Forge. If not, see http://www.gnu.org/licenses.
;; This project includes code modified from:
;;
;; chrome.el (https://github.com/anticomputer/chrome.el)
;; Copyright (C) 2020 [email protected]
;; 2020 [email protected]
;;
;; More specifically it repurposes the text filtering and rendering engine
;; developed by Xristos <[email protected]> for chrome.el.
;;
;; All his original author credits and licensing terms apply.
;;; Commentary:
;; This is gh-notify: A thin ui veneer on top of Magit/Forge porcelain for juggling
;; large amounts of GitHub notifications.
;;
;; It provides a more efficient interface to the Magit/Forge notification database
;; suited for rapid searching/narrow/filter based workflows. It also improves on
;; Magit/Forge's default notification fetching behavior by introducing support for
;; incremental notification fetching, which is a must for interactive notification
;; queue workflow iterations.
;;
;; This code should be plug and play if you already have Magit/Forge set up and have
;; fetched notifications for your GitHub account at least once. If not, please see:
;; https://magit.vc/manual/forge.html to get started.
;;
;; Note: this requires the latest versions of magit/forge to be installed from melpa
;; as per Feb 18, 2021.
;;
;;; Usage:
;;
;; M-x gh-notify
;;
;; Please see README.org for documentation.
;;; Code:
(require 'magit)
(require 'forge)
(require 'iso8601)
(require 'url)
(require 'json)
(require 'subr-x)
(require 'cl-lib)
(require 'auth-source)
(require 'url-util)
(defgroup gh-notify nil
"GitHub magit/forge notifications control."
:group 'comm)
(defface gh-notify-notification-filter-face
'((((class color) (background dark)) (:foreground "#aaffaa"))
(((class color) (background light)) (:foreground "#5faf00")))
"Face used to display current filter."
:group 'gh-notify)
(defface gh-notify-notification-marked-face
'((((class color) (background dark)) (:foreground "#ffaaff"))
(((class color) (background light)) (:foreground "#d70008")))
"Marked face."
:group 'gh-notify)
(defface gh-notify-notification-unread-face
'((((class color) (background dark)) (:weight ultra-bold))
(((class color) (background light)) (:weight ultra-bold)))
"Unread face."
:group 'gh-notify)
(defface gh-notify-notification-repo-face
'((((class color) (background dark)) (:weight ultra-light))
(((class color) (background light)) (:weight ultra-light)))
"Repo face."
:group 'gh-notify)
(defface gh-notify-notification-reason-face
'((((class color) (background dark)) (:foreground "#aaffaa"))
(((class color) (background light)) (:background "#5faf00")))
"Reason face."
:group 'gh-notify)
(defface gh-notify-notification-issue-face
'((((class color) (background dark)) (:foreground "#ff9999"))
(((class color) (background light)) (:background "#ff9999")))
"Issue face."
:group 'gh-notify)
(defface gh-notify-notification-pr-face
'((((class color) (background dark)) (:foreground "#ffff99"))
(((class color) (background light)) (:background "#ffff99")))
"PR face."
:group 'gh-notify)
(defvar gh-notify-render-function #'gh-notify-render-notification
"Function that renders a notification into a string for display.
The function must accept one argument, an gh-notify-notification instance,
and return a string that must not span more than one line.")
(defvar gh-notify-limit-function #'gh-notify-limit-notification
"Function that limits visible notifications based on certain criteria.
Function must accept one argument, an gh-notify-notification instance, and
return t if the notification is included in the limit, nil otherwise.")
(defvar gh-notify-filter-function #'gh-notify-filter-notification
"Function that filters visible notifications based on a user-typed regexp.
Function must accept one argument, gh-notify-notification instance, and
return t if the notification passes the filter, nil otherwise. The current
filter can be retrieved by calling `gh-notify-active-filter'.")
(defvar gh-notify-show-timing t
"Measure and display elapsed time after every operation.
This can be toggled by `gh-notify-toggle-timing'.")
(defvar gh-notify-show-state nil
"Show open/closed/merged state by default.
This requires an additional forge db query for every notification and makes
inits/refreshes SIGNIFICANTLY less snappy. Disabled by default and recommended
to use `gh-notify-display-state' via a keybinding instead.")
(defvar gh-notify-default-view :title
"Show notification titles when equal to :title, URLs otherwise.
This can be toggled by `gh-notify-toggle-url-view'.")
(defvar gh-notify-reason-limit :all
"Default display limit.")
(defvar gh-notify-default-repo-limit '()
"List of default repo limits.
Limits are in the form \"owner/repo\".
This will be your repo limit reset state to the exclusion of anything else.
and should only be used if you have a lot of permanent-noise from repositories
you do not care about for the long term.
Most people will want to keep this list empty and use
`gh-notify-exclude-repo-limit' instead.")
(defvar gh-notify-exclude-repo-limit '()
"List of repos to exclude from notifications display.
Repos are in the form \"owner/repo\".
Use this to muzzle specific repos that you want to silence across sessions.")
(defvar gh-notify-smokescreen-path "~/.gh-notify-smokescreen"
"The default path for the magit `forge-visit' smokescreen repo.")
(defvar gh-notify-redraw-on-visit t
"Automatically redraw notifications on `forge-visit'.
If you prefer to manually refresh notification display state after visits, set
this to nil.")
(cl-defstruct (gh-notify-notification
(:constructor gh-notify-notification-create)
(:copier nil))
(forge-obj nil :read-only nil)
(topic-obj nil :read-only nil)
(id nil :read-only t)
(type nil :read-only t)
(topic nil :read-only t)
(number nil :read-only t)
(repo-id nil :read-only t)
(repo nil :read-only t)
(unread nil :read-only nil)
(status nil :read-only nil)
(updated nil :read-only t)
(ts nil :read-only t)
(date nil :read-only t)
(reason nil :read-only t)
(url nil :read-only t)
(title nil :read-only t)
(state nil :read-only t)
is-marked
line)
;;;
;;; Internal API
;;;
(defvar gh-notify--current-buffer nil
"We have to play weird callback magic with Forge with buffer-local capabilities.")
(defvar-local gh-notify--repo-limit '()
"Repo filter list.")
(defvar-local gh-notify--unread-limit nil
"State limit.")
(defvar-local gh-notify--type-limit nil
"Type limit.")
(defvar-local gh-notify--repo-index nil
"Hash table that contains indexed `gh-notify' notifications.
Keys are repos, strings of form \"owner/repo\". Values are conses of form:
(notification-count . notification-list)")
(defun gh-notify--reindex-notifications (notifications)
"Index NOTIFICATIONS into `gh-notify--repo-index'.
NOTIFICATIONS must be an alist as returned from `gh-notify-get-notifications'."
(clrhash gh-notify--repo-index)
(cl-loop
for (repo . notification-data) in notifications
for repo-id = (format "%s/%s" (oref repo owner) (oref repo name))
for notification-count = 0
for process-notifications = nil
do
(cl-loop
for index from 0
for forge-notification in notification-data
;; get timestamp as an emacs time value to juggle
for ts = (encode-time (iso8601-parse (oref forge-notification updated)))
for date = (format-time-string "%F" ts) ; use local time on our end for display
for type = (oref forge-notification type)
for topic-obj = (pcase type
('issue
(forge-get-issue (oref forge-notification topic)))
('pullreq
(forge-get-pullreq (oref forge-notification topic))))
for status = (when topic-obj (oref topic-obj status))
for number = (when topic-obj (oref topic-obj number))
for id = (oref forge-notification id)
for reason = (oref forge-notification reason)
for updated = (oref forge-notification updated)
for topic = (oref forge-notification topic)
for url = (oref forge-notification url)
for title = (oref forge-notification title)
for state = (when gh-notify-show-state
(gh-notify--get-topic-state
(oref forge-notification type) repo
(oref forge-notification topic)))
do
(let* ((notification
(gh-notify-notification-create
;; retain the obj ref for db interactions
:forge-obj forge-notification
:topic-obj topic-obj
;; yank all the forge crud for convenience
:id id
:reason reason
:updated updated
:topic topic
:number number
:type type
:repo-id repo-id
:repo repo
;; unread-p changed to a topic status scheme in db version >= 11
;; https://github.com/magit/forge/blob/99d319823719339e0c324ad3e9f78564865ec07a/lisp/forge-db.el#L471
:unread (eq status 'unread)
:status status
:url url
:title title
;; we use this for an accurate sort
:ts ts
:date date
:state state)))
(push notification process-notifications))
finally (cl-incf notification-count index))
;; A hash table indexed by repo-id containing all notifications
(setf (gethash repo-id gh-notify--repo-index)
;; sort notifications by timestamp and then reverse for display
(cons notification-count
(cl-sort process-notifications
(lambda (a b) (not (time-less-p a b)))
:key #'gh-notify-notification-ts)))))
(defvar-local gh-notify--visible-notifications nil)
(defvar-local gh-notify--marked-notifications '())
(defvar-local gh-notify--total-notification-count 0)
(defun gh-notify--init-caches ()
"Init caches."
(setq gh-notify--repo-index (make-hash-table :test 'equal)
gh-notify--visible-notifications (make-hash-table)))
(defvar-local gh-notify--start-time nil)
(defvar-local gh-notify--elapsed-time nil)
(defun gh-notify--start-timer ()
"Start timer."
(unless gh-notify--start-time
(setq gh-notify--start-time (current-time))))
(defun gh-notify--stop-timer ()
"Stop timer."
(when gh-notify--start-time
(setq gh-notify--elapsed-time
(float-time (time-subtract
(current-time)
gh-notify--start-time))
gh-notify--start-time nil)))
(defvar-local gh-notify--header-update nil)
(cl-defmacro gh-notify--with-timing (&body body)
"Time BODY."
(declare (indent defun))
`(unwind-protect
(progn
(gh-notify--start-timer)
,@body)
(gh-notify--stop-timer)
(setq gh-notify--header-update t)))
(defun gh-notify--message (format-string &rest args)
"Message ARGS as FORMAT-STRING."
(let ((message-truncate-lines t))
(message "gh-notify: %s" (apply #'format format-string args))))
;;;
;;; Filtering
;;;
(defvar-local gh-notify--active-filter nil)
(defvar-local gh-notify--last-notification nil)
(defvar-local gh-notify--global-ts-sort t)
(defsubst gh-notify--goto-line (line)
"Goto LINE."
(goto-char (point-min))
(forward-line (1- line)))
(defsubst gh-notify--render-notification (notification &optional skip-goto)
"Render NOTIFICATION optionally SKIP-GOTO."
(unless skip-goto (gh-notify-goto-notification notification))
(delete-region (line-beginning-position) (line-end-position))
(insert (funcall gh-notify-render-function notification)))
(defsubst gh-notify--limit-notification (notification)
"Limit NOTIFICATION."
(funcall gh-notify-limit-function notification))
(defsubst gh-notify--filter-notification (notification)
"Filter NOTIFICATION."
(funcall gh-notify-filter-function notification))
(defun gh-notify--filter-notifications ()
"Filter notifications."
(when-let ((current-notification (gh-notify-current-notification)))
(setq gh-notify--last-notification current-notification))
(when (> (buffer-size) 0)
(let ((inhibit-read-only t))
(erase-buffer))
(clrhash gh-notify--visible-notifications))
;; resorting * every time we re-filter is not the most optimal of things :P
(gh-notify--with-timing
(cl-loop
for repo-id being the hash-keys of gh-notify--repo-index
for repo-notifications = (cdr (gethash repo-id gh-notify--repo-index))
with line = 1
with ts-sorted-notifications = '()
do
;; collect all notifications into a single list ... concat is faster here
(setq ts-sorted-notifications
(cl-concatenate 'list ts-sorted-notifications repo-notifications))
finally do
(progn
;; by default notifications are grouped and sorted by their repo blocks
;; but this overrides that behavior and lets you re-sort * by timestamp
(when gh-notify--global-ts-sort
(setq ts-sorted-notifications
(cl-sort ts-sorted-notifications
(lambda (a b) (not (time-less-p a b)))
:key #'gh-notify-notification-ts)))
;; filter as a sorted whole list instead of per-repo chunks
(cl-loop
for notification in ts-sorted-notifications do
(progn
;; Matching
(if (and (gh-notify--limit-notification notification)
(gh-notify--filter-notification notification))
;; Matches filter+limit
(let ((inhibit-read-only t))
(setf (gh-notify-notification-line notification) line
(gethash line gh-notify--visible-notifications) notification
line (1+ line))
(gh-notify--render-notification notification t)
(insert "\n"))
;; Doesn't match filter/limit
(setf (gh-notify-notification-line notification) nil))))
;; After all notifications have been filtered, determine where to set point
(when (> line 1)
;; Previously selected notification if it's still visible
(if-let ((last-notification gh-notify--last-notification)
(last-line (gh-notify-notification-line last-notification)))
(gh-notify-goto-notification last-notification)
(goto-char (point-min)))))))
(force-mode-line-update))
;;;
;;; Header
;;;
(defvar-local gh-notify--header-function #'gh-notify--header
"Function that returns a string for notification view header line.")
(defun gh-notify--header-1 ()
"Generate string for notification view header line."
(let* ((visible-notifications (hash-table-count gh-notify--visible-notifications))
(total-repos (hash-table-count gh-notify--repo-index)))
(cl-flet ((align (width str)
(let ((spec (format "%%%ds" width)))
(format spec str)))
(size10 (x) (if (= x 0) 1 (1+ (floor (log x 10))))))
(concat
(align (+ 1 (* 2 (size10 gh-notify--total-notification-count)))
(propertize (format "%s/%s" visible-notifications gh-notify--total-notification-count)
'help-echo "Visible / total notifications"))
" "
(align (size10 gh-notify--total-notification-count)
(propertize (int-to-string (length gh-notify--marked-notifications))
'help-echo "Marked notifications"
'face 'gh-notify-notification-marked-face))
" "
(align (1+ (* 2 (size10 total-repos)))
(propertize (format "(%s)" total-repos)
'help-echo "Total repos"))
" "
(format "by: %s " (if gh-notify--global-ts-sort "date" "repo"))
(when gh-notify--unread-limit
(format "%s " gh-notify--unread-limit))
(when gh-notify--type-limit (format "type: %s " gh-notify--type-limit))
(format "reason: %s " gh-notify-reason-limit)
;; if it's active, you already know which repos are in the filter, if not you don't care
(when gh-notify--repo-limit ":repo ")
(when gh-notify-show-timing
(propertize (format " %.4fs " gh-notify--elapsed-time)
'help-echo "Elapsed time for last operation"))
(when-let ((filter (gh-notify-active-filter)))
(format "Search: %s"
(propertize filter
'help-echo "Search filter"
'face 'gh-notify-notification-filter-face)))))))
(defvar-local gh-notify--header-cache nil)
(defun gh-notify--header ()
"Return string for notification view header line.
If a previously cached string is still valid, it is returned.
Otherwise, a new string is generated and returned by calling
`gh-notify--header-1'."
(if (and (null gh-notify--header-update)
(eql (car gh-notify--header-cache) (buffer-modified-tick)))
(cdr gh-notify--header-cache)
(let ((header (gh-notify--header-1)))
(prog1 header
(setq gh-notify--header-cache (cons (buffer-modified-tick) header)
gh-notify--header-update nil)))))
;;;
;;; Major mode
;;;
(defvar gh-notify-mode-map
;; Override self-insert-command with fallback to global-map
(let* ((map (make-keymap))
(prefix-map (make-sparse-keymap))
(char-table (cl-second map)))
;; Rebind keys that were bound to self-insert-command
(map-keymap
(lambda (event def)
(when (eq def 'self-insert-command)
(set-char-table-range
char-table event 'gh-notify--self-insert-command)))
global-map)
;; Standard bindings
(define-key map (kbd "DEL") 'gh-notify--self-insert-command)
(define-key map (kbd "C-c C-l") 'gh-notify-retrieve-notifications)
(define-key map (kbd "C-c C-k") 'gh-notify-reset-filter)
(define-key map (kbd "C-c C-t") 'gh-notify-toggle-timing)
(define-key map (kbd "C-c C-w") 'gh-notify-copy-url)
(define-key map (kbd "C-c C-s") 'gh-notify-display-state)
(define-key map (kbd "C-c C-i") 'gh-notify-ls-issues-at-point) ; all on prefix, open by default
(define-key map (kbd "C-c C-p") 'gh-notify-ls-pullreqs-at-point) ; all on prefix, open by default
(define-key map (kbd "G") 'gh-notify-forge-refresh)
(define-key map (kbd "RET") 'gh-notify-visit-notification) ; browse-url on prefix
(define-key map (kbd "C-c C-v") 'gh-notify-forge-visit-repo-at-point)
(define-key map (kbd "M-m") 'gh-notify-mark-notification)
(define-key map (kbd "M-M") 'gh-notify-mark-all-notifications)
(define-key map (kbd "M-u") 'gh-notify-unmark-notification)
(define-key map (kbd "M-U") 'gh-notify-unmark-all-notifications)
(define-key map (kbd "C-<up>") 'previous-line)
(define-key map (kbd "C-<down>") 'next-line)
(define-key map (kbd "\\") 'gh-notify-toggle-url-view)
;; Prefix bindings
(define-key map (kbd "/") prefix-map)
;; toggle date/repo sort under this prefix-map for better flow
(define-key prefix-map (kbd "d") 'gh-notify-toggle-global-ts-sort) ; date/repo sort toggle
;; state (read/unread) limit control
(define-key prefix-map (kbd "u") 'gh-notify-limit-unread) ; resets unread limit on prefix
;; repo limit control
(define-key prefix-map (kbd "'") 'gh-notify-limit-repo) ; pushes by default, pops on prefix
(define-key prefix-map (kbd "\"") 'gh-notify-limit-repo-none) ; resets to default repo limit
;; type limit control
(define-key prefix-map (kbd "p") 'gh-notify-limit-pr) ; resets type limit on prefix
(define-key prefix-map (kbd "i") 'gh-notify-limit-issue) ; resets type limit on prefix
;; reason limit control
(define-key prefix-map (kbd "*") 'gh-notify-limit-marked)
(define-key prefix-map (kbd "a") 'gh-notify-limit-assign)
(define-key prefix-map (kbd "y") 'gh-notify-limit-author)
(define-key prefix-map (kbd "m") 'gh-notify-limit-mention)
(define-key prefix-map (kbd "t") 'gh-notify-limit-team-mention)
(define-key prefix-map (kbd "s") 'gh-notify-limit-subscribed)
(define-key prefix-map (kbd "c") 'gh-notify-limit-comment)
(define-key prefix-map (kbd "r") 'gh-notify-limit-review-requested)
(define-key prefix-map (kbd "/") 'gh-notify-limit-none) ; resets reason limit
map)
"Keymap for `gh-notify-mode'.")
(defun gh-notify--self-insert-command ()
"Insert command."
(interactive)
(let ((event last-input-event)
updated)
(cond ((characterp event)
(if (and (= 127 event)
(not (display-graphic-p)))
(pop gh-notify--active-filter)
(push event gh-notify--active-filter))
(setq updated t))
((eql event 'backspace)
(pop gh-notify--active-filter)
(setq updated t))
(t (gh-notify--message "Unknown event %s" event)))
(when updated (gh-notify--filter-notifications))))
(defun gh-notify-mode ()
"Major mode for manipulating GitHub notifications through Magit/Forge.
\\<gh-notify-mode-map>
Notifications are retrieved from Magit/Forge and displayed in an Emacs buffer,
one notification per line. Display takes place in date/repo or repo/date
ordered fashion.
Notifications can be further filtered in realtime by a user-specified regular
expression and limited by certain criteria described below. This mode tries to
remember point so that it keeps its associated notification selected across
filtering/limiting operations, assuming the notification is visible.
To minimize the feedback loop, this mode does not use the minibuffer for input
\(e.g. when typing a filter regular expression). You can start typing
immediately and the filter updates, visible on the header line.
Other than regular keys being bound to `gh-notify--self-insert-command', the
following commands are available:
Type \\[gh-notify-visit-notification] to switch to notification at point in
magit/forge. With a prefix argument, switch to the topic associated to
notification through `browse-url'.
Type \\[gh-notify-retrieve-notifications] to retrieve local notifications from magit/forge.
Type \\[gh-notify-forge-refresh] to retrieve new remote notifications from GitHub.
Type \\[gh-notify-reset-filter] to kill the current Title/URL filter.
Type \\[gh-notify-toggle-url-view] to toggle notifications being shown as titles or API URLs.
Type \\[gh-notify-toggle-timing] to toggle timing information on the header line.
Type \\[gh-notify-copy-url] to copy API URL belonging to notification at point.
Type \\[gh-notify-ls-issues-at-point] to visit any other open issue associated
to the repo of the notification at point. With a prefix any of all issues
associated to the repo of the notification at point.
Type \\[gh-notify-ls-pullreqs-at-point] to visit any other open pull request
associated to the repo of the notification at point. With a prefix any of all
issues associated to the repo of the notification at point.
Limiting notifications:
Gh-notify operates on four layers of result limiting, a read-state limit, a
type limit, repo limit and a reason limit.
These are applied in repo -> state -> type -> reason order, which is generally
what you want. This allows you to intuitively add and remove limits. Repo
narrows to repo scope, state toggles for unread/read, type narrows for
notification type (issue, pullreq) and finally reason narrows on the reason
for the notification.
Repo limits:
Type \\[gh-notify-limit-repo] to add a repo to the repo limit. With a prefix argument, remove
a repo from the repo limit.
Type \\[gh-notify-limit-repo-none] to reset the repo limit to the default limit.
State limits:
Type \\[gh-notify-limit-unread] to limit to unread notifications. With a prefix argument,
remove unread limit.
Reason limits:
Independently from the repo limit are the various reason limits. These
correlate to the various notification reason states that may be associated
with a GitHub notification.
Type \\[gh-notify-limit-issue] to limit to issue notifications.
Type \\[gh-notify-limit-pr] to limit to pull request notifications.
Type \\[gh-notify-limit-assign] to limit to assign notifications.
Type \\[gh-notify-limit-author] to limit to author notifications.
Type \\[gh-notify-limit-mention] to limit to mention notifications.
Type \\[gh-notify-limit-team-mention] to limit to team-mention notifications.
Type \\[gh-notify-limit-subscribed] to limit to subscribed notifications.
Type \\[gh-notify-limit-comment] to limit to comment notifications.
Type \\[gh-notify-limit-review-requested] to limit to review-requested notifications.
Type \\[gh-notify-limit-none] to remove any active reason limit.
Type \\[gh-notify-limit-marked] to only show marked notifications.
Type \\[gh-notify-limit-none] to remove the current limit and show all notifications.
Marking:
Type \\[gh-notify-mark-notification] to mark notification at point.
Type \\[gh-notify-unmark-notification] to unmark notification at point.
Type \\[gh-notify-mark-all-notifications] to mark all notifications currently visible in Emacs. If
there is a region, only mark notifications in region.
Type \\[gh-notify-unmark-all-notifications] to unmark all notifications currently visible in Emacs.
If there is a region, only unmark notifications in region.
Note: marking support is currently moot, but will be used to support bulk
actions.
\\{gh-notify-mode-map}"
(interactive)
(kill-all-local-variables)
(use-local-map gh-notify-mode-map)
(font-lock-mode -1)
(make-local-variable 'font-lock-function)
(buffer-disable-undo)
(setq major-mode 'gh-notify-mode
mode-name "Gh-Notify"
truncate-lines t
buffer-read-only t
header-line-format '(:eval (funcall gh-notify--header-function))
font-lock-function (lambda (_) nil)
gh-notify--repo-limit gh-notify-default-repo-limit
gh-notify--current-buffer (current-buffer))
(gh-notify--init-caches)
(gh-notify--with-timing
(gh-notify--reindex-notifications (gh-notify-get-notifications))
(gh-notify--filter-notifications))
(hl-line-mode)
(run-mode-hooks 'gh-notify-mode-hook))
;;;
;;; API
;;;
(defun gh-notify-active-filter ()
"Return currently active filter string or nil."
(when gh-notify--active-filter
(apply #'string (reverse gh-notify--active-filter))))
(defun gh-notify-render-notification (notification)
"Return string representation of NOTIFICATION.
String is used as is to display NOTIFICATION in *github-notifications* buffer.
It must not span more than one line but it may contain text properties."
(let ((repo-id (gh-notify-notification-repo-id notification))
(type (gh-notify-notification-type notification))
(url (gh-notify-notification-url notification))
(title (gh-notify-notification-title notification))
(is-marked (gh-notify-notification-is-marked notification))
(unread (gh-notify-notification-unread notification))
(reason (gh-notify-notification-reason notification))
(date (gh-notify-notification-date notification))
(number (gh-notify-notification-number notification))
(state (gh-notify-notification-state notification)))
(let* ((unread-str
(cond (is-marked
"*")
(unread
"U")
((not unread)
"R")))
(date-str (format "%s" date))
(type-str
(cond
((eq type 'pullreq)
"P")
((eq type 'issue)
"I")
(t
"?")))
(state-str
(if gh-notify-show-state
(cond
((eq state 'open)
"O")
((eq state 'closed)
"C")
((eq state 'merged)
"M")
(t
"."))
""))
(repo-str (format "%s #%s" repo-id number))
(reason-str (format "[%s]" reason))
(desc-str
(if (eq gh-notify-default-view :title)
(if (string-equal "" title) url title)
url)))
;; use type as a visual marker for issue|pullreq
(pcase type
('pullreq
(setq type-str (propertize type-str 'face 'gh-notify-notification-pr-face)))
('issue
(setq type-str (propertize type-str 'face 'gh-notify-notification-issue-face))))
;; repo face is our default face for most components
(setq date-str (propertize date-str 'face 'gh-notify-notification-repo-face))
(setq state-str (propertize state-str 'face 'gh-notify-notification-repo-face))
(setq unread-str (propertize unread-str 'face 'gh-notify-notification-repo-face))
(setq repo-str (propertize repo-str 'face 'gh-notify-notification-repo-face))
(setq reason-str (propertize reason-str 'face 'gh-notify-notification-reason-face))
;; use the tail of the line for any mutex global state marker like marked/unread
(cond
(is-marked
(setq date-str (propertize date-str 'face 'gh-notify-notification-marked-face))
(setq unread-str (propertize unread-str 'face 'gh-notify-notification-marked-face))
(setq repo-str (propertize repo-str 'face 'gh-notify-notification-marked-face))
(setq desc-str (propertize desc-str 'face 'gh-notify-notification-marked-face)))
(unread
(setq date-str (propertize date-str 'face 'gh-notify-notification-unread-face))
(setq unread-str (propertize unread-str 'face 'gh-notify-notification-unread-face))
(setq repo-str (propertize repo-str 'face 'gh-notify-notification-unread-face))
(setq desc-str (propertize desc-str 'face 'gh-notify-notification-unread-face))))
(concat unread-str " "
date-str " "
type-str " "
state-str (if (string-equal state-str "") "" " ")
repo-str " "
reason-str " "
desc-str))))
(defun gh-notify-limit-notification (notification)
"Limits NOTIFICATION by status.
Limiting operation depends on `gh-notify-reason-limit', `gh-notify-type-limit'
and `gh-notify--repo-limit'."
(let ((repo-id (gh-notify-notification-repo-id notification)))
;; 3 pass filter: repo -> type -> reason
(when
;; repo limits
(and (or (member repo-id gh-notify--repo-limit)
(not gh-notify--repo-limit))
(not (member repo-id gh-notify-exclude-repo-limit)))
;;
(and
;; state filter
(if gh-notify--unread-limit
(gh-notify-notification-unread notification)
t)
;; type filters
(or (eq (gh-notify-notification-type notification) gh-notify--type-limit)
(not gh-notify--type-limit))
;; reason filters (layer 3 filter)
(cl-case gh-notify-reason-limit
(:all t)
(:mark (gh-notify-notification-is-marked notification))
(:unread (gh-notify-notification-unread notification))
(:assign (eq (gh-notify-notification-reason notification) 'assign))
(:mention (eq (gh-notify-notification-reason notification) 'mention))
(:team_mention (eq (gh-notify-notification-reason notification) 'team_mention))
(:subscribed (eq (gh-notify-notification-reason notification) 'subscribed))
(:author (eq (gh-notify-notification-reason notification) 'author))
(:review-requested (eq (gh-notify-notification-reason notification) 'review_requested))
(:comment (eq (gh-notify-notification-reason notification) 'comment)))))))
(defun gh-notify-filter-notification (notification)
"Filters NOTIFICATION using a case-insensitive match on either URL or title."
(let ((filter (gh-notify-active-filter)))
(or (null filter)
(let ((case-fold-search t)
(url (gh-notify-notification-url notification))
(title (gh-notify-notification-title notification)))
(or
(string-match (replace-regexp-in-string " " ".*" filter) url)
(string-match (replace-regexp-in-string " " ".*" filter) title))))))
(defun gh-notify-current-notification ()
"Return notification at point or nil."
(gethash (line-number-at-pos (point))
gh-notify--visible-notifications))
(defun gh-notify-goto-notification (notification)
"Move point to NOTIFICATION if it is visible."
(when-let ((line (gh-notify-notification-line notification)))
(gh-notify--goto-line line)))
;;;
;;; Forge API
;;;
(defun gh-notify--forge-get-notifications ()
"Get all forge notifications."
(let ((results '()))
(when-let ((ns (forge--ls-notifications '(unread pending done))))
(pcase-dolist (`(,key . ,grouped-ns) (seq-group-by (lambda (it) (oref it repository)) ns))
(let ((repo (forge-get-repository (car grouped-ns))))
(push (list repo grouped-ns) results))))
results))
(defun gh-notify-get-notifications ()
"Return a record (alist) containing notification information.
The alist contains (repo-id . notifications) pairs."
(cl-loop with total-notification-count = 0
with error-count = 0
with repo-count = 0
for repo-results in (gh-notify--forge-get-notifications)
for repo-id = (car repo-results)
for repo-notifications = (cadr repo-results)
when repo-id do (cl-incf repo-count)
when repo-notifications do
(setq
total-notification-count
(+ total-notification-count
(length repo-notifications)))
when repo-notifications collect (cons repo-id repo-notifications)
finally
(progn
(setq gh-notify--total-notification-count total-notification-count)
(message "Retrieved %d notifications across %d repos%s"
total-notification-count
repo-count
(if (> error-count 0)
(format ", %d errors" error-count)
"")))))
(defun gh-notify-forge-refresh()
"Refresh the Forge notification state."
(interactive)
(cl-assert (eq major-mode 'gh-notify-mode) t)
;; we fixed some of forge's all-or-nothing approach to notification updates
;; we want to routinely refresh without doing a full fetch, so instead we do
;; incremental refreshes based on the last known timestamp
;; ensure we always start from the most recent Magit/Forge db state
(call-interactively #'gh-notify-retrieve-notifications)
(forge--pull-notifications 'forge-github-repository "github.com" #'gh-notify-forge-refresh-cb))
(defun gh-notify-forge-refresh-cb ()
"Callback for Forge refresh."
(message "Forge is refreshed!")
(with-current-buffer gh-notify--current-buffer
(call-interactively #'gh-notify-retrieve-notifications)))
(defun gh-notify--insert-forge-obj (obj)
"Insert/Replace a forge db object with OBJ."
;; replace the updated object ...
(emacsql-with-transaction (forge-db)
(closql-insert (forge-db) obj t)))
(defun gh-notify--get-topic-state (type repo topic)
"Get current topic state from forge db."
(gh-notify--with-timing
(pcase type
('issue
(let ((issue (forge-get-issue topic)))
(oref issue state)))
('pullreq
(let ((pullreq (forge-get-pullreq topic)))
(oref pullreq state))))))
(defun gh-notify-set-notification-status (notification value)
"Set NOTIFICATION status as VALUE"
(when-let (topic-obj (gh-notify-notification-topic-obj notification))
(when (oref topic-obj status)
(oset topic-obj status value)
(gh-notify--insert-forge-obj topic-obj)
(when gh-notify-redraw-on-visit
(gh-notify-retrieve-notifications)))))
;;;
;;; Interactive
;;;
;; modified from forge-read-issue
(defun gh-notify-ls-pullreqs-at-point (P)
"Navigate a list of open pull requests available for notification at point.
All pull request on prefix P."
(interactive "P")
(cl-assert (eq major-mode 'gh-notify-mode) t)
(when-let ((notification (gh-notify-current-notification)))
(let* ((default-directory gh-notify-smokescreen-path)
(repo-id (gh-notify-notification-repo-id notification))
(repo (gh-notify-notification-repo notification))
(fmt (lambda (obj)
(format "#%s %s"
(oref obj number)
(oref obj title))))
;; list all issues on prefix, only open by default
(pullreqs (if P (forge--ls-pullreqs repo)
(forge--ls-active-pullreqs repo)))
(choice (completing-read
(format "%s visit pull request (%s): " repo-id (if P "all" "open"))
(mapcar fmt pullreqs) nil t)))
(unless (string-equal choice "")
;; parse the number we selected back out
(let ((topic (and (string-match "^#\\([0-9]+\\) " choice)
(string-to-number (match-string 1 choice)))))
(with-demoted-errors "Warning: %S"
(with-temp-buffer
(forge-visit-pullreq (forge-get-pullreq repo topic)))))))))