Skip to content

Commit

Permalink
General improvements (#163)
Browse files Browse the repository at this point in the history
Co-authored-by: Roberto Cella <[email protected]>
  • Loading branch information
MartelliEnrico and rob93c authored Oct 11, 2023
1 parent faaf54d commit 8db6f86
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 12 deletions.
5 changes: 5 additions & 0 deletions .github/workflows/qodana-code-quality.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand All @@ -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();
}
Expand All @@ -74,7 +82,9 @@ public String getDescription() {
}

private String getUsername() {
return Optional.ofNullable(message.from().username()).orElse("<anonymous>");
return Optional.ofNullable(message.from().username())
.map(username -> "@" + username)
.orElse("id:" + message.from().id());
}

public Answer getAnswerMessage() {
Expand Down

0 comments on commit 8db6f86

Please sign in to comment.