Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PE-6966: assign arns name to updated manifest #1903

Merged
merged 15 commits into from
Oct 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/blocs/create_manifest/create_manifest_cubit.dart
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,7 @@ class CreateManifestCubit extends Cubit<CreateManifestState> {
return ARNSUndername(
name: '@',
domain: _selectedAntRecord!.domain,
record: ARNSRecord(
record: const ARNSRecord(
transactionId: 'to_assign',
ttlSeconds: 3600,
),
Expand Down
179 changes: 124 additions & 55 deletions lib/blocs/upload/upload_cubit.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import 'package:ardrive/blocs/upload/upload_file_checker.dart';
import 'package:ardrive/core/activity_tracker.dart';
import 'package:ardrive/core/upload/domain/repository/upload_repository.dart';
import 'package:ardrive/core/upload/uploader.dart';
import 'package:ardrive/core/upload/view/blocs/upload_manifest_options_bloc.dart';
import 'package:ardrive/main.dart';
import 'package:ardrive/manifest/domain/manifest_repository.dart';
import 'package:ardrive/models/forms/cc.dart';
Expand Down Expand Up @@ -102,29 +103,22 @@ class UploadCubit extends Cubit<UploadState> {
late final bool _isUploadFolders;

/// Manifest
List<FileEntry> _manifestFiles = [];
final List<FileEntry> _selectedManifestFiles = [];

Map<String, UploadManifestModel> _manifestFiles = {};
final List<ManifestSelection> _selectedManifestModels = [];

UploadMethod? _manifestUploadMethod;

bool _isManifestsUploadCancelled = false;

void selectManifestFile(FileEntry file) {
final readyState = state as UploadReady;

final newReadyState = readyState.copyWith(
selectedManifests: List.of(_selectedManifestFiles)..add(file));

_selectedManifestFiles.add(file);
void updateManifestSelection(List<ManifestSelection> selections) {
_selectedManifestModels.clear();

emit(newReadyState);
}

void unselectManifestFile(FileEntry file) {
_selectedManifestFiles.remove(file);
_selectedManifestModels.addAll(selections);

emit((state as UploadReady)
.copyWith(selectedManifests: _selectedManifestFiles));
emit((state as UploadReady).copyWith(
selectedManifestSelections: selections,
));
}

void setManifestUploadMethod(
Expand All @@ -133,18 +127,16 @@ class UploadCubit extends Cubit<UploadState> {
}

Future<void> prepareManifestUpload() async {
final manifestModels = _selectedManifestFiles
.map(
(f) => UploadManifestModel(
name: f.name,
isCompleted: false,
freeThanksToTurbo:
f.size <= configService.config.allowedDataItemSizeForTurbo,
isUploading: false,
existingManifestFileId: f.id,
),
)
final manifestModels = _selectedManifestModels
.map((e) => UploadManifestModel(
entry: e.manifest,
freeThanksToTurbo: false,
existingManifestFileId: e.manifest.id,
antRecord: e.antRecord,
undername: e.undername,
))
.toList();

for (int i = 0; i < manifestModels.length; i++) {
if (_isManifestsUploadCancelled) {
break;
Expand All @@ -153,7 +145,7 @@ class UploadCubit extends Cubit<UploadState> {
manifestModels[i] = manifestModels[i].copyWith(isUploading: true);

await _createManifestCubit.prepareManifestTx(
manifestName: manifestModels[i].name,
manifestName: manifestModels[i].entry.name,
folderId: _targetFolder.id,
existingManifestFileId: manifestModels[i].existingManifestFileId,
);
Expand Down Expand Up @@ -208,7 +200,7 @@ class UploadCubit extends Cubit<UploadState> {
));

await _createManifestCubit.prepareManifestTx(
manifestName: manifestModels[i].name,
manifestName: manifestModels[i].entry.name,
folderId: _targetFolder.id,
existingManifestFileId: manifestModels[i].existingManifestFileId,
);
Expand All @@ -222,25 +214,70 @@ class UploadCubit extends Cubit<UploadState> {
method: _manifestUploadMethod,
);

manifestModels[i] =
manifestModels[i].copyWith(isCompleted: true, isUploading: false);
final manifestFile = await _driveDao
.fileById(
driveId: _driveId,
fileId: manifestModels[i].existingManifestFileId,
)
.getSingleOrNull();

emit(UploadingManifests(
manifestFiles: manifestModels,
completedCount: ++completedCount,
));
if (manifestFile == null) {
throw StateError('Manifest file not found');
}

if (manifestModels[i].antRecord != null) {
ARNSUndername undername;

if (manifestModels[i].undername == null) {
undername = ARNSUndername(
name: '@',
domain: manifestModels[i].antRecord!.domain,
record: ARNSRecord(
transactionId: manifestFile.dataTxId,
ttlSeconds: 3600,
),
);
} else {
undername = ARNSUndername(
name: manifestModels[i].undername!.name,
domain: manifestModels[i].antRecord!.domain,
record: ARNSRecord(
transactionId: manifestFile.dataTxId,
ttlSeconds: 3600,
),
);
}

manifestModels[i] = manifestModels[i].copyWith(
isCompleted: false, isUploading: false, isAssigningUndername: true);
emit(UploadingManifests(
manifestFiles: manifestModels,
completedCount: ++completedCount,
));

await _arnsRepository.setUndernamesToFile(
undername: undername,
driveId: _driveId,
fileId: manifestModels[i].existingManifestFileId,
processId: manifestModels[i].antRecord!.processId,
);

manifestModels[i] = manifestModels[i].copyWith(
isCompleted: true, isUploading: false, isAssigningUndername: false);

emit(UploadingManifests(
manifestFiles: manifestModels,
completedCount: completedCount,
));
}
}

emit(UploadComplete(
manifestFiles: _selectedManifestFiles,
));
emit(UploadComplete());
}

void cancelManifestsUpload() {
_isManifestsUploadCancelled = true;
emit(UploadComplete(
manifestFiles: _selectedManifestFiles,
));
emit(UploadComplete());
}

/// License forms
Expand Down Expand Up @@ -272,6 +309,7 @@ class UploadCubit extends Cubit<UploadState> {

/// ArNS
ANTRecord? _selectedAntRecord;
List<ANTRecord> _ants = [];
ARNSUndername? _selectedUndername;

/// Thumbnail upload
Expand Down Expand Up @@ -403,10 +441,12 @@ class UploadCubit extends Cubit<UploadState> {
loadingArNSNames: true,
arnsCheckboxChecked: _showArnsNameSelectionCheckBoxValue,
totalSize: await _getTotalSize(),
selectedManifests: _selectedManifestFiles,
showSettings: showSettings,
canShowSettings: showSettings,
manifestFiles: _manifestFiles,
manifestFiles: _manifestFiles.values.toList(),
arnsRecords: _ants,
showReviewButtonText: false,
selectedManifestSelections: _selectedManifestModels,
),
);

Expand Down Expand Up @@ -445,10 +485,12 @@ class UploadCubit extends Cubit<UploadState> {
showArnsNameSelection: false,
arnsCheckboxChecked: _showArnsNameSelectionCheckBoxValue,
totalSize: await _getTotalSize(),
selectedManifests: _selectedManifestFiles,
showSettings: showSettings,
manifestFiles: _manifestFiles,
manifestFiles: _manifestFiles.values.toList(),
arnsRecords: _ants,
canShowSettings: showSettings,
showReviewButtonText: false,
selectedManifestSelections: _selectedManifestModels,
),
);
}
Expand All @@ -459,7 +501,7 @@ class UploadCubit extends Cubit<UploadState> {
if (state is UploadReady) {
if (_showArnsNameSelectionCheckBoxValue) {
showArnsNameSelection(state as UploadReady);
} else if (_selectedManifestFiles.isNotEmpty) {
} else if (_selectedManifestModels.isNotEmpty) {
emit(UploadReview(readyState: state as UploadReady));
} else {
final readyState = state as UploadReady;
Expand Down Expand Up @@ -815,11 +857,22 @@ class UploadCubit extends Cubit<UploadState> {
emit(UploadLoadingFilesSuccess());
}

Future<List<ARNSUndername>> getARNSUndernames(
ANTRecord antRecord,
) async {
return _arnsRepository.getARNSUndernames(antRecord);
}

Future<void> startUploadPreparation({
bool isRetryingToPayWithTurbo = false,
}) async {
final walletAddress = await _auth.getWalletAddress();
_arnsRepository.getAntRecordsForWallet(walletAddress!);
_arnsRepository.getAntRecordsForWallet(walletAddress!).then((value) {
_ants = value;
if (state is UploadReady) {
emit((state as UploadReady).copyWith(arnsRecords: value));
}
});

_files
.removeWhere((file) => filesNamesToExclude.contains(file.ioFile.name));
Expand Down Expand Up @@ -942,16 +995,34 @@ class UploadCubit extends Cubit<UploadState> {
),
);

_manifestFiles = await _manifestRepository.getManifestFilesInFolder(
final manifestFileEntries =
await _manifestRepository.getManifestFilesInFolder(
folderId: _targetFolder.id,
driveId: _targetDrive.id,
);

_manifestFiles = {};

for (var entry in manifestFileEntries) {
_manifestFiles[entry.id] = UploadManifestModel(
entry: entry,
existingManifestFileId: entry.id,
freeThanksToTurbo:
entry.size <= configService.config.allowedDataItemSizeForTurbo,
);
}

// if there are no files that can be used to generate a thumbnail, we disable the option
if (!containsSupportedImageTypeForThumbnailGeneration) {
_uploadThumbnail = false;
}

if (manifestFileEntries.isNotEmpty) {
// load arns names
await _arnsRepository
.getAntRecordsForWallet(_auth.currentUser.walletAddress);
}

emit(
UploadReadyToPrepare(
params: UploadParams(
Expand Down Expand Up @@ -1171,13 +1242,11 @@ class UploadCubit extends Cubit<UploadState> {
);
}

if (_selectedManifestFiles.isNotEmpty) {
if (_selectedManifestModels.isNotEmpty) {
await prepareManifestUpload();
}

emit(UploadComplete(
manifestFiles: _selectedManifestFiles,
));
emit(UploadComplete());

unawaited(_profileCubit.refreshBalance());
},
Expand Down Expand Up @@ -1251,11 +1320,11 @@ class UploadCubit extends Cubit<UploadState> {
'Upload finished with success. Number of tasks: ${tasks.length}',
);

if (_selectedManifestFiles.isNotEmpty) {
if (_selectedManifestModels.isNotEmpty) {
await prepareManifestUpload();
}

emit(UploadComplete(manifestFiles: _selectedManifestFiles));
emit(UploadComplete());

PlausibleEventTracker.trackUploadSuccess();
},
Expand Down Expand Up @@ -1334,7 +1403,7 @@ class UploadCubit extends Cubit<UploadState> {
return ARNSUndername(
name: '@',
domain: _selectedAntRecord!.domain,
record: ARNSRecord(
record: const ARNSRecord(
transactionId: 'to_assign',
ttlSeconds: 3600,
),
Expand Down
Loading
Loading