diff --git a/src/components/Post/Embed/ExternalEmbed/PublicationEmbed/__tests__/util.test.ts b/src/components/Post/Embed/ExternalEmbed/PublicationEmbed/__tests__/util.test.ts new file mode 100644 index 0000000000..7d3e0ae85f --- /dev/null +++ b/src/components/Post/Embed/ExternalEmbed/PublicationEmbed/__tests__/util.test.ts @@ -0,0 +1,32 @@ +import {parseDidFromAtUri} from '#/components/Post/Embed/ExternalEmbed/PublicationEmbed/util' + +describe('parseDidFromAtUri', () => { + it('extracts a plc DID from a publication at-uri', () => { + expect( + parseDidFromAtUri('at://did:plc:abc123/site.standard.publication/3jx'), + ).toBe('did:plc:abc123') + }) + + it('extracts a web DID', () => { + expect( + parseDidFromAtUri( + 'at://did:web:example.com/site.standard.publication/3jx', + ), + ).toBe('did:web:example.com') + }) + + it('returns undefined for non-at-uri strings', () => { + expect(parseDidFromAtUri('https://example.com')).toBeUndefined() + }) + + it('returns undefined for empty / nullish input', () => { + expect(parseDidFromAtUri('')).toBeUndefined() + expect(parseDidFromAtUri(undefined)).toBeUndefined() + }) + + it('returns undefined when the authority is not a DID', () => { + expect( + parseDidFromAtUri('at://alice.bsky.social/site.standard.publication/3jx'), + ).toBeUndefined() + }) +}) diff --git a/src/components/Post/Embed/ExternalEmbed/PublicationEmbed/util.ts b/src/components/Post/Embed/ExternalEmbed/PublicationEmbed/util.ts new file mode 100644 index 0000000000..0fcd2c355c --- /dev/null +++ b/src/components/Post/Embed/ExternalEmbed/PublicationEmbed/util.ts @@ -0,0 +1,10 @@ +/** + * Extract the DID from an at-uri of the form `at://did:://`. + * Returns undefined if the input is falsy, not an at-uri, or the authority is not a DID. + */ +export function parseDidFromAtUri(uri: string | undefined): string | undefined { + if (!uri || !uri.startsWith('at://')) return undefined + const authority = uri.slice('at://'.length).split('/')[0] + if (!authority || !authority.startsWith('did:')) return undefined + return authority +}