Implement listStickerPacks command

This commit is contained in:
AsamK 2022-01-03 16:11:03 +01:00
parent 404063a080
commit e70463d7b8
10 changed files with 167 additions and 3 deletions

View file

@ -23,6 +23,7 @@ public class Commands {
addCommand(new ListDevicesCommand());
addCommand(new ListGroupsCommand());
addCommand(new ListIdentitiesCommand());
addCommand(new ListStickerPacksCommand());
addCommand(new QuitGroupCommand());
addCommand(new ReceiveCommand());
addCommand(new RegisterCommand());

View file

@ -0,0 +1,67 @@
package org.asamk.signal.commands;
import net.sourceforge.argparse4j.inf.Namespace;
import net.sourceforge.argparse4j.inf.Subparser;
import org.asamk.signal.commands.exceptions.CommandException;
import org.asamk.signal.manager.Manager;
import org.asamk.signal.manager.api.StickerPack;
import org.asamk.signal.output.JsonWriter;
import org.asamk.signal.output.OutputWriter;
import org.asamk.signal.output.PlainTextWriter;
import org.asamk.signal.util.Hex;
import java.util.List;
public class ListStickerPacksCommand implements JsonRpcLocalCommand {
@Override
public String getName() {
return "listStickerPacks";
}
@Override
public void attachToSubparser(final Subparser subparser) {
subparser.help("Show a list of known sticker packs.");
}
@Override
public void handleCommand(
final Namespace ns, final Manager c, final OutputWriter outputWriter
) throws CommandException {
final var stickerPacks = c.getStickerPacks();
if (outputWriter instanceof JsonWriter jsonWriter) {
final var jsonStickerPacks = stickerPacks.stream().map(JsonStickerPack::new).toList();
jsonWriter.write(jsonStickerPacks);
} else if (outputWriter instanceof PlainTextWriter plainTextWriter) {
for (final var sticker : stickerPacks) {
plainTextWriter.println("Pack {}: “{}” by “{}” has {} stickers",
Hex.toStringCondensed(sticker.packId().serialize()),
sticker.title(),
sticker.author(),
sticker.stickers().size());
}
}
}
private record JsonStickerPack(
String packId, boolean installed, String title, String author, JsonSticker cover, List<JsonSticker> stickers
) {
JsonStickerPack(StickerPack stickerPack) {
this(Hex.toStringCondensed(stickerPack.packId().serialize()),
stickerPack.installed(),
stickerPack.title(),
stickerPack.author(),
stickerPack.cover().map(JsonSticker::new).orElse(null),
stickerPack.stickers().stream().map(JsonSticker::new).toList());
}
private record JsonSticker(int id, String emoji, String contentType) {
JsonSticker(StickerPack.Sticker sticker) {
this(sticker.id(), sticker.emoji(), sticker.contentType());
}
}
}
}

View file

@ -18,6 +18,7 @@ import org.asamk.signal.manager.api.Pair;
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.TypingAction;
import org.asamk.signal.manager.api.UpdateGroup;
import org.asamk.signal.manager.groups.GroupId;
@ -443,6 +444,11 @@ public class DbusManagerImpl implements Manager {
}
}
@Override
public List<StickerPack> getStickerPacks() {
throw new UnsupportedOperationException();
}
@Override
public void requestAllSyncData() throws IOException {
signal.sendSyncRequest();