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 { InstagramApiError, InstagramDriver } from './Drivers/InstagramDriver' |
| 5 | import { ensureAccount, now, uuid } from './support' | |
| 5 | import { ensureAccount, expiresAt, 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 InstagramService { | ||
| 118 | 119 | code, |
| 119 | 120 | }) |
| 120 | 121 | |
| 121 | const account = await this.driver.resolveAccount(token.accessToken) | |
| 122 | // Exchange the short-lived user token for a long-lived (~60-day) one before | |
| 123 | // resolving the Page — a Page token minted from a long-lived user token does | |
| 124 | // not expire on its own, so there's no per-publish refresh grant to run. | |
| 125 | // Best-effort: fall back to the short-lived token if the exchange fails. | |
| 126 | let userToken = token.accessToken | |
| 127 | let expiresIn: number | undefined | |
| 128 | try { | |
| 129 | const longLived = await this.driver.exchangeLongLivedUserToken(cfg.clientId, cfg.clientSecret, token.accessToken) | |
| 130 | userToken = longLived.accessToken | |
| 131 | expiresIn = longLived.expiresIn | |
| 132 | } | |
| 133 | catch {} | |
| 134 | ||
| 135 | const account = await this.driver.resolveAccount(userToken) | |
| 122 | 136 | const identity = await this.saveSession({ |
| 123 | 137 | accessToken: account.pageAccessToken, |
| 124 | 138 | igUserId: account.igUserId, |
| 125 | 139 | username: account.username, |
| 140 | expiresIn, | |
| 126 | 141 | }) |
| 127 | 142 | return this.publicIdentity(identity) |
| 128 | 143 | } |
| @@ -260,7 +275,7 @@ export class InstagramService { | ||
| 260 | 275 | .executeTakeFirst() |
| 261 | 276 | } |
| 262 | 277 | |
| 263 | private async saveSession(input: { accessToken: string, igUserId: string, username?: string }): Promise<SocialIdentityRow> { | |
| 278 | private async saveSession(input: { accessToken: string, igUserId: string, username?: string, expiresIn?: number }): Promise<SocialIdentityRow> { | |
| 264 | 279 | const accountId = await ensureAccount() |
| 265 | 280 | const driver = await this.ensureDriver() |
| 266 | 281 | const existing = await this.findIdentity() |
| @@ -275,6 +290,7 @@ export class InstagramService { | ||
| 275 | 290 | auth_status: 'connected', |
| 276 | 291 | access_token: input.accessToken, |
| 277 | 292 | refresh_token: null, |
| 293 | token_expires_at: expiresAt(input.expiresIn), | |
| 278 | 294 | account_id: accountId, |
| 279 | 295 | social_driver_id: driver.id, |
| 280 | 296 | updated_at: savedAt, |