fix multiple empty chat
This commit is contained in:
parent
b0e8b1a652
commit
af9ffe79d7
5 changed files with 51 additions and 17 deletions
|
|
@ -1,4 +1,5 @@
|
|||
class BotMode {
|
||||
static const direct = "direct";
|
||||
static const discussion = "discussion";
|
||||
static const custom = "custom";
|
||||
static const storyGame = "story_game";
|
||||
|
|
|
|||
|
|
@ -20,7 +20,6 @@ import 'package:fluffychat/pangea/controllers/subscription_controller.dart';
|
|||
import 'package:fluffychat/pangea/controllers/text_to_speech_controller.dart';
|
||||
import 'package:fluffychat/pangea/controllers/user_controller.dart';
|
||||
import 'package:fluffychat/pangea/controllers/word_net_controller.dart';
|
||||
import 'package:fluffychat/pangea/extensions/client_extension/client_extension.dart';
|
||||
import 'package:fluffychat/pangea/extensions/pangea_room_extension/pangea_room_extension.dart';
|
||||
import 'package:fluffychat/pangea/guard/p_vguard.dart';
|
||||
import 'package:fluffychat/pangea/utils/bot_name.dart';
|
||||
|
|
@ -193,19 +192,53 @@ class PangeaController {
|
|||
void startChatWithBotIfNotPresent() {
|
||||
Future.delayed(const Duration(milliseconds: 10000), () async {
|
||||
// check if user is logged in
|
||||
if (!matrixState.client.isLogged() ||
|
||||
(await matrixState.client.hasBotDM)) {
|
||||
if (!matrixState.client.isLogged()) {
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
await matrixState.client.startDirectChat(
|
||||
BotName.byEnvironment,
|
||||
enableEncryption: false,
|
||||
);
|
||||
} catch (err, stack) {
|
||||
debugger(when: kDebugMode);
|
||||
ErrorHandler.logError(e: err, s: stack);
|
||||
const List<Room> botDMs = [];
|
||||
for (final room in matrixState.client.rooms) {
|
||||
if (await room.isBotDM) {
|
||||
botDMs.add(room);
|
||||
}
|
||||
}
|
||||
|
||||
if (botDMs.isEmpty) {
|
||||
try {
|
||||
await matrixState.client.startDirectChat(
|
||||
BotName.byEnvironment,
|
||||
enableEncryption: false,
|
||||
);
|
||||
} catch (err, stack) {
|
||||
debugger(when: kDebugMode);
|
||||
ErrorHandler.logError(e: err, s: stack);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
final Room botDMWithLatestActivity = botDMs.reduce((a, b) {
|
||||
if (a.timeline == null || b.timeline == null) {
|
||||
return a;
|
||||
}
|
||||
final aLastEvent = a.timeline!.events.last;
|
||||
final bLastEvent = b.timeline!.events.last;
|
||||
return aLastEvent.originServerTs.isAfter(bLastEvent.originServerTs)
|
||||
? a
|
||||
: b;
|
||||
});
|
||||
|
||||
for (final room in botDMs) {
|
||||
if (room.id != botDMWithLatestActivity.id) {
|
||||
await room.leave();
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
final participants = await botDMWithLatestActivity.requestParticipants();
|
||||
final joinedParticipants =
|
||||
participants.where((e) => e.membership == Membership.join).toList();
|
||||
if (joinedParticipants.length < 2) {
|
||||
await botDMWithLatestActivity.invite(BotName.byEnvironment);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
|
|
|||
|
|
@ -4,6 +4,7 @@ import 'dart:developer';
|
|||
|
||||
import 'package:adaptive_dialog/adaptive_dialog.dart';
|
||||
import 'package:collection/collection.dart';
|
||||
import 'package:fluffychat/pangea/constants/bot_mode.dart';
|
||||
import 'package:fluffychat/pangea/constants/class_default_values.dart';
|
||||
import 'package:fluffychat/pangea/constants/language_constants.dart';
|
||||
import 'package:fluffychat/pangea/constants/model_keys.dart';
|
||||
|
|
@ -253,7 +254,7 @@ extension PangeaRoom on Room {
|
|||
|
||||
// bool isMadeForLang(String langCode) => _isMadeForLang(langCode);
|
||||
|
||||
Future<bool> get isBotRoom async => await _isBotRoom;
|
||||
Future<bool> get botIsInRoom async => await _botIsInRoom;
|
||||
|
||||
Future<bool> get isBotDM async => await _isBotDM;
|
||||
|
||||
|
|
|
|||
|
|
@ -49,15 +49,14 @@ extension RoomInformationRoomExtension on Room {
|
|||
// creationContent?.tryGet<String>(ModelKey.oldLangCode) == langCode;
|
||||
// }
|
||||
|
||||
Future<bool> get _isBotRoom async {
|
||||
Future<bool> get _botIsInRoom async {
|
||||
final List<User> participants = await requestParticipants();
|
||||
return participants.any(
|
||||
(User user) => user.id == BotName.byEnvironment,
|
||||
);
|
||||
}
|
||||
|
||||
Future<bool> get _isBotDM async =>
|
||||
(await isBotRoom) && getParticipants().length == 2;
|
||||
Future<bool> get _isBotDM async => botOptions?.mode == BotMode.direct;
|
||||
|
||||
bool get _isLocked {
|
||||
if (isDirectChat) return false;
|
||||
|
|
|
|||
|
|
@ -46,7 +46,7 @@ class ConversationBotSettingsState extends State<ConversationBotSettings> {
|
|||
botOptions = widget.room?.botOptions != null
|
||||
? BotOptionsModel.fromJson(widget.room?.botOptions?.toJson())
|
||||
: BotOptionsModel();
|
||||
widget.room?.isBotRoom.then((bool isBotRoom) {
|
||||
widget.room?.botIsInRoom.then((bool isBotRoom) {
|
||||
setState(() {
|
||||
addBot = isBotRoom;
|
||||
});
|
||||
|
|
@ -260,7 +260,7 @@ class ConversationBotSettingsState extends State<ConversationBotSettings> {
|
|||
botOptions = botOptions;
|
||||
});
|
||||
final bool isBotRoomMember =
|
||||
await widget.room?.isBotRoom ?? false;
|
||||
await widget.room?.botIsInRoom ?? false;
|
||||
if (addBot && !isBotRoomMember) {
|
||||
await widget.room?.invite(BotName.byEnvironment);
|
||||
} else if (!addBot && isBotRoomMember) {
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue