From fb653292612eddca5b088ed88466c546d1597107 Mon Sep 17 00:00:00 2001 From: Elizabeth Alexander Hunt Date: Sat, 20 Jun 2026 09:40:07 -0700 Subject: Initial commit --- api/presence/presence.go | 64 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 api/presence/presence.go (limited to 'api/presence') 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) + } + } +} -- cgit v1.3