Skip to content

Commit

Permalink
Implement sending mentions
Browse files Browse the repository at this point in the history
Closes #584
  • Loading branch information
AsamK committed Nov 13, 2021
1 parent 89f568d commit 53b84ba
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 5 deletions.
10 changes: 10 additions & 0 deletions lib/src/main/java/org/asamk/signal/manager/ManagerImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -683,6 +683,16 @@ private void applyMessage(
if (attachments != null) {
messageBuilder.withAttachments(attachmentHelper.uploadAttachments(attachments));
}
if (message.mentions().size() > 0) {
final var mentions = new ArrayList<SignalServiceDataMessage.Mention>();
for (final var m : message.mentions()) {
final var recipientId = resolveRecipient(m.recipient());
mentions.add(new SignalServiceDataMessage.Mention(resolveSignalServiceAddress(recipientId).getAci(),
m.start(),
m.length()));
}
messageBuilder.withMentions(mentions);
}
}

@Override
Expand Down
5 changes: 4 additions & 1 deletion lib/src/main/java/org/asamk/signal/manager/api/Message.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,7 @@

import java.util.List;

public record Message(String messageText, List<String> attachments) {}
public record Message(String messageText, List<String> attachments, List<Mention> mentions) {

public record Mention(RecipientIdentifier.Single recipient, int start, int length) {}
}
5 changes: 5 additions & 0 deletions man/signal-cli.1.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,11 @@ Send the message to self without notification.
*-e*, *--end-session*::
Clear session state and send end session message.

*--mention*::
Mention another group member (syntax: start:length:recipientNumber)
In the apps the mention replaces part of the message text, which is specified by the start and length values.
e.g.: `-m "Hi X!" --mention "3:1:+123456789"`

=== sendReaction

Send reaction to a previously received or sent message.
Expand Down
26 changes: 25 additions & 1 deletion src/main/java/org/asamk/signal/commands/SendCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,10 @@

import java.io.IOException;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

public class SendCommand implements JsonRpcLocalCommand {
Expand All @@ -52,6 +54,9 @@ public void attachToSubparser(final Subparser subparser) {
subparser.addArgument("-e", "--end-session", "--endsession")
.help("Clear session state and send end session message.")
.action(Arguments.storeTrue());
subparser.addArgument("--mention")
.nargs("*")
.help("Mention another group member (syntax: start:length:recipientNumber)");
}

@Override
Expand Down Expand Up @@ -100,8 +105,27 @@ public void handleCommand(
attachments = List.of();
}

List<String> mentionStrings = ns.getList("mention");
List<Message.Mention> mentions;
if (mentionStrings == null) {
mentions = List.of();
} else {
final Pattern mentionPattern = Pattern.compile("([0-9]+):([0-9]+):(.+)");
mentions = new ArrayList<>();
for (final var mention : mentionStrings) {
final var matcher = mentionPattern.matcher(mention);
if (!matcher.matches()) {
throw new UserErrorException("Invalid mention syntax ("
+ mention
+ ") expected 'start:end:recipientNumber'");
}
mentions.add(new Message.Mention(CommandUtil.getSingleRecipientIdentifier(matcher.group(3),
m.getSelfNumber()), Integer.parseInt(matcher.group(1)), Integer.parseInt(matcher.group(2))));
}
}

try {
var results = m.sendMessage(new Message(messageText, attachments), recipientIdentifiers);
var results = m.sendMessage(new Message(messageText, attachments, mentions), recipientIdentifiers);
outputResult(outputWriter, results.timestamp());
ErrorUtils.handleSendMessageResults(results.results());
} catch (AttachmentInvalidException | IOException e) {
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/org/asamk/signal/dbus/DbusSignalImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ public long sendMessage(final String message, final List<String> attachments, fi
@Override
public long sendMessage(final String message, final List<String> attachments, final List<String> recipients) {
try {
final var results = m.sendMessage(new Message(message, attachments),
final var results = m.sendMessage(new Message(message, attachments, List.of()),
getSingleRecipientIdentifiers(recipients, m.getSelfNumber()).stream()
.map(RecipientIdentifier.class::cast)
.collect(Collectors.toSet()));
Expand Down Expand Up @@ -367,7 +367,7 @@ public long sendNoteToSelfMessage(
final String message, final List<String> attachments
) throws Error.AttachmentInvalid, Error.Failure, Error.UntrustedIdentity {
try {
final var results = m.sendMessage(new Message(message, attachments),
final var results = m.sendMessage(new Message(message, attachments, List.of()),
Set.of(RecipientIdentifier.NoteToSelf.INSTANCE));
checkSendMessageResults(results.timestamp(), results.results());
return results.timestamp();
Expand All @@ -393,7 +393,7 @@ public void sendEndSessionMessage(final List<String> recipients) {
@Override
public long sendGroupMessage(final String message, final List<String> attachments, final byte[] groupId) {
try {
var results = m.sendMessage(new Message(message, attachments),
var results = m.sendMessage(new Message(message, attachments, List.of()),
Set.of(new RecipientIdentifier.Group(getGroupId(groupId))));
checkSendMessageResults(results.timestamp(), results.results());
return results.timestamp();
Expand Down

0 comments on commit 53b84ba

Please sign in to comment.