59 lines
2.2 KiB
Dart
59 lines
2.2 KiB
Dart
// SPDX-FileCopyrightText: 2019-Present Christian Kußowski
|
|
// SPDX-FileCopyrightText: 2019-Present Contributors to FluffyChat
|
|
//
|
|
// SPDX-License-Identifier: AGPL-3.0-or-later
|
|
|
|
import 'package:matrix/matrix.dart';
|
|
import 'package:tatchat/config/app_config.dart';
|
|
|
|
/// State event type that marks the room category (canal/mural) on a room
|
|
/// level. Unlike the personal tags (per-account data), this marker is shared
|
|
/// by all room members which makes the categorization consistent across
|
|
/// different accounts.
|
|
const String roomTypeStateEvent = 'tatuchat.room_type';
|
|
|
|
/// Leaves the room and removes it from the local database so that it does not
|
|
/// reappear on the next sync or app restart.
|
|
Future<void> safeLeave(Room room) async {
|
|
try {
|
|
await room.leave();
|
|
} on MatrixException catch (e, s) {
|
|
// If the room was already left on the server (e.g. a stale local copy
|
|
// reappeared after a restart) the leave request fails, but the user
|
|
// intent (room not being listed anymore) is already fulfilled. Clean up
|
|
// locally instead of showing an error.
|
|
if (!{
|
|
MatrixError.M_FORBIDDEN,
|
|
MatrixError.M_NOT_FOUND,
|
|
MatrixError.M_UNKNOWN,
|
|
}.contains(e.error)) {
|
|
Logs().w('Failed to leave room ${room.id}', e, s);
|
|
rethrow;
|
|
}
|
|
Logs().w('Room ${room.id} is already gone, cleaning up locally...', e, s);
|
|
}
|
|
final client = room.client;
|
|
await client.database.forgetRoom(room.id);
|
|
client.rooms.remove(room);
|
|
}
|
|
|
|
extension RoomTypeExtension on Room {
|
|
String? get roomType =>
|
|
getState(roomTypeStateEvent)?.content.tryGet<String>('type');
|
|
|
|
bool get isCanal =>
|
|
tags.keys.contains(AppConfig.canalTag) || roomType == 'canal';
|
|
|
|
bool get isMural =>
|
|
tags.keys.contains(AppConfig.muralTag) || roomType == 'mural';
|
|
|
|
/// Whether the account left (or was banned from) this room. Such rooms must
|
|
/// not be shown in the chat list, even if a stale local copy with an old
|
|
/// "join" membership reappears after a restart.
|
|
bool get isLeftOrBanned =>
|
|
{Membership.leave, Membership.ban}.contains(membership);
|
|
|
|
Future<String> setRoomType(String type) =>
|
|
client.setRoomStateWithKey(id, roomTypeStateEvent, '', {'type': type});
|
|
}
|