From 0ceb2b26df4401c29885b4f3a04be9800f69636b Mon Sep 17 00:00:00 2001 From: Christian Pauly Date: Mon, 29 Mar 2021 10:38:51 +0200 Subject: [PATCH] refactor: Constants names --- analysis_options.yaml | 2 +- lib/encryption/key_manager.dart | 14 ++-- lib/encryption/ssss.dart | 58 +++++++------- lib/encryption/utils/bootstrap.dart | 2 +- lib/encryption/utils/key_verification.dart | 24 +++--- lib/src/database/database.dart | 10 +-- lib/src/event.dart | 28 +++---- lib/src/room.dart | 77 ++++++++++--------- lib/src/timeline.dart | 4 +- lib/src/utils/marked_unread.dart | 2 +- lib/src/utils/matrix_id_string_extension.dart | 8 +- lib/src/utils/matrix_localizations.dart | 4 +- test/device_keys_list_test.dart | 2 +- test/encryption/cross_signing_test.dart | 2 +- test/encryption/key_verification_test.dart | 6 +- test/encryption/online_key_backup_test.dart | 2 +- test/encryption/ssss_test.dart | 16 ++-- test/event_test.dart | 34 ++++---- test/fake_client.dart | 4 +- test/matrix_localizations_test.dart | 4 +- test/room_test.dart | 12 +-- test/sync_filter_test.dart | 4 +- test/timeline_test.dart | 6 +- 23 files changed, 165 insertions(+), 160 deletions(-) diff --git a/analysis_options.yaml b/analysis_options.yaml index 6d8082c2..bb697490 100644 --- a/analysis_options.yaml +++ b/analysis_options.yaml @@ -4,12 +4,12 @@ linter: rules: - camel_case_types - avoid_print + - constant_identifier_names analyzer: errors: todo: ignore exclude: - example/main.dart - # needed until crypto packages upgrade - lib/src/database/database.g.dart diff --git a/lib/encryption/key_manager.dart b/lib/encryption/key_manager.dart index 2127e320..e6994fee 100644 --- a/lib/encryption/key_manager.dart +++ b/lib/encryption/key_manager.dart @@ -28,7 +28,7 @@ import '../src/database/database.dart'; import '../src/utils/run_in_background.dart'; import '../src/utils/run_in_root.dart'; -const MEGOLM_KEY = EventTypes.MegolmBackup; +const megolmKey = EventTypes.MegolmBackup; class KeyManager { final Encryption encryption; @@ -41,7 +41,7 @@ class KeyManager { final Set _requestedSessionIds = {}; KeyManager(this.encryption) { - encryption.ssss.setValidator(MEGOLM_KEY, (String secret) async { + encryption.ssss.setValidator(megolmKey, (String secret) async { final keyObj = olm.PkDecryption(); try { final info = await getRoomKeysBackupInfo(false); @@ -56,7 +56,7 @@ class KeyManager { keyObj.free(); } }); - encryption.ssss.setCacheCallback(MEGOLM_KEY, (String secret) { + encryption.ssss.setCacheCallback(megolmKey, (String secret) { // we got a megolm key cached, clear our requested keys and try to re-decrypt // last events _requestedSessionIds.clear(); @@ -75,7 +75,7 @@ class KeyManager { }); } - bool get enabled => encryption.ssss.isSecret(MEGOLM_KEY); + bool get enabled => encryption.ssss.isSecret(megolmKey); /// clear all cached inbound group sessions. useful for testing void clearInboundGroupSessions() { @@ -522,7 +522,7 @@ class KeyManager { if (!enabled) { return false; } - return (await encryption.ssss.getCached(MEGOLM_KEY)) != null; + return (await encryption.ssss.getCached(megolmKey)) != null; } RoomKeysVersionResponse _roomKeysVersionCache; @@ -547,7 +547,7 @@ class KeyManager { return; } final privateKey = - base64.decode(await encryption.ssss.getCached(MEGOLM_KEY)); + base64.decode(await encryption.ssss.getCached(megolmKey)); final decryption = olm.PkDecryption(); final info = await getRoomKeysBackupInfo(); String backupPubKey; @@ -699,7 +699,7 @@ class KeyManager { return; // nothing to do } final privateKey = - base64.decode(await encryption.ssss.getCached(MEGOLM_KEY)); + base64.decode(await encryption.ssss.getCached(megolmKey)); // decryption is needed to calculate the public key and thus see if the claimed information is in fact valid final decryption = olm.PkDecryption(); final info = await getRoomKeysBackupInfo(false); diff --git a/lib/encryption/ssss.dart b/lib/encryption/ssss.dart index 2e16154b..ecad784d 100644 --- a/lib/encryption/ssss.dart +++ b/lib/encryption/ssss.dart @@ -32,21 +32,21 @@ import '../src/utils/run_in_background.dart'; import '../src/utils/run_in_root.dart'; import 'encryption.dart'; -const CACHE_TYPES = { +const cacheTypes = { EventTypes.CrossSigningSelfSigning, EventTypes.CrossSigningUserSigning, EventTypes.MegolmBackup, }; -const ZERO_STR = +const zeroStr = '\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'; -const BASE58_ALPHABET = +const base58Alphabet = '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz'; -const base58 = Base58Codec(BASE58_ALPHABET); -const OLM_RECOVERY_KEY_PREFIX = [0x8B, 0x01]; -const SSSS_KEY_LENGTH = 32; -const PBKDF2_DEFAULT_ITERATIONS = 500000; -const PBKDF2_SALT_LENGTH = 64; +const base58 = Base58Codec(base58Alphabet); +const olmRecoveryKeyPrefix = [0x8B, 0x01]; +const ssssKeyLength = 32; +const pbkdf2DefaultIterations = 500000; +const pbkdf2SaltLength = 64; /// SSSS: **S**ecure **S**ecret **S**torage and **S**haring /// Read more about SSSS at: @@ -129,23 +129,23 @@ class SSSS { throw Exception('Incorrect parity'); } - for (var i = 0; i < OLM_RECOVERY_KEY_PREFIX.length; i++) { - if (result[i] != OLM_RECOVERY_KEY_PREFIX[i]) { + for (var i = 0; i < olmRecoveryKeyPrefix.length; i++) { + if (result[i] != olmRecoveryKeyPrefix[i]) { throw Exception('Incorrect prefix'); } } - if (result.length != OLM_RECOVERY_KEY_PREFIX.length + SSSS_KEY_LENGTH + 1) { + if (result.length != olmRecoveryKeyPrefix.length + ssssKeyLength + 1) { throw Exception('Incorrect length'); } - return Uint8List.fromList(result.sublist(OLM_RECOVERY_KEY_PREFIX.length, - OLM_RECOVERY_KEY_PREFIX.length + SSSS_KEY_LENGTH)); + return Uint8List.fromList(result.sublist(olmRecoveryKeyPrefix.length, + olmRecoveryKeyPrefix.length + ssssKeyLength)); } static String encodeRecoveryKey(Uint8List recoveryKey) { final keyToEncode = []; - for (final b in OLM_RECOVERY_KEY_PREFIX) { + for (final b in olmRecoveryKeyPrefix) { keyToEncode.add(b); } keyToEncode.addAll(recoveryKey); @@ -208,10 +208,10 @@ class SSSS { // we need to derive the key off of the passphrase content.passphrase = PassphraseInfo(); content.passphrase.algorithm = AlgorithmTypes.pbkdf2; - content.passphrase.salt = base64 - .encode(SecureRandom(PBKDF2_SALT_LENGTH).bytes); // generate salt - content.passphrase.iterations = PBKDF2_DEFAULT_ITERATIONS; - content.passphrase.bits = SSSS_KEY_LENGTH * 8; + content.passphrase.salt = + base64.encode(SecureRandom(pbkdf2SaltLength).bytes); // generate salt + content.passphrase.iterations = pbkdf2DefaultIterations; + content.passphrase.bits = ssssKeyLength * 8; privateKey = await runInBackground( _keyFromPassphrase, _KeyFromPassphraseArgs( @@ -222,20 +222,20 @@ class SSSS { ); } else { // we need to just generate a new key from scratch - privateKey = Uint8List.fromList(SecureRandom(SSSS_KEY_LENGTH).bytes); + privateKey = Uint8List.fromList(SecureRandom(ssssKeyLength).bytes); } // now that we have the private key, let's create the iv and mac - final encrypted = encryptAes(ZERO_STR, privateKey, ''); + final encrypted = encryptAes(zeroStr, privateKey, ''); content.iv = encrypted.iv; content.mac = encrypted.mac; content.algorithm = AlgorithmTypes.secretStorageV1AesHmcSha2; - const KEYID_BYTE_LENGTH = 24; + const keyidByteLength = 24; // make sure we generate a unique key id - var keyId = base64.encode(SecureRandom(KEYID_BYTE_LENGTH).bytes); + var keyId = base64.encode(SecureRandom(keyidByteLength).bytes); while (getKey(keyId) != null) { - keyId = base64.encode(SecureRandom(KEYID_BYTE_LENGTH).bytes); + keyId = base64.encode(SecureRandom(keyidByteLength).bytes); } final accountDataType = EventTypes.secretStorageKey(keyId); @@ -255,7 +255,7 @@ class SSSS { bool checkKey(Uint8List key, SecretStorageKeyContent info) { if (info.algorithm == AlgorithmTypes.secretStorageV1AesHmcSha2) { if ((info.mac is String) && (info.iv is String)) { - final encrypted = encryptAes(ZERO_STR, key, '', info.iv); + final encrypted = encryptAes(zeroStr, key, '', info.iv); return info.mac.replaceAll(RegExp(r'=+$'), '') == encrypted.mac.replaceAll(RegExp(r'=+$'), ''); } else { @@ -314,7 +314,7 @@ class SSSS { final encryptInfo = _Encrypted( iv: enc['iv'], ciphertext: enc['ciphertext'], mac: enc['mac']); final decrypted = decryptAes(encryptInfo, key, type); - if (CACHE_TYPES.contains(type) && client.database != null) { + if (cacheTypes.contains(type) && client.database != null) { // cache the thing await client.database .storeSSSSCache(client.id, type, keyId, enc['ciphertext'], decrypted); @@ -347,7 +347,7 @@ class SSSS { }; // store the thing in your account data await client.setAccountData(client.userID, type, content); - if (CACHE_TYPES.contains(type) && client.database != null) { + if (cacheTypes.contains(type) && client.database != null) { // cache the thing await client.database .storeSSSSCache(client.id, type, keyId, encrypted.ciphertext, secret); @@ -373,7 +373,7 @@ class SSSS { } // store the thing in your account data await client.setAccountData(client.userID, type, content); - if (CACHE_TYPES.contains(type) && client.database != null) { + if (cacheTypes.contains(type) && client.database != null) { // cache the thing await client.database.storeSSSSCache(client.id, type, keyId, content['encrypted'][keyId]['ciphertext'], secret); @@ -381,7 +381,7 @@ class SSSS { } Future maybeCacheAll(String keyId, Uint8List key) async { - for (final type in CACHE_TYPES) { + for (final type in cacheTypes) { final secret = await getCached(type); if (secret == null) { try { @@ -394,7 +394,7 @@ class SSSS { } Future maybeRequestAll([List devices]) async { - for (final type in CACHE_TYPES) { + for (final type in cacheTypes) { if (keyIdsFromType(type) != null) { final secret = await getCached(type); if (secret == null) { diff --git a/lib/encryption/utils/bootstrap.dart b/lib/encryption/utils/bootstrap.dart index 2fb84fb2..7244033f 100644 --- a/lib/encryption/utils/bootstrap.dart +++ b/lib/encryption/utils/bootstrap.dart @@ -568,7 +568,7 @@ class Bootstrap { }, ); Logs().v('Store the secret...'); - await newSsssKey.store(MEGOLM_KEY, base64.encode(privKey)); + await newSsssKey.store(megolmKey, base64.encode(privKey)); Logs().v( 'And finally set all megolm keys as needing to be uploaded again...'); await client.database?.markInboundGroupSessionsAsNeedingUpload(client.id); diff --git a/lib/encryption/utils/key_verification.dart b/lib/encryption/utils/key_verification.dart index 06d935eb..b563bf36 100644 --- a/lib/encryption/utils/key_verification.dart +++ b/lib/encryption/utils/key_verification.dart @@ -648,9 +648,9 @@ abstract class _KeyVerificationMethod { void dispose() {} } -const KNOWN_KEY_AGREEMENT_PROTOCOLS = ['curve25519-hkdf-sha256', 'curve25519']; -const KNOWN_HASHES = ['sha256']; -const KNOWN_MESSAGE_AUTHENTIFICATION_CODES = ['hkdf-hmac-sha256']; +const knownKeyAgreementProtocols = ['curve25519-hkdf-sha256', 'curve25519']; +const knownHashes = ['sha256']; +const knownHashesAuthentificationCodes = ['hkdf-hmac-sha256']; class _KeyVerificationMethodSas extends _KeyVerificationMethod { _KeyVerificationMethodSas({KeyVerification request}) @@ -768,9 +768,9 @@ class _KeyVerificationMethodSas extends _KeyVerificationMethod { Future sendStart() async { final payload = { 'method': type, - 'key_agreement_protocols': KNOWN_KEY_AGREEMENT_PROTOCOLS, - 'hashes': KNOWN_HASHES, - 'message_authentication_codes': KNOWN_MESSAGE_AUTHENTIFICATION_CODES, + 'key_agreement_protocols': knownKeyAgreementProtocols, + 'hashes': knownHashes, + 'message_authentication_codes': knownHashesAuthentificationCodes, 'short_authentication_string': knownAuthentificationTypes, }; request.makePayload(payload); @@ -785,18 +785,18 @@ class _KeyVerificationMethodSas extends _KeyVerificationMethod { return false; } final possibleKeyAgreementProtocols = _intersect( - KNOWN_KEY_AGREEMENT_PROTOCOLS, payload['key_agreement_protocols']); + knownKeyAgreementProtocols, payload['key_agreement_protocols']); if (possibleKeyAgreementProtocols.isEmpty) { return false; } keyAgreementProtocol = possibleKeyAgreementProtocols.first; - final possibleHashes = _intersect(KNOWN_HASHES, payload['hashes']); + final possibleHashes = _intersect(knownHashes, payload['hashes']); if (possibleHashes.isEmpty) { return false; } hash = possibleHashes.first; final possibleMessageAuthenticationCodes = _intersect( - KNOWN_MESSAGE_AUTHENTIFICATION_CODES, + knownHashesAuthentificationCodes, payload['message_authentication_codes']); if (possibleMessageAuthenticationCodes.isEmpty) { return false; @@ -826,16 +826,16 @@ class _KeyVerificationMethodSas extends _KeyVerificationMethod { } bool _handleAccept(Map payload) { - if (!KNOWN_KEY_AGREEMENT_PROTOCOLS + if (!knownKeyAgreementProtocols .contains(payload['key_agreement_protocol'])) { return false; } keyAgreementProtocol = payload['key_agreement_protocol']; - if (!KNOWN_HASHES.contains(payload['hash'])) { + if (!knownHashes.contains(payload['hash'])) { return false; } hash = payload['hash']; - if (!KNOWN_MESSAGE_AUTHENTIFICATION_CODES + if (!knownHashesAuthentificationCodes .contains(payload['message_authentication_code'])) { return false; } diff --git a/lib/src/database/database.dart b/lib/src/database/database.dart index 0d01d0b7..be00a479 100644 --- a/lib/src/database/database.dart +++ b/lib/src/database/database.dart @@ -307,7 +307,7 @@ class Database extends _$Database { // we limit to only fetching 500 rooms at once. // This value might be fine-tune-able to be larger (and thus increase performance more for very large accounts), // however this very conservative value should be on the safe side. - const MAX_ROOMS_PER_QUERY = 500; + const maxRoomsPerQuery = 500; // as we iterate over our entries in separate chunks one-by-one we use an iterator // which persists accross the chunks, and thus we just re-sume iteration at the place // we prreviously left off. @@ -315,7 +315,7 @@ class Database extends _$Database { // now we iterate over all our 500-room-chunks... for (var i = 0; i < allMembersToPostload.keys.length; - i += MAX_ROOMS_PER_QUERY) { + i += maxRoomsPerQuery) { // query the current chunk and build the query final membersRes = await (select(roomStates) ..where((s) { @@ -330,7 +330,7 @@ class Database extends _$Database { // we must check for if our chunk is done *before* progressing the // iterator, else we might progress it twice around chunk edges, missing on rooms for (var j = 0; - j < MAX_ROOMS_PER_QUERY && entriesIterator.moveNext(); + j < maxRoomsPerQuery && entriesIterator.moveNext(); j++) { final entry = entriesIterator.current; // builds room_id = '!roomId1' AND state_key IN ('@member') @@ -468,8 +468,8 @@ class Database extends _$Database { // calculate the status var status = 2; if (eventContent['unsigned'] is Map && - eventContent['unsigned'][MessageSendingStatusKey] is num) { - status = eventContent['unsigned'][MessageSendingStatusKey]; + eventContent['unsigned'][messageSendingStatusKey] is num) { + status = eventContent['unsigned'][messageSendingStatusKey]; } if (eventContent['status'] is num) status = eventContent['status']; var storeNewEvent = !((status == 1 || status == -1) && diff --git a/lib/src/event.dart b/lib/src/event.dart index 39e834a9..3ea5d49a 100644 --- a/lib/src/event.dart +++ b/lib/src/event.dart @@ -31,9 +31,9 @@ import 'utils/run_in_background.dart'; import 'utils/event_localizations.dart'; abstract class RelationshipTypes { - static const String Reply = 'm.in_reply_to'; - static const String Edit = 'm.replace'; - static const String Reaction = 'm.annotation'; + static const String reply = 'm.in_reply_to'; + static const String edit = 'm.replace'; + static const String reaction = 'm.annotation'; } /// All data exchanged over Matrix is expressed as an "event". Typically each client action (e.g. sending a message) correlates with exactly one event. @@ -61,12 +61,12 @@ class Event extends MatrixEvent { int status; static const int defaultStatus = 2; - static const Map STATUS_TYPE = { - 'ERROR': -1, - 'SENDING': 0, - 'SENT': 1, - 'TIMELINE': 2, - 'ROOM_STATE': 3, + static const Map statusType = { + 'error': -1, + 'sending': 0, + 'sent': 1, + 'timeline': 2, + 'roomState': 3, }; /// Optional. The event that redacted this event, if any. Otherwise null. @@ -160,7 +160,7 @@ class Event extends MatrixEvent { final prevContent = Event.getMapFromPayload(jsonPayload['prev_content']); return Event( status: jsonPayload['status'] ?? - unsigned[MessageSendingStatusKey] ?? + unsigned[messageSendingStatusKey] ?? defaultStatus, stateKey: jsonPayload['state_key'], prevContent: prevContent, @@ -352,7 +352,7 @@ class Event extends MatrixEvent { /// Searches for the reply event in the given timeline. Future getReplyEvent(Timeline timeline) async { - if (relationshipType != RelationshipTypes.Reply) return null; + if (relationshipType != RelationshipTypes.reply) return null; return await timeline.getEventById(relationshipEventId); } @@ -630,7 +630,7 @@ class Event extends MatrixEvent { return null; } if (content['m.relates_to'].containsKey('m.in_reply_to')) { - return RelationshipTypes.Reply; + return RelationshipTypes.reply; } return content .tryGet>('m.relates_to') @@ -671,9 +671,9 @@ class Event extends MatrixEvent { if (redacted) { return this; } - if (hasAggregatedEvents(timeline, RelationshipTypes.Edit)) { + if (hasAggregatedEvents(timeline, RelationshipTypes.edit)) { // alright, we have an edit - final allEditEvents = aggregatedEvents(timeline, RelationshipTypes.Edit) + final allEditEvents = aggregatedEvents(timeline, RelationshipTypes.edit) // we only allow edits made by the original author themself .where((e) => e.senderId == senderId && e.type == EventTypes.Message) .toList(); diff --git a/lib/src/room.dart b/lib/src/room.dart index a4d45696..ed9bc9e6 100644 --- a/lib/src/room.dart +++ b/lib/src/room.dart @@ -34,12 +34,24 @@ import 'utils/marked_unread.dart'; import 'utils/matrix_file.dart'; import 'utils/matrix_localizations.dart'; -enum PushRuleState { notify, mentions_only, dont_notify } +enum PushRuleState { notify, mentionsOnly, dontNotify } enum JoinRules { public, knock, invite, private } -enum GuestAccess { can_join, forbidden } -enum HistoryVisibility { invited, joined, shared, world_readable } +enum GuestAccess { canJoin, forbidden } +enum HistoryVisibility { invited, joined, shared, worldReadable } -const String MessageSendingStatusKey = +const Map _guestAccessMap = { + GuestAccess.canJoin: 'can_join', + GuestAccess.forbidden: 'forbidden', +}; + +const Map _historyVisibilityMap = { + HistoryVisibility.invited: 'invited', + HistoryVisibility.joined: 'joined', + HistoryVisibility.shared: 'shared', + HistoryVisibility.worldReadable: 'world_readable', +}; + +const String messageSendingStatusKey = 'com.famedly.famedlysdk.message_sending_status'; /// Represents a Matrix room. @@ -345,7 +357,7 @@ class Room { /// The default count of how much events should be requested when requesting the /// history of this room. - static const int DefaultHistoryCount = 100; + static const int defaultHistoryCount = 100; /// Calculates the displayname. First checks if there is a name, then checks for a canonical alias and /// then generates a name from the heroes. @@ -448,7 +460,7 @@ class Room { bool get markedUnread { return MarkedUnread.fromJson( - roomAccountData[EventType.MarkedUnread]?.content ?? {}) + roomAccountData[EventType.markedUnread]?.content ?? {}) .unread; } @@ -467,12 +479,12 @@ class Room { BasicRoomEvent() ..content = content ..roomId = id - ..type = EventType.MarkedUnread + ..type = EventType.markedUnread ]))))); await client.setRoomAccountData( client.userID, id, - EventType.MarkedUnread, + EventType.markedUnread, content, ); if (unread == false && lastEvent != null) { @@ -618,7 +630,7 @@ class Room { Future sendReaction(String eventId, String key, {String txid}) { return sendEvent({ 'm.relates_to': { - 'rel_type': RelationshipTypes.Reaction, + 'rel_type': RelationshipTypes.reaction, 'event_id': eventId, 'key': key, }, @@ -805,7 +817,7 @@ class Room { content['m.new_content'] = newContent; content['m.relates_to'] = { 'event_id': editEventId, - 'rel_type': RelationshipTypes.Edit, + 'rel_type': RelationshipTypes.edit, }; if (content['body'] is String) { content['body'] = '* ' + content['body']; @@ -827,7 +839,7 @@ class Room { ..senderId = client.userID ..originServerTs = sentDate ..unsigned = { - MessageSendingStatusKey: 0, + messageSendingStatusKey: 0, 'transaction_id': messageID, }, ])))); @@ -853,14 +865,14 @@ class Room { } else { Logs().w('[Client] Problem while sending message', e, s); syncUpdate.rooms.join.values.first.timeline.events.first - .unsigned[MessageSendingStatusKey] = -1; + .unsigned[messageSendingStatusKey] = -1; await _handleFakeSync(syncUpdate); return null; } } } syncUpdate.rooms.join.values.first.timeline.events.first - .unsigned[MessageSendingStatusKey] = 1; + .unsigned[messageSendingStatusKey] = 1; syncUpdate.rooms.join.values.first.timeline.events.first.eventId = res; await _handleFakeSync(syncUpdate); @@ -952,7 +964,7 @@ class Room { /// be received maximum. When the request is answered, [onHistoryReceived] will be triggered **before** /// the historical events will be published in the onEvent stream. Future requestHistory( - {int historyCount = DefaultHistoryCount, onHistoryReceived}) async { + {int historyCount = defaultHistoryCount, onHistoryReceived}) async { final resp = await client.requestMessages( id, prev_batch, @@ -1458,7 +1470,7 @@ class Room { if (globalPushRules['override'][i]['actions'] .indexOf('dont_notify') != -1) { - return PushRuleState.dont_notify; + return PushRuleState.dontNotify; } break; } @@ -1470,7 +1482,7 @@ class Room { if (globalPushRules['room'][i]['rule_id'] == id) { if (globalPushRules['room'][i]['actions'].indexOf('dont_notify') != -1) { - return PushRuleState.mentions_only; + return PushRuleState.mentionsOnly; } break; } @@ -1488,15 +1500,15 @@ class Room { switch (newState) { // All push notifications should be sent to the user case PushRuleState.notify: - if (pushRuleState == PushRuleState.dont_notify) { + if (pushRuleState == PushRuleState.dontNotify) { await client.deletePushRule('global', PushRuleKind.override, id); - } else if (pushRuleState == PushRuleState.mentions_only) { + } else if (pushRuleState == PushRuleState.mentionsOnly) { await client.deletePushRule('global', PushRuleKind.room, id); } break; // Only when someone mentions the user, a push notification should be sent - case PushRuleState.mentions_only: - if (pushRuleState == PushRuleState.dont_notify) { + case PushRuleState.mentionsOnly: + if (pushRuleState == PushRuleState.dontNotify) { await client.deletePushRule('global', PushRuleKind.override, id); await client.setPushRule( 'global', @@ -1514,8 +1526,8 @@ class Room { } break; // No push notification should be ever sent for this room. - case PushRuleState.dont_notify: - if (pushRuleState == PushRuleState.mentions_only) { + case PushRuleState.dontNotify: + if (pushRuleState == PushRuleState.mentionsOnly) { await client.deletePushRule('global', PushRuleKind.room, id); } await client.setPushRule( @@ -1702,11 +1714,8 @@ class Room { /// This event controls whether guest users are allowed to join rooms. If this event /// is absent, servers should act as if it is present and has the guest_access value "forbidden". GuestAccess get guestAccess => getState(EventTypes.GuestAccess) != null - ? GuestAccess.values.firstWhere( - (r) => - r.toString().replaceAll('GuestAccess.', '') == - getState(EventTypes.GuestAccess).content['guest_access'], - orElse: () => GuestAccess.forbidden) + ? _guestAccessMap.map((k, v) => MapEntry(v, k))[ + getState(EventTypes.GuestAccess).content['guest_access']] : GuestAccess.forbidden; /// Changes the guest access. You should check first if the user is able to change it. @@ -1715,7 +1724,7 @@ class Room { id, EventTypes.GuestAccess, { - 'guest_access': guestAccess.toString().replaceAll('GuestAccess.', ''), + 'guest_access': _guestAccessMap[guestAccess], }, ); return; @@ -1727,12 +1736,9 @@ class Room { /// This event controls whether a user can see the events that happened in a room from before they joined. HistoryVisibility get historyVisibility => getState(EventTypes.HistoryVisibility) != null - ? HistoryVisibility.values.firstWhere( - (r) => - r.toString().replaceAll('HistoryVisibility.', '') == - getState(EventTypes.HistoryVisibility) - .content['history_visibility'], - orElse: () => null) + ? _historyVisibilityMap.map((k, v) => MapEntry(v, k))[ + getState(EventTypes.HistoryVisibility) + .content['history_visibility']] : null; /// Changes the history visibility. You should check first if the user is able to change it. @@ -1741,8 +1747,7 @@ class Room { id, EventTypes.HistoryVisibility, { - 'history_visibility': - historyVisibility.toString().replaceAll('HistoryVisibility.', ''), + 'history_visibility': _historyVisibilityMap[historyVisibility], }, ); return; diff --git a/lib/src/timeline.dart b/lib/src/timeline.dart index 7f568cbb..34859841 100644 --- a/lib/src/timeline.dart +++ b/lib/src/timeline.dart @@ -74,7 +74,7 @@ class Timeline { } Future requestHistory( - {int historyCount = Room.DefaultHistoryCount}) async { + {int historyCount = Room.defaultHistoryCount}) async { if (!isRequestingHistory) { isRequestingHistory = true; await room.requestHistory( @@ -264,7 +264,7 @@ class Timeline { } var status = eventUpdate.content['status'] ?? (eventUpdate.content['unsigned'] is Map - ? eventUpdate.content['unsigned'][MessageSendingStatusKey] + ? eventUpdate.content['unsigned'][messageSendingStatusKey] : null) ?? 2; // Redaction events are handled as modification for existing events. diff --git a/lib/src/utils/marked_unread.dart b/lib/src/utils/marked_unread.dart index 6dc3e327..b065335b 100644 --- a/lib/src/utils/marked_unread.dart +++ b/lib/src/utils/marked_unread.dart @@ -17,7 +17,7 @@ */ mixin EventType { - static const String MarkedUnread = 'com.famedly.marked_unread'; + static const String markedUnread = 'com.famedly.marked_unread'; } class MarkedUnread { diff --git a/lib/src/utils/matrix_id_string_extension.dart b/lib/src/utils/matrix_id_string_extension.dart index 1c0db5f1..ede1bdf1 100644 --- a/lib/src/utils/matrix_id_string_extension.dart +++ b/lib/src/utils/matrix_id_string_extension.dart @@ -16,9 +16,9 @@ * along with this program. If not, see . */ -const Set VALID_SIGILS = {'@', '!', '#', '\$', '+'}; +const Set validSigils = {'@', '!', '#', '\$', '+'}; -const int MAX_LENGTH = 255; +const int maxLength = 255; extension MatrixIdExtension on String { List _getParts() { @@ -32,8 +32,8 @@ extension MatrixIdExtension on String { bool get isValidMatrixId { if (isEmpty ?? true) return false; - if (length > MAX_LENGTH) return false; - if (!VALID_SIGILS.contains(substring(0, 1))) { + if (length > maxLength) return false; + if (!validSigils.contains(substring(0, 1))) { return false; } // event IDs do not have to have a domain diff --git a/lib/src/utils/matrix_localizations.dart b/lib/src/utils/matrix_localizations.dart index f9545465..4c96db66 100644 --- a/lib/src/utils/matrix_localizations.dart +++ b/lib/src/utils/matrix_localizations.dart @@ -146,7 +146,7 @@ extension HistoryVisibilityDisplayString on HistoryVisibility { return i18n.fromJoining; case HistoryVisibility.shared: return i18n.visibleForAllParticipants; - case HistoryVisibility.world_readable: + case HistoryVisibility.worldReadable: return i18n.visibleForEveryone; } return null; @@ -156,7 +156,7 @@ extension HistoryVisibilityDisplayString on HistoryVisibility { extension GuestAccessDisplayString on GuestAccess { String getLocalizedString(MatrixLocalizations i18n) { switch (this) { - case GuestAccess.can_join: + case GuestAccess.canJoin: return i18n.guestsCanJoin; case GuestAccess.forbidden: return i18n.guestsAreForbidden; diff --git a/test/device_keys_list_test.dart b/test/device_keys_list_test.dart index 6d1cb52f..ea3446c3 100644 --- a/test/device_keys_list_test.dart +++ b/test/device_keys_list_test.dart @@ -157,7 +157,7 @@ void main() { masterKey.setDirectVerified(true); // we need to populate the ssss cache to be able to test signing easily final handle = client.encryption.ssss.open(); - await handle.unlock(recoveryKey: SSSS_KEY); + await handle.unlock(recoveryKey: ssssKey); await handle.maybeCacheAll(); expect(key.verified, true); diff --git a/test/encryption/cross_signing_test.dart b/test/encryption/cross_signing_test.dart index 3c1cc5a0..0d8ffc18 100644 --- a/test/encryption/cross_signing_test.dart +++ b/test/encryption/cross_signing_test.dart @@ -57,7 +57,7 @@ void main() { final key = client.userDeviceKeys[client.userID].masterKey; key.setDirectVerified(false); FakeMatrixApi.calledEndpoints.clear(); - await client.encryption.crossSigning.selfSign(recoveryKey: SSSS_KEY); + await client.encryption.crossSigning.selfSign(recoveryKey: ssssKey); expect(key.directVerified, true); expect( FakeMatrixApi.calledEndpoints diff --git a/test/encryption/key_verification_test.dart b/test/encryption/key_verification_test.dart index 33d5a982..e5223aee 100644 --- a/test/encryption/key_verification_test.dart +++ b/test/encryption/key_verification_test.dart @@ -35,7 +35,7 @@ class MockSSSS extends SSSS { Future maybeRequestAll([List devices]) async { requestedSecrets = true; final handle = open(); - await handle.unlock(recoveryKey: SSSS_KEY); + await handle.unlock(recoveryKey: ssssKey); await handle.maybeCacheAll(); } } @@ -215,7 +215,7 @@ void main() { final req1 = await client1.userDeviceKeys[client2.userID].startVerification(); expect(req1.state, KeyVerificationState.askSSSS); - await req1.openSSSS(recoveryKey: SSSS_KEY); + await req1.openSSSS(recoveryKey: ssssKey); await Future.delayed(Duration(milliseconds: 10)); expect(req1.state, KeyVerificationState.waitingAccept); @@ -311,7 +311,7 @@ void main() { expect(req1.state, KeyVerificationState.askSSSS); expect(req2.state, KeyVerificationState.done); - await req1.openSSSS(recoveryKey: SSSS_KEY); + await req1.openSSSS(recoveryKey: ssssKey); await Future.delayed(Duration(milliseconds: 10)); expect(req1.state, KeyVerificationState.done); diff --git a/test/encryption/online_key_backup_test.dart b/test/encryption/online_key_backup_test.dart index 0f427a29..97eb4e98 100644 --- a/test/encryption/online_key_backup_test.dart +++ b/test/encryption/online_key_backup_test.dart @@ -56,7 +56,7 @@ void main() { expect(client.encryption.keyManager.enabled, true); expect(await client.encryption.keyManager.isCached(), false); final handle = client.encryption.ssss.open(); - await handle.unlock(recoveryKey: SSSS_KEY); + await handle.unlock(recoveryKey: ssssKey); await handle.maybeCacheAll(); expect(await client.encryption.keyManager.isCached(), true); }); diff --git a/test/encryption/ssss_test.dart b/test/encryption/ssss_test.dart index 4d278ac0..ee7ea000 100644 --- a/test/encryption/ssss_test.dart +++ b/test/encryption/ssss_test.dart @@ -37,7 +37,7 @@ class MockSSSS extends SSSS { Future maybeRequestAll([List devices]) async { requestedSecrets = true; final handle = open(); - await handle.unlock(recoveryKey: SSSS_KEY); + await handle.unlock(recoveryKey: ssssKey); await handle.maybeCacheAll(); } } @@ -97,8 +97,8 @@ void main() { } expect(failed, true); expect(handle.isUnlocked, false); - await handle.unlock(passphrase: SSSS_PASSPHRASE); - await handle.unlock(recoveryKey: SSSS_KEY); + await handle.unlock(passphrase: ssssPassphrase); + await handle.unlock(recoveryKey: ssssKey); expect(handle.isUnlocked, true); FakeMatrixApi.calledEndpoints.clear(); await handle.store('best animal', 'foxies'); @@ -123,8 +123,8 @@ void main() { expect(key, decoded); final handle = client.encryption.ssss.open(); - await handle.unlock(recoveryKey: SSSS_KEY); - expect(handle.recoveryKey, SSSS_KEY); + await handle.unlock(recoveryKey: ssssKey); + expect(handle.recoveryKey, ssssKey); }); test('cache', () async { @@ -132,7 +132,7 @@ void main() { await client.encryption.ssss.clearCache(); final handle = client.encryption.ssss.open(EventTypes.CrossSigningSelfSigning); - await handle.unlock(recoveryKey: SSSS_KEY, postUnlock: false); + await handle.unlock(recoveryKey: ssssKey, postUnlock: false); expect( (await client.encryption.ssss .getCached(EventTypes.CrossSigningSelfSigning)) != @@ -167,7 +167,7 @@ void main() { client.userDeviceKeys[client.userID].masterKey.setDirectVerified(false); final handle = client.encryption.ssss.open(EventTypes.CrossSigningSelfSigning); - await handle.unlock(recoveryKey: SSSS_KEY); + await handle.unlock(recoveryKey: ssssKey); expect( (await client.encryption.ssss .getCached(EventTypes.CrossSigningSelfSigning)) != @@ -305,7 +305,7 @@ void main() { key.setDirectVerified(true); final handle = client.encryption.ssss.open(EventTypes.CrossSigningSelfSigning); - await handle.unlock(recoveryKey: SSSS_KEY); + await handle.unlock(recoveryKey: ssssKey); await client.encryption.ssss.clearCache(); client.encryption.ssss.pendingShareRequests.clear(); diff --git a/test/event_test.dart b/test/event_test.dart index 2f966ab6..c39b1c0b 100644 --- a/test/event_test.dart +++ b/test/event_test.dart @@ -84,7 +84,7 @@ void main() { expect(event.formattedText, formatted_body); expect(event.body, body); expect(event.type, EventTypes.Message); - expect(event.relationshipType, RelationshipTypes.Reply); + expect(event.relationshipType, RelationshipTypes.reply); jsonObj['state_key'] = ''; var state = Event.fromJson(jsonObj, null); expect(state.eventId, id); @@ -183,7 +183,7 @@ void main() { }; event = Event.fromJson(jsonObj, null); expect(event.messageType, MessageTypes.Text); - expect(event.relationshipType, RelationshipTypes.Reply); + expect(event.relationshipType, RelationshipTypes.reply); expect(event.relationshipEventId, '1234'); }); @@ -203,12 +203,12 @@ void main() { 'event_id': 'abc', }; event = Event.fromJson(jsonObj, null); - expect(event.relationshipType, RelationshipTypes.Edit); + expect(event.relationshipType, RelationshipTypes.edit); expect(event.relationshipEventId, 'abc'); jsonObj['content']['m.relates_to']['rel_type'] = 'm.annotation'; event = Event.fromJson(jsonObj, null); - expect(event.relationshipType, RelationshipTypes.Reaction); + expect(event.relationshipType, RelationshipTypes.reaction); expect(event.relationshipEventId, 'abc'); jsonObj['content']['m.relates_to'] = { @@ -217,7 +217,7 @@ void main() { }, }; event = Event.fromJson(jsonObj, null); - expect(event.relationshipType, RelationshipTypes.Reply); + expect(event.relationshipType, RelationshipTypes.reply); expect(event.relationshipEventId, 'def'); }); @@ -908,7 +908,7 @@ void main() { 'msgtype': 'm.text', 'm.relates_to': { 'event_id': '\$source', - 'rel_type': RelationshipTypes.Edit, + 'rel_type': RelationshipTypes.edit, }, }, 'event_id': '\$edit1', @@ -919,30 +919,30 @@ void main() { 'msgtype': 'm.text', 'm.relates_to': { 'event_id': '\$source', - 'rel_type': RelationshipTypes.Edit, + 'rel_type': RelationshipTypes.edit, }, }, 'event_id': '\$edit2', }, null); var room = Room(client: client); var timeline = Timeline(events: [event, edit1, edit2], room: room); - expect(event.hasAggregatedEvents(timeline, RelationshipTypes.Edit), true); - expect(event.aggregatedEvents(timeline, RelationshipTypes.Edit), + expect(event.hasAggregatedEvents(timeline, RelationshipTypes.edit), true); + expect(event.aggregatedEvents(timeline, RelationshipTypes.edit), {edit1, edit2}); - expect(event.aggregatedEvents(timeline, RelationshipTypes.Reaction), + expect(event.aggregatedEvents(timeline, RelationshipTypes.reaction), {}); - expect(event.hasAggregatedEvents(timeline, RelationshipTypes.Reaction), + expect(event.hasAggregatedEvents(timeline, RelationshipTypes.reaction), false); timeline.removeAggregatedEvent(edit2); - expect(event.aggregatedEvents(timeline, RelationshipTypes.Edit), {edit1}); + expect(event.aggregatedEvents(timeline, RelationshipTypes.edit), {edit1}); timeline.addAggregatedEvent(edit2); - expect(event.aggregatedEvents(timeline, RelationshipTypes.Edit), + expect(event.aggregatedEvents(timeline, RelationshipTypes.edit), {edit1, edit2}); timeline.removeAggregatedEvent(event); expect( - event.aggregatedEvents(timeline, RelationshipTypes.Edit), {}); + event.aggregatedEvents(timeline, RelationshipTypes.edit), {}); }); test('getDisplayEvent', () { var event = Event.fromJson({ @@ -966,7 +966,7 @@ void main() { }, 'm.relates_to': { 'event_id': '\$source', - 'rel_type': RelationshipTypes.Edit, + 'rel_type': RelationshipTypes.edit, }, }, 'event_id': '\$edit1', @@ -984,7 +984,7 @@ void main() { }, 'm.relates_to': { 'event_id': '\$source', - 'rel_type': RelationshipTypes.Edit, + 'rel_type': RelationshipTypes.edit, }, }, 'event_id': '\$edit2', @@ -1002,7 +1002,7 @@ void main() { }, 'm.relates_to': { 'event_id': '\$source', - 'rel_type': RelationshipTypes.Edit, + 'rel_type': RelationshipTypes.edit, }, }, 'event_id': '\$edit3', diff --git a/test/fake_client.dart b/test/fake_client.dart index 8431d47a..9aadb898 100644 --- a/test/fake_client.dart +++ b/test/fake_client.dart @@ -21,8 +21,8 @@ import 'package:famedlysdk/famedlysdk.dart'; import 'fake_matrix_api.dart'; import 'fake_database.dart'; -const SSSS_PASSPHRASE = 'nae7ahDiequ7ohniufah3ieS2je1thohX4xeeka7aixohsho9O'; -const SSSS_KEY = 'EsT9 RzbW VhPW yqNp cC7j ViiW 5TZB LuY4 ryyv 9guN Ysmr WDPH'; +const ssssPassphrase = 'nae7ahDiequ7ohniufah3ieS2je1thohX4xeeka7aixohsho9O'; +const ssssKey = 'EsT9 RzbW VhPW yqNp cC7j ViiW 5TZB LuY4 ryyv 9guN Ysmr WDPH'; // key @test:fakeServer.notExisting const pickledOlmAccount = diff --git a/test/matrix_localizations_test.dart b/test/matrix_localizations_test.dart index 768a3052..fd80113a 100644 --- a/test/matrix_localizations_test.dart +++ b/test/matrix_localizations_test.dart @@ -37,11 +37,11 @@ void main() { .getLocalizedString(MatrixDefaultLocalizations()), 'Visible for all participants'); expect( - HistoryVisibility.world_readable + HistoryVisibility.worldReadable .getLocalizedString(MatrixDefaultLocalizations()), 'Visible for everyone'); expect( - GuestAccess.can_join.getLocalizedString(MatrixDefaultLocalizations()), + GuestAccess.canJoin.getLocalizedString(MatrixDefaultLocalizations()), 'Guests can join'); expect( GuestAccess.forbidden diff --git a/test/room_test.dart b/test/room_test.dart index 7241039e..a6e3fe58 100644 --- a/test/room_test.dart +++ b/test/room_test.dart @@ -603,10 +603,10 @@ void main() { }); test('pushRuleState', () async { - expect(room.pushRuleState, PushRuleState.mentions_only); + expect(room.pushRuleState, PushRuleState.mentionsOnly); matrix.accountData['m.push_rules'].content['global']['override'] .add(matrix.accountData['m.push_rules'].content['global']['room'][0]); - expect(room.pushRuleState, PushRuleState.dont_notify); + expect(room.pushRuleState, PushRuleState.dontNotify); }); test('Test call methods', () async { @@ -642,8 +642,8 @@ void main() { test('setPushRuleState', () async { await room.setPushRuleState(PushRuleState.notify); - await room.setPushRuleState(PushRuleState.dont_notify); - await room.setPushRuleState(PushRuleState.mentions_only); + await room.setPushRuleState(PushRuleState.dontNotify); + await room.setPushRuleState(PushRuleState.mentionsOnly); await room.setPushRuleState(PushRuleState.notify); }); @@ -708,8 +708,8 @@ void main() { 'type': 'm.room.guest_access', 'unsigned': {'age': 1234} }, room, 1432735824653.0)); - expect(room.guestAccess, GuestAccess.can_join); - await room.setGuestAccess(GuestAccess.can_join); + expect(room.guestAccess, GuestAccess.canJoin); + await room.setGuestAccess(GuestAccess.canJoin); }); test('historyVisibility', () async { diff --git a/test/sync_filter_test.dart b/test/sync_filter_test.dart index 27ee6616..818240c1 100644 --- a/test/sync_filter_test.dart +++ b/test/sync_filter_test.dart @@ -20,7 +20,7 @@ import 'package:famedlysdk/famedlysdk.dart'; import 'package:logger/logger.dart'; import 'package:test/test.dart'; -const UPDATES = { +const updates = { 'empty': { 'next_batch': 'blah', 'account_data': { @@ -128,7 +128,7 @@ const UPDATES = { }; void testUpdates(bool Function(SyncUpdate s) test, Map expected) { - for (final update in UPDATES.entries) { + for (final update in updates.entries) { var sync = SyncUpdate.fromJson(update.value); expect(test(sync), expected[update.key]); } diff --git a/test/timeline_test.dart b/test/timeline_test.dart index aba7a1f2..4b569844 100644 --- a/test/timeline_test.dart +++ b/test/timeline_test.dart @@ -432,7 +432,7 @@ void main() { 'event_id': 'transaction', 'origin_server_ts': testTimeStamp, 'unsigned': { - MessageSendingStatusKey: 0, + messageSendingStatusKey: 0, 'transaction_id': 'transaction', }, }, @@ -451,7 +451,7 @@ void main() { 'origin_server_ts': testTimeStamp, 'unsigned': { 'transaction_id': 'transaction', - MessageSendingStatusKey: 2, + messageSendingStatusKey: 2, }, }, sortOrder: room.newSortOrder)); @@ -469,7 +469,7 @@ void main() { 'origin_server_ts': testTimeStamp, 'unsigned': { 'transaction_id': 'transaction', - MessageSendingStatusKey: 1, + messageSendingStatusKey: 1, }, }, sortOrder: room.newSortOrder));