From 90b3d4dee79db7f53bbaf7e976f16b40daa41916 Mon Sep 17 00:00:00 2001 From: Jonathan Cobb Date: Fri, 14 Aug 2020 08:55:57 -0400 Subject: [PATCH] add icons using DOM manipulation instead of appending to innerHtml --- .../app/bblock/BubbleBlockAppConfigDriver.java | 2 +- .../main/java/bubble/model/app/AppRule.java | 4 ++-- .../bubble/rule/AbstractAppRuleDriver.java | 18 ++++++++++++++---- .../main/java/bubble/rule/AppRuleDriver.java | 3 ++- .../rule/bblock/BubbleBlockRuleDriver.java | 4 +++- .../stream/StandardAppPrimerService.java | 2 +- .../stream/StandardRuleEngineService.java | 12 ++++++++++-- .../rule/RequestModifierRule_icon.js.hbs | 13 ++++++++++--- .../bblock/BubbleBlockRuleDriver_stats.js.hbs | 6 ++++-- .../block/JsUserBlockerRuleDriver.js.hbs | 4 +++- 10 files changed, 50 insertions(+), 18 deletions(-) diff --git a/bubble-server/src/main/java/bubble/app/bblock/BubbleBlockAppConfigDriver.java b/bubble-server/src/main/java/bubble/app/bblock/BubbleBlockAppConfigDriver.java index 88b0d9b9..5b7a2a37 100644 --- a/bubble-server/src/main/java/bubble/app/bblock/BubbleBlockAppConfigDriver.java +++ b/bubble-server/src/main/java/bubble/app/bblock/BubbleBlockAppConfigDriver.java @@ -190,7 +190,7 @@ public class BubbleBlockAppConfigDriver extends AppConfigDriverBase { try { final AppRule rule = loadRule(account, app); final RuleDriver ruleDriver = loadDriver(account, rule, BubbleBlockRuleDriver.class); - final BubbleBlockRuleDriver unwiredDriver = (BubbleBlockRuleDriver) rule.initDriver(ruleDriver, TEST_MATCHER, account, TEST_DEVICE); + final BubbleBlockRuleDriver unwiredDriver = (BubbleBlockRuleDriver) rule.initDriver(app, ruleDriver, TEST_MATCHER, account, TEST_DEVICE); final BubbleBlockRuleDriver driver = configuration.autowire(unwiredDriver); final BlockDecision decision = driver.getDecision(host, path, userAgent, primary); return getBuiltinList(account, app).setResponse(decision); diff --git a/bubble-server/src/main/java/bubble/model/app/AppRule.java b/bubble-server/src/main/java/bubble/model/app/AppRule.java index b51263cf..b0afd903 100644 --- a/bubble-server/src/main/java/bubble/model/app/AppRule.java +++ b/bubble-server/src/main/java/bubble/model/app/AppRule.java @@ -90,9 +90,9 @@ public class AppRule extends IdentifiableBaseParentEntity implements AppTemplate @Column(nullable=false, length=UUID_MAXLEN) @Getter @Setter private String driver; - public AppRuleDriver initDriver(RuleDriver driver, AppMatcher matcher, Account account, Device device) { + public AppRuleDriver initDriver(BubbleApp app, RuleDriver driver, AppMatcher matcher, Account account, Device device) { final AppRuleDriver d = driver.getDriver(); - d.init(json(configJson, JsonNode.class), driver.getUserConfig(), this, matcher, account, device); + d.init(json(configJson, JsonNode.class), driver.getUserConfig(), app, this, matcher, account, device); return d; } diff --git a/bubble-server/src/main/java/bubble/rule/AbstractAppRuleDriver.java b/bubble-server/src/main/java/bubble/rule/AbstractAppRuleDriver.java index 929cdc81..05e0ec41 100644 --- a/bubble-server/src/main/java/bubble/rule/AbstractAppRuleDriver.java +++ b/bubble-server/src/main/java/bubble/rule/AbstractAppRuleDriver.java @@ -11,6 +11,7 @@ import bubble.dao.device.DeviceDAO; import bubble.model.account.Account; import bubble.model.app.AppMatcher; import bubble.model.app.AppRule; +import bubble.model.app.BubbleApp; import bubble.model.device.Device; import bubble.resources.stream.FilterHttpRequest; import bubble.server.BubbleConfiguration; @@ -60,6 +61,7 @@ public abstract class AbstractAppRuleDriver implements AppRuleDriver { protected JsonNode config; protected JsonNode userConfig; + protected BubbleApp app; protected AppMatcher matcher; protected AppRule rule; protected Account account; @@ -76,12 +78,14 @@ public abstract class AbstractAppRuleDriver implements AppRuleDriver { @Override public void init(JsonNode config, JsonNode userConfig, + BubbleApp app, AppRule rule, AppMatcher matcher, Account account, Device device) { this.config = config; this.userConfig = userConfig; + this.app = app; this.matcher = matcher; this.rule = rule; this.account = account; @@ -175,22 +179,28 @@ public abstract class AbstractAppRuleDriver implements AppRuleDriver { public static final String CTX_JS_PREFIX = "JS_PREFIX"; public static final String CTX_PAGE_PREFIX = "PAGE_PREFIX"; + public static final String CTX_PAGE_ONREADY_INTERVAL = "PAGE_ONREADY_INTERVAL"; public static final String CTX_BUBBLE_REQUEST_ID = "BUBBLE_REQUEST_ID"; public static final String CTX_BUBBLE_DATA_ID = "BUBBLE_DATA_ID"; public static final String CTX_BUBBLE_HOME = "BUBBLE_HOME"; - public static final String CTX_SITE = "SITE"; + public static final String CTX_BUBBLE_SITE_NAME = "BUBBLE_SITE_NAME"; + public static final String CTX_BUBBLE_APP_NAME = "BUBBLE_APP_NAME"; public static final String CTX_ICON_JS = "ICON_JS"; - private String getPagePrefix(String requestId) { return "__bubble_"+sha256_hex(requestId); } - private String getJsPrefix(String requestId) { return "__bubble_"+sha256_hex(requestId+"_"+getClass().getName()); } + public static final int PAGE_ONREADY_INTERVAL = 50; + + private String getPagePrefix(String requestId) { return "__bubble_page_"+sha256_hex(requestId); } + private String getJsPrefix(String requestId) { return "__bubble_js_"+sha256_hex(requestId+"_"+getClass().getName()); } protected Map getBubbleJsContext(String requestId, Map filterCtx) { final Map ctx = new HashMap<>(); ctx.put(CTX_PAGE_PREFIX, getPagePrefix(requestId)); ctx.put(CTX_JS_PREFIX, getJsPrefix(requestId)); + ctx.put(CTX_PAGE_ONREADY_INTERVAL, PAGE_ONREADY_INTERVAL); ctx.put(CTX_BUBBLE_REQUEST_ID, requestId); ctx.put(CTX_BUBBLE_HOME, configuration.getPublicUriBase()); - ctx.put(CTX_SITE, getSiteName(matcher)); + ctx.put(CTX_BUBBLE_SITE_NAME, getSiteName(matcher)); + ctx.put(CTX_BUBBLE_APP_NAME, app.getName()); ctx.put(CTX_BUBBLE_DATA_ID, getDataId(requestId)); return ctx; } diff --git a/bubble-server/src/main/java/bubble/rule/AppRuleDriver.java b/bubble-server/src/main/java/bubble/rule/AppRuleDriver.java index 765059ee..a7208e1c 100644 --- a/bubble-server/src/main/java/bubble/rule/AppRuleDriver.java +++ b/bubble-server/src/main/java/bubble/rule/AppRuleDriver.java @@ -7,6 +7,7 @@ package bubble.rule; import bubble.model.account.Account; import bubble.model.app.AppMatcher; import bubble.model.app.AppRule; +import bubble.model.app.BubbleApp; import bubble.model.device.Device; import bubble.resources.stream.FilterHttpRequest; import bubble.resources.stream.FilterMatchersRequest; @@ -30,7 +31,6 @@ import static org.apache.commons.lang3.RandomStringUtils.randomAlphanumeric; import static org.cobbzilla.util.daemon.ZillaRuntime.now; import static org.cobbzilla.util.io.StreamUtil.stream2bytes; import static org.cobbzilla.util.io.StreamUtil.stream2string; -import static org.cobbzilla.util.security.ShaUtil.sha256_hex; import static org.cobbzilla.util.string.StringUtil.getPackagePath; public interface AppRuleDriver { @@ -73,6 +73,7 @@ public interface AppRuleDriver { default void init(JsonNode config, JsonNode userConfig, + BubbleApp app, AppRule rule, AppMatcher matcher, Account account, diff --git a/bubble-server/src/main/java/bubble/rule/bblock/BubbleBlockRuleDriver.java b/bubble-server/src/main/java/bubble/rule/bblock/BubbleBlockRuleDriver.java index 52805821..9ced7d91 100644 --- a/bubble-server/src/main/java/bubble/rule/bblock/BubbleBlockRuleDriver.java +++ b/bubble-server/src/main/java/bubble/rule/bblock/BubbleBlockRuleDriver.java @@ -8,6 +8,7 @@ import bubble.abp.*; import bubble.model.account.Account; import bubble.model.app.AppMatcher; import bubble.model.app.AppRule; +import bubble.model.app.BubbleApp; import bubble.model.device.Device; import bubble.resources.stream.FilterHttpRequest; import bubble.resources.stream.FilterMatchersRequest; @@ -68,11 +69,12 @@ public class BubbleBlockRuleDriver extends TrafficAnalyticsRuleDriver implements @Override public void init(JsonNode config, JsonNode userConfig, + BubbleApp app, AppRule rule, AppMatcher matcher, Account account, Device device) { - super.init(config, userConfig, rule, matcher, account, device); + super.init(config, userConfig, app, rule, matcher, account, device); refreshBlockLists(); } diff --git a/bubble-server/src/main/java/bubble/service/stream/StandardAppPrimerService.java b/bubble-server/src/main/java/bubble/service/stream/StandardAppPrimerService.java index e9df6450..a37ecc4b 100644 --- a/bubble-server/src/main/java/bubble/service/stream/StandardAppPrimerService.java +++ b/bubble-server/src/main/java/bubble/service/stream/StandardAppPrimerService.java @@ -137,7 +137,7 @@ public class StandardAppPrimerService implements AppPrimerService { final Set blockDomains = new HashSet<>(); final Set filterDomains = new HashSet<>(); for (AppMatcher matcher : matchers) { - final AppRuleDriver appRuleDriver = rule.initDriver(driver, matcher, account, device); + final AppRuleDriver appRuleDriver = rule.initDriver(app, driver, matcher, account, device); final Set blocks = appRuleDriver.getPrimedBlockDomains(); if (empty(blocks)) { log.debug("_prime: no blockDomains for device/app/rule/matcher: " + device.getName() + "/" + app.getName() + "/" + rule.getName() + "/" + matcher.getName()); diff --git a/bubble-server/src/main/java/bubble/service/stream/StandardRuleEngineService.java b/bubble-server/src/main/java/bubble/service/stream/StandardRuleEngineService.java index f103dae8..1260aa9f 100644 --- a/bubble-server/src/main/java/bubble/service/stream/StandardRuleEngineService.java +++ b/bubble-server/src/main/java/bubble/service/stream/StandardRuleEngineService.java @@ -5,10 +5,12 @@ package bubble.service.stream; import bubble.dao.app.AppRuleDAO; +import bubble.dao.app.BubbleAppDAO; import bubble.dao.app.RuleDriverDAO; import bubble.model.account.Account; import bubble.model.app.AppMatcher; import bubble.model.app.AppRule; +import bubble.model.app.BubbleApp; import bubble.model.app.RuleDriver; import bubble.model.device.Device; import bubble.resources.stream.FilterHttpRequest; @@ -75,6 +77,7 @@ public class StandardRuleEngineService implements RuleEngineService { public static final String HEADER_PASSTHRU = "X-Bubble-Passthru"; + @Autowired private BubbleAppDAO appDAO; @Autowired private AppRuleDAO ruleDAO; @Autowired private RuleDriverDAO driverDAO; @Autowired private BubbleConfiguration configuration; @@ -243,10 +246,15 @@ public class StandardRuleEngineService implements RuleEngineService { for (AppRuleHarness h : rules) { final RuleDriver ruleDriver = driverDAO.findByUuid(h.getRule().getDriver()); if (ruleDriver == null) { - log.warn("initRules: driver not found: "+h.getRule().getDriver()); + log.warn("initRuleHarnesses: driver not found: "+h.getRule().getDriver()); continue; } - final AppRuleDriver unwiredDriver = h.getRule().initDriver(ruleDriver, h.getMatcher(), account, device); + final BubbleApp app = appDAO.findByAccountAndId(account.getUuid(), h.getRule().getApp()); + if (app == null) { + log.warn("initRuleHarnesses: app not found: "+h.getRule().getApp()); + continue; + } + final AppRuleDriver unwiredDriver = h.getRule().initDriver(app, ruleDriver, h.getMatcher(), account, device); final AppRuleDriver driver = configuration.autowire(unwiredDriver); h.setRuleDriver(ruleDriver); h.setDriver(driver); diff --git a/bubble-server/src/main/resources/bubble/rule/RequestModifierRule_icon.js.hbs b/bubble-server/src/main/resources/bubble/rule/RequestModifierRule_icon.js.hbs index a9406cad..074d5ae5 100644 --- a/bubble-server/src/main/resources/bubble/rule/RequestModifierRule_icon.js.hbs +++ b/bubble-server/src/main/resources/bubble/rule/RequestModifierRule_icon.js.hbs @@ -1,7 +1,6 @@ if (typeof {{PAGE_PREFIX}}_icon_status === 'undefined') { let {{PAGE_PREFIX}}_doc_ready = false; - const {{PAGE_PREFIX}}_interval = 50; {{PAGE_PREFIX}}_icon_status = []; @@ -12,7 +11,7 @@ if (typeof {{PAGE_PREFIX}}_icon_status === 'undefined') { window.clearInterval(intervalId); callback.call(this); } - }, {{PAGE_PREFIX}}_interval); + }, {{PAGE_ONREADY_INTERVAL}}); } {{PAGE_PREFIX}}_onReady(function() { @@ -28,7 +27,15 @@ if (typeof {{PAGE_PREFIX}}_icon_status === 'undefined') { document.getElementsByTagName('body')[0].appendChild(bubbleControlDiv); } for (let i=0; i<{{PAGE_PREFIX}}_icon_status.length; i++) { - bubbleControlDiv.innerHTML = bubbleControlDiv.innerHTML + {{PAGE_PREFIX}}_icon_status[i].iconHtml; + let br = document.createElement('br'); + let link = document.createElement('a'); + link.href = '{{{BUBBLE_HOME}}}/app/' + {{PAGE_PREFIX}}_icon_status[i].app + '/' + {{PAGE_PREFIX}}_icon_status[i].link; + let img = document.createElement('img'); + img.src = '/__bubble/api/filter/assets/{{BUBBLE_REQUEST_ID}}/' + {{PAGE_PREFIX}}_icon_status[i].app + '/' + {{PAGE_PREFIX}}_icon_status[i].icon + '?raw=true'; + img.width = 64; + link.appendChild(img); + bubbleControlDiv.appendChild(br); + bubbleControlDiv.appendChild(link); } }); } \ No newline at end of file diff --git a/bubble-server/src/main/resources/bubble/rule/bblock/BubbleBlockRuleDriver_stats.js.hbs b/bubble-server/src/main/resources/bubble/rule/bblock/BubbleBlockRuleDriver_stats.js.hbs index 1e6fe0c6..5124a58c 100644 --- a/bubble-server/src/main/resources/bubble/rule/bblock/BubbleBlockRuleDriver_stats.js.hbs +++ b/bubble-server/src/main/resources/bubble/rule/bblock/BubbleBlockRuleDriver_stats.js.hbs @@ -5,6 +5,8 @@ {{PAGE_PREFIX}}_icon_status.push({ jsPrefix: '{{JS_PREFIX}}', - iconHtml: '
' + app: '{{BUBBLE_APP_NAME}}', + link: 'view/last_24_hours', + icon: 'icon-gray' }); -console.log("BubbleBlock pushed icon, {{PAGE_PREFIX}}_icon_status="+JSON.stringify({{PAGE_PREFIX}}_icon_status)); \ No newline at end of file +console.log("BubbleBlock pushed icon, {{PAGE_PREFIX}}_icon_status="+JSON.stringify({{PAGE_PREFIX}}_icon_status)); diff --git a/bubble-server/src/main/resources/bubble/rule/social/block/JsUserBlockerRuleDriver.js.hbs b/bubble-server/src/main/resources/bubble/rule/social/block/JsUserBlockerRuleDriver.js.hbs index 00857ade..fdb037e1 100644 --- a/bubble-server/src/main/resources/bubble/rule/social/block/JsUserBlockerRuleDriver.js.hbs +++ b/bubble-server/src/main/resources/bubble/rule/social/block/JsUserBlockerRuleDriver.js.hbs @@ -55,6 +55,8 @@ function {{JS_PREFIX}}_block_user (author) { {{PAGE_PREFIX}}_icon_status.push({ jsPrefix: '{{JS_PREFIX}}', - iconHtml: '
' + app: '{{BUBBLE_APP_NAME}}', + link: 'site/{{BUBBLE_SITE_NAME}}/view/blocked_users', + icon: 'icon' }); console.log("JsUserBlocker pushed icon, {{PAGE_PREFIX}}_icon_status="+JSON.stringify({{PAGE_PREFIX}}_icon_status)); \ No newline at end of file