Skip to content

Commit 7c975d4

Browse files
committed
#1344 added additional placeholder for webhook
1 parent bd2a1b8 commit 7c975d4

File tree

3 files changed

+12
-8
lines changed

3 files changed

+12
-8
lines changed

server/core/src/main/java/cc/blynk/server/core/processors/BaseProcessorHandler.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ protected void processEventorAndWebhook(User user, DashBoard dash, int deviceId,
3131
PinType pinType, String value, long now) {
3232
try {
3333
eventorProcessor.process(user, session, dash, deviceId, pin, pinType, value, now);
34-
webhookProcessor.process(session, dash, deviceId, pin, pinType, value, now);
34+
webhookProcessor.process(user, session, dash, deviceId, pin, pinType, value, now);
3535
} catch (QuotaLimitException qle) {
3636
log.debug("User {} reached notification limit for eventor/webhook.", user.name);
3737
} catch (IllegalArgumentException iae) {

server/core/src/main/java/cc/blynk/server/core/processors/WebhookProcessor.java

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import cc.blynk.server.core.model.DashBoard;
44
import cc.blynk.server.core.model.DataStream;
55
import cc.blynk.server.core.model.auth.Session;
6+
import cc.blynk.server.core.model.auth.User;
67
import cc.blynk.server.core.model.enums.PinType;
78
import cc.blynk.server.core.model.widgets.others.webhook.Header;
89
import cc.blynk.server.core.model.widgets.others.webhook.WebHook;
@@ -24,6 +25,7 @@
2425

2526
import static cc.blynk.server.core.protocol.enums.Command.WEB_HOOKS;
2627
import static cc.blynk.utils.StringUtils.DATETIME_PATTERN;
28+
import static cc.blynk.utils.StringUtils.DEVICE_OWNER_EMAIL;
2729
import static cc.blynk.utils.StringUtils.GENERIC_PLACEHOLDER;
2830
import static cc.blynk.utils.StringUtils.PIN_PATTERN;
2931
import static cc.blynk.utils.StringUtils.PIN_PATTERN_0;
@@ -68,7 +70,7 @@ public WebhookProcessor(DefaultAsyncHttpClient httpclient,
6870
this.webhookFailureLimit = failureLimit;
6971
}
7072

71-
public void process(Session session, DashBoard dash, int deviceId, short pin,
73+
public void process(User user, Session session, DashBoard dash, int deviceId, short pin,
7274
PinType pinType, String triggerValue, long now) {
7375
WebHook webhook = dash.findWebhookByPin(deviceId, pin, pinType);
7476
if (webhook == null) {
@@ -78,12 +80,12 @@ public void process(Session session, DashBoard dash, int deviceId, short pin,
7880
checkIfNotificationQuotaLimitIsNotReached(now);
7981

8082
if (webhook.isNotFailed(webhookFailureLimit) && webhook.url != null) {
81-
process(session, dash.id, deviceId, webhook, triggerValue);
83+
process(user, session, dash.id, deviceId, webhook, triggerValue);
8284
}
8385
}
8486

85-
private void process(Session session, int dashId, int deviceId, WebHook webHook, String triggerValue) {
86-
String newUrl = format(webHook.url, triggerValue);
87+
private void process(User user, Session session, int dashId, int deviceId, WebHook webHook, String triggerValue) {
88+
String newUrl = format(webHook.url, triggerValue, user.email);
8789

8890
if (!WebHook.isValidUrl(newUrl)) {
8991
return;
@@ -114,7 +116,7 @@ private void process(Session session, int dashId, int deviceId, WebHook webHook
114116
builder.setHeader(header.name, header.value);
115117
if (webHook.body != null && !webHook.body.isEmpty()) {
116118
if (CONTENT_TYPE.equals(header.name)) {
117-
String newBody = format(webHook.body, triggerValue);
119+
String newBody = format(webHook.body, triggerValue, user.email);
118120
log.trace("Webhook formatted body : {}", newBody);
119121
builder.setBody(newBody);
120122
}
@@ -123,7 +125,7 @@ private void process(Session session, int dashId, int deviceId, WebHook webHook
123125
}
124126
}
125127

126-
log.trace("Sending webhook. ", webHook);
128+
log.trace("Sending webhook. {}", webHook);
127129
builder.execute(new AsyncCompletionHandler<Response>() {
128130

129131
private int length = 0;
@@ -184,7 +186,7 @@ private static boolean isValidResponseCode(int responseCode) {
184186
}
185187
}
186188

187-
private static String format(String data, String triggerValue) {
189+
private static String format(String data, String triggerValue, String ownerEmail) {
188190
//this is an ugly hack to make it work with Blynk HTTP API.
189191
String quotedValue = Matcher.quoteReplacement(triggerValue);
190192
data = PIN_PATTERN.matcher(data).replaceFirst(quotedValue);
@@ -214,6 +216,7 @@ private static String format(String data, String triggerValue) {
214216
default :
215217
data = GENERIC_PLACEHOLDER.matcher(data).replaceFirst(quotedValue);
216218
data = DATETIME_PATTERN.matcher(data).replaceFirst(Instant.now().toString());
219+
data = DEVICE_OWNER_EMAIL.matcher(data).replaceFirst(ownerEmail);
217220
}
218221
return data;
219222
}

server/utils/src/main/java/cc/blynk/utils/StringUtils.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ public final class StringUtils {
3434
private static final Pattern NOT_SUPPORTED_CHARS = Pattern.compile("[\\\\/:*?\"<>| ]");
3535

3636
public static final Pattern DATETIME_PATTERN = Pattern.compile("/datetime_iso/", Pattern.LITERAL);
37+
public static final Pattern DEVICE_OWNER_EMAIL = Pattern.compile("device_owner_email", Pattern.LITERAL);
3738
public static final String WEBSOCKET_PATH = "/websocket";
3839
public static final String WEBSOCKET_WEB_PATH = "/dashws";
3940

0 commit comments

Comments
 (0)