@@ -12,6 +12,7 @@ import (
12
12
"github.com/anyproto/any-sync/app/logger"
13
13
"github.com/anyproto/any-sync/commonspace"
14
14
"github.com/anyproto/any-sync/commonspace/object/accountdata"
15
+ "github.com/anyproto/any-sync/commonspace/object/acl/list"
15
16
"github.com/anyproto/any-sync/commonspace/spacesyncproto"
16
17
"github.com/anyproto/any-sync/consensus/consensusproto"
17
18
"github.com/anyproto/any-sync/coordinator/coordinatorproto"
@@ -298,3 +299,73 @@ func (c *coordinator) AclAddRecord(ctx context.Context, spaceId string, payload
298
299
}
299
300
return rawRecordWithId , nil
300
301
}
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