-
Notifications
You must be signed in to change notification settings - Fork 171
/
index.ts
529 lines (487 loc) · 16.5 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
import { defineStore } from "pinia"
import { warn } from "~/utils/console"
import { hash, rand as prng } from "~/utils/prng"
import { isRetriable, parseFetchingError } from "~/utils/errors"
import type {
AudioDetail,
DetailFromMediaType,
ImageDetail,
Media,
} from "~/types/media"
import type { FetchingError, FetchState } from "~/types/fetch-state"
import {
ALL_MEDIA,
AUDIO,
IMAGE,
isAdditionalSearchType,
SupportedMediaType,
supportedMediaTypes,
} from "~/constants/media"
import { NO_RESULT } from "~/constants/errors"
import { initServices } from "~/stores/media/services"
import { isSearchTypeSupported, useSearchStore } from "~/stores/search"
import { useRelatedMediaStore } from "~/stores/media/related-media"
import { deepFreeze } from "~/utils/deep-freeze"
export type MediaStoreResult = {
count: number
pageCount: number
page: number
items: Record<string, Media>
}
export interface MediaState {
results: {
audio: MediaStoreResult
image: MediaStoreResult
}
mediaFetchState: {
audio: FetchState
image: FetchState
}
currentPage: number
}
export type MediaResults = {
[MT in SupportedMediaType]: DetailFromMediaType<MT>[]
}
export const initialResults = deepFreeze({
count: 0,
page: 0,
pageCount: 0,
items: {},
}) as MediaStoreResult
export const useMediaStore = defineStore("media", {
state: (): MediaState => ({
results: {
[AUDIO]: { ...initialResults },
[IMAGE]: { ...initialResults },
},
mediaFetchState: {
[AUDIO]: {
isFetching: false,
hasStarted: false,
isFinished: false,
fetchingError: null,
},
[IMAGE]: {
isFetching: false,
hasStarted: false,
isFinished: false,
fetchingError: null,
},
},
currentPage: 0,
}),
getters: {
_searchType() {
const searchType = useSearchStore().searchType
if (isAdditionalSearchType(searchType)) {
return ALL_MEDIA
}
return searchType
},
/**
* Returns a media item that exists either in the search results
* or in the related media store.
* This makes the single result page rendering faster by providing initial data.
* We still need to fetch the single result from the API to get the full data.
* @param state - the media store state
*/
getItemById: (state) => {
return (mediaType: SupportedMediaType, id: string): Media | undefined => {
const itemFromSearchResults = state.results[mediaType].items[id]
if (itemFromSearchResults) return itemFromSearchResults
return useRelatedMediaStore().getItemById(id)
}
},
/**
* Returns object with a key for each supported media type and arrays of media items for each.
*/
resultItems(state): MediaResults {
return supportedMediaTypes.reduce(
(items, type) => ({
...items,
[type]: Object.values(state.results[type].items),
}),
{} as MediaResults
)
},
/**
* Returns result item counts for each supported media type.
*/
resultCountsPerMediaType(): [SupportedMediaType, number][] {
return supportedMediaTypes.map((type) => [type, this.results[type].count])
},
/**
* Returns the total count of results for selected search type, sums all media results for ALL_MEDIA or additional types.
* If the count is more than 10000, returns 10000 to match the API result.
*/
resultCount(state) {
const types = (
this._searchType === ALL_MEDIA ||
!isSearchTypeSupported(this._searchType)
? supportedMediaTypes
: [this._searchType]
) as SupportedMediaType[]
const count = types.reduce(
(sum, mediaType) => sum + state.results[mediaType].count,
0
)
return Math.min(count, 10000)
},
/**
* Search fetching state for selected search type. For 'All content', aggregates
* the values for supported media types.
*/
fetchState(): FetchState {
if (this._searchType === ALL_MEDIA) {
/**
* For all_media, we return 'All media fetching error' if all types have some kind of error.
*/
const atLeastOne = (property: keyof FetchState) =>
supportedMediaTypes.some(
(type) => this.mediaFetchState[type][property]
)
/**
* Returns a combined error for all media types.
*
* If at least one media type has a 429 error, returns 429 to stop the
* user from retrying the request.
*
* If all media types have a NO_RESULT error, returns it to show the "No results" page.
*
* If at least one media type has a different error, returns the first error.
* The handling of errors other than 429 should be improved after we
* get more information about the error codes we get from the API.
*/
const allMediaError = (): null | FetchingError => {
const errors = getMediaErrors(this.mediaFetchState)
if (!errors.length) {
return null
}
const tooManyRequestsError = findTooManyRequestsError(errors)
if (tooManyRequestsError !== null) {
tooManyRequestsError["searchType"] = ALL_MEDIA
return tooManyRequestsError
}
const noResultError = findNoResultError(errors)
if (noResultError) {
return noResultError
}
// Temporarily return the first error, until we have a better way to handle this.
const results = errors.filter((error) => error.code !== NO_RESULT)
return results.length ? results[0] : null
}
return {
isFetching: atLeastOne("isFetching"),
fetchingError: allMediaError(),
hasStarted: atLeastOne("hasStarted"),
isFinished: supportedMediaTypes.every(
(type) => this.mediaFetchState[type].isFinished
),
}
} else if (isSearchTypeSupported(this._searchType)) {
return this.mediaFetchState[this._searchType]
} else {
return {
isFetching: false,
fetchingError: null,
hasStarted: false,
isFinished: false,
}
}
},
/**
* Returns a mixed bag of search results across media types.
*
* This does not contain all hits across all media types! It contains all
* the hits for the media type with the most hits and as many hits from
* other media types as can be sparsely spliced into the list. The
* leftover hits will appear in subsequent pages.
*
* TODO: Fix the algorithm.
* This implementation can hide hits from media types with fewer hits.
*/
allMedia(state): (AudioDetail | ImageDetail)[] {
const media = this.resultItems
// Seed the random number generator with the ID of
// the first search result, so the non-image
// distribution is the same on repeated searches
const seedString = media[IMAGE][0]?.id
let seed: number
if (typeof seedString === "string") {
seed = hash(seedString)
} else {
let otherTypeId = "string"
for (const type of supportedMediaTypes.slice(1)) {
if (typeof media[type][0]?.id === "string") {
otherTypeId = media[type][0].id
break
}
}
seed = hash(otherTypeId)
}
const rand = prng(seed)
const randomIntegerInRange = (min: number, max: number) =>
Math.floor(rand() * (max - min + 1)) + min
// First, identify the media type with most hits
const [mostHits] = supportedMediaTypes
.map((type): [SupportedMediaType, number] => [
type,
state.results[type].count,
])
.sort(([, a], [, b]) => b - a)[0]
// First, set the results to the type with most hits...
const newResults = media[mostHits] as (AudioDetail | ImageDetail)[]
// ...then push other items into the list, using a random index.
let nonImageIndex = 1
for (const type of supportedMediaTypes.filter(
(type) => type !== mostHits
)) {
for (const item of media[type]) {
newResults.splice(nonImageIndex, 0, item)
nonImageIndex = randomIntegerInRange(
nonImageIndex + 1,
nonImageIndex + 6
)
// Prevent the bunching of audio results at the end.
if (nonImageIndex > newResults.length) break
}
}
return newResults
},
_fetchableMediaTypes(): SupportedMediaType[] {
return (
(this._searchType !== ALL_MEDIA
? [this._searchType]
: [IMAGE, AUDIO]) as SupportedMediaType[]
).filter(
(type) =>
!this.mediaFetchState[type].fetchingError &&
!this.mediaFetchState[type].isFetching &&
!this.mediaFetchState[type].isFinished
)
},
},
actions: {
_startFetching(mediaType: SupportedMediaType) {
this.mediaFetchState[mediaType].isFetching = true
this.mediaFetchState[mediaType].hasStarted = true
this.mediaFetchState[mediaType].isFinished = false
this.mediaFetchState[mediaType].fetchingError = null
},
/**
* Called when the request is finished, regardless of whether it was successful or not.
* @param mediaType - The media type for which the request was made.
* @param error - The string representation of the error, if any.
*/
_endFetching(mediaType: SupportedMediaType, error?: FetchingError) {
this.mediaFetchState[mediaType].fetchingError = error || null
this.mediaFetchState[mediaType].hasStarted = true
this.mediaFetchState[mediaType].isFetching = false
if (error && !isRetriable(error)) {
this.mediaFetchState[mediaType].isFinished = true
}
},
/**
* This is called when there are no more results available in the API for specific query.
* @param mediaType - The media type for which the request was made.
*/
_finishFetchingForQuery(mediaType: SupportedMediaType) {
this.mediaFetchState[mediaType].isFinished = true
this.mediaFetchState[mediaType].hasStarted = true
this.mediaFetchState[mediaType].isFetching = false
},
_resetFetchState() {
for (const mediaType of supportedMediaTypes) {
this.mediaFetchState[mediaType].isFetching = false
this.mediaFetchState[mediaType].hasStarted = false
this.mediaFetchState[mediaType].isFinished = false
this.mediaFetchState[mediaType].fetchingError = null
}
},
_updateFetchState(
mediaType: SupportedMediaType,
action: "reset" | "start" | "end" | "finish",
error?: FetchingError
) {
switch (action) {
case "reset":
this._resetFetchState()
break
case "start":
this._startFetching(mediaType)
break
case "end":
this._endFetching(mediaType, error)
break
case "finish":
this._finishFetchingForQuery(mediaType)
break
}
},
setMedia<T extends SupportedMediaType>(params: {
mediaType: T
media: Record<string, DetailFromMediaType<T>>
mediaCount: number
page: number
pageCount: number
shouldPersistMedia: boolean | undefined
}) {
const {
mediaType,
media,
mediaCount,
page,
pageCount,
shouldPersistMedia,
} = params
let mediaToSet
if (shouldPersistMedia) {
mediaToSet = { ...this.results[mediaType].items, ...media } as Record<
string,
DetailFromMediaType<T>
>
} else {
mediaToSet = media
}
const mediaPage = page || 1
this.results[mediaType].items = mediaToSet
this.results[mediaType].count = mediaCount || 0
this.results[mediaType].page = mediaCount === 0 ? 0 : mediaPage
this.results[mediaType].pageCount = pageCount
if (mediaPage >= pageCount) {
this._updateFetchState(mediaType, "finish")
}
},
/**
* Clears the items for all passed media types, and resets fetch state.
*/
resetMedia(mediaType: SupportedMediaType) {
this.results[mediaType].items = {}
this.results[mediaType].count = 0
this.results[mediaType].page = 0
this.results[mediaType].pageCount = 0
},
/**
* Calls `fetchSingleMediaType` for selected media type(s).
* Can be called by changing the search query
* (search term or filter item), or by clicking 'Load more' button.
* If the search query changed, fetch state is reset, otherwise only the media types for which
* fetchState.isFinished is not true are fetched.
*/
async fetchMedia(payload: { shouldPersistMedia?: boolean } = {}) {
const mediaType = this._searchType
const shouldPersistMedia = Boolean(payload.shouldPersistMedia)
if (!shouldPersistMedia) {
this.clearMedia()
}
const mediaToFetch = this._fetchableMediaTypes
const resultCounts = await Promise.all(
mediaToFetch.map((mediaType) =>
this.fetchSingleMediaType({ mediaType, shouldPersistMedia })
)
)
const resultCount = resultCounts.includes(null)
? null
: (resultCounts as number[]).reduce((a, b) => a + b, 0)
this.currentPage =
mediaType === ALL_MEDIA
? this.currentPage + 1
: this.results[mediaType].page
return resultCount
},
clearMedia() {
supportedMediaTypes.forEach((mediaType) => {
this.resetMedia(mediaType)
this._resetFetchState()
})
this.currentPage = 0
},
/**
* @param mediaType - the mediaType to fetch (do not use 'All_media' here)
* @param shouldPersistMedia - whether the existing media should be added to or replaced.
*/
async fetchSingleMediaType({
mediaType,
shouldPersistMedia,
}: {
mediaType: SupportedMediaType
shouldPersistMedia: boolean
}) {
const searchStore = useSearchStore()
const { pathSlug, query } = searchStore.getSearchUrlParts(mediaType)
let page = this.results[mediaType].page + 1
if (shouldPersistMedia) {
query.page = `${page}`
}
this._updateFetchState(mediaType, "start")
try {
const accessToken = this.$nuxt.$openverseApiToken
const service = initServices[mediaType](accessToken)
const data = await service.search(query, pathSlug)
const mediaCount = data.result_count
let errorData: FetchingError | undefined
/**
* When there are no results for a query, the API returns a 200 response.
* In such cases, we show the "No results" client error page.
*/
if (!mediaCount) {
page = 1
errorData = {
message: `No results found for ${query.q}`,
code: NO_RESULT,
requestKind: "search",
searchType: mediaType,
details: { searchTerm: query.q ?? "" },
}
}
this._updateFetchState(mediaType, "end", errorData)
this.setMedia({
mediaType,
media: data.results,
mediaCount,
pageCount: data.page_count,
shouldPersistMedia,
page,
})
return mediaCount
} catch (error: unknown) {
const errorData = parseFetchingError(error, mediaType, "search", {
searchTerm: query.q ?? "",
})
this._updateFetchState(mediaType, "end", errorData)
this.$nuxt.$sentry.captureException(error, {
extra: { errorData },
})
return null
}
},
setMediaProperties(
type: SupportedMediaType,
id: string,
properties: Partial<DetailFromMediaType<typeof type>>
) {
const item = this.getItemById(type, id)
if (item) {
Object.assign(item, properties)
} else {
warn(
`Attempted to update media item ${type} ${id} but could not find it.`
)
}
},
},
})
const getMediaErrors = (mediaFetchStates: MediaState["mediaFetchState"]) => {
return supportedMediaTypes
.map((mediaType) => mediaFetchStates[mediaType].fetchingError)
.filter((err): err is FetchingError => err !== null)
}
const findTooManyRequestsError = (errors: FetchingError[]) => {
return errors.find(({ statusCode }) => statusCode === 429) ?? null
}
const findNoResultError = (errors: FetchingError[]): FetchingError | null => {
return errors.length === supportedMediaTypes.length &&
errors.every(({ code }) => code === NO_RESULT)
? { ...errors[0], searchType: ALL_MEDIA }
: null
}