Add sendMessageRequestResponse command

This commit is contained in:
AsamK 2024-02-18 20:37:20 +01:00
parent 2c0ad7feb7
commit d84362eb0f
11 changed files with 210 additions and 0 deletions

View file

@ -204,6 +204,10 @@ public interface Manager extends Closeable {
SendMessageResults sendEndSessionMessage(Set<RecipientIdentifier.Single> recipients) throws IOException;
SendMessageResults sendMessageRequestResponse(
MessageEnvelope.Sync.MessageRequestResponse.Type type, Set<RecipientIdentifier> recipientIdentifiers
);
void hideRecipient(RecipientIdentifier.Single recipient);
void deleteRecipient(RecipientIdentifier.Single recipient);

View file

@ -2,6 +2,7 @@ package org.asamk.signal.manager.helper;
import org.asamk.signal.manager.api.Contact;
import org.asamk.signal.manager.api.GroupId;
import org.asamk.signal.manager.api.MessageEnvelope;
import org.asamk.signal.manager.api.TrustLevel;
import org.asamk.signal.manager.storage.SignalAccount;
import org.asamk.signal.manager.storage.groups.GroupInfoV1;
@ -28,6 +29,7 @@ import org.whispersystems.signalservice.api.messages.multidevice.DeviceGroup;
import org.whispersystems.signalservice.api.messages.multidevice.DeviceGroupsInputStream;
import org.whispersystems.signalservice.api.messages.multidevice.DeviceGroupsOutputStream;
import org.whispersystems.signalservice.api.messages.multidevice.KeysMessage;
import org.whispersystems.signalservice.api.messages.multidevice.MessageRequestResponseMessage;
import org.whispersystems.signalservice.api.messages.multidevice.RequestMessage;
import org.whispersystems.signalservice.api.messages.multidevice.SignalServiceSyncMessage;
import org.whispersystems.signalservice.api.messages.multidevice.StickerPackOperationMessage;
@ -365,6 +367,25 @@ public class SyncHelper {
}
}
public SendMessageResult sendMessageRequestResponse(
final MessageEnvelope.Sync.MessageRequestResponse.Type type, final GroupId groupId
) {
final var response = MessageRequestResponseMessage.forGroup(groupId.serialize(), localToRemoteType(type));
return context.getSendHelper().sendSyncMessage(SignalServiceSyncMessage.forMessageRequestResponse(response));
}
public SendMessageResult sendMessageRequestResponse(
final MessageEnvelope.Sync.MessageRequestResponse.Type type, final RecipientId recipientId
) {
final var address = account.getRecipientAddressResolver().resolveRecipientAddress(recipientId);
if (address.serviceId().isEmpty()) {
return null;
}
final var response = MessageRequestResponseMessage.forIndividual(address.serviceId().get(),
localToRemoteType(type));
return context.getSendHelper().sendSyncMessage(SignalServiceSyncMessage.forMessageRequestResponse(response));
}
private SendMessageResult requestSyncData(final SyncMessage.Request.Type type) {
var r = new SyncMessage.Request.Builder().type(type).build();
var message = SignalServiceSyncMessage.forRequest(new RequestMessage(r));
@ -389,4 +410,17 @@ public class SyncHelper {
logger.warn("Failed to download avatar for contact {}, ignoring: {}", address, e.getMessage());
}
}
private MessageRequestResponseMessage.Type localToRemoteType(final MessageEnvelope.Sync.MessageRequestResponse.Type type) {
return switch (type) {
case UNKNOWN -> MessageRequestResponseMessage.Type.UNKNOWN;
case ACCEPT -> MessageRequestResponseMessage.Type.ACCEPT;
case DELETE -> MessageRequestResponseMessage.Type.DELETE;
case BLOCK -> MessageRequestResponseMessage.Type.BLOCK;
case BLOCK_AND_DELETE -> MessageRequestResponseMessage.Type.BLOCK_AND_DELETE;
case UNBLOCK_AND_ACCEPT -> MessageRequestResponseMessage.Type.UNBLOCK_AND_ACCEPT;
case SPAM -> MessageRequestResponseMessage.Type.SPAM;
case BLOCK_AND_SPAM -> MessageRequestResponseMessage.Type.BLOCK_AND_SPAM;
};
}
}

View file

@ -38,6 +38,7 @@ import org.asamk.signal.manager.api.InvalidUsernameException;
import org.asamk.signal.manager.api.LastGroupAdminException;
import org.asamk.signal.manager.api.Message;
import org.asamk.signal.manager.api.MessageEnvelope;
import org.asamk.signal.manager.api.MessageEnvelope.Sync.MessageRequestResponse;
import org.asamk.signal.manager.api.NonNormalizedPhoneNumberException;
import org.asamk.signal.manager.api.NotAGroupMemberException;
import org.asamk.signal.manager.api.NotPrimaryDeviceException;
@ -874,6 +875,41 @@ public class ManagerImpl implements Manager {
}
}
@Override
public SendMessageResults sendMessageRequestResponse(
final MessageRequestResponse.Type type, final Set<RecipientIdentifier> recipients
) {
var results = new HashMap<RecipientIdentifier, List<SendMessageResult>>();
for (final var recipient : recipients) {
if (recipient instanceof RecipientIdentifier.NoteToSelf || (
recipient instanceof RecipientIdentifier.Single single
&& new RecipientAddress(single.toPartialRecipientAddress()).matches(account.getSelfRecipientAddress())
)) {
final var result = context.getSyncHelper()
.sendMessageRequestResponse(type, account.getSelfRecipientId());
if (result != null) {
results.put(recipient, List.of(toSendMessageResult(result)));
}
results.put(recipient, List.of(toSendMessageResult(result)));
} else if (recipient instanceof RecipientIdentifier.Single single) {
try {
final var recipientId = context.getRecipientHelper().resolveRecipient(single);
final var result = context.getSyncHelper().sendMessageRequestResponse(type, recipientId);
if (result != null) {
results.put(recipient, List.of(toSendMessageResult(result)));
}
} catch (UnregisteredRecipientException e) {
results.put(recipient,
List.of(SendMessageResult.unregisteredFailure(single.toPartialRecipientAddress())));
}
} else if (recipient instanceof RecipientIdentifier.Group group) {
final var result = context.getSyncHelper().sendMessageRequestResponse(type, group.groupId());
results.put(recipient, List.of(toSendMessageResult(result)));
}
}
return new SendMessageResults(0, results);
}
@Override
public void hideRecipient(final RecipientIdentifier.Single recipient) {
final var recipientIdOptional = context.getRecipientHelper().resolveRecipientOptional(recipient);
@ -929,6 +965,10 @@ public class ManagerImpl implements Manager {
continue;
}
context.getContactHelper().setContactBlocked(recipientId, blocked);
context.getSyncHelper()
.sendMessageRequestResponse(blocked
? MessageRequestResponse.Type.BLOCK
: MessageRequestResponse.Type.UNBLOCK_AND_ACCEPT, recipientId);
// if we don't have a common group with the blocked contact we need to rotate the profile key
shouldRotateProfileKey = blocked && (
shouldRotateProfileKey || account.getGroupStore()
@ -957,6 +997,10 @@ public class ManagerImpl implements Manager {
continue;
}
context.getGroupHelper().setGroupBlocked(groupId, blocked);
context.getSyncHelper()
.sendMessageRequestResponse(blocked
? MessageRequestResponse.Type.BLOCK
: MessageRequestResponse.Type.UNBLOCK_AND_ACCEPT, groupId);
shouldRotateProfileKey = blocked;
}
if (shouldRotateProfileKey) {