mirror of
https://gitlab.com/veilid/veilidchat.git
synced 2025-12-15 16:29:10 -05:00
message length limit
This commit is contained in:
parent
9dfb8c3f71
commit
94988718e8
13 changed files with 792 additions and 132 deletions
|
|
@ -42,6 +42,7 @@ class ChatComponentCubit extends Cubit<ChatComponentState> {
|
|||
super(ChatComponentState(
|
||||
chatKey: GlobalKey<ChatState>(),
|
||||
scrollController: AutoScrollController(),
|
||||
textEditingController: InputTextFieldController(),
|
||||
localUser: null,
|
||||
remoteUsers: const IMap.empty(),
|
||||
historicalRemoteUsers: const IMap.empty(),
|
||||
|
|
|
|||
|
|
@ -97,11 +97,13 @@ class SingleContactMessagesCubit extends Cubit<SingleContactMessagesState> {
|
|||
// Initialize everything
|
||||
Future<void> _init() async {
|
||||
_unsentMessagesQueue = PersistentQueue<proto.Message>(
|
||||
table: 'SingleContactUnsentMessages',
|
||||
key: _remoteConversationRecordKey.toString(),
|
||||
fromBuffer: proto.Message.fromBuffer,
|
||||
closure: _processUnsentMessages,
|
||||
);
|
||||
table: 'SingleContactUnsentMessages',
|
||||
key: _remoteConversationRecordKey.toString(),
|
||||
fromBuffer: proto.Message.fromBuffer,
|
||||
closure: _processUnsentMessages,
|
||||
onError: (e, sp) {
|
||||
log.error('Exception while processing unsent messages: $e\n$sp\n');
|
||||
});
|
||||
|
||||
// Make crypto
|
||||
await _initCrypto();
|
||||
|
|
@ -297,16 +299,23 @@ class SingleContactMessagesCubit extends Cubit<SingleContactMessagesState> {
|
|||
proto.Message? previousMessage;
|
||||
final processedMessages = messages.toList();
|
||||
for (final message in processedMessages) {
|
||||
await _processMessageToSend(message, previousMessage);
|
||||
previousMessage = message;
|
||||
try {
|
||||
await _processMessageToSend(message, previousMessage);
|
||||
previousMessage = message;
|
||||
} on Exception catch (e) {
|
||||
log.error('Exception processing unsent message: $e');
|
||||
}
|
||||
}
|
||||
|
||||
// _sendingMessages = messages;
|
||||
|
||||
// _renderState();
|
||||
|
||||
await _sentMessagesCubit!.operateAppendEventual((writer) =>
|
||||
writer.addAll(messages.map((m) => m.writeToBuffer()).toList()));
|
||||
try {
|
||||
await _sentMessagesCubit!.operateAppendEventual((writer) =>
|
||||
writer.addAll(messages.map((m) => m.writeToBuffer()).toList()));
|
||||
} on Exception catch (e) {
|
||||
log.error('Exception appending unsent messages: $e');
|
||||
}
|
||||
|
||||
// _sendingMessages = const IList.empty();
|
||||
}
|
||||
|
|
@ -403,6 +412,10 @@ class SingleContactMessagesCubit extends Cubit<SingleContactMessagesState> {
|
|||
..author = _accountInfo.identityTypedPublicKey.toProto()
|
||||
..timestamp = Veilid.instance.now().toInt64();
|
||||
|
||||
if ((message.writeToBuffer().lengthInBytes + 256) > 4096) {
|
||||
throw const FormatException('message is too long');
|
||||
}
|
||||
|
||||
// Put in the queue
|
||||
_unsentMessagesQueue.addSync(message);
|
||||
|
||||
|
|
|
|||
|
|
@ -2,7 +2,8 @@ import 'package:async_tools/async_tools.dart';
|
|||
import 'package:fast_immutable_collections/fast_immutable_collections.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_chat_types/flutter_chat_types.dart' show Message, User;
|
||||
import 'package:flutter_chat_ui/flutter_chat_ui.dart' show ChatState;
|
||||
import 'package:flutter_chat_ui/flutter_chat_ui.dart'
|
||||
show ChatState, InputTextFieldController;
|
||||
import 'package:freezed_annotation/freezed_annotation.dart';
|
||||
import 'package:scroll_to_index/scroll_to_index.dart';
|
||||
import 'package:veilid_support/veilid_support.dart';
|
||||
|
|
@ -19,6 +20,8 @@ class ChatComponentState with _$ChatComponentState {
|
|||
required GlobalKey<ChatState> chatKey,
|
||||
// ScrollController for the chat
|
||||
required AutoScrollController scrollController,
|
||||
// TextEditingController for the chat
|
||||
required InputTextFieldController textEditingController,
|
||||
// Local user
|
||||
required User? localUser,
|
||||
// Active remote users
|
||||
|
|
|
|||
|
|
@ -20,6 +20,8 @@ mixin _$ChatComponentState {
|
|||
GlobalKey<ChatState> get chatKey =>
|
||||
throw _privateConstructorUsedError; // ScrollController for the chat
|
||||
AutoScrollController get scrollController =>
|
||||
throw _privateConstructorUsedError; // TextEditingController for the chat
|
||||
InputTextFieldController get textEditingController =>
|
||||
throw _privateConstructorUsedError; // Local user
|
||||
User? get localUser =>
|
||||
throw _privateConstructorUsedError; // Active remote users
|
||||
|
|
@ -47,6 +49,7 @@ abstract class $ChatComponentStateCopyWith<$Res> {
|
|||
$Res call(
|
||||
{GlobalKey<ChatState> chatKey,
|
||||
AutoScrollController scrollController,
|
||||
InputTextFieldController textEditingController,
|
||||
User? localUser,
|
||||
IMap<Typed<FixedEncodedString43>, User> remoteUsers,
|
||||
IMap<Typed<FixedEncodedString43>, User> historicalRemoteUsers,
|
||||
|
|
@ -72,6 +75,7 @@ class _$ChatComponentStateCopyWithImpl<$Res, $Val extends ChatComponentState>
|
|||
$Res call({
|
||||
Object? chatKey = null,
|
||||
Object? scrollController = null,
|
||||
Object? textEditingController = null,
|
||||
Object? localUser = freezed,
|
||||
Object? remoteUsers = null,
|
||||
Object? historicalRemoteUsers = null,
|
||||
|
|
@ -88,6 +92,10 @@ class _$ChatComponentStateCopyWithImpl<$Res, $Val extends ChatComponentState>
|
|||
? _value.scrollController
|
||||
: scrollController // ignore: cast_nullable_to_non_nullable
|
||||
as AutoScrollController,
|
||||
textEditingController: null == textEditingController
|
||||
? _value.textEditingController
|
||||
: textEditingController // ignore: cast_nullable_to_non_nullable
|
||||
as InputTextFieldController,
|
||||
localUser: freezed == localUser
|
||||
? _value.localUser
|
||||
: localUser // ignore: cast_nullable_to_non_nullable
|
||||
|
|
@ -136,6 +144,7 @@ abstract class _$$ChatComponentStateImplCopyWith<$Res>
|
|||
$Res call(
|
||||
{GlobalKey<ChatState> chatKey,
|
||||
AutoScrollController scrollController,
|
||||
InputTextFieldController textEditingController,
|
||||
User? localUser,
|
||||
IMap<Typed<FixedEncodedString43>, User> remoteUsers,
|
||||
IMap<Typed<FixedEncodedString43>, User> historicalRemoteUsers,
|
||||
|
|
@ -160,6 +169,7 @@ class __$$ChatComponentStateImplCopyWithImpl<$Res>
|
|||
$Res call({
|
||||
Object? chatKey = null,
|
||||
Object? scrollController = null,
|
||||
Object? textEditingController = null,
|
||||
Object? localUser = freezed,
|
||||
Object? remoteUsers = null,
|
||||
Object? historicalRemoteUsers = null,
|
||||
|
|
@ -176,6 +186,10 @@ class __$$ChatComponentStateImplCopyWithImpl<$Res>
|
|||
? _value.scrollController
|
||||
: scrollController // ignore: cast_nullable_to_non_nullable
|
||||
as AutoScrollController,
|
||||
textEditingController: null == textEditingController
|
||||
? _value.textEditingController
|
||||
: textEditingController // ignore: cast_nullable_to_non_nullable
|
||||
as InputTextFieldController,
|
||||
localUser: freezed == localUser
|
||||
? _value.localUser
|
||||
: localUser // ignore: cast_nullable_to_non_nullable
|
||||
|
|
@ -210,6 +224,7 @@ class _$ChatComponentStateImpl implements _ChatComponentState {
|
|||
const _$ChatComponentStateImpl(
|
||||
{required this.chatKey,
|
||||
required this.scrollController,
|
||||
required this.textEditingController,
|
||||
required this.localUser,
|
||||
required this.remoteUsers,
|
||||
required this.historicalRemoteUsers,
|
||||
|
|
@ -223,6 +238,9 @@ class _$ChatComponentStateImpl implements _ChatComponentState {
|
|||
// ScrollController for the chat
|
||||
@override
|
||||
final AutoScrollController scrollController;
|
||||
// TextEditingController for the chat
|
||||
@override
|
||||
final InputTextFieldController textEditingController;
|
||||
// Local user
|
||||
@override
|
||||
final User? localUser;
|
||||
|
|
@ -244,7 +262,7 @@ class _$ChatComponentStateImpl implements _ChatComponentState {
|
|||
|
||||
@override
|
||||
String toString() {
|
||||
return 'ChatComponentState(chatKey: $chatKey, scrollController: $scrollController, localUser: $localUser, remoteUsers: $remoteUsers, historicalRemoteUsers: $historicalRemoteUsers, unknownUsers: $unknownUsers, messageWindow: $messageWindow, title: $title)';
|
||||
return 'ChatComponentState(chatKey: $chatKey, scrollController: $scrollController, textEditingController: $textEditingController, localUser: $localUser, remoteUsers: $remoteUsers, historicalRemoteUsers: $historicalRemoteUsers, unknownUsers: $unknownUsers, messageWindow: $messageWindow, title: $title)';
|
||||
}
|
||||
|
||||
@override
|
||||
|
|
@ -255,6 +273,8 @@ class _$ChatComponentStateImpl implements _ChatComponentState {
|
|||
(identical(other.chatKey, chatKey) || other.chatKey == chatKey) &&
|
||||
(identical(other.scrollController, scrollController) ||
|
||||
other.scrollController == scrollController) &&
|
||||
(identical(other.textEditingController, textEditingController) ||
|
||||
other.textEditingController == textEditingController) &&
|
||||
(identical(other.localUser, localUser) ||
|
||||
other.localUser == localUser) &&
|
||||
(identical(other.remoteUsers, remoteUsers) ||
|
||||
|
|
@ -273,6 +293,7 @@ class _$ChatComponentStateImpl implements _ChatComponentState {
|
|||
runtimeType,
|
||||
chatKey,
|
||||
scrollController,
|
||||
textEditingController,
|
||||
localUser,
|
||||
remoteUsers,
|
||||
historicalRemoteUsers,
|
||||
|
|
@ -292,6 +313,7 @@ abstract class _ChatComponentState implements ChatComponentState {
|
|||
const factory _ChatComponentState(
|
||||
{required final GlobalKey<ChatState> chatKey,
|
||||
required final AutoScrollController scrollController,
|
||||
required final InputTextFieldController textEditingController,
|
||||
required final User? localUser,
|
||||
required final IMap<Typed<FixedEncodedString43>, User> remoteUsers,
|
||||
required final IMap<Typed<FixedEncodedString43>, User>
|
||||
|
|
@ -304,6 +326,8 @@ abstract class _ChatComponentState implements ChatComponentState {
|
|||
GlobalKey<ChatState> get chatKey;
|
||||
@override // ScrollController for the chat
|
||||
AutoScrollController get scrollController;
|
||||
@override // TextEditingController for the chat
|
||||
InputTextFieldController get textEditingController;
|
||||
@override // Local user
|
||||
User? get localUser;
|
||||
@override // Active remote users
|
||||
|
|
|
|||
|
|
@ -1,3 +1,4 @@
|
|||
import 'dart:convert';
|
||||
import 'dart:math';
|
||||
|
||||
import 'package:async_tools/async_tools.dart';
|
||||
|
|
@ -6,6 +7,7 @@ import 'package:flutter/material.dart';
|
|||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
import 'package:flutter_chat_types/flutter_chat_types.dart' as types;
|
||||
import 'package:flutter_chat_ui/flutter_chat_ui.dart';
|
||||
import 'package:flutter_translate/flutter_translate.dart';
|
||||
import 'package:veilid_support/veilid_support.dart';
|
||||
|
||||
import '../../account_manager/account_manager.dart';
|
||||
|
|
@ -154,6 +156,14 @@ class ChatComponentWidget extends StatelessWidget {
|
|||
final scale = theme.extension<ScaleScheme>()!;
|
||||
final textTheme = Theme.of(context).textTheme;
|
||||
final chatTheme = makeChatTheme(scale, textTheme);
|
||||
final errorChatTheme = (ChatThemeEditor(chatTheme)
|
||||
..inputTextColor = scale.errorScale.primary
|
||||
..sendButtonIcon = Image.asset(
|
||||
'assets/icon-send.png',
|
||||
color: scale.errorScale.primary,
|
||||
package: 'flutter_chat_ui',
|
||||
))
|
||||
.commit();
|
||||
|
||||
// Get the enclosing chat component cubit that contains our state
|
||||
// (created by ChatComponentWidget.builder())
|
||||
|
|
@ -216,80 +226,125 @@ class ChatComponentWidget extends StatelessWidget {
|
|||
),
|
||||
Expanded(
|
||||
child: DecoratedBox(
|
||||
decoration: const BoxDecoration(),
|
||||
child: NotificationListener<ScrollNotification>(
|
||||
onNotification: (notification) {
|
||||
if (chatComponentCubit.scrollOffset != 0) {
|
||||
decoration: const BoxDecoration(),
|
||||
child: NotificationListener<ScrollNotification>(
|
||||
onNotification: (notification) {
|
||||
if (chatComponentCubit.scrollOffset != 0) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!isFirstPage &&
|
||||
notification.metrics.pixels <=
|
||||
((notification.metrics.maxScrollExtent -
|
||||
notification.metrics
|
||||
.minScrollExtent) *
|
||||
(1.0 - onEndReachedThreshold) +
|
||||
notification
|
||||
.metrics.minScrollExtent)) {
|
||||
//
|
||||
final scrollOffset = (notification
|
||||
.metrics.maxScrollExtent -
|
||||
notification.metrics.minScrollExtent) *
|
||||
(1.0 - onEndReachedThreshold);
|
||||
|
||||
chatComponentCubit.scrollOffset = scrollOffset;
|
||||
|
||||
//
|
||||
singleFuture(chatComponentState.chatKey,
|
||||
() async {
|
||||
await _handlePageForward(chatComponentCubit,
|
||||
messageWindow, notification);
|
||||
});
|
||||
} else if (!isLastPage &&
|
||||
notification.metrics.pixels >=
|
||||
((notification.metrics.maxScrollExtent -
|
||||
notification.metrics
|
||||
.minScrollExtent) *
|
||||
onEndReachedThreshold +
|
||||
notification
|
||||
.metrics.minScrollExtent)) {
|
||||
//
|
||||
final scrollOffset = -(notification
|
||||
.metrics.maxScrollExtent -
|
||||
notification.metrics.minScrollExtent) *
|
||||
(1.0 - onEndReachedThreshold);
|
||||
|
||||
chatComponentCubit.scrollOffset = scrollOffset;
|
||||
//
|
||||
singleFuture(chatComponentState.chatKey,
|
||||
() async {
|
||||
await _handlePageBackward(chatComponentCubit,
|
||||
messageWindow, notification);
|
||||
});
|
||||
}
|
||||
return false;
|
||||
}
|
||||
},
|
||||
child: ValueListenableBuilder(
|
||||
valueListenable:
|
||||
chatComponentState.textEditingController,
|
||||
builder: (context, textEditingValue, __) {
|
||||
final messageIsValid = utf8
|
||||
.encode(textEditingValue.text)
|
||||
.lengthInBytes <
|
||||
2048;
|
||||
|
||||
if (!isFirstPage &&
|
||||
notification.metrics.pixels <=
|
||||
((notification.metrics.maxScrollExtent -
|
||||
notification
|
||||
.metrics.minScrollExtent) *
|
||||
(1.0 - onEndReachedThreshold) +
|
||||
notification.metrics.minScrollExtent)) {
|
||||
//
|
||||
final scrollOffset = (notification
|
||||
.metrics.maxScrollExtent -
|
||||
notification.metrics.minScrollExtent) *
|
||||
(1.0 - onEndReachedThreshold);
|
||||
|
||||
chatComponentCubit.scrollOffset = scrollOffset;
|
||||
|
||||
//
|
||||
singleFuture(chatComponentState.chatKey,
|
||||
() async {
|
||||
await _handlePageForward(chatComponentCubit,
|
||||
messageWindow, notification);
|
||||
});
|
||||
} else if (!isLastPage &&
|
||||
notification.metrics.pixels >=
|
||||
((notification.metrics.maxScrollExtent -
|
||||
notification
|
||||
.metrics.minScrollExtent) *
|
||||
onEndReachedThreshold +
|
||||
notification.metrics.minScrollExtent)) {
|
||||
//
|
||||
final scrollOffset = -(notification
|
||||
.metrics.maxScrollExtent -
|
||||
notification.metrics.minScrollExtent) *
|
||||
(1.0 - onEndReachedThreshold);
|
||||
|
||||
chatComponentCubit.scrollOffset = scrollOffset;
|
||||
//
|
||||
singleFuture(chatComponentState.chatKey,
|
||||
() async {
|
||||
await _handlePageBackward(chatComponentCubit,
|
||||
messageWindow, notification);
|
||||
});
|
||||
}
|
||||
return false;
|
||||
},
|
||||
child: Chat(
|
||||
key: chatComponentState.chatKey,
|
||||
theme: chatTheme,
|
||||
messages: messageWindow.window.toList(),
|
||||
scrollToBottomOnSend: isFirstPage,
|
||||
scrollController:
|
||||
chatComponentState.scrollController,
|
||||
// isLastPage: isLastPage,
|
||||
// onEndReached: () async {
|
||||
// await _handlePageBackward(
|
||||
// chatComponentCubit, messageWindow);
|
||||
// },
|
||||
//onEndReachedThreshold: onEndReachedThreshold,
|
||||
//onAttachmentPressed: _handleAttachmentPressed,
|
||||
//onMessageTap: _handleMessageTap,
|
||||
//onPreviewDataFetched: _handlePreviewDataFetched,
|
||||
onSendPressed: (pt) =>
|
||||
_handleSendPressed(chatComponentCubit, pt),
|
||||
//showUserAvatars: false,
|
||||
//showUserNames: true,
|
||||
user: localUser,
|
||||
emptyState: const EmptyChatWidget())),
|
||||
),
|
||||
return Chat(
|
||||
key: chatComponentState.chatKey,
|
||||
theme: messageIsValid
|
||||
? chatTheme
|
||||
: errorChatTheme,
|
||||
messages: messageWindow.window.toList(),
|
||||
scrollToBottomOnSend: isFirstPage,
|
||||
scrollController:
|
||||
chatComponentState.scrollController,
|
||||
inputOptions: InputOptions(
|
||||
inputClearMode: messageIsValid
|
||||
? InputClearMode.always
|
||||
: InputClearMode.never,
|
||||
textEditingController:
|
||||
chatComponentState
|
||||
.textEditingController),
|
||||
// isLastPage: isLastPage,
|
||||
// onEndReached: () async {
|
||||
// await _handlePageBackward(
|
||||
// chatComponentCubit, messageWindow);
|
||||
// },
|
||||
//onEndReachedThreshold: onEndReachedThreshold,
|
||||
//onAttachmentPressed: _handleAttachmentPressed,
|
||||
//onMessageTap: _handleMessageTap,
|
||||
//onPreviewDataFetched: _handlePreviewDataFetched,
|
||||
onSendPressed: (pt) {
|
||||
try {
|
||||
if (!messageIsValid) {
|
||||
showErrorToast(
|
||||
context,
|
||||
translate(
|
||||
'chat.message_too_long'));
|
||||
return;
|
||||
}
|
||||
_handleSendPressed(
|
||||
chatComponentCubit, pt);
|
||||
} on FormatException {
|
||||
showErrorToast(
|
||||
context,
|
||||
translate(
|
||||
'chat.message_too_long'));
|
||||
}
|
||||
},
|
||||
listBottomWidget: messageIsValid
|
||||
? null
|
||||
: Text(
|
||||
translate(
|
||||
'chat.message_too_long'),
|
||||
style: TextStyle(
|
||||
color: scale
|
||||
.errorScale.primary))
|
||||
.toCenter(),
|
||||
//showUserAvatars: false,
|
||||
//showUserNames: true,
|
||||
user: localUser,
|
||||
emptyState: const EmptyChatWidget());
|
||||
}))),
|
||||
),
|
||||
],
|
||||
),
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue