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/CrosspostService.tsmodified+107-5
Changes to app/Services/Social/CrosspostService.ts
@@ -1,6 +1,7 @@
11import type { CrosspostTargetResult, PublishContent, SocialProvider } from '../../Support/Social/types'
22import { db } from '@stacksjs/database'
33import { env } from '@stacksjs/env'
4import { blog } from './BlogService'
45import { bluesky } from './BlueskyService'
56import { instagram } from './InstagramService'
67import { linkedin } from './LinkedInService'
@@ -19,6 +20,7 @@ const publishers: Partial<Record<SocialProvider, ProviderPublisher>> = {
1920 linkedin,
2021 instagram,
2122 threads,
23 blog,
2224}
2325
2426export function crosspostProviders(): SocialProvider[] {
@@ -67,11 +69,7 @@ export class CrosspostService {
6769 .where('uuid', '=', postUuid)
6870 .executeTakeFirstOrThrow()
6971
70 const results: CrosspostTargetResult[] = []
71 for (const provider of selected) {
72 const publisher = publishers[provider]!
73 results.push(await publisher.publishToPost({ id: Number(post.id), body }, content))
74 }
72 const results = await this.publishExisting({ id: Number(post.id), body }, selected, content)
7573
7674 const anyOk = results.some(result => result.ok)
7775 const finishedAt = now()
@@ -83,6 +81,110 @@ export class CrosspostService {
8381
8482 return { postId: Number(post.id), results }
8583 }
84
85 /**
86 * Publish a multi-post thread. Each segment gets its own `posts` row
87 * (grouped by `thread_key`). Providers that support reply chains
88 * (Bluesky) receive root/parent refs so segments render as an actual
89 * thread; other providers publish each segment independently.
90 */
91 async publishThread(
92 texts: string[],
93 providers: SocialProvider[],
94 content?: PublishContent,
95 ): Promise<{ postIds: number[], results: CrosspostTargetResult[] }> {
96 const segments = texts.map(text => text.trim()).filter(Boolean)
97 if (segments.length === 0) throw new Error('Post text is required.')
98
99 const selected = providers.filter(provider => publishers[provider])
100 if (selected.length === 0) {
101 throw new Error('Select at least one connected provider to publish.')
102 }
103
104 const accountId = await ensureAccount()
105 const threadKey = uuid()
106 const postIds: number[] = []
107 const results: CrosspostTargetResult[] = []
108 // Per-provider chain refs; only populated for providers whose results
109 // carry a cid (Bluesky).
110 const chains = new Map<SocialProvider, { root: { uri: string, cid: string }, parent: { uri: string, cid: string } }>()
111
112 for (const [index, body] of segments.entries()) {
113 const postUuid = uuid()
114 const createdAt = now()
115
116 await database.insertInto('posts').values({
117 uuid: postUuid,
118 title: body.slice(0, 80),
119 body,
120 status: 'publishing',
121 timezone: env.TZ || 'America/Los_Angeles',
122 source: 'composer',
123 thread_key: threadKey,
124 account_id: accountId,
125 created_at: createdAt,
126 updated_at: createdAt,
127 }).execute()
128
129 const post = await database
130 .selectFrom('posts')
131 .selectAll()
132 .where('uuid', '=', postUuid)
133 .executeTakeFirstOrThrow()
134 postIds.push(Number(post.id))
135
136 const segmentResults: CrosspostTargetResult[] = []
137 for (const provider of selected) {
138 const publisher = publishers[provider]!
139 const chain = chains.get(provider)
140 // Link previews/media only make sense on the first segment.
141 const segmentContent: PublishContent | undefined = index === 0
142 ? content
143 : chain ? { reply: chain } : undefined
144
145 const result = await publisher.publishToPost({ id: Number(post.id), body }, segmentContent)
146 segmentResults.push(result)
147
148 if (result.ok && result.uri && result.cid) {
149 const existing = chains.get(provider)
150 chains.set(provider, {
151 root: existing?.root || { uri: result.uri, cid: result.cid },
152 parent: { uri: result.uri, cid: result.cid },
153 })
154 }
155 }
156
157 results.push(...segmentResults)
158 const anyOk = segmentResults.some(result => result.ok)
159 const finishedAt = now()
160 await database.updateTable('posts').set({
161 status: anyOk ? 'published' : 'failed',
162 ...(anyOk ? { published_at: finishedAt } : {}),
163 updated_at: finishedAt,
164 }).where('id', '=', post.id).execute()
165 }
166
167 return { postIds, results }
168 }
169
170 /**
171 * Publish an existing `posts` row to the given providers. Used by the
172 * fresh-publish path above and by the queue when a scheduled or drafted
173 * post is (re)published.
174 */
175 async publishExisting(
176 post: { id: number, body: string },
177 providers: SocialProvider[],
178 content?: PublishContent,
179 ): Promise<CrosspostTargetResult[]> {
180 const results: CrosspostTargetResult[] = []
181 for (const provider of providers) {
182 const publisher = publishers[provider]
183 if (!publisher) continue
184 results.push(await publisher.publishToPost({ id: post.id, body: post.body }, content))
185 }
186 return results
187 }
86188}
87189
88190export const crosspost = new CrosspostService()