mirror of
https://github.com/fsolla/teqo.git
synced 2026-11-29 17:43:05 +00:00
Envio em lote (1 request raw-body por arquivo, fan-out ≤2, até 50 arquivos) e adicionar por link (Instagram/YouTube; extração só pela Graph API da conta própria, senão peça-link) em /campanha/comunicacao/conteudos. Cada peça tem estado próprio (Processando/Pronto/Falhou + Reprocessar), transcrição/texto e catalogação automática revisável; publicar/despublicar preserva arquivo, slug e publishedAt. - collections contentPiece + contentMedia (upload privado no S3) e migration 20260923_032714_add_content_piece (slug único gerado no 1º publish, sourceUrl única, searchText com índice trigram) - pipeline reusa C199 (job after() + reaper + retry condicional, transcrição DeepInfra, ffmpeg/chunk), o classificador de facetas do acervo e o institutionCatalog; curatedFields impede o job de sobrescrever curadoria - gate único canReadCommunicationCatalog (advisor/leader negados); delete só pelo admin; sem Consent novo - UI portada do design aprovado (lista desktop + cards mobile, diálogo de lote com progresso por arquivo, diálogo de link, ficha editável, kill switch); nav/chrome/column-picker/manifest atualizados - instagramFeed expõe mediaUrl; revalidateTag ganha wrapper seguro fora de request (fixtures/scripts) - testes: 2 unit specs, 1 int spec (16 casos) e 7 e2e na vertical - follow-up de engenharia: #1267 (docs/plans/escala-dry-pos-c211.md) Closes #1254
193 lines
6.4 KiB
TypeScript
193 lines
6.4 KiB
TypeScript
import 'server-only'
|
|
|
|
import { createWriteStream } from 'node:fs'
|
|
import { Readable, Transform } from 'node:stream'
|
|
import { pipeline } from 'node:stream/promises'
|
|
import type { Payload } from 'payload'
|
|
|
|
import {
|
|
CONTENT_MEDIA_SLUG,
|
|
CONTENT_PIECE_MAX_BYTES,
|
|
parseContentPieceLink,
|
|
type ContentPieceType,
|
|
} from '@/lib/contentPiece'
|
|
import { withPayloadTransaction } from '@/utilities/payloadTransaction'
|
|
import {
|
|
INSTAGRAM_MAX_RESULTS_CAP,
|
|
isInstagramFeedConfigured,
|
|
loadInstagramFeed,
|
|
} from '@/utilities/socialFeed/instagramFeed'
|
|
|
|
/**
|
|
* C211 — the official-path resolution of a piece that entered by link. There is
|
|
* NO scraping and no oEmbed: the only extraction route is the Instagram Graph
|
|
* API of the campaign's OWN account (listing the profile media and matching the
|
|
* pasted permalink), exactly what the gate decided. YouTube exposes only
|
|
* metadata through the Data API, so a YouTube piece always circulates by link.
|
|
*
|
|
* A failed extraction never throws at the caller: the piece becomes a
|
|
* peça-link (the job still catalogues the official caption when there is one).
|
|
* A download that starts and fails DOES throw — the row is preserved and the
|
|
* assessoria can reprocess or attach the original file.
|
|
*/
|
|
|
|
export type ContentPieceSourceMedia = {
|
|
id: number
|
|
filename?: string | null
|
|
filesize?: number | null
|
|
mimeType?: string | null
|
|
}
|
|
|
|
export type ContentPieceSourceResolution = {
|
|
/** The archived media (existing or newly extracted); null for a peça-link. */
|
|
media: ContentPieceSourceMedia | null
|
|
/** Local temp copy of an extracted file (the job's ffmpeg input). */
|
|
localPath: string | null
|
|
/** Official caption of the matched post; null when there was no match. */
|
|
caption: string | null
|
|
/** The type the official media reveals (an Instagram image is a photo). */
|
|
suggestedType?: ContentPieceType
|
|
}
|
|
|
|
type FeedLoader = typeof loadInstagramFeed
|
|
|
|
const streamResponseToFile = async ({
|
|
response,
|
|
destination,
|
|
maxBytes,
|
|
}: {
|
|
response: Response
|
|
destination: string
|
|
maxBytes: number
|
|
}): Promise<void> => {
|
|
if (!response.body) throw new Error('A mídia extraída não trouxe arquivo.')
|
|
let received = 0
|
|
const guard = new Transform({
|
|
transform(chunk: Buffer, _encoding, callback) {
|
|
received += chunk.length
|
|
if (received > maxBytes) {
|
|
callback(new Error('A mídia extraída excede o limite da Central.'))
|
|
return
|
|
}
|
|
callback(null, chunk)
|
|
},
|
|
})
|
|
await pipeline(
|
|
Readable.fromWeb(response.body as unknown as import('node:stream/web').ReadableStream),
|
|
guard,
|
|
createWriteStream(destination),
|
|
)
|
|
}
|
|
|
|
/**
|
|
* Resolves the source of one piece: the archived media when it exists,
|
|
* otherwise the official extraction for an Instagram link, otherwise nothing
|
|
* (peça-link). Injectable deps keep the int test free of network calls.
|
|
*/
|
|
export const resolveContentPieceSource = async ({
|
|
payload,
|
|
piece,
|
|
tempDir,
|
|
loadFeed = loadInstagramFeed,
|
|
fetchImpl = fetch,
|
|
}: {
|
|
payload: Payload
|
|
piece: {
|
|
id: number
|
|
title: string
|
|
origin: string
|
|
sourceUrl?: string | null
|
|
media?: number | ContentPieceSourceMedia | null
|
|
}
|
|
tempDir: string
|
|
loadFeed?: FeedLoader
|
|
fetchImpl?: (input: string, init?: RequestInit) => Promise<Response>
|
|
}): Promise<ContentPieceSourceResolution> => {
|
|
if (piece.media && typeof piece.media === 'object') {
|
|
return { media: piece.media, localPath: null, caption: null }
|
|
}
|
|
if (piece.media) {
|
|
// A bare id (depth 0) is not a file the pipeline can read; fail closed to
|
|
// the link resolution instead of handing a number to the downloader.
|
|
return { media: null, localPath: null, caption: null }
|
|
}
|
|
if (piece.origin !== 'instagram' || !piece.sourceUrl) {
|
|
return { media: null, localPath: null, caption: null }
|
|
}
|
|
|
|
const link = parseContentPieceLink(piece.sourceUrl)
|
|
if (!link || link.origin !== 'instagram') {
|
|
return { media: null, localPath: null, caption: null }
|
|
}
|
|
|
|
const settings = await payload.findGlobal({ slug: 'social-feed-settings', depth: 0 })
|
|
if (!isInstagramFeedConfigured(settings)) {
|
|
return { media: null, localPath: null, caption: null }
|
|
}
|
|
|
|
let feed: Awaited<ReturnType<FeedLoader>>
|
|
try {
|
|
feed = await loadFeed({
|
|
accessToken: settings.instagramAccessToken as string,
|
|
userId: settings.instagramUserId as string,
|
|
maxResults: INSTAGRAM_MAX_RESULTS_CAP,
|
|
fetchImpl,
|
|
})
|
|
} catch {
|
|
// Token expired, API down: fail closed to the link piece, never to an error
|
|
// in the assessoria's face.
|
|
return { media: null, localPath: null, caption: null }
|
|
}
|
|
|
|
// Canonical URLs on both sides: the shortcode is the identity and the form is
|
|
// the same, so two spellings of the same post always match.
|
|
const matched = feed.posts.find(
|
|
(post) => parseContentPieceLink(post.permalink)?.canonicalUrl === link.canonicalUrl,
|
|
)
|
|
if (!matched) return { media: null, localPath: null, caption: null }
|
|
|
|
const extractable =
|
|
matched.mediaType === 'IMAGE' || matched.mediaType === 'VIDEO' || matched.mediaType === 'REEL'
|
|
if (!extractable || !matched.mediaUrl) {
|
|
// Carousel (no single file) or a post without a media URL: the caption is
|
|
// still official metadata the cataloguing can use.
|
|
return { media: null, localPath: null, caption: matched.caption }
|
|
}
|
|
|
|
const response = await fetchImpl(matched.mediaUrl)
|
|
if (!response.ok) throw new Error('Não foi possível baixar a mídia do Instagram.')
|
|
|
|
const suggestedType: ContentPieceType = matched.mediaType === 'IMAGE' ? 'foto' : 'video'
|
|
const extension = suggestedType === 'foto' ? 'jpg' : 'mp4'
|
|
const localPath = `${tempDir}/instagram-${link.shortcode}.${extension}`
|
|
await streamResponseToFile({
|
|
response,
|
|
destination: localPath,
|
|
maxBytes: CONTENT_PIECE_MAX_BYTES,
|
|
})
|
|
|
|
const media = await withPayloadTransaction(payload, async ({ req }) => {
|
|
const created = await payload.create({
|
|
collection: CONTENT_MEDIA_SLUG,
|
|
data: { alt: piece.title },
|
|
filePath: localPath,
|
|
depth: 0,
|
|
// Intentional admin bypass: the extraction runs in the pipeline, after
|
|
// the response, and the piece was created behind the Central gate.
|
|
overrideAccess: true,
|
|
req,
|
|
})
|
|
await payload.update({
|
|
collection: 'contentPiece',
|
|
id: piece.id,
|
|
data: { media: created.id },
|
|
depth: 0,
|
|
// Intentional admin bypass: the pipeline owns the row state.
|
|
overrideAccess: true,
|
|
req,
|
|
})
|
|
return created
|
|
})
|
|
|
|
return { media, localPath, caption: matched.caption, suggestedType }
|
|
}
|