add _refreshLastEvent to Thread

use Event.fromMatrixEvent instead of casting
This commit is contained in:
OfficialDakari 2025-10-21 19:12:05 +05:00
parent dab7b8b5be
commit c1f42cf881
1 changed files with 57 additions and 6 deletions

View File

@ -3,11 +3,13 @@ import 'package:matrix/matrix.dart';
class Thread { class Thread {
final Room room; final Room room;
final Event rootEvent; final Event rootEvent;
final Event? lastEvent; Event? lastEvent;
final Client client;
Thread({ Thread({
required this.room, required this.room,
required this.rootEvent, required this.rootEvent,
required this.client,
this.lastEvent, this.lastEvent,
}); });
@ -15,14 +17,63 @@ class Thread {
final room = client.getRoomById(json['room_id']); final room = client.getRoomById(json['room_id']);
if (room == null) throw Error(); if (room == null) throw Error();
Event? lastEvent; Event? lastEvent;
if (json['unsigned']?['m.relations']?['m.thread']?['latest_event'] != null) { if (json['unsigned']?['m.relations']?['m.thread']?['latest_event'] !=
lastEvent = MatrixEvent.fromJson(json['unsigned']?['m.relations']?['m.thread']?['latest_event']) as Event; null) {
lastEvent = Event.fromMatrixEvent(
MatrixEvent.fromJson(
json['unsigned']?['m.relations']?['m.thread']?['latest_event']),
room,
);
} }
final thread = Thread( final thread = Thread(
room: room, room: room,
rootEvent: MatrixEvent.fromJson(json) as Event, client: client,
lastEvent: lastEvent, rootEvent: Event.fromMatrixEvent(
MatrixEvent.fromJson(json),
room,
),
lastEvent: lastEvent,
); );
return thread; return thread;
} }
Future<Event?>? _refreshingLastEvent;
Future<Event?> _refreshLastEvent({
timeout = const Duration(seconds: 30),
}) async {
if (room.membership != Membership.join) return null;
final result = await client
.getRelatingEventsWithRelType(
room.id,
rootEvent.eventId,
'm.thread',
)
.timeout(timeout);
final matrixEvent = result.chunk.firstOrNull;
if (matrixEvent == null) {
if (lastEvent?.type == EventTypes.refreshingLastEvent) {
lastEvent = null;
}
Logs().d(
'No last event found for thread ${rootEvent.eventId} in ${rootEvent.roomId}',
);
return null;
}
var event = Event.fromMatrixEvent(
matrixEvent,
room,
status: EventStatus.synced,
);
if (event.type == EventTypes.Encrypted) {
final encryption = client.encryption;
if (encryption != null) {
event = await encryption.decryptRoomEvent(event);
}
}
lastEvent = event;
return event;
}
} }