@@ -2125,76 +2125,75 @@ export function getPostBody(listing: Reddit.PostSubmission): Generic.Body {
21252125 ) as Generic . Thread ,
21262126 client_id : client . id ,
21272127 } ;
2128- if ( listing . is_self ) return {
2129- kind : "array" ,
2130- body : [
2131- listing . rtjson . document . length
2132- ? { kind : "richtext" , content : richtextDocument ( listing . rtjson , { media_metadata : listing . media_metadata ?? { } } ) }
2133- : listing . url === "https://www.reddit.com" + listing . permalink // isn't this what is_self is for? why am I doing this check?
2134- ? { kind : "none" }
2135- : { kind : "link" , client_id : client . id , url : listing . url , embed_html : listing . media_embed ?. content } , // does this code path ever get used?
2136- listing . poll_data
2137- ? { kind : "poll" ,
2138- votable : "Cannot vote" ,
2139- total_votes : listing . poll_data . total_vote_count ,
2140- choices : listing . poll_data . options . map ( choice => ( {
2141- name : choice . text ,
2142- votes : choice . vote_count ?? "hidden" ,
2143- id : choice . id ,
2144- } ) ) ,
2145- vote_data : "" ,
2146- select_many : false ,
2147- your_votes : listing . poll_data . user_selection != null ? [ { id : listing . poll_data . user_selection } ] : [ ] ,
2148- close_time : listing . poll_data . voting_end_timestamp ,
2149- } : undefined ,
2150- ] ,
2151- } ;
2152- if ( listing . gallery_data ) return {
2128+ const arrayElems : Generic . Body [ ] = [ ] ;
2129+ if ( listing . gallery_data ) arrayElems . push ( {
21532130 kind : "gallery" ,
21542131 images : listing . gallery_data . items . map ( gd => {
21552132 if ( ! listing . media_metadata ) throw new Error ( "missing media metadata" ) ;
21562133 const moreinfo = listing . media_metadata [ gd . media_id ] ;
21572134 if ( ! moreinfo ) throw new Error ( "missing mediameta for " + gd . media_id ) ;
21582135 return mediaMetaToBody ( moreinfo , gd . caption ) ;
21592136 } )
2160- } ;
2161- if ( listing . rpan_video ) return {
2137+ } ) ; else if ( listing . rpan_video ) arrayElems . push ( {
21622138 kind : "video" ,
21632139 source : { kind : "video" , sources : [ { url : listing . rpan_video . hls_url } ] } ,
21642140 gifv : false ,
2165- } ;
2166- if ( listing . preview && listing . preview . images . length === 1 ) {
2141+ } ) ; else if ( listing . preview && listing . preview . images . length === 1 ) {
21672142 const image = listing . preview . images [ 0 ] ! ;
21682143 if ( image . variants . mp4 ) {
21692144 const mp4 = image . variants . mp4 ;
21702145 const sources = [ ...mp4 . resolutions , mp4 . source ] . sort ( ( a , b ) => b . width - a . width ) . map ( source => ( {
21712146 url : source . url ,
21722147 quality : source . width + "×" + source . height ,
21732148 } ) ) ;
2174- return {
2149+ arrayElems . push ( {
21752150 kind : "video" ,
21762151 source : {
21772152 kind : "video" ,
21782153 sources : sources ,
21792154 preview : [ ...sources ] . reverse ( ) ,
21802155 } ,
21812156 gifv : true ,
2182- } ;
2183- }
2184- if ( listing . preview . enabled ) {
2157+ } ) ;
2158+ } else if ( listing . preview . enabled ) {
21852159 // not used on videos
21862160 // TODO have sources on images that specify resolutions
21872161 // in order to use lower quality versions when not in fullscreen
21882162 // preview
2189- return {
2163+ arrayElems . push ( {
21902164 kind : "captioned_image" ,
21912165 url : image . source . url ,
21922166 w : image . source . width ,
21932167 h : image . source . height ,
2194- } ;
2168+ } ) ;
21952169 }
21962170 }
2197- return { kind : "link" , client_id : client . id , url : listing . url , embed_html : listing . media_embed ?. content } ;
2171+ if ( arrayElems . length === 0 && listing . url !== "https://www.reddit.com" + listing . permalink ) { // this is probably the same as !listing.is_self
2172+ arrayElems . push ( { kind : "link" , client_id : client . id , url : listing . url , embed_html : listing . media_embed ?. content } ) ;
2173+ }
2174+
2175+ if ( listing . rtjson . document . length > 0 ) {
2176+ arrayElems . push ( { kind : "richtext" , content : richtextDocument ( listing . rtjson , { media_metadata : listing . media_metadata ?? { } } ) } ) ;
2177+ }
2178+ if ( listing . poll_data ) {
2179+ arrayElems . push ( { kind : "poll" ,
2180+ votable : "Cannot vote" ,
2181+ total_votes : listing . poll_data . total_vote_count ,
2182+ choices : listing . poll_data . options . map ( choice => ( {
2183+ name : choice . text ,
2184+ votes : choice . vote_count ?? "hidden" ,
2185+ id : choice . id ,
2186+ } ) ) ,
2187+ vote_data : "" ,
2188+ select_many : false ,
2189+ your_votes : listing . poll_data . user_selection != null ? [ { id : listing . poll_data . user_selection } ] : [ ] ,
2190+ close_time : listing . poll_data . voting_end_timestamp ,
2191+ } ) ;
2192+ }
2193+
2194+ if ( arrayElems . length === 1 ) return arrayElems [ 0 ] ! ;
2195+ if ( arrayElems . length > 0 ) return { kind : "array" , body : arrayElems } ;
2196+ return { kind : "none" } ;
21982197}
21992198
22002199export function getPostThumbnail (
0 commit comments