Run Telegram polling, document ingestion and search replies from a Rust executable
This commit is contained in:
@@ -0,0 +1,108 @@
|
||||
//! Line-delimited public document changes supplied by the caller.
|
||||
use serde::Deserialize;
|
||||
use std::{error::Error, io::BufRead};
|
||||
use telefind_core::{Document, DocumentId, Media, PublicName, SearchIndex};
|
||||
|
||||
#[derive(Deserialize)]
|
||||
#[serde(rename_all = "lowercase")]
|
||||
enum Kind {
|
||||
Channel,
|
||||
Group,
|
||||
Bot,
|
||||
Post,
|
||||
}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
#[serde(rename_all = "lowercase")]
|
||||
enum Attachment {
|
||||
Photo,
|
||||
Video,
|
||||
Audio,
|
||||
File,
|
||||
}
|
||||
impl From<Attachment> for Media {
|
||||
fn from(media: Attachment) -> Self {
|
||||
match media {
|
||||
Attachment::Photo => Media::Photo,
|
||||
Attachment::Video => Media::Video,
|
||||
Attachment::Audio => Media::Audio,
|
||||
Attachment::File => Media::File,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
struct Key {
|
||||
kind: Kind,
|
||||
id: i64,
|
||||
message_id: Option<u32>,
|
||||
}
|
||||
impl Key {
|
||||
fn resolve(&self) -> Result<DocumentId, &'static str> {
|
||||
match (&self.kind, self.message_id) {
|
||||
(Kind::Channel, None) => Ok(DocumentId::Channel(self.id)),
|
||||
(Kind::Group, None) => Ok(DocumentId::Group(self.id)),
|
||||
(Kind::Bot, None) => Ok(DocumentId::Bot(self.id)),
|
||||
(Kind::Post, Some(n)) if n > 0 => Ok(DocumentId::Post {
|
||||
chat: self.id,
|
||||
message: n,
|
||||
}),
|
||||
_ => Err("message_id must be positive for a post and absent for a directory record"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
#[serde(tag = "op", rename_all = "lowercase")]
|
||||
enum Change {
|
||||
Upsert {
|
||||
#[serde(flatten)]
|
||||
key: Key,
|
||||
username: String,
|
||||
#[serde(default)]
|
||||
title: String,
|
||||
#[serde(default)]
|
||||
body: String,
|
||||
media: Option<Attachment>,
|
||||
},
|
||||
Delete {
|
||||
#[serde(flatten)]
|
||||
key: Key,
|
||||
},
|
||||
}
|
||||
|
||||
pub fn load<R: BufRead, S: SearchIndex>(reader: R, index: &mut S) -> Result<(), Box<dyn Error>>
|
||||
where
|
||||
S::Error: Error + 'static,
|
||||
{
|
||||
for (number, line) in reader.lines().enumerate() {
|
||||
let line = line?;
|
||||
if line.trim().is_empty() {
|
||||
continue;
|
||||
}
|
||||
let context = |err: &str| format!("input line {}: {}", number + 1, err);
|
||||
let change: Change = serde_json::from_str(&line).map_err(|e| context(&e.to_string()))?;
|
||||
match change {
|
||||
Change::Delete { key } => {
|
||||
index.remove(key.resolve().map_err(context)?)?;
|
||||
}
|
||||
Change::Upsert {
|
||||
key,
|
||||
username,
|
||||
title,
|
||||
body,
|
||||
media,
|
||||
} => {
|
||||
index.upsert(Document {
|
||||
id: key.resolve().map_err(context)?,
|
||||
username: PublicName::new(&username)
|
||||
.ok_or_else(|| context("invalid public username"))?,
|
||||
title,
|
||||
body,
|
||||
media: media.map(Into::into),
|
||||
})?;
|
||||
}
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
Reference in New Issue
Block a user