From a541cc28c435f039b977087cf7d07648a160633b Mon Sep 17 00:00:00 2001 From: Nathan Perry Date: Sat, 9 Mar 2019 17:09:57 -0500 Subject: add silentmeme command --- src/db/mod.rs | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) (limited to 'src/db/mod.rs') diff --git a/src/db/mod.rs b/src/db/mod.rs index b3861d2..191b9e9 100644 --- a/src/db/mod.rs +++ b/src/db/mod.rs @@ -158,6 +158,27 @@ pub fn rand_audio_meme(conn: &PgConnection) -> Result { .map_err(Error::from) } +pub fn rand_silent_meme(conn: &PgConnection) -> Result { + use rand::{thread_rng, seq::SliceRandom}; + use failure::err_msg; + use std::ops::Try; + + let ids: Vec = memes::table + .select(memes::id) + .filter(memes::audio_id.is_null()) + .load(conn) + .map_err(Error::from)?; + + let id = ids.choose(&mut thread_rng()) + .into_result() + .map_err(|_| err_msg("couldn't load audio meme"))?; + + memes::table + .find(id) + .first::(conn) + .map_err(Error::from) +} + #[derive(Debug, Copy, Clone)] pub struct Stats { pub memes_overall: usize, -- cgit v1.3.1