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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
use crate::{
    client_data::{
        CacheStatsBuilder,
        CacheStatsProvider,
    },
    ClientDataKey,
};
use anyhow::Context as _;
use crossbeam::queue::ArrayQueue;
use indexmap::set::IndexSet;
use parking_lot::RwLock;
use pikadick_slash_framework::FromOptions;
use rand::Rng;
use serenity::builder::{
    CreateInteractionResponse,
    CreateInteractionResponseMessage,
};
use std::{
    str::FromStr,
    sync::Arc,
};
use tracing::error;
use url::Url;

/// Max images per single request
const BUFFER_SIZE: u8 = 100;

#[derive(Debug)]
struct NsfwArgParseError;

struct NsfwArg;

impl FromStr for NsfwArg {
    type Err = NsfwArgParseError;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        if s == "nsfw" {
            Ok(NsfwArg)
        } else {
            Err(NsfwArgParseError)
        }
    }
}

/// A nekos cache
#[derive(Clone, Debug)]
pub struct Cache(Arc<CacheInner>);

impl Cache {
    /// Make a new cache
    pub fn new() -> Self {
        Self(Arc::new(CacheInner {
            primary: ArrayQueue::new(BUFFER_SIZE.into()),
            secondary: RwLock::new(IndexSet::new()),
        }))
    }

    /// Get the size of the primary cache
    pub fn primary_len(&self) -> usize {
        self.0.primary.len()
    }

    /// Get the size of the secondary cache
    pub fn secondary_len(&self) -> usize {
        self.0.secondary.read().len()
    }

    /// Check if the primary cache is emoty
    pub fn primary_is_empty(&self) -> bool {
        self.0.primary.is_empty()
    }

    /// Check if the secondary cache is empty
    pub fn secondary_is_empty(&self) -> bool {
        self.0.secondary.read().is_empty()
    }

    /// Add a url to the cache
    pub fn add(&self, uri: Url) {
        let _ = self.0.primary.push(uri.clone()).is_ok();
        self.0.secondary.write().insert(uri);
    }

    /// Add many urls to the cache
    pub fn add_many<I>(&self, iter: I)
    where
        I: Iterator<Item = Url>,
    {
        let mut secondary = self.0.secondary.write();
        for uri in iter {
            let _ = self.0.primary.push(uri.clone()).is_ok();
            secondary.insert(uri);
        }
    }

    /// Get a random url
    pub async fn get_rand(&self) -> Option<Url> {
        if let Some(uri) = self.0.primary.pop() {
            Some(uri)
        } else {
            let lock = self.0.secondary.read();

            if lock.is_empty() {
                return None;
            }

            let mut rng = rand::thread_rng();
            let index = rng.gen_range(0..lock.len());

            lock.get_index(index).cloned()
        }
    }
}

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

/// The inner cache data
#[derive(Debug)]
struct CacheInner {
    primary: ArrayQueue<Url>,
    secondary: RwLock<IndexSet<Url>>,
}

/// The nekos client
#[derive(Clone, Debug)]
pub struct NekosClient {
    client: nekos::Client,

    cache: Cache,
    nsfw_cache: Cache,
}

impl NekosClient {
    /// Make a new nekos client
    pub fn new() -> Self {
        NekosClient {
            client: Default::default(),
            cache: Cache::new(),
            nsfw_cache: Cache::new(),
        }
    }

    /// Get a cache
    fn get_cache(&self, nsfw: bool) -> &Cache {
        if nsfw {
            &self.nsfw_cache
        } else {
            &self.cache
        }
    }

    /// Repopulate a cache
    pub async fn populate(&self, nsfw: bool) -> anyhow::Result<()> {
        let cache = self.get_cache(nsfw);
        let image_list = self
            .client
            .get_random(Some(nsfw), BUFFER_SIZE)
            .await
            .context("failed to get random nekos image list")?;

        cache.add_many(
            image_list
                .images
                .iter()
                .filter_map(|img| img.get_url().ok()),
        );

        Ok(())
    }

    /// Get a random nekos image
    pub async fn get_rand(&self, nsfw: bool) -> anyhow::Result<Url> {
        let cache = self.get_cache(nsfw);

        if cache.primary_is_empty() {
            let self_clone = self.clone();
            tokio::spawn(async move {
                // Best effort here, we can always fall back to secondary cache
                if let Err(e) = self_clone
                    .populate(nsfw)
                    .await
                    .context("failed to get new nekos data")
                {
                    error!("{:?}", e);
                }
            });
        }

        if cache.secondary_is_empty() {
            self.populate(nsfw)
                .await
                .context("failed to populate caches")?;
        }

        cache
            .get_rand()
            .await
            .context("both primary and secondary caches are empty")
    }
}

impl CacheStatsProvider for NekosClient {
    fn publish_cache_stats(&self, cache_stats_builder: &mut CacheStatsBuilder) {
        let cache = self.get_cache(false);
        let nsfw_cache = self.get_cache(true);

        cache_stats_builder.publish_stat("nekos", "primary_cache", cache.primary_len() as f32);
        cache_stats_builder.publish_stat(
            "nekos",
            "primary_nsfw_cache",
            nsfw_cache.primary_len() as f32,
        );
        cache_stats_builder.publish_stat("nekos", "secondary_cache", cache.secondary_len() as f32);
        cache_stats_builder.publish_stat(
            "nekos",
            "secondary_nsfw_cache",
            nsfw_cache.secondary_len() as f32,
        );
    }
}

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

// TODO:
// Consider adding https://nekos.life/api/v2/endpoints

/// Arguments for the nekos command
#[derive(Debug, Copy, Clone, FromOptions)]
pub struct NekosArguments {
    /// Whether the command should look for nsfw pictures
    pub nsfw: Option<bool>,
}

/// Make a nekos slash command
pub fn create_slash_command() -> anyhow::Result<pikadick_slash_framework::Command> {
    pikadick_slash_framework::CommandBuilder::new()
        .name("nekos")
        .description("Get a random neko")
        .argument(
            pikadick_slash_framework::ArgumentParamBuilder::new()
                .name("nsfw")
                .kind(pikadick_slash_framework::ArgumentKind::Boolean)
                .description("Whether this should use nsfw results")
                .build()?,
        )
        .on_process(|ctx, interaction, args: NekosArguments| async move {
            let data_lock = ctx.data.read().await;
            let client_data = data_lock
                .get::<ClientDataKey>()
                .expect("failed to get client data");
            let nekos_client = client_data.nekos_client.clone();
            drop(data_lock);

            let content = match nekos_client
                .get_rand(args.nsfw.unwrap_or(false))
                .await
                .context("failed to repopulate nekos caches")
            {
                Ok(url) => url.into(),
                Err(error) => {
                    error!("{error:?}");
                    format!("{error:?}")
                }
            };

            let message_builder = CreateInteractionResponseMessage::new().content(content);
            let response = CreateInteractionResponse::Message(message_builder);

            interaction.create_response(&ctx.http, response).await?;

            Ok(())
        })
        .build()
        .context("failed to build command")
}