Skip to content

Commit

Permalink
[wip] discussion+comment notifications
Browse files Browse the repository at this point in the history
  • Loading branch information
tarsius committed Oct 3, 2023
1 parent 3949956 commit 135eddb
Showing 1 changed file with 65 additions and 36 deletions.
101 changes: 65 additions & 36 deletions lisp/forge-github.el
Original file line number Diff line number Diff line change
Expand Up @@ -484,44 +484,70 @@
(defun forge--ghub-massage-notification (data githost)
(let-alist data
(let* ((type (intern (downcase .subject.type)))
(type (if (eq type 'pullrequest) 'pullreq type)))
(and (memq type '(pullreq issue))
(let* ((number (and (string-match "[0-9]*\\'" .subject.url)
(string-to-number (match-string 0 .subject.url))))
(repo (forge-get-repository
(list githost
.repository.owner.login
.repository.name)
nil 'create))
(repoid (oref repo id))
(owner (oref repo owner))
(name (oref repo name))
(id (forge--object-id repoid (string-to-number .id)))
(alias (intern (concat "_" (string-replace "=" "_" id)))))
(list alias id
`((,alias repository)
[(name ,name)
(owner ,owner)]
,@(cddr
(caddr
(ghub--graphql-prepare-query
ghub-fetch-repository
(if (eq type 'issue)
`(repository issues (issue . ,number))
`(repository pullRequest (pullRequest . ,number)))
))))
repo type number data))))))
(type (if (eq type 'pullrequest) 'pullreq type))
(_ (unless (memq type '(commit discussion issue pullreq))
(error "BUG: New unsupported notification type: %s" type)))
;; To get a topic using the GraphQL API, we need the topic
;; number, which is missing from the REST response, but for
;; issues and pull-requests we extract it from one of the
;; URLs included in the response. For discussions we don't
;; get so lucky; it features the same URL field, but its
;; value is always false! For commits we need the commit
;; hash, which we can extract from the same URL field.
(number-or-commit (and .subject.url
(string-match "[^/]*\\'" .subject.url)
(match-string 0 .subject.url)))
(number (and (not (eq type 'commit))
(string-to-number number-or-commit)))
(commit (and (eq type 'commit) number-or-commit))
(repo (forge-get-repository
(list githost
.repository.owner.login
.repository.name)
nil 'create))
(repoid (oref repo id))
(owner (oref repo owner))
(name (oref repo name))
(id (forge--object-id repoid (string-to-number .id)))
(alias (intern (concat "_" (string-replace "=" "_" id)))))
(when (and (eq type 'discussion)
(not number))
(setq number nil)) ; TODO
(list alias id
(and number
`((,alias repository)
[(name ,name)
(owner ,owner)]
,@(cddr
(caddr
(ghub--graphql-prepare-query
ghub-fetch-repository
(pcase type
('discussion `(repository
discussions
(discussion . ,number))) ; One can hope.
('issue `(repository
issues
(issue . ,number)))
('pullreq `(repository
pullRequest
(pullRequest . ,number)))))))))
repo type (or number commit .id) data))))

(defun forge--ghub-update-notifications (forge topics notifs)
(closql-with-transaction (forge-db)
(pcase-dolist (`(,alias ,id ,_ ,repo ,type ,number ,data) notifs)
(pcase-dolist (`(,alias ,id ,query ,repo ,type ,number ,data) notifs)
(let-alist data
(let ((topic (funcall (if (eq type 'issue)
#'forge--update-issue
#'forge--update-pullreq)
repo
(cdr (cadr (assq alias topics)))
nil))
(let ((topic (and query
(funcall
(pcase type
('discussion
(error "BUG: forge--update-discussion missing"))
('issue #'forge--update-issue)
('pullreq #'forge--update-pullreq))
repo
(cdr (cadr (assq alias topics)))
nil)))
(notif (or (forge-get-notification id)
(closql-insert
(forge-db)
Expand All @@ -538,12 +564,15 @@
(oset notif last-read .last_read_at)
(oset notif updated .updated_at)
(oset notif unread-p .unread)
(oset topic unread-p .unread)
(when topic (oset topic unread-p .unread))
(when (or .unread
(not (slot-boundp notif 'done-p)))
(let ((done (not .unread)))
(oset notif done-p done)
(oset topic done-p done)))))
(when topic (oset topic done-p done))))
;; (unless topic
;; ) ; TODO
))
(forge--zap-repository-cache repo))))

(cl-defmethod forge-topic-mark-read ((_ forge-github-repository) topic)
Expand Down

0 comments on commit 135eddb

Please sign in to comment.