Skip to content

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

9.0 KB
Raw Blame
/*
 * This module highly mirrors the Python code present in the backend repository.
 * For any changes made here, please make the corresponding changes in the
 * backend, or open an issue to track it.
 */
import enJson from "~~/i18n/locales/en.json"

import type { LicenseElement } from "#shared/constants/license"
import {
  getElements,
  getFullLicenseName,
  isPublicDomain,
} from "#shared/utils/license"
import { sanitizeHref } from "#shared/utils/sanitize-href"
import type { Media } from "#shared/types/media"

import type { Composer } from "vue-i18n"

/* Helper functions */

/**
 * Escape characters that have special meaning in HTML to break script injection
 * attacks.
 *
 * @param unsafe - the unsafe text to sanitise
 * @returns the sanitised text content
 */
const escapeHtml = (unsafe: string) =>
  unsafe
    .replace(/&/g, "&")
    .replace(/</g, "&lt;")
    .replace(/>/g, "&gt;")
    .replace(/"/g, "&quot;")
    .replace(/'/g, "&#039;")

/**
 * Create an HTML tag with the given name, attributes and children.
 *
 * @param name - the tag name
 * @param attrs - the attributes associated with the tag
 * @param children - the tags children
 * @returns the HTML markup of the tag
 */
const h = (
  name: string,
  attrs: Record<string, string>,
  children: string[] | null = null
): string => {
  const selfClosingTags = [
    "area",
    "base",
    "br",
    "col",
    "embed",
    "hr",
    "img",
    "input",
    "link",
    "meta",
    "param",
    "source",
    "track",
    "wbr",
  ]

  const map = Object.entries(attrs)
    .map(([key, value]) => `${key}="${escapeHtml(value)}"`)
    .join(" ")

  if (selfClosingTags.includes(name)) {
    return map ? `<${name} ${map} />` : `<${name} />`
  }

  const opening = map ? `<${name} ${map}>` : `<${name}>`
  const closing = `</${name}>`
  return `${opening}${(children ?? []).join("\n")}${closing}`
}

/**
 * Format the given string by replacing all placeholder keys with their values.
 * The pairs of placeholders and values are passed as a dictionary.
 *
 * @param text - the string in which to perform the formatting
 * @param replacements - the mapping of placeholders to values
 * @returns the formatted string
 */
const fmt = (text: string, replacements: Record<string, string>): string => {
  Object.entries(replacements).forEach(([key, value]) => {
    text = text.replace(`{${key}}`, value)
  })
  return text
}

const isObject = (value: unknown): value is Record<string, unknown> => {
  return typeof value === "object" && value !== null
}

const isBabelValue = (value: unknown): value is { loc: { source: string } } => {
  return (
    isObject(value) &&
    "loc" in value &&
    isObject(value.loc) &&
    "source" in value.loc &&
    typeof value.loc.source === "string"
  )
}

/**
 * Perform the same role as `i18n.t` except that it's restricted to reading
 * English from `en.json`. In unit tests, the json file is parsed by babel,
 * so we handle the parsed objects, too.
 *
 * @param path - the key to the JSON value to read
 * @param replacements - the pair of placeholders and their replacement strings
 * @returns the English string with placeholders substituted
 */
const fakeT = (
  path: string,
  replacements: Record<string, string> = {}
): string => {
  // enJson is parsed by babel in unit tests, so it returns a parsed object
  const jsonValue = enJson[path as keyof typeof enJson]
  if (isBabelValue(jsonValue)) {
    return fmt(jsonValue.loc.source, replacements)
  } else if (typeof jsonValue === "string") {
    return fmt(jsonValue, replacements)
  } else {
    return ""
  }
}

/**
 * Get an `<img>` tag corresponding to a license element.
 *
 * @param licenseElement - the license element for which to get the `<img>` tag
 * @returns the HTML markup of the `<img>` tag
 */
const licenseElementImg = (licenseElement: LicenseElement): string => {
  const filename = licenseElement.replace("-", ".")
  const src = `https://mirrors.creativecommons.org/presskit/icons/${filename}.svg`
  return h("img", {
    src,
    alt: "",
    style: "height: 1em; margin-right: 0.125em; display: inline;",
  })
}

/**
 * Get an HTML `<a>` tag set up with the `target` and `rel` attributes.
 *
 * @param href - the link that the anchor tag should point to
 * @param text - the textual content of the anchor tag
 * @returns the HTML markup of the `<a>` tag
 */
const extLink = (href: string, text: string) =>
  h("a", { rel: "noopener noreferrer", href: sanitizeHref(href) }, [text])

/**
 * Generate a Dublin Core conforming XML attribution snippet. Elements whose
 * values are not known are omitted.
 *
 * @param mediaItem - the media item being attributed
 * @returns the XML markup of the attribution
 */
const xmlAttribution = (mediaItem: Record<string, string | undefined>) => {
  const dcElements = {
    creator: mediaItem.creator,
    title: mediaItem.title,
    rights: mediaItem.license_url,
    identifier: mediaItem.foreign_landing_url,
    type: mediaItem.type,
  }
  const lines = Object.entries(dcElements)
    .filter((entry): entry is [string, string] => Boolean(entry[1]))
    .map(([name, value]) => `\t<dc:${name}>${escapeHtml(value)}</dc:${name}>`)
  return [
    '<attribution xmlns:dc="http://purl.org/dc/elements/1.1/">',
    ...lines,
    "</attribution>",
  ].join("\n")
}

/* Interfaces */

/**
 * This interface describes a subset of media that contains fields necessary
 * for generating a proper attribution.
 */
export type AttributableMedia = Pick<
  Media,
  | "originalTitle"
  | "foreign_landing_url"
  | "creator"
  | "creator_url"
  | "license"
  | "license_version"
  | "license_url"
  | "frontendMediaType"
  | "attribution"
>

/**
 * This interface describes the options that the `getAttribution` function can
 * take to customise its output.
 */
export interface AttributionOptions {
  includeIcons?: boolean
  isPlaintext?: boolean
  isXml?: boolean
}

/* Actual util */

/**
 * Get the HTML markup for properly attributing the given media item.
 *
 * @param mediaItem - the media item being attributed
 * @param t - the i18n `t` function to access translations
 * @param includePreview - whether to include the preview markup in the HTML
 * @returns the HTML markup of the attribution
 */
export const getAttribution = (
  mediaItem: AttributableMedia,
  t: Composer["t"] | null = null,
  { includeIcons, isPlaintext, isXml }: AttributionOptions = {
    isPlaintext: false,
    includeIcons: true,
    isXml: false,
  }
): string => {
  if (!mediaItem) {
    return ""
  }

  const isPd = isPublicDomain(mediaItem.license)

  const i18nBase = "mediaDetails.reuse.credit"

  const tFn = t
    ? (key: string, values?: Record<string, unknown>) =>
        values
          ? t(`${i18nBase}.${key}`, values as Record<string, unknown>)
          : t(`${i18nBase}.${key}`)
    : (key: string, values?: Record<string, unknown>) =>
        fakeT(`${i18nBase}.${key}`, values as Record<string, string>)

  /* Title */

  let title = mediaItem.originalTitle || tFn("genericTitle")
  if (!isPlaintext) {
    title = escapeHtml(title)
  }
  if (!isPlaintext && mediaItem.foreign_landing_url) {
    title = extLink(mediaItem.foreign_landing_url, title)
  }
  if (mediaItem.originalTitle) {
    title = tFn("actualTitle", { title })
  }

  /* License */

  const fullLicenseName = getFullLicenseName(
    mediaItem.license,
    mediaItem.license_version,
    t
  )
  let licenseIcons = ""
  if (includeIcons && mediaItem.license) {
    const elements = getElements(mediaItem.license)
    const icons = elements.map((element) => licenseElementImg(element))
    // Icons are only rendered if present for every element
    if (!icons.includes("")) {
      licenseIcons = icons.join("")
    }
  }
  let license = `${fullLicenseName} ${licenseIcons}`.trim()
  if (!isPlaintext && mediaItem.license_url) {
    license = extLink(`${mediaItem.license_url}?ref=openverse`, license)
  }

  /* Attribution */

  const attributionParts: Record<string, string> = {
    title,
    markedLicensed: tFn(isPd ? "marked" : "licensed"),
    license: license,
    viewLegal: "",
    creator: "",
  }

  if (isPlaintext && mediaItem.license_url) {
    attributionParts["viewLegal"] = tFn("viewLegalText", {
      termsCopy: tFn(isPd ? "termsText" : "copyText"),
      url: `${mediaItem.license_url}?ref=openverse`,
    })
  }

  if (mediaItem.creator) {
    let creator = mediaItem.creator
    if (!isPlaintext) {
      creator = escapeHtml(creator)
    }
    if (!isPlaintext && mediaItem.creator_url) {
      creator = extLink(mediaItem.creator_url, creator)
    }
    attributionParts.creator = tFn("creatorText", {
      creatorName: creator,
    })
  }

  const attribution = tFn("text", attributionParts)
    .replace(/\s{2}/g, " ")
    .trim()

  if (isXml) {
    const { frontendMediaType, ...restMediaItem } = mediaItem

    const xmlAttributionParts = {
      ...restMediaItem,
      title: mediaItem.originalTitle || tFn("genericTitle"),
      type: frontendMediaType === "audio" ? "Sound" : "StillImage",
    }

    return xmlAttribution(xmlAttributionParts)
  } else if (isPlaintext) {
    return attribution
  } else {
    return h("p", { class: "attribution" }, [attribution])
  }
}