Skip to content

📈 Open source, privacy-first web analytics — a cookie-less Google Analytics alternative with built-in error tracking and performance monitoring. Self-host it or use our cloud. Get started for free!

feat: RankPine content publishing integration

Blue Mouse349639e63403
diff --git a/backend/.env.example b/backend/.env.example
index 7f4ac1736..f6f55a852 100644
--- a/backend/.env.example
+++ b/backend/.env.example
@@ -139,6 +139,19 @@ SWETRIX_PID=STEzHcB1rALV
SWETRIX_CDN_URL=https://cdn.swetrix.com
SWETRIX_CDN_TOKEN=
+# RankPine -> Swetrix blog publishing (Cloud edition only)
+RANKPINE_BLOG_WEBHOOK_SECRET=
+RANKPINE_BLOG_GITHUB_TOKEN=
+RANKPINE_BLOG_GITHUB_REPOSITORY=Swetrix/blog-posts
+RANKPINE_BLOG_GITHUB_BRANCH=main
+RANKPINE_BLOG_SITE_URL=https://swetrix.com
+RANKPINE_BLOG_LANGUAGE=en
+RANKPINE_BLOG_TIME_ZONE=Europe/London
+RANKPINE_BLOG_AUTHOR='Andrii Romasiun'
+RANKPINE_BLOG_TWITTER_HANDLE=andrii_rom
+RANKPINE_BLOG_COMMITTER_NAME='Swetrix Content Bot'
+
# Managed reverse proxy ("Bypass adblockers") edge auth
# Shared secret the proxy.swetrix.org edge box sends in `X-Edge-Api-Key` to
# call the /v1/proxy-domain/allow and /v1/proxy-domain/active endpoints.
diff --git a/backend/apps/cloud/src/main.ts b/backend/apps/cloud/src/main.ts
index d06d45286..16b9b9821 100644
--- a/backend/apps/cloud/src/main.ts
+++ b/backend/apps/cloud/src/main.ts
@@ -26,6 +26,10 @@ async function bootstrap() {
})
app.use(cookieParser())
+ app.use(
+ '/webhook/rankpine',
+ express.raw({ type: 'application/json', limit: DEFAULT_JSON_BODY_LIMIT }),
+ )
app.use(
SESSION_REPLAY_CHUNK_PATHS,
express.json({ limit: SESSION_REPLAY_CHUNK_BODY_LIMIT }),
@@ -80,8 +84,6 @@ async function bootstrap() {
next()
})
- app.use('/webhook', express.raw({ type: 'application/json' }))
-
app.use('/webhook/sns', express.raw())
app.use('/webhook/sns', express.text())
diff --git a/backend/apps/cloud/src/webhook/rankpine-blog.service.spec.ts b/backend/apps/cloud/src/webhook/rankpine-blog.service.spec.ts
new file mode 100644
index 000000000..5ed812259
--- /dev/null
+++ b/backend/apps/cloud/src/webhook/rankpine-blog.service.spec.ts
@@ -0,0 +1,227 @@
+import crypto from 'crypto'
+import { UnauthorizedException } from '@nestjs/common'
+
+import { AppLoggerService } from '../logger/logger.service'
+import { RankPineBlogService } from './rankpine-blog.service'
+
+const SECRET = 'rankpine-test-secret'
+const ENV_KEYS = [
+ 'RANKPINE_BLOG_WEBHOOK_SECRET',
+ 'RANKPINE_BLOG_GITHUB_TOKEN',
+ 'RANKPINE_BLOG_GITHUB_REPOSITORY',
+ 'RANKPINE_BLOG_GITHUB_BRANCH',
+ 'RANKPINE_BLOG_SITE_URL',
+ 'RANKPINE_BLOG_LANGUAGE',
+ 'RANKPINE_BLOG_TIME_ZONE',
+ 'RANKPINE_BLOG_AUTHOR',
+ 'RANKPINE_BLOG_TWITTER_HANDLE',
+ 'RANKPINE_BLOG_COMMITTER_NAME',
+ 'RANKPINE_BLOG_COMMITTER_EMAIL',
+] as const
+const ORIGINAL_ENV = Object.fromEntries(
+ ENV_KEYS.map((key) => [key, process.env[key]]),
+)
+
+function signature(body: string): string {
+ return `sha256=${crypto.createHmac('sha256', SECRET).update(body).digest('hex')}`
+}
+
+function response(body: unknown, status = 200): Response {
+ return new Response(JSON.stringify(body), {
+ status,
+ headers: { 'content-type': 'application/json' },
+ })
+}
+
+describe('RankPineBlogService', () => {
+ const logger = {
+ error: jest.fn(),
+ log: jest.fn(),
+ } as unknown as AppLoggerService
+ const service = new RankPineBlogService(logger)
+
+ beforeEach(() => {
+ process.env.RANKPINE_BLOG_WEBHOOK_SECRET = SECRET
+ process.env.RANKPINE_BLOG_GITHUB_TOKEN = 'github-test-token'
+ process.env.RANKPINE_BLOG_GITHUB_REPOSITORY = 'Swetrix/blog-posts'
+ process.env.RANKPINE_BLOG_GITHUB_BRANCH = 'main'
+ process.env.RANKPINE_BLOG_SITE_URL = 'https://swetrix.com'
+ process.env.RANKPINE_BLOG_LANGUAGE = 'en'
+ process.env.RANKPINE_BLOG_TIME_ZONE = 'Europe/London'
+ process.env.RANKPINE_BLOG_AUTHOR = 'Andrii Romasiun'
+ process.env.RANKPINE_BLOG_TWITTER_HANDLE = 'andrii_rom'
+ process.env.RANKPINE_BLOG_COMMITTER_NAME = 'Swetrix Content Bot'
+ process.env.RANKPINE_BLOG_COMMITTER_EMAIL = '[email protected]'
+ jest.clearAllMocks()
+ })
+
+ afterAll(() => {
+ for (const key of ENV_KEYS) {
+ const value = ORIGINAL_ENV[key]
+
+ if (value === undefined) {
+ delete process.env[key]
+ } else {
+ process.env[key] = value
+ }
+ }
+ })
+
+ it('accepts a signed verification ping without calling GitHub', async () => {
+ const fetchMock = jest.spyOn(global, 'fetch')
+ const body = JSON.stringify({
+ event: 'verification',
+ sentAt: '2026-08-17T08:00:00.000Z',
+ })
+
+ await expect(
+ service.handle(Buffer.from(body), signature(body), 'verification'),
+ ).resolves.toEqual({ ok: true, verified: true })
+ expect(fetchMock).not.toHaveBeenCalled()
+
+ fetchMock.mockRestore()
+ })
+
+ it('rejects a request whose HMAC does not match', async () => {
+ const body = JSON.stringify({ event: 'verification' })
+
+ await expect(
+ service.handle(Buffer.from(body), 'sha256=wrong', 'verification'),
+ ).rejects.toBeInstanceOf(UnauthorizedException)
+ })
+
+ it('creates one idempotent bot-authored post and strips the duplicate H1', async () => {
+ const files = new Map<string, string>([
+ [
+ 'posts/2026-08-01-older-automated-post.md',
+ 'This different slug must not be overwritten.',
+ ],
+ ])
+ const writes: Array<Record<string, unknown>> = []
+ const fetchMock = jest
+ .spyOn(global, 'fetch')
+ .mockImplementation(async (input, init) => {
+ const url = new URL(String(input))
+ const contentsMarker = '/contents/'
+
+ if (url.pathname.includes('/git/trees/')) {
+ return response({
+ truncated: false,
+ tree: [...files.keys()].map((path) => ({ path, type: 'blob' })),
+ })
+ }
+
+ if (!url.pathname.includes(contentsMarker)) {
+ return response({ message: 'Not Found' }, 404)
+ }
+
+ const path = decodeURIComponent(
+ url.pathname.slice(
+ url.pathname.indexOf(contentsMarker) + contentsMarker.length,
+ ),
+ )
+
+ if ((init?.method || 'GET') === 'GET') {
+ const content = files.get(path)
+
+ if (content === undefined) {
+ return response({ message: 'Not Found' }, 404)
+ }
+
+ return response({
+ path,
+ sha: 'existing-sha',
+ encoding: 'base64',
+ content: Buffer.from(content).toString('base64'),
+ html_url: `https://github.com/Swetrix/blog-posts/blob/main/${path}`,
+ })
+ }
+
+ const request = JSON.parse(String(init?.body)) as {
+ content: string
+ author: { name: string; email: string }
+ committer: { name: string; email: string }
+ }
+ files.set(path, Buffer.from(request.content, 'base64').toString('utf8'))
+ writes.push(request)
+
+ return response(
+ {
+ content: {
+ path,
+ sha: 'new-sha',
+ html_url: `https://github.com/Swetrix/blog-posts/blob/main/${path}`,
+ },
+ },
+ 201,
+ )
+ })
+
+ const payload = {
+ event: 'article.published',
+ publishedAt: '2026-08-17T08:01:00.000Z',
+ site: {
+ name: 'Swetrix',
+ url: 'https://swetrix.com/',
+ language: 'en',
+ },
+ article: {
+ id: 'article-123',
+ title: 'Automated Post',
+ slug: 'automated-post',
+ metaDescription: 'An automated article.',
+ scheduledFor: '2026-08-17T00:30:00+01:00',
+ language: 'en',
+ markdown: '# Automated Post\n\nFirst paragraph.\n\n## Details\n\nBody.',
+ },
+ }
+ const body = JSON.stringify(payload)
+
+ const first = await service.handle(
+ Buffer.from(body),
+ signature(body),
+ 'article.published',
+ )
+ const second = await service.handle(
+ Buffer.from(body),
+ signature(body),
+ 'article.published',
+ )
+
+ expect(first).toEqual({
+ ok: true,
+ id: 'posts/2026-08-17-automated-post.md',
+ url: 'https://swetrix.com/blog/automated-post',
+ changed: true,
+ })
+ expect(second).toEqual({ ...first, changed: false })
+ expect(writes).toHaveLength(1)
+ expect(writes[0].author).toEqual({
+ name: 'Swetrix Content Bot',
+ email: '[email protected]',
+ })
+ expect(writes[0].committer).toEqual(writes[0].author)
+ expect(files.get('posts/2026-08-17-automated-post.md')).toBe(
+ [
+ '---',
+ 'title: "Automated Post"',
+ 'intro: "An automated article."',
+ 'date: August 17, 2026',
+ 'hidden: false',
+ 'author: "Andrii Romasiun"',
+ 'twitter_handle: "andrii_rom"',
+ 'rankpine_id: "article-123"',
+ '---',
+ '',
+ 'First paragraph.',
+ '',
+ '## Details',
+ '',
+ 'Body.',
+ '',
+ ].join('\n'),
+ )
+
+ fetchMock.mockRestore()
+ })
+})
diff --git a/backend/apps/cloud/src/webhook/rankpine-blog.service.ts b/backend/apps/cloud/src/webhook/rankpine-blog.service.ts
new file mode 100644
index 000000000..739808ba0
--- /dev/null
+++ b/backend/apps/cloud/src/webhook/rankpine-blog.service.ts
@@ -0,0 +1,550 @@
+import crypto from 'crypto'
+import {
+ BadGatewayException,
+ BadRequestException,
+ Injectable,
+ ServiceUnavailableException,
+ UnauthorizedException,
+} from '@nestjs/common'
+import { z } from 'zod'
+
+import { AppLoggerService } from '../logger/logger.service'
+
+const GITHUB_API_URL = 'https://api.github.com'
+const GITHUB_API_VERSION = '2026-03-10'
+const DEFAULT_REPOSITORY = 'Swetrix/blog-posts'
+const DEFAULT_BRANCH = 'main'
+const DEFAULT_SITE_URL = 'https://swetrix.com'
+const DEFAULT_LANGUAGE = 'en'
+const DEFAULT_TIME_ZONE = 'Europe/London'
+const DEFAULT_AUTHOR = 'Andrii Romasiun'
+const DEFAULT_TWITTER_HANDLE = 'andrii_rom'
+const DEFAULT_COMMITTER_NAME = 'Swetrix Content Bot'
+const DEFAULT_COMMITTER_EMAIL = '[email protected]'
+
+const verificationSchema = z.object({
+ event: z.literal('verification'),
+})
+
+const publishedArticleSchema = z.object({
+ event: z.literal('article.published'),
+ publishedAt: z.string().min(1),
+ site: z.object({
+ name: z.string().min(1),
+ url: z.string().min(1),
+ language: z.string().min(1),
+ }),
+ article: z.object({
+ id: z.union([z.string(), z.number()]).nullable().optional(),
+ title: z.string().trim().min(1).max(300),
+ slug: z.string().trim().min(1).max(200),
+ metaDescription: z.string().trim().max(500).optional(),
+ scheduledFor: z.string().nullable().optional(),
+ language: z.string().min(1),
+ markdown: z.string().min(1).max(900_000),
+ }),
+})
+
+interface PublisherConfig {
+ secret: string
+ githubToken: string
+ owner: string
+ repository: string
+ branch: string
+ siteUrl: string
+ language: string
+ timeZone: string
+ author: string
+ twitterHandle: string
+ committer: {
+ name: string
+ email: string
+ }
+}
+
+interface GitHubContent {
+ path: string
+ sha: string
+ content?: string
+ encoding?: string
+ html_url?: string
+}
+
+interface GitHubTree {
+ truncated: boolean
+ tree: Array<{
+ path: string
+ type: string
+ }>
+}
+
+interface GitHubWriteResponse {
+ content: {
+ path: string
+ sha: string
+ html_url: string
+ }
+}
+
+class GitHubApiError extends Error {
+ constructor(
+ public readonly status: number,
+ message: string,
+ ) {
+ super(message)
+ }
+}
+
+function safeEqual(left: string, right: string): boolean {
+ const leftBuffer = Buffer.from(left)
+ const rightBuffer = Buffer.from(right)
+
+ return (
+ leftBuffer.length === rightBuffer.length &&
+ crypto.timingSafeEqual(leftBuffer, rightBuffer)
+ )
+}
+
+function slugify(input: string): string {
+ const slug = input
+ .toLowerCase()
+ .normalize('NFKD')
+ .replace(/[\u0300-\u036f]/g, '')
+ .replace(/[^a-z0-9]+/g, '-')
+ .replace(/^-+|-+$/g, '')
+ .slice(0, 120)
+ .replace(/-+$/g, '')
+
+ if (!slug) {
+ throw new BadRequestException('Article slug is invalid')
+ }
+
+ return slug
+}
+
+function encodeGitHubPath(path: string): string {
+ return path.split('/').map(encodeURIComponent).join('/')
+}
+
+function getDateParts(date: Date, timeZone: string) {
+ const parts = new Intl.DateTimeFormat('en-GB', {
+ year: 'numeric',
+ month: '2-digit',
+ day: '2-digit',
+ timeZone,
+ }).formatToParts(date)
+ const byType = Object.fromEntries(
+ parts.map((part) => [part.type, part.value]),
+ )
+
+ return {
+ year: byType.year,
+ month: byType.month,
+ day: byType.day,
+ }
+}
+
+function formatFileDate(date: Date, timeZone: string): string {
+ const { year, month, day } = getDateParts(date, timeZone)
+ return `${year}-${month}-${day}`
+}
+
+function formatPostDate(date: Date, timeZone: string): string {
+ return new Intl.DateTimeFormat('en-US', {
+ year: 'numeric',
+ month: 'long',
+ day: 'numeric',
+ timeZone,
+ }).format(date)
+}
+
+function stripLeadingTitle(markdown: string): string {
+ return markdown.trim().replace(/^#(?!#)\s+[^\r\n]+(?:\r?\n)+/, '')
+}
+
+function quoteYaml(value: string): string {
+ return JSON.stringify(value)
+}
+
+function parseRepository(value: string): { owner: string; repository: string } {
+ const [owner, repository, ...rest] = value.split('/')
+
+ if (
+ rest.length > 0 ||
+ !owner ||
+ !repository ||
+ !/^[a-z0-9_.-]+$/i.test(owner) ||
+ !/^[a-z0-9_.-]+$/i.test(repository)
+ ) {
+ throw new Error('RANKPINE_BLOG_GITHUB_REPOSITORY must use owner/repository')
+ }
+
+ return { owner, repository }
+}
+
+function validDate(value: string | null | undefined): Date | null {
+ if (!value) {
+ return null
+ }
+
+ const parsed = new Date(value)
+ return Number.isNaN(parsed.getTime()) ? null : parsed
+}
+
+function articleMarkdown(
+ article: z.infer<typeof publishedArticleSchema>['article'],
+ publishedDate: Date,
+ config: PublisherConfig,
+): string {
+ const body = stripLeadingTitle(article.markdown)
+
+ if (!body) {
+ throw new BadRequestException('Article markdown has no body')
+ }
+
+ const intro = article.metaDescription || article.title
+ const rankPineId =
+ article.id == null ? [] : [`rankpine_id: ${quoteYaml(String(article.id))}`]
+ const twitterHandle = config.twitterHandle
+ ? [`twitter_handle: ${quoteYaml(config.twitterHandle)}`]
+ : []
+
+ return [
+ '---',
+ `title: ${quoteYaml(article.title)}`,
+ `intro: ${quoteYaml(intro)}`,
+ `date: ${formatPostDate(publishedDate, config.timeZone)}`,
+ 'hidden: false',
+ `author: ${quoteYaml(config.author)}`,
+ ...twitterHandle,
+ ...rankPineId,
+ '---',
+ '',
+ body,
+ '',
+ ].join('\n')
+}
+
+@Injectable()
+export class RankPineBlogService {
+ constructor(private readonly logger: AppLoggerService) {}
+
+ private getConfig(): PublisherConfig {
+ const secret = process.env.RANKPINE_BLOG_WEBHOOK_SECRET?.trim()
+ const githubToken = process.env.RANKPINE_BLOG_GITHUB_TOKEN?.trim()
+
+ if (!secret || !githubToken) {
+ this.logger.error(
+ 'RankPine blog publishing is disabled because its secret or GitHub token is missing',
+ )
+ throw new ServiceUnavailableException(
+ 'RankPine blog publishing is not configured',
+ )
+ }
+
+ const { owner, repository } = parseRepository(
+ process.env.RANKPINE_BLOG_GITHUB_REPOSITORY || DEFAULT_REPOSITORY,
+ )
+
+ return {
+ secret,
+ githubToken,
+ owner,
+ repository,
+ branch: process.env.RANKPINE_BLOG_GITHUB_BRANCH || DEFAULT_BRANCH,
+ siteUrl: process.env.RANKPINE_BLOG_SITE_URL || DEFAULT_SITE_URL,
+ language: process.env.RANKPINE_BLOG_LANGUAGE || DEFAULT_LANGUAGE,
+ timeZone: process.env.RANKPINE_BLOG_TIME_ZONE || DEFAULT_TIME_ZONE,
+ author: process.env.RANKPINE_BLOG_AUTHOR || DEFAULT_AUTHOR,
+ twitterHandle:
+ process.env.RANKPINE_BLOG_TWITTER_HANDLE ?? DEFAULT_TWITTER_HANDLE,
+ committer: {
+ name:
+ process.env.RANKPINE_BLOG_COMMITTER_NAME || DEFAULT_COMMITTER_NAME,
+ email:
+ process.env.RANKPINE_BLOG_COMMITTER_EMAIL || DEFAULT_COMMITTER_EMAIL,
+ },
+ }
+ }
+
+ private verifySignature(
+ rawBody: string,
+ signature: string | undefined,
+ secret: string,
+ ): void {
+ const expected = `sha256=${crypto
+ .createHmac('sha256', secret)
+ .update(rawBody, 'utf8')
+ .digest('hex')}`
+
+ if (!signature || !safeEqual(signature, expected)) {
+ throw new UnauthorizedException('Invalid RankPine webhook signature')
+ }
+ }
+
+ private async githubRequest<T>(
+ path: string,
+ config: PublisherConfig,
+ init: RequestInit = {},
+ ): Promise<T> {
+ let response: Response
+
+ try {
+ response = await fetch(`${GITHUB_API_URL}${path}`, {
+ ...init,
+ signal: AbortSignal.timeout(10_000),
+ headers: {
+ Accept: 'application/vnd.github+json',
+ Authorization: `Bearer ${config.githubToken}`,
+ 'Content-Type': 'application/json',
+ 'X-GitHub-Api-Version': GITHUB_API_VERSION,
+ ...init.headers,
+ },
+ })
+ } catch (error) {
+ const reason = error instanceof Error ? error.message : String(error)
+ throw new GitHubApiError(502, `GitHub request failed: ${reason}`)
+ }
+
+ if (!response.ok) {
+ const payload = (await response.json().catch(() => null)) as {
+ message?: string
+ } | null
+ throw new GitHubApiError(
+ response.status,
+ payload?.message || `GitHub returned ${response.status}`,
+ )
+ }
+
+ return (await response.json()) as T
+ }
+
+ private async getContent(
+ path: string,
+ config: PublisherConfig,
+ ): Promise<GitHubContent | null> {
+ try {
+ return await this.githubRequest<GitHubContent>(
+ `/repos/${encodeURIComponent(config.owner)}/${encodeURIComponent(
+ config.repository,
+ )}/contents/${encodeGitHubPath(path)}?ref=${encodeURIComponent(
+ config.branch,
+ )}`,
+ config,
+ )
+ } catch (error) {
+ if (error instanceof GitHubApiError && error.status === 404) {
+ return null
+ }
+
+ throw error
+ }
+ }
+
+ private async findExistingPath(
+ slug: string,
+ desiredPath: string,
+ config: PublisherConfig,
+ ): Promise<string> {
+ if (await this.getContent(desiredPath, config)) {
+ return desiredPath
+ }
+
+ const tree = await this.githubRequest<GitHubTree>(
+ `/repos/${encodeURIComponent(config.owner)}/${encodeURIComponent(
+ config.repository,
+ )}/git/trees/${encodeURIComponent(config.branch)}?recursive=1`,
+ config,
+ )
+ const pathPattern = new RegExp(`^posts/\\d{4}-\\d{2}-\\d{2}-${slug}\\.md$`)
+ const existing = tree.tree.find(
+ (entry) => entry.type === 'blob' && pathPattern.test(entry.path),
+ )
+
+ return existing?.path || desiredPath
+ }
+
+ private async writeContent(
+ path: string,
+ content: string,
+ slug: string,
+ config: PublisherConfig,
+ ): Promise<{ changed: boolean; htmlUrl: string }> {
+ const existing = await this.getContent(path, config)
+
+ if (existing?.encoding === 'base64' && existing.content) {
+ const current = Buffer.from(
+ existing.content.replace(/\s/g, ''),
+ 'base64',
+ ).toString('utf8')
+
+ if (current === content) {
+ return {
+ changed: false,
+ htmlUrl:
+ existing.html_url ||
+ `https://github.com/${config.owner}/${config.repository}/blob/${config.branch}/${path}`,
+ }
+ }
+ }
+
+ const body = {
+ message: existing
+ ? `Update blog post: ${slug}`
+ : `Publish blog post: ${slug}`,
+ content: Buffer.from(content, 'utf8').toString('base64'),
+ branch: config.branch,
+ author: config.committer,
+ committer: config.committer,
+ ...(existing ? { sha: existing.sha } : {}),
+ }
+
+ const result = await this.githubRequest<GitHubWriteResponse>(
+ `/repos/${encodeURIComponent(config.owner)}/${encodeURIComponent(
+ config.repository,
+ )}/contents/${encodeGitHubPath(path)}`,
+ config,
+ {
+ method: 'PUT',
+ body: JSON.stringify(body),
+ },
+ )
+
+ return { changed: true, htmlUrl: result.content.html_url }
+ }
+
+ private async publish(
+ payload: z.infer<typeof publishedArticleSchema>,
+ config: PublisherConfig,
+ ) {
+ let incomingOrigin: string
+ let expectedOrigin: string
+
+ try {
+ incomingOrigin = new URL(payload.site.url).origin
+ expectedOrigin = new URL(config.siteUrl).origin
+ } catch {
+ throw new BadRequestException('Webhook site URL is invalid')
+ }
+
+ if (incomingOrigin !== expectedOrigin) {
+ throw new BadRequestException('Webhook is for a different site')
+ }
+
+ if (payload.article.language !== config.language) {
+ throw new BadRequestException('Webhook article language is not supported')
+ }
+
+ const publishedDate =
+ validDate(payload.article.scheduledFor) || validDate(payload.publishedAt)
+
+ if (!publishedDate) {
+ throw new BadRequestException('Webhook publication date is invalid')
+ }
+
+ const slug = slugify(payload.article.slug || payload.article.title)
+ const desiredPath = `posts/${formatFileDate(
+ publishedDate,
+ config.timeZone,
+ )}-${slug}.md`
+ const path = await this.findExistingPath(slug, desiredPath, config)
+ const content = articleMarkdown(payload.article, publishedDate, config)
+
+ let result: { changed: boolean; htmlUrl: string }
+
+ try {
+ result = await this.writeContent(path, content, slug, config)
+ } catch (error) {
+ if (
+ !(error instanceof GitHubApiError) ||
+ ![409, 422].includes(error.status)
+ ) {
+ throw error
+ }
+
+ result = await this.writeContent(path, content, slug, config)
+ }
+
+ this.logger.log(
+ {
+ articleId: payload.article.id ?? null,
+ path,
+ changed: result.changed,
+ commitUrl: result.htmlUrl,
+ },
+ 'POST /webhook/rankpine',
+ true,
+ )
+
+ return {
+ ok: true,
+ id: path,
+ url: new URL(`/blog/${slug}`, expectedOrigin).toString(),
+ changed: result.changed,
+ }
+ }
+
+ async handle(
+ body: Buffer,
+ signature: string | undefined,
+ eventHeader: string | undefined,
+ ) {
+ const config = this.getConfig()
+
+ if (!Buffer.isBuffer(body)) {
+ throw new BadRequestException('Webhook body must be raw JSON')
+ }
+
+ const rawBody = body.toString('utf8')
+ this.verifySignature(rawBody, signature, config.secret)
+
+ let json: unknown
+
+ try {
+ json = JSON.parse(rawBody)
+ } catch {
+ throw new BadRequestException('Webhook body is invalid JSON')
+ }
+
+ const verification = verificationSchema.safeParse(json)
+
+ if (verification.success) {
+ if (eventHeader && eventHeader !== 'verification') {
+ throw new BadRequestException(
+ 'Webhook event header does not match body',
+ )
+ }
+
+ return { ok: true, verified: true }
+ }
+
+ const published = publishedArticleSchema.safeParse(json)
+
+ if (!published.success) {
+ throw new BadRequestException('Webhook payload is invalid')
+ }
+
+ if (eventHeader && eventHeader !== published.data.event) {
+ throw new BadRequestException('Webhook event header does not match body')
+ }
+
+ try {
+ return await this.publish(published.data, config)
+ } catch (error) {
+ if (error instanceof GitHubApiError) {
+ this.logger.error(
+ {
+ status: error.status,
+ reason: error.message,
+ articleId: published.data.article.id ?? null,
+ },
+ 'POST /webhook/rankpine',
+ true,
+ )
+ throw new BadGatewayException('Could not publish the article to GitHub')
+ }
+
+ throw error
+ }
+ }
+}
diff --git a/backend/apps/cloud/src/webhook/webhook.controller.ts b/backend/apps/cloud/src/webhook/webhook.controller.ts
index eafee2e06..61fdfa345 100644
--- a/backend/apps/cloud/src/webhook/webhook.controller.ts
+++ b/backend/apps/cloud/src/webhook/webhook.controller.ts
@@ -36,6 +36,7 @@ import {
RevenueStatus,
RevenueType,
} from '../revenue/interfaces/revenue.interface'
+import { RankPineBlogService } from './rankpine-blog.service'
const BILLING_URL = 'https://swetrix.com/user-settings?tab=billing'
const BILLING_DUNNING_GRACE_DAYS = 7
@@ -52,6 +53,7 @@ export class WebhookController {
private readonly mailerService: MailerService,
private readonly revenueService: RevenueService,
private readonly currencyService: CurrencyService,
+ private readonly rankPineBlogService: RankPineBlogService,
) {}
private getPaddleField(
@@ -571,6 +573,16 @@ export class WebhookController {
}
}
+ @Post('/rankpine')
+ @HttpCode(200)
+ async rankPineWebhook(
+ @Body() body: Buffer,
+ @Headers('x-rankpine-signature') signature?: string,
+ @Headers('x-rankpine-event') event?: string,
+ ) {
+ return this.rankPineBlogService.handle(body, signature, event)
+ }
+
// AWS SNS webhook
@Post('/sns')
@HttpCode(200)
diff --git a/backend/apps/cloud/src/webhook/webhook.module.ts b/backend/apps/cloud/src/webhook/webhook.module.ts
index 89102ac08..dca1ccde3 100644
--- a/backend/apps/cloud/src/webhook/webhook.module.ts
+++ b/backend/apps/cloud/src/webhook/webhook.module.ts
@@ -7,6 +7,7 @@ import { AppLoggerModule } from '../logger/logger.module'
import { MailerModule } from '../mailer/mailer.module'
import { WebhookService } from './webhook.service'
import { RevenueModule } from '../revenue/revenue.module'
+import { RankPineBlogService } from './rankpine-blog.service'
@Module({
imports: [
@@ -16,7 +17,7 @@ import { RevenueModule } from '../revenue/revenue.module'
MailerModule,
RevenueModule,
],
- providers: [WebhookService],
+ providers: [WebhookService, RankPineBlogService],
exports: [WebhookService],
controllers: [WebhookController],
})