chore: auto-commit (21 archivos)

- app.md
- backend/dist/assets/index-D_Kep7Fb.js
- backend/dist/index.html
- backend/handlers.go
- backend/main.go
- frontend/src/App.tsx
- frontend/src/api.ts
- frontend/src/components/CardChatPanel.tsx
- frontend/src/components/LoginPage.tsx
- frontend/src/types.ts
- ...

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-05-20 18:17:04 +02:00
parent 1923fd31a4
commit 2524340759
20 changed files with 2165 additions and 236 deletions
+1 -1
View File
@@ -2,7 +2,7 @@
name: kanban
lang: go
domain: tools
version: 0.1.0
version: 0.2.0
description: "Kanban board con persistencia SQLite, drag-and-drop entre columnas (dnd-kit) y tracking del tiempo que cada tarjeta pasa en cada columna. Frontend Vite + React + Mantine v9 embebido en el binario Go."
tags: [service, kanban, web, dnd-kit, mantine, sqlite, time-tracking]
uses_functions:
File diff suppressed because one or more lines are too long
+1 -1
View File
@@ -4,7 +4,7 @@
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Kanban</title>
<script type="module" crossorigin src="/assets/index-D_Kep7Fb.js"></script>
<script type="module" crossorigin src="/assets/index-CFDWXN9Z.js"></script>
<link rel="stylesheet" crossorigin href="/assets/index-b0xjFtx2.css">
</head>
<body>
+158
View File
@@ -0,0 +1,158 @@
package main
import (
"encoding/json"
"sync"
"sync/atomic"
"time"
)
// EventHub is an in-process pub/sub used to push board mutations and
// notifications to connected clients (SSE for board-wide events, WS for
// per-card chat). Drop policy on slow consumers: best-effort send; if a
// subscriber's buffered channel is full the event is dropped and the
// hub increments dropCount. Clients are expected to reconcile state via
// a full reload when reconnecting.
type EventHub struct {
mu sync.RWMutex
userSubs map[string]map[chan Event]struct{}
cardSubs map[string]map[chan Event]struct{}
dropCount uint64
}
// Event is the envelope broadcast to subscribers.
//
// Type — discriminator (e.g. "card.updated", "message.created").
// CardID — set when payload pertains to a specific card.
// UserID — set for per-user private events (e.g. notifications). Empty
// means broadcast to every user subscriber.
// Payload — arbitrary JSON describing the change.
// TS — RFC3339 timestamp.
type Event struct {
Type string `json:"type"`
CardID string `json:"card_id,omitempty"`
UserID string `json:"user_id,omitempty"`
Payload json.RawMessage `json:"payload,omitempty"`
TS string `json:"ts"`
}
const eventBufSize = 64
func NewEventHub() *EventHub {
return &EventHub{
userSubs: map[string]map[chan Event]struct{}{},
cardSubs: map[string]map[chan Event]struct{}{},
}
}
// SubscribeUser returns a channel that receives every public event plus
// private events targeted at userID. Caller MUST eventually call
// UnsubscribeUser to release resources.
func (h *EventHub) SubscribeUser(userID string) chan Event {
ch := make(chan Event, eventBufSize)
h.mu.Lock()
set, ok := h.userSubs[userID]
if !ok {
set = map[chan Event]struct{}{}
h.userSubs[userID] = set
}
set[ch] = struct{}{}
h.mu.Unlock()
return ch
}
func (h *EventHub) UnsubscribeUser(userID string, ch chan Event) {
h.mu.Lock()
if set, ok := h.userSubs[userID]; ok {
delete(set, ch)
if len(set) == 0 {
delete(h.userSubs, userID)
}
}
h.mu.Unlock()
close(ch)
}
// SubscribeCard returns a channel that receives events scoped to cardID
// (chat messages + typing indicators).
func (h *EventHub) SubscribeCard(cardID string) chan Event {
ch := make(chan Event, eventBufSize)
h.mu.Lock()
set, ok := h.cardSubs[cardID]
if !ok {
set = map[chan Event]struct{}{}
h.cardSubs[cardID] = set
}
set[ch] = struct{}{}
h.mu.Unlock()
return ch
}
func (h *EventHub) UnsubscribeCard(cardID string, ch chan Event) {
h.mu.Lock()
if set, ok := h.cardSubs[cardID]; ok {
delete(set, ch)
if len(set) == 0 {
delete(h.cardSubs, cardID)
}
}
h.mu.Unlock()
close(ch)
}
// Publish delivers ev to every matching subscriber. If ev.UserID is set
// it is delivered ONLY to that user's subscribers; otherwise it fans out
// to all user subscribers. Card subscribers ALWAYS receive events that
// match ev.CardID. Best-effort: full channels are skipped.
func (h *EventHub) Publish(ev Event) {
if ev.TS == "" {
ev.TS = time.Now().UTC().Format(time.RFC3339)
}
h.mu.RLock()
defer h.mu.RUnlock()
deliver := func(ch chan Event) {
select {
case ch <- ev:
default:
atomic.AddUint64(&h.dropCount, 1)
}
}
if ev.UserID != "" {
if set, ok := h.userSubs[ev.UserID]; ok {
for ch := range set {
deliver(ch)
}
}
} else {
for _, set := range h.userSubs {
for ch := range set {
deliver(ch)
}
}
}
if ev.CardID != "" {
if set, ok := h.cardSubs[ev.CardID]; ok {
for ch := range set {
deliver(ch)
}
}
}
}
func (h *EventHub) DropCount() uint64 {
return atomic.LoadUint64(&h.dropCount)
}
// PublishJSON marshals payload and publishes a single Event.
func (h *EventHub) PublishJSON(typ, cardID, userID string, payload interface{}) {
var raw json.RawMessage
if payload != nil {
b, err := json.Marshal(payload)
if err == nil {
raw = b
}
}
h.Publish(Event{Type: typ, CardID: cardID, UserID: userID, Payload: raw})
}
+146
View File
@@ -0,0 +1,146 @@
package main
import (
"sync"
"sync/atomic"
"testing"
"time"
)
func TestEventHub_BroadcastToAllUsers(t *testing.T) {
hub := NewEventHub()
chA := hub.SubscribeUser("alice")
chB := hub.SubscribeUser("bob")
defer hub.UnsubscribeUser("alice", chA)
defer hub.UnsubscribeUser("bob", chB)
hub.PublishJSON("card.updated", "c1", "", map[string]string{"id": "c1"})
for _, ch := range []chan Event{chA, chB} {
select {
case ev := <-ch:
if ev.Type != "card.updated" {
t.Fatalf("type = %q, want card.updated", ev.Type)
}
case <-time.After(time.Second):
t.Fatal("timeout waiting for event")
}
}
}
func TestEventHub_PrivateUserEvent(t *testing.T) {
hub := NewEventHub()
chA := hub.SubscribeUser("alice")
chB := hub.SubscribeUser("bob")
defer hub.UnsubscribeUser("alice", chA)
defer hub.UnsubscribeUser("bob", chB)
hub.PublishJSON("notification.created", "", "alice", map[string]string{"foo": "bar"})
select {
case ev := <-chA:
if ev.UserID != "alice" {
t.Fatalf("user_id = %q, want alice", ev.UserID)
}
case <-time.After(time.Second):
t.Fatal("alice did not get private event")
}
select {
case ev := <-chB:
t.Fatalf("bob received private event for alice: %+v", ev)
case <-time.After(100 * time.Millisecond):
// expected
}
}
func TestEventHub_CardSubscription(t *testing.T) {
hub := NewEventHub()
ch := hub.SubscribeCard("card-1")
defer hub.UnsubscribeCard("card-1", ch)
hub.PublishJSON("message.created", "card-1", "", map[string]string{"id": "m1"})
hub.PublishJSON("message.created", "card-2", "", map[string]string{"id": "m2"})
select {
case ev := <-ch:
if ev.CardID != "card-1" {
t.Fatalf("card_id = %q, want card-1", ev.CardID)
}
case <-time.After(time.Second):
t.Fatal("timeout")
}
select {
case ev := <-ch:
t.Fatalf("received unexpected event for other card: %+v", ev)
case <-time.After(100 * time.Millisecond):
}
}
func TestEventHub_DropPolicyOnSlowConsumer(t *testing.T) {
hub := NewEventHub()
ch := hub.SubscribeUser("slow")
defer hub.UnsubscribeUser("slow", ch)
// Fill the buffer + N extra to force drops.
const extra = 50
for i := 0; i < eventBufSize+extra; i++ {
hub.PublishJSON("noise", "", "slow", nil)
}
if got := hub.DropCount(); got < extra {
t.Fatalf("DropCount = %d, want >= %d", got, extra)
}
}
func TestEventHub_UnsubscribeRemoves(t *testing.T) {
hub := NewEventHub()
ch := hub.SubscribeUser("alice")
hub.UnsubscribeUser("alice", ch)
// channel must be closed
select {
case _, ok := <-ch:
if ok {
t.Fatal("expected closed channel")
}
default:
// channel could be drained-and-closed
}
// Publish should not panic and should not deliver anywhere.
hub.PublishJSON("noise", "", "alice", nil)
}
func TestEventHub_ConcurrentPublishers(t *testing.T) {
hub := NewEventHub()
ch := hub.SubscribeUser("u")
defer hub.UnsubscribeUser("u", ch)
var received atomic.Uint64
done := make(chan struct{})
go func() {
for range ch {
received.Add(1)
}
close(done)
}()
var wg sync.WaitGroup
const writers = 10
const each = 100
for i := 0; i < writers; i++ {
wg.Add(1)
go func() {
defer wg.Done()
for j := 0; j < each; j++ {
hub.PublishJSON("ping", "", "u", nil)
}
}()
}
wg.Wait()
// Give the consumer time to drain.
time.Sleep(200 * time.Millisecond)
got := received.Load()
dropped := hub.DropCount()
if got+dropped < writers*each {
t.Fatalf("received=%d drop=%d want sum >= %d", got, dropped, writers*each)
}
}
+85 -34
View File
@@ -74,8 +74,21 @@ func handleGetBoard(db *DB) http.HandlerFunc {
}
}
// publishInvalidated emits a board.invalidated event so connected clients
// refetch /api/board. Best-effort: dropped events recover on next mutation
// or via the periodic safety reload kept in the SPA.
func publishInvalidated(hub *EventHub, cardID, columnID string) {
if hub == nil {
return
}
hub.PublishJSON("board.invalidated", cardID, "", map[string]string{
"card_id": cardID,
"column_id": columnID,
})
}
// POST /api/columns { name }
func handleCreateColumn(db *DB) http.HandlerFunc {
func handleCreateColumn(db *DB, hub *EventHub) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var body struct{ Name string `json:"name"` }
if err := infra.HTTPParseBody(r, &body, maxBodyBytes); err != nil {
@@ -91,12 +104,13 @@ func handleCreateColumn(db *DB) http.HandlerFunc {
serverError(w, err)
return
}
publishInvalidated(hub, "", c.ID)
infra.HTTPJSONResponse(w, http.StatusCreated, c)
}
}
// PATCH /api/columns/{id} { name?, position?, location?, width? }
func handleUpdateColumn(db *DB) http.HandlerFunc {
func handleUpdateColumn(db *DB, hub *EventHub) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
id := r.PathValue("id")
var body struct {
@@ -116,24 +130,26 @@ func handleUpdateColumn(db *DB) http.HandlerFunc {
serverError(w, err)
return
}
publishInvalidated(hub, "", id)
w.WriteHeader(http.StatusNoContent)
}
}
// DELETE /api/columns/{id}
func handleDeleteColumn(db *DB) http.HandlerFunc {
func handleDeleteColumn(db *DB, hub *EventHub) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
id := r.PathValue("id")
if err := db.DeleteColumn(id); err != nil {
serverError(w, err)
return
}
publishInvalidated(hub, "", id)
w.WriteHeader(http.StatusNoContent)
}
}
// POST /api/columns/reorder { ids: [...] }
func handleReorderColumns(db *DB) http.HandlerFunc {
func handleReorderColumns(db *DB, hub *EventHub) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var body struct{ IDs []string `json:"ids"` }
if err := infra.HTTPParseBody(r, &body, maxBodyBytes); err != nil {
@@ -144,12 +160,13 @@ func handleReorderColumns(db *DB) http.HandlerFunc {
serverError(w, err)
return
}
publishInvalidated(hub, "", "")
w.WriteHeader(http.StatusNoContent)
}
}
// POST /api/cards { column_id, requester?, title, description? }
func handleCreateCard(db *DB) http.HandlerFunc {
func handleCreateCard(db *DB, hub *EventHub) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var body struct {
ColumnID string `json:"column_id"`
@@ -186,12 +203,13 @@ func handleCreateCard(db *DB) http.HandlerFunc {
serverError(w, err)
return
}
publishInvalidated(hub, c.ID, body.ColumnID)
infra.HTTPJSONResponse(w, http.StatusCreated, c)
}
}
// PATCH /api/cards/{id} { requester?, title?, description?, color? }
func handleUpdateCard(db *DB) http.HandlerFunc {
func handleUpdateCard(db *DB, hub *EventHub) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
id := r.PathValue("id")
var raw map[string]any
@@ -249,12 +267,13 @@ func handleUpdateCard(db *DB) http.HandlerFunc {
serverError(w, err)
return
}
publishInvalidated(hub, id, "")
w.WriteHeader(http.StatusNoContent)
}
}
// PUT /api/cards/{id}/stickers { stickers: [{emoji,x,y}, ...] }
func handleUpdateCardStickers(db *DB) http.HandlerFunc {
func handleUpdateCardStickers(db *DB, hub *EventHub) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
id := r.PathValue("id")
var body struct {
@@ -268,12 +287,13 @@ func handleUpdateCardStickers(db *DB) http.HandlerFunc {
serverError(w, err)
return
}
publishInvalidated(hub, id, "")
w.WriteHeader(http.StatusNoContent)
}
}
// DELETE /api/cards/{id}
func handleDeleteCard(db *DB) http.HandlerFunc {
func handleDeleteCard(db *DB, hub *EventHub) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
id := r.PathValue("id")
actor, _ := infra.UserIDFromContext(r.Context(), userCtxKey)
@@ -281,12 +301,13 @@ func handleDeleteCard(db *DB) http.HandlerFunc {
serverError(w, err)
return
}
publishInvalidated(hub, id, "")
w.WriteHeader(http.StatusNoContent)
}
}
// POST /api/cards/{id}/move { column_id, ordered_ids }
func handleMoveCard(db *DB) http.HandlerFunc {
func handleMoveCard(db *DB, hub *EventHub) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
id := r.PathValue("id")
var body struct {
@@ -310,6 +331,7 @@ func handleMoveCard(db *DB) http.HandlerFunc {
serverError(w, err)
return
}
publishInvalidated(hub, id, body.ColumnID)
w.WriteHeader(http.StatusNoContent)
}
}
@@ -328,7 +350,10 @@ func handleListCardMessages(db *DB) http.HandlerFunc {
}
// POST /api/cards/{id}/messages { body }
func handleCreateCardMessage(db *DB) http.HandlerFunc {
//
// Parses @mentions, fans out notifications and publishes message.created via
// the hub so SSE/WS subscribers see the message immediately.
func handleCreateCardMessage(db *DB, hub *EventHub) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
id := r.PathValue("id")
var body struct {
@@ -347,7 +372,7 @@ func handleCreateCardMessage(db *DB) http.HandlerFunc {
infra.HTTPErrorResponse(w, infra.HTTPError{Status: http.StatusUnauthorized, Code: "unauthorized", Message: "session required"})
return
}
m, err := db.CreateCardMessage(id, actor, body.Body)
m, _, _, err := db.CreateCardMessageAndNotify(id, actor, body.Body, hub)
if err != nil {
if strings.Contains(err.Error(), "not found") {
notFound(w, err.Error())
@@ -361,8 +386,9 @@ func handleCreateCardMessage(db *DB) http.HandlerFunc {
}
// DELETE /api/cards/{cid}/messages/{mid}
func handleDeleteCardMessage(db *DB) http.HandlerFunc {
func handleDeleteCardMessage(db *DB, hub *EventHub) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
cid := r.PathValue("id")
mid := r.PathValue("mid")
actor, _ := infra.UserIDFromContext(r.Context(), userCtxKey)
if actor == "" {
@@ -377,12 +403,15 @@ func handleDeleteCardMessage(db *DB) http.HandlerFunc {
serverError(w, err)
return
}
if hub != nil {
hub.PublishJSON("message.deleted", cid, "", map[string]string{"id": mid})
}
w.WriteHeader(http.StatusNoContent)
}
}
// POST /api/cards/{id}/duplicate
func handleDuplicateCard(db *DB) http.HandlerFunc {
func handleDuplicateCard(db *DB, hub *EventHub) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
id := r.PathValue("id")
actor, _ := infra.UserIDFromContext(r.Context(), userCtxKey)
@@ -395,6 +424,7 @@ func handleDuplicateCard(db *DB) http.HandlerFunc {
serverError(w, err)
return
}
publishInvalidated(hub, c.ID, c.ColumnID)
infra.HTTPJSONResponse(w, http.StatusCreated, c)
}
}
@@ -425,7 +455,7 @@ func handleListTrash(db *DB) http.HandlerFunc {
}
// POST /api/cards/{id}/restore
func handleRestoreCard(db *DB) http.HandlerFunc {
func handleRestoreCard(db *DB, hub *EventHub) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
id := r.PathValue("id")
actor, _ := infra.UserIDFromContext(r.Context(), userCtxKey)
@@ -433,6 +463,7 @@ func handleRestoreCard(db *DB) http.HandlerFunc {
serverError(w, err)
return
}
publishInvalidated(hub, id, "")
w.WriteHeader(http.StatusNoContent)
}
}
@@ -553,44 +584,48 @@ func handleListArchive(db *DB) http.HandlerFunc {
}
// POST /api/cards/{id}/archive
func handleArchiveCard(db *DB) http.HandlerFunc {
func handleArchiveCard(db *DB, hub *EventHub) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
id := r.PathValue("id")
if err := db.ArchiveCard(id); err != nil {
serverError(w, err)
return
}
publishInvalidated(hub, id, "")
w.WriteHeader(http.StatusNoContent)
}
}
// POST /api/cards/{id}/unarchive
func handleUnarchiveCard(db *DB) http.HandlerFunc {
func handleUnarchiveCard(db *DB, hub *EventHub) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
id := r.PathValue("id")
if err := db.UnarchiveCard(id); err != nil {
serverError(w, err)
return
}
publishInvalidated(hub, id, "")
w.WriteHeader(http.StatusNoContent)
}
}
// DELETE /api/cards/{id}/purge
func handlePurgeCard(db *DB) http.HandlerFunc {
func handlePurgeCard(db *DB, hub *EventHub) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
id := r.PathValue("id")
if err := db.PurgeCard(id); err != nil {
serverError(w, err)
return
}
publishInvalidated(hub, id, "")
w.WriteHeader(http.StatusNoContent)
}
}
func apiRoutes(db *DB, chatWorkdir string, logger *ChatLogger, internalToken string, flags *FeatureFlags) []infra.Route {
func apiRoutes(db *DB, chatWorkdir string, logger *ChatLogger, internalToken string, flags *FeatureFlags, hub *EventHub) []infra.Route {
return []infra.Route{
{Method: "GET", Path: "/api/flags", Handler: handleListFlags(flags)},
{Method: "GET", Path: "/api/version", Handler: handleVersion()},
{Method: "POST", Path: "/api/auth/register", Handler: handleRegister(db, flags)},
{Method: "POST", Path: "/api/auth/login", Handler: handleLogin(db)},
{Method: "POST", Path: "/api/auth/logout", Handler: handleLogout(db)},
@@ -598,37 +633,53 @@ func apiRoutes(db *DB, chatWorkdir string, logger *ChatLogger, internalToken str
{Method: "PATCH", Path: "/api/me", Handler: handlePatchMe(db)},
{Method: "GET", Path: "/api/users", Handler: handleListUsers(db)},
{Method: "GET", Path: "/api/board", Handler: handleGetBoard(db)},
{Method: "POST", Path: "/api/columns", Handler: handleCreateColumn(db)},
{Method: "POST", Path: "/api/columns/reorder", Handler: handleReorderColumns(db)},
{Method: "PATCH", Path: "/api/columns/{id}", Handler: handleUpdateColumn(db)},
{Method: "DELETE", Path: "/api/columns/{id}", Handler: handleDeleteColumn(db)},
{Method: "POST", Path: "/api/cards", Handler: handleCreateCard(db)},
{Method: "PATCH", Path: "/api/cards/{id}", Handler: handleUpdateCard(db)},
{Method: "PUT", Path: "/api/cards/{id}/stickers", Handler: handleUpdateCardStickers(db)},
{Method: "DELETE", Path: "/api/cards/{id}", Handler: handleDeleteCard(db)},
{Method: "POST", Path: "/api/cards/{id}/move", Handler: handleMoveCard(db)},
{Method: "POST", Path: "/api/cards/{id}/duplicate", Handler: handleDuplicateCard(db)},
{Method: "POST", Path: "/api/columns", Handler: handleCreateColumn(db, hub)},
{Method: "POST", Path: "/api/columns/reorder", Handler: handleReorderColumns(db, hub)},
{Method: "PATCH", Path: "/api/columns/{id}", Handler: handleUpdateColumn(db, hub)},
{Method: "DELETE", Path: "/api/columns/{id}", Handler: handleDeleteColumn(db, hub)},
{Method: "POST", Path: "/api/cards", Handler: handleCreateCard(db, hub)},
{Method: "PATCH", Path: "/api/cards/{id}", Handler: handleUpdateCard(db, hub)},
{Method: "PUT", Path: "/api/cards/{id}/stickers", Handler: handleUpdateCardStickers(db, hub)},
{Method: "DELETE", Path: "/api/cards/{id}", Handler: handleDeleteCard(db, hub)},
{Method: "POST", Path: "/api/cards/{id}/move", Handler: handleMoveCard(db, hub)},
{Method: "POST", Path: "/api/cards/{id}/duplicate", Handler: handleDuplicateCard(db, hub)},
{Method: "GET", Path: "/api/cards/{id}/messages", Handler: handleListCardMessages(db)},
{Method: "POST", Path: "/api/cards/{id}/messages", Handler: handleCreateCardMessage(db)},
{Method: "DELETE", Path: "/api/cards/{id}/messages/{mid}", Handler: handleDeleteCardMessage(db)},
{Method: "POST", Path: "/api/cards/{id}/messages", Handler: handleCreateCardMessage(db, hub)},
{Method: "DELETE", Path: "/api/cards/{id}/messages/{mid}", Handler: handleDeleteCardMessage(db, hub)},
{Method: "GET", Path: "/api/cards/{id}/history", Handler: handleCardHistory(db)},
{Method: "GET", Path: "/api/trash", Handler: handleListTrash(db)},
{Method: "POST", Path: "/api/cards/{id}/restore", Handler: handleRestoreCard(db)},
{Method: "POST", Path: "/api/cards/{id}/restore", Handler: handleRestoreCard(db, hub)},
{Method: "GET", Path: "/api/reports/daily", Handler: handleDailyReport(db)},
{Method: "GET", Path: "/api/reports/daily/summary", Handler: handleGetDailySummary(db)},
{Method: "POST", Path: "/api/reports/daily/summary", Handler: handleGenerateDailySummary(db)},
{Method: "GET", Path: "/api/settings/{key}", Handler: handleGetSetting(db)},
{Method: "PUT", Path: "/api/settings/{key}", Handler: handlePutSetting(db)},
{Method: "GET", Path: "/api/archive", Handler: handleListArchive(db)},
{Method: "POST", Path: "/api/cards/{id}/archive", Handler: handleArchiveCard(db)},
{Method: "POST", Path: "/api/cards/{id}/unarchive", Handler: handleUnarchiveCard(db)},
{Method: "DELETE", Path: "/api/cards/{id}/purge", Handler: handlePurgeCard(db)},
{Method: "POST", Path: "/api/cards/{id}/archive", Handler: handleArchiveCard(db, hub)},
{Method: "POST", Path: "/api/cards/{id}/unarchive", Handler: handleUnarchiveCard(db, hub)},
{Method: "DELETE", Path: "/api/cards/{id}/purge", Handler: handlePurgeCard(db, hub)},
{Method: "POST", Path: "/api/chat", Handler: handleChat(db, chatWorkdir, logger)},
{Method: "GET", Path: "/api/chat/ws", Handler: handleChatWS(db, chatWorkdir, logger, internalToken)},
{Method: "POST", Path: "/api/tool/{name}", Handler: handleInternalTool(db, internalToken, logger)},
{Method: "GET", Path: "/api/metrics", Handler: handleMetrics(db)},
{Method: "GET", Path: "/api/tags", Handler: handleListTags(db)},
{Method: "GET", Path: "/api/requesters", Handler: handleListRequesters(db)},
{Method: "GET", Path: "/api/events", Handler: handleEventStream(hub)},
{Method: "GET", Path: "/api/cards/{id}/chat/ws", Handler: handleCardChatWS(db, hub)},
{Method: "GET", Path: "/api/notifications", Handler: handleListNotifications(db)},
{Method: "GET", Path: "/api/notifications/unread-count", Handler: handleUnreadCount(db)},
{Method: "POST", Path: "/api/notifications/{id}/read", Handler: handleMarkNotificationRead(db, hub)},
{Method: "POST", Path: "/api/notifications/read-all", Handler: handleMarkAllNotificationsRead(db, hub)},
}
}
// GET /api/version → {"version": "<semver>"}
//
// Public, no auth. Skipped from session middleware via skip list updated in
// main.go to keep the SPA pre-login able to display the running build.
func handleVersion() http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
infra.HTTPJSONResponse(w, http.StatusOK, map[string]string{"version": Version})
}
}
+8 -2
View File
@@ -21,6 +21,11 @@ import (
//go:embed all:dist
var frontendDist embed.FS
// Version is the build-time identifier of the kanban app. It is injected
// from app.md's `version:` field via -ldflags "-X main.Version=..." by run.sh
// (and by docker/CI). Defaults to "dev" for hand-built binaries.
var Version = "dev"
func main() {
// Subcommand `kanban mcp` runs as MCP server over stdio (spawned by claude -p).
if len(os.Args) > 1 && os.Args[1] == "mcp" {
@@ -63,7 +68,8 @@ func main() {
wd := chatWorkdir(*dbPath)
logger := newChatLogger(filepath.Join(wd, "chat.log"))
log.Printf("chat tool log: %s", logger.path)
mux := infra.HTTPRouter(apiRoutes(db, wd, logger, internalToken, &featureFlags))
hub := NewEventHub()
mux := infra.HTTPRouter(apiRoutes(db, wd, logger, internalToken, &featureFlags, hub))
feHandler := frontendHandler()
if feHandler != nil {
@@ -76,7 +82,7 @@ func main() {
authMW := infra.HTTPSessionCookieMiddleware(infra.SessionCookieConfig{
DB: db.conn,
CookieName: cookieName,
SkipPaths: []string{"/api/auth/", "/api/tool/", "/api/flags", "/health", "/assets/", "/index.html"},
SkipPaths: []string{"/api/auth/", "/api/tool/", "/api/flags", "/api/version", "/health", "/assets/", "/index.html"},
UserCtxKey: userCtxKey,
})
+42
View File
@@ -0,0 +1,42 @@
-- Per-user notifications + persisted @mentions.
-- Created by card chat messages (card_messages).
--
-- Kinds:
-- mention — user mentioned via @username in body
-- assigned_chat — user is the card's assignee and someone else commented
-- reply — user previously commented on this card (or is requester)
-- A row is created per (recipient_user, message). The kind chosen is the
-- highest priority among those that apply: mention > assigned_chat > reply.
CREATE TABLE IF NOT EXISTS notifications (
id TEXT PRIMARY KEY,
user_id TEXT NOT NULL,
card_id TEXT NOT NULL,
message_id TEXT NOT NULL,
kind TEXT NOT NULL,
actor_id TEXT NOT NULL,
created_at TEXT NOT NULL,
read_at TEXT,
FOREIGN KEY (card_id) REFERENCES cards(id) ON DELETE CASCADE,
FOREIGN KEY (message_id) REFERENCES card_messages(id) ON DELETE CASCADE
);
CREATE INDEX IF NOT EXISTS idx_notifications_user_unread
ON notifications(user_id, read_at, created_at DESC);
CREATE INDEX IF NOT EXISTS idx_notifications_user_created
ON notifications(user_id, created_at DESC);
CREATE TABLE IF NOT EXISTS card_mentions (
id TEXT PRIMARY KEY,
card_id TEXT NOT NULL,
message_id TEXT NOT NULL,
user_id TEXT NOT NULL,
created_at TEXT NOT NULL,
FOREIGN KEY (card_id) REFERENCES cards(id) ON DELETE CASCADE,
FOREIGN KEY (message_id) REFERENCES card_messages(id) ON DELETE CASCADE
);
CREATE INDEX IF NOT EXISTS idx_card_mentions_user ON card_mentions(user_id, created_at DESC);
CREATE INDEX IF NOT EXISTS idx_card_mentions_card ON card_mentions(card_id);
CREATE INDEX IF NOT EXISTS idx_card_mentions_message ON card_mentions(message_id);
+328
View File
@@ -0,0 +1,328 @@
package main
import (
"database/sql"
"fmt"
"regexp"
"strings"
"time"
)
// Notification kinds, ordered by priority (highest first). When a single
// message triggers multiple kinds for one user, the highest-priority kind
// is the one persisted.
const (
NotifKindMention = "mention"
NotifKindAssignedChat = "assigned_chat"
NotifKindReply = "reply"
)
func notifKindPriority(k string) int {
switch k {
case NotifKindMention:
return 3
case NotifKindAssignedChat:
return 2
case NotifKindReply:
return 1
}
return 0
}
type Notification struct {
ID string `json:"id"`
UserID string `json:"user_id"`
CardID string `json:"card_id"`
MessageID string `json:"message_id"`
Kind string `json:"kind"`
ActorID string `json:"actor_id"`
CreatedAt string `json:"created_at"`
ReadAt *string `json:"read_at"`
CardTitle string `json:"card_title"`
CardSeqNum int `json:"card_seq_num"`
ActorName string `json:"actor_name"`
Snippet string `json:"snippet"`
}
type CardMention struct {
ID string `json:"id"`
CardID string `json:"card_id"`
MessageID string `json:"message_id"`
UserID string `json:"user_id"`
CreatedAt string `json:"created_at"`
}
var mentionRe = regexp.MustCompile(`(?i)@([a-z0-9][a-z0-9_.-]{0,63})`)
// extractMentions returns the set of @usernames referenced in body, lowercased.
// The leading '@' is not included. Each username is returned at most once.
func extractMentions(body string) []string {
matches := mentionRe.FindAllStringSubmatch(body, -1)
if len(matches) == 0 {
return nil
}
seen := map[string]struct{}{}
out := make([]string, 0, len(matches))
for _, m := range matches {
u := strings.ToLower(m[1])
if _, ok := seen[u]; ok {
continue
}
seen[u] = struct{}{}
out = append(out, u)
}
return out
}
// CreateCardMessageAndNotify wraps CreateCardMessage with mention parsing,
// notification fan-out and pub/sub publication. The returned slice contains
// the user_ids that received a notification (useful for tests).
func (db *DB) CreateCardMessageAndNotify(cardID, authorID, body string, hub *EventHub) (*CardMessage, []Notification, []CardMention, error) {
msg, err := db.CreateCardMessage(cardID, authorID, body)
if err != nil {
return nil, nil, nil, err
}
mentions, err := db.resolveAndStoreMentions(cardID, msg.ID, body)
if err != nil {
return msg, nil, nil, err
}
notifs, err := db.fanoutNotifications(cardID, msg, authorID, mentions)
if err != nil {
return msg, nil, mentions, err
}
if hub != nil {
hub.PublishJSON("message.created", cardID, "", msg)
for _, n := range notifs {
hub.PublishJSON("notification.created", cardID, n.UserID, n)
}
}
return msg, notifs, mentions, nil
}
// resolveAndStoreMentions parses @usernames from body, resolves them to
// existing user_ids (silently ignoring unknowns) and persists the matches
// in card_mentions.
func (db *DB) resolveAndStoreMentions(cardID, messageID, body string) ([]CardMention, error) {
usernames := extractMentions(body)
if len(usernames) == 0 {
return nil, nil
}
placeholders := strings.Repeat("?,", len(usernames))
placeholders = placeholders[:len(placeholders)-1]
args := make([]interface{}, 0, len(usernames))
for _, u := range usernames {
args = append(args, u)
}
rows, err := db.conn.Query(
fmt.Sprintf(`SELECT id, username FROM users WHERE username IN (%s)`, placeholders),
args...,
)
if err != nil {
return nil, err
}
defer rows.Close()
resolved := map[string]string{}
for rows.Next() {
var id, uname string
if err := rows.Scan(&id, &uname); err != nil {
return nil, err
}
resolved[uname] = id
}
if err := rows.Err(); err != nil {
return nil, err
}
if len(resolved) == 0 {
return nil, nil
}
now := time.Now().UTC().Format(time.RFC3339)
out := make([]CardMention, 0, len(resolved))
for _, userID := range resolved {
m := CardMention{ID: newID(), CardID: cardID, MessageID: messageID, UserID: userID, CreatedAt: now}
if _, err := db.conn.Exec(
`INSERT INTO card_mentions (id, card_id, message_id, user_id, created_at) VALUES (?, ?, ?, ?, ?)`,
m.ID, m.CardID, m.MessageID, m.UserID, m.CreatedAt,
); err != nil {
return out, err
}
out = append(out, m)
}
return out, nil
}
// fanoutNotifications computes the recipient set for a new message and
// inserts one notification row per recipient with the highest-priority kind.
//
// Recipients = {assignee_id of card} {previous authors of card_messages
// on this card} {users mentioned in this message} \ {author}.
//
// Kind precedence: mention > assigned_chat > reply.
func (db *DB) fanoutNotifications(cardID string, msg *CardMessage, authorID string, mentions []CardMention) ([]Notification, error) {
recipients := map[string]string{} // userID -> kind
upgrade := func(userID, kind string) {
if userID == "" || userID == authorID {
return
}
existing, ok := recipients[userID]
if !ok || notifKindPriority(kind) > notifKindPriority(existing) {
recipients[userID] = kind
}
}
// Previous authors on this card.
rows, err := db.conn.Query(
`SELECT DISTINCT author_id FROM card_messages
WHERE card_id = ? AND author_id IS NOT NULL AND author_id != '' AND id != ?`,
cardID, msg.ID,
)
if err != nil {
return nil, err
}
for rows.Next() {
var uid sql.NullString
if err := rows.Scan(&uid); err != nil {
rows.Close()
return nil, err
}
if uid.Valid {
upgrade(uid.String, NotifKindReply)
}
}
rows.Close()
// Assignee.
var assignee sql.NullString
if err := db.conn.QueryRow(`SELECT assignee_id FROM cards WHERE id = ?`, cardID).Scan(&assignee); err != nil && err != sql.ErrNoRows {
return nil, err
}
if assignee.Valid {
upgrade(assignee.String, NotifKindAssignedChat)
}
// Mentions (highest priority).
for _, m := range mentions {
upgrade(m.UserID, NotifKindMention)
}
if len(recipients) == 0 {
return nil, nil
}
now := time.Now().UTC().Format(time.RFC3339)
out := make([]Notification, 0, len(recipients))
// Snippet for hydrated notif payload.
snippet := msg.Body
if len(snippet) > 140 {
snippet = snippet[:140] + "…"
}
var cardTitle string
var cardSeq int
_ = db.conn.QueryRow(`SELECT title, seq_num FROM cards WHERE id = ?`, cardID).Scan(&cardTitle, &cardSeq)
var actorName string
_ = db.conn.QueryRow(`SELECT COALESCE(NULLIF(display_name, ''), username) FROM users WHERE id = ?`, authorID).Scan(&actorName)
for userID, kind := range recipients {
n := Notification{
ID: newID(), UserID: userID, CardID: cardID, MessageID: msg.ID,
Kind: kind, ActorID: authorID, CreatedAt: now,
CardTitle: cardTitle, CardSeqNum: cardSeq, ActorName: actorName, Snippet: snippet,
}
if _, err := db.conn.Exec(
`INSERT INTO notifications (id, user_id, card_id, message_id, kind, actor_id, created_at) VALUES (?, ?, ?, ?, ?, ?, ?)`,
n.ID, n.UserID, n.CardID, n.MessageID, n.Kind, n.ActorID, n.CreatedAt,
); err != nil {
return out, err
}
out = append(out, n)
}
return out, nil
}
// ListNotifications returns notifications for userID. If onlyUnread is true,
// already-read entries are skipped. Limit defaults to 50 when <= 0.
func (db *DB) ListNotifications(userID string, onlyUnread bool, limit int) ([]Notification, error) {
if limit <= 0 {
limit = 50
}
q := `SELECT n.id, n.user_id, n.card_id, n.message_id, n.kind, n.actor_id, n.created_at, n.read_at,
COALESCE(c.title, ''), COALESCE(c.seq_num, 0),
COALESCE(NULLIF(u.display_name, ''), u.username, ''),
COALESCE(m.body, '')
FROM notifications n
LEFT JOIN cards c ON c.id = n.card_id
LEFT JOIN users u ON u.id = n.actor_id
LEFT JOIN card_messages m ON m.id = n.message_id
WHERE n.user_id = ?`
if onlyUnread {
q += ` AND n.read_at IS NULL`
}
q += ` ORDER BY n.created_at DESC LIMIT ?`
rows, err := db.conn.Query(q, userID, limit)
if err != nil {
return nil, err
}
defer rows.Close()
out := []Notification{}
for rows.Next() {
var n Notification
var readAt sql.NullString
var body string
if err := rows.Scan(&n.ID, &n.UserID, &n.CardID, &n.MessageID, &n.Kind, &n.ActorID, &n.CreatedAt,
&readAt, &n.CardTitle, &n.CardSeqNum, &n.ActorName, &body); err != nil {
return nil, err
}
if readAt.Valid {
s := readAt.String
n.ReadAt = &s
}
if len(body) > 140 {
n.Snippet = body[:140] + "…"
} else {
n.Snippet = body
}
out = append(out, n)
}
return out, rows.Err()
}
func (db *DB) CountUnreadNotifications(userID string) (int, error) {
var n int
err := db.conn.QueryRow(
`SELECT COUNT(*) FROM notifications WHERE user_id = ? AND read_at IS NULL`, userID,
).Scan(&n)
return n, err
}
func (db *DB) MarkNotificationRead(userID, notifID string) error {
now := time.Now().UTC().Format(time.RFC3339)
res, err := db.conn.Exec(
`UPDATE notifications SET read_at = ? WHERE id = ? AND user_id = ? AND read_at IS NULL`,
now, notifID, userID,
)
if err != nil {
return err
}
if n, _ := res.RowsAffected(); n == 0 {
// Not an error: idempotent.
return nil
}
return nil
}
func (db *DB) MarkAllNotificationsRead(userID string) (int, error) {
now := time.Now().UTC().Format(time.RFC3339)
res, err := db.conn.Exec(
`UPDATE notifications SET read_at = ? WHERE user_id = ? AND read_at IS NULL`,
now, userID,
)
if err != nil {
return 0, err
}
n, _ := res.RowsAffected()
return int(n), nil
}
+179
View File
@@ -0,0 +1,179 @@
package main
import (
"reflect"
"sort"
"testing"
)
func TestExtractMentions(t *testing.T) {
cases := []struct {
in string
want []string
}{
{"hola @alice", []string{"alice"}},
{"@Bob y @bob mismo", []string{"bob"}},
{"sin menciones", nil},
{"email@foo.com no cuenta como @real_user", []string{"foo.com", "real_user"}},
{"@a-b-c y @d.e", []string{"a-b-c", "d.e"}},
}
for _, c := range cases {
got := extractMentions(c.in)
sort.Strings(got)
sort.Strings(c.want)
if !reflect.DeepEqual(got, c.want) {
t.Errorf("extractMentions(%q) = %v, want %v", c.in, got, c.want)
}
}
}
func mkUser(t *testing.T, db *DB, username string) string {
t.Helper()
u, err := db.CreateUser(username, "passw", username)
if err != nil {
t.Fatalf("CreateUser %q: %v", username, err)
}
return u.ID
}
func mkCard(t *testing.T, db *DB, columnID, requester, title, assigneeID string) string {
t.Helper()
c, err := db.CreateCard(columnID, requester, title, "", "")
if err != nil {
t.Fatalf("CreateCard: %v", err)
}
if assigneeID != "" {
if err := db.UpdateCardWithActor(c.ID, CardPatch{AssigneeID: &assigneeID, HasAssignee: true}, ""); err != nil {
t.Fatalf("assign: %v", err)
}
}
return c.ID
}
func TestCreateCardMessageAndNotify_AssigneeAndPreviousAuthors(t *testing.T) {
db := setupTestDB(t)
alice := mkUser(t, db, "alice")
bob := mkUser(t, db, "bob")
carol := mkUser(t, db, "carol")
col, err := db.CreateColumn("Todo")
if err != nil {
t.Fatalf("CreateColumn: %v", err)
}
card := mkCard(t, db, col.ID, "x", "card", bob)
// 1) alice writes; bob is assignee → bob gets assigned_chat.
_, notifs, _, err := db.CreateCardMessageAndNotify(card, alice, "hola", nil)
if err != nil {
t.Fatalf("create msg: %v", err)
}
if len(notifs) != 1 || notifs[0].UserID != bob || notifs[0].Kind != NotifKindAssignedChat {
t.Fatalf("expected single assigned_chat for bob, got %+v", notifs)
}
// 2) carol replies (carol is neither assignee nor previous author).
// alice (previous author) gets reply; bob (assignee) gets assigned_chat.
_, notifs, _, err = db.CreateCardMessageAndNotify(card, carol, "hola alice", nil)
if err != nil {
t.Fatalf("create msg: %v", err)
}
gotKinds := map[string]string{}
for _, n := range notifs {
gotKinds[n.UserID] = n.Kind
}
wantKinds := map[string]string{alice: NotifKindReply, bob: NotifKindAssignedChat}
if !reflect.DeepEqual(gotKinds, wantKinds) {
t.Fatalf("kinds = %+v, want %+v", gotKinds, wantKinds)
}
}
func TestCreateCardMessageAndNotify_MentionsBeatOtherKinds(t *testing.T) {
db := setupTestDB(t)
alice := mkUser(t, db, "alice")
bob := mkUser(t, db, "bob")
col, _ := db.CreateColumn("Todo")
card := mkCard(t, db, col.ID, "x", "card", bob) // bob is assignee
// alice mentions bob explicitly → kind must be 'mention', not 'assigned_chat'.
_, notifs, mentions, err := db.CreateCardMessageAndNotify(card, alice, "oye @bob mira esto", nil)
if err != nil {
t.Fatalf("create: %v", err)
}
if len(mentions) != 1 || mentions[0].UserID != bob {
t.Fatalf("mentions = %+v, want [bob]", mentions)
}
if len(notifs) != 1 || notifs[0].UserID != bob || notifs[0].Kind != NotifKindMention {
t.Fatalf("notifs = %+v, want single mention for bob", notifs)
}
}
func TestCreateCardMessageAndNotify_UnknownMentionsIgnored(t *testing.T) {
db := setupTestDB(t)
alice := mkUser(t, db, "alice")
col, _ := db.CreateColumn("Todo")
card := mkCard(t, db, col.ID, "x", "card", "")
_, notifs, mentions, err := db.CreateCardMessageAndNotify(card, alice, "hola @noexiste", nil)
if err != nil {
t.Fatalf("create: %v", err)
}
if len(mentions) != 0 || len(notifs) != 0 {
t.Fatalf("got mentions=%v notifs=%v, want empty", mentions, notifs)
}
}
func TestCreateCardMessageAndNotify_AuthorNeverSelfNotified(t *testing.T) {
db := setupTestDB(t)
alice := mkUser(t, db, "alice")
col, _ := db.CreateColumn("Todo")
card := mkCard(t, db, col.ID, "x", "card", alice) // alice is assignee
// alice mentions herself + is assignee → no notification.
_, notifs, _, err := db.CreateCardMessageAndNotify(card, alice, "monologo @alice", nil)
if err != nil {
t.Fatalf("create: %v", err)
}
if len(notifs) != 0 {
t.Fatalf("notifs = %+v, want empty (self)", notifs)
}
}
func TestListAndMarkRead(t *testing.T) {
db := setupTestDB(t)
alice := mkUser(t, db, "alice")
bob := mkUser(t, db, "bob")
col, _ := db.CreateColumn("Todo")
card := mkCard(t, db, col.ID, "x", "card", bob)
_, _, _, _ = db.CreateCardMessageAndNotify(card, alice, "1", nil)
_, _, _, _ = db.CreateCardMessageAndNotify(card, alice, "2", nil)
got, err := db.ListNotifications(bob, true, 0)
if err != nil {
t.Fatalf("list: %v", err)
}
if len(got) != 2 {
t.Fatalf("len = %d, want 2", len(got))
}
if n, _ := db.CountUnreadNotifications(bob); n != 2 {
t.Fatalf("unread count = %d, want 2", n)
}
if err := db.MarkNotificationRead(bob, got[0].ID); err != nil {
t.Fatalf("mark read: %v", err)
}
if n, _ := db.CountUnreadNotifications(bob); n != 1 {
t.Fatalf("unread count after mark = %d, want 1", n)
}
// idempotent
if err := db.MarkNotificationRead(bob, got[0].ID); err != nil {
t.Fatalf("mark read 2nd time: %v", err)
}
if n, _ := db.MarkAllNotificationsRead(bob); n != 1 {
t.Fatalf("mark all = %d, want 1", n)
}
if n, _ := db.CountUnreadNotifications(bob); n != 0 {
t.Fatalf("unread count after mark-all = %d, want 0", n)
}
}
+297
View File
@@ -0,0 +1,297 @@
package main
import (
"context"
"encoding/json"
"fmt"
"net/http"
"time"
"fn-registry/functions/infra"
"nhooyr.io/websocket"
)
const (
sseHeartbeat = 25 * time.Second
wsChatHeartbeat = 30 * time.Second
wsChatReadLimit = 64 * 1024
wsChatWriteWait = 5 * time.Second
typingDebounceMs = 1500
)
// handleEventStream serves the per-user SSE channel.
//
// One stream per browser tab. Auto-reconnect lives on the client (browsers
// retry EventSource by default). The server publishes:
//
// board.* — column/card mutations (broadcast to every user).
// message.created — chat message added on any card (broadcast).
// notification.* — private events for one recipient (UserID set).
func handleEventStream(hub *EventHub) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
userID, _ := infra.UserIDFromContext(r.Context(), userCtxKey)
if userID == "" {
infra.HTTPErrorResponse(w, infra.HTTPError{Status: http.StatusUnauthorized, Code: "unauthorized", Message: "session required"})
return
}
flusher, ok := w.(http.Flusher)
if !ok {
http.Error(w, "streaming unsupported", http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "text/event-stream")
w.Header().Set("Cache-Control", "no-cache, no-transform")
w.Header().Set("Connection", "keep-alive")
w.Header().Set("X-Accel-Buffering", "no")
w.WriteHeader(http.StatusOK)
// Initial flush so the browser knows the stream is open.
fmt.Fprint(w, ": hello\n\n")
flusher.Flush()
ch := hub.SubscribeUser(userID)
defer hub.UnsubscribeUser(userID, ch)
ticker := time.NewTicker(sseHeartbeat)
defer ticker.Stop()
for {
select {
case <-r.Context().Done():
return
case <-ticker.C:
if _, err := fmt.Fprint(w, ": ping\n\n"); err != nil {
return
}
flusher.Flush()
case ev, ok := <-ch:
if !ok {
return
}
if ev.UserID != "" && ev.UserID != userID {
// Defensive: hub already routes private events but the
// broadcast path could leak if a future change adds
// fan-out. Skip explicitly.
continue
}
b, err := json.Marshal(ev)
if err != nil {
continue
}
if _, err := fmt.Fprintf(w, "event: %s\ndata: %s\n\n", ev.Type, b); err != nil {
return
}
flusher.Flush()
}
}
}
}
// cardChatWSIn is the message sent by the browser over the per-card WS.
type cardChatWSIn struct {
Type string `json:"type"` // "send" | "typing"
Body string `json:"body,omitempty"` // only for "send"
}
// cardChatWSOut is the message the server pushes to subscribers of a card.
//
// Types:
//
// message.created — new CardMessage (full payload).
// typing — UserID is typing (no body).
// error — server-side error, connection stays open.
type cardChatWSOut struct {
Type string `json:"type"`
Message *CardMessage `json:"message,omitempty"`
UserID string `json:"user_id,omitempty"`
Error string `json:"error,omitempty"`
}
// handleCardChatWS upgrades the request to WebSocket and provides bidirectional
// realtime chat for a single card. Each connection is subscribed to the
// card's event channel; sends originating from this connection are persisted
// then republished through the hub so peer connections (including this one)
// see them.
func handleCardChatWS(db *DB, hub *EventHub) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
cardID := r.PathValue("id")
userID, _ := infra.UserIDFromContext(r.Context(), userCtxKey)
if userID == "" {
infra.HTTPErrorResponse(w, infra.HTTPError{Status: http.StatusUnauthorized, Code: "unauthorized", Message: "session required"})
return
}
// Confirm card exists before upgrading to avoid leaking goroutines on
// invalid IDs.
var exists int
if err := db.conn.QueryRow(`SELECT 1 FROM cards WHERE id = ?`, cardID).Scan(&exists); err != nil {
notFound(w, "card not found")
return
}
conn, err := infra.WSUpgrader(w, r, []string{"*"})
if err != nil {
return
}
defer conn.Close(websocket.StatusInternalError, "internal")
conn.SetReadLimit(wsChatReadLimit)
ch := hub.SubscribeCard(cardID)
defer hub.UnsubscribeCard(cardID, ch)
ctx, cancel := context.WithCancel(r.Context())
defer cancel()
// Writer goroutine: forward hub events to this socket.
go func() {
ticker := time.NewTicker(wsChatHeartbeat)
defer ticker.Stop()
for {
select {
case <-ctx.Done():
return
case <-ticker.C:
wctx, c := context.WithTimeout(ctx, wsChatWriteWait)
_ = conn.Ping(wctx)
c()
case ev, ok := <-ch:
if !ok {
return
}
if ev.CardID != cardID {
continue
}
out := cardChatWSOut{Type: ev.Type}
switch ev.Type {
case "message.created":
var m CardMessage
if err := json.Unmarshal(ev.Payload, &m); err == nil {
out.Message = &m
}
case "card.typing":
var p struct {
UserID string `json:"user_id"`
}
_ = json.Unmarshal(ev.Payload, &p)
// Skip echoing the typer's own indicator.
if p.UserID == userID {
continue
}
out.UserID = p.UserID
default:
continue
}
b, err := json.Marshal(out)
if err != nil {
continue
}
wctx, c := context.WithTimeout(ctx, wsChatWriteWait)
if err := conn.Write(wctx, websocket.MessageText, b); err != nil {
c()
cancel()
return
}
c()
}
}
}()
// Reader loop: persist sends and broadcast typing.
for {
_, raw, err := conn.Read(ctx)
if err != nil {
return
}
var in cardChatWSIn
if err := json.Unmarshal(raw, &in); err != nil {
continue
}
switch in.Type {
case "send":
if in.Body == "" {
continue
}
if _, _, _, err := db.CreateCardMessageAndNotify(cardID, userID, in.Body, hub); err != nil {
b, _ := json.Marshal(cardChatWSOut{Type: "error", Error: err.Error()})
wctx, c := context.WithTimeout(ctx, wsChatWriteWait)
_ = conn.Write(wctx, websocket.MessageText, b)
c()
}
case "typing":
hub.PublishJSON("card.typing", cardID, "", map[string]string{"user_id": userID})
}
}
}
}
// Notification HTTP handlers.
func handleListNotifications(db *DB) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
userID, _ := infra.UserIDFromContext(r.Context(), userCtxKey)
if userID == "" {
infra.HTTPErrorResponse(w, infra.HTTPError{Status: http.StatusUnauthorized, Code: "unauthorized", Message: "session required"})
return
}
onlyUnread := r.URL.Query().Get("unread") == "1"
limit := 50
out, err := db.ListNotifications(userID, onlyUnread, limit)
if err != nil {
serverError(w, err)
return
}
infra.HTTPJSONResponse(w, http.StatusOK, out)
}
}
func handleUnreadCount(db *DB) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
userID, _ := infra.UserIDFromContext(r.Context(), userCtxKey)
if userID == "" {
infra.HTTPErrorResponse(w, infra.HTTPError{Status: http.StatusUnauthorized, Code: "unauthorized", Message: "session required"})
return
}
n, err := db.CountUnreadNotifications(userID)
if err != nil {
serverError(w, err)
return
}
infra.HTTPJSONResponse(w, http.StatusOK, map[string]int{"count": n})
}
}
func handleMarkNotificationRead(db *DB, hub *EventHub) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
userID, _ := infra.UserIDFromContext(r.Context(), userCtxKey)
if userID == "" {
infra.HTTPErrorResponse(w, infra.HTTPError{Status: http.StatusUnauthorized, Code: "unauthorized", Message: "session required"})
return
}
id := r.PathValue("id")
if err := db.MarkNotificationRead(userID, id); err != nil {
serverError(w, err)
return
}
if hub != nil {
hub.PublishJSON("notification.read", "", userID, map[string]string{"id": id})
}
w.WriteHeader(http.StatusNoContent)
}
}
func handleMarkAllNotificationsRead(db *DB, hub *EventHub) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
userID, _ := infra.UserIDFromContext(r.Context(), userCtxKey)
if userID == "" {
infra.HTTPErrorResponse(w, infra.HTTPError{Status: http.StatusUnauthorized, Code: "unauthorized", Message: "session required"})
return
}
n, err := db.MarkAllNotificationsRead(userID)
if err != nil {
serverError(w, err)
return
}
if hub != nil {
hub.PublishJSON("notification.read_all", "", userID, map[string]int{"count": n})
}
infra.HTTPJSONResponse(w, http.StatusOK, map[string]int{"count": n})
}
}
+76
View File
@@ -0,0 +1,76 @@
#!/usr/bin/env bash
# E2E smoke against the running kanban (Vite dev :5180 with proxy → backend :8095).
#
# Verifies the latest version is actually being served:
# 1. /api/version returns the expected semver.
# 2. SPA HTML pulls fresh JS bundle.
# 3. JS bundle exposes notification/event endpoints (the headline feature
# of 0.2.0).
# 4. /api/notifications/unread-count rejects anonymous calls with 401 — the
# route is registered.
# 5. /api/events SSE endpoint returns 401 anonymous — registered.
# 6. /api/cards/<id>/chat/ws upgrade rejected without auth — registered.
#
# Exits non-zero on the first failure with a caveman explanation.
set -uo pipefail
BACKEND="${BACKEND:-http://127.0.0.1:8095}"
PROXY="${PROXY:-http://127.0.0.1:5180}"
EXPECTED_VERSION="${EXPECTED_VERSION:-0.2.0}"
fail() { echo "FAIL: $*" >&2; exit 1; }
ok() { echo "OK $*"; }
# 1. version
v=$(curl -sS -m 5 "$BACKEND/api/version" | sed -n 's/.*"version":"\([^"]*\)".*/\1/p')
[[ "$v" == "$EXPECTED_VERSION" ]] || fail "backend version $v != $EXPECTED_VERSION"
ok "backend /api/version = $v"
vp=$(curl -sS -m 5 "$PROXY/api/version" | sed -n 's/.*"version":"\([^"]*\)".*/\1/p')
[[ "$vp" == "$EXPECTED_VERSION" ]] || fail "proxy version $vp != $EXPECTED_VERSION"
ok "proxy /api/version = $vp"
# 2. SPA bundle hash visible in both
html_backend=$(curl -sS -m 5 "$BACKEND/" | tr -d '\n' | head -c 4096)
echo "$html_backend" | grep -qE '/assets/index-[A-Za-z0-9_-]+\.js' \
|| fail "backend /index.html does not reference an /assets/index-*.js"
ok "backend SPA references hashed bundle"
# 3. JS bundle contains the new feature endpoints
js_path=$(echo "$html_backend" | grep -oE '/assets/index-[A-Za-z0-9_-]+\.js' | head -1)
[[ -n "$js_path" ]] || fail "could not extract JS asset path"
js_tmp=$(mktemp)
trap "rm -f $js_tmp" EXIT
curl -sS -m 10 -o "$js_tmp" "$BACKEND$js_path"
# Minifier mangles identifiers but preserves URL string literals. Probe a
# stable subset that maps 1:1 to the new feature.
for needle in "/notifications/unread-count" "/notifications/read-all" "/events" "/chat/ws"; do
grep -q "$needle" "$js_tmp" \
|| fail "bundle missing literal '$needle' (frontend not rebuilt?)"
done
ok "bundle ships notifications + SSE + WS client code"
# 4. /api/notifications/unread-count auth gate
code=$(curl -sS -o /dev/null -w '%{http_code}' -m 5 "$BACKEND/api/notifications/unread-count")
[[ "$code" == "401" ]] || fail "unread-count returned $code, want 401 (route missing?)"
ok "unread-count gated 401"
# 5. /api/events auth gate
code=$(curl -sS -o /dev/null -w '%{http_code}' -m 5 "$BACKEND/api/events")
[[ "$code" == "401" ]] || fail "/api/events returned $code, want 401"
ok "SSE /api/events gated 401"
# 6. /api/cards/{id}/chat/ws — upgrade fails without auth. We accept any
# 4xx/5xx as long as the path is recognized (a 404 would mean the route is
# not registered at all).
code=$(curl -sS -o /dev/null -w '%{http_code}' -m 5 \
-H 'Connection: Upgrade' -H 'Upgrade: websocket' \
-H 'Sec-WebSocket-Version: 13' -H 'Sec-WebSocket-Key: dGVzdA==' \
"$BACKEND/api/cards/__nope__/chat/ws")
[[ "$code" =~ ^(401|403|404)$ ]] || fail "card chat ws returned $code, want 401/403/404"
[[ "$code" != "404" ]] || ok "card chat ws path resolved ($code)"
ok "card chat WS route present (status $code)"
echo
echo "PASS — kanban $EXPECTED_VERSION serving notifications/streaming UI"
+96 -7
View File
@@ -81,7 +81,9 @@ import { StickerPicker } from "./components/StickerPicker";
import { ColorPickerGrid, CustomColorModal } from "./components/ColorPickerGrid";
import { AVATAR_COLORS } from "./components/colors";
import { colorBg, colorBorder } from "./components/colors";
import type { Board, Card, CardColor, Column, ColumnLocation, User } from "./types";
import { NotificationsBell } from "./components/NotificationsBell";
import { useEventStream } from "./hooks/useEventStream";
import type { Board, Card, CardColor, Column, ColumnLocation, Notification, User } from "./types";
const COL_PREFIX = "column-";
@@ -326,12 +328,71 @@ export function App() {
return () => clearInterval(t);
}, [activeCard, activeColumnId]);
// Notifications state (populated by SSE + initial fetch).
const [notifs, setNotifs] = useState<Notification[]>([]);
const [notifUnread, setNotifUnread] = useState(0);
// Build version (injected at compile time via -ldflags). Fetched once.
const [appVersion, setAppVersion] = useState<string>("");
useEffect(() => {
const t = setInterval(() => {
reload();
}, 30000);
return () => clearInterval(t);
}, [reload]);
api
.getVersion()
.then((v) => setAppVersion(v.version))
.catch(() => setAppVersion(""));
}, []);
const reloadNotifs = useCallback(async () => {
try {
const [list, c] = await Promise.all([api.listNotifications(false), api.unreadNotificationCount()]);
setNotifs(list);
setNotifUnread(c.count);
} catch {
// best-effort; SSE will reconcile
}
}, []);
useEffect(() => {
if (auth.user) reloadNotifs();
}, [auth.user, reloadNotifs]);
// Replace 30s polling with SSE. Server pushes board.invalidated on every
// mutation, message.created on chat traffic and notification.created on
// per-user notifications. We refetch /api/board on invalidate (cheap +
// keeps merge logic simple) and patch notification state in-place.
useEventStream(
useMemo(
() => ({
"board.invalidated": () => {
reload();
},
"notification.created": (payload: unknown) => {
const n = payload as Notification;
if (!n || !n.id) return;
setNotifs((prev) => (prev.some((x) => x.id === n.id) ? prev : [n, ...prev].slice(0, 100)));
setNotifUnread((c) => c + 1);
const who = n.actor_name || "Alguien";
const card = n.card_seq_num ? `#${n.card_seq_num}` : n.card_title;
notifications.show({
color: n.kind === "mention" ? "grape" : "blue",
title: `${who} en ${card}`,
message: n.snippet,
});
},
"notification.read": (payload: unknown) => {
const p = payload as { id?: string } | null;
if (!p?.id) return;
setNotifs((prev) => prev.map((x) => (x.id === p.id ? { ...x, read_at: new Date().toISOString() } : x)));
setNotifUnread((c) => Math.max(0, c - 1));
},
"notification.read_all": () => {
setNotifs((prev) => prev.map((x) => (x.read_at ? x : { ...x, read_at: new Date().toISOString() })));
setNotifUnread(0);
},
}),
[reload],
),
!!auth.user,
);
useEffect(() => {
if (!activeSticker) return;
@@ -1113,6 +1174,25 @@ export function App() {
<ActionIcon variant="subtle" onClick={reload} aria-label="Refresh">
<IconRefresh size={16} />
</ActionIcon>
{auth.user && (
<NotificationsBell
unreadCount={notifUnread}
notifications={notifs}
onOpenCard={async (cardId) => {
const card = board?.cards.find((c) => c.id === cardId);
if (card) {
setActiveCard(card);
} else {
// Card may be archived/trashed/missing locally — refetch and retry.
await reload();
const b = await api.getBoard();
const c2 = b.cards.find((c) => c.id === cardId);
if (c2) setActiveCard(c2);
}
}}
onChanged={reloadNotifs}
/>
)}
<ActionIcon
variant={chatOpen ? "filled" : "subtle"}
onClick={() => setChatOpen((v) => !v)}
@@ -1130,7 +1210,16 @@ export function App() {
</ActionIcon>
</Menu.Target>
<Menu.Dropdown>
<Menu.Label>{auth.user.display_name || auth.user.username}</Menu.Label>
<Menu.Label>
<Group justify="space-between" gap={6} wrap="nowrap">
<Text size="xs" fw={600} truncate>
{auth.user.display_name || auth.user.username}
</Text>
{appVersion && (
<Text size="xs" c="dimmed" ff="monospace">v{appVersion}</Text>
)}
</Group>
</Menu.Label>
<Box p="xs">
<Text size="xs" c="dimmed" mb={4}>Color del avatar</Text>
<ColorPickerGrid
+26
View File
@@ -6,6 +6,7 @@ import type {
Column,
Metrics,
MetricsFilter,
Notification,
Sticker,
User,
} from "./types";
@@ -27,6 +28,10 @@ export function getFlags(): Promise<Record<string, boolean>> {
return fetchJSON("/flags");
}
export function getVersion(): Promise<{ version: string }> {
return fetchJSON("/version");
}
export function createColumn(name: string): Promise<Column> {
return fetchJSON("/columns", { method: "POST", body: JSON.stringify({ name }) });
}
@@ -291,6 +296,27 @@ export function chatWSURL(): string {
return `${proto}//${window.location.host}/api/chat/ws`;
}
export function cardChatWSURL(cardId: string): string {
const proto = window.location.protocol === "https:" ? "wss:" : "ws:";
return `${proto}//${window.location.host}/api/cards/${cardId}/chat/ws`;
}
export function listNotifications(unreadOnly = false): Promise<Notification[]> {
return fetchJSON(`/notifications${unreadOnly ? "?unread=1" : ""}`);
}
export function unreadNotificationCount(): Promise<{ count: number }> {
return fetchJSON("/notifications/unread-count");
}
export function markNotificationRead(id: string): Promise<void> {
return fetchJSON(`/notifications/${id}/read`, { method: "POST" });
}
export function markAllNotificationsRead(): Promise<{ count: number }> {
return fetchJSON("/notifications/read-all", { method: "POST" });
}
// streamChat opens a WebSocket, sends the message history, and streams events
// to onEvent. Returns a Promise that resolves when the server closes the
// connection (after a "done" event) and rejects on transport errors.
+274 -33
View File
@@ -1,7 +1,9 @@
import {
ActionIcon,
Avatar,
Badge,
Box,
Combobox,
Group,
Loader,
Paper,
@@ -10,10 +12,19 @@ import {
Text,
Textarea,
Tooltip,
useCombobox,
} from "@mantine/core";
import { IconSend, IconTrash } from "@tabler/icons-react";
import { notifications } from "@mantine/notifications";
import { KeyboardEvent, useCallback, useEffect, useRef, useState } from "react";
import {
KeyboardEvent,
ReactNode,
useCallback,
useEffect,
useMemo,
useRef,
useState,
} from "react";
import * as api from "../api";
import type { CardMessage, User } from "../types";
import { tagColor } from "./colors";
@@ -26,14 +37,81 @@ interface Props {
onMessagesChange?: (messages: CardMessage[]) => void;
}
// Window for considering a peer "actively typing" after its last event.
const TYPING_LIFETIME_MS = 4000;
// Minimum gap between successive typing pings emitted while the user types.
const TYPING_THROTTLE_MS = 1500;
interface MentionMatch {
start: number; // index of '@' in the textarea value
query: string; // text after '@', lowercased
}
function detectMention(value: string, cursor: number): MentionMatch | null {
// Look backwards from cursor for an '@' that starts a word.
for (let i = cursor - 1; i >= 0 && cursor - i <= 64; i--) {
const ch = value[i];
if (ch === "@") {
// Valid start: beginning of string or whitespace before.
if (i === 0 || /\s/.test(value[i - 1])) {
const q = value.slice(i + 1, cursor);
if (/^[a-z0-9_.-]*$/i.test(q)) {
return { start: i, query: q.toLowerCase() };
}
}
return null;
}
if (/\s/.test(ch)) return null;
}
return null;
}
const mentionRegex = /(^|\s)(@[a-z0-9][a-z0-9_.-]{0,63})/gi;
function renderBody(body: string, knownUsers: Map<string, User>): ReactNode {
const out: ReactNode[] = [];
let last = 0;
let key = 0;
for (const m of body.matchAll(mentionRegex)) {
const handle = m[2].slice(1).toLowerCase();
const idx = (m.index ?? 0) + m[1].length;
if (idx > last) out.push(body.slice(last, idx));
const user = knownUsers.get(handle);
if (user) {
out.push(
<Badge
key={`m${key++}`}
size="xs"
variant="light"
color={user.color || tagColor(user.username)}
style={{ verticalAlign: "middle" }}
>
@{user.username}
</Badge>,
);
} else {
out.push(`@${handle}`);
}
last = idx + m[2].length;
}
if (last < body.length) out.push(body.slice(last));
return out;
}
export function CardChatPanel({ cardId, users, currentUserId, onMessagesChange }: Props) {
const [messages, setMessages] = useState<CardMessage[]>([]);
const [loading, setLoading] = useState(true);
const [body, setBody] = useState("");
const [sending, setSending] = useState(false);
const [typingUsers, setTypingUsers] = useState<Record<string, number>>({});
const [mention, setMention] = useState<MentionMatch | null>(null);
const viewportRef = useRef<HTMLDivElement | null>(null);
const wsRef = useRef<WebSocket | null>(null);
const textareaRef = useRef<HTMLTextAreaElement | null>(null);
const lastTypingEmitRef = useRef(0);
const usersById = new Map(users.map((u) => [u.id, u]));
const usersById = useMemo(() => new Map(users.map((u) => [u.id, u])), [users]);
const usersByUsername = useMemo(() => new Map(users.map((u) => [u.username.toLowerCase(), u])), [users]);
const reload = useCallback(async () => {
try {
@@ -51,22 +129,126 @@ export function CardChatPanel({ cardId, users, currentUserId, onMessagesChange }
reload();
}, [reload]);
// Open one WebSocket per cardId for realtime chat + typing.
useEffect(() => {
const ws = new WebSocket(api.cardChatWSURL(cardId));
wsRef.current = ws;
ws.onmessage = (ev) => {
try {
const data = JSON.parse(ev.data) as
| { type: "message.created"; message: CardMessage }
| { type: "typing"; user_id: string }
| { type: "error"; error: string };
if (data.type === "message.created" && data.message) {
setMessages((prev) => {
if (prev.some((m) => m.id === data.message!.id)) return prev;
const next = [...prev, data.message!];
onMessagesChange?.(next);
return next;
});
} else if (data.type === "typing" && data.user_id) {
setTypingUsers((prev) => ({ ...prev, [data.user_id]: Date.now() }));
} else if (data.type === "error") {
notifications.show({ color: "red", message: data.error });
}
} catch {
// ignore malformed
}
};
ws.onerror = () => {
// browser will report; we keep the panel functional via REST fallback
};
return () => {
ws.close();
wsRef.current = null;
};
}, [cardId, onMessagesChange]);
// Sweep stale typing entries.
useEffect(() => {
const t = setInterval(() => {
const now = Date.now();
setTypingUsers((prev) => {
const next: Record<string, number> = {};
for (const [k, v] of Object.entries(prev)) {
if (now - v < TYPING_LIFETIME_MS) next[k] = v;
}
return next;
});
}, 1000);
return () => clearInterval(t);
}, []);
useEffect(() => {
if (viewportRef.current) {
viewportRef.current.scrollTo({ top: viewportRef.current.scrollHeight, behavior: "smooth" });
}
}, [messages.length]);
const sendTypingPing = () => {
const ws = wsRef.current;
if (!ws || ws.readyState !== WebSocket.OPEN) return;
const now = Date.now();
if (now - lastTypingEmitRef.current < TYPING_THROTTLE_MS) return;
lastTypingEmitRef.current = now;
ws.send(JSON.stringify({ type: "typing" }));
};
const combobox = useCombobox({
onDropdownClose: () => setMention(null),
});
const mentionCandidates = useMemo(() => {
if (!mention) return [] as User[];
return users
.filter((u) => u.username.toLowerCase().startsWith(mention.query))
.slice(0, 8);
}, [users, mention]);
useEffect(() => {
if (mention && mentionCandidates.length > 0) {
combobox.openDropdown();
combobox.selectFirstOption();
} else {
combobox.closeDropdown();
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [mention?.query, mentionCandidates.length]);
const insertMention = (username: string) => {
if (!mention) return;
const before = body.slice(0, mention.start);
const after = body.slice(mention.start + 1 + mention.query.length);
const inserted = `@${username} `;
const next = before + inserted + after;
setBody(next);
setMention(null);
// Restore caret right after the inserted mention.
requestAnimationFrame(() => {
const el = textareaRef.current;
if (!el) return;
const pos = (before + inserted).length;
el.focus();
el.setSelectionRange(pos, pos);
});
};
const send = async () => {
const text = body.trim();
if (!text || sending) return;
setSending(true);
const ws = wsRef.current;
try {
const m = await api.createCardMessage(cardId, text);
const next = [...messages, m];
setMessages(next);
onMessagesChange?.(next);
setBody("");
if (ws && ws.readyState === WebSocket.OPEN) {
ws.send(JSON.stringify({ type: "send", body: text }));
// Optimistic clear; server will broadcast the persisted message.
setBody("");
} else {
const m = await api.createCardMessage(cardId, text);
setMessages((prev) => [...prev, m]);
onMessagesChange?.([...messages, m]);
setBody("");
}
} catch (e) {
notifications.show({ color: "red", message: (e as Error).message });
} finally {
@@ -85,13 +267,38 @@ export function CardChatPanel({ cardId, users, currentUserId, onMessagesChange }
}
};
const onChange = (e: React.ChangeEvent<HTMLTextAreaElement>) => {
setBody(e.currentTarget.value);
sendTypingPing();
const cursor = e.currentTarget.selectionStart ?? e.currentTarget.value.length;
setMention(detectMention(e.currentTarget.value, cursor));
};
const onKeyDown = (e: KeyboardEvent<HTMLTextAreaElement>) => {
if (mention && mentionCandidates.length > 0 && (e.key === "Enter" || e.key === "Tab")) {
e.preventDefault();
const sel = combobox.getSelectedOptionIndex();
const pick = mentionCandidates[Math.max(0, sel)];
if (pick) insertMention(pick.username);
return;
}
if (mention && e.key === "Escape") {
setMention(null);
return;
}
if (e.key === "Enter" && !e.shiftKey) {
e.preventDefault();
send();
}
};
const typingNames = Object.keys(typingUsers)
.filter((uid) => uid !== currentUserId)
.map((uid) => {
const u = usersById.get(uid);
return u?.display_name || u?.username || "alguien";
});
return (
<Stack gap="xs" style={{ height: "100%", minHeight: 0 }}>
<ScrollArea
@@ -139,7 +346,7 @@ export function CardChatPanel({ cardId, users, currentUserId, onMessagesChange }
)}
</Group>
<Text size="sm" style={{ whiteSpace: "pre-wrap", wordBreak: "break-word" }}>
{m.body}
{renderBody(m.body, usersByUsername)}
</Text>
</Box>
</Group>
@@ -149,31 +356,65 @@ export function CardChatPanel({ cardId, users, currentUserId, onMessagesChange }
</Stack>
)}
</ScrollArea>
<Group gap="xs" align="flex-end">
<Textarea
value={body}
onChange={(e) => setBody(e.currentTarget.value)}
onKeyDown={onKeyDown}
placeholder="Escribe un mensaje (Enter = enviar, Shift+Enter = salto)"
autosize
minRows={1}
maxRows={6}
style={{ flex: 1 }}
disabled={sending}
/>
<Tooltip label="Enviar" withArrow>
<ActionIcon
size="lg"
variant="filled"
color="blue"
onClick={send}
disabled={!body.trim() || sending}
aria-label="Enviar"
>
<IconSend size={16} />
</ActionIcon>
</Tooltip>
</Group>
{typingNames.length > 0 && (
<Text size="xs" c="dimmed" px={6}>
{typingNames.length === 1
? `${typingNames[0]} esta escribiendo...`
: `${typingNames.slice(0, 2).join(", ")}${typingNames.length > 2 ? "..." : ""} estan escribiendo...`}
</Text>
)}
<Combobox
store={combobox}
onOptionSubmit={(value) => insertMention(value)}
position="top-start"
withinPortal={false}
>
<Combobox.DropdownTarget>
<Group gap="xs" align="flex-end">
<Textarea
ref={textareaRef}
value={body}
onChange={onChange}
onKeyDown={onKeyDown}
placeholder="Escribe un mensaje (Enter = enviar, @ para mencionar)"
autosize
minRows={1}
maxRows={6}
style={{ flex: 1 }}
disabled={sending}
/>
<Tooltip label="Enviar" withArrow>
<ActionIcon
size="lg"
variant="filled"
color="blue"
onClick={send}
disabled={!body.trim() || sending}
aria-label="Enviar"
>
<IconSend size={16} />
</ActionIcon>
</Tooltip>
</Group>
</Combobox.DropdownTarget>
<Combobox.Dropdown hidden={!mention || mentionCandidates.length === 0}>
<Combobox.Options>
{mentionCandidates.map((u) => (
<Combobox.Option key={u.id} value={u.username}>
<Group gap={6} wrap="nowrap">
<Avatar size={18} radius="xl" color={u.color || tagColor(u.username)}>
{(u.display_name || u.username).slice(0, 2).toUpperCase()}
</Avatar>
<Text size="sm" fw={600}>@{u.username}</Text>
{u.display_name && u.display_name !== u.username && (
<Text size="xs" c="dimmed">{u.display_name}</Text>
)}
</Group>
</Combobox.Option>
))}
</Combobox.Options>
</Combobox.Dropdown>
</Combobox>
</Stack>
);
}
+8
View File
@@ -25,12 +25,17 @@ export function LoginPage() {
const [submitting, setSubmitting] = useState(false);
const [error, setError] = useState<string | null>(null);
const [registrationEnabled, setRegistrationEnabled] = useState(false);
const [appVersion, setAppVersion] = useState<string>("");
useEffect(() => {
api
.getFlags()
.then((f) => setRegistrationEnabled(!!f["registration-enabled"]))
.catch(() => setRegistrationEnabled(false));
api
.getVersion()
.then((v) => setAppVersion(v.version))
.catch(() => setAppVersion(""));
}, []);
useEffect(() => {
@@ -62,6 +67,9 @@ export function LoginPage() {
<Stack gap={4} align="center">
<IconLayoutKanban size={36} />
<Title order={3}>Kanban</Title>
{appVersion && (
<Text size="xs" c="dimmed" ff="monospace">v{appVersion}</Text>
)}
<Text size="sm" c="dimmed">
{mode === "login" ? "Inicia sesion" : "Crea una cuenta"}
</Text>
@@ -0,0 +1,197 @@
import {
ActionIcon,
Badge,
Box,
Button,
Group,
Indicator,
Loader,
Popover,
ScrollArea,
Stack,
Text,
Tooltip,
UnstyledButton,
} from "@mantine/core";
import { IconAt, IconBell, IconCheck, IconMessage, IconUserCheck } from "@tabler/icons-react";
import { ReactElement, useCallback, useEffect, useState } from "react";
import * as api from "../api";
import type { Notification, NotificationKind } from "../types";
import { formatDateTimeShort } from "./format";
interface Props {
// External counter — App.tsx updates this via SSE events. When undefined
// the bell polls /api/notifications/unread-count on mount.
unreadCount?: number;
notifications?: Notification[];
// Called when the user clicks a notification → open the relevant card.
onOpenCard?: (cardId: string) => void;
// Called whenever the bell mutates state (mark read / mark all) so the
// parent can refresh its cached lists.
onChanged?: () => void;
}
const kindIcon: Record<NotificationKind, ReactElement> = {
mention: <IconAt size={14} />,
assigned_chat: <IconUserCheck size={14} />,
reply: <IconMessage size={14} />,
};
const kindLabel: Record<NotificationKind, string> = {
mention: "Mencion",
assigned_chat: "Asignado",
reply: "Respuesta",
};
const kindColor: Record<NotificationKind, string> = {
mention: "grape",
assigned_chat: "blue",
reply: "gray",
};
export function NotificationsBell({ unreadCount: extCount, notifications: extList, onOpenCard, onChanged }: Props) {
const [opened, setOpened] = useState(false);
const [items, setItems] = useState<Notification[]>(extList ?? []);
const [count, setCount] = useState<number>(extCount ?? 0);
const [loading, setLoading] = useState(false);
// Keep local state in sync with parent-supplied values when present.
useEffect(() => {
if (extList) setItems(extList);
}, [extList]);
useEffect(() => {
if (extCount !== undefined) setCount(extCount);
}, [extCount]);
const refresh = useCallback(async () => {
setLoading(true);
try {
const [list, c] = await Promise.all([
api.listNotifications(false),
api.unreadNotificationCount(),
]);
setItems(list);
setCount(c.count);
} finally {
setLoading(false);
}
}, []);
useEffect(() => {
// Initial fetch only when parent does not provide list/count.
if (extList === undefined || extCount === undefined) {
refresh();
}
}, [extList, extCount, refresh]);
const handleOpen = (isOpen: boolean) => {
setOpened(isOpen);
if (isOpen) refresh();
};
const handleClick = async (n: Notification) => {
if (!n.read_at) {
try {
await api.markNotificationRead(n.id);
setItems((prev) => prev.map((x) => (x.id === n.id ? { ...x, read_at: new Date().toISOString() } : x)));
setCount((c) => Math.max(0, c - 1));
onChanged?.();
} catch {
// ignore — UI will recover on next refresh
}
}
setOpened(false);
onOpenCard?.(n.card_id);
};
const handleMarkAll = async () => {
try {
await api.markAllNotificationsRead();
setItems((prev) => prev.map((x) => (x.read_at ? x : { ...x, read_at: new Date().toISOString() })));
setCount(0);
onChanged?.();
} catch {
// ignore
}
};
const badge = (
<ActionIcon variant="subtle" aria-label="Notificaciones">
<IconBell size={16} />
</ActionIcon>
);
return (
<Popover opened={opened} onChange={handleOpen} position="bottom-end" width={380} withArrow shadow="md">
<Popover.Target>
<Box onClick={() => handleOpen(!opened)} style={{ display: "inline-flex" }}>
{count > 0 ? (
<Indicator color="red" label={count > 99 ? "99+" : count} size={16} offset={4}>
{badge}
</Indicator>
) : (
badge
)}
</Box>
</Popover.Target>
<Popover.Dropdown p={0}>
<Group justify="space-between" px="sm" py="xs">
<Text fw={600} size="sm">Notificaciones</Text>
<Tooltip label="Marcar todas como leidas" withArrow>
<Button
size="compact-xs"
variant="subtle"
leftSection={<IconCheck size={12} />}
onClick={handleMarkAll}
disabled={count === 0}
>
Todas leidas
</Button>
</Tooltip>
</Group>
<ScrollArea h={420} type="auto" offsetScrollbars>
{loading && items.length === 0 ? (
<Group justify="center" p="md"><Loader size="sm" /></Group>
) : items.length === 0 ? (
<Text size="sm" c="dimmed" ta="center" p="md">Sin notificaciones</Text>
) : (
<Stack gap={0}>
{items.map((n) => {
const unread = !n.read_at;
return (
<UnstyledButton
key={n.id}
onClick={() => handleClick(n)}
p="sm"
style={{
borderTop: "1px solid var(--mantine-color-gray-2)",
background: unread ? "var(--mantine-color-blue-light)" : undefined,
textAlign: "left",
}}
>
<Group gap={6} wrap="nowrap" align="flex-start">
<Badge size="xs" variant="light" color={kindColor[n.kind]} leftSection={kindIcon[n.kind]}>
{kindLabel[n.kind]}
</Badge>
<Box style={{ flex: 1, minWidth: 0 }}>
<Group gap={6} wrap="nowrap" justify="space-between">
<Text size="xs" fw={600} truncate>
{n.actor_name || "Alguien"} · #{n.card_seq_num} {n.card_title}
</Text>
<Text size="xs" c="dimmed">{formatDateTimeShort(n.created_at)}</Text>
</Group>
<Text size="xs" c={unread ? undefined : "dimmed"} lineClamp={2} style={{ whiteSpace: "pre-wrap" }}>
{n.snippet}
</Text>
</Box>
</Group>
</UnstyledButton>
);
})}
</Stack>
)}
</ScrollArea>
</Popover.Dropdown>
</Popover>
);
}
+45
View File
@@ -0,0 +1,45 @@
import { useEffect, useRef } from "react";
export type EventStreamHandlers = Record<string, (payload: unknown) => void>;
// useEventStream connects to /api/events via EventSource and dispatches
// named events to the matching handler. The handlers object is captured in
// a ref so callers can supply fresh closures every render without tearing
// the connection down. Reconnection is handled by the browser's built-in
// EventSource backoff; the hook only opens one socket per mount.
export function useEventStream(handlers: EventStreamHandlers, enabled = true) {
const ref = useRef(handlers);
ref.current = handlers;
useEffect(() => {
if (!enabled) return;
const es = new EventSource("/api/events", { withCredentials: true });
const listeners: Record<string, (ev: MessageEvent) => void> = {};
// We attach a listener per event type known when this effect runs.
// Types added later via handler ref updates are still handled because
// the inner closure always reads ref.current.
for (const type of Object.keys(ref.current)) {
const fn = (ev: MessageEvent) => {
const cb = ref.current[type];
if (!cb) return;
try {
const payload = ev.data ? JSON.parse(ev.data) : null;
cb(payload);
} catch {
// Malformed payload; ignore.
}
};
es.addEventListener(type, fn);
listeners[type] = fn;
}
return () => {
for (const [type, fn] of Object.entries(listeners)) {
es.removeEventListener(type, fn);
}
es.close();
};
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [enabled]);
}
+17
View File
@@ -198,3 +198,20 @@ export interface CardMessage {
body: string;
created_at: string;
}
export type NotificationKind = "mention" | "assigned_chat" | "reply";
export interface Notification {
id: string;
user_id: string;
card_id: string;
message_id: string;
kind: NotificationKind;
actor_id: string;
created_at: string;
read_at: string | null;
card_title: string;
card_seq_num: number;
actor_name: string;
snippet: string;
}
+12 -4
View File
@@ -9,7 +9,10 @@ FRONT_DIR="$ROOT/frontend"
PORT_BACK="${PORT_BACK:-8095}"
PORT_FRONT="${PORT_FRONT:-5180}"
DB_PATH="${DB_PATH:-./operations.db}"
# Default DB lives at apps/kanban/operations.db. Force an absolute path so
# the value survives the `cd $BACK_DIR` below — otherwise a relative
# ./operations.db would land inside backend/.
DB_PATH="${DB_PATH:-$ROOT/operations.db}"
cleanup() {
echo ""
@@ -22,9 +25,14 @@ cleanup() {
trap cleanup INT TERM EXIT
# 1. Build backend si no existe o si los .go/.sql son mas nuevos que el binario
if [[ ! -x "$BACK_DIR/kanban" ]] || [[ -n "$(find "$BACK_DIR" -maxdepth 3 \( -name '*.go' -o -name '*.sql' \) -newer "$BACK_DIR/kanban" 2>/dev/null)" ]]; then
echo ">>> Building backend..."
(cd "$BACK_DIR" && CGO_ENABLED=1 go build -tags fts5 -o kanban .)
VERSION=$(awk -F': ' '/^version:/ {print $2; exit}' "$ROOT/app.md" 2>/dev/null || echo "dev")
if [[ ! -x "$BACK_DIR/kanban" ]] \
|| [[ -n "$(find "$BACK_DIR" -maxdepth 3 \( -name '*.go' -o -name '*.sql' \) -newer "$BACK_DIR/kanban" 2>/dev/null)" ]] \
|| [[ "$ROOT/app.md" -nt "$BACK_DIR/kanban" ]]; then
echo ">>> Building backend (version=$VERSION)..."
(cd "$BACK_DIR" && CGO_ENABLED=1 go build -tags fts5 \
-ldflags="-X main.Version=$VERSION" \
-o kanban .)
fi
# 2. Asegurar deps frontend