2024-10-02 17:17:48 +00:00
|
|
|
import 'package:flutter/foundation.dart';
|
2024-12-07 02:33:51 +00:00
|
|
|
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
2023-03-12 01:50:31 +00:00
|
|
|
|
2024-12-07 02:33:51 +00:00
|
|
|
import '../riverpod_controllers/connection_manager_services.dart';
|
|
|
|
import 'auth/profile.dart';
|
2023-01-25 01:53:55 +00:00
|
|
|
import 'connection.dart';
|
|
|
|
import 'direct_message.dart';
|
|
|
|
|
|
|
|
class DirectMessageThread {
|
|
|
|
final List<DirectMessage> messages;
|
|
|
|
|
|
|
|
final List<Connection> participants;
|
|
|
|
|
|
|
|
final String title;
|
|
|
|
|
|
|
|
final String parentUri;
|
|
|
|
|
|
|
|
DirectMessageThread({
|
|
|
|
required this.messages,
|
|
|
|
required this.participants,
|
|
|
|
required this.title,
|
|
|
|
required this.parentUri,
|
|
|
|
});
|
|
|
|
|
2024-10-02 17:17:48 +00:00
|
|
|
DirectMessageThread deepCopy() => DirectMessageThread(
|
|
|
|
messages: List.from(messages),
|
|
|
|
participants: List.from(participants),
|
|
|
|
title: title,
|
|
|
|
parentUri: parentUri,
|
|
|
|
);
|
|
|
|
|
2023-01-25 17:26:29 +00:00
|
|
|
get allSeen => messages.isEmpty
|
|
|
|
? false
|
|
|
|
: messages
|
|
|
|
.map((m) => m.seen)
|
|
|
|
.reduce((allUnseen, thisUnseen) => allUnseen && thisUnseen);
|
|
|
|
|
2023-01-25 01:53:55 +00:00
|
|
|
static List<DirectMessageThread> createThreads(
|
2024-12-07 02:33:51 +00:00
|
|
|
Ref ref,
|
|
|
|
Profile profile,
|
|
|
|
Iterable<DirectMessage> messages,
|
|
|
|
) {
|
2023-01-25 01:53:55 +00:00
|
|
|
final threads = <String, List<DirectMessage>>{};
|
|
|
|
for (final m in messages) {
|
|
|
|
final thread = threads.putIfAbsent(m.parentUri, () => []);
|
|
|
|
thread.add(m);
|
|
|
|
}
|
|
|
|
|
|
|
|
final result = <DirectMessageThread>[];
|
|
|
|
for (final t in threads.entries) {
|
|
|
|
final parentUri = t.key;
|
|
|
|
final threadMessages = t.value;
|
|
|
|
final participantIds = <String>{};
|
|
|
|
var title = '';
|
|
|
|
threadMessages.sort((m1, m2) => m1.createdAt.compareTo(m2.createdAt));
|
|
|
|
for (final m in threadMessages) {
|
|
|
|
participantIds.add(m.senderId);
|
|
|
|
participantIds.add(m.recipientId);
|
|
|
|
if (title.isEmpty) {
|
|
|
|
title = m.title;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
final participants = participantIds
|
2024-12-07 02:33:51 +00:00
|
|
|
.map((pid) => ref.read(connectionByIdProvider(profile, pid)))
|
|
|
|
.where((pr) => pr.isSuccess)
|
|
|
|
.map((pr) => pr.value)
|
2023-01-25 01:53:55 +00:00
|
|
|
.toList();
|
|
|
|
final thread = DirectMessageThread(
|
|
|
|
messages: threadMessages,
|
|
|
|
participants: participants,
|
|
|
|
title: title,
|
|
|
|
parentUri: parentUri,
|
|
|
|
);
|
|
|
|
result.add(thread);
|
|
|
|
}
|
|
|
|
|
|
|
|
result.sort((t1, t2) =>
|
|
|
|
t2.messages.first.createdAt.compareTo(t1.messages.first.createdAt));
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
bool operator ==(Object other) =>
|
|
|
|
identical(this, other) ||
|
|
|
|
other is DirectMessageThread &&
|
|
|
|
runtimeType == other.runtimeType &&
|
2024-10-02 17:17:48 +00:00
|
|
|
title == other.title &&
|
|
|
|
parentUri == other.parentUri &&
|
|
|
|
listEquals(messages, other.messages) &&
|
|
|
|
listEquals(participants, other.participants);
|
2023-01-25 01:53:55 +00:00
|
|
|
|
|
|
|
@override
|
2024-10-02 17:17:48 +00:00
|
|
|
int get hashCode =>
|
|
|
|
title.hashCode ^
|
|
|
|
parentUri.hashCode ^
|
|
|
|
Object.hashAll(messages) ^
|
|
|
|
Object.hashAll(participants);
|
2023-01-25 01:53:55 +00:00
|
|
|
}
|