Skip to content

Commit

Permalink
Migrating notification preference into participants
Browse files Browse the repository at this point in the history
  • Loading branch information
shubham1206agra committed Aug 16, 2024
1 parent a45b5a2 commit 4c41990
Show file tree
Hide file tree
Showing 22 changed files with 213 additions and 130 deletions.
6 changes: 3 additions & 3 deletions src/libs/OptionsListUtils.ts
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>) {
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;
}

/**
* Get the notification preference given a report
*/
function getReportNotificationPreference(report: OnyxEntry<Report>) {
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,
},
},
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,
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,
},
};

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,
);
}

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,
},
},
},
},
{
Expand Down
Loading

0 comments on commit 4c41990

Please sign in to comment.