From 4c719e4b13e0ca3b970b322a67dfe2c38472645a Mon Sep 17 00:00:00 2001 From: Dirk Schumann Date: Fri, 16 Aug 2024 15:35:59 +0200 Subject: [PATCH] feat(setting) - set contributor/transcript module on if needed --- client/src/sagas/helpers/podcast.ts | 4 ---- client/src/sagas/helpers/settings.ts | 24 +++++++++++++++++++++++ client/src/sagas/import.saga.ts | 14 ++++++++++--- client/src/sagas/podcast.saga.ts | 4 ++-- server/lib/publisher/feed_parser.ex | 4 +++- server/lib/publisher/wordpress/episode.ex | 8 ++++---- server/lib/publisher/wordpress/media.ex | 22 +++++++++++++++++++-- server/lib/publisher/wordpress/podcast.ex | 17 ++++++++++++++-- 8 files changed, 79 insertions(+), 18 deletions(-) create mode 100644 client/src/sagas/helpers/settings.ts diff --git a/client/src/sagas/helpers/podcast.ts b/client/src/sagas/helpers/podcast.ts index cbdc369..23d28e5 100644 --- a/client/src/sagas/helpers/podcast.ts +++ b/client/src/sagas/helpers/podcast.ts @@ -5,10 +5,6 @@ import { type locales } from '../../types/locales.types'; import { type category } from '../../types/categories.types'; import { extractImageType } from '../../lib/image'; -export function* setOnboardingPodcastSettings() { - yield request.post(request.origin('/api/v1/set_podcast_settings'), { params: {}, data: {} }); -} - function transferPodcastCoverFromData(imageData: string, imageName: string) { const parts: string[] = imageData.split(','); const imageType: string | null = extractImageType(parts[0]); diff --git a/client/src/sagas/helpers/settings.ts b/client/src/sagas/helpers/settings.ts new file mode 100644 index 0000000..0b62b48 --- /dev/null +++ b/client/src/sagas/helpers/settings.ts @@ -0,0 +1,24 @@ +import { select } from 'redux-saga/effects'; +import * as request from '../../lib/request'; +import { selectors } from '../../store'; +import { type State as EpisodeState } from '../../store/episode.store'; + +export function* setOnboardingPodcastSettings() { + const episodes : EpisodeState = yield select(selectors.episodes.list); + if (!episodes || episodes.length === 0) { + yield request.post(request.origin('/api/v1/set_podcast_settings'), { params: {}, data: {} }); + } + + const hasContributor = episodes.some( + (episode) => episode.data.contributors && episode.data.contributors.length > 0 + ); + const hasTranscript = episodes.some( + (episode) => episode.data.transcript && episode.data.transcript.url && episode.data.transcript.url.length > 0 + ); + + const module = { + transcript: hasTranscript, + contributor: hasContributor + }; + yield request.post(request.origin('/api/v1/set_podcast_settings'), { params: {}, data: module }); +} diff --git a/client/src/sagas/import.saga.ts b/client/src/sagas/import.saga.ts index 94b2757..a1e78b6 100644 --- a/client/src/sagas/import.saga.ts +++ b/client/src/sagas/import.saga.ts @@ -8,7 +8,8 @@ import * as request from '../lib/request'; import { findCategories } from '../helper/categories'; import { LanguageLocales } from '../types/locales.types'; import { type Episode, type EpisodeDetailsPayload } from '../types/episode.types'; -import { setOnboardingPodcastSettings, savePodcastMetadata } from './helpers/podcast'; +import { savePodcastMetadata } from './helpers/podcast'; +import { setOnboardingPodcastSettings } from './helpers/settings'; function* validateFeedUrl({ payload }: Action) { const feed = payload.trim(); @@ -94,7 +95,14 @@ function* fetchEpisodes(): any { url: get(episode, ['enclosure', 'url'], null), type: get(episode, ['enclosure', 'type'], null) }, - cover: get(episode, 'cover', null) + cover: get(episode, 'cover', null), + contributors: get(episode, 'contributors', []), + transcript: { + language: get(episode, ['transcript', 'language'], null), + rel: get(episode, ['transcript', 'rel'], null), + type: get(episode, ['transcript', 'type'], null), + url: get(episode, ['transcript', 'url'], null) + } })) ) ); @@ -107,9 +115,9 @@ function* fetchEpisodes(): any { } function* importPodcast() { - yield setOnboardingPodcastSettings(); yield savePodcastMetadata(); yield fetchEpisodes(); + yield setOnboardingPodcastSettings(); yield put(actions.onboarding.next()); } diff --git a/client/src/sagas/podcast.saga.ts b/client/src/sagas/podcast.saga.ts index 98ae9b3..80ba2ac 100644 --- a/client/src/sagas/podcast.saga.ts +++ b/client/src/sagas/podcast.saga.ts @@ -5,8 +5,8 @@ import { convertImageToBase64 } from '../lib/image'; import { actions } from '../store'; import * as request from '../lib/request'; import { type setPodcastCoverPayload } from '../store/podcast.store'; -import { setOnboardingPodcastSettings, savePodcastMetadata } from './helpers/podcast'; - +import { savePodcastMetadata } from './helpers/podcast'; +import { setOnboardingPodcastSettings } from './helpers/settings'; function readImage(file: File): Promise { return new Promise((resolve) => { diff --git a/server/lib/publisher/feed_parser.ex b/server/lib/publisher/feed_parser.ex index e3aa266..13f40b0 100644 --- a/server/lib/publisher/feed_parser.ex +++ b/server/lib/publisher/feed_parser.ex @@ -29,7 +29,9 @@ defmodule Publisher.FeedParser do guid: episode.guid, pub_date: episode.pub_date, enclosure: sanitize_enclosure(episode.enclosure), - cover: episode.image_url + cover: episode.image_url, + transcript: transcript(episode), + contributors: episode.contributors } end) diff --git a/server/lib/publisher/wordpress/episode.ex b/server/lib/publisher/wordpress/episode.ex index 6c8a995..404360a 100644 --- a/server/lib/publisher/wordpress/episode.ex +++ b/server/lib/publisher/wordpress/episode.ex @@ -16,7 +16,7 @@ defmodule Publisher.WordPress.Episode do :ok <- upload_transcript(req, episode_id, params), :ok <- upload_contributors(req, episode_id, params), :ok <- verify_media(req, episode_id), - :ok <- upload_cover(req, episode_id, params) do + :ok <- upload_cover(req, episode_id, post_id, params) do :ok else error -> error @@ -326,10 +326,10 @@ defmodule Publisher.WordPress.Episode do :ok end - defp upload_cover(req, episode_id, %{"cover" => cover} = params) + defp upload_cover(req, episode_id, post_id, %{"cover" => cover} = params) when not is_nil(cover) do image_name = "cover-" <> params["slug"] - with {:ok, source_url} <- Media.upload_media_from_url(req, cover, image_name), + with {:ok, source_url} <- Media.upload_media_from_url(req, post_id, cover, image_name), :ok <- save_episode_image_url(req, episode_id, source_url) do :ok else @@ -337,7 +337,7 @@ defmodule Publisher.WordPress.Episode do end end - defp upload_cover(_req, episode_id, _params) do + defp upload_cover(_req, episode_id, _post_id, _params) do Logger.info("Episode has no epiosde cover: #{episode_id}") :ok end diff --git a/server/lib/publisher/wordpress/media.ex b/server/lib/publisher/wordpress/media.ex index 4062315..b66e33e 100644 --- a/server/lib/publisher/wordpress/media.ex +++ b/server/lib/publisher/wordpress/media.ex @@ -1,13 +1,19 @@ defmodule Publisher.WordPress.Media do require Logger - def upload_media_from_url(req, url, slug) do + def upload_media_from_url(req, post_id \\ nil, url, slug) do ext = extension_from_url(url) filename = [slug, ext] |> Enum.join(".") {:ok, resp} = Req.get(url) - with {:ok, response} <- upload_media(req, filename, content_type(resp), resp.body), + response = if post_id do + upload_media(req, post_id, filename, content_type(resp), resp.body) + else + upload_media(req, filename, content_type(resp), resp.body) + end + + with {:ok, response} <- response, {:ok, _} <- extract_status(response), {:ok, source_url} <- extract_source_url(response) do {:ok, source_url} @@ -65,6 +71,18 @@ defmodule Publisher.WordPress.Media do end end + defp upload_media(req, post_id, content_name, content_type, content) do + Req.post(req, + url: "wp/v2/media", + params: [post: post_id], + headers: [ + {"Content-Type", content_type}, + {"Content-Disposition", "attachment; filename=\"" <> content_name <> "\""} + ], + body: content + ) + end + defp upload_media(req, content_name, content_type, content) do Req.post(req, url: "wp/v2/media", diff --git a/server/lib/publisher/wordpress/podcast.ex b/server/lib/publisher/wordpress/podcast.ex index 04e5ede..35a185f 100644 --- a/server/lib/publisher/wordpress/podcast.ex +++ b/server/lib/publisher/wordpress/podcast.ex @@ -21,12 +21,20 @@ defmodule Publisher.WordPress.Podcast do end end - def set_settings(headers, _body) do + def set_settings(headers, body) do Logger.info("Padcast.set_settings") + payload = + %{ + transcript: Map.get(body, "transcript", nil), + contributor: Map.get(body, "contributor", nil) + } + |> reject_empty_values() + |> Enum.into(%{}) + req = API.new(headers) - with {:ok, response} <- Req.post(req, url: "podlove/v2/onboarding/setup"), + with {:ok, response} <- Req.post(req, url: "podlove/v2/onboarding/setup", json: payload), {:ok, _} <- extract_status(response) do :ok else @@ -34,6 +42,10 @@ defmodule Publisher.WordPress.Podcast do end end + defp reject_empty_values(map) do + Enum.reject(map, fn {_, v} -> is_nil(v) end) + end + def save_podcast_data(headers, body) do Logger.info("Podcast.save_podcast_data") @@ -139,4 +151,5 @@ defmodule Publisher.WordPress.Podcast do result -> result |> Map.values() |> hd() end end + end