1 package org
.asamk
.signal
.manager
.jobs
;
3 import org
.asamk
.signal
.manager
.JsonStickerPack
;
4 import org
.asamk
.signal
.manager
.storage
.stickers
.StickerPackId
;
5 import org
.asamk
.signal
.manager
.util
.IOUtils
;
6 import org
.slf4j
.Logger
;
7 import org
.slf4j
.LoggerFactory
;
8 import org
.whispersystems
.libsignal
.InvalidMessageException
;
9 import org
.whispersystems
.signalservice
.internal
.util
.Hex
;
11 import java
.io
.IOException
;
12 import java
.util
.HashSet
;
14 public class RetrieveStickerPackJob
implements Job
{
16 private final static Logger logger
= LoggerFactory
.getLogger(RetrieveStickerPackJob
.class);
18 private final StickerPackId packId
;
19 private final byte[] packKey
;
21 public RetrieveStickerPackJob(final StickerPackId packId
, final byte[] packKey
) {
23 this.packKey
= packKey
;
27 public void run(Context context
) {
28 if (context
.getStickerPackStore().existsStickerPack(packId
)) {
29 logger
.debug("Sticker pack {} already downloaded.", Hex
.toStringCondensed(packId
.serialize()));
32 logger
.debug("Retrieving sticker pack {}.", Hex
.toStringCondensed(packId
.serialize()));
34 final var manifest
= context
.getDependencies()
36 .retrieveStickerManifest(packId
.serialize(), packKey
);
38 final var stickerIds
= new HashSet
<Integer
>();
39 if (manifest
.getCover().isPresent()) {
40 stickerIds
.add(manifest
.getCover().get().getId());
42 for (var sticker
: manifest
.getStickers()) {
43 stickerIds
.add(sticker
.getId());
46 for (var id
: stickerIds
) {
47 final var inputStream
= context
.getDependencies()
49 .retrieveSticker(packId
.serialize(), packKey
, id
);
50 context
.getStickerPackStore().storeSticker(packId
, id
, o
-> IOUtils
.copyStream(inputStream
, o
));
53 final var jsonManifest
= new JsonStickerPack(manifest
.getTitle().orNull(),
54 manifest
.getAuthor().orNull(),
56 .transform(c
-> new JsonStickerPack
.JsonSticker(c
.getEmoji(),
57 String
.valueOf(c
.getId()),
60 manifest
.getStickers()
62 .map(c
-> new JsonStickerPack
.JsonSticker(c
.getEmoji(),
63 String
.valueOf(c
.getId()),
66 context
.getStickerPackStore().storeManifest(packId
, jsonManifest
);
67 } catch (IOException e
) {
68 logger
.warn("Failed to retrieve sticker pack {}: {}",
69 Hex
.toStringCondensed(packId
.serialize()),
71 } catch (InvalidMessageException e
) {
72 logger
.warn("Failed to retrieve sticker pack {}, invalid pack data: {}",
73 Hex
.toStringCondensed(packId
.serialize()),