archivist-core 0.2.0

Platform-neutral core for the FicHub companion bot: FicHub REST API client, search/recommendation/download command logic, intent classification, pagination cache, and the PlatformMessage IR. Shared by every platform adapter (Discord, Telegram, Matrix, Slack, IRC, fediverse, CLI, web).
Documentation
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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
//! Lemmy/Reddthat community monitor.
//!
//! Watches EVERY post + comment in a community (e.g. `c/fanfiction` on
//! reddthat.com), not just mentions. Fanfiction URLs get a metadata reply from
//! the bot; text that reads like a request ("looking for a fic where...") gets
//! an `/ask` search reply. State (last seen post/comment ids) is kept in Redis
//! under `archivist:lemmy:*` so the monitor is idempotent across restarts.
//!
//! Works against any Lemmy instance's public JSON API (`/api/v3/...`); no
//! websocket needed for polling.
// Serde needs these fields present to deserialize the API payloads, even
// though the monitor only reads a few of them.
#![allow(dead_code)]

use serde::Deserialize;

use crate::cache::PageCache;
use crate::config::BotConfig;
use crate::core::PlatformMessage;
use crate::dispatch::{do_ask, do_metadata, CoreCtx};
use crate::error::Result;
use crate::util::{extract_fanfic_url, normalize_url};

#[derive(Debug, Clone, Deserialize)]
struct LoginResponse {
    jwt: String,
}

#[derive(Debug, Clone, Deserialize)]
struct CommunityResponse {
    community_view: CommunityView,
}

#[derive(Debug, Clone, Deserialize)]
struct CommunityView {
    community: Community,
}

#[derive(Debug, Clone, Deserialize)]
struct Community {
    id: i64,
    name: String,
}

#[derive(Debug, Clone, Deserialize)]
struct PostsResponse {
    posts: Vec<PostView>,
}

#[derive(Debug, Clone, Deserialize)]
struct PostView {
    post: Post,
    creator: Person,
}

#[derive(Debug, Clone, Deserialize)]
struct Post {
    id: i64,
    name: String,
    body: Option<String>,
    url: Option<String>,
    creator_id: i64,
    published: String,
    community_id: i64,
}

#[derive(Debug, Clone, Deserialize)]
struct Person {
    name: String,
    // present in post views
}

#[derive(Debug, Clone, Deserialize)]
struct CommentsResponse {
    comments: Vec<CommentView>,
}

#[derive(Debug, Clone, Deserialize)]
struct CommentView {
    comment: Comment,
    creator: CommentPerson,
}

#[derive(Debug, Clone, Deserialize)]
struct Comment {
    id: i64,
    content: String,
    creator_id: i64,
    published: String,
}

#[derive(Debug, Clone, Deserialize)]
struct CommentPerson {
    name: String,
}

/// Redis key prefix for monitor state.
const STATE_PREFIX: &str = "archivist:lemmy";

/// Run the monitor loop forever (spawned as a background task by `main`).
/// Polls the community's new posts + comments; replies to anything that has a
/// fanfiction URL or reads like a fic request. Best-effort: every failure is
/// logged and the loop continues.
pub async fn run_monitor(ctx: CoreCtx) {
    let config = ctx.config.clone();
    if !config.lemmy_enabled {
        tracing::info!("Lemmy monitor disabled (FANFIC_ARCHIVIST_LEMMY_ENABLED=0)");
        return;
    }
    if config.lemmy_username.is_empty() || config.lemmy_password.is_empty() {
        tracing::error!("Lemmy monitor enabled but LEMMY_USERNAME/LEMMY_PASSWORD are empty — monitor will not run");
        return;
    }
    tracing::info!(
        "Lemmy monitor starting: {}/c/{} (poll {}s)",
        config.lemmy_url,
        config.lemmy_community,
        config.lemmy_poll_secs
    );

    let http = reqwest::Client::new();
    let jwt = match login(&config, &http).await {
        Ok(jwt) => jwt,
        Err(e) => {
            tracing::error!("Lemmy login failed: {e}");
            return;
        }
    };
    tracing::info!("Lemmy login OK (jwt len {})", jwt.len());

    let community_id = match resolve_community(&config, &http, &jwt).await {
        Ok(id) => id,
        Err(e) => {
            tracing::error!("Lemmy community resolve failed: {e}");
            return;
        }
    };
    tracing::info!("Monitoring community id {community_id}");

    let mut last_post = last_seen(&ctx.cache, "post").await;
    let mut last_comment = last_seen(&ctx.cache, "comment").await;

    let mut interval = tokio::time::interval(std::time::Duration::from_secs(config.lemmy_poll_secs));
    interval.tick().await; // first tick immediate

    loop {
        interval.tick().await;
        // Posts.
        match fetch_posts(&config, &http, &jwt, community_id, 1).await {
            Ok(posts) => {
                for pv in posts {
                    if pv.post.id <= last_post {
                        continue;
                    }
                    last_post = pv.post.id;
                    let text = format!(
                        "{}\n{}",
                        pv.post.name,
                        pv.post.body.clone().unwrap_or_default()
                    );
                    if let Some(url) = extract_fanfic_url(&text) {
                        let url = normalize_url(&url);
                        let msg = match do_metadata(&ctx, &url).await {
                            Ok(m) => m,
                            Err(e) => {
                                tracing::warn!("post {} metadata failed: {e}", pv.post.id);
                                continue;
                            }
                        };
                        let reply = render_reply(&msg);
                        if let Err(e) = reply_to_post(&config, &http, &jwt, pv.post.id, &reply).await
                        {
                            tracing::warn!("reply to post {} failed: {e}", pv.post.id);
                        } else {
                            tracing::info!("replied to post {} (fic url)", pv.post.id);
                        }
                    } else if looks_like_request(&text) {
                        let query = text.trim().to_string();
                        let msg = match do_ask(&ctx, 0, &query, None).await {
                            Ok(m) => m,
                            Err(e) => {
                                tracing::warn!("post {} ask failed: {e}", pv.post.id);
                                continue;
                            }
                        };
                        let reply = render_reply(&msg);
                        if let Err(e) = reply_to_post(&config, &http, &jwt, pv.post.id, &reply).await
                        {
                            tracing::warn!("reply to post {} failed: {e}", pv.post.id);
                        } else {
                            tracing::info!("replied to post {} (request)", pv.post.id);
                        }
                    }
                }
                let _ = ctx
                    .cache
                    .cache_raw(&format!("{STATE_PREFIX}:post"), 60 * 60 * 24 * 7, &serde_json::json!(last_post))
                    .await;
            }
            Err(e) => tracing::warn!("fetch posts failed: {e}"),
        }
        // Comments.
        match fetch_comments(&config, &http, &jwt, community_id, 1).await {
            Ok(comments) => {
                for cv in comments {
                    if cv.comment.id <= last_comment {
                        continue;
                    }
                    last_comment = cv.comment.id;
                    let text = cv.comment.content.clone();
                    if let Some(url) = extract_fanfic_url(&text) {
                        let url = normalize_url(&url);
                        let msg = match do_metadata(&ctx, &url).await {
                            Ok(m) => m,
                            Err(e) => {
                                tracing::warn!("comment {} metadata failed: {e}", cv.comment.id);
                                continue;
                            }
                        };
                        let reply = render_reply(&msg);
                        if let Err(e) =
                            reply_to_comment(&config, &http, &jwt, cv.comment.id, &reply).await
                        {
                            tracing::warn!("reply to comment {} failed: {e}", cv.comment.id);
                        } else {
                            tracing::info!("replied to comment {} (fic url)", cv.comment.id);
                        }
                    } else if looks_like_request(&text) {
                        let query = text.trim().to_string();
                        let msg = match do_ask(&ctx, 0, &query, None).await {
                            Ok(m) => m,
                            Err(e) => {
                                tracing::warn!("comment {} ask failed: {e}", cv.comment.id);
                                continue;
                            }
                        };
                        let reply = render_reply(&msg);
                        if let Err(e) =
                            reply_to_comment(&config, &http, &jwt, cv.comment.id, &reply).await
                        {
                            tracing::warn!("reply to comment {} failed: {e}", cv.comment.id);
                        } else {
                            tracing::info!("replied to comment {} (request)", cv.comment.id);
                        }
                    }
                }
                let _ = ctx
                    .cache
                    .cache_raw(&format!("{STATE_PREFIX}:comment"), 60 * 60 * 24 * 7, &serde_json::json!(last_comment))
                    .await;
            }
            Err(e) => tracing::warn!("fetch comments failed: {e}"),
        }
    }
}

/// Render a `PlatformMessage` as plain text for a Lemmy reply (Lemmy supports
/// markdown, so rich items keep their text).
fn render_reply(msg: &PlatformMessage) -> String {
    match msg {
        PlatformMessage::Text(s) => s.clone(),
        PlatformMessage::Rich { header, items, .. } => {
            let mut out = String::new();
            if let Some(h) = header {
                out.push_str(h);
                out.push('\n');
            }
            for (i, item) in items.iter().enumerate() {
                if i > 0 {
                    out.push_str("\n---\n");
                }
                if let Some(t) = &item.title {
                    out.push_str(&format!("**{}**\n", t));
                }
                if let Some(u) = &item.url {
                    out.push_str(&format!("<{u}>\n"));
                }
                out.push_str(&item.body);
                out.push('\n');
                for (k, v) in &item.fields {
                    out.push_str(&format!("**{k}**: {v}\n"));
                }
                if let Some(f) = &item.footer {
                    out.push_str(&format!("*{f}*\n"));
                }
            }
            out
        }
        PlatformMessage::File { .. } => "*(file uploads are not supported on this platform)*".into(),
        PlatformMessage::Ephemeral(inner) => render_reply(inner),
    }
}

/// Very light heuristic: does this text read like a fic request?
fn looks_like_request(text: &str) -> bool {
    let lower = text.to_lowercase();
    let words = [
        "looking for", "find a fic", "fic where", "fic about", "recommend",
        "recommendations", "any fics", "suggest", "suggestions", "need a fic",
        "help me find", "trope", "request",
    ];
    // Must be reasonably long to avoid replying to random chatter.
    if text.trim().chars().count() < 25 {
        return false;
    }
    words.iter().any(|w| lower.contains(w))
}

async fn login(config: &BotConfig, http: &reqwest::Client) -> Result<String> {
    let url = format!("{}/api/v3/user/login", config.lemmy_url.trim_end_matches('/'));
    let resp: LoginResponse = http
        .post(&url)
        .json(&serde_json::json!({
            "username_or_email": config.lemmy_username,
            "password": config.lemmy_password,
        }))
        .send()
        .await?
        .error_for_status()?
        .json()
        .await?;
    Ok(resp.jwt)
}

async fn resolve_community(config: &BotConfig, http: &reqwest::Client, jwt: &str) -> Result<i64> {
    let url = format!(
        "{}/api/v3/community?name={}",
        config.lemmy_url.trim_end_matches('/'),
        config.lemmy_community
    );
    let resp: CommunityResponse = http
        .get(&url)
        .header("Authorization", format!("Bearer {jwt}"))
        .send()
        .await?
        .error_for_status()?
        .json()
        .await?;
    Ok(resp.community_view.community.id)
}

async fn fetch_posts(
    config: &BotConfig,
    http: &reqwest::Client,
    jwt: &str,
    community_id: i64,
    page: i64,
) -> Result<Vec<PostView>> {
    let url = format!(
        "{}/api/v3/post/list?community_id={}&page={}&limit=20&sort=New",
        config.lemmy_url.trim_end_matches('/'),
        community_id,
        page
    );
    let resp: PostsResponse = http
        .get(&url)
        .header("Authorization", format!("Bearer {jwt}"))
        .send()
        .await?
        .error_for_status()?
        .json()
        .await?;
    Ok(resp.posts)
}

async fn fetch_comments(
    config: &BotConfig,
    http: &reqwest::Client,
    jwt: &str,
    community_id: i64,
    page: i64,
) -> Result<Vec<CommentView>> {
    let url = format!(
        "{}/api/v3/comment/list?community_id={}&page={}&limit=20&sort=New&max_depth=1",
        config.lemmy_url.trim_end_matches('/'),
        community_id,
        page
    );
    let resp: CommentsResponse = http
        .get(&url)
        .header("Authorization", format!("Bearer {jwt}"))
        .send()
        .await?
        .error_for_status()?
        .json()
        .await?;
    Ok(resp.comments)
}

async fn reply_to_post(
    config: &BotConfig,
    http: &reqwest::Client,
    jwt: &str,
    post_id: i64,
    content: &str,
) -> Result<()> {
    let url = format!(
        "{}/api/v3/comment",
        config.lemmy_url.trim_end_matches('/')
    );
    http.post(&url)
        .header("Authorization", format!("Bearer {jwt}"))
        .json(&serde_json::json!({
            "content": content,
            "post_id": post_id,
            "form_id": None::<String>,
        }))
        .send()
        .await?
        .error_for_status()?;
    Ok(())
}

async fn reply_to_comment(
    config: &BotConfig,
    http: &reqwest::Client,
    jwt: &str,
    comment_id: i64,
    content: &str,
) -> Result<()> {
    let url = format!(
        "{}/api/v3/comment",
        config.lemmy_url.trim_end_matches('/')
    );
    http.post(&url)
        .header("Authorization", format!("Bearer {jwt}"))
        .json(&serde_json::json!({
            "content": content,
            "parent_id": comment_id,
            "form_id": None::<String>,
        }))
        .send()
        .await?
        .error_for_status()?;
    Ok(())
}

/// Last seen id for a kind ("post" | "comment"), from Redis (best-effort).
async fn last_seen(cache: &PageCache, kind: &str) -> i64 {
    match cache.cached_raw(&format!("{STATE_PREFIX}:{kind}")).await {
        Some(v) => v.as_i64().unwrap_or(0),
        None => 0,
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn looks_like_request_detects() {
        assert!(looks_like_request(
            "Looking for a fic where Harry is raised by wolves, complete, over 50k words"
        ));
        assert!(looks_like_request("Any recommendations for slow-burn enemies to lovers?"));
    }

    #[test]
    fn looks_like_request_ignores_chatter() {
        assert!(!looks_like_request("hi"));
        assert!(!looks_like_request("what do you guys think about the new chapter?"));
    }

    #[test]
    fn render_reply_rich() {
        let msg = PlatformMessage::Rich {
            header: Some("search: \"dark harry\"".into()),
            items: vec![crate::core::RichItem::new("body text")
                .title("A Fic")
                .url("https://x")
                .field("Words", "50k")],
            actions: vec![],
        };
        let out = render_reply(&msg);
        assert!(out.contains("**A Fic**"));
        assert!(out.contains("body text"));
        assert!(out.contains("**Words**: 50k"));
    }
}