diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 5285d35..99a3cd2 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -11,16 +11,19 @@ coverage: dependencies: [] script: - apt update - - apt install -y curl gnupg2 git - - curl https://dl-ssl.google.com/linux/linux_signing_key.pub | apt-key add - - - curl https://storage.googleapis.com/download.dartlang.org/linux/debian/dart_stable.list > /etc/apt/sources.list.d/dart_stable.list + - apt install -y curl gnupg2 git unzip + - git clone https://github.com/flutter/flutter.git -b stable + - export PATH="$PATH:`pwd`/flutter/bin" + - sed -i s/#//g pubspec.yaml + - flutter doctor - apt update - - apt install -y dart chromium lcov libolm3 sqlite3 libsqlite3-dev + - apt install -y chromium lcov libolm3 sqlite3 libsqlite3-dev - ln -s /usr/lib/dart/bin/pub /usr/bin/ - useradd -m test - chown -R 'test:' '.' - chmod +x ./prepare.sh - chmod +x ./test.sh + - rm -r example - su -c ./prepare.sh test - su -c ./test.sh test artifacts: @@ -125,4 +128,4 @@ pages: paths: - public only: - - main \ No newline at end of file + - main diff --git a/lib/src/client.dart b/lib/src/client.dart index d6794b4..4abcd23 100644 --- a/lib/src/client.dart +++ b/lib/src/client.dart @@ -762,6 +762,8 @@ class Client extends MatrixApi { _sortRooms(); } prevBatch = syncResp.nextBatch; + await database?.deleteOldFiles( + DateTime.now().subtract(Duration(days: 30)).millisecondsSinceEpoch); await _updateUserDeviceKeys(); if (encryptionEnabled) { encryption.onSync(); diff --git a/lib/src/database/database.dart b/lib/src/database/database.dart index a61dfa3..9de2ff3 100644 --- a/lib/src/database/database.dart +++ b/lib/src/database/database.dart @@ -164,8 +164,8 @@ class Database extends _$Database { Future getClient(String name) async { final res = await dbGetClient(name).get(); if (res.isEmpty) return null; - await markPendingEventsAsError(res.first.clientId); - return res.first; + await markPendingEventsAsError(res.single.clientId); + return res.single; } Future> getUserDeviceKeys( @@ -216,7 +216,7 @@ class Database extends _$Database { if (res.isEmpty) { return null; } - return res.first; + return res.single; } Future> getDbInboundGroupSessions( @@ -231,7 +231,7 @@ class Database extends _$Database { if (res.isEmpty) { return null; } - return res.first; + return res.single; } Future getSSSSCache(int clientId, String type) async { @@ -239,7 +239,7 @@ class Database extends _$Database { if (res.isEmpty) { return null; } - return res.first; + return res.single; } Future> getRoomList(sdk.Client client, @@ -563,7 +563,7 @@ class Database extends _$Database { if (event.isEmpty) { return null; } - return sdk.Event.fromDb(event.first, room); + return sdk.Event.fromDb(event.single, room); } Future redactMessage(int clientId, sdk.EventUpdate eventUpdate) async { @@ -649,7 +649,7 @@ class Database extends _$Database { if (res.isEmpty) { return null; } - return sdk.Event.fromDb(res.first, room).asUser; + return sdk.Event.fromDb(res.single, room).asUser; } Future> getUsers(int clientId, sdk.Room room) async { @@ -673,6 +673,6 @@ class Database extends _$Database { Future getFile(String mxcUri) async { final res = await dbGetFile(mxcUri).get(); if (res.isEmpty) return null; - return res.first.bytes; + return res.single.bytes; } } diff --git a/lib/src/database/database.g.dart b/lib/src/database/database.g.dart index 984288a..52a2ac4 100644 --- a/lib/src/database/database.g.dart +++ b/lib/src/database/database.g.dart @@ -6892,6 +6892,15 @@ abstract class _$Database extends GeneratedDatabase { ); } + Future deleteOldFiles(int saved_at) { + return customUpdate( + 'DELETE FROM files WHERE saved_at < :saved_at', + variables: [Variable.withInt(saved_at)], + updates: {files}, + updateKind: UpdateKind.delete, + ); + } + @override Iterable get allTables => allSchemaEntities.whereType(); @override diff --git a/lib/src/database/database.moor b/lib/src/database/database.moor index 7624359..6399574 100644 --- a/lib/src/database/database.moor +++ b/lib/src/database/database.moor @@ -234,3 +234,4 @@ removeSuccessfulRoomEvents: DELETE FROM events WHERE client_id = :client_id AND storeFile: INSERT OR REPLACE INTO files (mxc_uri, bytes, saved_at) VALUES (:mxc_uri, :bytes, :time); dbGetFile: SELECT * FROM files WHERE mxc_uri = :mxc_uri; markPendingEventsAsError: UPDATE events SET status = -1 WHERE client_id = :client_id AND status = 0; +deleteOldFiles: DELETE FROM files WHERE saved_at < :saved_at; diff --git a/lib/src/event.dart b/lib/src/event.dart index af7bc54..6ec4df4 100644 --- a/lib/src/event.dart +++ b/lib/src/event.dart @@ -234,7 +234,7 @@ class Event extends MatrixEvent { String get messageType => type == EventTypes.Sticker ? MessageTypes.Sticker - : content['msgtype'] ?? MessageTypes.Text; + : (content['msgtype'] is String ? content['msgtype'] : MessageTypes.Text); void setRedactionEvent(Event redactedBecause) { unsigned = { @@ -281,10 +281,11 @@ class Event extends MatrixEvent { } /// Returns the body of this event if it has a body. - String get text => content['body'] ?? ''; + String get text => content['body'] is String ? content['body'] : ''; /// Returns the formatted boy of this event if it has a formatted body. - String get formattedText => content['formatted_body'] ?? ''; + String get formattedText => + content['formatted_body'] is String ? content['formatted_body'] : ''; /// Use this to get the body. String get body { @@ -800,13 +801,13 @@ class Event extends MatrixEvent { /// Returns if a given event only has emotes, emojis or whitespace as content. /// This is useful to determine if stand-alone emotes should be displayed bigger. bool get onlyEmotes => isRichMessage - ? _onlyEmojiEmoteRegex.hasMatch(content['formatted_body']) - : _onlyEmojiRegex.hasMatch(content['body'] ?? ''); + ? _onlyEmojiEmoteRegex.hasMatch(formattedText) + : _onlyEmojiRegex.hasMatch(text); /// Gets the number of emotes in a given message. This is useful to determine if /// emotes should be displayed bigger. WARNING: This does **not** test if there are /// only emotes. Use `event.onlyEmotes` for that! int get numberEmotes => isRichMessage - ? _countEmojiEmoteRegex.allMatches(content['formatted_body']).length - : _countEmojiRegex.allMatches(content['body'] ?? '').length; + ? _countEmojiEmoteRegex.allMatches(formattedText).length + : _countEmojiRegex.allMatches(text).length; } diff --git a/lib/src/room.dart b/lib/src/room.dart index aba01d2..80f34ba 100644 --- a/lib/src/room.dart +++ b/lib/src/room.dart @@ -174,7 +174,8 @@ class Room { StreamController.broadcast(); /// The name of the room if set by a participant. - String get name => states[EventTypes.RoomName] != null + String get name => states[EventTypes.RoomName] != null && + states[EventTypes.RoomName].content['name'] is String ? states[EventTypes.RoomName].content['name'] : ''; @@ -205,14 +206,15 @@ class Room { } /// The topic of the room if set by a participant. - String get topic => states[EventTypes.RoomTopic] != null + String get topic => states[EventTypes.RoomTopic] != null && + states[EventTypes.RoomTopic].content['topic'] is String ? states[EventTypes.RoomTopic].content['topic'] : ''; /// The avatar of the room if set by a participant. Uri get avatar { if (states[EventTypes.RoomAvatar] != null && - states[EventTypes.RoomAvatar].content['url'] != null) { + states[EventTypes.RoomAvatar].content['url'] is String) { return Uri.parse(states[EventTypes.RoomAvatar].content['url']); } if (mHeroes != null && mHeroes.length == 1 && states[mHeroes[0]] != null) { diff --git a/prepare.sh b/prepare.sh index e480bfb..a619b42 100644 --- a/prepare.sh +++ b/prepare.sh @@ -20,4 +20,8 @@ cmake --build . cd .. fi -pub get +if which flutter >/dev/null; then + flutter pub get +else + pub get +fi diff --git a/pubspec.yaml b/pubspec.yaml index 2c283e1..8ae05fd 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -30,8 +30,5 @@ dev_dependencies: moor_generator: ^3.0.0 build_runner: ^1.5.2 pedantic: ^1.9.0 - moor_ffi: ^0.5.0 - -dependency_overrides: - # See https://github.com/flutter/flutter/issues/62240 - analyzer: '0.39.14' + #flutter_test: + # sdk: flutter diff --git a/test.sh b/test.sh index f6300dd..0c3f63a 100644 --- a/test.sh +++ b/test.sh @@ -1,6 +1,6 @@ #!/bin/sh -e # pub run test -p vm -pub run test_coverage --print-test-output -pub global activate remove_from_coverage -pub global run remove_from_coverage:remove_from_coverage -f coverage/lcov.info -r '\.g\.dart$' +flutter test --coverage +flutter pub global activate remove_from_coverage +flutter pub global run remove_from_coverage:remove_from_coverage -f coverage/lcov.info -r '\.g\.dart$' genhtml -o coverage coverage/lcov.info || true diff --git a/test/fake_database.dart b/test/fake_database.dart index 7d85757..d270c80 100644 --- a/test/fake_database.dart +++ b/test/fake_database.dart @@ -18,7 +18,7 @@ import 'package:famedlysdk/famedlysdk.dart'; import 'package:moor/moor.dart'; -import 'package:moor_ffi/moor_ffi.dart' as moor; +import 'package:moor/ffi.dart' as moor; Database getDatabase() { moorRuntimeOptions.dontWarnAboutMultipleDatabases = true;