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/BlueskyService.tsmodified+66-23
Changes to app/Services/Social/BlueskyService.ts
@@ -1,4 +1,4 @@
1import type { BlueskySession, PublishedPost, TimelineResult } from '../../Support/Social/types'
1import type { BlueskySession, CrosspostTargetResult, PublishContent, PublishedPost, TimelineResult } from '../../Support/Social/types'
22import { db } from '@stacksjs/database'
33import { env } from '@stacksjs/env'
44import { BlueskyApiError, BlueskyDriver } from './Drivers/BlueskyDriver'
@@ -108,11 +108,8 @@ export class BlueskyService {
108108 throw new Error(`Bluesky posts must be ${this.driver.characterLimit} characters or fewer.`)
109109 }
110110
111 const identity = await this.requireIdentity()
112 const driver = await this.ensureDriver()
113111 const accountId = await this.ensureAccount()
114112 const postUuid = uuid()
115 const targetUuid = uuid()
116113 const createdAt = now()
117114
118115 await database.insertInto('posts').values({
@@ -132,10 +129,65 @@ export class BlueskyService {
132129 .selectAll()
133130 .where('uuid', '=', postUuid)
134131 .executeTakeFirstOrThrow()
135 const publishInput = { text: body } as Parameters<BlueskyDriver['publish']>[1] & {
132
133 const result = await this.publishToPost({ id: Number(post.id), body }, external ? { external } : undefined)
134 const finishedAt = now()
135
136 if (!result.ok) {
137 await database.updateTable('posts').set({
138 status: 'failed',
139 updated_at: finishedAt,
140 }).where('id', '=', post.id).execute()
141
142 throw new Error(result.error || 'Bluesky publish failed.')
143 }
144
145 await database.updateTable('posts').set({
146 status: 'published',
147 published_at: finishedAt,
148 updated_at: finishedAt,
149 }).where('id', '=', post.id).execute()
150
151 return {
152 post: { provider: 'bluesky', uri: result.uri || '', url: result.url },
153 postId: Number(post.id),
154 targetId: Number(result.targetId),
155 }
156 }
157
158 /**
159 * Publish an already-created post row to Bluesky as a new target. Never
160 * throws — failures are recorded on the target and returned so a crosspost
161 * to other providers can still proceed.
162 */
163 async publishToPost(
164 post: { id: number, body: string },
165 content?: PublishContent,
166 ): Promise<CrosspostTargetResult> {
167 const driver = await this.ensureDriver()
168
169 let identity: SocialIdentityRow
170 try {
171 identity = await this.requireIdentity()
172 }
173 catch (error) {
174 return { provider: 'bluesky', ok: false, error: error instanceof Error ? error.message : String(error) }
175 }
176
177 if (post.body.length > this.driver.characterLimit) {
178 return {
179 provider: 'bluesky',
180 ok: false,
181 error: `Bluesky posts must be ${this.driver.characterLimit} characters or fewer.`,
182 }
183 }
184
185 const targetUuid = uuid()
186 const createdAt = now()
187 const publishInput = { text: post.body } as Parameters<BlueskyDriver['publish']>[1] & {
136188 external?: { uri: string, title: string, description?: string }
137189 }
138 if (external) publishInput.external = external
190 if (content?.external) publishInput.external = content.external
139191
140192 await database.insertInto('post_targets').values({
141193 uuid: targetUuid,
@@ -158,19 +210,13 @@ export class BlueskyService {
158210 const published = await this.withFreshSession(identity, freshIdentity =>
159211 this.driver.publish({
160212 handle: freshIdentity.handle,
161 did: freshIdentity.external_id || undefined,
162 accessToken: freshIdentity.access_token || undefined,
163 refreshToken: freshIdentity.refresh_token || undefined,
213 did: freshIdentity.external_id || undefined,
214 accessToken: freshIdentity.access_token || undefined,
215 refreshToken: freshIdentity.refresh_token || undefined,
164216 }, publishInput),
165217 )
166218
167219 const publishedAt = now()
168 await database.updateTable('posts').set({
169 status: 'published',
170 published_at: publishedAt,
171 updated_at: publishedAt,
172 }).where('id', '=', post.id).execute()
173
174220 await database.updateTable('post_targets').set({
175221 status: 'published',
176222 remote_uri: published.uri,
@@ -180,26 +226,23 @@ export class BlueskyService {
180226 }).where('id', '=', target.id).execute()
181227
182228 return {
183 post: published,
184 postId: Number(post.id),
229 provider: 'bluesky',
230 ok: true,
231 url: published.url,
232 uri: published.uri,
185233 targetId: Number(target.id),
186234 }
187235 }
188236 catch (error) {
189237 const message = error instanceof Error ? error.message : String(error)
190238 const failedAt = now()
191 await database.updateTable('posts').set({
192 status: 'failed',
193 updated_at: failedAt,
194 }).where('id', '=', post.id).execute()
195
196239 await database.updateTable('post_targets').set({
197240 status: 'failed',
198241 failure_reason: message,
199242 updated_at: failedAt,
200243 }).where('id', '=', target.id).execute()
201244
202 throw error
245 return { provider: 'bluesky', ok: false, error: message, targetId: Number(target.id) }
203246 }
204247 }
205248