-
Notifications
You must be signed in to change notification settings - Fork 3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #24407 from wildan-m/wildan/fix/21518/client-pushe…
…r-method Wildan/fix/21518/client pusher method
- Loading branch information
Showing
4 changed files
with
133 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -103,24 +103,24 @@ function getReportChannelName(reportID) { | |
} | ||
|
||
/** | ||
* There are 2 possibilities that we can receive via pusher for a user's typing status: | ||
* There are 2 possibilities that we can receive via pusher for a user's typing/leaving status: | ||
* 1. The "new" way from New Expensify is passed as {[login]: Boolean} (e.g. {[email protected]: true}), where the value | ||
* is whether the user with that login is typing on the report or not. | ||
* is whether the user with that login is typing/leaving on the report or not. | ||
* 2. The "old" way from e.com which is passed as {userLogin: login} (e.g. {userLogin: [email protected]}) | ||
* | ||
* This method makes sure that no matter which we get, we return the "new" format | ||
* | ||
* @param {Object} typingStatus | ||
* @param {Object} status | ||
* @returns {Object} | ||
*/ | ||
function getNormalizedTypingStatus(typingStatus) { | ||
let normalizedTypingStatus = typingStatus; | ||
function getNormalizedStatus(status) { | ||
let normalizedStatus = status; | ||
|
||
if (_.first(_.keys(typingStatus)) === 'userLogin') { | ||
normalizedTypingStatus = {[typingStatus.userLogin]: true}; | ||
if (_.first(_.keys(status)) === 'userLogin') { | ||
normalizedStatus = {[status.userLogin]: true}; | ||
} | ||
|
||
return normalizedTypingStatus; | ||
return normalizedStatus; | ||
} | ||
|
||
/** | ||
|
@@ -141,7 +141,7 @@ function subscribeToReportTypingEvents(reportID) { | |
// If the pusher message comes from OldDot, we expect the typing status to be keyed by user | ||
// login OR by 'Concierge'. If the pusher message comes from NewDot, it is keyed by accountID | ||
// since personal details are keyed by accountID. | ||
const normalizedTypingStatus = getNormalizedTypingStatus(typingStatus); | ||
const normalizedTypingStatus = getNormalizedStatus(typingStatus); | ||
const accountIDOrLogin = _.first(_.keys(normalizedTypingStatus)); | ||
|
||
if (!accountIDOrLogin) { | ||
|
@@ -170,6 +170,41 @@ function subscribeToReportTypingEvents(reportID) { | |
}); | ||
} | ||
|
||
/** | ||
* Initialize our pusher subscriptions to listen for someone leaving a room. | ||
* | ||
* @param {String} reportID | ||
*/ | ||
function subscribeToReportLeavingEvents(reportID) { | ||
if (!reportID) { | ||
return; | ||
} | ||
|
||
// Make sure we have a clean Leaving indicator before subscribing to leaving events | ||
Onyx.set(`${ONYXKEYS.COLLECTION.REPORT_USER_IS_LEAVING_ROOM}${reportID}`, false); | ||
|
||
const pusherChannelName = getReportChannelName(reportID); | ||
Pusher.subscribe(pusherChannelName, Pusher.TYPE.USER_IS_LEAVING_ROOM, (leavingStatus) => { | ||
// If the pusher message comes from OldDot, we expect the leaving status to be keyed by user | ||
// login OR by 'Concierge'. If the pusher message comes from NewDot, it is keyed by accountID | ||
// since personal details are keyed by accountID. | ||
const normalizedLeavingStatus = getNormalizedStatus(leavingStatus); | ||
const accountIDOrLogin = _.first(_.keys(normalizedLeavingStatus)); | ||
|
||
if (!accountIDOrLogin) { | ||
return; | ||
} | ||
|
||
if (Number(accountIDOrLogin) !== currentUserAccountID) { | ||
return; | ||
} | ||
|
||
Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT_USER_IS_LEAVING_ROOM}${reportID}`, true); | ||
}).catch((error) => { | ||
Log.hmmm('[Report] Failed to initially subscribe to Pusher channel', false, {errorType: error.type, pusherChannelName}); | ||
}); | ||
} | ||
|
||
/** | ||
* Remove our pusher subscriptions to listen for someone typing in a report. | ||
* | ||
|
@@ -185,6 +220,21 @@ function unsubscribeFromReportChannel(reportID) { | |
Pusher.unsubscribe(pusherChannelName, Pusher.TYPE.USER_IS_TYPING); | ||
} | ||
|
||
/** | ||
* Remove our pusher subscriptions to listen for someone leaving a report. | ||
* | ||
* @param {String} reportID | ||
*/ | ||
function unsubscribeFromLeavingRoomReportChannel(reportID) { | ||
if (!reportID) { | ||
return; | ||
} | ||
|
||
const pusherChannelName = getReportChannelName(reportID); | ||
Onyx.set(`${ONYXKEYS.COLLECTION.REPORT_USER_IS_LEAVING_ROOM}${reportID}`, false); | ||
Pusher.unsubscribe(pusherChannelName, Pusher.TYPE.USER_IS_LEAVING_ROOM); | ||
} | ||
|
||
// New action subscriber array for report pages | ||
let newActionSubscribers = []; | ||
|
||
|
@@ -865,6 +915,17 @@ function broadcastUserIsTyping(reportID) { | |
typingStatus[currentUserAccountID] = true; | ||
Pusher.sendEvent(privateReportChannelName, Pusher.TYPE.USER_IS_TYPING, typingStatus); | ||
} | ||
/** | ||
* Broadcasts to the report's private pusher channel whether a user is leaving a report | ||
* | ||
* @param {String} reportID | ||
*/ | ||
function broadcastUserIsLeavingRoom(reportID) { | ||
const privateReportChannelName = getReportChannelName(reportID); | ||
const leavingStatus = {}; | ||
leavingStatus[currentUserAccountID] = true; | ||
Pusher.sendEvent(privateReportChannelName, Pusher.TYPE.USER_IS_LEAVING_ROOM, leavingStatus); | ||
} | ||
|
||
/** | ||
* When a report changes in Onyx, this fetches the report from the API if the report doesn't have a name | ||
|
@@ -1781,6 +1842,12 @@ function getCurrentUserAccountID() { | |
function leaveRoom(reportID) { | ||
const report = lodashGet(allReports, [reportID], {}); | ||
const reportKeys = _.keys(report); | ||
|
||
// Pusher's leavingStatus should be sent earlier. | ||
// Place the broadcast before calling the LeaveRoom API to prevent a race condition | ||
// between Onyx report being null and Pusher's leavingStatus becoming true. | ||
broadcastUserIsLeavingRoom(reportID); | ||
|
||
API.write( | ||
'LeaveRoom', | ||
{ | ||
|
@@ -2071,10 +2138,13 @@ export { | |
updateWriteCapabilityAndNavigate, | ||
updateNotificationPreferenceAndNavigate, | ||
subscribeToReportTypingEvents, | ||
subscribeToReportLeavingEvents, | ||
unsubscribeFromReportChannel, | ||
unsubscribeFromLeavingRoomReportChannel, | ||
saveReportComment, | ||
saveReportCommentNumberOfLines, | ||
broadcastUserIsTyping, | ||
broadcastUserIsLeavingRoom, | ||
togglePinnedState, | ||
editReportComment, | ||
handleUserDeletedLinksInHtml, | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters