Skip to content

Commit

Permalink
#1344 added additional placeholder for webhook
Browse files Browse the repository at this point in the history
  • Loading branch information
doom369 committed Sep 25, 2019
1 parent bd2a1b8 commit 7c975d4
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ protected void processEventorAndWebhook(User user, DashBoard dash, int deviceId,
PinType pinType, String value, long now) {
try {
eventorProcessor.process(user, session, dash, deviceId, pin, pinType, value, now);
webhookProcessor.process(session, dash, deviceId, pin, pinType, value, now);
webhookProcessor.process(user, session, dash, deviceId, pin, pinType, value, now);
} catch (QuotaLimitException qle) {
log.debug("User {} reached notification limit for eventor/webhook.", user.name);
} catch (IllegalArgumentException iae) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import cc.blynk.server.core.model.DashBoard;
import cc.blynk.server.core.model.DataStream;
import cc.blynk.server.core.model.auth.Session;
import cc.blynk.server.core.model.auth.User;
import cc.blynk.server.core.model.enums.PinType;
import cc.blynk.server.core.model.widgets.others.webhook.Header;
import cc.blynk.server.core.model.widgets.others.webhook.WebHook;
Expand All @@ -24,6 +25,7 @@

import static cc.blynk.server.core.protocol.enums.Command.WEB_HOOKS;
import static cc.blynk.utils.StringUtils.DATETIME_PATTERN;
import static cc.blynk.utils.StringUtils.DEVICE_OWNER_EMAIL;
import static cc.blynk.utils.StringUtils.GENERIC_PLACEHOLDER;
import static cc.blynk.utils.StringUtils.PIN_PATTERN;
import static cc.blynk.utils.StringUtils.PIN_PATTERN_0;
Expand Down Expand Up @@ -68,7 +70,7 @@ public WebhookProcessor(DefaultAsyncHttpClient httpclient,
this.webhookFailureLimit = failureLimit;
}

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

if (webhook.isNotFailed(webhookFailureLimit) && webhook.url != null) {
process(session, dash.id, deviceId, webhook, triggerValue);
process(user, session, dash.id, deviceId, webhook, triggerValue);
}
}

private void process(Session session, int dashId, int deviceId, WebHook webHook, String triggerValue) {
String newUrl = format(webHook.url, triggerValue);
private void process(User user, Session session, int dashId, int deviceId, WebHook webHook, String triggerValue) {
String newUrl = format(webHook.url, triggerValue, user.email);

if (!WebHook.isValidUrl(newUrl)) {
return;
Expand Down Expand Up @@ -114,7 +116,7 @@ private void process(Session session, int dashId, int deviceId, WebHook webHook
builder.setHeader(header.name, header.value);
if (webHook.body != null && !webHook.body.isEmpty()) {
if (CONTENT_TYPE.equals(header.name)) {
String newBody = format(webHook.body, triggerValue);
String newBody = format(webHook.body, triggerValue, user.email);
log.trace("Webhook formatted body : {}", newBody);
builder.setBody(newBody);
}
Expand All @@ -123,7 +125,7 @@ private void process(Session session, int dashId, int deviceId, WebHook webHook
}
}

log.trace("Sending webhook. ", webHook);
log.trace("Sending webhook. {}", webHook);
builder.execute(new AsyncCompletionHandler<Response>() {

private int length = 0;
Expand Down Expand Up @@ -184,7 +186,7 @@ private static boolean isValidResponseCode(int responseCode) {
}
}

private static String format(String data, String triggerValue) {
private static String format(String data, String triggerValue, String ownerEmail) {
//this is an ugly hack to make it work with Blynk HTTP API.
String quotedValue = Matcher.quoteReplacement(triggerValue);
data = PIN_PATTERN.matcher(data).replaceFirst(quotedValue);
Expand Down Expand Up @@ -214,6 +216,7 @@ private static String format(String data, String triggerValue) {
default :
data = GENERIC_PLACEHOLDER.matcher(data).replaceFirst(quotedValue);
data = DATETIME_PATTERN.matcher(data).replaceFirst(Instant.now().toString());
data = DEVICE_OWNER_EMAIL.matcher(data).replaceFirst(ownerEmail);
}
return data;
}
Expand Down
1 change: 1 addition & 0 deletions server/utils/src/main/java/cc/blynk/utils/StringUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ public final class StringUtils {
private static final Pattern NOT_SUPPORTED_CHARS = Pattern.compile("[\\\\/:*?\"<>| ]");

public static final Pattern DATETIME_PATTERN = Pattern.compile("/datetime_iso/", Pattern.LITERAL);
public static final Pattern DEVICE_OWNER_EMAIL = Pattern.compile("device_owner_email", Pattern.LITERAL);
public static final String WEBSOCKET_PATH = "/websocket";
public static final String WEBSOCKET_WEB_PATH = "/dashws";

Expand Down

0 comments on commit 7c975d4

Please sign in to comment.