Skip to content
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

Migrate notification preference #47577

Merged
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
4c41990
Migrating notification preference into participants
shubham1206agra Aug 16, 2024
8504a1d
fix/tests
ishpaul777 Aug 18, 2024
4d34462
FIXES MORE TESTS
ishpaul777 Aug 18, 2024
49b79ca
Merge pull request #4 from ishpaul777/fix/ioutest
shubham1206agra Aug 19, 2024
24fe828
Apply suggestions from code review
shubham1206agra Aug 19, 2024
cc71ee5
Fix lint
shubham1206agra Aug 19, 2024
3aba64b
Apply suggestions from code review
shubham1206agra Aug 21, 2024
6967e8d
Fix lint
shubham1206agra Aug 21, 2024
cf6ab27
Fix jest test
shubham1206agra Aug 21, 2024
7733be8
Fix jest test
shubham1206agra Aug 21, 2024
ade6683
Merge branch 'Expensify:main' into migrate-notification-preference
shubham1206agra Aug 21, 2024
b8d8979
Apply suggestions from code review
shubham1206agra Aug 21, 2024
8fe8e83
Fix ts
shubham1206agra Aug 22, 2024
aae2e2e
Merge branch 'main' into migrate-notification-preference
shubham1206agra Aug 22, 2024
f8d3e1f
Merge branch 'main' into migrate-notification-preference
shubham1206agra Aug 26, 2024
ce2fc9c
Merge branch 'main' into migrate-notification-preference
shubham1206agra Aug 27, 2024
fb066c1
Fix lint
shubham1206agra Aug 27, 2024
dbbf085
Fixed some optimistic cases
shubham1206agra Aug 29, 2024
ddb38a0
Fixed some more optimistic cases
shubham1206agra Aug 29, 2024
292d348
Merge branch 'Expensify:main' into migrate-notification-preference
shubham1206agra Aug 29, 2024
ad7911f
Merge branch 'main' into migrate-notification-preference
shubham1206agra Sep 1, 2024
152e09a
Fix test
shubham1206agra Sep 1, 2024
26f8695
Fixed jest tests
shubham1206agra Sep 1, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/libs/OptionsListUtils.ts
shubham1206agra marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -805,7 +805,7 @@ function createOption(
result.isWaitingOnBankAccount = report.isWaitingOnBankAccount;
result.policyID = report.policyID;
result.isSelfDM = ReportUtils.isSelfDM(report);
result.notificationPreference = report.notificationPreference;
result.notificationPreference = ReportUtils.getReportNotificationPreference(report);

const visibleParticipantAccountIDs = ReportUtils.getParticipantsAccountIDsForDisplay(report, true);

Expand Down Expand Up @@ -899,7 +899,7 @@ function getPolicyExpenseReportOption(participant: Participant | ReportUtils.Opt
const expenseReport = ReportUtils.isPolicyExpenseChat(participant) ? getReportOrDraftReport(participant.reportID) : null;

const visibleParticipantAccountIDs = Object.entries(expenseReport?.participants ?? {})
.filter(([, reportParticipant]) => reportParticipant && !reportParticipant.hidden)
.filter(([, reportParticipant]) => reportParticipant && reportParticipant.notificationPreference !== CONST.REPORT.NOTIFICATION_PREFERENCE.HIDDEN)
.map(([accountID]) => Number(accountID));

const option = createOption(
Expand Down Expand Up @@ -2578,7 +2578,7 @@ function getEmptyOptions(): Options {
}

function shouldUseBoldText(report: ReportUtils.OptionData): boolean {
return report.isUnread === true && report.notificationPreference !== CONST.REPORT.NOTIFICATION_PREFERENCE.MUTE;
return report.isUnread === true && ReportUtils.getReportNotificationPreference(report) !== CONST.REPORT.NOTIFICATION_PREFERENCE.MUTE;
}

export {
Expand Down
85 changes: 51 additions & 34 deletions src/libs/ReportUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,10 +154,10 @@ type OptimisticExpenseReport = Pick<
| 'statusNum'
| 'total'
| 'nonReimbursableTotal'
| 'notificationPreference'
| 'parentReportID'
| 'lastVisibleActionCreated'
| 'parentReportActionID'
| 'participants'
| 'fieldList'
>;

Expand Down Expand Up @@ -274,7 +274,6 @@ type OptimisticChatReport = Pick<
| 'lastMessageText'
| 'lastReadTime'
| 'lastVisibleActionCreated'
| 'notificationPreference'
| 'oldPolicyName'
| 'ownerAccountID'
| 'pendingFields'
Expand Down Expand Up @@ -355,7 +354,6 @@ type OptimisticTaskReport = Pick<
| 'policyID'
| 'stateNum'
| 'statusNum'
| 'notificationPreference'
| 'parentReportActionID'
| 'lastVisibleActionCreated'
| 'hasParentAccess'
Expand Down Expand Up @@ -395,7 +393,6 @@ type OptimisticIOUReport = Pick<
| 'statusNum'
| 'total'
| 'reportName'
| 'notificationPreference'
| 'parentReportID'
| 'lastVisibleActionCreated'
| 'fieldList'
Expand Down Expand Up @@ -1153,6 +1150,32 @@ function isSystemChat(report: OnyxEntry<Report>): boolean {
return getChatType(report) === CONST.REPORT.CHAT_TYPE.SYSTEM;
}

function getDefaultNotificationPreferenceForReport(report: OnyxEntry<Report>): ValueOf<typeof CONST.REPORT.NOTIFICATION_PREFERENCE> {
if (isAnnounceRoom(report)) {
return CONST.REPORT.NOTIFICATION_PREFERENCE.ALWAYS;
}
if (isPublicRoom(report)) {
return CONST.REPORT.NOTIFICATION_PREFERENCE.DAILY;
}
if (!getChatType(report) || isGroupChat(report)) {
return CONST.REPORT.NOTIFICATION_PREFERENCE.ALWAYS;
}
if (isAdminRoom(report) || isPolicyExpenseChat(report) || isInvoiceRoom(report)) {
return CONST.REPORT.NOTIFICATION_PREFERENCE.ALWAYS;
}
if (isSelfDM(report)) {
return CONST.REPORT.NOTIFICATION_PREFERENCE.MUTE;
}
return CONST.REPORT.NOTIFICATION_PREFERENCE.DAILY;
}
shubham1206agra marked this conversation as resolved.
Show resolved Hide resolved

/**
* Get the notification preference given a report
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* Get the notification preference given a report
* Get the notification preference given a report for the current user

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NAB but I think this is a bit more clear.

*/
function getReportNotificationPreference(report: OnyxEntry<Report>): ValueOf<typeof CONST.REPORT.NOTIFICATION_PREFERENCE> {
return report?.participants?.[currentUserAccountID ?? -1]?.notificationPreference ?? getDefaultNotificationPreferenceForReport(report);
}

const CONCIERGE_ACCOUNT_ID_STRING = CONST.ACCOUNT_ID.CONCIERGE.toString();
/**
* Only returns true if this is our main 1:1 DM report with Concierge.
Expand Down Expand Up @@ -1258,7 +1281,7 @@ function hasExpensifyGuidesEmails(accountIDs: number[]): boolean {

function getMostRecentlyVisitedReport(reports: Array<OnyxEntry<Report>>, reportMetadata: OnyxCollection<ReportMetadata>): OnyxEntry<Report> {
const filteredReports = reports.filter((report) => {
const shouldKeep = !isChatThread(report) || report?.notificationPreference !== CONST.REPORT.NOTIFICATION_PREFERENCE.HIDDEN;
const shouldKeep = !isChatThread(report) || getReportNotificationPreference(report) !== CONST.REPORT.NOTIFICATION_PREFERENCE.HIDDEN;
return shouldKeep && !!report?.reportID && !!(reportMetadata?.[`${ONYXKEYS.COLLECTION.REPORT_METADATA}${report.reportID}`]?.lastVisitTime ?? report?.lastReadTime);
});
return lodashMaxBy(filteredReports, (a) => new Date(reportMetadata?.[`${ONYXKEYS.COLLECTION.REPORT_METADATA}${a?.reportID}`]?.lastVisitTime ?? a?.lastReadTime ?? '').valueOf());
Expand Down Expand Up @@ -1634,13 +1657,6 @@ function isPayer(session: OnyxEntry<Session>, iouReport: OnyxEntry<Report>) {
return isAdmin || (isMoneyRequestReport(iouReport) && isManager);
}

/**
* Get the notification preference given a report
*/
function getReportNotificationPreference(report: OnyxEntry<Report>): string | number {
return report?.notificationPreference ?? '';
}

/**
* Checks if the current user is the action's author
*/
Expand Down Expand Up @@ -2070,7 +2086,7 @@ function getParticipantsAccountIDsForDisplay(report: OnyxEntry<Report>, shouldEx
return false;
}

if (shouldExcludeHidden && participant.hidden) {
if (shouldExcludeHidden && participant.notificationPreference === CONST.REPORT.NOTIFICATION_PREFERENCE.HIDDEN) {
return false;
}

Expand Down Expand Up @@ -2120,7 +2136,7 @@ function buildParticipantsFromAccountIDs(accountIDs: number[]): Participants {
const finalParticipants: Participants = {};
return accountIDs.reduce((participants, accountID) => {
// eslint-disable-next-line no-param-reassign
participants[accountID] = {hidden: false};
participants[accountID] = {notificationPreference: CONST.REPORT.NOTIFICATION_PREFERENCE.ALWAYS};
return participants;
}, finalParticipants);
}
Expand Down Expand Up @@ -4224,8 +4240,8 @@ function buildOptimisticIOUReport(payeeAccountID: number, payerAccountID: number
const policy = getPolicy(policyID);

const participants: Participants = {
[payeeAccountID]: {hidden: true},
[payerAccountID]: {hidden: true},
[payeeAccountID]: {notificationPreference: CONST.REPORT.NOTIFICATION_PREFERENCE.HIDDEN},
[payerAccountID]: {notificationPreference: CONST.REPORT.NOTIFICATION_PREFERENCE.HIDDEN},
};

return {
Expand All @@ -4243,7 +4259,6 @@ function buildOptimisticIOUReport(payeeAccountID: number, payerAccountID: number

// We don't translate reportName because the server response is always in English
reportName: `${payerEmail} owes ${formattedTotal}`,
notificationPreference: CONST.REPORT.NOTIFICATION_PREFERENCE.HIDDEN,
parentReportID: chatReportID,
lastVisibleActionCreated: DateUtils.getDBTime(),
fieldList: policy?.fieldList,
Expand Down Expand Up @@ -4298,7 +4313,11 @@ function buildOptimisticInvoiceReport(chatReportID: string, policyID: string, re
stateNum: CONST.REPORT.STATE_NUM.SUBMITTED,
statusNum: CONST.REPORT.STATUS_NUM.OPEN,
total,
notificationPreference: CONST.REPORT.NOTIFICATION_PREFERENCE.HIDDEN,
participants: {
[currentUserAccountID ?? -1]: {
notificationPreference: CONST.REPORT.NOTIFICATION_PREFERENCE.HIDDEN,
},
},
shubham1206agra marked this conversation as resolved.
Show resolved Hide resolved
parentReportID: chatReportID,
lastVisibleActionCreated: DateUtils.getDBTime(),
};
Expand Down Expand Up @@ -4348,7 +4367,11 @@ function buildOptimisticExpenseReport(
statusNum,
total: storedTotal,
nonReimbursableTotal: reimbursable ? 0 : storedTotal,
notificationPreference: CONST.REPORT.NOTIFICATION_PREFERENCE.HIDDEN,
participants: {
[payeeAccountID]: {
notificationPreference: CONST.REPORT.NOTIFICATION_PREFERENCE.HIDDEN,
},
},
parentReportID: chatReportID,
lastVisibleActionCreated: DateUtils.getDBTime(),
parentReportActionID,
Expand Down Expand Up @@ -4977,11 +5000,10 @@ function buildOptimisticChatReport(
description = '',
avatarUrl = '',
optimisticReportID = '',
shouldShowParticipants = true,
): OptimisticChatReport {
const participants = participantList.reduce((reportParticipants: Participants, accountID: number) => {
const participant: ReportParticipant = {
hidden: !shouldShowParticipants,
notificationPreference,
shubham1206agra marked this conversation as resolved.
Show resolved Hide resolved
role: accountID === currentUserAccountID ? CONST.REPORT.ROLE.ADMIN : CONST.REPORT.ROLE.MEMBER,
};
// eslint-disable-next-line no-param-reassign
Expand All @@ -5002,7 +5024,6 @@ function buildOptimisticChatReport(
lastMessageText: undefined,
lastReadTime: currentTime,
lastVisibleActionCreated: currentTime,
notificationPreference,
oldPolicyName,
ownerAccountID: ownerAccountID || CONST.REPORT.OWNER_ACCOUNT_ID_FAKE,
parentReportActionID,
Expand Down Expand Up @@ -5509,12 +5530,12 @@ function buildOptimisticTaskReport(
): OptimisticTaskReport {
const participants: Participants = {
[ownerAccountID]: {
hidden: false,
notificationPreference,
shubham1206agra marked this conversation as resolved.
Show resolved Hide resolved
},
};

if (assigneeAccountID) {
participants[assigneeAccountID] = {hidden: false};
participants[assigneeAccountID] = {notificationPreference};
}

return {
Expand All @@ -5529,7 +5550,6 @@ function buildOptimisticTaskReport(
policyID,
stateNum: CONST.REPORT.STATE_NUM.OPEN,
statusNum: CONST.REPORT.STATUS_NUM.OPEN,
notificationPreference,
lastVisibleActionCreated: DateUtils.getDBTime(),
hasParentAccess: true,
};
Expand Down Expand Up @@ -5607,10 +5627,6 @@ function buildTransactionThread(
CONST.REPORT.NOTIFICATION_PREFERENCE.HIDDEN,
reportAction?.reportActionID,
moneyRequestReport?.reportID,
'',
'',
'',
false,
);
}

Expand Down Expand Up @@ -5975,7 +5991,7 @@ function shouldReportBeInOptionList({

// All unread chats (even archived ones) in GSD mode will be shown. This is because GSD mode is specifically for focusing the user on the most relevant chats, primarily, the unread ones
if (isInFocusMode) {
return isUnread(report) && report.notificationPreference !== CONST.REPORT.NOTIFICATION_PREFERENCE.MUTE;
return isUnread(report) && getReportNotificationPreference(report) !== CONST.REPORT.NOTIFICATION_PREFERENCE.MUTE;
}

// Archived reports should always be shown when in default (most recent) mode. This is because you should still be able to access and search for the chats to find them.
Expand Down Expand Up @@ -7362,15 +7378,15 @@ function isAdminOwnerApproverOrReportOwner(report: OnyxEntry<Report>, policy: On
/**
* Whether the user can join a report
*/
function canJoinChat(report: OnyxInputOrEntry<Report>, parentReportAction: OnyxInputOrEntry<ReportAction>, policy: OnyxInputOrEntry<Policy>): boolean {
function canJoinChat(report: OnyxEntry<Report>, parentReportAction: OnyxInputOrEntry<ReportAction>, policy: OnyxInputOrEntry<Policy>): boolean {
// We disabled thread functions for whisper action
// So we should not show join option for existing thread on whisper message that has already been left, or manually leave it
if (ReportActionsUtils.isWhisperAction(parentReportAction)) {
return false;
}

// If the notification preference of the chat is not hidden that means we have already joined the chat
if (report?.notificationPreference !== CONST.REPORT.NOTIFICATION_PREFERENCE.HIDDEN) {
if (getReportNotificationPreference(report) !== CONST.REPORT.NOTIFICATION_PREFERENCE.HIDDEN) {
return false;
}

Expand Down Expand Up @@ -7404,7 +7420,7 @@ function canLeaveChat(report: OnyxEntry<Report>, policy: OnyxEntry<Policy>): boo
return false;
}

if (report?.notificationPreference === CONST.REPORT.NOTIFICATION_PREFERENCE.HIDDEN) {
if (getReportNotificationPreference(report) === CONST.REPORT.NOTIFICATION_PREFERENCE.HIDDEN) {
return false;
}

Expand All @@ -7422,7 +7438,7 @@ function canLeaveChat(report: OnyxEntry<Report>, policy: OnyxEntry<Policy>): boo
return canLeaveInvoiceRoom(report);
}

return (isChatThread(report) && !!report?.notificationPreference?.length) || isUserCreatedPolicyRoom(report) || isNonAdminOrOwnerOfPolicyExpenseChat(report, policy);
return (isChatThread(report) && !!getReportNotificationPreference(report)) || isUserCreatedPolicyRoom(report) || isNonAdminOrOwnerOfPolicyExpenseChat(report, policy);
}

function getReportActionActorAccountID(reportAction: OnyxInputOrEntry<ReportAction>, iouReport: OnyxInputOrEntry<Report> | undefined): number | undefined {
Expand Down Expand Up @@ -7933,6 +7949,7 @@ export {
isInvoiceRoomWithID,
isInvoiceReport,
isOpenInvoiceReport,
getDefaultNotificationPreferenceForReport,
canWriteInReport,
navigateToDetailsPage,
navigateToPrivateNotes,
Expand Down
4 changes: 2 additions & 2 deletions src/libs/SidebarUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ function getOrderedReportIDs(
const reportActions = allReportActions?.[`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${report.reportID}`] ?? {};
const parentReportAction = ReportActionsUtils.getReportAction(report?.parentReportID ?? '-1', report?.parentReportActionID ?? '-1');
const doesReportHaveViolations = OptionsListUtils.shouldShowViolations(report, transactionViolations);
const isHidden = report.notificationPreference === CONST.REPORT.NOTIFICATION_PREFERENCE.HIDDEN;
const isHidden = ReportUtils.getReportNotificationPreference(report) === CONST.REPORT.NOTIFICATION_PREFERENCE.HIDDEN;
const isFocused = report.reportID === currentReportId;
const allReportErrors = OptionsListUtils.getAllReportErrors(report, reportActions) ?? {};
const transactionReportActions = ReportActionsUtils.getAllReportActions(report.reportID);
Expand Down Expand Up @@ -340,7 +340,7 @@ function getOptionData({
result.hasOutstandingChildRequest = report.hasOutstandingChildRequest;
result.parentReportID = report.parentReportID ?? '-1';
result.isWaitingOnBankAccount = report.isWaitingOnBankAccount;
result.notificationPreference = report.notificationPreference;
result.notificationPreference = ReportUtils.getReportNotificationPreference(report);
result.isAllowedToComment = ReportUtils.canUserPerformWriteAction(report);
result.chatType = report.chatType;
result.isDeletedParentAction = report.isDeletedParentAction;
Expand Down
4 changes: 2 additions & 2 deletions src/libs/UnreadIndicatorUpdater/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ function getUnreadReportsForUnreadIndicator(reports: OnyxCollection<Report>, cur
* Furthermore, muted reports may or may not appear in the LHN depending on priority mode,
* but they should not be considered in the unread indicator count.
*/
report?.notificationPreference !== CONST.REPORT.NOTIFICATION_PREFERENCE.HIDDEN &&
report?.notificationPreference !== CONST.REPORT.NOTIFICATION_PREFERENCE.MUTE,
ReportUtils.getReportNotificationPreference(report) !== CONST.REPORT.NOTIFICATION_PREFERENCE.HIDDEN &&
ReportUtils.getReportNotificationPreference(report) !== CONST.REPORT.NOTIFICATION_PREFERENCE.MUTE,
shubham1206agra marked this conversation as resolved.
Show resolved Hide resolved
);
}

Expand Down
8 changes: 6 additions & 2 deletions src/libs/actions/IOU.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3942,7 +3942,7 @@ function createSplitsAndOnyxData(
splitChatReport.lastActorAccountID = currentUserAccountID;
splitChatReport.lastVisibleActionCreated = splitIOUReportAction.created;

let splitChatReportNotificationPreference = splitChatReport.notificationPreference;
let splitChatReportNotificationPreference = ReportUtils.getReportNotificationPreference(splitChatReport);
if (splitChatReportNotificationPreference === CONST.REPORT.NOTIFICATION_PREFERENCE.HIDDEN) {
splitChatReportNotificationPreference = CONST.REPORT.NOTIFICATION_PREFERENCE.ALWAYS;
}
Expand All @@ -3962,7 +3962,11 @@ function createSplitsAndOnyxData(
key: `${ONYXKEYS.COLLECTION.REPORT}${splitChatReport.reportID}`,
value: {
...splitChatReport,
notificationPreference: splitChatReportNotificationPreference,
participants: {
[currentUserAccountID]: {
notificationPreference: splitChatReportNotificationPreference,
},
},
shubham1206agra marked this conversation as resolved.
Show resolved Hide resolved
},
},
{
Expand Down
Loading
Loading