Skip to content

Commit

Permalink
implement update rss
Browse files Browse the repository at this point in the history
  • Loading branch information
Vova Stelmashchuk committed Aug 11, 2024
1 parent cb9d993 commit 40fbef6
Show file tree
Hide file tree
Showing 11 changed files with 133 additions and 26 deletions.
4 changes: 2 additions & 2 deletions src/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ const init = async () => {
engines: { html: Handlebars },
relativeTo: __dirname,
partialsPath: 'templates/partials',
path: 'templates/pages',
path: ['templates/pages', 'templates/partials'],
layout: true,
layoutPath: 'templates/layouts'
layoutPath: 'templates/layouts',
});

registerViewFunctions()
Expand Down
22 changes: 22 additions & 0 deletions src/core/episodeRepo.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,20 @@ export function getPublicPosts() {
).toArray();
}

export function getPodcastForRss() {
return Database.collection('posts').find(
{
'visibility': 'public',
'type': 'public'
},
{
sort: {
'publish_date': -1
}
}
).toArray();
}

export function getAllPosts() {
return Database.collection('posts').find(
{},
Expand Down Expand Up @@ -63,3 +77,11 @@ export function updateMontageStatusBySlug(slug, status) {
return Database.collection('posts').updateOne({ slug: slug }, { $set: { montage_status: status } });
}

export function publishPodcast(slug) {
return Database.collection('posts').updateOne({ slug: slug }, { $set: { visibility: 'public' } });
}

export function unpublishPodcast(slug) {
return Database.collection('posts').updateOne({ slug: slug }, { $set: { visibility: 'private' } });
}

21 changes: 14 additions & 7 deletions src/core/generator.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
import { Podcast } from 'podcast';

import { getPublicPosts } from './episodeRepo.js';
import { getPodcastForRss } from './episodeRepo.js';
import { buildObjectURL, getFileSizeInByte, uploadFile } from '../minio/utils.js';

import dotenv from 'dotenv';

dotenv.config();

const host = process.env.BASE_URL;

export async function updateRss() {
const podcasts = getPublicPosts();
const host = config.host;
const podcasts = await getPodcastForRss();
const logoUrl = buildObjectURL('logo.jpg')

const description = 'Два андроїдщики, два Вови і деколи дві різні думки. Кожний подкаст ми обговорюємо нові релізи в світі android розробки, кращі і не дуже практики. Ділимося своїми думками, досвідом і деколи пробуємо не смішно жартувати. Також тут ви знайдете рекомендації початківцям, а хто давно в розробці мають тут просто гарно провести час. Якщо вам тут сподобалося то заходьте в наш telegram chat https://t.me/androidstory_chat Якщо прям сильно сподобалося закиньте там трішки грошей. https://www.patreon.com/androidstory'
Expand Down Expand Up @@ -57,9 +62,9 @@ export async function updateRss() {
podcasts.forEach((post, index) => {
let description = buildRssDescription(post);

let linkToEpisode = `${config.host}/podcast/${post.slug}`;
let linkToEpisode = `${host}/podcast/${post.slug}`;

let guid = post.id.toString();
let guid = post._id.toString();

let date = post.publish_date.toISOString();

Expand Down Expand Up @@ -87,7 +92,9 @@ export async function updateRss() {
});
});

await uploadFile('rss_1.xml', feed.buildXml());
const xml = feed.buildXml();

await uploadFile('rss.xml', xml);
}

export function buildPublicChapters(chapters) {
Expand Down Expand Up @@ -122,7 +129,7 @@ function buildRssDescription(post) {
const publicChapters = buildPublicChapters(post.charters);
description += '<ul>'
publicChapters
.filter(chapter => chapter.isPublic)
.filter(chapter => chapter.isPublic !== false)
.forEach(chapter => {
description += `<li>${chapter.time} - <em>${chapter.description}</em></li>`;
});
Expand Down
2 changes: 2 additions & 0 deletions src/routers/admin/detail/getDetails.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ async function podcastDetailsHandler(request, h) {
}
}),
isAudioBuildInProgress: podcast.montage_status === 'in_progress',
publish_button_text: podcast.visibility === 'private' ? 'Publish' : 'Unpublish',
url: podcast.visibility === 'private' ? `/admin/podcast/${slug}/publish` : `/admin/podcast/${slug}/unpublish`,
},
{ layout: 'admin' }
)
Expand Down
58 changes: 58 additions & 0 deletions src/routers/admin/detail/publish.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import { publishPodcast, unpublishPodcast } from "../../../core/episodeRepo.js";
import { updateRss } from "../../../core/generator.js";

async function publishHandler(request, h) {
const slug = request.params.slug;
await publishPodcast(slug)

await updateRss();

return h.view(
'buttons/publish_button',
{
publish_button_text: 'Unpublish',
url: `/admin/podcast/${slug}/unpublish`,
},
{
layout: false
}
)
}

async function unpublishHandler(request, h) {
const slug = request.params.slug;
await unpublishPodcast(slug)

await updateRss();

return h.view(
'buttons/publish_button',
{
publish_button_text: 'Publish',
url: `/admin/podcast/${slug}/publish`,
},
{
layout: false
}
)
}

export function publishController(server) {
server.route({
method: 'PUT',
path: '/admin/podcast/{slug}/publish',
handler: publishHandler,
options: {
auth: 'adminSession',
}
});

server.route({
method: 'PUT',
path: '/admin/podcast/{slug}/unpublish',
handler: unpublishHandler,
options: {
auth: 'adminSession',
}
});
}
12 changes: 11 additions & 1 deletion src/routers/admin/details.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { adminPodcastGetInfoController } from "./detail/getDetails.js";
import { editPodcastMetaInfo } from "./detail/edit_meta.js";
import { getPostBySlug } from "../../core/episodeRepo.js";
import { createPublicAudio } from "../../montage/publicAudioGenerator.js";
import { publishController } from "./detail/publish.js";

async function updatePodcastName(request, h) {
const slug = request.params.slug;
Expand All @@ -20,7 +21,15 @@ async function updateFiles(request, h) {

await createPublicAudio(podcast);

return h.response().code(200).header('HX-Trigger', 'update-progress');
return h.view(
'buttons/build_audio',
{
isAudioBuildInProgress: true,
},
{
layout: false
}
).header('HX-Trigger', 'update-progress');
}

async function getProgress(request, h) {
Expand All @@ -44,6 +53,7 @@ async function getProgress(request, h) {
export function editPodcastDetails(server) {
adminPodcastGetInfoController(server)
editPodcastMetaInfo(server)
publishController(server)

server.route({
method: 'PUT',
Expand Down
17 changes: 8 additions & 9 deletions src/routers/details.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,14 @@ async function podcastDetailsHandler(request, h) {
title: chapter.description,
timeInSeconds: chapter.timeInSeconds
}
}
),
links: podcast.links.map(link => {
return {
link: link.link,
title: link.title,
}
}
)
}),
links: podcast.links
.map(link => {
return {
link: link.link,
title: link.title,
}
}),
},
{
layout: layout
Expand Down
10 changes: 5 additions & 5 deletions src/templates/pages/admin/admin_podcast_detail.html
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,7 @@ <h2 class="text-2xl">Links</h2>
</button>
</div>

<button hx-swap="none" hx-post="/admin/podcast/{{slug}}/update-files"
class="common-button w-full px-4 py-2 my-2 transition-all duration-300 ease-in-out" {{#if
isAudioBuildInProgress}}disabled{{/if}}>
Build audio files
</button>
{{> buttons/build_audio }}

<h2 class="text-2xl">Preview episode</h2>
<div class="border-2 border-black p-8 mt-2">
Expand All @@ -55,4 +51,8 @@ <h2 class="text-2xl">Preview episode</h2>
<div hx-get="/admin/podcast/{{slug}}/youtube-description" hx-swap="innerHTML"
hx-trigger="update-preview from:body, load"></div>

<div>
{{> buttons/publish_button}}
</div>

</div>
4 changes: 2 additions & 2 deletions src/templates/pages/admin/dashboard.html
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
<div class="container mx-auto p-4">
<div hx-get="/admin/podcast" hx-swap="innerHTML" hx-trigger="load"></div>
</div>
<div hx-get="/admin/podcast" hx-swap="innerHTML" hx-trigger="load"></div>
</div>
5 changes: 5 additions & 0 deletions src/templates/partials/buttons/build_audio.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<button hx-swap="outerHTML" hx-post="/admin/podcast/{{slug}}/update-files"
class="common-button w-full px-4 py-2 my-2 transition-all duration-300 ease-in-out" {{#if
isAudioBuildInProgress}}disabled{{/if}}>
Build audio files
</button>
4 changes: 4 additions & 0 deletions src/templates/partials/buttons/publish_button.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<button hx-swap="outerHTML" hx-put="{{url}}" hx-confirm="Are you sure you want to publish this episode?"
class="common-button w-full px-4 py-2 my-2">
{{publish_button_text}}
</button>

0 comments on commit 40fbef6

Please sign in to comment.