sojorn/go-backend/internal/realtime/hub.go
Patrick Britton 3c4680bdd7 Initial commit: Complete threaded conversation system with inline replies
**Major Features Added:**
- **Inline Reply System**: Replace compose screen with inline reply boxes
- **Thread Navigation**: Parent/child navigation with jump functionality
- **Chain Flow UI**: Reply counts, expand/collapse animations, visual hierarchy
- **Enhanced Animations**: Smooth transitions, hover effects, micro-interactions

 **Frontend Changes:**
- **ThreadedCommentWidget**: Complete rewrite with animations and navigation
- **ThreadNode Model**: Added parent references and descendant counting
- **ThreadedConversationScreen**: Integrated navigation handlers
- **PostDetailScreen**: Replaced with threaded conversation view
- **ComposeScreen**: Added reply indicators and context
- **PostActions**: Fixed visibility checks for chain buttons

 **Backend Changes:**
- **API Route**: Added /posts/:id/thread endpoint
- **Post Repository**: Include allow_chain and visibility fields in feed
- **Thread Handler**: Support for fetching post chains

 **UI/UX Improvements:**
- **Reply Context**: Clear indication when replying to specific posts
- **Character Counting**: 500 character limit with live counter
- **Visual Hierarchy**: Depth-based indentation and styling
- **Smooth Animations**: SizeTransition, FadeTransition, hover states
- **Chain Navigation**: Parent/child buttons with visual feedback

 **Technical Enhancements:**
- **Animation Controllers**: Proper lifecycle management
- **State Management**: Clean separation of concerns
- **Navigation Callbacks**: Reusable navigation system
- **Error Handling**: Graceful fallbacks and user feedback

This creates a Reddit-style threaded conversation experience with smooth
animations, inline replies, and intuitive navigation between posts in a chain.
2026-01-30 07:40:19 -06:00

76 lines
1.6 KiB
Go

package realtime
import (
"sync"
"time"
"github.com/gorilla/websocket"
"github.com/rs/zerolog/log"
)
type Client struct {
UserID string
Conn *websocket.Conn
Send chan interface{}
}
type Hub struct {
// Map userID -> set of clients (multi-device)
clients map[string]map[*Client]bool
mu sync.RWMutex
}
func NewHub() *Hub {
return &Hub{
clients: make(map[string]map[*Client]bool),
}
}
func (h *Hub) Register(client *Client) {
h.mu.Lock()
defer h.mu.Unlock()
if _, ok := h.clients[client.UserID]; !ok {
h.clients[client.UserID] = make(map[*Client]bool)
}
h.clients[client.UserID][client] = true
log.Info().Str("user_id", client.UserID).Msg("Registered WebSocket client")
}
func (h *Hub) Unregister(client *Client) {
h.mu.Lock()
defer h.mu.Unlock()
if userClients, ok := h.clients[client.UserID]; ok {
if _, exists := userClients[client]; exists {
client.Conn.Close()
delete(userClients, client)
if len(userClients) == 0 {
delete(h.clients, client.UserID)
}
log.Info().Str("user_id", client.UserID).Msg("Unregistered WebSocket client")
}
}
}
func (h *Hub) SendToUser(userID string, message interface{}) error {
h.mu.RLock()
userClients, ok := h.clients[userID]
h.mu.RUnlock()
if !ok {
return nil // User not connected
}
for client := range userClients {
// Use the channel to ensure single-writer concurrency
select {
case client.Send <- message:
case <-time.After(1 * time.Second):
log.Warn().Str("user_id", userID).Msg("Timed out sending message to client channel")
go h.Unregister(client)
}
}
return nil
}