todo-webclient/client/users-cache.ls

66 lines
1.4 KiB
Plaintext
Raw Normal View History

2019-12-22 19:48:21 +01:00
2019-12-25 16:10:20 +01:00
{h} = require "maquette"
2019-12-22 19:48:21 +01:00
UsersCache = (authd-ws, on-user) ->
self = {}
on-user ||= (user) ->
2019-12-25 06:57:40 +01:00
# uid => user | "request sent"
# FIXME: "request sent" stays forever that way on failure.
2019-12-22 19:48:21 +01:00
self.users = {}
2019-12-25 06:57:40 +01:00
# TODO: Well probably want to handle “no such user” errors.
2019-12-22 19:48:21 +01:00
authd-ws.add-event-listener \user, (message) ->
user = message.user
self.users[user.uid] := user
2019-12-25 06:57:40 +01:00
self.users[user.login] := user
2019-12-22 19:48:21 +01:00
on-user user
2019-12-25 06:57:40 +01:00
authd-ws.add-event-listener \error, (message) ->
if message.reason == "user not found"
console.log "warning: got a “user not found” error"
2019-12-25 16:10:20 +01:00
self.render-user = (uid) ->
if user = self.get-user uid
h \div.level.user {key: "user-#{uid}"} [
h \div.level-left [
2019-12-26 08:18:55 +01:00
console.log "#{user.login}", user.profile?.avatar
2019-12-25 16:10:20 +01:00
if avatar = user.profile?.avatar
h \div.level-item [
h \figure.image.is-32x32.is-rounded [
h \img {
src: avatar
alt: ""
}
]
]
h \div.level-item [
"@" + (user.profile?.full_name || user.login)
]
]
]
2019-12-25 06:57:40 +01:00
# Note: uid may also be a login.
self.get-user = (uid, request-if-missing = true) ->
switch user = self.users[uid]
when void
if request-if-missing
console.log "Sending request for #{uid}"
self.users[uid] = "request sent"
authd-ws.get-user uid
void
when "request sent"
void
2019-12-22 19:48:21 +01:00
else
2019-12-25 06:57:40 +01:00
user
2019-12-22 19:48:21 +01:00
self
module.exports = UsersCache