diff --git a/lib/src/client.dart b/lib/src/client.dart index 72935624..79068e35 100644 --- a/lib/src/client.dart +++ b/lib/src/client.dart @@ -479,6 +479,27 @@ class Client extends MatrixApi { return completer.future; } + /// Returns an existing direct room ID with this user or creates a new one. + /// Returns null on error. + Future startDirectChat(String mxid) async { + // Try to find an existing direct chat + var roomId = getDirectChatFromUserId(mxid); + if (roomId != null) return roomId; + + // Start a new direct chat + roomId = await createRoom( + invite: [mxid], + isDirect: true, + preset: CreateRoomPreset.trusted_private_chat, + ); + + if (roomId == null) return roomId; + + await Room(id: roomId, client: this).addToDirectChat(mxid); + + return roomId; + } + /// Returns the user's own displayname and avatar url. In Matrix it is possible that /// one user can have different displaynames and avatar urls in different rooms. So /// this endpoint first checks if the profile is the same in all rooms. If not, the diff --git a/lib/src/user.dart b/lib/src/user.dart index 52011669..b404d94f 100644 --- a/lib/src/user.dart +++ b/lib/src/user.dart @@ -144,24 +144,7 @@ class User extends Event { /// Returns an existing direct chat ID with this user or creates a new one. /// Returns null on error. - Future startDirectChat() async { - // Try to find an existing direct chat - var roomID = await room.client?.getDirectChatFromUserId(id); - if (roomID != null) return roomID; - - // Start a new direct chat - final newRoomID = await room.client.createRoom( - invite: [id], - isDirect: true, - preset: CreateRoomPreset.trusted_private_chat, - ); - - if (newRoomID == null) return newRoomID; - - await Room(id: newRoomID, client: room.client).addToDirectChat(id); - - return newRoomID; - } + Future startDirectChat() => room.client.startDirectChat(id); /// The newest presence of this user if there is any and null if not. Presence get presence => room.client.presences[id];