Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

General improvements #163

Merged
merged 4 commits into from
Oct 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading