Skip to content

Commit

Permalink
fix: Trigger url evaluation requiring CORS access (RocketChat#29304)
Browse files Browse the repository at this point in the history
  • Loading branch information
aleksandernsilva authored Jun 13, 2023
1 parent 40d7f79 commit c0cb917
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .changeset/strong-fans-exist.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@rocket.chat/livechat': patch
---

Fixes Livechat page-url triggers requiring CORS to access the parent url
3 changes: 3 additions & 0 deletions packages/livechat/src/lib/hooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,9 @@ const api = {
store.setState({ minimized: false });
parentCall('openWidget');
},
setParentUrl(parentUrl) {
store.setState({ parentUrl });
},
};

const onNewMessage = (event) => {
Expand Down
2 changes: 1 addition & 1 deletion packages/livechat/src/lib/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ export const getUnreadMessages = () => {
.findIndex((item) => item.u._id === user?._id);

if (lastReadMessageIndex !== -1) {
const unreadMessages = renderedMessages.slice(lastReadMessageIndex + 1).filter((message) => message.u._id !== user._id);
const unreadMessages = renderedMessages.slice(lastReadMessageIndex + 1).filter((message) => message.u._id !== user?._id);

return unreadMessages;
}
Expand Down
29 changes: 28 additions & 1 deletion packages/livechat/src/lib/triggers.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,12 @@ class Triggers {
}
});
});

store.on('change', ([state, prevState]) => {
if (prevState.parentUrl !== state.parentUrl) {
this.processPageUrlTriggers();
}
});
}

async fire(trigger) {
Expand Down Expand Up @@ -162,11 +168,13 @@ class Triggers {
if (trigger.skip) {
return;
}

trigger.conditions.forEach((condition) => {
switch (condition.name) {
case 'page-url':
const { parentUrl } = store.state;
const hrefRegExp = new RegExp(condition.value, 'g');
if (hrefRegExp.test(window.parent.location.href)) {
if (parentUrl && hrefRegExp.test(parentUrl)) {
this.fire(trigger);
}
break;
Expand All @@ -188,6 +196,25 @@ class Triggers {
this._requests = [];
}

processPageUrlTriggers() {
const { parentUrl } = store.state;

if (!parentUrl) return;

this._triggers.forEach((trigger) => {
if (trigger.skip) return;

trigger.conditions.forEach((condition) => {
if (condition.name !== 'page-url') return;

const hrefRegExp = new RegExp(condition.value, 'g');
if (hrefRegExp.test(parentUrl)) {
this.fire(trigger);
}
});
});
}

set triggers(newTriggers) {
this._triggers = [...newTriggers];
}
Expand Down
13 changes: 12 additions & 1 deletion packages/livechat/src/store/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,18 @@ export const initialState = () => ({
businessUnit: null,
});

const dontPersist = ['messages', 'typing', 'loading', 'alerts', 'unread', 'noMoreMessages', 'modal', 'incomingCallAlert', 'ongoingCall'];
const dontPersist = [
'messages',
'typing',
'loading',
'alerts',
'unread',
'noMoreMessages',
'modal',
'incomingCallAlert',
'ongoingCall',
'parentUrl',
];
export const store = new Store(initialState(), { dontPersist });

if (process.env.NODE_ENV === 'development') {
Expand Down
9 changes: 9 additions & 0 deletions packages/livechat/src/widget.js
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ const createWidget = (url) => {
smallScreen = matches;
updateWidgetStyle(widget.dataset.state === 'opened');
callHook('setExpanded', smallScreen);
callHook('setParentUrl', window.location.href);
};

const mediaQueryList = window.matchMedia('screen and (max-device-width: 480px)');
Expand Down Expand Up @@ -318,6 +319,10 @@ function minimizeWidget() {
callHook('minimizeWidget');
}

function setParentUrl(url) {
callHook('setParentUrl', url);
}

function initialize(params) {
for (const method in params) {
if (!params.hasOwnProperty(method)) {
Expand Down Expand Up @@ -367,6 +372,9 @@ function initialize(params) {
case 'agent':
setAgent(params[method]);
continue;
case 'parentUrl':
setParentUrl(params[method]);
continue;
default:
continue;
}
Expand Down Expand Up @@ -457,6 +465,7 @@ window.RocketChat.livechat = {
minimizeWidget,
setBusinessUnit,
clearBusinessUnit,
setParentUrl,

// callbacks
onChatMaximized(fn) {
Expand Down

0 comments on commit c0cb917

Please sign in to comment.