messages wip

This commit is contained in:
Christien Rioux 2024-05-27 18:04:00 -04:00
parent 17f6dfce46
commit 9c5feed732
18 changed files with 274 additions and 171 deletions

View file

@ -4,7 +4,7 @@ import 'package:veilid_support/veilid_support.dart';
class ActiveChatCubit extends Cubit<TypedKey?> {
ActiveChatCubit(super.initialState);
void setActiveChat(TypedKey? activeChatRemoteConversationRecordKey) {
emit(activeChatRemoteConversationRecordKey);
void setActiveChat(TypedKey? activeChatLocalConversationRecordKey) {
emit(activeChatLocalConversationRecordKey);
}
}

View file

@ -53,14 +53,12 @@ class SingleContactMessagesCubit extends Cubit<SingleContactMessagesState> {
required TypedKey localMessagesRecordKey,
required TypedKey remoteConversationRecordKey,
required TypedKey remoteMessagesRecordKey,
required OwnedDHTRecordPointer reconciledChatRecord,
}) : _activeAccountInfo = activeAccountInfo,
_remoteIdentityPublicKey = remoteIdentityPublicKey,
_localConversationRecordKey = localConversationRecordKey,
_localMessagesRecordKey = localMessagesRecordKey,
_remoteConversationRecordKey = remoteConversationRecordKey,
_remoteMessagesRecordKey = remoteMessagesRecordKey,
_reconciledChatRecord = reconciledChatRecord,
super(const AsyncValue.loading()) {
// Async Init
_initWait.add(_init);
@ -420,7 +418,14 @@ class SingleContactMessagesCubit extends Cubit<SingleContactMessagesState> {
emit(AsyncValue.data(renderedState));
}
void addMessage({required proto.Message message}) {
void addTextMessage({required proto.Message_Text messageText}) {
final message = proto.Message()
..author = _activeAccountInfo.localAccount.identityMaster
.identityPublicTypedKey()
.toProto()
..timestamp = Veilid.instance.now().toInt64()
..text = messageText;
_unreconciledMessagesQueue.addSync(message);
_sendingMessagesQueue.addSync(message);
@ -428,6 +433,21 @@ class SingleContactMessagesCubit extends Cubit<SingleContactMessagesState> {
_renderState();
}
/////////////////////////////////////////////////////////////////////////
static Future<void> cleanupAndDeleteMessages(
{required TypedKey localConversationRecordKey}) async {
final recmsgdbname =
_reconciledMessagesTableDBName(localConversationRecordKey);
await Veilid.instance.deleteTableDB(recmsgdbname);
}
static String _reconciledMessagesTableDBName(
TypedKey localConversationRecordKey) =>
'msg_$localConversationRecordKey';
/////////////////////////////////////////////////////////////////////////
final WaitSet<void> _initWait = WaitSet();
final ActiveAccountInfo _activeAccountInfo;
final TypedKey _remoteIdentityPublicKey;
@ -435,7 +455,6 @@ class SingleContactMessagesCubit extends Cubit<SingleContactMessagesState> {
final TypedKey _localMessagesRecordKey;
final TypedKey _remoteConversationRecordKey;
final TypedKey _remoteMessagesRecordKey;
final OwnedDHTRecordPointer _reconciledChatRecord;
late final VeilidCrypto _messagesCrypto;

View file

@ -30,10 +30,28 @@ class MessageState with _$MessageState {
required proto.Message content,
// Received or delivered timestamp
required Timestamp timestamp,
// The state of the mssage
// The state of the message
required MessageSendState? sendState,
}) = _MessageState;
factory MessageState.fromJson(dynamic json) =>
_$MessageStateFromJson(json as Map<String, dynamic>);
}
extension MessageStateExt on MessageState {
String get uniqueId {
final author = content.author.toVeilid().toString();
final id = base64UrlNoPadEncode(content.id);
return '$author|$id';
}
static (proto.TypedKey, Uint8List) splitUniqueId(String uniqueId) {
final parts = uniqueId.split('|');
if (parts.length != 2) {
throw Exception('invalid unique id');
}
final author = TypedKey.fromString(parts[0]).toProto();
final id = base64UrlNoPadDecode(parts[1]);
return (author, id);
}
}

View file

@ -25,7 +25,7 @@ mixin _$MessageState {
proto.Message get content =>
throw _privateConstructorUsedError; // Received or delivered timestamp
Timestamp get timestamp =>
throw _privateConstructorUsedError; // The state of the mssage
throw _privateConstructorUsedError; // The state of the message
MessageSendState? get sendState => throw _privateConstructorUsedError;
Map<String, dynamic> toJson() => throw _privateConstructorUsedError;
@ -147,7 +147,7 @@ class _$MessageStateImpl with DiagnosticableTreeMixin implements _MessageState {
// Received or delivered timestamp
@override
final Timestamp timestamp;
// The state of the mssage
// The state of the message
@override
final MessageSendState? sendState;
@ -211,7 +211,7 @@ abstract class _MessageState implements MessageState {
proto.Message get content;
@override // Received or delivered timestamp
Timestamp get timestamp;
@override // The state of the mssage
@override // The state of the message
MessageSendState? get sendState;
@override
@JsonKey(ignore: true)

View file

@ -1,5 +1,8 @@
import 'dart:typed_data';
import 'package:async_tools/async_tools.dart';
import 'package:awesome_extensions/awesome_extensions.dart';
import 'package:fixnum/fixnum.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flutter_chat_types/flutter_chat_types.dart' as types;
@ -13,6 +16,10 @@ import '../../proto/proto.dart' as proto;
import '../../theme/theme.dart';
import '../chat.dart';
const String metadataKeyExpirationDuration = 'expiration';
const String metadataKeyViewLimit = 'view_limit';
const String metadataKeyAttachments = 'attachments';
class ChatComponent extends StatelessWidget {
const ChatComponent._(
{required TypedKey localUserIdentityKey,
@ -35,7 +42,7 @@ class ChatComponent extends StatelessWidget {
// Builder wrapper function that takes care of state management requirements
static Widget builder(
{required TypedKey remoteConversationRecordKey, Key? key}) =>
{required TypedKey localConversationRecordKey, Key? key}) =>
Builder(builder: (context) {
// Get all watched dependendies
final activeAccountInfo = context.watch<ActiveAccountInfo>();
@ -51,7 +58,7 @@ class ChatComponent extends StatelessWidget {
}
final avconversation = context.select<ActiveConversationsBlocMapCubit,
AsyncValue<ActiveConversationState>?>(
(x) => x.state[remoteConversationRecordKey]);
(x) => x.state[localConversationRecordKey]);
if (avconversation == null) {
return waitingPage();
}
@ -77,7 +84,7 @@ class ChatComponent extends StatelessWidget {
// Get the messages cubit
final messages = context.select<ActiveSingleContactChatBlocMapCubit,
(SingleContactMessagesCubit, SingleContactMessagesState)?>(
(x) => x.tryOperate(remoteConversationRecordKey,
(x) => x.tryOperate(localConversationRecordKey,
closure: (cubit) => (cubit, cubit.state)));
// Get the messages to display
@ -97,8 +104,8 @@ class ChatComponent extends StatelessWidget {
/////////////////////////////////////////////////////////////////////
types.Message messageToChatMessage(MessageState message) {
final isLocal = message.author == _localUserIdentityKey;
types.Message? messageToChatMessage(MessageState message) {
final isLocal = message.content.author.toVeilid() == _localUserIdentityKey;
types.Status? status;
if (message.sendState != null) {
@ -113,31 +120,83 @@ class ChatComponent extends StatelessWidget {
}
}
final textMessage = types.TextMessage(
author: isLocal ? _localUser : _remoteUser,
createdAt: (message.timestamp.value ~/ BigInt.from(1000)).toInt(),
id: message.timestamp.toString(),
text: message.text,
showStatus: status != null,
status: status);
return textMessage;
switch (message.content.whichKind()) {
case proto.Message_Kind.text:
final contextText = message.content.text;
final textMessage = types.TextMessage(
author: isLocal ? _localUser : _remoteUser,
createdAt: (message.timestamp.value ~/ BigInt.from(1000)).toInt(),
id: message.uniqueId,
text: contextText.text,
showStatus: status != null,
status: status);
return textMessage;
case proto.Message_Kind.secret:
case proto.Message_Kind.delete:
case proto.Message_Kind.erase:
case proto.Message_Kind.settings:
case proto.Message_Kind.permissions:
case proto.Message_Kind.membership:
case proto.Message_Kind.moderation:
case proto.Message_Kind.notSet:
return null;
}
}
void _addMessage(proto.Message message) {
if (message.text.isEmpty) {
return;
void _addTextMessage(
{required String text,
String? topic,
Uint8List? replyId,
Timestamp? expiration,
int? viewLimit,
List<proto.Attachment> attachments = const []}) {
final protoMessageText = proto.Message_Text()..text = text;
if (topic != null) {
protoMessageText.topic = topic;
}
_messagesCubit.addMessage(message: message);
if (replyId != null) {
protoMessageText.replyId = replyId;
}
protoMessageText
..expiration = expiration?.toInt64() ?? Int64.ZERO
..viewLimit = viewLimit ?? 0;
protoMessageText.attachments.addAll(attachments);
_messagesCubit.addTextMessage(messageText: protoMessageText);
}
void _handleSendPressed(types.PartialText message) {
final protoMessage = proto.Message()
..author = _localUserIdentityKey.toProto()
..timestamp = Veilid.instance.now().toInt64()
..text = message.text;
//..signature = signature;
final text = message.text;
final replyId = (message.repliedMessage != null)
? MessageStateExt.splitUniqueId(message.repliedMessage!.id).$2
: null;
Timestamp? expiration;
int? viewLimit;
List<proto.Attachment>? attachments;
final metadata = message.metadata;
if (metadata != null) {
final expirationValue =
metadata[metadataKeyExpirationDuration] as TimestampDuration?;
if (expirationValue != null) {
expiration = Veilid.instance.now().offset(expirationValue);
}
final viewLimitValue = metadata[metadataKeyViewLimit] as int?;
if (viewLimitValue != null) {
viewLimit = viewLimitValue;
}
final attachmentsValue =
metadata[metadataKeyAttachments] as List<proto.Attachment>?;
if (attachmentsValue != null) {
attachments = attachmentsValue;
}
}
_addMessage(protoMessage);
_addTextMessage(
text: text,
replyId: replyId,
expiration: expiration,
viewLimit: viewLimit,
attachments: attachments ?? []);
}
// void _handleAttachmentPressed() async {
@ -161,6 +220,9 @@ class ChatComponent extends StatelessWidget {
final tsSet = <String>{};
for (final message in messages) {
final chatMessage = messageToChatMessage(message);
if (chatMessage == null) {
continue;
}
chatMessages.insert(0, chatMessage);
if (!tsSet.add(chatMessage.id)) {
// ignore: avoid_print