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 { LinkedInApiError, LinkedInDriver } from './Drivers/LinkedInDriver' |
| 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 | } |
| @@ -125,6 +126,8 @@ export class LinkedInService { | ||
| 125 | 126 | |
| 126 | 127 | const identity = await this.saveSession({ |
| 127 | 128 | accessToken: token.accessToken, |
| 129 | refreshToken: token.refreshToken, | |
| 130 | expiresIn: token.expiresIn, | |
| 128 | 131 | authorUrn, |
| 129 | 132 | name: profile?.name, |
| 130 | 133 | }) |
| @@ -204,6 +207,7 @@ export class LinkedInService { | ||
| 204 | 207 | }, { |
| 205 | 208 | text: post.body, |
| 206 | 209 | ...(content?.external ? { external: content.external } : {}), |
| 210 | ...(content?.media?.length ? { media: content.media } : {}), | |
| 207 | 211 | }) |
| 208 | 212 | |
| 209 | 213 | await database.updateTable('post_targets').set({ |
| @@ -238,7 +242,9 @@ export class LinkedInService { | ||
| 238 | 242 | |
| 239 | 243 | private async requireIdentity(): Promise<SocialIdentityRow> { |
| 240 | 244 | const existing = await this.findIdentity() |
| 241 | if (existing?.access_token && existing.auth_status === 'connected') return existing | |
| 245 | if (existing?.access_token && existing.auth_status === 'connected') { | |
| 246 | return await this.refreshIfExpiring(existing) | |
| 247 | } | |
| 242 | 248 | |
| 243 | 249 | // Fall back to an env-configured token if one is available. |
| 244 | 250 | if (this.config().accessToken) { |
| @@ -250,6 +256,41 @@ export class LinkedInService { | ||
| 250 | 256 | throw new Error('Connect LinkedIn before publishing.') |
| 251 | 257 | } |
| 252 | 258 | |
| 259 | /** | |
| 260 | * Refresh the access token when it's within its pre-expiry window and a | |
| 261 | * refresh token is on file (LinkedIn only issues one when the app is enrolled | |
| 262 | * in the refresh-token program). Best-effort: any failure leaves the current | |
| 263 | * token in place so a real auth failure still surfaces at publish time. | |
| 264 | */ | |
| 265 | private async refreshIfExpiring(identity: SocialIdentityRow): Promise<SocialIdentityRow> { | |
| 266 | const cfg = this.config() | |
| 267 | if (!identity.refresh_token || !isExpiringSoon(identity.token_expires_at)) return identity | |
| 268 | if (!cfg.clientId || !cfg.clientSecret) return identity | |
| 269 | ||
| 270 | try { | |
| 271 | const refreshed = await this.driver.refreshAccessToken({ | |
| 272 | clientId: cfg.clientId, | |
| 273 | clientSecret: cfg.clientSecret, | |
| 274 | refreshToken: identity.refresh_token, | |
| 275 | }) | |
| 276 | const nextExpiry = expiresAt(refreshed.expiresIn) | |
| 277 | // LinkedIn rotates the refresh token on some grants — keep the new one. | |
| 278 | const nextRefresh = refreshed.refreshToken ?? identity.refresh_token | |
| 279 | await database.updateTable('social_identities').set({ | |
| 280 | access_token: refreshed.accessToken, | |
| 281 | refresh_token: nextRefresh, | |
| 282 | token_expires_at: nextExpiry, | |
| 283 | auth_status: 'connected', | |
| 284 | updated_at: now(), | |
| 285 | }).where('id', '=', identity.id).execute() | |
| 286 | ||
| 287 | return { ...identity, access_token: refreshed.accessToken, refresh_token: nextRefresh, token_expires_at: nextExpiry } | |
| 288 | } | |
| 289 | catch { | |
| 290 | return identity | |
| 291 | } | |
| 292 | } | |
| 293 | ||
| 253 | 294 | private async findIdentity(): Promise<SocialIdentityRow | undefined> { |
| 254 | 295 | return await database |
| 255 | 296 | .selectFrom('social_identities') |
| @@ -259,7 +300,7 @@ export class LinkedInService { | ||
| 259 | 300 | .executeTakeFirst() |
| 260 | 301 | } |
| 261 | 302 | |
| 262 | private async saveSession(input: { accessToken: string, authorUrn: string, name?: string }): Promise<SocialIdentityRow> { | |
| 303 | private async saveSession(input: { accessToken: string, authorUrn: string, name?: string, refreshToken?: string, expiresIn?: number }): Promise<SocialIdentityRow> { | |
| 263 | 304 | const accountId = await ensureAccount() |
| 264 | 305 | const driver = await this.ensureDriver() |
| 265 | 306 | const existing = await this.findIdentity() |
| @@ -273,7 +314,8 @@ export class LinkedInService { | ||
| 273 | 314 | external_id: input.authorUrn, |
| 274 | 315 | auth_status: 'connected', |
| 275 | 316 | access_token: input.accessToken, |
| 276 | refresh_token: null, | |
| 317 | refresh_token: input.refreshToken ?? null, | |
| 318 | token_expires_at: expiresAt(input.expiresIn), | |
| 277 | 319 | account_id: accountId, |
| 278 | 320 | social_driver_id: driver.id, |
| 279 | 321 | updated_at: savedAt, |