Skip to content

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

Add peaks=true to all audio searches (#1802)

Zack Kridab91e74ef9a03
diff --git a/src/data/api-service.ts b/src/data/api-service.ts
index 0d4ba1937..03b817034 100644
--- a/src/data/api-service.ts
+++ b/src/data/api-service.ts
@@ -55,7 +55,11 @@ export interface ApiService {
resource: string,
params: Record<string, string>
): Promise<AxiosResponse<T>>
- get<T = unknown>(resource: string, slug: string): Promise<AxiosResponse<T>>
+ get<T = unknown>(
+ resource: string,
+ slug: string,
+ params?: Record<string, string>
+ ): Promise<AxiosResponse<T>>
post<T = unknown>(
resource: string,
data: Parameters<AxiosInstance['post']>[1],
@@ -141,9 +145,10 @@ export const createApiService = ({
*/
get<T = unknown>(
resource: string,
- slug: string
+ slug: string,
+ params: Record<string, string> = {}
): Promise<AxiosResponse<T>> {
- return client.get(`${getResourceSlug(resource)}${slug}/`)
+ return client.get(`${getResourceSlug(resource)}${slug}/`, { params })
},
/**
diff --git a/src/data/media-service.ts b/src/data/media-service.ts
index 2391954e1..17b9cb0c4 100644
--- a/src/data/media-service.ts
+++ b/src/data/media-service.ts
@@ -2,7 +2,7 @@ import { decodeMediaData } from '~/utils/decode-media-data'
import type { ApiQueryParams } from '~/utils/search-query-transform'
import type { ApiService } from '~/data/api-service'
import type { DetailFromMediaType, Media } from '~/models/media'
-import type { SupportedMediaType } from '~/constants/media'
+import { AUDIO, type SupportedMediaType } from '~/constants/media'
import type { AxiosResponse } from 'axios'
@@ -49,6 +49,11 @@ class MediaService<T extends Media> {
async search(
params: ApiQueryParams
): Promise<MediaResult<Record<string, Media>>> {
+ // Add the `peaks` param to all audio searches automatically
+ if (this.mediaType === AUDIO) {
+ params.peaks = 'true'
+ }
+
const res = await this.apiService.query<MediaResult<T[]>>(
this.mediaType,
params as unknown as Record<string, string>
@@ -83,9 +88,14 @@ class MediaService<T extends Media> {
`MediaService.getRelatedMedia() id parameter required to retrieve related media.`
)
}
+ const params: ApiQueryParams = {}
+ if (this.mediaType === AUDIO) {
+ params.peaks = 'true'
+ }
const res = (await this.apiService.get(
this.mediaType,
- `${id}/related`
+ `${id}/related`,
+ params as unknown as Record<string, string>
)) as AxiosResponse<MediaResult<DetailFromMediaType<T>[]>>
return {
...res.data,
diff --git a/src/utils/search-query-transform.ts b/src/utils/search-query-transform.ts
index e8a6f719c..b4ddf3408 100644
--- a/src/utils/search-query-transform.ts
+++ b/src/utils/search-query-transform.ts
@@ -27,6 +27,11 @@ export interface ApiQueryParams {
length?: string
mature?: string
page?: string
+ /**
+ * A conditional to show audio waveform data.
+ * TODO: We'll need new ApiQueryParams types that accept a media type to allow media-specific params
+ */
+ peaks?: string
}
export type ApiQueryFilters = Omit<ApiQueryParams, 'q'>
diff --git a/test/tapes/search/audio/license_type=commercial&license=cc0&searchBy=creator&creator=cat_close.json5 b/test/tapes/search/audio/license_type=commercial&license=cc0&searchBy=creator&creator=cat_close.json5
index f0e704616..2d3c14798 100644
--- a/test/tapes/search/audio/license_type=commercial&license=cc0&searchBy=creator&creator=cat_close.json5
+++ b/test/tapes/search/audio/license_type=commercial&license=cc0&searchBy=creator&creator=cat_close.json5
@@ -9,7 +9,7 @@
accept: 'application/json, text/plain, */*',
connection: 'close',
},
- url: '/v1/audio/?license_type=commercial&license=cc0&searchBy=creator&creator=cat',
+ url: '/v1/audio/?license_type=commercial&license=cc0&searchBy=creator&creator=cat&peaks=true',
method: 'GET',
body: '',
},
@@ -85,4 +85,4 @@
results: [],
},
},
-}
\ No newline at end of file
+}
diff --git a/test/tapes/search/audio/license_type=commercial&license=cc0&searchBy=creator&creator=cat_keep-alive.json5 b/test/tapes/search/audio/license_type=commercial&license=cc0&searchBy=creator&creator=cat_keep-alive.json5
index ba6ba76e9..9b9360b8e 100644
--- a/test/tapes/search/audio/license_type=commercial&license=cc0&searchBy=creator&creator=cat_keep-alive.json5
+++ b/test/tapes/search/audio/license_type=commercial&license=cc0&searchBy=creator&creator=cat_keep-alive.json5
@@ -20,7 +20,7 @@
referer: 'http://localhost:8443/',
'accept-encoding': 'gzip, deflate, br',
},
- url: '/v1/audio/?license_type=commercial&license=cc0&searchBy=creator&creator=cat',
+ url: '/v1/audio/?license_type=commercial&license=cc0&searchBy=creator&creator=cat&peaks=true',
method: 'GET',
body: '',
},
@@ -99,4 +99,4 @@
results: [],
},
},
-}
\ No newline at end of file
+}
diff --git a/test/tapes/search/audio/q=birds&source=freesound_close.json5 b/test/tapes/search/audio/q=birds&source=freesound_close.json5
index 093a21c8b..eb3f893db 100644
--- a/test/tapes/search/audio/q=birds&source=freesound_close.json5
+++ b/test/tapes/search/audio/q=birds&source=freesound_close.json5
@@ -8,7 +8,7 @@
headers: {
connection: 'close',
},
- url: '/v1/audio/?q=birds&source=freesound',
+ url: '/v1/audio/?q=birds&source=freesound&peaks=true',
method: 'GET',
body: '',
},
@@ -20513,4 +20513,4 @@
],
},
},
-}
\ No newline at end of file
+}
diff --git a/test/tapes/search/audio/q=birds_close.json5 b/test/tapes/search/audio/q=birds_close.json5
index 245f61243..acd22104e 100644
--- a/test/tapes/search/audio/q=birds_close.json5
+++ b/test/tapes/search/audio/q=birds_close.json5
@@ -9,7 +9,7 @@
accept: 'application/json, text/plain, */*',
connection: 'close',
},
- url: '/v1/audio/?q=birds',
+ url: '/v1/audio/?q=birds&peaks=true',
method: 'GET',
body: '',
},
@@ -3188,4 +3188,4 @@
],
},
},
-}
\ No newline at end of file
+}
diff --git a/test/tapes/search/audio/q=birds_keep-alive.json5 b/test/tapes/search/audio/q=birds_keep-alive.json5
index 4fbba03d3..49735dd8e 100644
--- a/test/tapes/search/audio/q=birds_keep-alive.json5
+++ b/test/tapes/search/audio/q=birds_keep-alive.json5
@@ -20,7 +20,7 @@
referer: 'http://localhost:8443/',
'accept-encoding': 'gzip, deflate, br',
},
- url: '/v1/audio/?q=birds',
+ url: '/v1/audio/?q=birds&peaks=true',
method: 'GET',
body: '',
},
@@ -3205,4 +3205,4 @@
],
},
},
-}
\ No newline at end of file
+}
diff --git a/test/tapes/search/audio/q=cat&license=cc0_keep-alive.json5 b/test/tapes/search/audio/q=cat&license=cc0_keep-alive.json5
index f74d5a91d..c42ecf14d 100644
--- a/test/tapes/search/audio/q=cat&license=cc0_keep-alive.json5
+++ b/test/tapes/search/audio/q=cat&license=cc0_keep-alive.json5
@@ -20,7 +20,7 @@
referer: 'http://localhost:8443/',
'accept-encoding': 'gzip, deflate, br',
},
- url: '/v1/audio/?q=cat&license=cc0',
+ url: '/v1/audio/?q=cat&license=cc0&peaks=true',
method: 'GET',
body: '',
},
@@ -1989,4 +1989,4 @@
],
},
},
-}
\ No newline at end of file
+}
diff --git a/test/tapes/search/audio/q=cat&license_type=commercial_close.json5 b/test/tapes/search/audio/q=cat&license_type=commercial_close.json5
index a06aca54f..fc72b2038 100644
--- a/test/tapes/search/audio/q=cat&license_type=commercial_close.json5
+++ b/test/tapes/search/audio/q=cat&license_type=commercial_close.json5
@@ -9,7 +9,7 @@
accept: 'application/json, text/plain, */*',
connection: 'close',
},
- url: '/v1/audio/?q=cat&license_type=commercial',
+ url: '/v1/audio/?q=cat&license_type=commercial&peaks=true',
method: 'GET',
body: '',
},
@@ -4128,4 +4128,4 @@
],
},
},
-}
\ No newline at end of file
+}
diff --git a/test/tapes/search/audio/q=cat_close.json5 b/test/tapes/search/audio/q=cat_close.json5
index ec03f3b3e..ec3d8c3cc 100644
--- a/test/tapes/search/audio/q=cat_close.json5
+++ b/test/tapes/search/audio/q=cat_close.json5
@@ -9,7 +9,7 @@
accept: 'application/json, text/plain, */*',
connection: 'close',
},
- url: '/v1/audio/?q=cat',
+ url: '/v1/audio/?q=cat&peaks=true',
method: 'GET',
body: '',
},
@@ -7668,4 +7668,4 @@
],
},
},
-}
\ No newline at end of file
+}
diff --git a/test/tapes/search/audio/q=cat_keep-alive.json5 b/test/tapes/search/audio/q=cat_keep-alive.json5
index c5ee49bd4..208a7683d 100644
--- a/test/tapes/search/audio/q=cat_keep-alive.json5
+++ b/test/tapes/search/audio/q=cat_keep-alive.json5
@@ -20,7 +20,7 @@
referer: 'http://localhost:8443/',
'accept-encoding': 'gzip, deflate, br',
},
- url: '/v1/audio/?q=cat',
+ url: '/v1/audio/?q=cat&peaks=true',
method: 'GET',
body: '',
},
@@ -7685,4 +7685,4 @@
],
},
},
-}
\ No newline at end of file
+}
diff --git a/test/tapes/search/audio/q=dog_close.json5 b/test/tapes/search/audio/q=dog_close.json5
index bb9ceaaf5..bb2f3220f 100644
--- a/test/tapes/search/audio/q=dog_close.json5
+++ b/test/tapes/search/audio/q=dog_close.json5
@@ -8,7 +8,7 @@
headers: {
connection: 'close',
},
- url: '/v1/audio/?q=dog',
+ url: '/v1/audio/?q=dog&peaks=true',
method: 'GET',
body: '',
},
@@ -18909,4 +18909,4 @@
],
},
},
-}
\ No newline at end of file
+}
diff --git a/test/tapes/search/audio/q=galah&license_type=modification_keep-alive.json5 b/test/tapes/search/audio/q=galah&license_type=modification_keep-alive.json5
index 7396248f4..5ca790c3b 100644
--- a/test/tapes/search/audio/q=galah&license_type=modification_keep-alive.json5
+++ b/test/tapes/search/audio/q=galah&license_type=modification_keep-alive.json5
@@ -20,7 +20,7 @@
referer: 'http://localhost:8443/',
'accept-encoding': 'gzip, deflate, br',
},
- url: '/v1/audio/?q=galah&license_type=modification',
+ url: '/v1/audio/?q=galah&license_type=modification&peaks=true',
method: 'GET',
body: '',
},
@@ -1158,4 +1158,4 @@
],
},
},
-}
\ No newline at end of file
+}
diff --git a/test/tapes/search/audio/q=galah_close.json5 b/test/tapes/search/audio/q=galah_close.json5
index d7a4cc41d..048cd2348 100644
--- a/test/tapes/search/audio/q=galah_close.json5
+++ b/test/tapes/search/audio/q=galah_close.json5
@@ -9,7 +9,7 @@
accept: 'application/json, text/plain, */*',
connection: 'close',
},
- url: '/v1/audio/?q=galah',
+ url: '/v1/audio/?q=galah&peaks=true',
method: 'GET',
body: '',
},
@@ -1141,4 +1141,4 @@
],
},
},
-}
\ No newline at end of file
+}
diff --git a/test/tapes/search/audio/q=galah_keep-alive.json5 b/test/tapes/search/audio/q=galah_keep-alive.json5
index f8f99ce43..241ea851f 100644
--- a/test/tapes/search/audio/q=galah_keep-alive.json5
+++ b/test/tapes/search/audio/q=galah_keep-alive.json5
@@ -20,7 +20,7 @@
referer: 'http://localhost:8443/',
'accept-encoding': 'gzip, deflate, br',
},
- url: '/v1/audio/?q=galah',
+ url: '/v1/audio/?q=galah&peaks=true',
method: 'GET',
body: '',
},
@@ -1158,4 +1158,4 @@
],
},
},
-}
\ No newline at end of file
+}
diff --git a/test/tapes/search/audio/q=honey_close.json5 b/test/tapes/search/audio/q=honey_close.json5
index f14350470..ef3ff0c65 100644
--- a/test/tapes/search/audio/q=honey_close.json5
+++ b/test/tapes/search/audio/q=honey_close.json5
@@ -8,7 +8,7 @@
headers: {
connection: 'close',
},
- url: '/v1/audio/?q=honey',
+ url: '/v1/audio/?q=honey&peaks=true',
method: 'GET',
body: '',
},
@@ -16846,4 +16846,4 @@
],
},
},
-}
\ No newline at end of file
+}
diff --git a/test/tapes/search/audio/q=honey_keep-alive.json5 b/test/tapes/search/audio/q=honey_keep-alive.json5
index 27ee07c12..18ef4ec8e 100644
--- a/test/tapes/search/audio/q=honey_keep-alive.json5
+++ b/test/tapes/search/audio/q=honey_keep-alive.json5
@@ -9,7 +9,7 @@
headers: {
connection: 'keep-alive',
},
- url: '/v1/audio/?q=honey',
+ url: '/v1/audio/?q=honey&peaks=true',
method: 'GET',
body: '',
},
@@ -7992,4 +7992,4 @@
],
},
},
-}
\ No newline at end of file
+}