add _refreshLastEvent to Thread
use Event.fromMatrixEvent instead of casting
This commit is contained in:
parent
dab7b8b5be
commit
c1f42cf881
|
|
@ -3,11 +3,13 @@ import 'package:matrix/matrix.dart';
|
|||
class Thread {
|
||||
final Room room;
|
||||
final Event rootEvent;
|
||||
final Event? lastEvent;
|
||||
Event? lastEvent;
|
||||
final Client client;
|
||||
|
||||
Thread({
|
||||
required this.room,
|
||||
required this.rootEvent,
|
||||
required this.client,
|
||||
this.lastEvent,
|
||||
});
|
||||
|
||||
|
|
@ -15,14 +17,63 @@ class Thread {
|
|||
final room = client.getRoomById(json['room_id']);
|
||||
if (room == null) throw Error();
|
||||
Event? lastEvent;
|
||||
if (json['unsigned']?['m.relations']?['m.thread']?['latest_event'] != null) {
|
||||
lastEvent = MatrixEvent.fromJson(json['unsigned']?['m.relations']?['m.thread']?['latest_event']) as Event;
|
||||
if (json['unsigned']?['m.relations']?['m.thread']?['latest_event'] !=
|
||||
null) {
|
||||
lastEvent = Event.fromMatrixEvent(
|
||||
MatrixEvent.fromJson(
|
||||
json['unsigned']?['m.relations']?['m.thread']?['latest_event']),
|
||||
room,
|
||||
);
|
||||
}
|
||||
final thread = Thread(
|
||||
room: room,
|
||||
rootEvent: MatrixEvent.fromJson(json) as Event,
|
||||
lastEvent: lastEvent,
|
||||
room: room,
|
||||
client: client,
|
||||
rootEvent: Event.fromMatrixEvent(
|
||||
MatrixEvent.fromJson(json),
|
||||
room,
|
||||
),
|
||||
lastEvent: lastEvent,
|
||||
);
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue