Skip to content

Commit 0207bb9

Browse files
authored
Merge pull request #62 from anyproto/GO-3125-shared-spaces-limit
GO-3125 shared spaces limit
2 parents 8b6c493 + c277a1a commit 0207bb9

File tree

16 files changed

+901
-10
lines changed

16 files changed

+901
-10
lines changed

accountlimit/accountlimit.go

Lines changed: 5 additions & 0 deletions
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 (
@@ -36,6 +37,7 @@ type configGetter interface {
3637
type SpaceLimits struct {
3738
SpaceMembersRead uint32 `yaml:"spaceMembersRead" bson:"spaceMembersRead"`
3839
SpaceMembersWrite uint32 `yaml:"spaceMembersWrite" bson:"spaceMembersWrite"`
40+
SharedSpacesLimit uint32 `yaml:"sharedSpacesLimit" bson:"sharedSpacesLimit"`
3941
}
4042

4143
type Limits struct {
@@ -44,6 +46,7 @@ type Limits struct {
4446
FileStorageBytes uint64 `bson:"fileStorageBytes"`
4547
SpaceMembersRead uint32 `bson:"spaceMembersRead"`
4648
SpaceMembersWrite uint32 `bson:"spaceMembersWrite"`
49+
SharedSpacesLimit uint32 `bson:"sharedSpacesLimit"`
4750
UpdatedTime time.Time `bson:"updatedTime"`
4851
}
4952

@@ -118,6 +121,7 @@ func (al *accountLimit) GetLimits(ctx context.Context, identity string) (limits
118121
Identity: identity,
119122
SpaceMembersRead: al.defaultLimits.SpaceMembersRead,
120123
SpaceMembersWrite: al.defaultLimits.SpaceMembersWrite,
124+
SharedSpacesLimit: al.defaultLimits.SharedSpacesLimit,
121125
UpdatedTime: time.Now(),
122126
}, nil
123127
}
@@ -144,5 +148,6 @@ func (al *accountLimit) GetLimitsBySpace(ctx context.Context, spaceId string) (s
144148
return SpaceLimits{
145149
SpaceMembersRead: limits.SpaceMembersRead,
146150
SpaceMembersWrite: limits.SpaceMembersWrite,
151+
SharedSpacesLimit: limits.SharedSpacesLimit,
147152
}, nil
148153
}

accountlimit/accountlimit_test.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,11 @@ func TestAccountLimit_SetLimits(t *testing.T) {
3131

3232
limits := Limits{
3333
Identity: "123",
34+
Reason: "reason",
35+
FileStorageBytes: 12345,
3436
SpaceMembersRead: 100,
3537
SpaceMembersWrite: 200,
38+
SharedSpacesLimit: 3,
3639
}
3740

3841
// set
@@ -82,7 +85,7 @@ func TestAccountLimit_GetLimitsBySpace(t *testing.T) {
8285

8386
limits, err := fx.GetLimitsBySpace(ctx, spaceId)
8487
require.NoError(t, err)
85-
assert.Equal(t, SpaceLimits{1, 1}, limits)
88+
assert.Equal(t, SpaceLimits{1, 1, 0}, limits)
8689
})
8790

8891
t.Run("regular", func(t *testing.T) {
@@ -97,7 +100,7 @@ func TestAccountLimit_GetLimitsBySpace(t *testing.T) {
97100

98101
limits, err := fx.GetLimitsBySpace(ctx, spaceId)
99102
require.NoError(t, err)
100-
assert.Equal(t, SpaceLimits{10, 5}, limits)
103+
assert.Equal(t, SpaceLimits{10, 5, 3}, limits)
101104
})
102105

103106
}
@@ -165,7 +168,8 @@ func (c *testConfig) GetMongo() db.Mongo {
165168

166169
func (c *testConfig) GetAccountLimit() SpaceLimits {
167170
return SpaceLimits{
168-
SpaceMembersWrite: 5,
169171
SpaceMembersRead: 10,
172+
SpaceMembersWrite: 5,
173+
SharedSpacesLimit: 3,
170174
}
171175
}

accountlimit/mock_accountlimit/mock_accountlimit.go

Lines changed: 114 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

coordinator/coordinator.go

Lines changed: 82 additions & 0 deletions
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"
@@ -270,6 +271,7 @@ func (c *coordinator) AccountLimitsSet(ctx context.Context, req *coordinatorprot
270271
FileStorageBytes: req.FileStorageLimitBytes,
271272
SpaceMembersRead: req.SpaceMembersRead,
272273
SpaceMembersWrite: req.SpaceMembersWrite,
274+
SharedSpacesLimit: req.SharedSpacesLimit,
273275
})
274276
}
275277

@@ -280,6 +282,16 @@ func (c *coordinator) AclAddRecord(ctx context.Context, spaceId string, payload
280282
return
281283
}
282284

285+
statusEntry, err := c.spaceStatus.Status(ctx, spaceId)
286+
if err != nil {
287+
return
288+
}
289+
290+
if !statusEntry.IsShareable {
291+
err = coordinatorproto.ErrSpaceNotShareable
292+
return
293+
}
294+
283295
limits, err := c.accountLimit.GetLimitsBySpace(ctx, spaceId)
284296
if err != nil {
285297
return nil, err
@@ -297,3 +309,73 @@ func (c *coordinator) AclAddRecord(ctx context.Context, spaceId string, payload
297309
}
298310
return rawRecordWithId, nil
299311
}
312+
313+
func (c *coordinator) MakeSpaceShareable(ctx context.Context, spaceId string) (err error) {
314+
pubKey, err := peer.CtxPubKey(ctx)
315+
if err != nil {
316+
return coordinatorproto.ErrForbidden
317+
}
318+
statusEntry, err := c.spaceStatus.Status(ctx, spaceId)
319+
if err != nil {
320+
return
321+
}
322+
if statusEntry.Identity != pubKey.Account() {
323+
return coordinatorproto.ErrForbidden
324+
}
325+
if statusEntry.IsShareable {
326+
return nil
327+
}
328+
329+
limits, err := c.accountLimit.GetLimitsBySpace(ctx, spaceId)
330+
if err != nil {
331+
return err
332+
}
333+
return c.spaceStatus.MakeShareable(ctx, spaceId, limits.SharedSpacesLimit)
334+
}
335+
336+
func (c *coordinator) MakeSpaceUnshareable(ctx context.Context, spaceId, aclHead string) (err error) {
337+
pubKey, err := peer.CtxPubKey(ctx)
338+
if err != nil {
339+
return coordinatorproto.ErrForbidden
340+
}
341+
statusEntry, err := c.spaceStatus.Status(ctx, spaceId)
342+
if err != nil {
343+
return
344+
}
345+
if statusEntry.Identity != pubKey.Account() {
346+
return coordinatorproto.ErrForbidden
347+
}
348+
if !statusEntry.IsShareable {
349+
return nil
350+
}
351+
352+
hasRecord, err := c.acl.HasRecord(ctx, spaceId, aclHead)
353+
if err != nil {
354+
return err
355+
}
356+
if !hasRecord {
357+
return coordinatorproto.ErrAclHeadIsMissing
358+
}
359+
360+
var (
361+
activeMembers int
362+
hasInvites bool
363+
)
364+
err = c.acl.ReadState(ctx, spaceId, func(s *list.AclState) error {
365+
for _, acc := range s.CurrentAccounts() {
366+
if acc.Permissions.NoPermissions() {
367+
continue
368+
}
369+
activeMembers++
370+
}
371+
if len(s.Invites()) > 0 {
372+
hasInvites = true
373+
}
374+
return nil
375+
})
376+
if hasInvites || activeMembers > 1 {
377+
return coordinatorproto.ErrAclNonEmpty
378+
}
379+
380+
return c.spaceStatus.MakeUnshareable(ctx, spaceId)
381+
}

0 commit comments

Comments
 (0)