Add command to retrieve avatars and stickers

Fixes #1125
This commit is contained in:
AsamK 2024-02-09 22:06:46 +01:00
parent d486563099
commit 7cf3a989bf
13 changed files with 296 additions and 3 deletions

View file

@ -34,6 +34,7 @@ import org.asamk.signal.manager.api.RecipientIdentifier;
import org.asamk.signal.manager.api.SendGroupMessageResults;
import org.asamk.signal.manager.api.SendMessageResults;
import org.asamk.signal.manager.api.StickerPack;
import org.asamk.signal.manager.api.StickerPackId;
import org.asamk.signal.manager.api.StickerPackInvalidException;
import org.asamk.signal.manager.api.StickerPackUrl;
import org.asamk.signal.manager.api.TypingAction;
@ -307,6 +308,14 @@ public interface Manager extends Closeable {
InputStream retrieveAttachment(final String id) throws IOException;
InputStream retrieveContactAvatar(final RecipientIdentifier.Single recipient) throws IOException, UnregisteredRecipientException;
InputStream retrieveProfileAvatar(final RecipientIdentifier.Single recipient) throws IOException, UnregisteredRecipientException;
InputStream retrieveGroupAvatar(final GroupId groupId) throws IOException;
InputStream retrieveSticker(final StickerPackId stickerPackId, final int stickerId) throws IOException;
@Override
void close();

View file

@ -1,7 +1,8 @@
package org.asamk.signal.manager.api;
import org.whispersystems.signalservice.internal.util.Hex;
import java.util.Arrays;
import java.util.Base64;
public class StickerPackId {
@ -36,6 +37,6 @@ public class StickerPackId {
@Override
public String toString() {
return "StickerPackId{" + Base64.getUrlEncoder().encodeToString(id) + '}';
return "StickerPackId{" + Hex.toStringCondensed(id) + '}';
}
}

View file

@ -104,6 +104,7 @@ import org.whispersystems.signalservice.internal.util.Util;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
@ -1337,6 +1338,58 @@ public class ManagerImpl implements Manager {
return context.getAttachmentHelper().retrieveAttachment(id).getStream();
}
@Override
public InputStream retrieveContactAvatar(final RecipientIdentifier.Single recipient) throws IOException, UnregisteredRecipientException {
final var recipientId = context.getRecipientHelper().resolveRecipient(recipient);
final var address = account.getRecipientStore().resolveRecipientAddress(recipientId);
final var streamDetails = context.getAvatarStore().retrieveContactAvatar(address);
if (streamDetails == null) {
throw new FileNotFoundException();
}
return streamDetails.getStream();
}
@Override
public InputStream retrieveProfileAvatar(final RecipientIdentifier.Single recipient) throws IOException, UnregisteredRecipientException {
final var recipientId = context.getRecipientHelper().resolveRecipient(recipient);
context.getProfileHelper().getRecipientProfile(recipientId);
final var address = account.getRecipientStore().resolveRecipientAddress(recipientId);
final var streamDetails = context.getAvatarStore().retrieveProfileAvatar(address);
if (streamDetails == null) {
throw new FileNotFoundException();
}
return streamDetails.getStream();
}
@Override
public InputStream retrieveGroupAvatar(final GroupId groupId) throws IOException {
final var streamDetails = context.getAvatarStore().retrieveGroupAvatar(groupId);
context.getGroupHelper().getGroup(groupId);
if (streamDetails == null) {
throw new FileNotFoundException();
}
return streamDetails.getStream();
}
@Override
public InputStream retrieveSticker(final StickerPackId stickerPackId, final int stickerId) throws IOException {
var streamDetails = context.getStickerPackStore().retrieveSticker(stickerPackId, stickerId);
if (streamDetails == null) {
final var pack = account.getStickerStore().getStickerPack(stickerPackId);
if (pack != null) {
try {
context.getStickerHelper().retrieveStickerPack(stickerPackId, pack.packKey());
} catch (InvalidMessageException e) {
logger.warn("Failed to download sticker pack");
}
}
}
if (streamDetails == null) {
throw new FileNotFoundException();
}
return streamDetails.getStream();
}
@Override
public void close() {
Thread thread;