diff --git a/Cargo.lock b/Cargo.lock index f499d3d..4a31e9f 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -29,12 +29,30 @@ version = "0.2.22" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "57b212daf2e827b1ccdd778b7eb48ba433e58cb0e4f27b43cd6b7a608425a014" +[[package]] +name = "app" +version = "0.1.0" +dependencies = [ + "bot", + "core", + "serde", + "serde_json", + "ureq", +] + [[package]] name = "base64" version = "0.23.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ac07cdecf99051d9a5238b80f35af32cdeba5b336e55d957b318b50137e18da5" +[[package]] +name = "bot" +version = "0.1.0" +dependencies = [ + "core", +] + [[package]] name = "bytes" version = "1.12.2" @@ -97,6 +115,14 @@ dependencies = [ "url", ] +[[package]] +name = "core" +version = "0.1.0" +dependencies = [ + "jieba-rs", + "unicode-normalization", +] + [[package]] name = "crc32fast" version = "1.5.6" @@ -811,32 +837,6 @@ dependencies = [ "syn", ] -[[package]] -name = "telefind-app" -version = "0.1.0" -dependencies = [ - "serde", - "serde_json", - "telefind-bot", - "telefind-core", - "ureq", -] - -[[package]] -name = "telefind-bot" -version = "0.1.0" -dependencies = [ - "telefind-core", -] - -[[package]] -name = "telefind-core" -version = "0.1.0" -dependencies = [ - "jieba-rs", - "unicode-normalization", -] - [[package]] name = "time" version = "0.3.55" diff --git a/Cargo.toml b/Cargo.toml index f89b0a1..388028d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,5 +1,5 @@ [workspace] -members = ["crates/telefind-core", "crates/telefind-bot", "crates/telefind-app"] +members = ["crates/core", "crates/bot", "crates/app"] resolver = "2" [workspace.package] diff --git a/README.md b/README.md index 368ac57..68bba9f 100644 --- a/README.md +++ b/README.md @@ -7,7 +7,7 @@ Rust keyword search for public Telegram channels, groups, bots and posts. The ex From the workspace root: ```sh -cargo run -p telefind-app -- query - '/search type:video 上海相机' <<'JSONL' +cargo run -p app -- query - '/search type:video 上海相机' <<'JSONL' {"op":"upsert","kind":"post","id":-100501,"message_id":42,"username":"example_feed","title":"相机入门.mp4","body":"上海 摄影","media":"video"} JSONL ``` @@ -19,7 +19,7 @@ This prints the reply as JSON, including a button with the URL `https://t.me/exa Supply the bot token through the environment variable `TELEGRAM_BOT_TOKEN` and run: ```sh -cargo run -p telefind-app -- bot events.jsonl +cargo run -p app -- bot events.jsonl ``` The input file is optional. The process identifies its username via `getMe`, then long-polls for incoming messages and sends search results with URL buttons. Type keywords in private chat or use `/search@YourBot keywords` in a group. In a public channel where the bot receives posts, incoming posts and edits update the in-memory index. For channels without a public username, protected posts and unsupported content, the bot skips indexing the message. Telegram does not send ordinary post-deletion notifications via this update stream. An upstream source can supply deletion events. If a reply reached Telegram but its HTTP acknowledgement was lost, retrying an update can send the reply twice. @@ -34,7 +34,7 @@ Pass an ordered UTF-8 JSONL stream in a file, or pass `-` to read standard input {"op":"delete","kind":"post","id":-100501,"message_id":42} ``` -The process keeps no database or history archive. Restarting loads whichever input the caller supplies. For a persistent or concurrent host, implement `telefind_core::index::IndexStore` and its snapshot reader, then use `Engine::new(store)`. Pass the resulting engine to `telefind_app::telegram::run` with a `telefind_app::telegram::Client` to reuse the same polling and reply logic. The JSONL reader `telefind_app::input::load` also accepts any implementation of `SearchIndex`. +The process keeps no database or history archive. Restarting loads whichever input the caller supplies. For a persistent or concurrent host, implement `search_core::index::IndexStore` and its snapshot reader, then use `Engine::new(store)`. Pass the resulting engine to `app::telegram::run` with a `app::telegram::Client` to reuse the same polling and reply logic. The JSONL reader `app::input::load` also accepts any implementation of `SearchIndex`. ## Verification diff --git a/crates/telefind-app/Cargo.toml b/crates/app/Cargo.toml similarity index 69% rename from crates/telefind-app/Cargo.toml rename to crates/app/Cargo.toml index 225fc1e..71efb05 100644 --- a/crates/telefind-app/Cargo.toml +++ b/crates/app/Cargo.toml @@ -1,5 +1,5 @@ [package] -name = "telefind-app" +name = "app" version.workspace = true edition.workspace = true default-run = "telefind" @@ -9,8 +9,8 @@ name = "telefind" path = "src/main.rs" [dependencies] -telefind-core = { path = "../telefind-core" } -telefind-bot = { path = "../telefind-bot" } +core = { path = "../core" } +bot = { path = "../bot" } ureq = { version = "3", features = ["json"] } serde = { version = "1", features = ["derive"] } serde_json = "1" diff --git a/crates/telefind-app/src/input.rs b/crates/app/src/input.rs similarity index 97% rename from crates/telefind-app/src/input.rs rename to crates/app/src/input.rs index 4851c46..1daa66c 100644 --- a/crates/telefind-app/src/input.rs +++ b/crates/app/src/input.rs @@ -1,7 +1,7 @@ //! Line-delimited public document changes supplied by the caller. +use search_core::{Document, DocumentId, Media, PublicName, SearchIndex}; use serde::Deserialize; use std::{error::Error, io::BufRead}; -use telefind_core::{Document, DocumentId, Media, PublicName, SearchIndex}; #[derive(Deserialize)] #[serde(rename_all = "lowercase")] diff --git a/crates/telefind-app/src/lib.rs b/crates/app/src/lib.rs similarity index 100% rename from crates/telefind-app/src/lib.rs rename to crates/app/src/lib.rs diff --git a/crates/telefind-app/src/main.rs b/crates/app/src/main.rs similarity index 91% rename from crates/telefind-app/src/main.rs rename to crates/app/src/main.rs index 3d10ae3..7af32d2 100644 --- a/crates/telefind-app/src/main.rs +++ b/crates/app/src/main.rs @@ -1,3 +1,5 @@ +use app::{input, telegram}; +use search_core::MemoryIndex; use std::{ env, error::Error, @@ -5,8 +7,6 @@ use std::{ io::{self, BufReader}, process::ExitCode, }; -use telefind_app::{input, telegram}; -use telefind_core::MemoryIndex; const USAGE: &str = concat!( "telefind bot [events.jsonl|-]\n", @@ -36,7 +36,7 @@ fn run() -> Result<(), Box> { } Some("query") if args.len() >= 3 => { load(&args[1], &mut index)?; - let reply = telefind_bot::handle(&index, "telefind", &args[2..].join(" "))?; + let reply = bot::handle(&index, "telefind", &args[2..].join(" "))?; println!("{}", serde_json::to_string(&telegram::render(reply))?); Ok(()) } diff --git a/crates/telefind-app/src/telegram/client.rs b/crates/app/src/telegram/client.rs similarity index 100% rename from crates/telefind-app/src/telegram/client.rs rename to crates/app/src/telegram/client.rs diff --git a/crates/telefind-app/src/telegram/mod.rs b/crates/app/src/telegram/mod.rs similarity index 98% rename from crates/telefind-app/src/telegram/mod.rs rename to crates/app/src/telegram/mod.rs index b5aa855..cb28111 100644 --- a/crates/telefind-app/src/telegram/mod.rs +++ b/crates/app/src/telegram/mod.rs @@ -6,10 +6,10 @@ use self::{ update::{dispatch, Update}, }; pub use client::Client; +use search_core::SearchIndex; use serde::Deserialize; use serde_json::{json, Value}; use std::{error::Error, thread, time::Duration}; -use telefind_core::SearchIndex; pub use update::render; fn with_retry( diff --git a/crates/telefind-app/src/telegram/queue.rs b/crates/app/src/telegram/queue.rs similarity index 99% rename from crates/telefind-app/src/telegram/queue.rs rename to crates/app/src/telegram/queue.rs index 8686107..61a3232 100644 --- a/crates/telefind-app/src/telegram/queue.rs +++ b/crates/app/src/telegram/queue.rs @@ -1,8 +1,8 @@ //! Broker-neutral gateway and leased workers. Each adapter is scoped to one bot. use super::{client::Failure, process, receive, run_loop, Client, Update}; +use search_core::SearchIndex; use serde::{Deserialize, Serialize}; use std::{error::Error, time::Duration}; -use telefind_core::SearchIndex; #[derive(Clone, Copy, Debug, PartialEq, Eq)] pub enum Lane { diff --git a/crates/telefind-app/src/telegram/tests.rs b/crates/app/src/telegram/tests.rs similarity index 98% rename from crates/telefind-app/src/telegram/tests.rs rename to crates/app/src/telegram/tests.rs index 7940562..282dcd1 100644 --- a/crates/telefind-app/src/telegram/tests.rs +++ b/crates/app/src/telegram/tests.rs @@ -1,9 +1,9 @@ use super::*; +use search_core::MemoryIndex; use std::{ io::{BufRead, BufReader, Read, Write}, net::TcpListener, }; -use telefind_core::MemoryIndex; fn respond(server: &TcpListener, reply: impl FnOnce(&str, Value) -> (u16, String)) { let (mut socket, _) = server.accept().unwrap(); @@ -105,7 +105,7 @@ fn nonpublic_and_protected_updates_do_not_add_searchable_posts() { } assert_eq!( index - .search(&telefind_core::Query::new("private")) + .search(&search_core::Query::new("private")) .unwrap() .total, 0 diff --git a/crates/telefind-app/src/telegram/tests/queued.rs b/crates/app/src/telegram/tests/queued.rs similarity index 98% rename from crates/telefind-app/src/telegram/tests/queued.rs rename to crates/app/src/telegram/tests/queued.rs index 13c2530..ee084a3 100644 --- a/crates/telefind-app/src/telegram/tests/queued.rs +++ b/crates/app/src/telegram/tests/queued.rs @@ -108,7 +108,7 @@ fn publish_failure_keeps_offset_then_workers_replay_and_defer_flooded_reply() { assert_eq!(step(&mut queries), Outcome::Completed); assert_eq!( index - .search(&telefind_core::Query::new("tutorial")) + .search(&search_core::Query::new("tutorial")) .unwrap() .total, 0 diff --git a/crates/telefind-app/src/telegram/update.rs b/crates/app/src/telegram/update.rs similarity index 98% rename from crates/telefind-app/src/telegram/update.rs rename to crates/app/src/telegram/update.rs index fc1c6b2..6ffd888 100644 --- a/crates/telefind-app/src/telegram/update.rs +++ b/crates/app/src/telegram/update.rs @@ -1,7 +1,7 @@ +use bot::{handle, Reply}; +use search_core::{Document, DocumentId, Media, PublicName, SearchIndex}; use serde::{Deserialize, Serialize}; use serde_json::{json, Value}; -use telefind_bot::{handle, Reply}; -use telefind_core::{Document, DocumentId, Media, PublicName, SearchIndex}; #[derive(Deserialize, Serialize)] pub struct Update { diff --git a/crates/telefind-app/tests/cli.rs b/crates/app/tests/cli.rs similarity index 100% rename from crates/telefind-app/tests/cli.rs rename to crates/app/tests/cli.rs diff --git a/crates/telefind-bot/Cargo.toml b/crates/bot/Cargo.toml similarity index 52% rename from crates/telefind-bot/Cargo.toml rename to crates/bot/Cargo.toml index f5fe7b0..7c0cba8 100644 --- a/crates/telefind-bot/Cargo.toml +++ b/crates/bot/Cargo.toml @@ -1,7 +1,7 @@ [package] -name = "telefind-bot" +name = "bot" version.workspace = true edition.workspace = true [dependencies] -telefind-core = { path = "../telefind-core" } +core = { path = "../core" } diff --git a/crates/telefind-bot/src/command.rs b/crates/bot/src/command.rs similarity index 98% rename from crates/telefind-bot/src/command.rs rename to crates/bot/src/command.rs index c394f37..6d01ffd 100644 --- a/crates/telefind-bot/src/command.rs +++ b/crates/bot/src/command.rs @@ -1,4 +1,4 @@ -use telefind_core::{Kind, Media, Query}; +use search_core::{Kind, Media, Query}; pub(crate) enum Input<'a> { Ignore, Help, diff --git a/crates/telefind-bot/src/lib.rs b/crates/bot/src/lib.rs similarity index 97% rename from crates/telefind-bot/src/lib.rs rename to crates/bot/src/lib.rs index 0533f55..103cca0 100644 --- a/crates/telefind-bot/src/lib.rs +++ b/crates/bot/src/lib.rs @@ -1,7 +1,7 @@ //! Transport-independent Telegram search interaction. The host supplies //! incoming text and sends the returned plain-text labels and URL buttons. mod command; -use telefind_core::{Document, Kind, SearchIndex}; +use search_core::{Document, Kind, SearchIndex}; #[derive(Debug, PartialEq)] pub struct ResultLink { diff --git a/crates/telefind-bot/tests/commands.rs b/crates/bot/tests/commands.rs similarity index 94% rename from crates/telefind-bot/tests/commands.rs rename to crates/bot/tests/commands.rs index 2f863b3..51151dd 100644 --- a/crates/telefind-bot/tests/commands.rs +++ b/crates/bot/tests/commands.rs @@ -1,5 +1,5 @@ -use telefind_bot::{handle, Reply}; -use telefind_core::{Document, DocumentId, MemoryIndex, PublicName, SearchIndex}; +use bot::{handle, Reply}; +use search_core::{Document, DocumentId, MemoryIndex, PublicName, SearchIndex}; #[test] fn plain_and_addressed_search_return_public_post_link() { @@ -72,7 +72,7 @@ fn paging_is_stateless_and_rejects_invalid_numbers() { #[test] fn type_filter_is_available_in_search_and_page_commands() { let mut index = MemoryIndex::default(); - for (id, media) in [(1, None), (2, Some(telefind_core::Media::Video))] { + for (id, media) in [(1, None), (2, Some(search_core::Media::Video))] { index .upsert(Document { id: DocumentId::Post { @@ -117,7 +117,7 @@ fn scoped_media_search_keeps_quoted_phrase_and_original_link() { username: PublicName::new("my_feed").unwrap(), title: String::new(), body: body.into(), - media: Some(telefind_core::Media::Video), + media: Some(search_core::Media::Video), }) .unwrap(); } diff --git a/crates/telefind-core/Cargo.toml b/crates/core/Cargo.toml similarity index 74% rename from crates/telefind-core/Cargo.toml rename to crates/core/Cargo.toml index 58b77d6..2329c57 100644 --- a/crates/telefind-core/Cargo.toml +++ b/crates/core/Cargo.toml @@ -1,8 +1,11 @@ [package] -name = "telefind-core" +name = "core" version.workspace = true edition.workspace = true +[lib] +name = "search_core" + [dependencies] jieba-rs = "0.8" unicode-normalization = "0.1" diff --git a/crates/telefind-core/src/document.rs b/crates/core/src/document.rs similarity index 100% rename from crates/telefind-core/src/document.rs rename to crates/core/src/document.rs diff --git a/crates/telefind-core/src/index/memory.rs b/crates/core/src/index/memory.rs similarity index 100% rename from crates/telefind-core/src/index/memory.rs rename to crates/core/src/index/memory.rs diff --git a/crates/telefind-core/src/index/mod.rs b/crates/core/src/index/mod.rs similarity index 100% rename from crates/telefind-core/src/index/mod.rs rename to crates/core/src/index/mod.rs diff --git a/crates/telefind-core/src/index/store.rs b/crates/core/src/index/store.rs similarity index 100% rename from crates/telefind-core/src/index/store.rs rename to crates/core/src/index/store.rs diff --git a/crates/telefind-core/src/lib.rs b/crates/core/src/lib.rs similarity index 100% rename from crates/telefind-core/src/lib.rs rename to crates/core/src/lib.rs diff --git a/crates/telefind-core/src/search/analysis.rs b/crates/core/src/search/analysis.rs similarity index 100% rename from crates/telefind-core/src/search/analysis.rs rename to crates/core/src/search/analysis.rs diff --git a/crates/telefind-core/src/search/mod.rs b/crates/core/src/search/mod.rs similarity index 100% rename from crates/telefind-core/src/search/mod.rs rename to crates/core/src/search/mod.rs diff --git a/crates/telefind-core/src/search/rank.rs b/crates/core/src/search/rank.rs similarity index 100% rename from crates/telefind-core/src/search/rank.rs rename to crates/core/src/search/rank.rs diff --git a/crates/telefind-core/src/search/retrieve.rs b/crates/core/src/search/retrieve.rs similarity index 100% rename from crates/telefind-core/src/search/retrieve.rs rename to crates/core/src/search/retrieve.rs diff --git a/crates/telefind-core/src/search/scope.rs b/crates/core/src/search/scope.rs similarity index 100% rename from crates/telefind-core/src/search/scope.rs rename to crates/core/src/search/scope.rs diff --git a/crates/telefind-core/tests/retrieval.rs b/crates/core/tests/retrieval.rs similarity index 98% rename from crates/telefind-core/tests/retrieval.rs rename to crates/core/tests/retrieval.rs index 95bd957..eaccf0b 100644 --- a/crates/telefind-core/tests/retrieval.rs +++ b/crates/core/tests/retrieval.rs @@ -1,6 +1,4 @@ -use telefind_core::{ - Document, DocumentId, Kind, Media, MemoryIndex, PublicName, Query, SearchIndex, -}; +use search_core::{Document, DocumentId, Kind, Media, MemoryIndex, PublicName, Query, SearchIndex}; fn post(chat: i64, message: u32, title: &str, body: &str) -> Document { Document {