forked from BasedHardware/omi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconversation_sync_utils.dart
More file actions
52 lines (42 loc) · 2.21 KB
/
Copy pathconversation_sync_utils.dart
File metadata and controls
52 lines (42 loc) · 2.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import 'package:omi/backend/http/api/conversations.dart';
import 'package:omi/backend/schema/conversation.dart';
class ConversationSyncUtils {
static const Duration _fetchTimeout = Duration(seconds: 30);
static Future<List<SyncedConversationPointer>> processConversationIds({
required List<String> newConversationIds,
required List<String> updatedConversationIds,
}) async {
final List<SyncedConversationPointer> result = [];
if (newConversationIds.isNotEmpty) {
final newConversations = await _fetchConversations(newConversationIds);
final newPointers = _createPointers(newConversations, SyncedConversationType.newConversation);
result.addAll(newPointers);
}
if (updatedConversationIds.isNotEmpty) {
final updatedConversations = await _fetchConversations(updatedConversationIds);
final updatedPointers = _createPointers(updatedConversations, SyncedConversationType.updatedConversation);
result.addAll(updatedPointers);
}
return result;
}
static Future<List<ServerConversation?>> _fetchConversations(List<String> conversationIds) async {
final futures = conversationIds.map((id) => _fetchSingleConversation(id)).toList();
return await Future.wait(futures).timeout(_fetchTimeout);
}
static Future<ServerConversation?> _fetchSingleConversation(String conversationId) async {
return await getConversationById(conversationId);
}
static List<SyncedConversationPointer> _createPointers(
List<ServerConversation?> conversations,
SyncedConversationType type,
) {
final validConversations = conversations.where((conversation) => conversation != null).toList();
final completedConversations =
validConversations.where((conversation) => conversation!.status == ConversationStatus.completed).toList();
return completedConversations.map((conversation) => _createPointer(conversation!, type)).toList();
}
static SyncedConversationPointer _createPointer(ServerConversation conversation, SyncedConversationType type) {
final date = DateTime(conversation.createdAt.year, conversation.createdAt.month, conversation.createdAt.day);
return SyncedConversationPointer(type: type, index: 0, key: date, conversation: conversation);
}
}