veilidchat/lib/old_to_refactor/providers/contact_invite.dart

57 lines
1.8 KiB
Dart
Raw Normal View History

2023-08-05 23:59:15 -04:00
import 'dart:typed_data';
import 'package:fast_immutable_collections/fast_immutable_collections.dart';
import 'package:fixnum/fixnum.dart';
import 'package:riverpod_annotation/riverpod_annotation.dart';
2023-12-26 20:26:54 -05:00
import '../../entities/local_account.dart';
import '../../proto/proto.dart' as proto;
import '../../tools/tools.dart';
2023-12-27 22:56:24 -05:00
import '../../../packages/veilid_support/veilid_support.dart';
2023-08-05 23:59:15 -04:00
import 'account.dart';
import 'conversation.dart';
part 'contact_invite.g.dart';
/// Get the active account contact invitation list
@riverpod
2023-12-18 20:05:23 -05:00
Future<IList<proto.ContactInvitationRecord>?> fetchContactInvitationRecords(
2023-08-05 23:59:15 -04:00
FetchContactInvitationRecordsRef ref) async {
// See if we've logged into this account or if it is locked
final activeAccountInfo = await ref.watch(fetchActiveAccountProvider.future);
if (activeAccountInfo == null) {
return null;
}
final accountRecordKey =
activeAccountInfo.userLogin.accountRecordInfo.accountRecord.recordKey;
// Decode the contact invitation list from the DHT
2023-12-18 20:05:23 -05:00
IList<proto.ContactInvitationRecord> out = const IListConst([]);
2023-10-21 19:23:43 -04:00
try {
await (await DHTShortArray.openOwned(
proto.OwnedDHTRecordPointerProto.fromProto(
activeAccountInfo.account.contactInvitationRecords),
parent: accountRecordKey))
.scope((cirList) async {
for (var i = 0; i < cirList.length; i++) {
final cir = await cirList.getItem(i);
if (cir == null) {
throw Exception('Failed to get contact invitation record');
}
2023-12-18 20:05:23 -05:00
out = out.add(proto.ContactInvitationRecord.fromBuffer(cir));
2023-08-05 23:59:15 -04:00
}
2023-10-21 19:23:43 -04:00
});
} on VeilidAPIExceptionTryAgain catch (_) {
// Try again later
ref.invalidateSelf();
return null;
} on Exception catch (_) {
// Try again later
ref.invalidateSelf();
rethrow;
}
2023-08-05 23:59:15 -04:00
return out;
}