From 9b098212a5ba4e2e03b5b9fe39ac8a643ddaa0b9 Mon Sep 17 00:00:00 2001 From: Andreas Hubel <andreas.hubel@pub.tech> Date: Thu, 13 Mar 2025 15:18:33 +0100 Subject: [PATCH] chore: cleanup & format --- src/server.ts | 209 ++++++++++++++++++++++---------------------------- 1 file changed, 90 insertions(+), 119 deletions(-) diff --git a/src/server.ts b/src/server.ts index 2f5a5c4..5a3b8b1 100644 --- a/src/server.ts +++ b/src/server.ts @@ -7,55 +7,13 @@ import { transformEpisode, transformEpisodeList } from './mappings'; const app = express(); const PORT = 3000; -const root = "https://social.audiothek.de/@blaue-couch" - +const root = 'https://social.audiothek.de/@blaue-couch'; const audiothekApi = new Client({ url: 'https://api.ardaudiothek.de/graphql', exchanges: [cacheExchange, fetchExchange], }); -const query = gql` - query Item { - item(id: "14278953") { - show { - coreId - externalId - title - url - publisher: publicationService { - coreId - dvbServiceId - title - url - organizationName - organization { - name - url - } - } - coreDocument - } - assetId - url: sharingUrl - title - description - published: publishDate - updated: core(key: "modified") - imagesList { - url - title - } - coreDocument - audioList { - title - audioCodec - href - } - } - } -`; - async function fetchData<R>(query: any, variables: any) { const result = await audiothekApi.query<R>(query, variables).toPromise(); if (result.error) { @@ -65,52 +23,71 @@ async function fetchData<R>(query: any, variables: any) { return result.data; } - - -async function main() { - const response = await fetchData<EpisodeResponse>(query, { id: '14278953' }); - - if (response) { - const activityPubEpisode = transformEpisode(response); - console.log(JSON.stringify(activityPubEpisode, null, 2)); - } -} - -// main(); - - app.get('/:actor/episodes/:id', async (req, res) => { - - try { - const data = await fetchData<EpisodeResponse>(query, { id: req.params.id }); - if (!data) { - res.status(404).json({ error: 'Episode not found' }); - return; + const query = gql` + query Item($id: String!) { + item(id: $id) { + show { + coreId + externalId + title + url + publisher: publicationService { + coreId + dvbServiceId + title + url + organizationName + organization { + name + url + } + } + } + assetId + url: sharingUrl + title + description + published: publishDate + updated: core(key: "modified") + imagesList { + url + title + } + #coreDocument + audioList { + title + audioCodec + href + } + } } + `; - // TODO: verifiy that the actor is the same as the attributedTo field - - const activityPubDocument = transformEpisode(data); - res.json(activityPubDocument); - } catch (error) { - res.status(500).json({ error: 'Failed to fetch episode data' }); + const data = await fetchData<EpisodeResponse>(query, { id: req.params.id }); + if (!data) { + res.status(404).json({ error: 'Episode not found' }); + return; } + + // TODO: verifiy that the actor is the same as the attributedTo field + + const activityPubDocument = transformEpisode(data); + res.json(activityPubDocument); }); app.get('/:actor/episodes', async (req, res) => { const offset = 10 * ((+req.query.page! || 1) - 1); const query = gql` - query ShowWithEpisodes($showId: ID!, $first: Int = 10, $offset: Int) { - show(id: $showId) { - coreId + query ShowWithEpisodes($showId: ID!, $first: Int = 10, $offset: Int) { + show(id: $showId) { + coreId externalId title url - items(first: $first, orderBy: PUBLISH_DATE_DESC, offset: $offset, condition: { - isPublished: true - }) { + items(first: $first, orderBy: PUBLISH_DATE_DESC, offset: $offset, condition: { isPublished: true }) { nodes { assetId title @@ -132,19 +109,15 @@ app.get('/:actor/episodes', async (req, res) => { } `; - try { - // TODO: verifiy that the actor is the same as the attributedTo field - - const data = await fetchData<any>(query, { showId: "urn:ard:show:a42d1ea0b4a07053", offset }); + const data = await fetchData<any>(query, { showId: 'urn:ard:show:a42d1ea0b4a07053', offset }); if (!data) { res.status(404).json({ error: 'Episode not found' }); return; } - const activityPubDocument = transformEpisodeList(data); res.json(activityPubDocument); } catch (error) { @@ -154,21 +127,21 @@ app.get('/:actor/episodes', async (req, res) => { app.get('/:actor/outbox', async (req, res) => { const query = gql` - query ShowEpisodeTotal($showId: ID!) { - show(id: $showId) { - coreId - externalId - title - url - - items(condition: { isPublished: true }) { - totalCount - } - } -}`; + query ShowEpisodeTotal($showId: ID!) { + show(id: $showId) { + coreId + externalId + title + url + items(condition: { isPublished: true }) { + totalCount + } + } + } + `; - const data = await fetchData<any>(query, { showId: "urn:ard:show:a42d1ea0b4a07053" }); + const data = await fetchData<any>(query, { showId: 'urn:ard:show:a42d1ea0b4a07053' }); if (!data) { res.status(404).json({ error: 'Show not found' }); return; @@ -177,37 +150,35 @@ app.get('/:actor/outbox', async (req, res) => { const { totalCount } = data.show.items; res.json({ - "@context": "https://www.w3.org/ns/activitystreams", - "id": `${root}/outbox`, - "type": "OrderedCollection", - "to": [ - "https://www.w3.org/ns/activitystreams#Public" - ], - "totalItems": totalCount, - "first": `${root}/episodes?page=1`, - "current": `${root}/outbox`, - "last": `${root}/episodes?page=${Math.floor(totalCount/10)}` + '@context': 'https://www.w3.org/ns/activitystreams', + id: `${root}/outbox`, + type: 'OrderedCollection', + to: ['https://www.w3.org/ns/activitystreams#Public'], + totalItems: totalCount, + first: `${root}/episodes?page=1`, + current: `${root}/outbox`, + last: `${root}/episodes?page=${Math.floor(totalCount / 10)}`, }); }); app.get('/:actor', async (req, res) => { res.json({ - "@context": "https://www.w3.org/ns/activitystreams", - "id": "urn:ard:show:a42d1ea0b4a07053", - "type": "Service", - "name": "Blaue Couch", - "externalId": "https://feeds.br.de/blaue-couch/feed.xml", - "attributedTo": "urn:ard:publisher:c4a9cee041835529", - "publisher": { - "type": "BroadcastService", - "name": "BAYERN 1", - "url": "https://www.ardaudiothek.de/radio/br/bayern-1/", - "organization": { - "type": "Organization", - "name": "BR", - "url": "https://www.ardaudiothek.de/radio/br/" - } - } + '@context': 'https://www.w3.org/ns/activitystreams', + id: 'urn:ard:show:a42d1ea0b4a07053', + type: 'Service', + name: 'Blaue Couch', + externalId: 'https://feeds.br.de/blaue-couch/feed.xml', + attributedTo: 'urn:ard:publisher:c4a9cee041835529', + publisher: { + type: 'BroadcastService', + name: 'BAYERN 1', + url: 'https://www.ardaudiothek.de/radio/br/bayern-1/', + organization: { + type: 'Organization', + name: 'BR', + url: 'https://www.ardaudiothek.de/radio/br/', + }, + }, }); }); -- GitLab