From 8db6f86498f04e4d02795ac1e1c8662b1fa1336b Mon Sep 17 00:00:00 2001 From: Enrico Martelli Date: Wed, 11 Oct 2023 11:05:34 +0200 Subject: [PATCH] General improvements (#163) Co-authored-by: Roberto Cella --- .github/workflows/qodana-code-quality.yml | 5 +++ .../telegram/model/TelegramRequest.java | 34 ++++++++++++------- 2 files changed, 27 insertions(+), 12 deletions(-) diff --git a/.github/workflows/qodana-code-quality.yml b/.github/workflows/qodana-code-quality.yml index 8cee1280..63708b4e 100644 --- a/.github/workflows/qodana-code-quality.yml +++ b/.github/workflows/qodana-code-quality.yml @@ -26,3 +26,8 @@ jobs: use-caches: false post-pr-comment: false pr-mode: false + + - name: Upload results to GitHub + uses: github/codeql-action/upload-sarif@v2 + with: + sarif_file: ${{ runner.temp }}/qodana/results/qodana.sarif.json diff --git a/src/main/java/com/github/stickerifier/stickerify/telegram/model/TelegramRequest.java b/src/main/java/com/github/stickerifier/stickerify/telegram/model/TelegramRequest.java index 72e34ce7..6f063b0e 100644 --- a/src/main/java/com/github/stickerifier/stickerify/telegram/model/TelegramRequest.java +++ b/src/main/java/com/github/stickerifier/stickerify/telegram/model/TelegramRequest.java @@ -29,17 +29,9 @@ public record TelegramRequest(Message message) { private static final String HELP_COMMAND = "/help"; public TelegramFile getFile() { - return Stream.of(message.photo(), message.document(), message.sticker(), - message.video(), message.videoNote(), - message.audio(), message.voice()) - .filter(Objects::nonNull) - .findFirst() - .map(inputFile -> switch (inputFile) { - case PhotoSize[] photos when photos.length > 0 -> Arrays.stream(photos) - .map(photo -> new TelegramFile(photo.fileId(), photo.fileSize())) - .filter(TelegramFile::canBeDownloaded) - .max(comparing(TelegramFile::size)) - .orElse(TelegramFile.TOO_LARGE); + return getMessageMedia() + .map(media -> switch (media) { + case PhotoSize[] photos when photos.length > 0 -> getBestPhoto(photos); case Document document -> new TelegramFile(document.fileId(), document.fileSize()); case Sticker sticker -> new TelegramFile(sticker.fileId(), sticker.fileSize()); case Video video -> new TelegramFile(video.fileId(), video.fileSize()); @@ -49,6 +41,22 @@ public TelegramFile getFile() { .orElse(null); } + private Optional getMessageMedia() { + return Stream.of(message.photo(), message.document(), message.sticker(), + message.video(), message.videoNote(), + message.audio(), message.voice()) + .filter(Objects::nonNull) + .findFirst(); + } + + private TelegramFile getBestPhoto(PhotoSize[] photos) { + return Arrays.stream(photos) + .map(photo -> new TelegramFile(photo.fileId(), photo.fileSize())) + .filter(TelegramFile::canBeDownloaded) + .max(comparing(TelegramFile::size)) + .orElse(TelegramFile.TOO_LARGE); + } + public Long getChatId() { return message.chat().id(); } @@ -74,7 +82,9 @@ public String getDescription() { } private String getUsername() { - return Optional.ofNullable(message.from().username()).orElse(""); + return Optional.ofNullable(message.from().username()) + .map(username -> "@" + username) + .orElse("id:" + message.from().id()); } public Answer getAnswerMessage() {