Improve splitting of long message bodies

This commit is contained in:
AsamK 2024-12-26 20:15:19 +01:00
parent 22a0ff976a
commit 11d38f29ef
2 changed files with 23 additions and 13 deletions

View file

@ -20,7 +20,7 @@ public class ServiceConfig {
public static final int MAX_ATTACHMENT_SIZE = 150 * 1024 * 1024; public static final int MAX_ATTACHMENT_SIZE = 150 * 1024 * 1024;
public static final long MAX_ENVELOPE_SIZE = 0; public static final long MAX_ENVELOPE_SIZE = 0;
public static final int MAX_MESSAGE_BODY_SIZE = 2000; public static final int MAX_MESSAGE_SIZE_BYTES = 2000;
public static final long AVATAR_DOWNLOAD_FAILSAFE_MAX_SIZE = 10 * 1024 * 1024; public static final long AVATAR_DOWNLOAD_FAILSAFE_MAX_SIZE = 10 * 1024 * 1024;
public static final boolean AUTOMATIC_NETWORK_RETRY = true; public static final boolean AUTOMATIC_NETWORK_RETRY = true;
public static final int GROUP_MAX_SIZE = 1001; public static final int GROUP_MAX_SIZE = 1001;

View file

@ -68,7 +68,6 @@ import org.asamk.signal.manager.api.UserStatus;
import org.asamk.signal.manager.api.UsernameLinkUrl; import org.asamk.signal.manager.api.UsernameLinkUrl;
import org.asamk.signal.manager.api.UsernameStatus; import org.asamk.signal.manager.api.UsernameStatus;
import org.asamk.signal.manager.api.VerificationMethodNotAvailableException; import org.asamk.signal.manager.api.VerificationMethodNotAvailableException;
import org.asamk.signal.manager.config.ServiceConfig;
import org.asamk.signal.manager.config.ServiceEnvironmentConfig; import org.asamk.signal.manager.config.ServiceEnvironmentConfig;
import org.asamk.signal.manager.helper.AccountFileUpdater; import org.asamk.signal.manager.helper.AccountFileUpdater;
import org.asamk.signal.manager.helper.Context; import org.asamk.signal.manager.helper.Context;
@ -140,6 +139,10 @@ import java.util.stream.Stream;
import io.reactivex.rxjava3.disposables.CompositeDisposable; import io.reactivex.rxjava3.disposables.CompositeDisposable;
import io.reactivex.rxjava3.schedulers.Schedulers; import io.reactivex.rxjava3.schedulers.Schedulers;
import okio.Utf8;
import static org.asamk.signal.manager.config.ServiceConfig.MAX_MESSAGE_SIZE_BYTES;
import static org.signal.core.util.StringExtensionsKt.splitByByteLength;
public class ManagerImpl implements Manager { public class ManagerImpl implements Manager {
@ -763,17 +766,24 @@ public class ManagerImpl implements Manager {
final Message message final Message message
) throws AttachmentInvalidException, IOException, UnregisteredRecipientException, InvalidStickerException { ) throws AttachmentInvalidException, IOException, UnregisteredRecipientException, InvalidStickerException {
final var additionalAttachments = new ArrayList<SignalServiceAttachment>(); final var additionalAttachments = new ArrayList<SignalServiceAttachment>();
if (message.messageText().length() > ServiceConfig.MAX_MESSAGE_BODY_SIZE) { if (Utf8.size(message.messageText()) > MAX_MESSAGE_SIZE_BYTES) {
final var messageBytes = message.messageText().getBytes(StandardCharsets.UTF_8); final var result = splitByByteLength(message.messageText(), MAX_MESSAGE_SIZE_BYTES);
final var uploadSpec = dependencies.getMessageSender().getResumableUploadSpec(); final var trimmed = result.getFirst();
final var streamDetails = new StreamDetails(new ByteArrayInputStream(messageBytes), final var remainder = result.getSecond();
MimeUtils.LONG_TEXT, if (remainder != null) {
messageBytes.length); final var messageBytes = message.messageText().getBytes(StandardCharsets.UTF_8);
final var textAttachment = AttachmentUtils.createAttachmentStream(streamDetails, final var uploadSpec = dependencies.getMessageSender().getResumableUploadSpec();
Optional.empty(), final var streamDetails = new StreamDetails(new ByteArrayInputStream(messageBytes),
uploadSpec); MimeUtils.LONG_TEXT,
messageBuilder.withBody(message.messageText().substring(0, ServiceConfig.MAX_MESSAGE_BODY_SIZE)); messageBytes.length);
additionalAttachments.add(context.getAttachmentHelper().uploadAttachment(textAttachment)); final var textAttachment = AttachmentUtils.createAttachmentStream(streamDetails,
Optional.empty(),
uploadSpec);
messageBuilder.withBody(trimmed);
additionalAttachments.add(context.getAttachmentHelper().uploadAttachment(textAttachment));
} else {
messageBuilder.withBody(message.messageText());
}
} else { } else {
messageBuilder.withBody(message.messageText()); messageBuilder.withBody(message.messageText());
} }