ReviewOS

also looking at this

stacks/postline

feat: crosspost to LinkedIn and Instagram

#1
Merged chris wants to merge feat/crosspost-linkedin-instagram into main
31 files +2335 -87
app/Services/Social/CrosspostService.tsadded+86-0
Changes to app/Services/Social/CrosspostService.ts
@@ -0,0 +1,86 @@
1import type { CrosspostTargetResult, PublishContent, SocialProvider } from '../../Support/Social/types'
2import { db } from '@stacksjs/database'
3import { env } from '@stacksjs/env'
4import { bluesky } from './BlueskyService'
5import { instagram } from './InstagramService'
6import { linkedin } from './LinkedInService'
7import { ensureAccount, now, uuid } from './support'
8
9const database = db as any
10
11interface ProviderPublisher {
12 publishToPost: (post: { id: number, body: string }, content?: PublishContent) => Promise<CrosspostTargetResult>
13}
14
15// Each provider owns its own connection/token handling behind `publishToPost`.
16const publishers: Partial<Record<SocialProvider, ProviderPublisher>> = {
17 bluesky,
18 linkedin,
19 instagram,
20}
21
22export function crosspostProviders(): SocialProvider[] {
23 return Object.keys(publishers) as SocialProvider[]
24}
25
26export class CrosspostService {
27 /**
28 * Publish one piece of content to several providers at once. A single
29 * `posts` row is created and each provider gets its own `post_targets` row.
30 * Per-provider failures are isolated — one platform erroring never aborts
31 * the others.
32 */
33 async publish(
34 text: string,
35 providers: SocialProvider[],
36 content?: PublishContent,
37 ): Promise<{ postId: number, results: CrosspostTargetResult[] }> {
38 const body = text.trim()
39 if (!body) throw new Error('Post text is required.')
40
41 const selected = providers.filter(provider => publishers[provider])
42 if (selected.length === 0) {
43 throw new Error('Select at least one connected provider to publish.')
44 }
45
46 const accountId = await ensureAccount()
47 const postUuid = uuid()
48 const createdAt = now()
49
50 await database.insertInto('posts').values({
51 uuid: postUuid,
52 title: body.slice(0, 80),
53 body,
54 status: 'publishing',
55 timezone: env.TZ || 'America/Los_Angeles',
56 source: 'composer',
57 account_id: accountId,
58 created_at: createdAt,
59 updated_at: createdAt,
60 }).execute()
61
62 const post = await database
63 .selectFrom('posts')
64 .selectAll()
65 .where('uuid', '=', postUuid)
66 .executeTakeFirstOrThrow()
67
68 const results: CrosspostTargetResult[] = []
69 for (const provider of selected) {
70 const publisher = publishers[provider]!
71 results.push(await publisher.publishToPost({ id: Number(post.id), body }, content))
72 }
73
74 const anyOk = results.some(result => result.ok)
75 const finishedAt = now()
76 await database.updateTable('posts').set({
77 status: anyOk ? 'published' : 'failed',
78 ...(anyOk ? { published_at: finishedAt } : {}),
79 updated_at: finishedAt,
80 }).where('id', '=', post.id).execute()
81
82 return { postId: Number(post.id), results }
83 }
84}
85
86export const crosspost = new CrosspostService()