aboutsummaryrefslogtreecommitdiff
path: root/src/db
diff options
context:
space:
mode:
Diffstat (limited to 'src/db')
-rw-r--r--src/db/mod.rs21
1 files changed, 21 insertions, 0 deletions
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<Meme> {
.map_err(Error::from)
}
+pub fn rand_silent_meme(conn: &PgConnection) -> Result<Meme> {
+ use rand::{thread_rng, seq::SliceRandom};
+ use failure::err_msg;
+ use std::ops::Try;
+
+ let ids: Vec<i32> = 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::<Meme>(conn)
+ .map_err(Error::from)
+}
+
#[derive(Debug, Copy, Clone)]
pub struct Stats {
pub memes_overall: usize,