Skip to content

Commit 1934387

Browse files
committed
Add toggles for media blackout
1 parent fba050f commit 1934387

File tree

2 files changed

+37
-17
lines changed

2 files changed

+37
-17
lines changed

rsky-feedgen/Cargo.toml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "rsky-feedgen"
3-
version = "1.1.3"
3+
version = "1.1.4"
44
authors = ["Rudy Fraser <him@rudyfraser.com>"]
55
description = "A framework for building AT Protocol feed generators, in Rust."
66
license = "Apache-2.0"
@@ -28,6 +28,7 @@ base64 = "0.21.2"
2828
rand = "0.8.5"
2929
once_cell = "1.19.0"
3030
moka = { version = "0.12", features = ["future"] }
31+
chrono-tz = "0.10.1"
3132

3233
[dependencies.rocket_sync_db_pools]
3334
version = "=0.1.0"
@@ -40,4 +41,4 @@ features = ["json", "multipart"]
4041
[dev-dependencies]
4142
tokio = { version = "1", features = ["full"] }
4243
temp-env = { version = "0.3.6",features = ["async_closure"] }
43-
serial_test = "*"
44+
serial_test = "*"

rsky-feedgen/src/apis/mod.rs

Lines changed: 34 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use once_cell::sync::Lazy;
1414
use rand::Rng;
1515
use regex::Regex;
1616
use rocket::State;
17-
use rsky_common::env::env_int;
17+
use rsky_common::env::{env_bool, env_int};
1818
use rsky_common::explicit_slurs::contains_explicit_slurs;
1919
use rsky_lexicon::app::bsky::embed::{Embeds, MediaUnion};
2020
use 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

Comments
 (0)