veilidchat/lib/old_to_refactor/providers/chat.dart

119 lines
3.8 KiB
Dart
Raw Normal View History

2023-08-06 19:46:40 -04:00
import 'package:fast_immutable_collections/fast_immutable_collections.dart';
2023-09-30 21:00:22 -04:00
import 'package:flutter_riverpod/flutter_riverpod.dart';
2023-08-06 19:46:40 -04:00
import 'package:riverpod_annotation/riverpod_annotation.dart';
2023-12-26 20:26:54 -05:00
import '../../proto/proto.dart' as proto;
2023-08-06 19:46:40 -04:00
2023-12-27 22:56:24 -05:00
import '../../../packages/veilid_support/veilid_support.dart';
2023-08-06 19:46:40 -04:00
import 'account.dart';
part 'chat.g.dart';
/// Create a new chat (singleton for single contact chats)
Future<void> getOrCreateChatSingleContact({
required ActiveAccountInfo activeAccountInfo,
required TypedKey remoteConversationRecordKey,
}) async {
final accountRecordKey =
activeAccountInfo.userLogin.accountRecordInfo.accountRecord.recordKey;
// Create conversation type Chat
2023-12-18 20:05:23 -05:00
final chat = proto.Chat()
..type = proto.ChatType.SINGLE_CONTACT
2023-08-06 19:46:40 -04:00
..remoteConversationKey = remoteConversationRecordKey.toProto();
// Add Chat to account's list
// if this fails, don't keep retrying, user can try again later
await (await DHTShortArray.openOwned(
proto.OwnedDHTRecordPointerProto.fromProto(
activeAccountInfo.account.chatList),
parent: accountRecordKey))
.scope((chatList) async {
for (var i = 0; i < chatList.length; i++) {
final cbuf = await chatList.getItem(i);
if (cbuf == null) {
throw Exception('Failed to get chat');
}
2023-12-18 20:05:23 -05:00
final c = proto.Chat.fromBuffer(cbuf);
2023-08-06 19:46:40 -04:00
if (c == chat) {
return;
}
}
if (await chatList.tryAddItem(chat.writeToBuffer()) == false) {
throw Exception('Failed to add chat');
}
});
}
/// Delete a chat
Future<void> deleteChat(
{required ActiveAccountInfo activeAccountInfo,
required TypedKey remoteConversationRecordKey}) async {
final accountRecordKey =
activeAccountInfo.userLogin.accountRecordInfo.accountRecord.recordKey;
// Create conversation type Chat
final remoteConversationKey = remoteConversationRecordKey.toProto();
// Add Chat to account's list
// if this fails, don't keep retrying, user can try again later
await (await DHTShortArray.openOwned(
proto.OwnedDHTRecordPointerProto.fromProto(
activeAccountInfo.account.chatList),
parent: accountRecordKey))
.scope((chatList) async {
for (var i = 0; i < chatList.length; i++) {
final cbuf = await chatList.getItem(i);
if (cbuf == null) {
throw Exception('Failed to get chat');
}
2023-12-18 20:05:23 -05:00
final c = proto.Chat.fromBuffer(cbuf);
2023-08-06 19:46:40 -04:00
if (c.remoteConversationKey == remoteConversationKey) {
await chatList.tryRemoveItem(i);
2023-08-07 00:55:57 -04:00
2023-09-30 21:00:22 -04:00
if (activeChatState.state == remoteConversationRecordKey) {
activeChatState.state = null;
2023-08-07 00:55:57 -04:00
}
2023-08-06 19:46:40 -04:00
return;
}
}
});
}
/// Get the active account contact list
@riverpod
2023-12-18 20:05:23 -05:00
Future<IList<proto.Chat>?> fetchChatList(FetchChatListRef ref) async {
2023-08-06 19:46:40 -04:00
// 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 chat list from the DHT
2023-12-18 20:05:23 -05:00
IList<proto.Chat> out = const IListConst([]);
2023-08-06 19:46:40 -04:00
await (await DHTShortArray.openOwned(
proto.OwnedDHTRecordPointerProto.fromProto(
activeAccountInfo.account.chatList),
parent: accountRecordKey))
.scope((cList) async {
for (var i = 0; i < cList.length; i++) {
final cir = await cList.getItem(i);
if (cir == null) {
throw Exception('Failed to get chat');
}
2023-12-18 20:05:23 -05:00
out = out.add(proto.Chat.fromBuffer(cir));
2023-08-06 19:46:40 -04:00
}
});
return out;
}
// The selected chat
2023-09-30 21:00:22 -04:00
final activeChatState = StateController<TypedKey?>(null);
final activeChatStateProvider =
2023-09-30 21:28:45 -04:00
StateNotifierProvider<StateController<TypedKey?>, TypedKey?>(
2023-09-30 21:00:22 -04:00
(ref) => activeChatState);