Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(setting) - set contributor/transcript module on if needed #44

Merged
merged 1 commit into from
Aug 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 0 additions & 4 deletions client/src/sagas/helpers/podcast.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
Expand Down
24 changes: 24 additions & 0 deletions client/src/sagas/helpers/settings.ts
Original file line number Diff line number Diff line change
@@ -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 });
}
14 changes: 11 additions & 3 deletions client/src/sagas/import.saga.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<validateFeedUrlPayload>) {
const feed = payload.trim();
Expand Down Expand Up @@ -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)
}
}))
)
);
Expand All @@ -107,9 +115,9 @@ function* fetchEpisodes(): any {
}

function* importPodcast() {
yield setOnboardingPodcastSettings();
yield savePodcastMetadata();
yield fetchEpisodes();
yield setOnboardingPodcastSettings();
yield put(actions.onboarding.next());
}

Expand Down
4 changes: 2 additions & 2 deletions client/src/sagas/podcast.saga.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string> {
return new Promise((resolve) => {
Expand Down
4 changes: 3 additions & 1 deletion server/lib/publisher/feed_parser.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
8 changes: 4 additions & 4 deletions server/lib/publisher/wordpress/episode.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -326,18 +326,18 @@ 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
:error -> :error
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
Expand Down
22 changes: 20 additions & 2 deletions server/lib/publisher/wordpress/media.ex
Original file line number Diff line number Diff line change
@@ -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}
Expand Down Expand Up @@ -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",
Expand Down
17 changes: 15 additions & 2 deletions server/lib/publisher/wordpress/podcast.ex
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,31 @@ 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
error -> error
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")

Expand Down Expand Up @@ -139,4 +151,5 @@ defmodule Publisher.WordPress.Podcast do
result -> result |> Map.values() |> hd()
end
end

end
Loading