Skip to content

Commit 8e11227

Browse files
authored
refactor: exclude protected previews from linking search (#2853)
1 parent d6cb83a commit 8e11227

2 files changed

Lines changed: 63 additions & 1 deletion

File tree

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
import { ContentType } from '@standardnotes/domain-core'
2+
import { getItemTitleInContextOfLinkBubble, doesItemMatchSearchQuery } from './doesItemMatchSearchQuery'
3+
import { DecryptedItemInterface, ItemContent, SNNote, SNTag, isNote, isTag } from '@standardnotes/snjs'
4+
import { WebApplicationInterface } from '@standardnotes/ui-services'
5+
6+
describe('getItemTitleInContextOfLinkBubble', () => {
7+
it('returns the title if present', () => {
8+
const item = { title: 'Test Title', content_type: ContentType.TYPES.Note } as jest.Mocked<SNNote>
9+
expect(getItemTitleInContextOfLinkBubble(item as DecryptedItemInterface<ItemContent>)).toBe('Test Title')
10+
})
11+
12+
it('returns the note preview if title is empty and item is a note', () => {
13+
const item = {
14+
preview_plain: 'Note Preview',
15+
title: '',
16+
content_type: ContentType.TYPES.Note,
17+
} as jest.Mocked<SNNote>
18+
expect(getItemTitleInContextOfLinkBubble(item as DecryptedItemInterface<ItemContent>)).toBe('Note Preview')
19+
})
20+
21+
it('returns empty string if title is empty and item is not a note', () => {
22+
const item = { title: '', content_type: ContentType.TYPES.Tag } as jest.Mocked<SNNote>
23+
expect(getItemTitleInContextOfLinkBubble(item as DecryptedItemInterface<ItemContent>)).toBe('')
24+
})
25+
})
26+
27+
describe('doesItemMatchSearchQuery', () => {
28+
const application = {} as WebApplicationInterface
29+
30+
it('returns false for a protected note even if the title matches the search query', () => {
31+
const item = {
32+
title: '',
33+
preview_plain: 'Protected Note Content',
34+
protected: true,
35+
archived: false,
36+
trashed: false,
37+
content_type: ContentType.TYPES.Note,
38+
} as jest.Mocked<SNNote>
39+
expect(
40+
doesItemMatchSearchQuery(item as DecryptedItemInterface<ItemContent>, 'protected note content', application),
41+
).toBeFalsy()
42+
})
43+
44+
it('returns true if the item title matches the search query', () => {
45+
const item = { title: 'Matched Item', archived: false, trashed: false }
46+
expect(doesItemMatchSearchQuery(item as DecryptedItemInterface<ItemContent>, 'matched', application)).toBeTruthy()
47+
})
48+
49+
it('returns false if the item is archived', () => {
50+
const item = { title: 'Archived Item', archived: true, trashed: false }
51+
expect(doesItemMatchSearchQuery(item as DecryptedItemInterface<ItemContent>, 'archived', application)).toBeFalsy()
52+
})
53+
54+
it('returns false if the item is trashed', () => {
55+
const item = { title: 'Trashed Item', archived: false, trashed: true }
56+
expect(doesItemMatchSearchQuery(item as DecryptedItemInterface<ItemContent>, 'trashed', application)).toBeFalsy()
57+
})
58+
})

packages/web/src/javascripts/Utils/Items/Search/doesItemMatchSearchQuery.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,11 @@ export function getItemTitleInContextOfLinkBubble(item: DecryptedItemInterface<I
77

88
function getItemSearchableString(item: DecryptedItemInterface<ItemContent>, application: WebApplicationInterface) {
99
if (isNote(item)) {
10-
return item.title.length > 0 ? item.title : item.preview_plain
10+
if (item.title.length > 0) {
11+
return item.title
12+
} else if (!item.protected) {
13+
return item.preview_plain
14+
}
1115
} else if (isTag(item)) {
1216
return application.items.getTagLongTitle(item)
1317
}

0 commit comments

Comments
 (0)