Merge branch 'krille/start-direct-chat' into 'main'

refactor: New start direct chat method in client

See merge request famedly/famedlysdk!639
This commit is contained in:
Krille Fear 2021-02-08 14:45:24 +00:00
commit b2a4799c9d
2 changed files with 22 additions and 18 deletions

View File

@ -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<String> 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

View File

@ -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<String> 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<String> 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];