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

fix(external): Allow null replacements #676

Merged
merged 8 commits into from
Mar 6, 2025
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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
## Unreleased

- Bugfix: Actually allow null replacements to be sent to the external plugin notifier. (#676)

## 1.11.2

- Dev: Harden input validation for urls provided to the external plugin notifier. (#672)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@

import javax.inject.Inject;
import javax.inject.Singleton;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

@Slf4j
@Singleton
Expand Down Expand Up @@ -87,7 +89,7 @@ private void handleNotify(ExternalNotificationRequest input) {
var player = Utils.getPlayerName(client);
var template = Template.builder()
.template(input.getText())
.replacements(input.getReplacements())
.replacements(Objects.requireNonNullElseGet(input.getReplacements(), () -> new HashMap<>(2)))
.replacement("%USERNAME%", Replacements.ofText(player))
.build();

Expand Down
83 changes: 83 additions & 0 deletions src/test/java/dinkplugin/notifiers/ExternalPluginNotifierTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,89 @@ void testNotify() {
);
}

@Test
void testNoReplacements() {
// fire event
var payload = samplePayload("https://example.com/");
payload.remove("replacements");
payload.put("text", "text with no replacements");
plugin.onPluginMessage(new PluginMessage("dink", "notify", payload));

// verify notification
verifyCreateMessage(
"https://example.com/",
false,
NotificationBody.builder()
.type(NotificationType.EXTERNAL_PLUGIN)
.playerName(PLAYER_NAME)
.customTitle("My Title")
.customFooter("Sent by MyExternalPlugin via Dink")
.text(
Template.builder()
.template("text with no replacements")
.build()
)
.extra(new ExternalNotificationData("MyExternalPlugin", List.of(new Field("sample key", "sample value")), Collections.singletonMap("hello", "world")))
.build()
);
}

@Test
void testMissingReplacement() {
// fire event
var payload = samplePayload("https://example.com/");
payload.remove("replacements");
payload.put("text", "text with no custom replacement %USERNAME%");
plugin.onPluginMessage(new PluginMessage("dink", "notify", payload));

// verify notification
verifyCreateMessage(
"https://example.com/",
false,
NotificationBody.builder()
.type(NotificationType.EXTERNAL_PLUGIN)
.playerName(PLAYER_NAME)
.customTitle("My Title")
.customFooter("Sent by MyExternalPlugin via Dink")
.text(
Template.builder()
.template("text with no custom replacement %USERNAME%")
.replacement("%USERNAME%", Replacements.ofText(PLAYER_NAME))
.build()
)
.extra(new ExternalNotificationData("MyExternalPlugin", List.of(new Field("sample key", "sample value")), Collections.singletonMap("hello", "world")))
.build()
);
}

@Test
void testPatternWithoutReplacement() {
// fire event
var payload = samplePayload("https://example.com/");
payload.remove("replacements");
payload.put("text", "text with no custom replacement %XD%");
plugin.onPluginMessage(new PluginMessage("dink", "notify", payload));

// verify notification
verifyCreateMessage(
"https://example.com/",
false,
NotificationBody.builder()
.type(NotificationType.EXTERNAL_PLUGIN)
.playerName(PLAYER_NAME)
.customTitle("My Title")
.customFooter("Sent by MyExternalPlugin via Dink")
.text(
Template.builder()
.template("text with no custom replacement %XD%")
.replacement("%USERNAME%", Replacements.ofText(PLAYER_NAME))
.build()
)
.extra(new ExternalNotificationData("MyExternalPlugin", List.of(new Field("sample key", "sample value")), Collections.singletonMap("hello", "world")))
.build()
);
}

@Test
void testFallbackUrl() {
// update config mocks
Expand Down