@playpilot/tpi 5.28.0 → 5.30.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/link-injections.js +8 -8
- package/package.json +1 -1
- package/src/lib/api/externalPages.ts +26 -13
- package/src/routes/components/Rails/ParticipantsRail.svelte +28 -1
- package/src/tests/lib/api/externalPages.test.js +16 -6
- package/src/tests/routes/components/Rails/ParticipantsRail.test.js +18 -2
package/package.json
CHANGED
|
@@ -10,7 +10,7 @@ import { getApiToken } from '$lib/token'
|
|
|
10
10
|
let pollTimeout: ReturnType<typeof setTimeout> | null = null
|
|
11
11
|
|
|
12
12
|
/**
|
|
13
|
-
* Fetch link injections for a URL.
|
|
13
|
+
* Fetch link injections for a URL. This will be either a POST or a GET request depending on whether on not the AI should run.
|
|
14
14
|
* @param pageText Text content of the article
|
|
15
15
|
* @param options
|
|
16
16
|
* @param [options.url] URL of the given article
|
|
@@ -28,21 +28,34 @@ export async function fetchLinkInjections(
|
|
|
28
28
|
|
|
29
29
|
if (!apiToken) throw new Error('No token was provided')
|
|
30
30
|
|
|
31
|
-
|
|
32
|
-
|
|
31
|
+
const apiUrl = `/external-pages/?api-token=${apiToken}&include_title_details=true${isEditorialMode ? '&editorial_mode_enabled=true' : ''}&language=${language}`
|
|
32
|
+
let response: LinkInjectionResponse
|
|
33
|
+
|
|
34
|
+
// We use separate requests when running the AI or setting the editor session vs when only getting the results.
|
|
35
|
+
// For regular requests we use a GET endpoint, but when saving data we POST to the same url.
|
|
36
|
+
if (Object.entries(params).length) {
|
|
37
|
+
// Additional params are added when re-running AI. These are necessary for the API
|
|
38
|
+
if (params.run_ai) {
|
|
39
|
+
params.hash = hash
|
|
40
|
+
params.page_text = pageText
|
|
41
|
+
params.metadata = getPageMetaData()
|
|
42
|
+
}
|
|
33
43
|
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
44
|
+
// Always add the given URL, this is used to match the article to the API record
|
|
45
|
+
params.url = url
|
|
46
|
+
|
|
47
|
+
response = await api<LinkInjectionResponse>(apiUrl, {
|
|
48
|
+
method: 'POST',
|
|
49
|
+
body: params,
|
|
50
|
+
})
|
|
51
|
+
} else {
|
|
52
|
+
// When getting injections without posting we append the URL of the page to the URL for the request.
|
|
53
|
+
// All other params are only relevant during the POST request.
|
|
54
|
+
response = await api<LinkInjectionResponse>(apiUrl + `&url=${url}`, {
|
|
55
|
+
method: 'GET',
|
|
56
|
+
})
|
|
39
57
|
}
|
|
40
58
|
|
|
41
|
-
const response = await api<LinkInjectionResponse>(`/external-pages/?api-token=${apiToken}&include_title_details=true${isEditorialMode ? '&editorial_mode_enabled=true' : ''}&language=${language}`, {
|
|
42
|
-
method: 'POST',
|
|
43
|
-
body: params,
|
|
44
|
-
})
|
|
45
|
-
|
|
46
59
|
// This is used when debugging (using window.PlayPilotLinkInjections.debug())
|
|
47
60
|
window.PlayPilotLinkInjections.last_successful_fetch = response
|
|
48
61
|
|
|
@@ -1,7 +1,9 @@
|
|
|
1
1
|
<script lang="ts">
|
|
2
2
|
import { fetchParticipantsForTitle } from '$lib/api/participants'
|
|
3
3
|
import { TrackingEvent } from '$lib/enums/TrackingEvent'
|
|
4
|
+
import { getLinkInjectionElements, getLinkInjectionsParentElement, getPageText } from '$lib/injection'
|
|
4
5
|
import { openModal } from '$lib/modal'
|
|
6
|
+
import { cleanPhrase, findNumberOfMatchesInString } from '$lib/text'
|
|
5
7
|
import { track } from '$lib/tracking'
|
|
6
8
|
import type { ParticipantData } from '$lib/types/participant'
|
|
7
9
|
import type { TitleData } from '$lib/types/title'
|
|
@@ -13,6 +15,31 @@
|
|
|
13
15
|
|
|
14
16
|
const { title }: Props = $props()
|
|
15
17
|
|
|
18
|
+
/**
|
|
19
|
+
* Order participants by how often their name appears in an the page text. This only takes into account
|
|
20
|
+
* their full name. If an actor is mentioned by first name only, which is often the case, they won't get
|
|
21
|
+
* boosted. Often times if an actor is mentioned at least once, their full name will be somewhere in the
|
|
22
|
+
* page text, so the goal is still the same; to list participants that are mentioned in the page text before
|
|
23
|
+
* other participants.
|
|
24
|
+
*/
|
|
25
|
+
function orderParticipantsByPageTextOccurrence(participants: ParticipantData[]): ParticipantData[] {
|
|
26
|
+
const pageText = cleanPhrase(getPageText(getLinkInjectionElements(getLinkInjectionsParentElement())))
|
|
27
|
+
|
|
28
|
+
// Create a new object with all participants by their sid with the number of times their names appear in the page text
|
|
29
|
+
const participantsByOccurrence: Record<string, { participant: ParticipantData, count: number }> = {}
|
|
30
|
+
for(const participant of participants) {
|
|
31
|
+
const count = findNumberOfMatchesInString(pageText, cleanPhrase(participant.name))
|
|
32
|
+
|
|
33
|
+
participantsByOccurrence[participant.sid] = { participant, count }
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
// Sort all participants by their occurrences. If a participant did not occur their positioned will be retained
|
|
37
|
+
// relative to where it was before
|
|
38
|
+
const participantsSortedByCount = Object.values(participantsByOccurrence).sort((a, b) => b.count - a.count)
|
|
39
|
+
|
|
40
|
+
return participantsSortedByCount.map(({ participant }) => participant)
|
|
41
|
+
}
|
|
42
|
+
|
|
16
43
|
function onclick(event: MouseEvent, participant: ParticipantData): void {
|
|
17
44
|
openModal({ event, type: 'participant', data: participant })
|
|
18
45
|
track(TrackingEvent.ParticipantClick, null, { title_source: title.original_title, participant: participant.name })
|
|
@@ -28,7 +55,7 @@
|
|
|
28
55
|
{:then participants}
|
|
29
56
|
{#if participants?.length}
|
|
30
57
|
<Rail heading="Cast">
|
|
31
|
-
{#each participants.slice(0, 15) as participant}
|
|
58
|
+
{#each orderParticipantsByPageTextOccurrence(participants).slice(0, 15) as participant}
|
|
32
59
|
<button class="participant" data-testid="participant" onclick={event => onclick(event, participant)}>
|
|
33
60
|
<span class="truncate">{participant.name}</span>
|
|
34
61
|
|
|
@@ -33,22 +33,32 @@ describe('$lib/api/externalPages', () => {
|
|
|
33
33
|
window.PlayPilotLinkInjections = { token: 'a' }
|
|
34
34
|
})
|
|
35
35
|
|
|
36
|
-
it('Should call api with given url
|
|
36
|
+
it('Should call api with GET method with given url', async () => {
|
|
37
37
|
vi.mocked(api).mockResolvedValueOnce('Some response')
|
|
38
38
|
|
|
39
39
|
const response = await fetchLinkInjections('Some text', { url: 'https://some-url', hash: 'some-hash' })
|
|
40
40
|
|
|
41
|
+
expect(response).toBe('Some response')
|
|
42
|
+
expect(api).toHaveBeenCalledWith(
|
|
43
|
+
'/external-pages/?api-token=a&include_title_details=true&language=en-US&url=https://some-url',
|
|
44
|
+
expect.objectContaining({
|
|
45
|
+
method: 'GET',
|
|
46
|
+
}),
|
|
47
|
+
)
|
|
48
|
+
})
|
|
49
|
+
|
|
50
|
+
it('Should call api with POST method and full details when fetching with run_ai as true', async () => {
|
|
51
|
+
vi.mocked(api).mockResolvedValueOnce('Some response')
|
|
52
|
+
|
|
53
|
+
const response = await fetchLinkInjections('Some text', { url: 'https://some-url', hash: 'some-hash', params: { key: 'value' } })
|
|
54
|
+
|
|
41
55
|
expect(response).toBe('Some response')
|
|
42
56
|
expect(api).toHaveBeenCalledWith(
|
|
43
57
|
expect.stringContaining('api-token'),
|
|
44
58
|
expect.objectContaining({
|
|
45
59
|
body: {
|
|
46
60
|
url: 'https://some-url',
|
|
47
|
-
|
|
48
|
-
content_heading: null,
|
|
49
|
-
content_modified_time: null,
|
|
50
|
-
content_published_time: null,
|
|
51
|
-
},
|
|
61
|
+
key: 'value',
|
|
52
62
|
},
|
|
53
63
|
method: 'POST',
|
|
54
64
|
}),
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { fireEvent, render, waitFor } from '@testing-library/svelte'
|
|
2
|
-
import { describe, expect, it, vi } from 'vitest'
|
|
2
|
+
import { describe, expect, it, vi, beforeEach } from 'vitest'
|
|
3
3
|
|
|
4
4
|
import ParticipantsRail from '../../../../routes/components/Rails/ParticipantsRail.svelte'
|
|
5
5
|
import { openModal } from '$lib/modal'
|
|
@@ -21,10 +21,13 @@ vi.mock('$lib/api/participants', () => ({
|
|
|
21
21
|
}))
|
|
22
22
|
|
|
23
23
|
describe('ParticipantsRail.svelte', () => {
|
|
24
|
+
beforeEach(() => {
|
|
25
|
+
document.body.innerHTML = ''
|
|
26
|
+
})
|
|
27
|
+
|
|
24
28
|
it('Should render each given participant', async () => {
|
|
25
29
|
vi.mocked(fetchParticipantsForTitle).mockResolvedValueOnce(participants)
|
|
26
30
|
|
|
27
|
-
// @ts-ignore
|
|
28
31
|
const { getByText } = render(ParticipantsRail, { title })
|
|
29
32
|
|
|
30
33
|
await waitFor(() => {
|
|
@@ -33,6 +36,19 @@ describe('ParticipantsRail.svelte', () => {
|
|
|
33
36
|
})
|
|
34
37
|
})
|
|
35
38
|
|
|
39
|
+
it('Should order participants by their number of occurrences on the page', async () => {
|
|
40
|
+
document.body.innerHTML = `<main>${participants[1].name}. ${participants[2].name}, ${participants[2].name}</main>`
|
|
41
|
+
vi.mocked(fetchParticipantsForTitle).mockResolvedValueOnce(participants)
|
|
42
|
+
|
|
43
|
+
const { getAllByTestId } = render(ParticipantsRail, { title })
|
|
44
|
+
|
|
45
|
+
await waitFor(() => {
|
|
46
|
+
expect(getAllByTestId('participant')[0].innerText).toContain(participants[2].name)
|
|
47
|
+
expect(getAllByTestId('participant')[1].innerText).toContain(participants[1].name)
|
|
48
|
+
expect(getAllByTestId('participant')[2].innerText).toContain(participants[0].name)
|
|
49
|
+
})
|
|
50
|
+
})
|
|
51
|
+
|
|
36
52
|
it('Should not render when no participants are returned', async () => {
|
|
37
53
|
vi.mocked(fetchParticipantsForTitle).mockResolvedValueOnce([])
|
|
38
54
|
|