updates to locking room functionality
This commit is contained in:
parent
294d5a2e66
commit
d4561b3a17
4 changed files with 91 additions and 82 deletions
|
|
@ -75,7 +75,8 @@ class ChatEventList extends StatelessWidget {
|
|||
|
||||
// #Pangea
|
||||
if (i == 1) {
|
||||
return controller.room.locked && !controller.room.isRoomAdmin
|
||||
return (controller.room.locked ?? false) &&
|
||||
!controller.room.isRoomAdmin
|
||||
? const LockedChatMessage()
|
||||
: const SizedBox.shrink();
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,12 +1,4 @@
|
|||
// Flutter imports:
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
// Package imports:
|
||||
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';
|
||||
|
||||
// Project imports:
|
||||
import 'package:fluffychat/config/app_config.dart';
|
||||
import 'package:fluffychat/pages/chat_details/chat_details.dart';
|
||||
|
|
@ -29,6 +21,12 @@ import 'package:fluffychat/widgets/avatar.dart';
|
|||
import 'package:fluffychat/widgets/chat_settings_popup_menu.dart';
|
||||
import 'package:fluffychat/widgets/layouts/max_width_body.dart';
|
||||
import 'package:fluffychat/widgets/matrix.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
// Package imports:
|
||||
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';
|
||||
|
||||
class ChatDetailsView extends StatelessWidget {
|
||||
final ChatDetailsController controller;
|
||||
|
|
@ -553,13 +551,18 @@ class ChatDetailsView extends StatelessWidget {
|
|||
Icons.lock_outlined,
|
||||
),
|
||||
),
|
||||
value: room.locked,
|
||||
value: room.locked ?? false,
|
||||
onChanged: (value) => showFutureLoadingDialog(
|
||||
context: context,
|
||||
future: () => toggleLockRoom(
|
||||
room,
|
||||
Matrix.of(context).client,
|
||||
),
|
||||
future: () => value
|
||||
? lockRoom(
|
||||
room,
|
||||
Matrix.of(context).client,
|
||||
)
|
||||
: unlockRoom(
|
||||
room,
|
||||
Matrix.of(context).client,
|
||||
),
|
||||
),
|
||||
),
|
||||
const Divider(height: 1),
|
||||
|
|
|
|||
|
|
@ -2,15 +2,6 @@
|
|||
import 'dart:async';
|
||||
import 'dart:developer';
|
||||
|
||||
// Flutter imports:
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
// Package imports:
|
||||
import 'package:matrix/matrix.dart';
|
||||
import 'package:matrix/src/utils/space_child.dart';
|
||||
import 'package:sentry_flutter/sentry_flutter.dart';
|
||||
|
||||
// Project imports:
|
||||
import 'package:fluffychat/pangea/constants/class_default_values.dart';
|
||||
import 'package:fluffychat/pangea/constants/model_keys.dart';
|
||||
|
|
@ -19,6 +10,14 @@ import 'package:fluffychat/pangea/models/class_model.dart';
|
|||
import 'package:fluffychat/pangea/models/pangea_message_event.dart';
|
||||
import 'package:fluffychat/pangea/utils/bot_name.dart';
|
||||
import 'package:fluffychat/pangea/utils/error_handler.dart';
|
||||
// Flutter imports:
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
// Package imports:
|
||||
import 'package:matrix/matrix.dart';
|
||||
import 'package:matrix/src/utils/space_child.dart';
|
||||
import 'package:sentry_flutter/sentry_flutter.dart';
|
||||
|
||||
import '../../config/app_config.dart';
|
||||
import '../constants/pangea_event_types.dart';
|
||||
import '../enum/construct_type_enum.dart';
|
||||
|
|
@ -756,8 +755,7 @@ extension PangeaRoom on Room {
|
|||
}
|
||||
final toAdd = [
|
||||
...getParticipants([Membership.invite, Membership.join])
|
||||
.map((e) => e.id)
|
||||
.toList(),
|
||||
.map((e) => e.id),
|
||||
BotName.byEnvironment,
|
||||
];
|
||||
for (final teacher in await client.myTeachers) {
|
||||
|
|
@ -920,29 +918,20 @@ extension PangeaRoom on Room {
|
|||
);
|
||||
}
|
||||
|
||||
bool get locked {
|
||||
final Event? powerLevels = getState(EventTypes.RoomPowerLevels);
|
||||
if (powerLevels == null) {
|
||||
return false;
|
||||
}
|
||||
final Map<String, dynamic> powerLevelsContent = Map<String, dynamic>.from(
|
||||
powerLevels.content,
|
||||
);
|
||||
int? get eventsDefaultPowerLevel => getState(EventTypes.RoomPowerLevels)
|
||||
?.content
|
||||
.tryGet<int>('events_default');
|
||||
|
||||
bool? get locked {
|
||||
if (isDirectChat) return false;
|
||||
if (!isSpace) {
|
||||
return powerLevelsContent['events_default'] != null &&
|
||||
powerLevelsContent['events_default'] >= 100;
|
||||
if (eventsDefaultPowerLevel == null) return null;
|
||||
return eventsDefaultPowerLevel! >= ClassDefaultValues.powerLevelOfAdmin;
|
||||
}
|
||||
|
||||
final List<Room?> children = spaceChildren
|
||||
.map(
|
||||
(child) =>
|
||||
child.roomId != null ? client.getRoomById(child.roomId!) : null,
|
||||
)
|
||||
.toList();
|
||||
|
||||
for (final Room? child in children) {
|
||||
if (child != null && !child.locked) {
|
||||
for (final child in spaceChildren) {
|
||||
if (child.roomId == null) continue;
|
||||
final Room? room = client.getRoomById(child.roomId!);
|
||||
if (room?.locked == false && (room?.canChangePowerLevel ?? false)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,11 +1,39 @@
|
|||
// Package imports:
|
||||
// Project imports:
|
||||
import 'package:matrix/matrix.dart';
|
||||
|
||||
// Project imports:
|
||||
import 'package:fluffychat/pangea/extensions/pangea_room_extension.dart';
|
||||
import 'package:fluffychat/pangea/utils/join_all_space_chats.dart';
|
||||
Future<void> lockRoom(Room room, Client client) async {
|
||||
room.isSpace ? await lockSpace(room, client) : await lockChat(room, client);
|
||||
}
|
||||
|
||||
Future<void> unlockRoom(Room room, Client client) async {
|
||||
room.isSpace
|
||||
? await unlockSpace(room, client)
|
||||
: await unlockChat(room, client);
|
||||
}
|
||||
|
||||
Future<void> lockChat(Room room, Client client) async {
|
||||
if (!room.canChangePowerLevel) {
|
||||
return;
|
||||
}
|
||||
final Map<String, dynamic> powerLevelsContent = Map<String, dynamic>.from(
|
||||
room.getState(EventTypes.RoomPowerLevels)!.content,
|
||||
);
|
||||
powerLevelsContent['events_default'] = 100;
|
||||
powerLevelsContent['events'][EventTypes.spaceChild] = 100;
|
||||
|
||||
await room.client.setRoomStateWithKey(
|
||||
room.id,
|
||||
EventTypes.RoomPowerLevels,
|
||||
'',
|
||||
powerLevelsContent,
|
||||
);
|
||||
}
|
||||
|
||||
Future<void> unlockChat(Room room, Client client) async {
|
||||
if (!room.canChangePowerLevel) {
|
||||
return;
|
||||
}
|
||||
final Map<String, dynamic> powerLevelsContent = Map<String, dynamic>.from(
|
||||
room.getState(EventTypes.RoomPowerLevels)!.content,
|
||||
);
|
||||
|
|
@ -21,46 +49,34 @@ Future<void> unlockChat(Room room, Client client) async {
|
|||
);
|
||||
}
|
||||
|
||||
Future<void> lockChat(Room room, Client client) async {
|
||||
final Map<String, dynamic> powerLevelsContent = Map<String, dynamic>.from(
|
||||
room.getState(EventTypes.RoomPowerLevels)!.content,
|
||||
);
|
||||
powerLevelsContent['events_default'] = 100;
|
||||
powerLevelsContent['events'][EventTypes.spaceChild] = 100;
|
||||
|
||||
await room.client.setRoomStateWithKey(
|
||||
room.id,
|
||||
EventTypes.RoomPowerLevels,
|
||||
'',
|
||||
powerLevelsContent,
|
||||
);
|
||||
}
|
||||
|
||||
Future<void> lockSpace(Room space, Client client) async {
|
||||
final List<Room> children = await joinAllSpaceChats(space, client);
|
||||
for (final Room child in children) {
|
||||
await lockChat(child, client);
|
||||
for (final spaceChild in space.spaceChildren) {
|
||||
Room? child = client.getRoomById(spaceChild.roomId!);
|
||||
if (child == null) {
|
||||
try {
|
||||
await client.joinRoom(spaceChild.roomId!);
|
||||
await client.waitForRoomInSync(spaceChild.roomId!, join: true);
|
||||
child = client.getRoomById(spaceChild.roomId!);
|
||||
} catch (err) {
|
||||
await client.leaveRoom(spaceChild.roomId!);
|
||||
continue;
|
||||
}
|
||||
}
|
||||
if (child == null) continue;
|
||||
child.isSpace
|
||||
? await lockSpace(child, client)
|
||||
: await lockChat(child, client);
|
||||
}
|
||||
await lockChat(space, client);
|
||||
}
|
||||
|
||||
Future<void> unlockSpace(Room space, Client client) async {
|
||||
final List<Room?> children = space.spaceChildren
|
||||
.map((child) => client.getRoomById(child.roomId!))
|
||||
.toList();
|
||||
for (final Room? child in children) {
|
||||
if (child != null) {
|
||||
await unlockChat(child, client);
|
||||
}
|
||||
for (final spaceChild in space.spaceChildren) {
|
||||
final Room? child = client.getRoomById(spaceChild.roomId!);
|
||||
if (child == null) continue;
|
||||
child.isSpace
|
||||
? await unlockSpace(child, client)
|
||||
: await unlockChat(child, client);
|
||||
}
|
||||
await unlockChat(space, client);
|
||||
}
|
||||
|
||||
Future<void> toggleLockRoom(Room? room, Client client) async {
|
||||
if (room == null || !room.isRoomAdmin) return;
|
||||
if (!room.isSpace) {
|
||||
room.locked ? await unlockChat(room, client) : await lockChat(room, client);
|
||||
return;
|
||||
}
|
||||
room.locked ? await unlockSpace(room, client) : await lockSpace(room, client);
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue