refactor: Event Update Type

This commit is contained in:
Christian Pauly 2020-10-22 12:21:20 +02:00
parent be6824b746
commit 66e590073e
11 changed files with 118 additions and 90 deletions

View File

@ -105,7 +105,7 @@ class Encryption {
}
Future<void> handleEventUpdate(EventUpdate update) async {
if (update.type == 'ephemeral') {
if (update.type == EventUpdateType.ephemeral) {
return;
}
if (update.eventType.startsWith('m.key.verification.') ||
@ -235,7 +235,8 @@ class Encryption {
}
Future<Event> decryptRoomEvent(String roomId, Event event,
{bool store = false, String updateType = 'timeline'}) async {
{bool store = false,
EventUpdateType updateType = EventUpdateType.timeline}) async {
final doStore = () async {
await client.database?.storeEventUpdate(
client.id,
@ -247,7 +248,7 @@ class Encryption {
sortOrder: event.sortOrder,
),
);
if (updateType != 'history') {
if (updateType != EventUpdateType.history) {
event.room?.setState(event);
}
};

View File

@ -887,14 +887,16 @@ class Client extends MatrixApi {
if (room.state?.isNotEmpty ?? false) {
// TODO: This method seems to be comperatively slow for some updates
await _handleRoomEvents(
id, room.state.map((i) => i.toJson()).toList(), 'state');
id,
room.state.map((i) => i.toJson()).toList(),
EventUpdateType.state);
handledEvents = true;
}
if (room.timeline?.events?.isNotEmpty ?? false) {
await _handleRoomEvents(
id,
room.timeline.events.map((i) => i.toJson()).toList(),
sortAtTheEnd ? 'history' : 'timeline',
sortAtTheEnd ? EventUpdateType.history : EventUpdateType.timeline,
sortAtTheEnd: sortAtTheEnd);
handledEvents = true;
}
@ -904,30 +906,40 @@ class Client extends MatrixApi {
id, room.ephemeral.map((i) => i.toJson()).toList());
}
if (room.accountData?.isNotEmpty ?? false) {
await _handleRoomEvents(id,
room.accountData.map((i) => i.toJson()).toList(), 'account_data');
await _handleRoomEvents(
id,
room.accountData.map((i) => i.toJson()).toList(),
EventUpdateType.accountData);
}
}
if (room is LeftRoomUpdate) {
if (room.timeline?.events?.isNotEmpty ?? false) {
await _handleRoomEvents(id,
room.timeline.events.map((i) => i.toJson()).toList(), 'timeline');
await _handleRoomEvents(
id,
room.timeline.events.map((i) => i.toJson()).toList(),
EventUpdateType.timeline);
handledEvents = true;
}
if (room.accountData?.isNotEmpty ?? false) {
await _handleRoomEvents(id,
room.accountData.map((i) => i.toJson()).toList(), 'account_data');
await _handleRoomEvents(
id,
room.accountData.map((i) => i.toJson()).toList(),
EventUpdateType.accountData);
}
if (room.state?.isNotEmpty ?? false) {
await _handleRoomEvents(
id, room.state.map((i) => i.toJson()).toList(), 'state');
id,
room.state.map((i) => i.toJson()).toList(),
EventUpdateType.state);
handledEvents = true;
}
}
if (room is InvitedRoomUpdate &&
(room.inviteState?.isNotEmpty ?? false)) {
await _handleRoomEvents(id,
room.inviteState.map((i) => i.toJson()).toList(), 'invite_state');
await _handleRoomEvents(
id,
room.inviteState.map((i) => i.toJson()).toList(),
EventUpdateType.inviteState);
}
if (handledEvents && database != null && roomObj != null) {
await roomObj.updateSortOrder();
@ -937,7 +949,7 @@ class Client extends MatrixApi {
Future<void> _handleEphemerals(String id, List<dynamic> events) async {
for (num i = 0; i < events.length; i++) {
await _handleEvent(events[i], id, 'ephemeral');
await _handleEvent(events[i], id, EventUpdateType.ephemeral);
// Receipt events are deltas between two states. We will create a
// fake room account data event for this and store the difference
@ -974,13 +986,13 @@ class Client extends MatrixApi {
}
}
events[i]['content'] = receiptStateContent;
await _handleEvent(events[i], id, 'account_data');
await _handleEvent(events[i], id, EventUpdateType.accountData);
}
}
}
Future<void> _handleRoomEvents(
String chat_id, List<dynamic> events, String type,
String chat_id, List<dynamic> events, EventUpdateType type,
{bool sortAtTheEnd = false}) async {
for (num i = 0; i < events.length; i++) {
await _handleEvent(events[i], chat_id, type, sortAtTheEnd: sortAtTheEnd);
@ -988,7 +1000,7 @@ class Client extends MatrixApi {
}
Future<void> _handleEvent(
Map<String, dynamic> event, String roomID, String type,
Map<String, dynamic> event, String roomID, EventUpdateType type,
{bool sortAtTheEnd = false}) async {
if (event['type'] is String && event['content'] is Map<String, dynamic>) {
// The client must ignore any new m.room.encryption event to prevent
@ -1004,7 +1016,7 @@ class Client extends MatrixApi {
// ephemeral events aren't persisted and don't need a sort order - they are
// expected to be processed as soon as they come in
final sortOrder = type != 'ephemeral'
final sortOrder = type != EventUpdateType.ephemeral
? (sortAtTheEnd ? room.oldSortOrder : room.newSortOrder)
: 0.0;
var update = EventUpdate(
@ -1027,7 +1039,7 @@ class Client extends MatrixApi {
room.setState(user);
}
}
if (type != 'ephemeral' && database != null) {
if (type != EventUpdateType.ephemeral && database != null) {
await database.storeEventUpdate(id, update);
}
_updateRoomsByEventUpdate(update);
@ -1038,7 +1050,7 @@ class Client extends MatrixApi {
final rawUnencryptedEvent = update.content;
if (prevBatch != null && type == 'timeline') {
if (prevBatch != null && type == EventUpdateType.timeline) {
if (rawUnencryptedEvent['type'] == EventTypes.CallInvite) {
onCallInvite
.add(Event.fromJson(rawUnencryptedEvent, room, sortOrder));
@ -1117,15 +1129,15 @@ class Client extends MatrixApi {
}
void _updateRoomsByEventUpdate(EventUpdate eventUpdate) {
if (eventUpdate.type == 'history') return;
if (eventUpdate.type == EventUpdateType.history) return;
final room = getRoomById(eventUpdate.roomID);
if (room == null) return;
switch (eventUpdate.type) {
case 'timeline':
case 'state':
case 'invite_state':
case EventUpdateType.timeline:
case EventUpdateType.state:
case EventUpdateType.inviteState:
var stateEvent =
Event.fromJson(eventUpdate.content, room, eventUpdate.sortOrder);
var prevState = room.getState(stateEvent.type, stateEvent.stateKey);
@ -1151,14 +1163,16 @@ sort order of ${prevState.sortOrder}. This should never happen...''');
room.setState(stateEvent);
}
break;
case 'account_data':
case EventUpdateType.accountData:
room.roomAccountData[eventUpdate.eventType] =
BasicRoomEvent.fromJson(eventUpdate.content);
break;
case 'ephemeral':
case EventUpdateType.ephemeral:
room.ephemerals[eventUpdate.eventType] =
BasicRoomEvent.fromJson(eventUpdate.content);
break;
case EventUpdateType.history:
break;
}
room.onUpdate.add(room.id);
}

View File

@ -6,7 +6,6 @@ import 'package:olm/olm.dart' as olm;
import '../../famedlysdk.dart' as sdk;
import '../../matrix_api.dart' as api;
import '../../matrix_api.dart';
import '../client.dart';
import '../room.dart';
import '../utils/logs.dart';
@ -266,13 +265,13 @@ class Database extends _$Database {
// let's see if we need any m.room.member events
final membersToPostload = <String>{};
// the lastEvent message preview might have an author we need to fetch, if it is a group chat
if (room.getState(EventTypes.Message) != null && !room.isDirectChat) {
membersToPostload.add(room.getState(EventTypes.Message).senderId);
if (room.getState(api.EventTypes.Message) != null && !room.isDirectChat) {
membersToPostload.add(room.getState(api.EventTypes.Message).senderId);
}
// if the room has no name and no canonical alias, its name is calculated
// based on the heroes of the room
if (room.getState(EventTypes.RoomName) == null &&
room.getState(EventTypes.RoomCanonicalAlias) == null &&
if (room.getState(api.EventTypes.RoomName) == null &&
room.getState(api.EventTypes.RoomCanonicalAlias) == null &&
room.mHeroes != null) {
// we don't have a name and no canonical alias, so we'll need to
// post-load the heroes
@ -430,7 +429,7 @@ class Database extends _$Database {
/// [transaction].
Future<void> storeEventUpdate(
int clientId, sdk.EventUpdate eventUpdate) async {
if (eventUpdate.type == 'ephemeral') return;
if (eventUpdate.type == sdk.EventUpdateType.ephemeral) return;
final eventContent = eventUpdate.content;
final type = eventUpdate.type;
final chatId = eventUpdate.roomID;
@ -441,11 +440,12 @@ class Database extends _$Database {
stateKey = eventContent['state_key'];
}
if (eventUpdate.eventType == EventTypes.Redaction) {
if (eventUpdate.eventType == api.EventTypes.Redaction) {
await redactMessage(clientId, eventUpdate);
}
if (type == 'timeline' || type == 'history') {
if (type == sdk.EventUpdateType.timeline ||
type == sdk.EventUpdateType.history) {
// calculate the status
var status = 2;
if (eventContent['unsigned'] is Map<String, dynamic> &&
@ -493,7 +493,7 @@ class Database extends _$Database {
}
if (storeNewEvent) {
DbEvent oldEvent;
if (type == 'history') {
if (type == sdk.EventUpdateType.history) {
final allOldEvents =
await getEvent(clientId, eventContent['event_id'], chatId).get();
if (allOldEvents.isNotEmpty) {
@ -527,12 +527,15 @@ class Database extends _$Database {
}
}
if (type == 'history') return;
if (type == sdk.EventUpdateType.history) return;
if (type != 'account_data' &&
if (type != sdk.EventUpdateType.accountData &&
((stateKey is String) ||
[EventTypes.Message, EventTypes.Sticker, EventTypes.Encrypted]
.contains(eventUpdate.eventType))) {
[
api.EventTypes.Message,
api.EventTypes.Sticker,
api.EventTypes.Encrypted
].contains(eventUpdate.eventType))) {
final now = DateTime.now();
await storeRoomState(
clientId,
@ -547,7 +550,7 @@ class Database extends _$Database {
json.encode(eventContent['prev_content'] ?? ''),
stateKey ?? '',
);
} else if (type == 'account_data') {
} else if (type == sdk.EventUpdateType.accountData) {
await storeRoomAccountData(
clientId,
eventContent['type'],

View File

@ -316,7 +316,7 @@ class Event extends MatrixEvent {
room.client.onEvent.add(EventUpdate(
roomID: room.id,
type: 'timeline',
type: EventUpdateType.timeline,
eventType: type,
content: {
'event_id': eventId,

View File

@ -1190,7 +1190,7 @@ class Room {
EventUpdate(
content: content,
roomID: id,
type: 'state',
type: EventUpdateType.state,
eventType: EventTypes.RoomMember,
sortOrder: 0.0),
);

View File

@ -211,7 +211,8 @@ class Timeline {
try {
if (eventUpdate.roomID != room.id) return;
if (eventUpdate.type == 'timeline' || eventUpdate.type == 'history') {
if (eventUpdate.type == EventUpdateType.timeline ||
eventUpdate.type == EventUpdateType.history) {
var status = eventUpdate.content['status'] ??
(eventUpdate.content['unsigned'] is Map<String, dynamic>
? eventUpdate.content['unsigned'][MessageSendingStatusKey]
@ -252,7 +253,7 @@ class Timeline {
var newEvent = Event.fromJson(
eventUpdate.content, room, eventUpdate.sortOrder);
if (eventUpdate.type == 'history' &&
if (eventUpdate.type == EventUpdateType.history &&
events.indexWhere(
(e) => e.eventId == eventUpdate.content['event_id']) !=
-1) return;

View File

@ -20,11 +20,20 @@ import '../../famedlysdk.dart';
import '../../matrix_api.dart';
import 'logs.dart';
enum EventUpdateType {
timeline,
state,
history,
accountData,
ephemeral,
inviteState
}
/// Represents a new event (e.g. a message in a room) or an update for an
/// already known event.
class EventUpdate {
/// Usually 'timeline', 'state' or whatever.
final String type;
final EventUpdateType type;
/// Most events belong to a room. If not, this equals to eventType.
final String roomID;

View File

@ -240,55 +240,55 @@ void main() {
expect(eventUpdateList[0].eventType, 'm.room.member');
expect(eventUpdateList[0].roomID, '!726s6s6q:example.com');
expect(eventUpdateList[0].type, 'state');
expect(eventUpdateList[0].type, EventUpdateType.state);
expect(eventUpdateList[1].eventType, 'm.room.canonical_alias');
expect(eventUpdateList[1].roomID, '!726s6s6q:example.com');
expect(eventUpdateList[1].type, 'state');
expect(eventUpdateList[1].type, EventUpdateType.state);
expect(eventUpdateList[2].eventType, 'm.room.encryption');
expect(eventUpdateList[2].roomID, '!726s6s6q:example.com');
expect(eventUpdateList[2].type, 'state');
expect(eventUpdateList[2].type, EventUpdateType.state);
expect(eventUpdateList[3].eventType, 'm.room.pinned_events');
expect(eventUpdateList[3].roomID, '!726s6s6q:example.com');
expect(eventUpdateList[3].type, 'state');
expect(eventUpdateList[3].type, EventUpdateType.state);
expect(eventUpdateList[4].eventType, 'm.room.member');
expect(eventUpdateList[4].roomID, '!726s6s6q:example.com');
expect(eventUpdateList[4].type, 'timeline');
expect(eventUpdateList[4].type, EventUpdateType.timeline);
expect(eventUpdateList[5].eventType, 'm.room.message');
expect(eventUpdateList[5].roomID, '!726s6s6q:example.com');
expect(eventUpdateList[5].type, 'timeline');
expect(eventUpdateList[5].type, EventUpdateType.timeline);
expect(eventUpdateList[6].eventType, 'm.typing');
expect(eventUpdateList[6].roomID, '!726s6s6q:example.com');
expect(eventUpdateList[6].type, 'ephemeral');
expect(eventUpdateList[6].type, EventUpdateType.ephemeral);
expect(eventUpdateList[7].eventType, 'm.receipt');
expect(eventUpdateList[7].roomID, '!726s6s6q:example.com');
expect(eventUpdateList[7].type, 'ephemeral');
expect(eventUpdateList[7].type, EventUpdateType.ephemeral);
expect(eventUpdateList[8].eventType, 'm.receipt');
expect(eventUpdateList[8].roomID, '!726s6s6q:example.com');
expect(eventUpdateList[8].type, 'account_data');
expect(eventUpdateList[8].type, EventUpdateType.accountData);
expect(eventUpdateList[9].eventType, 'm.tag');
expect(eventUpdateList[9].roomID, '!726s6s6q:example.com');
expect(eventUpdateList[9].type, 'account_data');
expect(eventUpdateList[9].type, EventUpdateType.accountData);
expect(eventUpdateList[10].eventType, 'org.example.custom.room.config');
expect(eventUpdateList[10].roomID, '!726s6s6q:example.com');
expect(eventUpdateList[10].type, 'account_data');
expect(eventUpdateList[10].type, EventUpdateType.accountData);
expect(eventUpdateList[11].eventType, 'm.room.name');
expect(eventUpdateList[11].roomID, '!696r7674:example.com');
expect(eventUpdateList[11].type, 'invite_state');
expect(eventUpdateList[11].type, EventUpdateType.inviteState);
expect(eventUpdateList[12].eventType, 'm.room.member');
expect(eventUpdateList[12].roomID, '!696r7674:example.com');
expect(eventUpdateList[12].type, 'invite_state');
expect(eventUpdateList[12].type, EventUpdateType.inviteState);
});
test('To Device Update Test', () async {

View File

@ -54,7 +54,7 @@ EventUpdate getLastSentEvent(KeyVerification req) {
'sender': req.client.userID,
},
eventType: type,
type: 'timeline',
type: EventUpdateType.timeline,
roomID: req.room.id,
);
}
@ -446,7 +446,7 @@ void main() {
'sender': client2.userID,
},
eventType: 'm.key.verification.ready',
type: 'timeline',
type: EventUpdateType.timeline,
roomID: req2.room.id,
));
expect(req2.state, KeyVerificationState.error);

View File

@ -40,7 +40,7 @@ void main() {
test('storeEventUpdate', () async {
// store a simple update
var update = EventUpdate(
type: 'timeline',
type: EventUpdateType.timeline,
roomID: room.id,
eventType: 'm.room.message',
content: {
@ -58,7 +58,7 @@ void main() {
// insert a transaction id
update = EventUpdate(
type: 'timeline',
type: EventUpdateType.timeline,
roomID: room.id,
eventType: 'm.room.message',
content: {
@ -75,7 +75,7 @@ void main() {
event = await database.getEventById(clientId, 'transaction-1', room);
expect(event.eventId, 'transaction-1');
update = EventUpdate(
type: 'timeline',
type: EventUpdateType.timeline,
roomID: room.id,
eventType: 'm.room.message',
content: {
@ -98,7 +98,7 @@ void main() {
// insert a transaction id if the event id for it already exists
update = EventUpdate(
type: 'timeline',
type: EventUpdateType.timeline,
roomID: room.id,
eventType: 'm.room.message',
content: {
@ -115,7 +115,7 @@ void main() {
event = await database.getEventById(clientId, '\$event-3', room);
expect(event.eventId, '\$event-3');
update = EventUpdate(
type: 'timeline',
type: EventUpdateType.timeline,
roomID: room.id,
eventType: 'm.room.message',
content: {
@ -140,7 +140,7 @@ void main() {
// insert transaction id and not update status
update = EventUpdate(
type: 'timeline',
type: EventUpdateType.timeline,
roomID: room.id,
eventType: 'm.room.message',
content: {
@ -157,7 +157,7 @@ void main() {
event = await database.getEventById(clientId, '\$event-4', room);
expect(event.eventId, '\$event-4');
update = EventUpdate(
type: 'timeline',
type: EventUpdateType.timeline,
roomID: room.id,
eventType: 'm.room.message',
content: {

View File

@ -52,7 +52,7 @@ void main() {
await client.checkServer('https://fakeServer.notExisting');
client.onEvent.add(EventUpdate(
type: 'timeline',
type: EventUpdateType.timeline,
roomID: roomID,
eventType: 'm.room.message',
content: {
@ -65,7 +65,7 @@ void main() {
},
sortOrder: room.newSortOrder));
client.onEvent.add(EventUpdate(
type: 'timeline',
type: EventUpdateType.timeline,
roomID: roomID,
eventType: 'm.room.message',
content: {
@ -114,7 +114,7 @@ void main() {
expect(timeline.events[0].receipts[0].user.id, '@alice:example.com');
client.onEvent.add(EventUpdate(
type: 'timeline',
type: EventUpdateType.timeline,
roomID: roomID,
eventType: 'm.room.redaction',
content: {
@ -149,7 +149,7 @@ void main() {
expect(timeline.events[0].status, 1);
client.onEvent.add(EventUpdate(
type: 'timeline',
type: EventUpdateType.timeline,
roomID: roomID,
eventType: 'm.room.message',
content: {
@ -174,7 +174,7 @@ void main() {
test('Send message with error', () async {
client.onEvent.add(EventUpdate(
type: 'timeline',
type: EventUpdateType.timeline,
roomID: roomID,
eventType: 'm.room.message',
content: {
@ -221,7 +221,7 @@ void main() {
test('Resend message', () async {
timeline.events.clear();
client.onEvent.add(EventUpdate(
type: 'timeline',
type: EventUpdateType.timeline,
roomID: roomID,
eventType: 'm.room.message',
content: {
@ -278,7 +278,7 @@ void main() {
test('sort errors on top', () async {
timeline.events.clear();
client.onEvent.add(EventUpdate(
type: 'timeline',
type: EventUpdateType.timeline,
roomID: roomID,
eventType: 'm.room.message',
content: {
@ -291,7 +291,7 @@ void main() {
},
sortOrder: room.newSortOrder));
client.onEvent.add(EventUpdate(
type: 'timeline',
type: EventUpdateType.timeline,
roomID: roomID,
eventType: 'm.room.message',
content: {
@ -311,7 +311,7 @@ void main() {
test('sending event to failed update', () async {
timeline.events.clear();
client.onEvent.add(EventUpdate(
type: 'timeline',
type: EventUpdateType.timeline,
roomID: roomID,
eventType: 'm.room.message',
content: {
@ -327,7 +327,7 @@ void main() {
expect(timeline.events[0].status, 0);
expect(timeline.events.length, 1);
client.onEvent.add(EventUpdate(
type: 'timeline',
type: EventUpdateType.timeline,
roomID: roomID,
eventType: 'm.room.message',
content: {
@ -347,7 +347,7 @@ void main() {
() async {
timeline.events.clear();
client.onEvent.add(EventUpdate(
type: 'timeline',
type: EventUpdateType.timeline,
roomID: roomID,
eventType: 'm.room.message',
content: {
@ -363,7 +363,7 @@ void main() {
expect(timeline.events[0].status, 0);
expect(timeline.events.length, 1);
client.onEvent.add(EventUpdate(
type: 'timeline',
type: EventUpdateType.timeline,
roomID: roomID,
eventType: 'm.room.message',
content: {
@ -380,7 +380,7 @@ void main() {
expect(timeline.events[0].status, 1);
expect(timeline.events.length, 1);
client.onEvent.add(EventUpdate(
type: 'timeline',
type: EventUpdateType.timeline,
roomID: roomID,
eventType: 'm.room.message',
content: {
@ -401,7 +401,7 @@ void main() {
() async {
timeline.events.clear();
client.onEvent.add(EventUpdate(
type: 'timeline',
type: EventUpdateType.timeline,
roomID: roomID,
eventType: 'm.room.message',
content: {
@ -420,7 +420,7 @@ void main() {
expect(timeline.events[0].status, 0);
expect(timeline.events.length, 1);
client.onEvent.add(EventUpdate(
type: 'timeline',
type: EventUpdateType.timeline,
roomID: roomID,
eventType: 'm.room.message',
content: {
@ -439,7 +439,7 @@ void main() {
expect(timeline.events[0].status, 2);
expect(timeline.events.length, 1);
client.onEvent.add(EventUpdate(
type: 'timeline',
type: EventUpdateType.timeline,
roomID: roomID,
eventType: 'm.room.message',
content: {
@ -461,7 +461,7 @@ void main() {
test('sending an event 0 -> -1 -> 2', () async {
timeline.events.clear();
client.onEvent.add(EventUpdate(
type: 'timeline',
type: EventUpdateType.timeline,
roomID: roomID,
eventType: 'm.room.message',
content: {
@ -477,7 +477,7 @@ void main() {
expect(timeline.events[0].status, 0);
expect(timeline.events.length, 1);
client.onEvent.add(EventUpdate(
type: 'timeline',
type: EventUpdateType.timeline,
roomID: roomID,
eventType: 'm.room.message',
content: {
@ -493,7 +493,7 @@ void main() {
expect(timeline.events[0].status, -1);
expect(timeline.events.length, 1);
client.onEvent.add(EventUpdate(
type: 'timeline',
type: EventUpdateType.timeline,
roomID: roomID,
eventType: 'm.room.message',
content: {
@ -513,7 +513,7 @@ void main() {
test('sending an event 0 -> 2 -> -1', () async {
timeline.events.clear();
client.onEvent.add(EventUpdate(
type: 'timeline',
type: EventUpdateType.timeline,
roomID: roomID,
eventType: 'm.room.message',
content: {
@ -529,7 +529,7 @@ void main() {
expect(timeline.events[0].status, 0);
expect(timeline.events.length, 1);
client.onEvent.add(EventUpdate(
type: 'timeline',
type: EventUpdateType.timeline,
roomID: roomID,
eventType: 'm.room.message',
content: {
@ -546,7 +546,7 @@ void main() {
expect(timeline.events[0].status, 2);
expect(timeline.events.length, 1);
client.onEvent.add(EventUpdate(
type: 'timeline',
type: EventUpdateType.timeline,
roomID: roomID,
eventType: 'm.room.message',
content: {