ReviewOS

also looking at this

stacks/postline

UI polish, real queue + scheduling, Bluesky threads, live analytics, blog target

#8
Merged glennmichael123 wants to merge feat/ui-polish into main
54 files +2036 -589
app/Services/Social/BlogService.tsadded+162-0
Changes to app/Services/Social/BlogService.ts
@@ -0,0 +1,162 @@
1import type { CrosspostTargetResult, PublishContent } from '../../Support/Social/types'
2import { mkdir } from 'node:fs/promises'
3import { join } from 'node:path'
4import process from 'node:process'
5import { db } from '@stacksjs/database'
6import { ensureAccount, now, uuid } from './support'
7
8const database = db as any
9
10// The framework's BunPress blog renders markdown from content/blog/*.md at
11// /blog/<slug> — publishing a file there makes the post immediately live.
12const CONTENT_DIR = join(process.cwd(), 'content/blog')
13
14/** Postline's own blog has no external character ceiling worth enforcing. */
15const BLOG_CHARACTER_LIMIT = 10000
16
17function slugify(value: string): string {
18 return value
19 .toLowerCase()
20 .normalize('NFKD')
21 .replace(/[̀-ͯ]/g, '')
22 .replace(/[^a-z0-9\s-]/g, '')
23 .trim()
24 .replace(/[\s-]+/g, '-')
25 .slice(0, 80)
26 .replace(/^-+|-+$/g, '') || 'post'
27}
28
29async function uniqueSlug(base: string): Promise<string> {
30 let candidate = base
31 for (let attempt = 2; attempt < 50; attempt++) {
32 const existing = await database
33 .selectFrom('blog_posts')
34 .select(['id'])
35 .where('slug', '=', candidate)
36 .executeTakeFirst()
37 if (!existing) return candidate
38 candidate = `${base}-${attempt}`
39 }
40 return `${base}-${uuid().slice(0, 8)}`
41}
42
43/**
44 * The blog is "just another crosspost target": publishing writes a
45 * `blog_posts` row instead of calling an external API. The first line of
46 * the text becomes the title, the rest the body.
47 */
48export class BlogService {
49 async status(): Promise<{ provider: 'blog', handle: string, canPublish: boolean, characterLimit: number, configured: boolean }> {
50 return {
51 provider: 'blog',
52 handle: 'Postline Blog',
53 canPublish: true,
54 characterLimit: BLOG_CHARACTER_LIMIT,
55 configured: true,
56 }
57 }
58
59 async publishToPost(
60 post: { id: number, body: string },
61 _content?: PublishContent,
62 ): Promise<CrosspostTargetResult> {
63 const targetUuid = uuid()
64 const createdAt = now()
65
66 await database.insertInto('post_targets').values({
67 uuid: targetUuid,
68 provider: 'blog',
69 status: 'publishing',
70 post_id: post.id,
71 created_at: createdAt,
72 updated_at: createdAt,
73 }).execute()
74
75 const target = await database
76 .selectFrom('post_targets')
77 .selectAll()
78 .where('uuid', '=', targetUuid)
79 .executeTakeFirstOrThrow()
80
81 try {
82 const lines = post.body.split('\n').map(line => line.trim())
83 const title = (lines.find(Boolean) || 'Untitled post').slice(0, 200)
84 const body = post.body.trim()
85 const slug = await uniqueSlug(slugify(title))
86 const accountId = await ensureAccount()
87 const publishedAt = now()
88
89 // Body minus the title line — the blog theme already renders the title.
90 const firstContentIndex = post.body.split('\n').findIndex(line => line.trim())
91 const markdownBody = post.body.split('\n').slice(firstContentIndex + 1).join('\n').trim() || body
92
93 await mkdir(CONTENT_DIR, { recursive: true })
94 await Bun.write(join(CONTENT_DIR, `${slug}.md`), [
95 '---',
96 `title: ${JSON.stringify(title)}`,
97 `date: ${publishedAt.slice(0, 10)}`,
98 'author: Postline',
99 '---',
100 '',
101 markdownBody,
102 '',
103 ].join('\n'))
104
105 await database.insertInto('blog_posts').values({
106 uuid: uuid(),
107 title,
108 slug,
109 body,
110 excerpt: body.slice(0, 280),
111 status: 'published',
112 published_at: publishedAt,
113 post_id: post.id,
114 account_id: accountId,
115 created_at: publishedAt,
116 updated_at: publishedAt,
117 }).execute()
118
119 const url = `/blog/${slug}`
120 await database.updateTable('post_targets').set({
121 status: 'published',
122 remote_uri: url,
123 failure_reason: null,
124 updated_at: publishedAt,
125 }).where('id', '=', target.id).execute()
126
127 return { provider: 'blog', ok: true, url, uri: url, targetId: Number(target.id) }
128 }
129 catch (error) {
130 const message = error instanceof Error ? error.message : String(error)
131 await database.updateTable('post_targets').set({
132 status: 'failed',
133 failure_reason: message.slice(0, 1000),
134 updated_at: now(),
135 }).where('id', '=', target.id).execute()
136
137 return { provider: 'blog', ok: false, error: message, targetId: Number(target.id) }
138 }
139 }
140
141 /** Published blog posts, newest first. */
142 async list(limit = 50): Promise<Array<{ id: number, title: string, slug: string, excerpt: string | null, body: string, publishedAt: string | null }>> {
143 const rows = await database
144 .selectFrom('blog_posts')
145 .selectAll()
146 .where('status', '=', 'published')
147 .orderBy('published_at', 'desc')
148 .limit(limit)
149 .execute()
150
151 return rows.map((row: any) => ({
152 id: Number(row.id),
153 title: String(row.title),
154 slug: String(row.slug),
155 excerpt: row.excerpt || null,
156 body: String(row.body),
157 publishedAt: row.published_at || null,
158 }))
159 }
160}
161
162export const blog = new BlogService()