Skip to content

Commit

Permalink
✨ persist list of hidden message IDs closes #64
Browse files Browse the repository at this point in the history
  • Loading branch information
McPringle committed Apr 1, 2024
1 parent 60864b1 commit 13cd181
Showing 1 changed file with 35 additions and 0 deletions.
35 changes: 35 additions & 0 deletions src/main/java/swiss/fihlon/apus/service/SocialService.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@
import swiss.fihlon.apus.social.Message;
import swiss.fihlon.apus.social.mastodon.MastodonAPI;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.time.Duration;
import java.util.ArrayList;
import java.util.Collections;
Expand Down Expand Up @@ -62,6 +65,7 @@ public SocialService(@NotNull final TaskScheduler taskScheduler,
filterWords = configuration.getFilter().words().stream()
.map(filterWord -> filterWord.toLowerCase(DEFAULT_LOCALE).trim())
.toList();
loadHiddenMessageIds();
updateMessages();
updateScheduler = taskScheduler.scheduleAtFixedRate(this::updateMessages, UPDATE_FREQUENCY);
}
Expand Down Expand Up @@ -108,5 +112,36 @@ public void hideMessage(@NotNull final Message message) {
message.id(), message.profile(), message.author());
messages.remove(message);
manuallyHiddenId.add(message.id());
saveHiddenMessageIds();
}

private Path getHiddenMessagesFilePath() {
final Path configDir = Path.of(System.getProperty("user.home"),".apus");
if (!configDir.toFile().exists()) {
try {
Files.createDirectories(configDir);
} catch (final IOException e) {
LOGGER.error("Unable to create configuration directory {}: {}", configDir, e.getMessage());
}
}
return configDir.resolve("hiddenMessageIds");
}

private void saveHiddenMessageIds() {
final var filePath = getHiddenMessagesFilePath();
try {
Files.writeString(filePath, String.join("\n", manuallyHiddenId));
} catch (final IOException e) {
LOGGER.error("Unable to save hidden message IDs to file '{}': {}", filePath, e.getMessage());
}
}

private void loadHiddenMessageIds() {
final var filePath = getHiddenMessagesFilePath();
try {
manuallyHiddenId.addAll(Files.readAllLines(filePath));
} catch (IOException e) {
LOGGER.error("Unable to load hidden message IDs from file '{}': {}", filePath, e.getMessage());
}
}
}

0 comments on commit 13cd181

Please sign in to comment.