diff --git a/lib/src/client.dart b/lib/src/client.dart index 0f337b7..ff8cbaf 100644 --- a/lib/src/client.dart +++ b/lib/src/client.dart @@ -1519,6 +1519,52 @@ sort order of ${prevState.sortOrder}. This should never happen...'''); } } + /// Clear all local cached messages and perform a new clean sync. + Future clearLocalCachedMessages() async { + prevBatch = null; + rooms.forEach((r) => r.prev_batch = null); + await database?.clearCache(id); + } + + /// A list of mxids of users who are ignored. + List get ignoredUsers => + accountData.containsKey('m.ignored_user_list') + ? accountData['m.ignored_user_list'].content['ignored_users'] + : []; + + /// Ignore another user. This will clear the local cached messages to + /// hide all previous messages from this user. + Future ignoreUser(String userId) async { + if (!userId.isValidMatrixId) { + throw Exception('$userId is not a valid mxid!'); + } + await setAccountData( + userID, + 'm.ignored_user_list', + {'ignored_users': ignoredUsers..add(userId)}, + ); + await clearLocalCachedMessages(); + return; + } + + /// Unignore a user. This will clear the local cached messages and request + /// them again from the server to avoid gaps in the timeline. + Future unignoreUser(String userId) async { + if (!userId.isValidMatrixId) { + throw Exception('$userId is not a valid mxid!'); + } + if (!ignoredUsers.contains(userId)) { + throw Exception('$userId is not in the ignore list!'); + } + await setAccountData( + userID, + 'm.ignored_user_list', + {'ignored_users': ignoredUsers..remove(userId)}, + ); + await clearLocalCachedMessages(); + return; + } + bool _disposed = false; Future _currentTransaction = Future.sync(() => {}); diff --git a/test/client_test.dart b/test/client_test.dart index 2fa4178..ce67a9d 100644 --- a/test/client_test.dart +++ b/test/client_test.dart @@ -421,6 +421,18 @@ void main() { test('changePassword', () async { await matrix.changePassword('1234', oldPassword: '123456'); }); + test('ignoredUsers', () async { + expect(matrix.ignoredUsers, []); + matrix.accountData['m.ignored_user_list'] = + BasicEvent(type: 'm.ignored_user_list', content: { + 'ignored_users': ['@charley:stupid.abc'] + }); + expect(matrix.ignoredUsers, ['@charley:stupid.abc']); + }); + test('ignoredUsers', () async { + await matrix.ignoreUser('@charley2:stupid.abc'); + await matrix.unignoreUser('@charley:stupid.abc'); + }); test('dispose', () async { await matrix.dispose(closeDatabase: true); diff --git a/test/fake_matrix_api.dart b/test/fake_matrix_api.dart index 2400de2..156d86a 100644 --- a/test/fake_matrix_api.dart +++ b/test/fake_matrix_api.dart @@ -1982,6 +1982,8 @@ class FakeMatrixApi extends MockClient { '/client/unstable/room_keys/version': (var reqI) => {'version': '5'}, }, 'PUT': { + '/client/r0/user/%40test%3AfakeServer.notExisting/account_data/m.ignored_user_list': + (var req) => {}, '/client/r0/presence/${Uri.encodeComponent('@alice:example.com')}/status': (var req) => {}, '/client/r0/pushrules/global/content/nocake/enabled': (var req) => {},