Skip to content

Commit

Permalink
Merge pull request #8104 from Expensify/marcaaron-cleanupPusher
Browse files Browse the repository at this point in the history
Remove duplication in Report.js
  • Loading branch information
Tim Szot authored Mar 15, 2022
2 parents b9c7fd0 + f083145 commit 7acbc16
Showing 1 changed file with 49 additions and 86 deletions.
135 changes: 49 additions & 86 deletions src/libs/actions/Report.js
Original file line number Diff line number Diff line change
Expand Up @@ -711,6 +711,46 @@ function getReportChannelName(reportID) {
return `private-report-reportID-${reportID}`;
}

/**
* Abstraction around subscribing to private user channel events. Handles all logs and errors automatically.
*
* @param {String} eventName
* @param {Function} onEvent
* @param {Boolean} isChunked
*/
function subscribeToPrivateUserChannelEvent(eventName, onEvent, isChunked = false) {
const pusherChannelName = `private-user-accountID-${currentUserAccountID}`;

/**
* @param {Object} pushJSON
*/
function logPusherEvent(pushJSON) {
Log.info(`[Report] Handled ${eventName} event sent by Pusher`, false, {reportID: pushJSON.reportID, reportActionID: pushJSON.reportActionID});
}

function onPusherResubscribeToPrivateUserChannel() {
NetworkConnection.triggerReconnectionCallbacks('Pusher re-subscribed to private user channel');
}

/**
* @param {*} pushJSON
*/
function onEventPush(pushJSON) {
logPusherEvent(pushJSON);
onEvent(pushJSON);
}

/**
* @param {*} error
*/
function onSubscriptionFailed(error) {
Log.info('[Report] Failed to subscribe to Pusher channel', false, {error, pusherChannelName, eventName});
}

Pusher.subscribe(pusherChannelName, eventName, onEventPush, isChunked, onPusherResubscribeToPrivateUserChannel)
.catch(onSubscriptionFailed);
}

/**
* Initialize our pusher subscriptions to listen for new report comments and pin toggles
*/
Expand All @@ -726,100 +766,23 @@ function subscribeToUserEvents() {
}

// Live-update a report's actions when a 'report comment' event is received.
Pusher.subscribe(pusherChannelName, Pusher.TYPE.REPORT_COMMENT, (pushJSON) => {
Log.info(
`[Report] Handled ${Pusher.TYPE.REPORT_COMMENT} event sent by Pusher`, false, {reportID: pushJSON.reportID},
);
updateReportWithNewAction(pushJSON.reportID, pushJSON.reportAction, pushJSON.notificationPreference);
}, false,
() => {
NetworkConnection.triggerReconnectionCallbacks('pusher re-subscribed to private user channel');
})
.catch((error) => {
Log.info(
'[Report] Failed to subscribe to Pusher channel',
false,
{error, pusherChannelName, eventName: Pusher.TYPE.REPORT_COMMENT},
);
});
subscribeToPrivateUserChannelEvent(Pusher.TYPE.REPORT_COMMENT, pushJSON => updateReportWithNewAction(pushJSON.reportID, pushJSON.reportAction, pushJSON.notificationPreference));

// Live-update a report's actions when a 'chunked report comment' event is received.
Pusher.subscribe(pusherChannelName, Pusher.TYPE.REPORT_COMMENT_CHUNK, (pushJSON) => {
Log.info(
`[Report] Handled ${Pusher.TYPE.REPORT_COMMENT_CHUNK} event sent by Pusher`, false, {reportID: pushJSON.reportID},
);
updateReportWithNewAction(pushJSON.reportID, pushJSON.reportAction, pushJSON.notificationPreference);
}, true,
() => {
NetworkConnection.triggerReconnectionCallbacks('pusher re-subscribed to private user channel');
})
.catch((error) => {
Log.info(
'[Report] Failed to subscribe to Pusher channel',
false,
{error, pusherChannelName, eventName: Pusher.TYPE.REPORT_COMMENT_CHUNK},
);
});
subscribeToPrivateUserChannelEvent(
Pusher.TYPE.REPORT_COMMENT_CHUNK,
pushJSON => updateReportWithNewAction(pushJSON.reportID, pushJSON.reportAction, pushJSON.notificationPreference),
true,
);

// Live-update a report's actions when an 'edit comment' event is received.
Pusher.subscribe(pusherChannelName, Pusher.TYPE.REPORT_COMMENT_EDIT, (pushJSON) => {
Log.info(
`[Report] Handled ${Pusher.TYPE.REPORT_COMMENT_EDIT} event sent by Pusher`, false, {
reportActionID: pushJSON.reportActionID,
},
);
updateReportActionMessage(pushJSON.reportID, pushJSON.sequenceNumber, pushJSON.message);
}, false,
() => {
NetworkConnection.triggerReconnectionCallbacks('pusher re-subscribed to private user channel');
})
.catch((error) => {
Log.info(
'[Report] Failed to subscribe to Pusher channel',
false,
{error, pusherChannelName, eventName: Pusher.TYPE.REPORT_COMMENT_EDIT},
);
});
subscribeToPrivateUserChannelEvent(Pusher.TYPE.REPORT_COMMENT_EDIT, pushJSON => updateReportActionMessage(pushJSON.reportID, pushJSON.sequenceNumber, pushJSON.message));

// Live-update a report's actions when an 'edit comment chunk' event is received.
Pusher.subscribe(pusherChannelName, Pusher.TYPE.REPORT_COMMENT_EDIT_CHUNK, (pushJSON) => {
Log.info(
`[Report] Handled ${Pusher.TYPE.REPORT_COMMENT_EDIT_CHUNK} event sent by Pusher`, false, {
reportActionID: pushJSON.reportActionID,
},
);
updateReportActionMessage(pushJSON.reportID, pushJSON.sequenceNumber, pushJSON.message);
}, true,
() => {
NetworkConnection.triggerReconnectionCallbacks('pusher re-subscribed to private user channel');
})
.catch((error) => {
Log.info(
'[Report] Failed to subscribe to Pusher channel',
false,
{error, pusherChannelName, eventName: Pusher.TYPE.REPORT_COMMENT_EDIT_CHUNK},
);
});
subscribeToPrivateUserChannelEvent(Pusher.TYPE.REPORT_COMMENT_EDIT_CHUNK, pushJSON => updateReportActionMessage(pushJSON.reportID, pushJSON.sequenceNumber, pushJSON.message), true);

// Live-update a report's pinned state when a 'report toggle pinned' event is received.
Pusher.subscribe(pusherChannelName, Pusher.TYPE.REPORT_TOGGLE_PINNED, (pushJSON) => {
Log.info(
`[Report] Handled ${Pusher.TYPE.REPORT_TOGGLE_PINNED} event sent by Pusher`,
false,
{reportID: pushJSON.reportID},
);
updateReportPinnedState(pushJSON.reportID, pushJSON.isPinned);
}, false,
() => {
NetworkConnection.triggerReconnectionCallbacks('pusher re-subscribed to private user channel');
})
.catch((error) => {
Log.info(
'[Report] Failed to subscribe to Pusher channel',
false,
{error, pusherChannelName, eventName: Pusher.TYPE.REPORT_TOGGLE_PINNED},
);
});
subscribeToPrivateUserChannelEvent(Pusher.TYPE.REPORT_TOGGLE_PINNED, pushJSON => updateReportPinnedState(pushJSON.reportID, pushJSON.isPinned));
}

/**
Expand Down

0 comments on commit 7acbc16

Please sign in to comment.