diff options
| author | Elizabeth Alexander Hunt <me@liz.coffee> | 2026-06-20 09:40:07 -0700 |
|---|---|---|
| committer | Elizabeth Alexander Hunt <me@liz.coffee> | 2026-06-20 09:40:07 -0700 |
| commit | fb653292612eddca5b088ed88466c546d1597107 (patch) | |
| tree | 900ed32e066812688858ed3bb15128c41bb78054 /api/presence | |
| download | penguins.lan-fb653292612eddca5b088ed88466c546d1597107.tar.gz penguins.lan-fb653292612eddca5b088ed88466c546d1597107.zip | |
Initial commit
Diffstat (limited to 'api/presence')
| -rw-r--r-- | api/presence/presence.go | 64 |
1 files changed, 64 insertions, 0 deletions
diff --git a/api/presence/presence.go b/api/presence/presence.go new file mode 100644 index 0000000..645b34e --- /dev/null +++ b/api/presence/presence.go @@ -0,0 +1,64 @@ +package presence + +import ( + "encoding/json" + "log" + "net/http" + + "penguins.lan/api/types" + "penguins.lan/api/ws" + "penguins.lan/database" + "penguins.lan/utils" +) + +type Waddler struct { + Username string `json:"username"` + Status string `json:"status"` + LastSeen string `json:"lastSeen"` +} + +func waddlers(context *types.RequestContext, hub *ws.Hub) []Waddler { + users, err := database.ListUsers(context.DBConn) + if err != nil { + log.Println("presence: list users failed:", err) + } + onChat := hub.Online() + onNetwork, err := database.NetworkUserIDs(context.DBConn) + if err != nil { + log.Println("presence: network ids failed:", err) + } + + list := make([]Waddler, 0, len(users)) + for _, u := range users { + status := "offline" + if onNetwork[u.ID] { + status = "network" + } + if onChat[u.ID] { + status = "chat" + } + list = append(list, Waddler{u.Username, status, utils.FormatSince(u.LastSeen)}) + } + return list +} + +func RosterContinuation(hub *ws.Hub) types.Continuation { + return func(context *types.RequestContext, req *http.Request, resp http.ResponseWriter) types.ContinuationChain { + return func(success types.Continuation, _failure types.Continuation) types.ContinuationChain { + (*context.TemplateData)["Users"] = waddlers(context, hub) + return success(context, req, resp) + } + } +} + +func StatusContinuation(hub *ws.Hub) types.Continuation { + return func(context *types.RequestContext, req *http.Request, resp http.ResponseWriter) types.ContinuationChain { + return func(success types.Continuation, _failure types.Continuation) types.ContinuationChain { + resp.Header().Set("Content-Type", "application/json") + if err := json.NewEncoder(resp).Encode(waddlers(context, hub)); err != nil { + log.Println("presence: encode failed:", err) + } + return success(context, req, resp) + } + } +} |
