Add: (ement--room-display-name) Heroes-based name

This commit is contained in:
Adam Porter 2021-07-28 20:12:25 -05:00
parent fc3579a21b
commit 98f12d9970

View file

@ -486,17 +486,53 @@ Adds sender to `ement-users' when necessary."
(defun ement--room-display-name (room) (defun ement--room-display-name (room)
"Return the displayname for ROOM." "Return the displayname for ROOM."
;; SPEC: <https://matrix.org/docs/spec/client_server/r0.6.1#calculating-the-display-name-for-a-room>. ;; SPEC: <https://matrix.org/docs/spec/client_server/r0.6.1#calculating-the-display-name-for-a-room>.
(cl-labels ((latest-event (type content-field)
;; TODO: Implement step 3 from the spec, using room members to compose name. (or (cl-loop for event in (ement-room-timeline room)
when (and (equal type (ement-event-type event))
;; NOTE: That spec says "state event," but in practice some rooms have these events only in (not (string-empty-p (alist-get content-field (ement-event-content event)))))
;; their "timeline" events, so these functions need to check both. Which should take return (alist-get content-field (ement-event-content event)))
;; priority? I don't know. Since some don't have, e.g. "m.room.name" events in their state (cl-loop for event in (ement-room-state room)
;; events, I'll assume that the timeline events take priority and should be checked first. when (and (equal type (ement-event-type event))
(or (ement--room-name room) (not (string-empty-p (alist-get content-field (ement-event-content event)))))
(ement--room-alias room) return (alist-get content-field (ement-event-content event)))))
;; FIXME: Steps 3, etc. (heroes-name
(ement-room-id room))) () (pcase-let* (((cl-struct ement-room summary) room)
((map ('m.heroes hero-ids) ('m.joined_member_count joined-count)
('m.invited_member_count invited-count))
summary))
;; TODO: Disambiguate hero display names.
(when hero-ids
(cond ((>= (length hero-ids) (1- (+ joined-count invited-count)))
;; Members == heroes.
(hero-names hero-ids))
((and (< (length hero-ids) (1- (+ joined-count invited-count)))
(> (+ joined-count invited-count) 1))
;; More members than heroes.
(heroes-and-others hero-ids joined-count))
((<= (+ joined-count invited-count) 1)
;; Empty room.
(empty-room hero-ids joined-count))))))
(hero-names
(heroes) (string-join (mapcar #'hero-name heroes) ", "))
(hero-name
(id) (if-let ((user (gethash id ement-users)))
(ement-room--user-display-name user room)
id))
(heroes-and-others
(heroes joined)
(format "%s, and %s others" (hero-names heroes)
(- joined (length heroes))))
(empty-room
(heroes joined) (cl-etypecase (length heroes)
((satisfies zerop) "Empty room")
((number 1 5) (format "Empty room (was %s)"
(hero-names heroes)))
(t (format "Empty room (was %s)"
(heroes-and-others heroes joined))))))
(or (latest-event "m.room.name" 'name)
(latest-event "m.room.canonical_alias" 'alias)
(heroes-name)
(ement-room-id room))))
;; FIXME: These functions probably need to compare timestamps to ;; FIXME: These functions probably need to compare timestamps to
;; ensure that older events that are inserted at the head of the ;; ensure that older events that are inserted at the head of the