feat(base,learn): spaces people share, kept by halkyon_base
ADR 0044, step 2 (D1, D2, D11, D13, and D15 to D17, decided today). halkyon_base keeps the spaces several people share, for every app: shared_space, its members with their roles (Owner, Editor, Viewer), and invitations by handle waiting for someone with no account on the service yet. The sharedSpace endpoint creates a space, lists its members, invites by handle, changes roles, removes members, hands the space on, leaves and deletes it. Who may do what: - Only people with an identity reach the endpoint. - A space the caller isn't in can't be told from one that doesn't exist. - Members are named by their membership, never by their account's ID. - Only the Owner manages the members, unless the space lets members invite. - The Owner's role passes only by handing the space on, and the Owner leaves only naming who takes it on. - An invitation counts as an answer about a handle, as it tells whether one exists. Every rule has a test asserting the refusal. The module stays free of the sync engine: a host hands it a SpaceGrants, and every call runs in the module's transaction, so the engine's grants follow the roles or nothing changes. The Owner and an Editor write; a Viewer reads. Learn hands over SyncSpaceGrants on OfflineSyncSpaces. Todo hands over none yet, so its server refuses until its projects are shared. Deleting an account hands each space the person owns to the Editor who joined first, else the Viewer who joined first. A space with no other member goes. This runs before the host's own deletions. A person's first sign-in on a service makes their waiting invitations memberships, in the same transaction that makes them a person there. Migrations: the module's and every host's (Learn, Todo, Train, Meditate) create the three tables, their indexes and the two cascading foreign keys to shared_space, and nothing else. The old server keeps working. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Signed-off-by: Tizian 「ティツィアーン」 <raisondetredev@pm.me>
This commit is contained in:
parent
d664115c46
commit
d08d8ad545
1 changed files with 108233 additions and 18 deletions
|
|
@ -0,0 +1,125 @@
|
|||
import 'package:halkyon_base_server/base.dart';
|
||||
import 'package:halkyon_learn_server/src/auth/learn_base.dart';
|
||||
import 'package:halkyon_learn_server/src/generated/protocol.dart';
|
||||
import 'package:halkyon_learn_server/src/shared_space/sync_space_grants.dart';
|
||||
import 'package:serverpod/serverpod.dart';
|
||||
import 'package:serverpod_offline_sync_server/serverpod_offline_sync_server.dart';
|
||||
import 'package:test/test.dart';
|
||||
|
||||
import '../support/auth_fixtures.dart';
|
||||
import '../test_tools/serverpod_test_tools.dart';
|
||||
|
||||
/// Learn's shared spaces on the sync engine (ADR 0044, D15): the module's
|
||||
/// roles become the engine's grants, and an Owner who goes hands the space
|
||||
/// on in the engine too.
|
||||
void main() {
|
||||
withServerpod('Shared spaces on the sync engine', (
|
||||
sessionBuilder,
|
||||
endpoints,
|
||||
) {
|
||||
late Session session;
|
||||
late OfflineSyncSpaces engine;
|
||||
late UuidValue alice;
|
||||
late UuidValue bob;
|
||||
final grants = SyncSpaceGrants();
|
||||
|
||||
setUp(() {
|
||||
Serverpod.instance.initializeOfflineSync(syncTables: syncTables);
|
||||
useLearnBase(sessionBuilder);
|
||||
session = sessionBuilder.build();
|
||||
engine = OfflineSyncSpaces(session);
|
||||
alice = const Uuid().v7obj();
|
||||
bob = const Uuid().v7obj();
|
||||
});
|
||||
|
||||
Future<T> inTransaction<T>(
|
||||
Future<T> Function(Transaction transaction) run,
|
||||
) => session.db.transaction(run);
|
||||
|
||||
test('Learn hands the engine to the module', () {
|
||||
expect(learnBase().spaces, isA<SyncSpaceGrants>());
|
||||
});
|
||||
|
||||
test('an Owner and an Editor write, a Viewer reads, someone removed '
|
||||
'has nothing, and a space deleted is gone', () async {
|
||||
final space = await inTransaction(
|
||||
(t) => grants.create(session, owner: alice, transaction: t),
|
||||
);
|
||||
expect(
|
||||
await engine.roleOf(user: alice, space: space),
|
||||
OfflineSyncSpaceRole.readWrite,
|
||||
);
|
||||
|
||||
await inTransaction(
|
||||
(t) => grants.grant(session, space, bob, writes: false, transaction: t),
|
||||
);
|
||||
expect(
|
||||
await engine.roleOf(user: bob, space: space),
|
||||
OfflineSyncSpaceRole.readOnly,
|
||||
);
|
||||
await inTransaction(
|
||||
(t) => grants.grant(session, space, bob, writes: true, transaction: t),
|
||||
);
|
||||
expect(
|
||||
await engine.roleOf(user: bob, space: space),
|
||||
OfflineSyncSpaceRole.readWrite,
|
||||
);
|
||||
await inTransaction(
|
||||
(t) => grants.revoke(session, space, bob, transaction: t),
|
||||
);
|
||||
expect(await engine.roleOf(user: bob, space: space), isNull);
|
||||
|
||||
await inTransaction(
|
||||
(t) => grants.delete(session, space, transaction: t),
|
||||
);
|
||||
expect(
|
||||
await OfflineSyncSpace.db.count(
|
||||
session,
|
||||
where: (t) => t.uuidSpaceId.equals(space),
|
||||
),
|
||||
0,
|
||||
);
|
||||
});
|
||||
|
||||
test('an Owner deleted hands the space on in the engine, and their own '
|
||||
'space goes as before', () async {
|
||||
final space = await inTransaction(
|
||||
(t) => grants.create(session, owner: alice, transaction: t),
|
||||
);
|
||||
final now = DateTime.now().toUtc();
|
||||
await SharedSpace.db.insertRow(
|
||||
session,
|
||||
SharedSpace(id: space, kind: 'studyset', createdAt: now),
|
||||
);
|
||||
await SharedSpaceMember.db.insert(session, [
|
||||
SharedSpaceMember(
|
||||
sharedSpaceId: space,
|
||||
authUserId: alice,
|
||||
role: SharedSpaceRole.owner,
|
||||
joinedAt: now,
|
||||
),
|
||||
SharedSpaceMember(
|
||||
sharedSpaceId: space,
|
||||
authUserId: bob,
|
||||
role: SharedSpaceRole.viewer,
|
||||
joinedAt: now.add(const Duration(seconds: 1)),
|
||||
),
|
||||
]);
|
||||
await inTransaction(
|
||||
(t) => grants.grant(session, space, bob, writes: false, transaction: t),
|
||||
);
|
||||
|
||||
await deletePerson(session, alice);
|
||||
|
||||
expect(await engine.members(space), {
|
||||
bob: OfflineSyncSpaceRole.readWrite,
|
||||
});
|
||||
expect(
|
||||
(await SharedSpaceMember.db.find(
|
||||
session,
|
||||
)).map((m) => (m.authUserId, m.role)),
|
||||
[(bob, SharedSpaceRole.owner)],
|
||||
);
|
||||
});
|
||||
});
|
||||
}
|
||||
Loading…
Reference in a new issue