Merge pull request #1965 from famedly/krille/improve-join-room-documentation
refactor: Clarify Room.join() behavior and make sure DM link is purged if room not found
This commit is contained in:
commit
cd31df9b27
|
|
@ -1153,21 +1153,29 @@ class Room {
|
||||||
/// Call the Matrix API to join this room if the user is not already a member.
|
/// Call the Matrix API to join this room if the user is not already a member.
|
||||||
/// If this room is intended to be a direct chat, the direct chat flag will
|
/// If this room is intended to be a direct chat, the direct chat flag will
|
||||||
/// automatically be set.
|
/// automatically be set.
|
||||||
Future<void> join({bool leaveIfNotFound = true}) async {
|
Future<void> join({
|
||||||
|
/// In case of the room is not found on the server, the client leaves the
|
||||||
|
/// room and rethrows the exception.
|
||||||
|
bool leaveIfNotFound = true,
|
||||||
|
}) async {
|
||||||
|
final dmId = directChatMatrixID;
|
||||||
try {
|
try {
|
||||||
// If this is a DM, mark it as a DM first, because otherwise the current member
|
// If this is a DM, mark it as a DM first, because otherwise the current member
|
||||||
// event might be the join event already and there is also a race condition there for SDK users.
|
// event might be the join event already and there is also a race condition there for SDK users.
|
||||||
final dmId = directChatMatrixID;
|
if (dmId != null) await addToDirectChat(dmId);
|
||||||
if (dmId != null) {
|
|
||||||
await addToDirectChat(dmId);
|
|
||||||
}
|
|
||||||
|
|
||||||
// now join
|
// now join
|
||||||
await client.joinRoomById(id);
|
await client.joinRoomById(id);
|
||||||
} on MatrixException catch (exception) {
|
} on MatrixException catch (exception) {
|
||||||
|
if (dmId != null) await removeFromDirectChat();
|
||||||
if (leaveIfNotFound &&
|
if (leaveIfNotFound &&
|
||||||
[MatrixError.M_NOT_FOUND, MatrixError.M_UNKNOWN]
|
membership == Membership.invite &&
|
||||||
.contains(exception.error)) {
|
// Right now Synapse responses with `M_UNKNOWN` when the room can not
|
||||||
|
// be found. This is the case for example when User A invites User B
|
||||||
|
// to a direct chat and then User A leaves the chat before User B
|
||||||
|
// joined.
|
||||||
|
// See: https://github.com/element-hq/synapse/issues/1533
|
||||||
|
exception.error == MatrixError.M_UNKNOWN) {
|
||||||
await leave();
|
await leave();
|
||||||
}
|
}
|
||||||
rethrow;
|
rethrow;
|
||||||
|
|
@ -1180,9 +1188,13 @@ class Room {
|
||||||
Future<void> leave() async {
|
Future<void> leave() async {
|
||||||
try {
|
try {
|
||||||
await client.leaveRoom(id);
|
await client.leaveRoom(id);
|
||||||
} on MatrixException catch (exception) {
|
} on MatrixException catch (e, s) {
|
||||||
if ([MatrixError.M_NOT_FOUND, MatrixError.M_UNKNOWN]
|
if ([MatrixError.M_NOT_FOUND, MatrixError.M_UNKNOWN].contains(e.error)) {
|
||||||
.contains(exception.error)) {
|
Logs().w(
|
||||||
|
'Unable to leave room. Deleting manually from database...',
|
||||||
|
e,
|
||||||
|
s,
|
||||||
|
);
|
||||||
await _handleFakeSync(
|
await _handleFakeSync(
|
||||||
SyncUpdate(
|
SyncUpdate(
|
||||||
nextBatch: '',
|
nextBatch: '',
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue