Skip to content

Commit 80d595c

Browse files
authored
Merge branch 'main' into feat/rbac-api
2 parents f29b93c + d125b1a commit 80d595c

29 files changed

+846
-105
lines changed

catalyst_voices/apps/voices/lib/configs/bootstrap.dart

+6
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,12 @@ Future<BootstrapArgs> bootstrap({
5757

5858
Bloc.observer = AppBlocObserver();
5959

60+
// TODO(damian-molinski): delete it before merge.
61+
// TODO(damian-molinski): enable it after after docs sync is ready.
62+
/*await Dependencies.instance.get<CatalystDatabase>().clear();
63+
64+
Dependencies.instance.get<SyncManager>().start().ignore();*/
65+
6066
return BootstrapArgs(routerConfig: router);
6167
}
6268

catalyst_voices/apps/voices/lib/dependency/dependencies.dart

+14
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,7 @@ final class Dependencies extends DependencyProvider {
156156
})
157157
..registerLazySingleton<CatGatewayDocumentDataSource>(() {
158158
return CatGatewayDocumentDataSource(
159+
get<ApiServices>(),
159160
get<SignedDocumentManager>(),
160161
);
161162
})
@@ -225,6 +226,11 @@ final class Dependencies extends DependencyProvider {
225226
get<ConfigRepository>(),
226227
);
227228
});
229+
registerLazySingleton<DocumentsService>(() {
230+
return DocumentsService(
231+
get<DocumentRepository>(),
232+
);
233+
});
228234
}
229235

230236
void _registerStorages() {
@@ -248,6 +254,14 @@ final class Dependencies extends DependencyProvider {
248254

249255
void _registerUtils() {
250256
registerLazySingleton<SignedDocumentManager>(SignedDocumentManager.new);
257+
registerLazySingleton<SyncManager>(
258+
() {
259+
return SyncManager(
260+
get<DocumentsService>(),
261+
);
262+
},
263+
dispose: (manager) async => manager.dispose(),
264+
);
251265
registerLazySingleton<UserObserver>(
252266
StreamUserObserver.new,
253267
dispose: (observer) async => observer.dispose(),

catalyst_voices/packages/internal/catalyst_voices_models/lib/src/catalyst_voices_models.dart

+1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ export 'document/validation/document_validation_result.dart';
2626
export 'document/validation/document_validator.dart';
2727
export 'document/values/grouped_tags.dart';
2828
export 'errors/errors.dart';
29+
export 'errors/sync_exception.dart';
2930
export 'file/voices_file.dart';
3031
export 'hi_lo/hi_lo.dart';
3132
export 'hi_lo/uuid_hi_lo.dart';

catalyst_voices/packages/internal/catalyst_voices_models/lib/src/document/document_ref.dart

+3
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,9 @@ final class SignedDocumentRef extends DocumentRef {
9090
super.version,
9191
});
9292

93+
/// Creates ref for first version of [id] document.
94+
const SignedDocumentRef.first(String id) : this(id: id, version: id);
95+
9396
factory SignedDocumentRef.generateFirstRef() {
9497
final id = const Uuid().v7();
9598
return SignedDocumentRef(
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import 'package:catalyst_voices_models/catalyst_voices_models.dart';
2+
3+
final class RefsSyncException extends SyncException {
4+
final List<Object> errors;
5+
6+
RefsSyncException(this.errors);
7+
8+
@override
9+
String toString() => 'RefsSyncException errors[${errors.length}]';
10+
}
11+
12+
final class RefSyncException extends SyncException {
13+
final DocumentRef ref;
14+
final Object? error;
15+
final StackTrace? stack;
16+
17+
const RefSyncException(this.ref, {this.error, this.stack});
18+
19+
@override
20+
String toString() => 'RefSyncException($ref) failed with $error';
21+
}
22+
23+
sealed class SyncException implements Exception {
24+
const SyncException();
25+
}

catalyst_voices/packages/internal/catalyst_voices_repositories/lib/src/catalyst_voices_repositories.dart

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ export 'api/api.dart';
22
export 'campaign/campaign_repository.dart' show CampaignRepository;
33
export 'config/config_repository.dart' show ConfigRepository;
44
export 'database/database.dart';
5+
export 'document/constants.dart';
56
export 'document/document_mapper.dart' show DocumentMapperImpl;
67
export 'document/document_repository.dart' show DocumentRepository;
78
export 'document/exception/document_data_local_source_exception.dart';

catalyst_voices/packages/internal/catalyst_voices_repositories/lib/src/database/dao/documents_dao.dart

+36-11
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,11 @@ import 'package:flutter/foundation.dart';
1313
abstract interface class DocumentsDao {
1414
/// Counts documents matching required [ref] id and optional [ref] ver.
1515
///
16+
/// If [ref] is null counts all documents.
17+
///
1618
/// If [ref] ver is not specified it will return count of all version
1719
/// matching [ref] id.
18-
Future<int> count({required DocumentRef ref});
19-
20-
/// Counts all documents.
21-
Future<int> countAll();
20+
Future<int> count({DocumentRef? ref});
2221

2322
/// Counts unique documents. All versions of same document are counted as 1.
2423
Future<int> countDocuments();
@@ -49,6 +48,9 @@ abstract interface class DocumentsDao {
4948
/// all will be returned.
5049
Future<List<DocumentEntity>> queryAll();
5150

51+
/// Returns all known document refs.
52+
Future<List<SignedDocumentRef>> queryAllRefs();
53+
5254
/// Returns a list of version of ref object.
5355
/// Can be used to get versions count.
5456
Future<List<String>> queryVersionIds({required String id});
@@ -91,13 +93,12 @@ class DriftDocumentsDao extends DatabaseAccessor<DriftCatalystDatabase>
9193
DriftDocumentsDao(super.attachedDatabase);
9294

9395
@override
94-
Future<int> count({required DocumentRef ref}) {
95-
return documents.count(where: (row) => _filterRef(row, ref)).getSingle();
96-
}
97-
98-
@override
99-
Future<int> countAll() {
100-
return documents.count().getSingle();
96+
Future<int> count({DocumentRef? ref}) {
97+
if (ref == null) {
98+
return documents.count().getSingle();
99+
} else {
100+
return documents.count(where: (row) => _filterRef(row, ref)).getSingle();
101+
}
101102
}
102103

103104
@override
@@ -173,6 +174,30 @@ class DriftDocumentsDao extends DatabaseAccessor<DriftCatalystDatabase>
173174
return select(documents).get();
174175
}
175176

177+
@override
178+
Future<List<SignedDocumentRef>> queryAllRefs() {
179+
final select = selectOnly(documents)
180+
..addColumns([
181+
documents.idHi,
182+
documents.idLo,
183+
documents.verHi,
184+
documents.verLo,
185+
]);
186+
187+
return select.map((row) {
188+
final id = UuidHiLo(
189+
high: row.read(documents.idHi)!,
190+
low: row.read(documents.idLo)!,
191+
);
192+
final version = UuidHiLo(
193+
high: row.read(documents.verHi)!,
194+
low: row.read(documents.verLo)!,
195+
);
196+
197+
return SignedDocumentRef(id: id.uuid, version: version.uuid);
198+
}).get();
199+
}
200+
176201
@override
177202
Future<List<String>> queryVersionIds({required String id}) {
178203
final query = select(documents)

catalyst_voices/packages/internal/catalyst_voices_repositories/lib/src/database/dao/drafts_dao.dart

+36-11
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,11 @@ import 'package:flutter/foundation.dart';
1010
abstract interface class DraftsDao {
1111
/// Counts drafts matching required [ref] id and optional [ref] ver.
1212
///
13+
/// If ref is null it will count all drafts.
14+
///
1315
/// If [ref] ver is not specified it will return count of all version
1416
/// matching [ref] id.
15-
Future<int> count({required DocumentRef ref});
16-
17-
/// Counts unique drafts. All versions of same document are counted as 1.
18-
Future<int> countAll();
17+
Future<int> count({DocumentRef? ref});
1918

2019
/// Deletes a document draft with [ref].
2120
///
@@ -29,6 +28,9 @@ abstract interface class DraftsDao {
2928
/// Returns all drafts
3029
Future<List<DocumentDraftEntity>> queryAll();
3130

31+
/// Returns all known document drafts refs.
32+
Future<List<DraftRef>> queryAllRefs();
33+
3234
/// Singular version of [saveAll]. Does not run in transaction.
3335
Future<void> save(DocumentDraftEntity draft);
3436

@@ -59,13 +61,12 @@ class DriftDraftsDao extends DatabaseAccessor<DriftCatalystDatabase>
5961
DriftDraftsDao(super.attachedDatabase);
6062

6163
@override
62-
Future<int> count({required DocumentRef ref}) {
63-
return drafts.count(where: (row) => _filterRef(row, ref)).getSingle();
64-
}
65-
66-
@override
67-
Future<int> countAll() {
68-
return drafts.count().getSingle();
64+
Future<int> count({DocumentRef? ref}) {
65+
if (ref == null) {
66+
return drafts.count().getSingle();
67+
} else {
68+
return drafts.count(where: (row) => _filterRef(row, ref)).getSingle();
69+
}
6970
}
7071

7172
@override
@@ -87,6 +88,30 @@ class DriftDraftsDao extends DatabaseAccessor<DriftCatalystDatabase>
8788
return select(drafts).get();
8889
}
8990

91+
@override
92+
Future<List<DraftRef>> queryAllRefs() {
93+
final select = selectOnly(drafts)
94+
..addColumns([
95+
drafts.idHi,
96+
drafts.idLo,
97+
drafts.verHi,
98+
drafts.verLo,
99+
]);
100+
101+
return select.map((row) {
102+
final id = UuidHiLo(
103+
high: row.read(drafts.idHi)!,
104+
low: row.read(drafts.idLo)!,
105+
);
106+
final version = UuidHiLo(
107+
high: row.read(drafts.verHi)!,
108+
low: row.read(drafts.verLo)!,
109+
);
110+
111+
return DraftRef(id: id.uuid, version: version.uuid);
112+
}).get();
113+
}
114+
90115
@override
91116
Future<void> save(DocumentDraftEntity draft) async {
92117
await into(drafts).insert(draft, mode: InsertMode.insertOrReplace);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
import 'package:catalyst_voices_models/catalyst_voices_models.dart';
2+
3+
/// At the moment categories are hard coded.
4+
/// See list in link below
5+
/// https://input-output-hk.github.io/catalyst-libs/architecture/08_concepts/catalyst_docs/proposal/#fund-14-defined-category-ids
6+
/// https://input-output-hk.github.io/catalyst-libs/architecture/08_concepts/catalyst_docs/proposal/#fund-14-defined-templates-ids
7+
/// https://input-output-hk.github.io/catalyst-libs/architecture/08_concepts/catalyst_docs/comment/#fund-14-defined-templates-ids
8+
const categoriesTemplatesRefs = <CategoryTemplatesRefs>[
9+
(
10+
category: SignedDocumentRef(id: '0194d490-30bf-7473-81c8-a0eaef369619'),
11+
proposal: SignedDocumentRef.first('0194d492-1daa-75b5-b4a4-5cf331cd8d1a'),
12+
comment: SignedDocumentRef.first('0194d494-4402-7e0e-b8d6-171f8fea18b0'),
13+
),
14+
(
15+
category: SignedDocumentRef(id: '0194d490-30bf-7043-8c5c-f0e09f8a6d8c'),
16+
proposal: SignedDocumentRef.first('0194d492-1daa-7371-8bd3-c15811b2b063'),
17+
comment: SignedDocumentRef.first('0194d494-4402-7444-9058-9030815eb029'),
18+
),
19+
(
20+
category: SignedDocumentRef(id: '0194d490-30bf-7e75-95c1-a6cf0e8086d9'),
21+
proposal: SignedDocumentRef.first('0194d492-1daa-79c7-a222-2c3b581443a8'),
22+
comment: SignedDocumentRef.first('0194d494-4402-7351-b4f7-24938dc2c12e'),
23+
),
24+
(
25+
category: SignedDocumentRef(id: '0194d490-30bf-7703-a1c0-83a916b001e7'),
26+
proposal: SignedDocumentRef.first('0194d492-1daa-716f-a04e-f422f08a99dc'),
27+
comment: SignedDocumentRef.first('0194d494-4402-79ad-93ba-4d7a0b65d563'),
28+
),
29+
(
30+
category: SignedDocumentRef(id: '0194d490-30bf-79d1-9a0f-84943123ef38'),
31+
proposal: SignedDocumentRef.first('0194d492-1daa-78fc-818a-bf20fc3e9b87'),
32+
comment: SignedDocumentRef.first('0194d494-4402-7cee-a5a6-5739839b3b8a'),
33+
),
34+
(
35+
category: SignedDocumentRef(id: '0194d490-30bf-706d-91c6-0d4707f74cdf'),
36+
proposal: SignedDocumentRef.first('0194d492-1daa-7d98-a3aa-c57d99121f78'),
37+
comment: SignedDocumentRef.first('0194d494-4402-7aee-8b24-b5300c976846'),
38+
),
39+
(
40+
category: SignedDocumentRef(id: '0194d490-30bf-759e-b729-304306fbaa5e'),
41+
proposal: SignedDocumentRef.first('0194d492-1daa-77be-a1a5-c238fe25fe4f'),
42+
comment: SignedDocumentRef.first('0194d494-4402-7d75-be7f-1c4f3471a53c'),
43+
),
44+
(
45+
category: SignedDocumentRef(id: '0194d490-30bf-7e27-b5fd-de3133b54bf6'),
46+
proposal: SignedDocumentRef.first('0194d492-1daa-7254-a512-30a4cdecfb90'),
47+
comment: SignedDocumentRef.first('0194d494-4402-7a2c-8971-1b4c255c826d'),
48+
),
49+
(
50+
category: SignedDocumentRef(id: '0194d490-30bf-7f9e-8a5d-91fb67c078f2'),
51+
proposal: SignedDocumentRef.first('0194d492-1daa-7de9-b535-1a0b0474ed4e'),
52+
comment: SignedDocumentRef.first('0194d494-4402-7074-86ac-3efd097ba9b0'),
53+
),
54+
(
55+
category: SignedDocumentRef(id: '0194d490-30bf-7676-9658-36c0b67e656e'),
56+
proposal: SignedDocumentRef.first('0194d492-1daa-7fce-84ee-b872a4661075'),
57+
comment: SignedDocumentRef.first('0194d494-4402-7202-8ebb-8c4c47c286d8'),
58+
),
59+
(
60+
category: SignedDocumentRef(id: '0194d490-30bf-7978-b031-7aa2ccc5e3fd'),
61+
proposal: SignedDocumentRef.first('0194d492-1daa-7878-9bcc-2c79fef0fc13'),
62+
comment: SignedDocumentRef.first('0194d494-4402-7fb5-b680-c23fe4beb088'),
63+
),
64+
(
65+
category: SignedDocumentRef(id: '0194d490-30bf-7d34-bba9-8498094bd627'),
66+
proposal: SignedDocumentRef.first('0194d492-1daa-722f-94f4-687f2c068a5d'),
67+
comment: SignedDocumentRef.first('0194d494-4402-7aa5-9dbc-5fe886e60ebc'),
68+
),
69+
];
70+
71+
typedef CategoryTemplatesRefs = ({
72+
SignedDocumentRef category,
73+
SignedDocumentRef proposal,
74+
SignedDocumentRef comment,
75+
});

0 commit comments

Comments
 (0)