Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 74 additions & 9 deletions crates/openplotva-app/src/image_jobs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ use openplotva_telegram::{
build_photo_message_method, build_text_message_methods, ensure_telegram_safe_text,
escape_telegram_html_text, execute_telegram_method, strip_telegram_html,
};
use rand::{Rng, RngExt};
use rand::{Rng, RngExt, seq::SliceRandom};
use serde::Serialize;
use serde_json::{Value, json};
use time::OffsetDateTime;
Expand Down Expand Up @@ -109,8 +109,18 @@ pub const MEDIA_JOB_TELEGRAM_TIMEOUT: StdDuration = StdDuration::from_secs(30);
pub const STICKER_DOWN_FILE_ID: &str =
"CAACAgIAAxkBAAEeROBkDjnz1i3WxxyNLBgWA_IKyjxbnQACuioAAqPicEh1C96_WINTHS8E";
pub const NSFW_BLOCKED_MESSAGE_TEXT: &str = "Ваш запрос заблокирован, так как содержит неприемлемый контент. Попробуйте переформулировать запрос.";
pub const IMAGE_PLACEHOLDER_FILE_ID: &str =
"AgACAgIAAxkBAAFhmg5oDV5-lLcooLSE8nKFLlF768nEygAC6O8xG2uvaUjfFg40SWg2rgEAAwIAA3kAAzYE";
/// Loader pictures uploaded by this bot; each placeholder frame shows a random one.
pub const IMAGE_PLACEHOLDER_FILE_IDS: [&str; 9] = [
"AgACAgIAAxUHaxQQrzFkhSV8IoQVmd460nKeLIwAAugkaxuAc6BIYq6lcw9lQY4BAAMCAAN5AAM9BA",
"AgACAgIAAxUHaxQQr81VCgpl8NmR1l76qqLuiY0AAuokaxuAc6BICyWlqCwO6goBAAMCAAN5AAM9BA",
"AgACAgIAAxUHaxQQr27-vZGaTWoF7xvEzqmw_EcAAuckaxuAc6BIg4PCrHzWbLgBAAMCAAN5AAM9BA",
"AgACAgIAAxUHaxQQr_8eo8x34D7bl9B9WCSbdvgAAuskaxuAc6BIckkjDWiCvzcBAAMCAAN5AAM9BA",
"AgACAgIAAxUHaxQQr97bOIHvLLOBZsmKNf9lJVwAAukkaxuAc6BIJ_hT-Ln4aOgBAAMCAAN5AAM9BA",
"AgACAgIAAxUHaxQQr_jJrXObcoiLA2vNkjeQbrQAAu8kaxuAc6BIM2HNeeH49ygBAAMCAAN5AAM9BA",
"AgACAgIAAxUHaxQQr3LBMRwp15csHO5GTnRu-3oAAu4kaxuAc6BI8k0hY-lbZVoBAAMCAAN5AAM9BA",
"AgACAgIAAxUHaxQQr7BOHSRkH_YrpU2y26_ncWcAAuwkaxuAc6BIeLjoLqSkxk0BAAMCAAN5AAM9BA",
"AgACAgIAAxUHaxQQrxIMU5cq8bVInsIWhDKfge0AAu0kaxuAc6BIcCsz0ddjeAcBAAMCAAN5AAM9BA",
];
pub const IMAGE_JOB_POLL_INTERVAL: StdDuration = StdDuration::from_secs(1);
pub const IMAGE_JOB_WORKER_QUEUES: [&str; 2] = [IMAGE_VIP_QUEUE_NAME, IMAGE_REGULAR_QUEUE_NAME];
pub const IMAGE_VIP_JOB_WORKER_QUEUES: [&str; 1] = [IMAGE_VIP_QUEUE_NAME];
Expand Down Expand Up @@ -2813,13 +2823,15 @@ where
) -> ImageJobEffectFuture<'a, Result<Vec<i32>, String>> {
Box::pin(async move {
let count = count.clamp(1, TELEGRAM_MEDIA_GROUP_MAX_ITEMS);
let file_ids = random_placeholder_file_ids(count);
let response = if count == 1 {
let request = placeholder_image_message_request(
chat_id,
message_id,
thread_id,
caption_text,
is_nsfw,
file_ids[0],
);
let method =
build_photo_message_method(&request).map_err(|error| error.to_string())?;
Expand All @@ -2833,7 +2845,7 @@ where
thread_id,
caption_text,
is_nsfw,
count,
&file_ids,
);
let method = build_media_group_message_method(&request)
.map_err(|error| error.to_string())?;
Expand Down Expand Up @@ -3004,19 +3016,27 @@ fn generated_image_message_request(
}
}

/// `count` loader file IDs in random order; distinct while the set lasts.
fn random_placeholder_file_ids(count: usize) -> Vec<&'static str> {
let mut file_ids = IMAGE_PLACEHOLDER_FILE_IDS;
file_ids.shuffle(&mut rand::rng());
file_ids.into_iter().cycle().take(count).collect()
}

fn placeholder_image_message_request(
chat_id: i64,
message_id: i32,
thread_id: Option<i32>,
caption_text: String,
is_nsfw: bool,
file_id: &str,
) -> PhotoMessageRequest {
let mut request = generated_image_message_request(
chat_id,
message_id,
thread_id,
caption_text,
PhotoSource::FileId(IMAGE_PLACEHOLDER_FILE_ID.to_owned()),
PhotoSource::FileId(file_id.to_owned()),
);
request.render_as = TELEGRAM_PARSE_MODE_HTML.to_owned();
request.has_spoiler = is_nsfw;
Expand All @@ -3029,7 +3049,7 @@ fn placeholder_image_media_group_request(
thread_id: Option<i32>,
caption_text: String,
is_nsfw: bool,
count: usize,
file_ids: &[&str],
) -> MediaGroupMessageRequest {
let message_thread_id = i64::from(thread_id.unwrap_or_default());
let is_topic_message = message_thread_id != 0;
Expand All @@ -3041,9 +3061,11 @@ fn placeholder_image_media_group_request(
},
message_thread_id,
disable_notification: false,
items: (0..count)
.map(|index| MediaGroupPhotoItem {
photo: PhotoSource::FileId(IMAGE_PLACEHOLDER_FILE_ID.to_owned()),
items: file_ids
.iter()
.enumerate()
.map(|(index, file_id)| MediaGroupPhotoItem {
photo: PhotoSource::FileId((*file_id).to_owned()),
caption: if index == 0 {
caption_text.clone()
} else {
Expand Down Expand Up @@ -10187,6 +10209,49 @@ mod tests {
);
}

#[test]
fn random_placeholder_file_ids_pick_distinct_loader_pictures() {
let pair = random_placeholder_file_ids(2);
assert_eq!(pair.len(), 2);
assert_ne!(pair[0], pair[1]);
assert!(
pair.iter()
.all(|id| IMAGE_PLACEHOLDER_FILE_IDS.contains(id))
);

let full = random_placeholder_file_ids(TELEGRAM_MEDIA_GROUP_MAX_ITEMS);
assert_eq!(full.len(), TELEGRAM_MEDIA_GROUP_MAX_ITEMS);
assert!(
IMAGE_PLACEHOLDER_FILE_IDS
.iter()
.all(|id| full.contains(id))
);
}

#[test]
fn placeholder_media_group_request_shows_one_picture_per_frame() {
let file_ids = [IMAGE_PLACEHOLDER_FILE_IDS[3], IMAGE_PLACEHOLDER_FILE_IDS[7]];
let request = placeholder_image_media_group_request(
-10042,
77,
None,
"<code>caption</code>".to_owned(),
false,
&file_ids,
);

let photos: Vec<_> = request.items.iter().map(|item| &item.photo).collect();
assert_eq!(
photos,
vec![
&PhotoSource::FileId(file_ids[0].to_owned()),
&PhotoSource::FileId(file_ids[1].to_owned()),
]
);
assert_eq!(request.items[0].caption, "<code>caption</code>");
assert!(request.items[1].caption.is_empty());
}

#[tokio::test]
async fn telegram_image_job_effects_replace_placeholder_uses_edit_media() {
let telegram = TelegramSenderStub::new(vec![Ok(TelegramOutboundResponse::Boolean(true))]);
Expand Down
Loading