also looking at this
feat: crossposting — media (LinkedIn/IG/Threads), OAuth token refresh, and X/Twitter
#30
28 files
+1810
-109
| @@ -2,7 +2,7 @@ import type { CrosspostTargetResult, PublishContent } from '../../Support/Social | ||
| 2 | 2 | import { db } from '@stacksjs/database' |
| 3 | 3 | import { env } from '@stacksjs/env' |
| 4 | 4 | import { ThreadsApiError, ThreadsDriver } from './Drivers/ThreadsDriver' |
| 5 | import { ensureAccount, now, uuid } from './support' | |
| 5 | import { ensureAccount, expiresAt, isExpiringSoon, now, uuid } from './support' | |
| 6 | 6 | |
| 7 | 7 | const database = db as any |
| 8 | 8 | |
| @@ -15,6 +15,7 @@ type SocialIdentityRow = { | ||
| 15 | 15 | auth_status: 'connected' | 'expired' | 'revoked' | 'missing' |
| 16 | 16 | access_token?: string | null |
| 17 | 17 | refresh_token?: string | null |
| 18 | token_expires_at?: string | null | |
| 18 | 19 | account_id?: number | null |
| 19 | 20 | social_driver_id?: number | null |
| 20 | 21 | } |
| @@ -118,11 +119,25 @@ export class ThreadsService { | ||
| 118 | 119 | code, |
| 119 | 120 | }) |
| 120 | 121 | |
| 121 | const account = await this.driver.resolveAccount(token.accessToken) | |
| 122 | // Upgrade the short-lived (~1h) token to a long-lived (~60-day) one so | |
| 123 | // scheduled posts keep working, then resolve the account against it. If the | |
| 124 | // exchange fails, fall back to the short-lived token — publishing still | |
| 125 | // works, just briefly. | |
| 126 | let accessToken = token.accessToken | |
| 127 | let expiresIn = token.expiresIn | |
| 128 | try { | |
| 129 | const longLived = await this.driver.exchangeLongLivedToken(cfg.clientSecret, token.accessToken) | |
| 130 | accessToken = longLived.accessToken | |
| 131 | expiresIn = longLived.expiresIn | |
| 132 | } | |
| 133 | catch {} | |
| 134 | ||
| 135 | const account = await this.driver.resolveAccount(accessToken) | |
| 122 | 136 | const identity = await this.saveSession({ |
| 123 | 137 | accessToken: account.accessToken, |
| 124 | 138 | threadsUserId: account.threadsUserId, |
| 125 | 139 | username: account.username, |
| 140 | expiresIn, | |
| 126 | 141 | }) |
| 127 | 142 | return this.publicIdentity(identity) |
| 128 | 143 | } |
| @@ -235,7 +250,9 @@ export class ThreadsService { | ||
| 235 | 250 | |
| 236 | 251 | private async requireIdentity(): Promise<SocialIdentityRow> { |
| 237 | 252 | const existing = await this.findIdentity() |
| 238 | if (existing?.access_token && existing.auth_status === 'connected') return existing | |
| 253 | if (existing?.access_token && existing.auth_status === 'connected') { | |
| 254 | return await this.refreshIfExpiring(existing) | |
| 255 | } | |
| 239 | 256 | |
| 240 | 257 | if (this.config().accessToken) { |
| 241 | 258 | await this.connectFromEnv() |
| @@ -246,6 +263,32 @@ export class ThreadsService { | ||
| 246 | 263 | throw new Error('Connect Threads before publishing.') |
| 247 | 264 | } |
| 248 | 265 | |
| 266 | /** | |
| 267 | * Refresh a long-lived token that's within its pre-expiry window, extending | |
| 268 | * it another ~60 days. Best-effort: a refresh failure leaves the current | |
| 269 | * token in place, so a genuinely dead token still surfaces at publish time | |
| 270 | * (marking the identity expired and prompting a reconnect) rather than here. | |
| 271 | */ | |
| 272 | private async refreshIfExpiring(identity: SocialIdentityRow): Promise<SocialIdentityRow> { | |
| 273 | if (!identity.access_token || !isExpiringSoon(identity.token_expires_at)) return identity | |
| 274 | ||
| 275 | try { | |
| 276 | const refreshed = await this.driver.refreshLongLivedToken(identity.access_token) | |
| 277 | const nextExpiry = expiresAt(refreshed.expiresIn) | |
| 278 | await database.updateTable('social_identities').set({ | |
| 279 | access_token: refreshed.accessToken, | |
| 280 | token_expires_at: nextExpiry, | |
| 281 | auth_status: 'connected', | |
| 282 | updated_at: now(), | |
| 283 | }).where('id', '=', identity.id).execute() | |
| 284 | ||
| 285 | return { ...identity, access_token: refreshed.accessToken, token_expires_at: nextExpiry } | |
| 286 | } | |
| 287 | catch { | |
| 288 | return identity | |
| 289 | } | |
| 290 | } | |
| 291 | ||
| 249 | 292 | private async findIdentity(): Promise<SocialIdentityRow | undefined> { |
| 250 | 293 | return await database |
| 251 | 294 | .selectFrom('social_identities') |
| @@ -255,7 +298,7 @@ export class ThreadsService { | ||
| 255 | 298 | .executeTakeFirst() |
| 256 | 299 | } |
| 257 | 300 | |
| 258 | private async saveSession(input: { accessToken: string, threadsUserId: string, username?: string }): Promise<SocialIdentityRow> { | |
| 301 | private async saveSession(input: { accessToken: string, threadsUserId: string, username?: string, expiresIn?: number }): Promise<SocialIdentityRow> { | |
| 259 | 302 | const accountId = await ensureAccount() |
| 260 | 303 | const driver = await this.ensureDriver() |
| 261 | 304 | const existing = await this.findIdentity() |
| @@ -270,6 +313,7 @@ export class ThreadsService { | ||
| 270 | 313 | auth_status: 'connected', |
| 271 | 314 | access_token: input.accessToken, |
| 272 | 315 | refresh_token: null, |
| 316 | token_expires_at: expiresAt(input.expiresIn), | |
| 273 | 317 | account_id: accountId, |
| 274 | 318 | social_driver_id: driver.id, |
| 275 | 319 | updated_at: savedAt, |