( rawId: string, userAgent: string | undefined )
| 13 | } |
| 14 | |
| 15 | export async function constructThreadsPost( |
| 16 | rawId: string, |
| 17 | userAgent: string | undefined |
| 18 | ): Promise<SocialThread> { |
| 19 | const shortcode = normalizeThreadsPostId(rawId); |
| 20 | let mediaId: string; |
| 21 | try { |
| 22 | mediaId = threadsShortcodeToMediaId(shortcode); |
| 23 | } catch { |
| 24 | return { code: 400, status: null, thread: null, author: null }; |
| 25 | } |
| 26 | |
| 27 | const session = await fetchThreadsSession(userAgent); |
| 28 | if (!session) { |
| 29 | return { code: 500, status: null, thread: null, author: null }; |
| 30 | } |
| 31 | |
| 32 | const res = await fetchThreadsPostPage({ |
| 33 | mediaId, |
| 34 | sortOrder: 'TOP', |
| 35 | after: null, |
| 36 | first: null, |
| 37 | session, |
| 38 | userAgent |
| 39 | }); |
| 40 | if (!res.ok || res.json == null) { |
| 41 | return { code: res.status === 404 ? 404 : 500, status: null, thread: null, author: null }; |
| 42 | } |
| 43 | |
| 44 | const { edges } = extractPostPageEdges(res.json); |
| 45 | if (!edges.length) { |
| 46 | return { code: 404, status: null, thread: null, author: null }; |
| 47 | } |
| 48 | |
| 49 | const focalNode = edges[0]?.node; |
| 50 | if (!focalNode) { |
| 51 | return { code: 404, status: null, thread: null, author: null }; |
| 52 | } |
| 53 | |
| 54 | const items = (focalNode.thread_items as unknown[]) ?? []; |
| 55 | if (!Array.isArray(items) || items.length === 0) { |
| 56 | return { code: 404, status: null, thread: null, author: null }; |
| 57 | } |
| 58 | |
| 59 | const firstPost = (items[0] as { post?: Record<string, unknown> })?.post; |
| 60 | const owner = firstPost?.user as Record<string, unknown> | undefined; |
| 61 | const ownerFb = { |
| 62 | id: String(owner?.pk ?? owner?.id ?? ''), |
| 63 | username: String(owner?.username ?? ''), |
| 64 | fullName: typeof owner?.full_name === 'string' ? owner.full_name : undefined, |
| 65 | pic: typeof owner?.profile_pic_url === 'string' ? owner.profile_pic_url : null |
| 66 | }; |
| 67 | |
| 68 | const chainStatuses = items |
| 69 | .map(it => { |
| 70 | const p = (it as { post?: Record<string, unknown> }).post; |
| 71 | return p ? threadsPostToStatus(p, ownerFb) : null; |
| 72 | }) |
no test coverage detected