@@ -14,7 +14,7 @@ use once_cell::sync::Lazy;
1414use rand:: Rng ;
1515use regex:: Regex ;
1616use rocket:: State ;
17- use rsky_common:: env:: env_int;
17+ use rsky_common:: env:: { env_bool , env_int} ;
1818use rsky_common:: explicit_slurs:: contains_explicit_slurs;
1919use rsky_lexicon:: app:: bsky:: embed:: { Embeds , MediaUnion } ;
2020use rsky_lexicon:: app:: bsky:: feed:: PostLabels ;
@@ -363,15 +363,16 @@ pub async fn get_all_posts(
363363 config : & State < FeedGenConfig > ,
364364) -> Result < AlgoResponse , ValidationErrorMessageResponse > {
365365 use crate :: schema:: post:: dsl as PostSchema ;
366+ use chrono:: Timelike ;
367+ use chrono_tz:: America :: New_York ;
366368
367369 let show_sponsored_post = config. show_sponsored_post . clone ( ) ;
368370 let sponsored_post_uri = config. sponsored_post_uri . clone ( ) ;
369371 let sponsored_post_probability = config. sponsored_post_probability . clone ( ) ;
370-
371- let params_cursor = match params_cursor {
372- None => None ,
373- Some ( params_cursor) => Some ( params_cursor. to_string ( ) ) ,
374- } ;
372+ let blackout_enabled = env_bool ( "FEEDGEN_MEDIA_BLACKOUT_ENABLED" ) . unwrap_or ( false ) ;
373+ let blackout_start_hour = env_int ( "FEEDGEN_MEDIA_BLACKOUT_START" ) . unwrap_or ( 22 ) as u32 ;
374+ let blackout_end_hour = env_int ( "FEEDGEN_MEDIA_BLACKOUT_END" ) . unwrap_or ( 4 ) as u32 ;
375+ let params_cursor = params_cursor. map ( |s| s. to_string ( ) ) ;
375376
376377 let result = connection
377378 . run ( move |conn| {
@@ -382,12 +383,34 @@ pub async fn get_all_posts(
382383 . filter ( sql :: < Bool > ( "COALESCE(array_length(labels, 1), 0) = 0" ) )
383384 . into_boxed ( ) ;
384385
386+ // Apply the media exclusion filters only if blackout is enabled, and we're within the blackout period.
387+ if blackout_enabled {
388+ let now_et = Utc :: now ( ) . with_timezone ( & New_York ) ;
389+ let now_hour = now_et. hour ( ) ;
390+
391+ let in_blackout = if blackout_start_hour < blackout_end_hour {
392+ now_hour >= blackout_start_hour && now_hour < blackout_end_hour
393+ } else {
394+ // For periods spanning midnight, e.g. 22:00 to 04:00.
395+ now_hour >= blackout_start_hour || now_hour < blackout_end_hour
396+ } ;
397+
398+ if in_blackout {
399+ query = query
400+ . filter ( sql :: < Bool > (
401+ "NOT EXISTS (SELECT 1 FROM image WHERE image.\" postUri\" = post.uri)" ,
402+ ) )
403+ . filter ( sql :: < Bool > (
404+ "NOT EXISTS (SELECT 1 FROM video WHERE video.\" postUri\" = post.uri)" ,
405+ ) ) ;
406+ }
407+ }
408+
385409 if let Some ( lang) = lang {
386410 query = query. filter ( PostSchema :: lang. like ( format ! ( "%{}%" , lang) ) ) ;
387411 }
388412
389- if params_cursor. is_some ( ) {
390- let cursor_str = params_cursor. unwrap ( ) ;
413+ if let Some ( cursor_str) = params_cursor {
391414 let v = cursor_str
392415 . split ( "::" )
393416 . take ( 2 )
@@ -431,13 +454,9 @@ pub async fn get_all_posts(
431454 cursor = Some ( format ! ( "{}::{}" , timestamp_millis, last_post. cid) ) ;
432455 }
433456
434- results
435- . into_iter ( )
436- . map ( |result| {
437- let post_result = PostResult { post : result. uri } ;
438- post_results. push ( post_result) ;
439- } )
440- . for_each ( drop) ;
457+ for result in results {
458+ post_results. push ( PostResult { post : result. uri } ) ;
459+ }
441460
442461 // Insert the sponsored post if the conditions are met
443462 if show_sponsored_post && post_results. len ( ) >= 3 && !sponsored_post_uri. is_empty ( ) {
0 commit comments