Skip to content

Commit

Permalink
Clean up logic for creating Group Chats
Browse files Browse the repository at this point in the history
  • Loading branch information
marcaaron committed Mar 30, 2024
1 parent 10beb0a commit 04cfc03
Showing 1 changed file with 25 additions and 10 deletions.
35 changes: 25 additions & 10 deletions src/libs/actions/IOU.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2249,15 +2249,30 @@ function trackExpense(
}

function getOrCreateOptimisticSplitChatReport(existingSplitChatReportID: string, participants: Participant[], participantAccountIDs: number[], currentUserAccountID: number) {
// The existing chat report could be passed as reportID or exist on the sole "participant" (in this case a report option)
const existingChatReportID = existingSplitChatReportID || participants[0].reportID;

// Check if the report is available locally if we do have one
let existingSplitChatReport = allReports?.[`${ONYXKEYS.COLLECTION.REPORT}${existingChatReportID}`];
if (!existingSplitChatReport) {
existingSplitChatReport = participants.length < 2 ? ReportUtils.getChatByParticipants(participantAccountIDs) : null;

// If we do not have one locally then we will search for a chat with the same participants (only for 1:1 chats).
const shouldGetOrCreateOneOneDM = participants.length < 2;
if (!existingSplitChatReport && shouldGetOrCreateOneOneDM) {
existingSplitChatReport = ReportUtils.getChatByParticipants(participantAccountIDs);
}

// We found an existing chat report we are done...
if (existingSplitChatReport) {
// Yes, these are the same, but give the caller a way to identify if we created a new report or not
return {existingSplitChatReport, splitChatReport: existingSplitChatReport};
}
let newChat: ReportUtils.OptimisticChatReport | EmptyObject = {};

// No existing chat by this point we need to create it
const allParticipantsAccountIDs = [...participantAccountIDs, currentUserAccountID];
if (!existingSplitChatReport && participants.length > 1) {
newChat = ReportUtils.buildOptimisticChatReport(

// Create a Group Chat if we have multiple participants
if (participants.length > 1) {
const splitChatReport = ReportUtils.buildOptimisticChatReport(
allParticipantsAccountIDs,
'',
CONST.REPORT.CHAT_TYPE.GROUP,
Expand All @@ -2269,12 +2284,12 @@ function getOrCreateOptimisticSplitChatReport(existingSplitChatReportID: string,
undefined,
CONST.REPORT.NOTIFICATION_PREFERENCE.HIDDEN,
);
return {existingSplitChatReport: null, splitChatReport};
}
if (isEmptyObject(newChat)) {
newChat = ReportUtils.buildOptimisticChatReport(allParticipantsAccountIDs);
}
const splitChatReport = existingSplitChatReport ?? newChat;
return {splitChatReport, existingSplitChatReport};

// Otherwise, create a new 1:1 chat report
const splitChatReport = ReportUtils.buildOptimisticChatReport(allParticipantsAccountIDs);
return {existingSplitChatReport: null, splitChatReport};
}

/**
Expand Down

0 comments on commit 04cfc03

Please sign in to comment.