51 lines
1.5 KiB
Dart
51 lines
1.5 KiB
Dart
import '../../../../api/marianumcloud/talk/chat/get_chat_response.dart';
|
|
import '../../../../api/marianumcloud/talk/chat/rich_object_string_processor.dart';
|
|
import '../../../../api/marianumcloud/talk/room/get_room_response.dart';
|
|
|
|
class ChatSearchMatch {
|
|
final int messageId;
|
|
final int timestamp;
|
|
|
|
const ChatSearchMatch({required this.messageId, required this.timestamp});
|
|
}
|
|
|
|
class ChatSearchController {
|
|
static List<ChatSearchMatch> findMatches(
|
|
GetChatResponse response,
|
|
String query,
|
|
) {
|
|
final q = query.trim().toLowerCase();
|
|
if (q.isEmpty) return const [];
|
|
|
|
final matches = <ChatSearchMatch>[];
|
|
for (final element in response.sortByTimestamp()) {
|
|
if (element.systemMessage.contains('reaction')) continue;
|
|
if (element.systemMessage.contains('poll_voted')) continue;
|
|
if (element.systemMessage.contains('message_deleted')) continue;
|
|
|
|
final haystackText = RichObjectStringProcessor.parseToString(
|
|
element.message,
|
|
element.messageParameters,
|
|
).toLowerCase();
|
|
|
|
var matched = haystackText.contains(q);
|
|
if (!matched &&
|
|
element.messageType != GetRoomResponseObjectMessageType.system) {
|
|
matched = element.actorDisplayName.toLowerCase().contains(q);
|
|
}
|
|
|
|
if (matched) {
|
|
matches.add(
|
|
ChatSearchMatch(
|
|
messageId: element.id,
|
|
timestamp: element.timestamp,
|
|
),
|
|
);
|
|
}
|
|
}
|
|
|
|
matches.sort((a, b) => b.timestamp.compareTo(a.timestamp));
|
|
return matches;
|
|
}
|
|
}
|