fix: Store sending files in database and fix retrying to send them

This commit is contained in:
Krille Fear 2022-04-01 06:36:51 +00:00 committed by Christian Pauly
commit 6f977b7c9d
4 changed files with 34 additions and 13 deletions

View File

@ -327,13 +327,20 @@ class Event extends MatrixEvent {
/// Try to send this event again. Only works with events of status -1. /// Try to send this event again. Only works with events of status -1.
Future<String?> sendAgain({String? txid}) async { Future<String?> sendAgain({String? txid}) async {
if (!status.isError) return null; if (!status.isError) return null;
// If this is a failed file sending event, try to fetch the file from the
// database first.
final url = getAttachmentUrl();
if (url?.scheme == 'local') {
final file = await downloadAndDecryptAttachment();
return await room.sendFileEvent(file, extraContent: content);
}
// we do not remove the event here. It will automatically be updated // we do not remove the event here. It will automatically be updated
// in the `sendEvent` method to transition -1 -> 0 -> 1 -> 2 // in the `sendEvent` method to transition -1 -> 0 -> 1 -> 2
final newEventId = await room.sendEvent( return await room.sendEvent(
content, content,
txid: txid ?? unsigned?['transaction_id'] ?? eventId, txid: txid ?? unsigned?['transaction_id'] ?? eventId,
); );
return newEventId;
} }
/// Whether the client is allowed to redact this event. /// Whether the client is allowed to redact this event.

View File

@ -695,7 +695,7 @@ class Room {
/// Set [shrinkImageMaxDimension] to for example `1600` if you want to shrink /// Set [shrinkImageMaxDimension] to for example `1600` if you want to shrink
/// your image before sending. This is ignored if the File is not a /// your image before sending. This is ignored if the File is not a
/// [MatrixImageFile]. /// [MatrixImageFile].
Future<Uri> sendFileEvent( Future<String?> sendFileEvent(
MatrixFile file, { MatrixFile file, {
String? txid, String? txid,
Event? inReplyTo, Event? inReplyTo,
@ -755,6 +755,7 @@ class Room {
name: file.name, name: file.name,
maxDimension: shrinkImageMaxDimension, maxDimension: shrinkImageMaxDimension,
customImageResizer: client.customImageResizer, customImageResizer: client.customImageResizer,
compute: client.runInBackground,
); );
} }
} }
@ -859,14 +860,14 @@ class Room {
}, },
if (extraContent != null) ...extraContent, if (extraContent != null) ...extraContent,
}; };
await sendEvent( final eventId = await sendEvent(
content, content,
txid: txid, txid: txid,
inReplyTo: inReplyTo, inReplyTo: inReplyTo,
editEventId: editEventId, editEventId: editEventId,
); );
sendingFilePlaceholders.remove(txid); sendingFilePlaceholders.remove(txid);
return uploadResp; return eventId;
} }
Future<String?> _sendContent( Future<String?> _sendContent(
@ -1275,8 +1276,7 @@ class Room {
void Function()? onUpdate, void Function()? onUpdate,
}) async { }) async {
await postLoad(); await postLoad();
var events; final events = await client.database?.getEventList(
events = await client.database?.getEventList(
this, this,
limit: defaultHistoryCount, limit: defaultHistoryCount,
) ?? ) ??
@ -1288,13 +1288,20 @@ class Room {
for (var i = 0; i < events.length; i++) { for (var i = 0; i < events.length; i++) {
if (events[i].type == EventTypes.Encrypted && if (events[i].type == EventTypes.Encrypted &&
events[i].content['can_request_session'] == true) { events[i].content['can_request_session'] == true) {
events[i] = await client.encryption events[i] = await client.encryption!
?.decryptRoomEvent(id, events[i], store: true); .decryptRoomEvent(id, events[i], store: true);
} }
} }
}); });
} }
// Fetch all users from database we have got here.
for (final event in events) {
if (getState(EventTypes.RoomMember, event.senderId) != null) continue;
final dbUser = await client.database?.getUser(event.senderId, this);
if (dbUser != null) setState(dbUser);
}
final timeline = Timeline( final timeline = Timeline(
room: this, room: this,
events: events, events: events,
@ -1303,9 +1310,6 @@ class Room {
onInsert: onInsert, onInsert: onInsert,
onUpdate: onUpdate, onUpdate: onUpdate,
); );
if (client.database == null) {
await requestHistory(historyCount: 10);
}
return timeline; return timeline;
} }

View File

@ -85,6 +85,16 @@ class Timeline {
limit: Room.defaultHistoryCount, limit: Room.defaultHistoryCount,
); );
if (eventsFromStore != null && eventsFromStore.isNotEmpty) { if (eventsFromStore != null && eventsFromStore.isNotEmpty) {
// Fetch all users from database we have got here.
for (final event in events) {
if (room.getState(EventTypes.RoomMember, event.senderId) != null) {
continue;
}
final dbUser =
await room.client.database?.getUser(event.senderId, room);
if (dbUser != null) room.setState(dbUser);
}
events.addAll(eventsFromStore); events.addAll(eventsFromStore);
final startIndex = events.length - eventsFromStore.length; final startIndex = events.length - eventsFromStore.length;
final endIndex = events.length; final endIndex = events.length;

View File

@ -745,7 +745,7 @@ void main() {
test('sendFileEvent', () async { test('sendFileEvent', () async {
final testFile = MatrixFile(bytes: Uint8List(0), name: 'file.jpeg'); final testFile = MatrixFile(bytes: Uint8List(0), name: 'file.jpeg');
final dynamic resp = await room.sendFileEvent(testFile, txid: 'testtxid'); final dynamic resp = await room.sendFileEvent(testFile, txid: 'testtxid');
expect(resp.toString(), 'mxc://example.com/AQwafuaFswefuhsfAFAgsw'); expect(resp.toString(), '\$event10');
}); });
test('pushRuleState', () async { test('pushRuleState', () async {