Merge pull request #236 from pangeachat/prevent-empty-chats
Prevent Empty Chats
This commit is contained in:
commit
629b5e48e3
7 changed files with 27 additions and 247 deletions
|
|
@ -728,6 +728,11 @@ class ChatListController extends State<ChatList>
|
|||
while (selectedRoomIds.isNotEmpty) {
|
||||
final roomId = selectedRoomIds.first;
|
||||
try {
|
||||
// #Pangea
|
||||
if (client.getRoomById(roomId)!.isUnread) {
|
||||
await client.getRoomById(roomId)!.markUnread(false);
|
||||
}
|
||||
// Pangea#
|
||||
await client.getRoomById(roomId)!.leave();
|
||||
} finally {
|
||||
toggleSelection(roomId);
|
||||
|
|
|
|||
|
|
@ -53,6 +53,11 @@ class ChatListItem extends StatelessWidget {
|
|||
message: L10n.of(context)!.archiveRoomDescription,
|
||||
);
|
||||
if (confirmed == OkCancelResult.cancel) return;
|
||||
// #Pangea
|
||||
if (room.isUnread) {
|
||||
await room.markUnread(false);
|
||||
}
|
||||
// Pangea#
|
||||
await showFutureLoadingDialog(
|
||||
context: context,
|
||||
future: () => room.leave(),
|
||||
|
|
|
|||
|
|
@ -288,7 +288,10 @@ class _SpaceViewState extends State<SpaceView> {
|
|||
// #Pangea
|
||||
// future: room!.leave,
|
||||
future: () async {
|
||||
await room!.leave();
|
||||
if (room!.isUnread) {
|
||||
await room.markUnread(false);
|
||||
}
|
||||
await room.leave();
|
||||
if (Matrix.of(context).activeRoomId == room.id) {
|
||||
context.go('/rooms');
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,15 +1,13 @@
|
|||
import 'package:flutter/material.dart';
|
||||
|
||||
import 'package:adaptive_dialog/adaptive_dialog.dart';
|
||||
import 'package:fluffychat/pages/chat/send_file_dialog.dart';
|
||||
import 'package:fluffychat/utils/matrix_sdk_extensions/matrix_locals.dart';
|
||||
import 'package:fluffychat/widgets/matrix.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_gen/gen_l10n/l10n.dart';
|
||||
import 'package:future_loading_dialog/future_loading_dialog.dart';
|
||||
import 'package:go_router/go_router.dart';
|
||||
import 'package:matrix/matrix.dart';
|
||||
|
||||
import 'package:fluffychat/pages/chat/send_file_dialog.dart';
|
||||
import 'package:fluffychat/utils/matrix_sdk_extensions/matrix_locals.dart';
|
||||
import 'package:fluffychat/widgets/matrix.dart';
|
||||
|
||||
void onChatTap(Room room, BuildContext context) async {
|
||||
if (room.membership == Membership.invite) {
|
||||
final inviterId =
|
||||
|
|
@ -47,6 +45,11 @@ void onChatTap(Room room, BuildContext context) async {
|
|||
return;
|
||||
}
|
||||
if (inviteAction == InviteActions.decline) {
|
||||
// #Pangea
|
||||
if (room.isUnread) {
|
||||
await room.markUnread(false);
|
||||
}
|
||||
// Pangea#
|
||||
await showFutureLoadingDialog(
|
||||
context: context,
|
||||
future: room.leave,
|
||||
|
|
|
|||
|
|
@ -1,149 +0,0 @@
|
|||
import 'package:flutter/material.dart';
|
||||
|
||||
import 'package:flutter_gen/gen_l10n/l10n.dart';
|
||||
import 'package:future_loading_dialog/future_loading_dialog.dart';
|
||||
import 'package:go_router/go_router.dart';
|
||||
import 'package:matrix/matrix.dart';
|
||||
|
||||
import 'package:fluffychat/pangea/utils/delete_room.dart';
|
||||
import 'package:fluffychat/widgets/matrix.dart';
|
||||
|
||||
class DeleteSpaceTile extends StatelessWidget {
|
||||
final Room room;
|
||||
|
||||
const DeleteSpaceTile({
|
||||
super.key,
|
||||
required this.room,
|
||||
});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
bool classNameMatch = true;
|
||||
final textController = TextEditingController();
|
||||
Future<void> deleteSpace() async {
|
||||
final Client client = Matrix.of(context).client;
|
||||
final GetSpaceHierarchyResponse spaceHierarchy =
|
||||
await client.getSpaceHierarchy(room.id);
|
||||
|
||||
if (spaceHierarchy.rooms.isNotEmpty) {
|
||||
final List<Room> spaceChats = spaceHierarchy.rooms
|
||||
.where((c) => c.roomId != room.id)
|
||||
.map((e) => Matrix.of(context).client.getRoomById(e.roomId))
|
||||
.where((c) => c != null && !c.isSpace && !c.isDirectChat)
|
||||
.cast<Room>()
|
||||
.toList();
|
||||
|
||||
await Future.wait(
|
||||
spaceChats.map((c) => deleteRoom(c.id, client)),
|
||||
);
|
||||
}
|
||||
deleteRoom(room.id, client);
|
||||
context.go('/rooms');
|
||||
return;
|
||||
}
|
||||
|
||||
Future<void> deleteChat() {
|
||||
context.go('/rooms');
|
||||
return deleteRoom(room.id, Matrix.of(context).client);
|
||||
}
|
||||
|
||||
Future<void> deleteChatAction() async {
|
||||
showDialog(
|
||||
context: context,
|
||||
useRootNavigator: false,
|
||||
builder: (context) {
|
||||
return StatefulBuilder(
|
||||
builder: (context, setState) {
|
||||
return AlertDialog(
|
||||
title: Column(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
Text(
|
||||
room.isSpace
|
||||
? L10n.of(context)!.areYouSureDeleteClass
|
||||
: L10n.of(context)!.areYouSureDeleteGroup,
|
||||
style: const TextStyle(
|
||||
fontSize: 20,
|
||||
),
|
||||
textAlign: TextAlign.center,
|
||||
),
|
||||
const SizedBox(height: 5),
|
||||
Text(
|
||||
L10n.of(context)!.cannotBeReversed,
|
||||
style: const TextStyle(
|
||||
fontSize: 16,
|
||||
),
|
||||
textAlign: TextAlign.center,
|
||||
),
|
||||
const SizedBox(height: 10),
|
||||
if (room.isSpace)
|
||||
Text(
|
||||
L10n.of(context)!.enterDeletedClassName,
|
||||
style: const TextStyle(
|
||||
fontSize: 14,
|
||||
),
|
||||
textAlign: TextAlign.center,
|
||||
),
|
||||
],
|
||||
),
|
||||
content: room.isSpace
|
||||
? TextField(
|
||||
autofocus: true,
|
||||
controller: textController,
|
||||
decoration: InputDecoration(
|
||||
hintText: room.name,
|
||||
errorText: !classNameMatch
|
||||
? L10n.of(context)!.incorrectClassName
|
||||
: null,
|
||||
),
|
||||
)
|
||||
: null,
|
||||
actions: <Widget>[
|
||||
TextButton(
|
||||
child: Text(L10n.of(context)!.ok),
|
||||
onPressed: () async {
|
||||
if (room.isSpace) {
|
||||
setState(() {
|
||||
classNameMatch = textController.text == room.name;
|
||||
});
|
||||
if (classNameMatch) {
|
||||
Navigator.of(context).pop();
|
||||
await showFutureLoadingDialog(
|
||||
context: context,
|
||||
future: () => deleteSpace(),
|
||||
);
|
||||
}
|
||||
} else {
|
||||
await showFutureLoadingDialog(
|
||||
context: context,
|
||||
future: () => deleteChat(),
|
||||
);
|
||||
}
|
||||
},
|
||||
),
|
||||
TextButton(
|
||||
child: Text(L10n.of(context)!.cancel),
|
||||
onPressed: () {
|
||||
Navigator.of(context).pop();
|
||||
},
|
||||
),
|
||||
],
|
||||
);
|
||||
},
|
||||
);
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
return ListTile(
|
||||
trailing: const Icon(Icons.delete_outlined),
|
||||
title: Text(
|
||||
room.isSpace
|
||||
? L10n.of(context)!.deleteSpace
|
||||
: L10n.of(context)!.deleteGroup,
|
||||
style: const TextStyle(color: Colors.red),
|
||||
),
|
||||
onTap: () => deleteChatAction(),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
@ -1,7 +1,6 @@
|
|||
import 'package:matrix/matrix.dart';
|
||||
|
||||
import 'package:fluffychat/pangea/extensions/pangea_room_extension.dart';
|
||||
import 'package:fluffychat/pangea/utils/error_handler.dart';
|
||||
import 'package:matrix/matrix.dart';
|
||||
|
||||
Future<void> archiveSpace(Room? space, Client client) async {
|
||||
if (space == null) {
|
||||
|
|
@ -14,6 +13,9 @@ Future<void> archiveSpace(Room? space, Client client) async {
|
|||
|
||||
final List<Room> children = await space.getChildRooms();
|
||||
for (final Room child in children) {
|
||||
if (child.isUnread) {
|
||||
await child.markUnread(false);
|
||||
}
|
||||
await child.leave();
|
||||
}
|
||||
await space.leave();
|
||||
|
|
|
|||
|
|
@ -1,89 +0,0 @@
|
|||
import 'package:matrix/matrix.dart';
|
||||
|
||||
import 'package:fluffychat/pangea/constants/class_default_values.dart';
|
||||
import 'error_handler.dart';
|
||||
|
||||
Future<void> deleteRoom(String? roomID, Client client) async {
|
||||
if (roomID == null) {
|
||||
ErrorHandler.logError(
|
||||
m: "in deleteRoomAction with null pangeaClassRoomID",
|
||||
s: StackTrace.current,
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
final Room? room = client.getRoomById(roomID);
|
||||
if (room == null) {
|
||||
ErrorHandler.logError(
|
||||
m: "failed to fetch room with roomID $roomID",
|
||||
s: StackTrace.current,
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
await room.join();
|
||||
} catch (err) {
|
||||
ErrorHandler.logError(
|
||||
m: "failed to join room with roomID $roomID",
|
||||
s: StackTrace.current,
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
List<User> members;
|
||||
try {
|
||||
members = await room.requestParticipants();
|
||||
} catch (err) {
|
||||
ErrorHandler.logError(
|
||||
m: "failed to fetch members for room with roomID $roomID",
|
||||
s: StackTrace.current,
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
final List<User> otherAdmins = [];
|
||||
for (final User member in members) {
|
||||
final String memberID = member.id;
|
||||
final int memberPowerLevel = room.getPowerLevelByUserId(memberID);
|
||||
if (memberID == client.userID) continue;
|
||||
if (memberPowerLevel >= ClassDefaultValues.powerLevelOfAdmin) {
|
||||
otherAdmins.add(member);
|
||||
continue;
|
||||
}
|
||||
try {
|
||||
await room.kick(memberID);
|
||||
} catch (err) {
|
||||
ErrorHandler.logError(
|
||||
m: "Failed to kick user $memberID from room with id $roomID. Error: $err",
|
||||
s: StackTrace.current,
|
||||
);
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
if (otherAdmins.isNotEmpty && room.canSendEvent(EventTypes.RoomJoinRules)) {
|
||||
try {
|
||||
await client.setRoomStateWithKey(
|
||||
roomID,
|
||||
EventTypes.RoomJoinRules,
|
||||
"",
|
||||
{"join_rules": "invite"},
|
||||
);
|
||||
} catch (err) {
|
||||
ErrorHandler.logError(
|
||||
m: "Failed to update student create room permissions. error: $err, roomId: $roomID",
|
||||
s: StackTrace.current,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
await room.leave();
|
||||
} catch (err) {
|
||||
ErrorHandler.logError(
|
||||
m: "Failed to leave room with id $roomID. Error: $err",
|
||||
s: StackTrace.current,
|
||||
);
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Reference in a new issue