Fix #9: Add WebSub support

This commit is contained in:
Max Nuding 2023-04-14 20:04:46 +02:00
parent c57828d3e2
commit 5ab1167d38
Signed by: phlaym
GPG Key ID: A06651BAB6777237
3 changed files with 20 additions and 1 deletions

View File

@ -6,6 +6,7 @@ MASTODON_INSTANCE = 'metalhead.club'
BASE_URL = 'https://moshingmammut.phlaym.net'
VERBOSE = false
IGNORE_USERS = @moshhead@metalhead.club
WEBSUB_HUB = 'http://pubsubhubbub.superfeedr.com'
PUBLIC_REFRESH_INTERVAL = 10000
PUBLIC_MASTODON_INSTANCE_DISPLAY_NAME = 'Metalhead.club'

View File

@ -15,6 +15,7 @@
<meta name="theme-color" content="#17063b" media="(prefers-color-scheme: dark)" />
<meta name="theme-color" content="#BCB9B2" media="(prefers-color-scheme: light)" />
<link rel="alternate" type="application/atom+xml" href="/feed.xml" title="Atom Feed" />
<link rel="hub" href="https://pubsubhubbub.superfeedr.com" />
%sveltekit.head%
<style>
body {

View File

@ -1,4 +1,4 @@
import { BASE_URL } from '$env/static/private';
import { BASE_URL, WEBSUB_HUB } from '$env/static/private';
import { PUBLIC_MASTODON_INSTANCE_DISPLAY_NAME } from '$env/static/public';
import type { Post } from '$lib//mastodon/response';
import { Feed } from 'feed';
@ -6,6 +6,7 @@ import fs from 'fs/promises';
export function createFeed(posts: Post[]): Feed {
const baseUrl = BASE_URL.endsWith('/') ? BASE_URL : BASE_URL + '/';
const hub = WEBSUB_HUB ? WEBSUB_HUB : undefined;
const feed = new Feed({
title: `${PUBLIC_MASTODON_INSTANCE_DISPLAY_NAME} music feed`,
description: `Posts about music on ${PUBLIC_MASTODON_INSTANCE_DISPLAY_NAME}`,
@ -19,6 +20,7 @@ export function createFeed(posts: Post[]): Feed {
feedLinks: {
atom: `${BASE_URL}/feed.xml`
},
hub: hub,
author: {
name: '@aymm',
link: 'https://metalhead.club/@aymm'
@ -40,8 +42,23 @@ export function createFeed(posts: Post[]): Feed {
});
});
feed.addCategory('Music');
return feed;
}
export async function saveAtomFeed(feed: Feed) {
await fs.writeFile('feed.xml', feed.atom1(), { encoding: 'utf8' });
if (!WEBSUB_HUB) {
return;
}
try {
const params = new URLSearchParams();
params.append('hub.mode', 'publish');
params.append('hub.url', `${BASE_URL}/feed.xml`);
await fetch(WEBSUB_HUB, {
method: 'POST',
body: params
});
} catch (e) {
console.error('Failed to update WebSub hub', e);
}
}