feat(collab): redesign note collaboration with REST auto-save, opt-in live session, and block locking
MindSift CI & Docker Release / Build, Test in Container & Push (push) Successful in 59s
MindSift CI & Docker Release / Build, Test in Container & Push (push) Successful in 59s
This commit is contained in:
1 parent
50b017f387
commit
0e06cc5577
11 files changed
+1373
-239
No files matched your search
@@ -92,6 +92,24 @@ type docLockItem struct {
|
||||
expiresAt time.Time
|
||||
}
|
||||
|
||||
type blockLockItem struct {
|
||||
docID string
|
||||
blockID string
|
||||
userID string
|
||||
userName string
|
||||
userColor string
|
||||
lockedAt time.Time
|
||||
expiresAt time.Time
|
||||
}
|
||||
|
||||
type liveSessionItem struct {
|
||||
docID string
|
||||
initiatorID string
|
||||
initiatorName string
|
||||
startedAt time.Time
|
||||
expiresAt time.Time
|
||||
}
|
||||
|
||||
type DocStore interface {
|
||||
LoadDocumentState(docID string) (string, []domain.DocumentBlock, error)
|
||||
FlushDocumentState(docID string, title string, blocks []domain.DocumentBlock) error
|
||||
@@ -106,6 +124,8 @@ type Hub struct {
|
||||
sseUserRooms map[string]map[*SSEClient]bool
|
||||
sseRooms map[string]map[*SSEClient]bool
|
||||
locks map[string]*docLockItem
|
||||
blockLocks map[string]*blockLockItem // key: docID + ":" + blockID
|
||||
liveSessions map[string]*liveSessionItem // key: docID
|
||||
docRooms map[string]*DocRoom
|
||||
docStore DocStore
|
||||
stopCleanup chan struct{}
|
||||
@@ -121,6 +141,8 @@ func NewHub() *Hub {
|
||||
sseUserRooms: make(map[string]map[*SSEClient]bool),
|
||||
sseRooms: make(map[string]map[*SSEClient]bool),
|
||||
locks: make(map[string]*docLockItem),
|
||||
blockLocks: make(map[string]*blockLockItem),
|
||||
liveSessions: make(map[string]*liveSessionItem),
|
||||
docRooms: make(map[string]*DocRoom),
|
||||
stopCleanup: make(chan struct{}),
|
||||
}
|
||||
@@ -459,6 +481,187 @@ func (h *Hub) GetDocLock(docID string) *domain.DocLockInfo {
|
||||
}
|
||||
}
|
||||
|
||||
// Block Locking Methods (Option B)
|
||||
|
||||
func (h *Hub) AcquireBlockLock(docID, blockID, userID, userName, userColor string, ttl time.Duration) (*domain.BlockLockInfo, bool, *domain.BlockLockInfo) {
|
||||
h.mu.Lock()
|
||||
defer h.mu.Unlock()
|
||||
|
||||
now := time.Now()
|
||||
key := docID + ":" + blockID
|
||||
existing, exists := h.blockLocks[key]
|
||||
if exists && existing.expiresAt.After(now) {
|
||||
if existing.userID != userID {
|
||||
info := &domain.BlockLockInfo{
|
||||
DocID: existing.docID,
|
||||
BlockID: existing.blockID,
|
||||
UserID: existing.userID,
|
||||
UserName: existing.userName,
|
||||
UserColor: existing.userColor,
|
||||
ExpiresAt: existing.expiresAt.Format(time.RFC3339),
|
||||
}
|
||||
return nil, false, info
|
||||
}
|
||||
// Refresh lease
|
||||
existing.expiresAt = now.Add(ttl)
|
||||
if userName != "" {
|
||||
existing.userName = userName
|
||||
}
|
||||
if userColor != "" {
|
||||
existing.userColor = userColor
|
||||
}
|
||||
info := &domain.BlockLockInfo{
|
||||
DocID: existing.docID,
|
||||
BlockID: existing.blockID,
|
||||
UserID: existing.userID,
|
||||
UserName: existing.userName,
|
||||
UserColor: existing.userColor,
|
||||
ExpiresAt: existing.expiresAt.Format(time.RFC3339),
|
||||
}
|
||||
return info, true, nil
|
||||
}
|
||||
|
||||
item := &blockLockItem{
|
||||
docID: docID,
|
||||
blockID: blockID,
|
||||
userID: userID,
|
||||
userName: userName,
|
||||
userColor: userColor,
|
||||
lockedAt: now,
|
||||
expiresAt: now.Add(ttl),
|
||||
}
|
||||
h.blockLocks[key] = item
|
||||
info := &domain.BlockLockInfo{
|
||||
DocID: item.docID,
|
||||
BlockID: item.blockID,
|
||||
UserID: item.userID,
|
||||
UserName: item.userName,
|
||||
UserColor: item.userColor,
|
||||
ExpiresAt: item.expiresAt.Format(time.RFC3339),
|
||||
}
|
||||
return info, true, nil
|
||||
}
|
||||
|
||||
func (h *Hub) ReleaseBlockLock(docID, blockID, userID string) bool {
|
||||
h.mu.Lock()
|
||||
defer h.mu.Unlock()
|
||||
|
||||
key := docID + ":" + blockID
|
||||
existing, exists := h.blockLocks[key]
|
||||
if !exists {
|
||||
return false
|
||||
}
|
||||
if userID != "" && existing.userID != userID && userID != "admin" {
|
||||
return false
|
||||
}
|
||||
delete(h.blockLocks, key)
|
||||
return true
|
||||
}
|
||||
|
||||
func (h *Hub) GetDocBlockLocks(docID string) []domain.BlockLockInfo {
|
||||
h.mu.RLock()
|
||||
defer h.mu.RUnlock()
|
||||
|
||||
now := time.Now()
|
||||
var result []domain.BlockLockInfo
|
||||
prefix := docID + ":"
|
||||
for k, l := range h.blockLocks {
|
||||
if strings.HasPrefix(k, prefix) && l.expiresAt.After(now) {
|
||||
result = append(result, domain.BlockLockInfo{
|
||||
DocID: l.docID,
|
||||
BlockID: l.blockID,
|
||||
UserID: l.userID,
|
||||
UserName: l.userName,
|
||||
UserColor: l.userColor,
|
||||
ExpiresAt: l.expiresAt.Format(time.RFC3339),
|
||||
})
|
||||
}
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
// Live Session Methods (Opt-In Live Collaboration)
|
||||
|
||||
func (h *Hub) StartLiveSession(docID, userID, userName string, ttl time.Duration) (*domain.LiveSessionInfo, bool, *domain.LiveSessionInfo) {
|
||||
h.mu.Lock()
|
||||
defer h.mu.Unlock()
|
||||
|
||||
now := time.Now()
|
||||
existing, exists := h.liveSessions[docID]
|
||||
if exists && existing.expiresAt.After(now) {
|
||||
if existing.initiatorID != userID {
|
||||
info := &domain.LiveSessionInfo{
|
||||
DocID: existing.docID,
|
||||
InitiatorID: existing.initiatorID,
|
||||
InitiatorName: existing.initiatorName,
|
||||
StartedAt: existing.startedAt.Format(time.RFC3339),
|
||||
ExpiresAt: existing.expiresAt.Format(time.RFC3339),
|
||||
}
|
||||
return nil, false, info
|
||||
}
|
||||
// Refresh lease
|
||||
existing.expiresAt = now.Add(ttl)
|
||||
info := &domain.LiveSessionInfo{
|
||||
DocID: existing.docID,
|
||||
InitiatorID: existing.initiatorID,
|
||||
InitiatorName: existing.initiatorName,
|
||||
StartedAt: existing.startedAt.Format(time.RFC3339),
|
||||
ExpiresAt: existing.expiresAt.Format(time.RFC3339),
|
||||
}
|
||||
return info, true, nil
|
||||
}
|
||||
|
||||
item := &liveSessionItem{
|
||||
docID: docID,
|
||||
initiatorID: userID,
|
||||
initiatorName: userName,
|
||||
startedAt: now,
|
||||
expiresAt: now.Add(ttl),
|
||||
}
|
||||
h.liveSessions[docID] = item
|
||||
info := &domain.LiveSessionInfo{
|
||||
DocID: item.docID,
|
||||
InitiatorID: item.initiatorID,
|
||||
InitiatorName: item.initiatorName,
|
||||
StartedAt: item.startedAt.Format(time.RFC3339),
|
||||
ExpiresAt: item.expiresAt.Format(time.RFC3339),
|
||||
}
|
||||
return info, true, nil
|
||||
}
|
||||
|
||||
func (h *Hub) EndLiveSession(docID, userID string) bool {
|
||||
h.mu.Lock()
|
||||
defer h.mu.Unlock()
|
||||
|
||||
existing, exists := h.liveSessions[docID]
|
||||
if !exists {
|
||||
return false
|
||||
}
|
||||
if userID != "" && existing.initiatorID != userID && userID != "admin" {
|
||||
return false
|
||||
}
|
||||
delete(h.liveSessions, docID)
|
||||
return true
|
||||
}
|
||||
|
||||
func (h *Hub) GetLiveSession(docID string) *domain.LiveSessionInfo {
|
||||
h.mu.RLock()
|
||||
defer h.mu.RUnlock()
|
||||
|
||||
now := time.Now()
|
||||
existing, exists := h.liveSessions[docID]
|
||||
if !exists || existing.expiresAt.Before(now) {
|
||||
return nil
|
||||
}
|
||||
return &domain.LiveSessionInfo{
|
||||
DocID: existing.docID,
|
||||
InitiatorID: existing.initiatorID,
|
||||
InitiatorName: existing.initiatorName,
|
||||
StartedAt: existing.startedAt.Format(time.RFC3339),
|
||||
ExpiresAt: existing.expiresAt.Format(time.RFC3339),
|
||||
}
|
||||
}
|
||||
|
||||
func (h *Hub) cleanupLocksLoop() {
|
||||
ticker := time.NewTicker(2 * time.Second)
|
||||
defer ticker.Stop()
|
||||
@@ -470,6 +673,7 @@ func (h *Hub) cleanupLocksLoop() {
|
||||
case <-ticker.C:
|
||||
h.mu.Lock()
|
||||
now := time.Now()
|
||||
|
||||
var expiredDocIDs []string
|
||||
for docID, l := range h.locks {
|
||||
if l.expiresAt.Before(now) {
|
||||
@@ -477,11 +681,42 @@ func (h *Hub) cleanupLocksLoop() {
|
||||
delete(h.locks, docID)
|
||||
}
|
||||
}
|
||||
|
||||
type expBlock struct {
|
||||
docID string
|
||||
blockID string
|
||||
}
|
||||
var expiredBlocks []expBlock
|
||||
for k, bl := range h.blockLocks {
|
||||
if bl.expiresAt.Before(now) {
|
||||
expiredBlocks = append(expiredBlocks, expBlock{docID: bl.docID, blockID: bl.blockID})
|
||||
delete(h.blockLocks, k)
|
||||
}
|
||||
}
|
||||
|
||||
var expiredLiveSessions []string
|
||||
for docID, ls := range h.liveSessions {
|
||||
if ls.expiresAt.Before(now) {
|
||||
expiredLiveSessions = append(expiredLiveSessions, docID)
|
||||
delete(h.liveSessions, docID)
|
||||
}
|
||||
}
|
||||
h.mu.Unlock()
|
||||
|
||||
for _, docID := range expiredDocIDs {
|
||||
h.BroadcastToAll("DOC_UNLOCKED", map[string]string{"docId": docID, "doc_id": docID})
|
||||
}
|
||||
for _, b := range expiredBlocks {
|
||||
h.BroadcastRoom("doc:"+b.docID, "DOC_BLOCK_UNLOCKED", map[string]string{
|
||||
"docId": b.docID,
|
||||
"blockId": b.blockID,
|
||||
}, nil)
|
||||
}
|
||||
for _, docID := range expiredLiveSessions {
|
||||
h.BroadcastRoom("doc:"+docID, "DOC_LIVE_ENDED", map[string]string{
|
||||
"docId": docID,
|
||||
}, nil)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user