Skip to content

Commit

Permalink
Update threadIsPendingSidebar to handle thick sidebars
Browse files Browse the repository at this point in the history
Summary: This differential updates `threadIsPendingSidebar` to handle thick sidebars as well

Test Plan: Test was added to `thread-utils.test.js`

Reviewers: inka, tomek

Reviewed By: inka, tomek

Subscribers: ashoat

Differential Revision: https://phab.comm.dev/D13274
  • Loading branch information
marcinwasowicz committed Sep 11, 2024
1 parent 0e2412f commit 085726b
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 8 deletions.
22 changes: 15 additions & 7 deletions lib/shared/thread-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,11 @@ import { useSelector } from '../utils/redux-utils.js';
import { userSurfacedPermissionsFromRolePermissions } from '../utils/role-utils.js';
import { usingOlmViaTunnelbrokerForDMs } from '../utils/services-utils.js';
import { firstLine } from '../utils/string-utils.js';
import { pendingThreadIDRegex } from '../utils/validation-utils.js';
import {
pendingThreadIDRegex,
pendingThickSidebarURLPrefix,
pendingSidebarURLPrefix,
} from '../utils/validation-utils.js';

function threadHasPermission(
threadInfo: ?(ThreadInfo | LegacyRawThreadInfo | RawThreadInfo),
Expand Down Expand Up @@ -373,7 +377,10 @@ function threadIsPending(threadID: ?string): boolean {
}
function threadIsPendingSidebar(threadID: ?string): boolean {
return !!threadID?.startsWith('pending/sidebar/');
return (
!!threadID?.startsWith(`pending/${pendingSidebarURLPrefix}/`) ||
!!threadID?.startsWith(`pending/${pendingThickSidebarURLPrefix}`)
);
}
function getSingleOtherUser(
Expand All @@ -397,9 +404,9 @@ function getPendingThreadID(
): string {
let pendingThreadKey;
if (sourceMessageID && threadTypeIsThick(threadType)) {
pendingThreadKey = `dm_sidebar/${sourceMessageID}`;
pendingThreadKey = `${pendingThickSidebarURLPrefix}/${sourceMessageID}`;
} else if (sourceMessageID) {
pendingThreadKey = `sidebar/${sourceMessageID}`;
pendingThreadKey = `${pendingSidebarURLPrefix}/${sourceMessageID}`;
} else {
pendingThreadKey = [...memberIDs].sort().join('+');
}
Expand All @@ -425,16 +432,17 @@ function parsePendingThreadID(
const [threadTypeString, threadKey] = pendingThreadIDMatches[1].split('/');
let threadType;
if (threadTypeString === 'dm_sidebar') {
if (threadTypeString === pendingThickSidebarURLPrefix) {
threadType = threadTypes.THICK_SIDEBAR;
} else if (threadTypeString === 'sidebar') {
} else if (threadTypeString === pendingSidebarURLPrefix) {
threadType = threadTypes.SIDEBAR;
} else {
threadType = assertThreadType(Number(threadTypeString.replace('type', '')));
}

const threadTypeStringIsSidebar =
threadTypeString === 'sidebar' || threadTypeString === 'dm_sidebar';
threadTypeString === pendingSidebarURLPrefix ||
threadTypeString === pendingThickSidebarURLPrefix;
const memberIDs = threadTypeStringIsSidebar ? [] : threadKey.split('+');
const sourceMessageID = threadTypeStringIsSidebar ? threadKey : null;
return {
Expand Down
26 changes: 26 additions & 0 deletions lib/shared/thread-utils.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
parsePendingThreadID,
threadInfoFromRawThreadInfo,
getPendingThreadID,
threadIsPendingSidebar,
} from './thread-utils.js';
import { threadInfoValidator } from '../permissions/minimally-encoded-thread-permissions-validators.js';
import type { RawThreadInfo } from '../types/minimally-encoded-thread-permissions-types.js';
Expand Down Expand Up @@ -213,3 +214,28 @@ describe('getPendingThreadID', () => {
).toStrictEqual('pending/dm_sidebar/12345');
});
});

describe('threadIsPendingSidebar', () => {
it('should correctly check if sidebar is pending', () => {
const thinPendingSidebarID = getPendingThreadID(
threadTypes.SIDEBAR,
[],
'12345',
);
const thickPendingSidebarID = getPendingThreadID(
threadTypes.THICK_SIDEBAR,
[],
'12345',
);
const nonSidebarPendinThreadID = getPendingThreadID(
threadTypes.PERSONAL,
[],
);

expect(threadIsPendingSidebar(thinPendingSidebarID)).toStrictEqual(true);
expect(threadIsPendingSidebar(thickPendingSidebarID)).toStrictEqual(true);
expect(threadIsPendingSidebar(nonSidebarPendinThreadID)).toStrictEqual(
false,
);
});
});
6 changes: 5 additions & 1 deletion lib/utils/validation-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,9 @@ const uuidRegex =
'[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}';
const idSchemaRegex = `(?:(?:[0-9]+|${uuidRegex})\\|)?(?:[0-9]+|${uuidRegex})`;

const pendingThreadIDRegex = `pending/(type[0-9]+/[0-9]+(\\+[0-9]+)*|(sidebar|dm_sidebar)/${idSchemaRegex})`;
const pendingSidebarURLPrefix = 'sidebar';
const pendingThickSidebarURLPrefix = 'dm_sidebar';
const pendingThreadIDRegex = `pending/(type[0-9]+/[0-9]+(\\+[0-9]+)*|(${pendingSidebarURLPrefix}|${pendingThickSidebarURLPrefix})/${idSchemaRegex})`;
const thickThreadIDRegex: RegExp = new RegExp(`^${uuidRegex}$`);

const chatNameMaxLength = 191;
Expand Down Expand Up @@ -146,6 +148,8 @@ export {
assertWithValidator,
ashoatKeyserverID,
idSchemaRegex,
pendingSidebarURLPrefix,
pendingThickSidebarURLPrefix,
pendingThreadIDRegex,
thickThreadIDRegex,
validChatNameRegex,
Expand Down

0 comments on commit 085726b

Please sign in to comment.