moderation: refactor clean command #1669
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
The current implementation of the
clean
command is somewhat difficult to follow: depending on the arguments, we set about 10 different variables and pass them toAdvancedDeleteMessages
as arguments:yagpdb/moderation/commands.go
Line 751 in 736d1b3
(
AdvancedDeleteMessages
subsequently checks each filter one-by-one in a long series of conditionals, but this code is not particularly problematic; it is just long, but is relatively easy to understand.)This PR refactors the code by introducing a
Filter
interface (which, given a message, reports whether it passes the filter); each filter is a separate struct implementing this interface. The mainclean
code then boils down to building a slice ofFilter
s based on the arguments, then deleting messages that match all filters. The benefits of this refactor are as follows:Implementations of different filters are isolated (and indeed can now be unit-tested if so desired.) Naturally, it is much easier to find the implementation of a given filter; for instance, if one wishes to verify that the pinned message filter works correctly, we can examine
and then use go-to-definition on
NewIgnorePinnedMessagesFilter
to findwhereas previously, one would have to see that the
pe
variable is set to true if thenopin
flag is provided (and then trace how that variable is used inAdvancedDeleteMessages
.)The separate
AdvancedDeleteMessages
function (the one with 10 arguments) can be removed entirely, as the main deletion logic is so simple:Still just as easy (arguably slightly more so) to extend with new filters: it suffices to implement a new
Filter
, then change theclean
command to recognize it. Previously one would have had to add a new parameter toAdvancedDeleteMessages
, add a new conditional there, and makeclean
pass that variable toAdvancedDeleteMessages
.Despite the additional (and useful) abstraction, the overall diff remains in favor of this change: it is not heavy code-wise.