Never silently truncate the draft when attaching an IPFS link
Check before uploading that the link (from the gateway and a CIDv1-sized placeholder) fits with the draft and the reply prefix, and tell the user when it doesn't instead of cutting their text. The channel limit now counts the @[name] reply prefix like the send path.
This commit is contained in:
1 parent
084a8fb98b
commit
34db64d7af
25 files changed
+315
-103
No files matched your search
@@ -14,8 +14,10 @@ import 'snack_bar_builder.dart';
|
||||
/// puts the resulting link at the start of the composer draft.
|
||||
///
|
||||
/// [maxBytes] is the screen's outbound byte limit and [encodeOutbound] turns
|
||||
/// draft text into what the radio would actually send, so the limit is
|
||||
/// checked against the encoded form.
|
||||
/// draft text into exactly what the send path measures (including any reply
|
||||
/// prefix), so the limit is checked against that form. The draft is never
|
||||
/// shortened: if the link won't fit, nothing is uploaded and the user is told
|
||||
/// to shorten the message.
|
||||
Future<void> attachIpfsLinkToDraft({
|
||||
required BuildContext context,
|
||||
required TextEditingController controller,
|
||||
@@ -33,6 +35,25 @@ Future<void> attachIpfsLinkToDraft({
|
||||
return;
|
||||
}
|
||||
|
||||
String draftWithLink(String messageUrl) => composeIpfsLinkDraft(
|
||||
messageUrl: messageUrl,
|
||||
existingText: controller.text.trim(),
|
||||
);
|
||||
bool fits(String draft) => ipfsLinkDraftFits(
|
||||
draft,
|
||||
maxBytes: maxBytes,
|
||||
encodeOutbound: encodeOutbound,
|
||||
);
|
||||
|
||||
final sampleLink = IpfsUploadHelper.linkForCid(
|
||||
uploadGateway: settings.uploadIpfsGateway,
|
||||
cid: IpfsUploadHelper.sampleCid,
|
||||
);
|
||||
if (!fits(draftWithLink(sampleLink))) {
|
||||
showDismissibleSnackBar(context, content: Text(l10n.chat_ipfsLinkNoRoom));
|
||||
return;
|
||||
}
|
||||
|
||||
void showFailure(Object error) {
|
||||
if (!context.mounted) return;
|
||||
showDismissibleSnackBar(
|
||||
@@ -73,50 +94,31 @@ Future<void> attachIpfsLinkToDraft({
|
||||
}
|
||||
if (!context.mounted) return;
|
||||
|
||||
final messageText = composeIpfsLinkDraft(
|
||||
messageUrl: messageUrl,
|
||||
existingText: controller.text.trim(),
|
||||
maxBytes: maxBytes,
|
||||
encodeOutbound: encodeOutbound,
|
||||
);
|
||||
if (messageText == null) {
|
||||
showDismissibleSnackBar(
|
||||
context,
|
||||
content: Text(l10n.chat_messageTooLong(maxBytes)),
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
final messageText = draftWithLink(messageUrl);
|
||||
controller.value = TextEditingValue(
|
||||
text: messageText,
|
||||
selection: TextSelection.collapsed(offset: messageText.length),
|
||||
);
|
||||
focusNode.requestFocus();
|
||||
if (!fits(messageText)) {
|
||||
showDismissibleSnackBar(
|
||||
context,
|
||||
content: Text(l10n.chat_ipfsLinkOverLimit),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/// Builds the draft with [messageUrl] in front of [existingText], trimming
|
||||
/// [existingText] from the end until the encoded result fits [maxBytes].
|
||||
/// Returns null when even the link alone does not fit.
|
||||
/// Puts [messageUrl] in front of [existingText], leaving the text intact.
|
||||
@visibleForTesting
|
||||
String? composeIpfsLinkDraft({
|
||||
String composeIpfsLinkDraft({
|
||||
required String messageUrl,
|
||||
required String existingText,
|
||||
}) => existingText.isEmpty ? '$messageUrl ' : '$messageUrl\n$existingText';
|
||||
|
||||
/// Whether [draft], encoded the way the send path encodes it, fits [maxBytes].
|
||||
@visibleForTesting
|
||||
bool ipfsLinkDraftFits(
|
||||
String draft, {
|
||||
required int maxBytes,
|
||||
required String Function(String text) encodeOutbound,
|
||||
}) {
|
||||
bool fits(String text) =>
|
||||
utf8.encode(encodeOutbound(text)).length <= maxBytes;
|
||||
|
||||
final messageText = existingText.isEmpty
|
||||
? '$messageUrl '
|
||||
: '$messageUrl\n$existingText';
|
||||
if (fits(messageText)) return messageText;
|
||||
if (existingText.isEmpty) return null;
|
||||
|
||||
final originalCharacters = existingText.characters.toList();
|
||||
for (var count = originalCharacters.length - 1; count >= 0; count--) {
|
||||
final candidate = '$messageUrl\n${originalCharacters.take(count).join()}';
|
||||
if (fits(candidate)) return candidate;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}) => utf8.encode(encodeOutbound(draft.trim())).length <= maxBytes;
|
||||
@@ -22,6 +22,19 @@ class IpfsUploadHelper {
|
||||
|
||||
static final _kuboAddPath = RegExp(r'/api/v0/add/?$');
|
||||
|
||||
/// A CIDv1 (base32, sha2-256) as long as the CID of every upload, used to
|
||||
/// size the link before uploading.
|
||||
static const sampleCid =
|
||||
'bafkreihdwdcefgh4dqkjv67uzcmw7ojee6xedzdetojuzjevtenxquvyku';
|
||||
|
||||
static String linkForCid({
|
||||
required String uploadGateway,
|
||||
required String cid,
|
||||
}) {
|
||||
final gateway = normalizeIpfsGateway(uploadGateway);
|
||||
return gateway.isEmpty ? 'ipfs://$cid' : '$gateway$cid';
|
||||
}
|
||||
|
||||
static String mimeTypeForFileName(String fileName) {
|
||||
switch (fileName.toLowerCase().split('.').last) {
|
||||
case 'jpg':
|
||||
@@ -149,8 +162,7 @@ class IpfsUploadHelper {
|
||||
);
|
||||
}
|
||||
|
||||
final gateway = normalizeIpfsGateway(uploadGateway);
|
||||
return gateway.isEmpty ? 'ipfs://$cid' : '$gateway$cid';
|
||||
return linkForCid(uploadGateway: uploadGateway, cid: cid);
|
||||
}
|
||||
|
||||
static Future<void> _copyKuboFileToMfs({
|
||||
|
||||
Reference in new issue
Block a user