43 lines
1.4 KiB
Go
43 lines
1.4 KiB
Go
package livestore
|
|
|
|
import (
|
|
"context"
|
|
|
|
"cloud.google.com/go/firestore"
|
|
)
|
|
|
|
//go:generate mockgen -source ./membershippublisher.go -destination ./mocks/membershippublisher.go
|
|
|
|
// MembershipPublisher publishes network membership changes to the live store
|
|
// (Firestore) that clients subscribe to. Postgres remains the source of truth;
|
|
// the membership reconciler heals any drift, so callers may log and ignore
|
|
// publish failures.
|
|
type MembershipPublisher interface {
|
|
Add(ctx context.Context, humanId, networkID string) error
|
|
Remove(ctx context.Context, humanId, networkID string) error
|
|
}
|
|
|
|
func NewMembershipPublisher(fs *firestore.Client) MembershipPublisher {
|
|
return &firestoreMembershipPublisher{fs: fs}
|
|
}
|
|
|
|
type firestoreMembershipPublisher struct {
|
|
fs *firestore.Client
|
|
}
|
|
|
|
func (p *firestoreMembershipPublisher) Add(ctx context.Context, humanId, networkID string) error {
|
|
_, err := p.fs.Collection("humans").Doc(humanId).Set(ctx, map[string]any{
|
|
"networks": firestore.ArrayUnion(networkID),
|
|
"updated_at": firestore.ServerTimestamp,
|
|
}, firestore.MergeAll)
|
|
return err
|
|
}
|
|
|
|
func (p *firestoreMembershipPublisher) Remove(ctx context.Context, humanId, networkID string) error {
|
|
_, err := p.fs.Collection("humans").Doc(humanId).Set(ctx, map[string]any{
|
|
"networks": firestore.ArrayRemove(networkID),
|
|
"updated_at": firestore.ServerTimestamp,
|
|
}, firestore.MergeAll)
|
|
return err
|
|
}
|