also looking at this
feat: crosspost to LinkedIn and Instagram
#1
31 files
+2335
-87
| @@ -1,4 +1,4 @@ | ||
| 1 | import type { BlueskySession, PublishedPost, TimelineResult } from '../../Support/Social/types' | |
| 1 | import type { BlueskySession, CrosspostTargetResult, PublishContent, PublishedPost, TimelineResult } from '../../Support/Social/types' | |
| 2 | 2 | import { db } from '@stacksjs/database' |
| 3 | 3 | import { env } from '@stacksjs/env' |
| 4 | 4 | import { BlueskyApiError, BlueskyDriver } from './Drivers/BlueskyDriver' |
| @@ -108,11 +108,8 @@ export class BlueskyService { | ||
| 108 | 108 | throw new Error(`Bluesky posts must be ${this.driver.characterLimit} characters or fewer.`) |
| 109 | 109 | } |
| 110 | 110 | |
| 111 | const identity = await this.requireIdentity() | |
| 112 | const driver = await this.ensureDriver() | |
| 113 | 111 | const accountId = await this.ensureAccount() |
| 114 | 112 | const postUuid = uuid() |
| 115 | const targetUuid = uuid() | |
| 116 | 113 | const createdAt = now() |
| 117 | 114 | |
| 118 | 115 | await database.insertInto('posts').values({ |
| @@ -132,10 +129,65 @@ export class BlueskyService { | ||
| 132 | 129 | .selectAll() |
| 133 | 130 | .where('uuid', '=', postUuid) |
| 134 | 131 | .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] & { | |
| 136 | 188 | external?: { uri: string, title: string, description?: string } |
| 137 | 189 | } |
| 138 | if (external) publishInput.external = external | |
| 190 | if (content?.external) publishInput.external = content.external | |
| 139 | 191 | |
| 140 | 192 | await database.insertInto('post_targets').values({ |
| 141 | 193 | uuid: targetUuid, |
| @@ -158,19 +210,13 @@ export class BlueskyService { | ||
| 158 | 210 | const published = await this.withFreshSession(identity, freshIdentity => |
| 159 | 211 | this.driver.publish({ |
| 160 | 212 | 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, | |
| 164 | 216 | }, publishInput), |
| 165 | 217 | ) |
| 166 | 218 | |
| 167 | 219 | 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 | ||
| 174 | 220 | await database.updateTable('post_targets').set({ |
| 175 | 221 | status: 'published', |
| 176 | 222 | remote_uri: published.uri, |
| @@ -180,26 +226,23 @@ export class BlueskyService { | ||
| 180 | 226 | }).where('id', '=', target.id).execute() |
| 181 | 227 | |
| 182 | 228 | return { |
| 183 | post: published, | |
| 184 | postId: Number(post.id), | |
| 229 | provider: 'bluesky', | |
| 230 | ok: true, | |
| 231 | url: published.url, | |
| 232 | uri: published.uri, | |
| 185 | 233 | targetId: Number(target.id), |
| 186 | 234 | } |
| 187 | 235 | } |
| 188 | 236 | catch (error) { |
| 189 | 237 | const message = error instanceof Error ? error.message : String(error) |
| 190 | 238 | const failedAt = now() |
| 191 | await database.updateTable('posts').set({ | |
| 192 | status: 'failed', | |
| 193 | updated_at: failedAt, | |
| 194 | }).where('id', '=', post.id).execute() | |
| 195 | ||
| 196 | 239 | await database.updateTable('post_targets').set({ |
| 197 | 240 | status: 'failed', |
| 198 | 241 | failure_reason: message, |
| 199 | 242 | updated_at: failedAt, |
| 200 | 243 | }).where('id', '=', target.id).execute() |
| 201 | 244 | |
| 202 | throw error | |
| 245 | return { provider: 'bluesky', ok: false, error: message, targetId: Number(target.id) } | |
| 203 | 246 | } |
| 204 | 247 | } |
| 205 | 248 | |