Skip to content

Commit

Permalink
♻️ Refactor hide profile to block profile
Browse files Browse the repository at this point in the history
Signed-off-by: Marcus Fihlon <[email protected]>
  • Loading branch information
McPringle committed Apr 9, 2024
1 parent 54dc937 commit 4b1cf42
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 30 deletions.
48 changes: 24 additions & 24 deletions src/main/java/swiss/fihlon/apus/service/SocialService.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ public final class SocialService {
private final boolean filterReplies;
private final boolean filterSensitive;
private final List<String> filterWords;
private final Set<String> manuallyHiddenId = new HashSet<>();
private final Set<String> manuallyHiddenProfile = new HashSet<>();
private final Set<String> hiddenMessages = new HashSet<>();
private final Set<String> blockedProfiles = new HashSet<>();
private List<Message> messages = List.of();

public SocialService(@NotNull final TaskScheduler taskScheduler,
Expand All @@ -65,7 +65,7 @@ public SocialService(@NotNull final TaskScheduler taskScheduler,
.map(filterWord -> filterWord.toLowerCase(DEFAULT_LOCALE).trim())
.toList();
loadHiddenMessageIds();
loadHiddenProfiles();
loadBlockedProfiles();
updateMessages();
updateScheduler = taskScheduler.scheduleAtFixedRate(this::updateMessages, UPDATE_FREQUENCY);
}
Expand All @@ -77,8 +77,8 @@ public void stopUpdateScheduler() {

private void updateMessages() {
final var newMessages = mastodonAPI.getMessages().stream()
.filter(message -> !manuallyHiddenId.contains(message.id()))
.filter(message -> !manuallyHiddenProfile.contains(message.profile()))
.filter(message -> !hiddenMessages.contains(message.id()))
.filter(message -> !blockedProfiles.contains(message.profile()))
.filter(message -> !filterSensitive || !message.isSensitive())
.filter(message -> !filterReplies || !message.isReply())
.filter(this::checkWordFilter)
Expand Down Expand Up @@ -112,16 +112,16 @@ public void hideMessage(@NotNull final Message message) {
LOGGER.warn("Hiding message (id={}, profile={}, author={})",
message.id(), message.profile(), message.author());
messages.remove(message);
manuallyHiddenId.add(message.id());
hiddenMessages.add(message.id());
saveHiddenMessageIds();
}

public void hideProfile(@NotNull final Message message) {
LOGGER.warn("Hide profile (id={}, profile={}, author={})",
message.id(), message.profile(), message.author());
messages.remove(message);
manuallyHiddenProfile.add(message.profile());
saveHiddenProfiles();
blockedProfiles.add(message.profile());
saveBlockedProfiles();
}

private Path getConfigDir() {
Expand All @@ -137,46 +137,46 @@ private Path getConfigDir() {
}

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

private void saveHiddenProfiles() {
final var filePath = getConfigDir().resolve("hiddenProfiles");
private void saveBlockedProfiles() {
final var filePath = getConfigDir().resolve("blockedProfiles");
try {
Files.writeString(filePath, String.join("\n", manuallyHiddenProfile));
Files.writeString(filePath, String.join("\n", blockedProfiles));
} catch (final IOException e) {
LOGGER.error("Unable to save hidden profiles to file '{}': {}", filePath, e.getMessage());
LOGGER.error("Unable to save blocked profiles to file '{}': {}", filePath, e.getMessage());
}
}

private void loadHiddenMessageIds() {
final var filePath = getConfigDir().resolve("hiddenMessageIds");
final var filePath = getConfigDir().resolve("hiddenMessages");
if (filePath.toFile().exists()) {
try {
manuallyHiddenId.addAll(Files.readAllLines(filePath));
hiddenMessages.addAll(Files.readAllLines(filePath));
} catch (IOException e) {
LOGGER.error("Unable to load hidden message IDs from file '{}': {}", filePath, e.getMessage());
LOGGER.error("Unable to load hidden messages from file '{}': {}", filePath, e.getMessage());
}
} else {
LOGGER.info("No previously saved hidden message IDs found.");
LOGGER.info("No previously saved hidden messages found.");
}
}

private void loadHiddenProfiles() {
final var filePath = getConfigDir().resolve("hiddenProfiles");
private void loadBlockedProfiles() {
final var filePath = getConfigDir().resolve("blockedProfiles");
if (filePath.toFile().exists()) {
try {
manuallyHiddenProfile.addAll(Files.readAllLines(filePath));
blockedProfiles.addAll(Files.readAllLines(filePath));
} catch (IOException e) {
LOGGER.error("Unable to load hidden profiles from file '{}': {}", filePath, e.getMessage());
LOGGER.error("Unable to load blocked profiles from file '{}': {}", filePath, e.getMessage());
}
} else {
LOGGER.info("No previously saved hidden profiles found.");
LOGGER.info("No previously saved blocked profiles found.");
}
}
}
4 changes: 2 additions & 2 deletions src/main/java/swiss/fihlon/apus/ui/view/SocialView.java
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ private void updateMessages() {
if (adminModeEnabled) {
final var messageMenu = new ContextMenu();
messageMenu.addItem(getTranslation("social.message.contextmenu.hide.message"), event -> hideMessage(message));
messageMenu.addItem(getTranslation("social.message.contextmenu.hide.profile"), event -> blockProfile(message));
messageMenu.addItem(getTranslation("social.message.contextmenu.block.profile"), event -> blockProfile(message));
messageMenu.setTarget(messageView);
}
messageContainer.add(messageView);
Expand All @@ -148,7 +148,7 @@ private void hideMessage(@NotNull final Message message) {

private void blockProfile(@NotNull final Message message) {
socialService.hideProfile(message);
Notification.show(getTranslation("social.message.contextmenu.hide.profile.done"));
Notification.show(getTranslation("social.message.contextmenu.block.profile.done"));
updateMessages();
}
}
4 changes: 2 additions & 2 deletions src/main/resources/vaadin-i18n/translations.properties
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ social.admin.login.rejected=Your authentication was not successful!
social.admin.login.successful=You have administrative rights.
social.admin.login.title=Authentification
social.heading=Posts with #{0} on Mastodon
social.message.contextmenu.block.profile.done=All messages from this profile have been hidden.
social.message.contextmenu.block.profile=Hide this profile
social.message.contextmenu.hide.message.done=The message was hidden as requested.
social.message.contextmenu.hide.message=Hide this message
social.message.contextmenu.hide.profile.done=All messages from this profile have been hidden.
social.message.contextmenu.hide.profile=Hide this profile
4 changes: 2 additions & 2 deletions src/main/resources/vaadin-i18n/translations_de.properties
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ social.admin.login.rejected=Du hast dich nicht erfolgreich authentifiziert!
social.admin.login.successful=Du hast jetzt Administrationsrechte.
social.admin.login.title=Authentifizierung
social.heading=Schreibe einen Beitrag mit #{0} auf Mastodon
social.message.contextmenu.block.profile.done=Alle Beiträge dieses Profils wurden wie gewünscht blockiert.
social.message.contextmenu.block.profile=Dieses Profil blockieren
social.message.contextmenu.hide.message.done=Der Beitrag wurde wie gewünscht ausgeblendet.
social.message.contextmenu.hide.message=Diesen Beitrag ausblenden
social.message.contextmenu.hide.profile.done=Alle Beiträge dieses Profils wurden wie gewünscht ausgeblendet.
social.message.contextmenu.hide.profile=Dieses Profil ausblenden

0 comments on commit 4b1cf42

Please sign in to comment.