feat(realtime): доставлять клиентам события жизненного цикла альбома и изменения состава фото
Публиковать album.created/updated/deleted и изменения состава фото в личный поток SSE, а также photo.created при загрузке, чтобы списки альбомов, счётчики и открытые страницы альбома обновлялись без перезагрузки. - members.Repository получил Audience (владелец + участники) для дешёвого веера - albums.Service рассылает события в личные потоки получателей, перечитывая альбом через GetByID и attachCover, чтобы payload совпадал с GET /api/albums - members.Service публикует при принятии инвайта, смене роли и исключении - photos.Service публикует photo.created при загрузке и album.updated при добавлении/копировании/перемещении/восстановлении/корзине/окончательном удалении - photos_count теперь считает фото вне корзины - web-клиент подписывается на /api/sse/me один раз на уровне layout'а (app) и применяет события к стору альбомов; страница альбома уходит при потере доступа
This commit is contained in:
@@ -12,8 +12,13 @@ import (
|
||||
"galo/internal/sse"
|
||||
)
|
||||
|
||||
type AlbumReader interface {
|
||||
// AlbumAccess is what the members domain needs from the albums service: the
|
||||
// owner of an album for authorization, and the ability to publish an album
|
||||
// lifecycle event to specific users' personal streams. It is satisfied by
|
||||
// albums.Service.
|
||||
type AlbumAccess interface {
|
||||
OwnerID(ctx context.Context, albumID string) (string, error)
|
||||
PublishAlbumTo(ctx context.Context, albumID, eventType string, userIDs []string)
|
||||
}
|
||||
|
||||
type Notifier interface {
|
||||
@@ -22,15 +27,53 @@ type Notifier interface {
|
||||
|
||||
type Service struct {
|
||||
store Repository
|
||||
albums AlbumReader
|
||||
albums AlbumAccess
|
||||
hub *sse.Hub
|
||||
notify Notifier
|
||||
}
|
||||
|
||||
func NewService(store Repository, albums AlbumReader, hub *sse.Hub, notify Notifier) *Service {
|
||||
func NewService(store Repository, albums AlbumAccess, hub *sse.Hub, notify Notifier) *Service {
|
||||
return &Service{store: store, albums: albums, hub: hub, notify: notify}
|
||||
}
|
||||
|
||||
// Event names published to personal streams on membership changes. They match
|
||||
// the album lifecycle contract published by albums.Service.
|
||||
const (
|
||||
eventAlbumCreated = "album.created"
|
||||
eventAlbumUpdated = "album.updated"
|
||||
eventAlbumDeleted = "album.deleted"
|
||||
)
|
||||
|
||||
// audience returns the users an album is visible to (owner + members), or nil
|
||||
// when the lookup fails. A missed fan-out is recovered by the client's
|
||||
// reconnect sync, so it must not fail the membership mutation.
|
||||
func (s *Service) audience(ctx context.Context, albumID string) []string {
|
||||
if s.albums == nil {
|
||||
return nil
|
||||
}
|
||||
ids, err := s.store.Audience(ctx, albumID)
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
return ids
|
||||
}
|
||||
|
||||
// audienceExcept is audience without the given user, used when one recipient
|
||||
// must get a different event (the invitee, the removed member).
|
||||
func (s *Service) audienceExcept(ctx context.Context, albumID, exclude string) []string {
|
||||
ids := s.audience(ctx, albumID)
|
||||
if exclude == "" {
|
||||
return ids
|
||||
}
|
||||
out := make([]string, 0, len(ids))
|
||||
for _, id := range ids {
|
||||
if id != exclude {
|
||||
out = append(out, id)
|
||||
}
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
func (s *Service) Role(ctx context.Context, albumID, userID string) (string, error) {
|
||||
return s.store.Role(ctx, albumID, userID)
|
||||
}
|
||||
@@ -113,6 +156,10 @@ func (s *Service) Accept(ctx context.Context, token, userID string) (*models.Inv
|
||||
return nil, err
|
||||
}
|
||||
s.hub.Publish(inv.AlbumID, "member.joined", map[string]string{"album_id": inv.AlbumID, "user_id": userID, "role": inv.Role})
|
||||
// The invitee gains the album and gets its object; everyone already in the
|
||||
// album sees the updated member count.
|
||||
s.albums.PublishAlbumTo(ctx, inv.AlbumID, eventAlbumCreated, []string{userID})
|
||||
s.albums.PublishAlbumTo(ctx, inv.AlbumID, eventAlbumUpdated, s.audienceExcept(ctx, inv.AlbumID, userID))
|
||||
if s.notify != nil {
|
||||
ownerID, err := s.albums.OwnerID(ctx, inv.AlbumID)
|
||||
if err == nil && ownerID != userID {
|
||||
@@ -168,6 +215,7 @@ func (s *Service) ChangeRole(ctx context.Context, albumID, actorID, targetID, ne
|
||||
return err
|
||||
}
|
||||
s.hub.Publish(albumID, "member.updated", map[string]string{"album_id": albumID, "user_id": targetID, "role": newRole})
|
||||
s.albums.PublishAlbumTo(ctx, albumID, eventAlbumUpdated, s.audience(ctx, albumID))
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -196,6 +244,10 @@ func (s *Service) Remove(ctx context.Context, albumID, actorID, targetID string)
|
||||
return err
|
||||
}
|
||||
s.hub.Publish(albumID, "member.left", map[string]string{"album_id": albumID, "user_id": targetID})
|
||||
// Those who remain see the updated member count; the removed member only
|
||||
// learns that the album is gone (album.deleted carries just its id).
|
||||
s.albums.PublishAlbumTo(ctx, albumID, eventAlbumUpdated, s.audience(ctx, albumID))
|
||||
s.albums.PublishAlbumTo(ctx, albumID, eventAlbumDeleted, []string{targetID})
|
||||
return nil
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user