Add log notification filters (#1980)

This commit is contained in:
Luck 2020-06-20 20:46:54 +01:00
parent 97a0dbebcb
commit bd9718c5af
No known key found for this signature in database
GPG Key ID: EFA9B3EC5FD90F8B
7 changed files with 84 additions and 13 deletions

View File

@ -304,6 +304,12 @@ require-sender-group-membership-to-modify: false
# '/lp log notify <on|off>'
log-notify: true
# Defines a list of log entries which should not be sent as notifications to users.
#
# - Each entry in the list is a RegEx expression which is matched against the log entry description.
log-notify-filtered-descriptions:
# - "parent add example"
# Defines the options for prefix and suffix stacking.
#
# - The feature allows you to display multiple prefixes or suffixes alongside a players username in

View File

@ -312,6 +312,12 @@ require-sender-group-membership-to-modify: false
# '/lp log notify <on|off>'
log-notify: true
# Defines a list of log entries which should not be sent as notifications to users.
#
# - Each entry in the list is a RegEx expression which is matched against the log entry description.
log-notify-filtered-descriptions:
# - "parent add example"
# Defines the options for prefix and suffix stacking.
#
# - The feature allows you to display multiple prefixes or suffixes alongside a players username in

View File

@ -35,6 +35,9 @@ import me.lucko.luckperms.common.sender.Sender;
import net.luckperms.api.event.log.LogBroadcastEvent;
import net.luckperms.api.event.log.LogNotifyEvent;
import java.util.Collection;
import java.util.regex.Pattern;
public class LogDispatcher {
private final LuckPermsPlugin plugin;
@ -42,6 +45,26 @@ public class LogDispatcher {
this.plugin = plugin;
}
private boolean shouldBroadcast(LoggedAction entry, LogBroadcastEvent.Origin origin) {
boolean cancelled = false;
if (!this.plugin.getConfiguration().get(ConfigKeys.LOG_NOTIFY)) {
cancelled = true;
} else if (origin == LogBroadcastEvent.Origin.REMOTE && !this.plugin.getConfiguration().get(ConfigKeys.BROADCAST_RECEIVED_LOG_ENTRIES)) {
cancelled = true;
} else {
Collection<Pattern> filters = this.plugin.getConfiguration().get(ConfigKeys.LOG_NOTIFY_FILTERED_DESCRIPTIONS);
for (Pattern filter : filters) {
if (filter.matcher(entry.getDescription()).matches()) {
cancelled = true;
break;
}
}
}
return !this.plugin.getEventDispatcher().dispatchLogBroadcast(cancelled, entry, origin);
}
private void broadcast(LoggedAction entry, LogNotifyEvent.Origin origin, Sender sender) {
this.plugin.getOnlineSenders()
.filter(CommandPermission.LOG_NOTIFY::isAuthorized)
@ -64,12 +87,19 @@ public class LogDispatcher {
this.plugin.getMessagingService().ifPresent(service -> service.pushLog(entry));
boolean shouldCancel = !this.plugin.getConfiguration().get(ConfigKeys.LOG_NOTIFY);
if (!this.plugin.getEventDispatcher().dispatchLogBroadcast(shouldCancel, entry, LogBroadcastEvent.Origin.LOCAL)) {
if (shouldBroadcast(entry, LogBroadcastEvent.Origin.LOCAL)) {
broadcast(entry, LogNotifyEvent.Origin.LOCAL, sender);
}
}
public void broadcastFromApi(LoggedAction entry) {
this.plugin.getMessagingService().ifPresent(extendedMessagingService -> extendedMessagingService.pushLog(entry));
if (shouldBroadcast(entry, LogBroadcastEvent.Origin.LOCAL_API)) {
broadcast(entry, LogNotifyEvent.Origin.LOCAL_API, null);
}
}
public void dispatchFromApi(LoggedAction entry) {
if (!this.plugin.getEventDispatcher().dispatchLogPublish(false, entry)) {
try {
@ -82,18 +112,8 @@ public class LogDispatcher {
broadcastFromApi(entry);
}
public void broadcastFromApi(LoggedAction entry) {
this.plugin.getMessagingService().ifPresent(extendedMessagingService -> extendedMessagingService.pushLog(entry));
boolean shouldCancel = !this.plugin.getConfiguration().get(ConfigKeys.LOG_NOTIFY);
if (!this.plugin.getEventDispatcher().dispatchLogBroadcast(shouldCancel, entry, LogBroadcastEvent.Origin.LOCAL_API)) {
broadcast(entry, LogNotifyEvent.Origin.LOCAL_API, null);
}
}
public void dispatchFromRemote(LoggedAction entry) {
boolean shouldCancel = !this.plugin.getConfiguration().get(ConfigKeys.BROADCAST_RECEIVED_LOG_ENTRIES) || !this.plugin.getConfiguration().get(ConfigKeys.LOG_NOTIFY);
if (!this.plugin.getEventDispatcher().dispatchLogBroadcast(shouldCancel, entry, LogBroadcastEvent.Origin.REMOTE)) {
if (shouldBroadcast(entry, LogBroadcastEvent.Origin.REMOTE)) {
broadcast(entry, LogNotifyEvent.Origin.REMOTE, null);
}
}

View File

@ -25,6 +25,7 @@
package me.lucko.luckperms.common.config;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Maps;
@ -62,6 +63,8 @@ import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.function.Function;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;
import static me.lucko.luckperms.common.config.generic.key.ConfigKeyFactory.booleanKey;
import static me.lucko.luckperms.common.config.generic.key.ConfigKeyFactory.key;
@ -388,6 +391,23 @@ public final class ConfigKeys {
*/
public static final ConfigKey<Boolean> LOG_NOTIFY = booleanKey("log-notify", true);
/**
* Defines a list of log entries which should not be sent as notifications to users.
*/
public static final ConfigKey<List<Pattern>> LOG_NOTIFY_FILTERED_DESCRIPTIONS = key(c -> {
return c.getStringList("log-notify-filtered-descriptions", ImmutableList.of()).stream()
.map(entry -> {
try {
return Pattern.compile(entry, Pattern.CASE_INSENSITIVE);
} catch (PatternSyntaxException e) {
new IllegalArgumentException("Invalid pattern: " + entry, e).printStackTrace();
return null;
}
})
.filter(Objects::nonNull)
.collect(ImmutableCollectors.toList());
});
/**
* If auto op is enabled. Only used by the Bukkit platform.
*/

View File

@ -299,6 +299,12 @@ require-sender-group-membership-to-modify: false
# '/lp log notify <on|off>'
log-notify: true
# Defines a list of log entries which should not be sent as notifications to users.
#
# - Each entry in the list is a RegEx expression which is matched against the log entry description.
log-notify-filtered-descriptions:
# - "parent add example"
# Defines the options for prefix and suffix stacking.
#
# - The feature allows you to display multiple prefixes or suffixes alongside a players username in

View File

@ -308,6 +308,13 @@ require-sender-group-membership-to-modify = false
# '/lp log notify <on|off>'
log-notify = true
# Defines a list of log entries which should not be sent as notifications to users.
#
# - Each entry in the list is a RegEx expression which is matched against the log entry description.
log-notify-filtered-descriptions = [
# "parent add example"
]
# Defines the options for prefix and suffix stacking.
#
# - The feature allows you to display multiple prefixes or suffixes alongside a players username in

View File

@ -303,6 +303,12 @@ require-sender-group-membership-to-modify: false
# '/lp log notify <on|off>'
log-notify: true
# Defines a list of log entries which should not be sent as notifications to users.
#
# - Each entry in the list is a RegEx expression which is matched against the log entry description.
log-notify-filtered-descriptions:
# - "parent add example"
# Defines the options for prefix and suffix stacking.
#
# - The feature allows you to display multiple prefixes or suffixes alongside a players username in