From 3338da4e0999ba0f0067f987bd462756fc21db4a Mon Sep 17 00:00:00 2001 From: Krille Fear Date: Mon, 22 Nov 2021 19:36:38 +0100 Subject: [PATCH] fix: Ability to remove avatar from room and account To remove an avatar the client needs to send an empty object. This is now possible by making the MatrixFile parameter nullable. --- lib/src/client.dart | 17 ++++++++++------- lib/src/room.dart | 13 ++++++++----- 2 files changed, 18 insertions(+), 12 deletions(-) diff --git a/lib/src/client.dart b/lib/src/client.dart index c9a10128..75b0e220 100644 --- a/lib/src/client.dart +++ b/lib/src/client.dart @@ -821,13 +821,16 @@ class Client extends MatrixApi { } } - /// Uploads a new user avatar for this user. - Future setAvatar(MatrixFile file) async { - final uploadResp = await uploadContent( - file.bytes, - filename: file.name, - contentType: file.mimeType, - ); + /// Uploads a new user avatar for this user. Leave file null to remove the + /// current avatar. + Future setAvatar(MatrixFile? file) async { + final uploadResp = file == null + ? null + : await uploadContent( + file.bytes, + filename: file.name, + contentType: file.mimeType, + ); await setAvatarUrl(userID!, uploadResp); return; } diff --git a/lib/src/room.dart b/lib/src/room.dart index b674bf81..20e8abc0 100644 --- a/lib/src/room.dart +++ b/lib/src/room.dart @@ -1356,15 +1356,18 @@ class Room { } /// Uploads a new user avatar for this room. Returns the event ID of the new - /// m.room.avatar event. - Future setAvatar(MatrixFile file) async { - final uploadResp = - await client.uploadContent(file.bytes, filename: file.name); + /// m.room.avatar event. Leave empty to remove the current avatar. + Future setAvatar(MatrixFile? file) async { + final uploadResp = file == null + ? null + : await client.uploadContent(file.bytes, filename: file.name); return await client.setRoomStateWithKey( id, EventTypes.RoomAvatar, '', - {'url': uploadResp.toString()}, + { + if (uploadResp != null) 'url': uploadResp.toString(), + }, ); }