Refactor message send methods

This commit is contained in:
AsamK 2021-08-24 12:36:09 +02:00
parent 23a006c311
commit cd7172ee57
3 changed files with 45 additions and 20 deletions

View file

@ -17,6 +17,7 @@
package org.asamk.signal.manager;
import org.asamk.signal.manager.api.Device;
import org.asamk.signal.manager.api.Message;
import org.asamk.signal.manager.api.TypingAction;
import org.asamk.signal.manager.config.ServiceConfig;
import org.asamk.signal.manager.config.ServiceEnvironment;
@ -697,12 +698,10 @@ public class Manager implements Closeable {
}
public Pair<Long, List<SendMessageResult>> sendGroupMessage(
String messageText, List<String> attachments, GroupId groupId
Message message, GroupId groupId
) throws IOException, GroupNotFoundException, AttachmentInvalidException, NotAGroupMemberException {
final var messageBuilder = createMessageBuilder().withBody(messageText);
if (attachments != null) {
messageBuilder.withAttachments(AttachmentUtils.getSignalServiceAttachments(attachments));
}
final var messageBuilder = createMessageBuilder();
applyMessage(messageBuilder, message);
return sendHelper.sendAsGroupMessage(messageBuilder, groupId);
}
@ -1230,11 +1229,19 @@ public class Manager implements Closeable {
}
public Pair<Long, List<SendMessageResult>> sendMessage(
String messageText, List<String> attachments, List<String> recipients
Message message, List<String> recipients
) throws IOException, AttachmentInvalidException, InvalidNumberException {
final var messageBuilder = createMessageBuilder().withBody(messageText);
if (attachments != null) {
var attachmentStreams = AttachmentUtils.getSignalServiceAttachments(attachments);
final var messageBuilder = createMessageBuilder();
applyMessage(messageBuilder, message);
return sendHelper.sendMessage(messageBuilder, getRecipientIds(recipients));
}
private void applyMessage(
final SignalServiceDataMessage.Builder messageBuilder, final Message message
) throws AttachmentInvalidException, IOException {
messageBuilder.withBody(message.getMessageText());
if (message.getAttachments() != null) {
var attachmentStreams = AttachmentUtils.getSignalServiceAttachments(message.getAttachments());
// Upload attachments here, so we only upload once even for multiple recipients
var messageSender = dependencies.getMessageSender();
@ -1249,16 +1256,11 @@ public class Manager implements Closeable {
messageBuilder.withAttachments(attachmentPointers);
}
return sendHelper.sendMessage(messageBuilder, getRecipientIds(recipients));
}
public Pair<Long, SendMessageResult> sendSelfMessage(
String messageText, List<String> attachments
) throws IOException, AttachmentInvalidException {
final var messageBuilder = createMessageBuilder().withBody(messageText);
if (attachments != null) {
messageBuilder.withAttachments(AttachmentUtils.getSignalServiceAttachments(attachments));
}
public Pair<Long, SendMessageResult> sendSelfMessage(final Message message) throws IOException, AttachmentInvalidException {
final var messageBuilder = createMessageBuilder();
applyMessage(messageBuilder, message);
return sendHelper.sendSelfMessage(messageBuilder);
}

View file

@ -0,0 +1,22 @@
package org.asamk.signal.manager.api;
import java.util.List;
public class Message {
private final String messageText;
private final List<String> attachments;
public Message(final String messageText, final List<String> attachments) {
this.messageText = messageText;
this.attachments = attachments;
}
public String getMessageText() {
return messageText;
}
public List<String> getAttachments() {
return attachments;
}
}

View file

@ -5,6 +5,7 @@ import org.asamk.signal.BaseConfig;
import org.asamk.signal.manager.AttachmentInvalidException;
import org.asamk.signal.manager.Manager;
import org.asamk.signal.manager.NotMasterDeviceException;
import org.asamk.signal.manager.api.Message;
import org.asamk.signal.manager.groups.GroupId;
import org.asamk.signal.manager.groups.GroupInviteLinkUrl;
import org.asamk.signal.manager.groups.GroupNotFoundException;
@ -100,7 +101,7 @@ public class DbusSignalImpl implements Signal {
@Override
public long sendMessage(final String message, final List<String> attachments, final List<String> recipients) {
try {
final var results = m.sendMessage(message, attachments, recipients);
final var results = m.sendMessage(new Message(message, attachments), recipients);
checkSendMessageResults(results.first(), results.second());
return results.first();
} catch (InvalidNumberException e) {
@ -188,7 +189,7 @@ public class DbusSignalImpl implements Signal {
final String message, final List<String> attachments
) throws Error.AttachmentInvalid, Error.Failure, Error.UntrustedIdentity {
try {
final var results = m.sendSelfMessage(message, attachments);
final var results = m.sendSelfMessage(new Message(message, attachments));
checkSendMessageResult(results.first(), results.second());
return results.first();
} catch (AttachmentInvalidException e) {
@ -213,7 +214,7 @@ public class DbusSignalImpl implements Signal {
@Override
public long sendGroupMessage(final String message, final List<String> attachments, final byte[] groupId) {
try {
var results = m.sendGroupMessage(message, attachments, GroupId.unknownVersion(groupId));
var results = m.sendGroupMessage(new Message(message, attachments), GroupId.unknownVersion(groupId));
checkSendMessageResults(results.first(), results.second());
return results.first();
} catch (IOException e) {