mirror of
https://github.com/AsamK/signal-cli
synced 2025-08-29 10:30:38 +00:00
Refactor message send methods
This commit is contained in:
parent
23a006c311
commit
cd7172ee57
3 changed files with 45 additions and 20 deletions
|
@ -17,6 +17,7 @@
|
||||||
package org.asamk.signal.manager;
|
package org.asamk.signal.manager;
|
||||||
|
|
||||||
import org.asamk.signal.manager.api.Device;
|
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.api.TypingAction;
|
||||||
import org.asamk.signal.manager.config.ServiceConfig;
|
import org.asamk.signal.manager.config.ServiceConfig;
|
||||||
import org.asamk.signal.manager.config.ServiceEnvironment;
|
import org.asamk.signal.manager.config.ServiceEnvironment;
|
||||||
|
@ -697,12 +698,10 @@ public class Manager implements Closeable {
|
||||||
}
|
}
|
||||||
|
|
||||||
public Pair<Long, List<SendMessageResult>> sendGroupMessage(
|
public Pair<Long, List<SendMessageResult>> sendGroupMessage(
|
||||||
String messageText, List<String> attachments, GroupId groupId
|
Message message, GroupId groupId
|
||||||
) throws IOException, GroupNotFoundException, AttachmentInvalidException, NotAGroupMemberException {
|
) throws IOException, GroupNotFoundException, AttachmentInvalidException, NotAGroupMemberException {
|
||||||
final var messageBuilder = createMessageBuilder().withBody(messageText);
|
final var messageBuilder = createMessageBuilder();
|
||||||
if (attachments != null) {
|
applyMessage(messageBuilder, message);
|
||||||
messageBuilder.withAttachments(AttachmentUtils.getSignalServiceAttachments(attachments));
|
|
||||||
}
|
|
||||||
|
|
||||||
return sendHelper.sendAsGroupMessage(messageBuilder, groupId);
|
return sendHelper.sendAsGroupMessage(messageBuilder, groupId);
|
||||||
}
|
}
|
||||||
|
@ -1230,11 +1229,19 @@ public class Manager implements Closeable {
|
||||||
}
|
}
|
||||||
|
|
||||||
public Pair<Long, List<SendMessageResult>> sendMessage(
|
public Pair<Long, List<SendMessageResult>> sendMessage(
|
||||||
String messageText, List<String> attachments, List<String> recipients
|
Message message, List<String> recipients
|
||||||
) throws IOException, AttachmentInvalidException, InvalidNumberException {
|
) throws IOException, AttachmentInvalidException, InvalidNumberException {
|
||||||
final var messageBuilder = createMessageBuilder().withBody(messageText);
|
final var messageBuilder = createMessageBuilder();
|
||||||
if (attachments != null) {
|
applyMessage(messageBuilder, message);
|
||||||
var attachmentStreams = AttachmentUtils.getSignalServiceAttachments(attachments);
|
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
|
// Upload attachments here, so we only upload once even for multiple recipients
|
||||||
var messageSender = dependencies.getMessageSender();
|
var messageSender = dependencies.getMessageSender();
|
||||||
|
@ -1249,16 +1256,11 @@ public class Manager implements Closeable {
|
||||||
|
|
||||||
messageBuilder.withAttachments(attachmentPointers);
|
messageBuilder.withAttachments(attachmentPointers);
|
||||||
}
|
}
|
||||||
return sendHelper.sendMessage(messageBuilder, getRecipientIds(recipients));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public Pair<Long, SendMessageResult> sendSelfMessage(
|
public Pair<Long, SendMessageResult> sendSelfMessage(final Message message) throws IOException, AttachmentInvalidException {
|
||||||
String messageText, List<String> attachments
|
final var messageBuilder = createMessageBuilder();
|
||||||
) throws IOException, AttachmentInvalidException {
|
applyMessage(messageBuilder, message);
|
||||||
final var messageBuilder = createMessageBuilder().withBody(messageText);
|
|
||||||
if (attachments != null) {
|
|
||||||
messageBuilder.withAttachments(AttachmentUtils.getSignalServiceAttachments(attachments));
|
|
||||||
}
|
|
||||||
return sendHelper.sendSelfMessage(messageBuilder);
|
return sendHelper.sendSelfMessage(messageBuilder);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
22
lib/src/main/java/org/asamk/signal/manager/api/Message.java
Normal file
22
lib/src/main/java/org/asamk/signal/manager/api/Message.java
Normal 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;
|
||||||
|
}
|
||||||
|
}
|
|
@ -5,6 +5,7 @@ import org.asamk.signal.BaseConfig;
|
||||||
import org.asamk.signal.manager.AttachmentInvalidException;
|
import org.asamk.signal.manager.AttachmentInvalidException;
|
||||||
import org.asamk.signal.manager.Manager;
|
import org.asamk.signal.manager.Manager;
|
||||||
import org.asamk.signal.manager.NotMasterDeviceException;
|
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.GroupId;
|
||||||
import org.asamk.signal.manager.groups.GroupInviteLinkUrl;
|
import org.asamk.signal.manager.groups.GroupInviteLinkUrl;
|
||||||
import org.asamk.signal.manager.groups.GroupNotFoundException;
|
import org.asamk.signal.manager.groups.GroupNotFoundException;
|
||||||
|
@ -100,7 +101,7 @@ public class DbusSignalImpl implements Signal {
|
||||||
@Override
|
@Override
|
||||||
public long sendMessage(final String message, final List<String> attachments, final List<String> recipients) {
|
public long sendMessage(final String message, final List<String> attachments, final List<String> recipients) {
|
||||||
try {
|
try {
|
||||||
final var results = m.sendMessage(message, attachments, recipients);
|
final var results = m.sendMessage(new Message(message, attachments), recipients);
|
||||||
checkSendMessageResults(results.first(), results.second());
|
checkSendMessageResults(results.first(), results.second());
|
||||||
return results.first();
|
return results.first();
|
||||||
} catch (InvalidNumberException e) {
|
} catch (InvalidNumberException e) {
|
||||||
|
@ -188,7 +189,7 @@ public class DbusSignalImpl implements Signal {
|
||||||
final String message, final List<String> attachments
|
final String message, final List<String> attachments
|
||||||
) throws Error.AttachmentInvalid, Error.Failure, Error.UntrustedIdentity {
|
) throws Error.AttachmentInvalid, Error.Failure, Error.UntrustedIdentity {
|
||||||
try {
|
try {
|
||||||
final var results = m.sendSelfMessage(message, attachments);
|
final var results = m.sendSelfMessage(new Message(message, attachments));
|
||||||
checkSendMessageResult(results.first(), results.second());
|
checkSendMessageResult(results.first(), results.second());
|
||||||
return results.first();
|
return results.first();
|
||||||
} catch (AttachmentInvalidException e) {
|
} catch (AttachmentInvalidException e) {
|
||||||
|
@ -213,7 +214,7 @@ public class DbusSignalImpl implements Signal {
|
||||||
@Override
|
@Override
|
||||||
public long sendGroupMessage(final String message, final List<String> attachments, final byte[] groupId) {
|
public long sendGroupMessage(final String message, final List<String> attachments, final byte[] groupId) {
|
||||||
try {
|
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());
|
checkSendMessageResults(results.first(), results.second());
|
||||||
return results.first();
|
return results.first();
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue