Skip to content

Openverse is a search engine for openly-licensed media. This monorepo includes all application code.

Handle unknown creator in attribution (#1371)

Dhruv Bhanushali05a9b0dc16a5
diff --git a/src/models/media.ts b/src/models/media.ts
index 547022ec9..e945d6829 100644
--- a/src/models/media.ts
+++ b/src/models/media.ts
@@ -13,7 +13,7 @@ export interface Media {
id: string
title: string
- creator: string
+ creator?: string
creator_url?: string
url: string
diff --git a/src/utils/attribution-html.ts b/src/utils/attribution-html.ts
index 1f7126579..d647bdb95 100644
--- a/src/utils/attribution-html.ts
+++ b/src/utils/attribution-html.ts
@@ -111,7 +111,7 @@ export const getAttribution = (
/* Creator */
let creatorLink = mediaItem.creator || ''
- if (!isPlaintext && mediaItem.creator_url)
+ if (!isPlaintext && mediaItem.creator_url && creatorLink)
creatorLink = extLink(mediaItem.creator_url, creatorLink)
/* License */
@@ -145,11 +145,13 @@ export const getAttribution = (
if (i18n) {
let fillers: Record<string, string> = {
title: titleLink,
- creator: i18n
- .t(`${i18nBase}.creator-text`, {
- 'creator-name': creatorLink,
- })
- .toString(),
+ creator: creatorLink
+ ? i18n
+ .t(`${i18nBase}.creator-text`, {
+ 'creator-name': creatorLink,
+ })
+ .toString()
+ : '',
'marked-licensed': i18n
.t(`${i18nBase}.${isPd ? 'marked' : 'licensed'}`)
.toString(),
@@ -170,24 +172,21 @@ export const getAttribution = (
}
attribution = i18n.t(`${i18nBase}.text`, fillers).toString()
} else {
- let attributionParts = [
- `"${titleLink}"`,
- ' by ',
- creatorLink,
+ const attributionParts = [`"${titleLink}"`]
+ if (creatorLink) attributionParts.push(' by ', creatorLink)
+ attributionParts.push(
isPd ? ' is marked with ' : ' is licensed under ',
licenseLink,
- '.',
- ]
- if (isPlaintext) {
- attributionParts = [
- ...attributionParts,
+ '.'
+ )
+ if (isPlaintext)
+ attributionParts.push(
' To view ',
isPd ? 'the terms,' : 'a copy of this license,',
' visit',
mediaItem.license_url,
- '.',
- ]
- }
+ '.'
+ )
attribution = attributionParts.join('')
}
diff --git a/test/unit/specs/utils/attribution-html.spec.ts b/test/unit/specs/utils/attribution-html.spec.ts
index 12e1d03a4..08c9e3eba 100644
--- a/test/unit/specs/utils/attribution-html.spec.ts
+++ b/test/unit/specs/utils/attribution-html.spec.ts
@@ -43,6 +43,15 @@ describe('getAttribution', () => {
}
)
+ it('omits creator if not known', () => {
+ const mediaItemNoCreator = { ...mediaItem, creator: undefined }
+ const attrText = getAttribution(mediaItemNoCreator, i18n, {
+ isPlaintext: true,
+ })
+ const expectation = '"Title" is marked with Public Domain Mark 1.0'
+ expect(attrText).toContain(expectation)
+ })
+
it('does not use anchors in plain-text mode', () => {
document.body.innerHTML = getAttribution(mediaItem, i18n)
expect(document.getElementsByTagName('a').length !== 0)