Skip to content

Commit

Permalink
fix(external): Allow null replacements (#676)
Browse files Browse the repository at this point in the history
Co-authored-by: iProdigy <[email protected]>
  • Loading branch information
pajlada and iProdigy authored Mar 6, 2025
1 parent 2ca2ed1 commit ed907cc
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 1 deletion.
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

0 comments on commit ed907cc

Please sign in to comment.