1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
use crate::{
    checks::ENABLED_CHECK,
    client_data::{
        CacheStatsBuilder,
        CacheStatsProvider,
    },
    util::{
        LoadingReaction,
        TimedCache,
        TimedCacheEntry,
    },
    ClientDataKey,
};
use anyhow::Context as _;
use serenity::{
    builder::{
        CreateEmbed,
        CreateMessage,
    },
    framework::standard::{
        macros::command,
        Args,
        CommandResult,
    },
    model::prelude::*,
    prelude::*,
};
use std::sync::Arc;
use tracing::error;

#[derive(Clone, Debug)]
pub struct IqdbClient {
    client: iqdb::Client,
    search_cache: TimedCache<String, iqdb::SearchResults>,
}

impl IqdbClient {
    pub fn new() -> Self {
        Self {
            client: iqdb::Client::new(),
            search_cache: TimedCache::new(),
        }
    }

    /// Search for an image, with caching
    pub async fn search(
        &self,
        query: &str,
    ) -> anyhow::Result<Arc<TimedCacheEntry<iqdb::SearchResults>>> {
        if let Some(entry) = self.search_cache.get_if_fresh(query) {
            return Ok(entry);
        }

        let search_results = self
            .client
            .search(query)
            .await
            .context("failed to search for image")?;

        self.search_cache
            .insert(String::from(query), search_results);

        self.search_cache
            .get_if_fresh(query)
            .context("cache data expired")
    }
}

impl Default for IqdbClient {
    fn default() -> Self {
        Self::new()
    }
}

impl CacheStatsProvider for IqdbClient {
    fn publish_cache_stats(&self, cache_stats_builder: &mut CacheStatsBuilder) {
        cache_stats_builder.publish_stat("iqdb", "search_cache", self.search_cache.len() as f32);
    }
}

#[command]
#[description("Search IQDB for an image at a url")]
#[usage("<img_url>")]
#[example("https://konachan.com/image/5982d8946ae503351e960f097f84cd90/Konachan.com%20-%20330136%20animal%20nobody%20original%20signed%20yutaka_kana.jpg")]
#[checks(Enabled)]
#[min_args(1)]
#[max_args(1)]
#[bucket("default")]
async fn iqdb(ctx: &Context, msg: &Message, mut args: Args) -> CommandResult {
    let data_lock = ctx.data.read().await;
    let client_data = data_lock
        .get::<ClientDataKey>()
        .expect("missing client data");
    let client = client_data.iqdb_client.clone();
    drop(data_lock);

    let query = args.trimmed().current().expect("missing query");

    let mut loading = LoadingReaction::new(ctx.http.clone(), msg);

    match client
        .search(query)
        .await
        .context("failed to search for image")
    {
        Ok(data) => {
            let data = data.data();
            match data.best_match.as_ref() {
                Some(data) => {
                    let embed_builder = CreateEmbed::new()
                        .title("IQDB Best Match")
                        .image(data.image_url.as_str())
                        .url(data.url.as_str())
                        .description(data.url.as_str());
                    let message_builder = CreateMessage::new().embed(embed_builder);
                    msg.channel_id
                        .send_message(&ctx.http, message_builder)
                        .await?;

                    loading.send_ok();
                }
                None => {
                    msg.channel_id
                        .say(&ctx.http, format!("No results on iqdb for \"{query}\"",))
                        .await?;
                }
            }
        }
        Err(error) => {
            error!("{error:?}");
            msg.channel_id.say(&ctx.http, format!("{error:?}")).await?;
        }
    }

    Ok(())
}