From ab77ad80d17490064ae1f55c1adebc35c7ccd8e5 Mon Sep 17 00:00:00 2001 From: Sarah Jamie Lewis Date: Thu, 23 Jun 2022 11:25:26 -0700 Subject: [PATCH 1/2] Fix Bugs in Quoted Message Selection found by Fuzzbot --- lib/models/messagecache.dart | 4 ++++ lib/widgets/quotedmessage.dart | 11 +++++++---- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/lib/models/messagecache.dart b/lib/models/messagecache.dart index b2c348ce..59c83b0c 100644 --- a/lib/models/messagecache.dart +++ b/lib/models/messagecache.dart @@ -118,6 +118,10 @@ class MessageCache extends ChangeNotifier { return cache[id]; } + int findIndex(int id) { + return cacheByIndex.indexWhere((element) => element.messageId == id); + } + MessageInfo? getByContentHash(String contenthash) => cache[cacheByHash[contenthash]]; void addNew(String profileOnion, int conversation, int messageID, DateTime timestamp, String senderHandle, String senderImage, bool isAuto, String data, String contenthash) { diff --git a/lib/widgets/quotedmessage.dart b/lib/widgets/quotedmessage.dart index 14f9be72..51c157be 100644 --- a/lib/widgets/quotedmessage.dart +++ b/lib/widgets/quotedmessage.dart @@ -87,10 +87,13 @@ class QuotedMessageBubbleState extends State { cursor: SystemMouseCursors.click, child: GestureDetector( onTap: () { - var index = Provider.of(context, listen: false).messageCache.cacheByHash[qMessage.getMetadata().contenthash]; - var totalMessages = Provider.of(context, listen: false).totalMessages; - // we have to reverse here because the list itself is reversed... - Provider.of(context).messageScrollController.scrollTo(index: totalMessages - index!, duration: Duration(milliseconds: 100)); + var messageInfo = Provider.of(context, listen: false).messageCache.getByContentHash(qMessage.getMetadata().contenthash); + if (messageInfo != null) { + var index = Provider.of(context, listen: false).messageCache.findIndex(messageInfo.metadata.messageID); + if (index != null) { + Provider.of(context, listen: false).messageScrollController.scrollTo(index: index, duration: Duration(milliseconds: 100)); + } + } }, child: Container( margin: EdgeInsets.all(5), From 5770eb4b66b15a8caef294b0c5492bec98e9fcf8 Mon Sep 17 00:00:00 2001 From: Sarah Jamie Lewis Date: Thu, 23 Jun 2022 12:07:39 -0700 Subject: [PATCH 2/2] Fix File Bubble Preview Cache Issues + Wrap Quoted Message Malformed Bubbles as Rows --- lib/models/messagecache.dart | 3 --- lib/models/messages/quotedmessage.dart | 5 +++-- lib/widgets/filebubble.dart | 2 +- lib/widgets/messagelist.dart | 8 ++++---- lib/widgets/quotedmessage.dart | 1 - 5 files changed, 8 insertions(+), 11 deletions(-) diff --git a/lib/models/messagecache.dart b/lib/models/messagecache.dart index 59c83b0c..0145689d 100644 --- a/lib/models/messagecache.dart +++ b/lib/models/messagecache.dart @@ -1,8 +1,5 @@ import 'dart:async'; -import 'dart:ffi'; - import 'package:flutter/foundation.dart'; - import 'message.dart'; // we only count up to 100 unread messages, if more than that we can't accurately resync message cache, just reset diff --git a/lib/models/messages/quotedmessage.dart b/lib/models/messages/quotedmessage.dart index 084953eb..471ed536 100644 --- a/lib/models/messages/quotedmessage.dart +++ b/lib/models/messages/quotedmessage.dart @@ -1,6 +1,7 @@ import 'dart:convert'; import 'package:cwtch/models/message.dart'; +import 'package:cwtch/models/messages/malformedmessage.dart'; import 'package:cwtch/widgets/malformedbubble.dart'; import 'package:cwtch/widgets/messagerow.dart'; import 'package:cwtch/widgets/quotedmessage.dart'; @@ -54,7 +55,7 @@ class QuotedMessage extends Message { dynamic message = jsonDecode(this.content); if (message["body"] == null || message["quotedHash"] == null) { - return MalformedBubble(); + return MalformedMessage(this.metadata).getWidget(context, key, index); } return ChangeNotifierProvider.value( @@ -64,7 +65,7 @@ class QuotedMessage extends Message { key: key); }); } catch (e) { - return MalformedBubble(); + return MalformedMessage(this.metadata).getWidget(context, key, index); } } } diff --git a/lib/widgets/filebubble.dart b/lib/widgets/filebubble.dart index e533eee8..e44990eb 100644 --- a/lib/widgets/filebubble.dart +++ b/lib/widgets/filebubble.dart @@ -86,7 +86,7 @@ class FileBubbleState extends State { if (downloadComplete && path != null) { var lpath = path.toLowerCase(); if (lpath.endsWith(".jpg") || lpath.endsWith(".jpeg") || lpath.endsWith(".png") || lpath.endsWith(".gif") || lpath.endsWith(".webp") || lpath.endsWith(".bmp")) { - if (myFile == null) { + if (myFile == null || myFile?.path != path) { setState(() { myFile = new File(path!); diff --git a/lib/widgets/messagelist.dart b/lib/widgets/messagelist.dart index bc50376b..449acee5 100644 --- a/lib/widgets/messagelist.dart +++ b/lib/widgets/messagelist.dart @@ -85,19 +85,19 @@ class _MessageListState extends State { itemCount: Provider.of(outerContext).totalMessages, reverse: true, // NOTE: There seems to be a bug in flutter that corrects the mouse wheel scroll, but not the drag direction... itemBuilder: (itemBuilderContext, index) { - var profileOnion = Provider.of(outerContext, listen: false).onion; - var contactHandle = Provider.of(outerContext, listen: false).identifier; + var profileOnion = Provider.of(itemBuilderContext, listen: false).onion; + var contactHandle = Provider.of(itemBuilderContext, listen: false).identifier; var messageIndex = index; return FutureBuilder( - future: messageHandler(outerContext, profileOnion, contactHandle, ByIndex(messageIndex)), + future: messageHandler(itemBuilderContext, profileOnion, contactHandle, ByIndex(messageIndex)), builder: (context, snapshot) { if (snapshot.hasData) { var message = snapshot.data as Message; // here we create an index key for the contact and assign it to the row. Indexes are unique so we can // reliably use this without running into duplicate keys...it isn't ideal as it means keys need to be re-built // when new messages are added...however it is better than the alternative of not having widget keys at all. - var key = Provider.of(outerContext, listen: false).getMessageKey(contactHandle, messageIndex); + var key = Provider.of(itemBuilderContext, listen: false).getMessageKey(contactHandle, messageIndex); return message.getWidget(context, key, messageIndex); } else { return MessageLoadingBubble(); diff --git a/lib/widgets/quotedmessage.dart b/lib/widgets/quotedmessage.dart index 51c157be..038d9052 100644 --- a/lib/widgets/quotedmessage.dart +++ b/lib/widgets/quotedmessage.dart @@ -114,7 +114,6 @@ class QuotedMessageBubbleState extends State { )) ])))); } catch (e) { - print(e); return MalformedBubble(); } } else {