Skip to content

Commit abf49be

Browse files
committed
shareable/unshareable coordinator
1 parent dcb291f commit abf49be

File tree

11 files changed

+655
-13
lines changed

11 files changed

+655
-13
lines changed

accountlimit/accountlimit.go

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
//go:generate mockgen -destination mock_accountlimit/mock_accountlimit.go github.com/anyproto/any-sync-coordinator/accountlimit AccountLimit
12
package accountlimit
23

34
import (

accountlimit/mock_accountlimit/mock_accountlimit.go

+114
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

coordinator/coordinator.go

+71
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"github.com/anyproto/any-sync/app/logger"
1313
"github.com/anyproto/any-sync/commonspace"
1414
"github.com/anyproto/any-sync/commonspace/object/accountdata"
15+
"github.com/anyproto/any-sync/commonspace/object/acl/list"
1516
"github.com/anyproto/any-sync/commonspace/spacesyncproto"
1617
"github.com/anyproto/any-sync/consensus/consensusproto"
1718
"github.com/anyproto/any-sync/coordinator/coordinatorproto"
@@ -298,3 +299,73 @@ func (c *coordinator) AclAddRecord(ctx context.Context, spaceId string, payload
298299
}
299300
return rawRecordWithId, nil
300301
}
302+
303+
func (c *coordinator) MakeSpaceShareable(ctx context.Context, spaceId string) (err error) {
304+
pubKey, err := peer.CtxPubKey(ctx)
305+
if err != nil {
306+
return coordinatorproto.ErrForbidden
307+
}
308+
statusEntry, err := c.spaceStatus.Status(ctx, spaceId)
309+
if err != nil {
310+
return
311+
}
312+
if statusEntry.Identity != pubKey.Account() {
313+
return coordinatorproto.ErrForbidden
314+
}
315+
if statusEntry.IsShareable {
316+
return nil
317+
}
318+
319+
limits, err := c.accountLimit.GetLimitsBySpace(ctx, spaceId)
320+
if err != nil {
321+
return err
322+
}
323+
return c.spaceStatus.MakeShareable(ctx, spaceId, limits.SharedSpacesLimit)
324+
}
325+
326+
func (c *coordinator) MakeSpaceUnshareable(ctx context.Context, spaceId, aclHead string) (err error) {
327+
pubKey, err := peer.CtxPubKey(ctx)
328+
if err != nil {
329+
return coordinatorproto.ErrForbidden
330+
}
331+
statusEntry, err := c.spaceStatus.Status(ctx, spaceId)
332+
if err != nil {
333+
return
334+
}
335+
if statusEntry.Identity != pubKey.Account() {
336+
return coordinatorproto.ErrForbidden
337+
}
338+
if !statusEntry.IsShareable {
339+
return nil
340+
}
341+
342+
hasRecord, err := c.acl.HasRecord(ctx, spaceId, aclHead)
343+
if err != nil {
344+
return err
345+
}
346+
if !hasRecord {
347+
return coordinatorproto.ErrAclHeadIsMissing
348+
}
349+
350+
var (
351+
activeMembers int
352+
hasInvites bool
353+
)
354+
err = c.acl.ReadState(ctx, spaceId, func(s *list.AclState) error {
355+
for _, acc := range s.CurrentAccounts() {
356+
if acc.Permissions.NoPermissions() {
357+
continue
358+
}
359+
activeMembers++
360+
}
361+
if len(s.Invites()) > 0 {
362+
hasInvites = true
363+
}
364+
return nil
365+
})
366+
if hasInvites || activeMembers > 1 {
367+
return coordinatorproto.ErrAclNonEmpty
368+
}
369+
370+
return c.spaceStatus.MakeUnshareable(ctx, spaceId)
371+
}

0 commit comments

Comments
 (0)