-
Notifications
You must be signed in to change notification settings - Fork 2.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Load chats when handling browser notifications. Fix initial sign in bug. #339
Changes from all commits
cf027e0
d2c502c
ab38126
7abe3cf
fe7538c
93c599e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,74 +11,6 @@ import ExpensiMark from '../ExpensiMark'; | |
import Notification from '../Notification'; | ||
import * as PersonalDetails from './PersonalDetails'; | ||
|
||
/** | ||
* Updates a report in the store with a new report action | ||
* | ||
* @param {string} reportID | ||
* @param {object} reportAction | ||
*/ | ||
function updateReportWithNewAction(reportID, reportAction) { | ||
let currentUserEmail; | ||
Ion.get(`${IONKEYS.REPORT}_${reportID}`, 'reportID') | ||
.then((ionReportID) => { | ||
// This is necessary for local development because there will be pusher events from other engineers with | ||
// different reportIDs | ||
if (!CONFIG.IS_IN_PRODUCTION && !ionReportID) { | ||
throw new Error('report does not exist in the store, so ignoring new comments'); | ||
} | ||
|
||
// Get the report history and return that to the next chain | ||
return Ion.get(`${IONKEYS.REPORT_HISTORY}_${reportID}`); | ||
}) | ||
|
||
// Look to see if the report action from pusher already exists or not (it would exist if it's a comment just | ||
// written by the user). If the action doesn't exist, then update the unread flag on the report so the user | ||
// knows there is a new comment | ||
.then((reportHistory) => { | ||
if (reportHistory && !reportHistory[reportAction.sequenceNumber]) { | ||
Ion.merge(`${IONKEYS.REPORT}_${reportID}`, {hasUnread: true}); | ||
} | ||
return reportHistory || {}; | ||
}) | ||
|
||
// Put the report action from pusher into the history, it's OK to overwrite it if it already exists | ||
.then(reportHistory => ({ | ||
...reportHistory, | ||
[reportAction.sequenceNumber]: reportAction, | ||
})) | ||
|
||
// Put the report history back into Ion | ||
.then(reportHistory => Ion.set(`${IONKEYS.REPORT_HISTORY}_${reportID}`, reportHistory)) | ||
|
||
// Check to see if we need to show a notification for this report | ||
.then(() => Ion.get(IONKEYS.SESSION, 'email')) | ||
.then((email) => { | ||
currentUserEmail = email; | ||
return Ion.get(IONKEYS.CURRENT_URL); | ||
}) | ||
.then((currentUrl) => { | ||
// If this comment is from the current user we don't want to parrot whatever they wrote back to them. | ||
if (reportAction.actorEmail === currentUserEmail) { | ||
return; | ||
} | ||
|
||
const currentReportID = Number(lodashGet(currentUrl.split('/'), [1], 0)); | ||
|
||
// If we are currently viewing this report do not show a notification. | ||
if (reportID === currentReportID) { | ||
return; | ||
} | ||
|
||
Notification.showCommentNotification({ | ||
reportAction, | ||
onClick: () => { | ||
// Navigate to this report onClick | ||
Ion.set(IONKEYS.APP_REDIRECT_TO, `/${reportID}`); | ||
} | ||
}); | ||
}); | ||
} | ||
|
||
/** | ||
* Checks the report to see if there are any unread history items | ||
* | ||
|
@@ -126,21 +58,6 @@ function getSimplifiedReportObject(report, accountID) { | |
}; | ||
} | ||
|
||
/** | ||
* Initialize our pusher subscriptions to listen for new report comments | ||
* | ||
* @returns {Promise} | ||
*/ | ||
function subscribeToReportCommentEvents() { | ||
return Ion.get(IONKEYS.SESSION, 'accountID') | ||
.then((accountID) => { | ||
const pusherChannelName = `private-user-accountID-${accountID}`; | ||
Pusher.subscribe(pusherChannelName, 'reportComment', (pushJSON) => { | ||
updateReportWithNewAction(pushJSON.reportID, pushJSON.reportAction); | ||
}); | ||
}); | ||
} | ||
|
||
/** | ||
* Returns a generated report title based on the participants | ||
* | ||
|
@@ -159,27 +76,26 @@ function getChatReportName(sharedReportList, personalDetails, currentUserEmail) | |
} | ||
|
||
/** | ||
* Get all chat reports and provide the proper report name | ||
* by fetching sharedReportList and personalDetails | ||
* Fetches chat reports when provided a list of | ||
* chat report IDs | ||
* | ||
* @returns {Promise} | ||
* @param {Array} chatList | ||
* @return {Promise} | ||
*/ | ||
function fetchChatReports() { | ||
function fetchChatReportsByIDs(chatList) { | ||
let currentUserEmail; | ||
let currentUserAccountID; | ||
let fetchedReports; | ||
|
||
return Ion.get(IONKEYS.SESSION) | ||
.then((session) => { | ||
currentUserEmail = session.email; | ||
currentUserAccountID = session.accountID; | ||
return queueRequest('Get', {returnValueList: 'chatList'}); | ||
return queueRequest('Get', { | ||
returnValueList: 'reportStuff', | ||
reportIDList: chatList.join(','), | ||
shouldLoadOptionalKeys: true, | ||
}); | ||
}) | ||
.then(({chatList}) => queueRequest('Get', { | ||
returnValueList: 'reportStuff', | ||
reportIDList: chatList, | ||
shouldLoadOptionalKeys: true, | ||
})) | ||
.then(({reports}) => { | ||
fetchedReports = reports; | ||
|
||
|
@@ -220,6 +136,111 @@ function fetchChatReports() { | |
}); | ||
} | ||
|
||
/** | ||
* Updates a report in the store with a new report action | ||
* | ||
* @param {string} reportID | ||
* @param {object} reportAction | ||
*/ | ||
function updateReportWithNewAction(reportID, reportAction) { | ||
let currentUserEmail; | ||
Ion.get(`${IONKEYS.REPORT}_${reportID}`, 'reportID') | ||
.then((ionReportID) => { | ||
// This is necessary for local development because there will be pusher events from other engineers with | ||
// different reportIDs. This means that while in development it's not possible to make new chats appear | ||
// by creating chats then leaving comments in other windows. | ||
if (!CONFIG.IS_IN_PRODUCTION && !ionReportID) { | ||
throw new Error('report does not exist in the store, so ignoring new comments'); | ||
} | ||
|
||
// When handling a realtime update for a chat that does not yet exist in our store we | ||
// need to fetch it so that we can properly navigate to it. This enables us populate | ||
// newly created chats in the LHN without requiring a full refresh of the app. | ||
if (!ionReportID) { | ||
return fetchChatReportsByIDs([reportID]) | ||
.then(() => Ion.get(`${IONKEYS.REPORT_HISTORY}_${reportID}`)); | ||
} | ||
|
||
// Get the report history and return that to the next chain | ||
return Ion.get(`${IONKEYS.REPORT_HISTORY}_${reportID}`); | ||
}) | ||
|
||
// Look to see if the report action from pusher already exists or not (it would exist if it's a comment just | ||
// written by the user). If the action doesn't exist, then update the unread flag on the report so the user | ||
// knows there is a new comment | ||
.then((reportHistory) => { | ||
if (reportHistory && !reportHistory[reportAction.sequenceNumber]) { | ||
Ion.merge(`${IONKEYS.REPORT}_${reportID}`, {hasUnread: true}); | ||
} | ||
return reportHistory || {}; | ||
}) | ||
|
||
// Put the report action from pusher into the history, it's OK to overwrite it if it already exists | ||
.then(reportHistory => ({ | ||
...reportHistory, | ||
[reportAction.sequenceNumber]: reportAction, | ||
})) | ||
|
||
// Put the report history back into Ion | ||
.then(reportHistory => Ion.set(`${IONKEYS.REPORT_HISTORY}_${reportID}`, reportHistory)) | ||
|
||
// Check to see if we need to show a notification for this report | ||
.then(() => Ion.get(IONKEYS.SESSION, 'email')) | ||
.then((email) => { | ||
currentUserEmail = email; | ||
return Ion.get(IONKEYS.CURRENT_URL); | ||
}) | ||
.then((currentUrl) => { | ||
// If this comment is from the current user we don't want to parrot whatever they wrote back to them. | ||
if (reportAction.actorEmail === currentUserEmail) { | ||
return; | ||
} | ||
|
||
const currentReportID = Number(lodashGet(currentUrl.split('/'), [1], 0)); | ||
|
||
// If we are currently viewing this report do not show a notification. | ||
if (reportID === currentReportID) { | ||
return; | ||
} | ||
|
||
Notification.showCommentNotification({ | ||
reportAction, | ||
onClick: () => { | ||
// Navigate to this report onClick | ||
Ion.set(IONKEYS.APP_REDIRECT_TO, `/${reportID}`); | ||
} | ||
}); | ||
}); | ||
} | ||
|
||
/** | ||
* Initialize our pusher subscriptions to listen for new report comments | ||
* | ||
* @returns {Promise} | ||
*/ | ||
function subscribeToReportCommentEvents() { | ||
return Ion.get(IONKEYS.SESSION, 'accountID') | ||
.then((accountID) => { | ||
const pusherChannelName = `private-user-accountID-${accountID}`; | ||
Pusher.subscribe(pusherChannelName, 'reportComment', (pushJSON) => { | ||
updateReportWithNewAction(pushJSON.reportID, pushJSON.reportAction); | ||
}); | ||
}); | ||
} | ||
|
||
/** | ||
* Get all chat reports and provide the proper report name | ||
* by fetching sharedReportList and personalDetails | ||
* | ||
* @returns {Promise} | ||
*/ | ||
function fetchChatReports() { | ||
return queueRequest('Get', {returnValueList: 'chatList'}) | ||
|
||
// The string cast below is necessary as Get rvl='chatList' may return an int | ||
.then(({chatList}) => fetchChatReportsByIDs(String(chatList).split(','))); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why is it necessary to cast this to a string? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's because There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh, that's good to know! That would be another good thing to add as a comment in the code. It could bite someone. |
||
} | ||
|
||
/** | ||
* Get all of our reports | ||
* | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you please add a comment here to explain how it gets into this situation?