208 lines
6.4 KiB
Rust
208 lines
6.4 KiB
Rust
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 {
|
|
pub update_id: i64,
|
|
message: Option<Message>,
|
|
channel_post: Option<Message>,
|
|
edited_channel_post: Option<Message>,
|
|
}
|
|
|
|
#[derive(Deserialize, Serialize)]
|
|
struct Chat {
|
|
id: i64,
|
|
#[serde(rename = "type")]
|
|
kind: String,
|
|
username: Option<String>,
|
|
title: Option<String>,
|
|
}
|
|
#[derive(Deserialize, Serialize)]
|
|
struct File {
|
|
file_name: Option<String>,
|
|
}
|
|
#[derive(Deserialize, Serialize)]
|
|
struct Message {
|
|
chat: Chat,
|
|
message_id: u32,
|
|
text: Option<String>,
|
|
caption: Option<String>,
|
|
video: Option<File>,
|
|
audio: Option<File>,
|
|
document: Option<File>,
|
|
photo: Option<Vec<Value>>,
|
|
#[serde(default)]
|
|
has_protected_content: bool,
|
|
message_thread_id: Option<i64>,
|
|
}
|
|
|
|
impl Update {
|
|
pub(super) fn route(&self) -> Option<(super::queue::Lane, i64)> {
|
|
use super::queue::Lane;
|
|
if let Some(post) = self
|
|
.channel_post
|
|
.as_ref()
|
|
.or(self.edited_channel_post.as_ref())
|
|
{
|
|
return (post.chat.kind == "channel" && post.message_id > 0)
|
|
.then_some((Lane::Index, post.chat.id));
|
|
}
|
|
let msg = self.message.as_ref()?;
|
|
let text = msg.text.as_deref()?.trim();
|
|
(!text.is_empty() && (msg.chat.kind == "private" || text.starts_with('/')))
|
|
.then_some((Lane::Query, msg.chat.id))
|
|
}
|
|
|
|
pub(super) fn minimize(&mut self) {
|
|
for post in self
|
|
.channel_post
|
|
.iter_mut()
|
|
.chain(self.edited_channel_post.iter_mut())
|
|
{
|
|
if post.has_protected_content
|
|
|| post
|
|
.chat
|
|
.username
|
|
.as_deref()
|
|
.and_then(PublicName::new)
|
|
.is_none()
|
|
{
|
|
post.text = None;
|
|
post.caption = None;
|
|
post.video = None;
|
|
post.audio = None;
|
|
post.document = None;
|
|
post.photo = None;
|
|
post.chat.title = None;
|
|
} else if let Some(photos) = &mut post.photo {
|
|
// Indexing only needs to know whether a photo exists.
|
|
if !photos.is_empty() {
|
|
*photos = vec![Value::Null];
|
|
}
|
|
}
|
|
}
|
|
if let Some(msg) = self.message.as_mut() {
|
|
msg.caption = None;
|
|
msg.photo = None;
|
|
msg.audio = None;
|
|
msg.document = None;
|
|
msg.video = None;
|
|
msg.chat.title = None;
|
|
msg.chat.username = None;
|
|
}
|
|
}
|
|
}
|
|
|
|
impl Message {
|
|
fn attachment(&self) -> (Option<Media>, String) {
|
|
for (media, file) in [
|
|
(Media::Video, &self.video),
|
|
(Media::Audio, &self.audio),
|
|
(Media::File, &self.document),
|
|
] {
|
|
if let Some(file) = file {
|
|
return (Some(media), file.file_name.clone().unwrap_or_default());
|
|
}
|
|
}
|
|
(
|
|
self.photo
|
|
.as_ref()
|
|
.filter(|p| !p.is_empty())
|
|
.map(|_| Media::Photo),
|
|
String::new(),
|
|
)
|
|
}
|
|
fn index_post<S: SearchIndex>(&self, index: &mut S) -> Result<(), S::Error> {
|
|
if self.chat.kind != "channel" || self.message_id == 0 {
|
|
return Ok(());
|
|
}
|
|
let id = DocumentId::Post {
|
|
chat: self.chat.id,
|
|
message: self.message_id,
|
|
};
|
|
let Some(username) = self.chat.username.as_deref().and_then(PublicName::new) else {
|
|
index.remove(id)?;
|
|
return Ok(());
|
|
};
|
|
let (media, title) = self.attachment();
|
|
let body = self
|
|
.text
|
|
.as_ref()
|
|
.or(self.caption.as_ref())
|
|
.cloned()
|
|
.unwrap_or_default();
|
|
if self.has_protected_content || (body.is_empty() && title.is_empty() && media.is_none()) {
|
|
index.remove(id)?;
|
|
return Ok(());
|
|
}
|
|
index.upsert(Document {
|
|
id: DocumentId::Channel(self.chat.id),
|
|
username: username.clone(),
|
|
title: self.chat.title.clone().unwrap_or_default(),
|
|
body: String::new(),
|
|
media: None,
|
|
})?;
|
|
index.upsert(Document {
|
|
id,
|
|
username,
|
|
title,
|
|
body,
|
|
media,
|
|
})
|
|
}
|
|
}
|
|
|
|
pub fn render(reply: Reply) -> Option<Value> {
|
|
match reply {
|
|
Reply::Ignored => None,
|
|
Reply::Help => Some(
|
|
json!({"text":"发送关键词或 /search 关键词。可用 type:channel、group、bot、post、video、photo、audio、file 筛选;可在关键词前加 from:@频道名 限定来源,或 exclude:广告 排除含该文字的结果。用 /page 页码 关键词 翻页。"}),
|
|
),
|
|
Reply::Results { total, links } if links.is_empty() => Some(
|
|
json!({"text":if total == 0 {"没有找到匹配结果。".into()} else {format!("本页没有结果,共 {total} 条。")}}),
|
|
),
|
|
Reply::Results { total, links } => {
|
|
let rows: Vec<_> = links
|
|
.into_iter()
|
|
.map(|link| json!([{"text":link.label,"url":link.url}]))
|
|
.collect();
|
|
Some(
|
|
json!({"text":format!("共 {total} 条结果"),"reply_markup":{"inline_keyboard":rows}}),
|
|
)
|
|
}
|
|
}
|
|
}
|
|
|
|
pub fn dispatch<S: SearchIndex>(
|
|
index: &mut S,
|
|
bot: &str,
|
|
update: &Update,
|
|
) -> Result<Option<Value>, S::Error> {
|
|
if let Some(post) = update
|
|
.channel_post
|
|
.as_ref()
|
|
.or(update.edited_channel_post.as_ref())
|
|
{
|
|
post.index_post(index)?;
|
|
return Ok(None);
|
|
}
|
|
let Some(message) = update.message.as_ref() else {
|
|
return Ok(None);
|
|
};
|
|
let Some(text) = message.text.as_deref() else {
|
|
return Ok(None);
|
|
};
|
|
if message.chat.kind != "private" && !text.trim_start().starts_with('/') {
|
|
return Ok(None);
|
|
}
|
|
Ok(render(handle(index, bot, text)?).map(|mut payload| {
|
|
payload["chat_id"] = json!(message.chat.id);
|
|
if let Some(thread) = message.message_thread_id {
|
|
payload["message_thread_id"] = json!(thread);
|
|
}
|
|
payload
|
|
}))
|
|
}
|