Skip to content

Commit

Permalink
Revert "Merge pull request Expensify#24407 from wildan-m/wildan/fix/2…
Browse files Browse the repository at this point in the history
…1518/client-pusher-method"

This reverts commit fa6a767, reversing
changes made to b50dfac.
  • Loading branch information
hoangzinh committed Oct 2, 2023
1 parent 1b94d11 commit 74b25a3
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 127 deletions.
2 changes: 0 additions & 2 deletions src/ONYXKEYS.ts
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,6 @@ const ONYXKEYS = {
REPORT_DRAFT_COMMENT_NUMBER_OF_LINES: 'reportDraftCommentNumberOfLines_',
REPORT_IS_COMPOSER_FULL_SIZE: 'reportIsComposerFullSize_',
REPORT_USER_IS_TYPING: 'reportUserIsTyping_',
REPORT_USER_IS_LEAVING_ROOM: 'reportUserIsLeavingRoom_',
SECURITY_GROUP: 'securityGroup_',
TRANSACTION: 'transactions_',

Expand Down Expand Up @@ -384,7 +383,6 @@ type OnyxValues = {
[ONYXKEYS.COLLECTION.REPORT_DRAFT_COMMENT_NUMBER_OF_LINES]: number;
[ONYXKEYS.COLLECTION.REPORT_IS_COMPOSER_FULL_SIZE]: boolean;
[ONYXKEYS.COLLECTION.REPORT_USER_IS_TYPING]: boolean;
[ONYXKEYS.COLLECTION.REPORT_USER_IS_LEAVING_ROOM]: boolean;
[ONYXKEYS.COLLECTION.SECURITY_GROUP]: OnyxTypes.SecurityGroup;
[ONYXKEYS.COLLECTION.TRANSACTION]: OnyxTypes.Transaction;
[ONYXKEYS.COLLECTION.POLICY_RECENTLY_USED_TAGS]: OnyxTypes.RecentlyUsedTags;
Expand Down
1 change: 0 additions & 1 deletion src/libs/Pusher/EventType.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
export default {
REPORT_COMMENT: 'reportComment',
ONYX_API_UPDATE: 'onyxApiUpdate',
USER_IS_LEAVING_ROOM: 'client-userIsLeavingRoom',
USER_IS_TYPING: 'client-userIsTyping',
MULTIPLE_EVENTS: 'multipleEvents',
MULTIPLE_EVENT_TYPE: {
Expand Down
88 changes: 9 additions & 79 deletions src/libs/actions/Report.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,24 +102,24 @@ function getReportChannelName(reportID) {
}

/**
* There are 2 possibilities that we can receive via pusher for a user's typing/leaving status:
* There are 2 possibilities that we can receive via pusher for a user's typing status:
* 1. The "new" way from New Expensify is passed as {[login]: Boolean} (e.g. {yuwen@expensify.com: true}), where the value
* is whether the user with that login is typing/leaving on the report or not.
* is whether the user with that login is typing on the report or not.
* 2. The "old" way from e.com which is passed as {userLogin: login} (e.g. {userLogin: bstites@expensify.com})
*
* This method makes sure that no matter which we get, we return the "new" format
*
* @param {Object} status
* @param {Object} typingStatus
* @returns {Object}
*/
function getNormalizedStatus(status) {
let normalizedStatus = status;
function getNormalizedTypingStatus(typingStatus) {
let normalizedTypingStatus = typingStatus;

if (_.first(_.keys(status)) === 'userLogin') {
normalizedStatus = {[status.userLogin]: true};
if (_.first(_.keys(typingStatus)) === 'userLogin') {
normalizedTypingStatus = {[typingStatus.userLogin]: true};
}

return normalizedStatus;
return normalizedTypingStatus;
}

/**
Expand All @@ -140,7 +140,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 = getNormalizedStatus(typingStatus);
const normalizedTypingStatus = getNormalizedTypingStatus(typingStatus);
const accountIDOrLogin = _.first(_.keys(normalizedTypingStatus));

if (!accountIDOrLogin) {
Expand Down Expand Up @@ -169,41 +169,6 @@ 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.
*
Expand All @@ -219,21 +184,6 @@ 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 = [];

Expand Down Expand Up @@ -941,17 +891,6 @@ 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
Expand Down Expand Up @@ -1879,12 +1818,6 @@ 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',
{
Expand Down Expand Up @@ -2175,13 +2108,10 @@ export {
updateWriteCapabilityAndNavigate,
updateNotificationPreference,
subscribeToReportTypingEvents,
subscribeToReportLeavingEvents,
unsubscribeFromReportChannel,
unsubscribeFromLeavingRoomReportChannel,
saveReportComment,
saveReportCommentNumberOfLines,
broadcastUserIsTyping,
broadcastUserIsLeavingRoom,
togglePinnedState,
editReportComment,
handleUserDeletedLinksInHtml,
Expand Down
87 changes: 42 additions & 45 deletions src/pages/home/ReportScreen.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,12 +88,16 @@ const propTypes = {
/** All of the personal details for everyone */
personalDetails: PropTypes.objectOf(personalDetailsPropType),

<<<<<<< HEAD
/** Onyx function that marks the component ready for hydration */
markReadyForHydration: PropTypes.func,

/** Whether user is leaving the current report */
userLeavingStatus: PropTypes.bool,

=======
...windowDimensionsPropTypes,
>>>>>>> parent of fa6a767987 (Merge pull request #24407 from wildan-m/wildan/fix/21518/client-pusher-method)
...viewportOffsetTopPropTypes,
...withCurrentReportIDPropTypes,
};
Expand All @@ -112,7 +116,6 @@ const defaultProps = {
betas: [],
policies: {},
accountManagerReportID: null,
userLeavingStatus: false,
personalDetails: {},
markReadyForHydration: null,
...withCurrentReportIDDefaultProps,
Expand Down Expand Up @@ -153,7 +156,6 @@ function ReportScreen({
viewportOffsetTop,
isComposerFullSize,
errors,
userLeavingStatus,
currentReportID,
}) {
const {translate} = useLocalize();
Expand All @@ -163,7 +165,12 @@ function ReportScreen({
const flatListRef = useRef();
const reactionListRef = useRef();
const prevReport = usePrevious(report);
<<<<<<< HEAD
const prevUserLeavingStatus = usePrevious(userLeavingStatus);
=======

const [skeletonViewContainerHeight, setSkeletonViewContainerHeight] = useState(0);
>>>>>>> parent of fa6a767987 (Merge pull request #24407 from wildan-m/wildan/fix/21518/client-pusher-method)
const [isBannerVisible, setIsBannerVisible] = useState(true);

const reportID = getReportID(route);
Expand All @@ -186,7 +193,6 @@ function ReportScreen({
const policy = policies[`${ONYXKEYS.COLLECTION.POLICY}${report.policyID}`] || {};

const isTopMostReportId = currentReportID === getReportID(route);
const didSubscribeToReportLeavingEvents = useRef(false);

const isDefaultReport = checkDefaultReport(report);

Expand Down Expand Up @@ -246,7 +252,7 @@ function ReportScreen({
}

// It possible that we may not have the report object yet in Onyx yet e.g. we navigated to a URL for an accessible report that
// is not stored locally yet. If report.reportID exists, then the report has been stored locally and nothing more needs to be done.
// is not stored locally yet. If props.report.reportID exists, then the report has been stored locally and nothing more needs to be done.
// If it doesn't exist, then we fetch the report from the API.
if (report.reportID && report.reportID === getReportID(route)) {
return;
Expand Down Expand Up @@ -294,14 +300,6 @@ function ReportScreen({
useEffect(() => {
fetchReportIfNeeded();
ComposerActions.setShouldShowComposeInput(true);
return () => {
if (!didSubscribeToReportLeavingEvents) {
return;
}

Report.unsubscribeFromLeavingRoomReportChannel(report.reportID);
};

// I'm disabling the warning, as it expects to use exhaustive deps, even though we want this useEffect to run only on the first render.
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);
Expand All @@ -312,46 +310,19 @@ function ReportScreen({
firstRenderRef.current = false;
return;
}

const onyxReportID = report.reportID;
const prevOnyxReportID = prevReport.reportID;
const routeReportID = getReportID(route);

// Navigate to the Concierge chat if the room was removed from another device (e.g. user leaving a room)
if (
// non-optimistic case
(!prevUserLeavingStatus && userLeavingStatus) ||
// optimistic case
(prevOnyxReportID && prevOnyxReportID === routeReportID && !onyxReportID && prevReport.statusNum === CONST.REPORT.STATUS.OPEN && report.statusNum === CONST.REPORT.STATUS.CLOSED)
) {
Navigation.goBack();
Report.navigateToConciergeChat();
return;
}

// If you already have a report open and are deeplinking to a new report on native,
// the ReportScreen never actually unmounts and the reportID in the route also doesn't change.
// Therefore, we need to compare if the existing reportID is the same as the one in the route
// before deciding that we shouldn't call OpenReport.
const onyxReportID = report.reportID;
const routeReportID = getReportID(route);
if (onyxReportID === prevReport.reportID && (!onyxReportID || onyxReportID === routeReportID)) {
return;
}

fetchReportIfNeeded();
ComposerActions.setShouldShowComposeInput(true);
}, [route, report, errors, fetchReportIfNeeded, prevReport.reportID, prevUserLeavingStatus, userLeavingStatus, prevReport.statusNum]);

useEffect(() => {
// Ensures subscription event succeeds when the report/workspace room is created optimistically.
// Check if the optimistic `OpenReport` or `AddWorkspaceRoom` has succeeded by confirming
// any `pendingFields.createChat` or `pendingFields.addWorkspaceRoom` fields are set to null.
// Existing reports created will have empty fields for `pendingFields`.
const didCreateReportSuccessfully = !report.pendingFields || (!report.pendingFields.addWorkspaceRoom && !report.pendingFields.createChat);
if (!didSubscribeToReportLeavingEvents.current && didCreateReportSuccessfully) {
Report.subscribeToReportLeavingEvents(reportID);
didSubscribeToReportLeavingEvents.current = true;
}
}, [report, didSubscribeToReportLeavingEvents, reportID]);
}, [route, report, errors, fetchReportIfNeeded, prevReport.reportID]);

const onListLayout = useCallback(() => {
if (!markReadyForHydration) {
Expand All @@ -371,10 +342,9 @@ function ReportScreen({
!report.reportID &&
!isOptimisticDelete &&
!report.isLoadingReportActions &&
!isLoading &&
!userLeavingStatus) ||
!isLoading) ||
shouldHideReport,
[report, isLoading, shouldHideReport, isDefaultReport, isOptimisticDelete, userLeavingStatus],
[report, isLoading, shouldHideReport, isDefaultReport, isOptimisticDelete],
);

return (
Expand Down Expand Up @@ -513,6 +483,33 @@ export default compose(
initialValue: false,
},
},
<<<<<<< HEAD
true,
),
=======
reportActions: {
key: ({route}) => `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${getReportID(route)}`,
canEvict: false,
selector: ReportActionsUtils.getSortedReportActionsForDisplay,
},
report: {
key: ({route}) => `${ONYXKEYS.COLLECTION.REPORT}${getReportID(route)}`,
},
isComposerFullSize: {
key: ({route}) => `${ONYXKEYS.COLLECTION.REPORT_IS_COMPOSER_FULL_SIZE}${getReportID(route)}`,
},
betas: {
key: ONYXKEYS.BETAS,
},
policies: {
key: ONYXKEYS.COLLECTION.POLICY,
},
accountManagerReportID: {
key: ONYXKEYS.ACCOUNT_MANAGER_REPORT_ID,
},
personalDetails: {
key: ONYXKEYS.PERSONAL_DETAILS_LIST,
},
}),
>>>>>>> parent of fa6a767987 (Merge pull request #24407 from wildan-m/wildan/fix/21518/client-pusher-method)
)(ReportScreen);

0 comments on commit 74b25a3

Please sign in to comment.