2024-06-04 23:48:01 +08:00
|
|
|
use crate::data_struct::tailchat::messages::SendingMessage;
|
|
|
|
// use crate::data_struct::tailchat::{ConverseId, GroupId, MessageId, UserId};
|
2024-03-30 18:30:43 +08:00
|
|
|
|
2024-06-04 23:48:01 +08:00
|
|
|
use colored::Colorize;
|
2024-06-15 23:31:10 +08:00
|
|
|
use reqwest::multipart;
|
|
|
|
use rust_socketio::asynchronous::Client;
|
2024-06-10 16:05:24 +08:00
|
|
|
use serde_json::{json, Value};
|
2024-06-15 23:31:10 +08:00
|
|
|
use tracing::{event, span, Level};
|
2024-06-04 23:48:01 +08:00
|
|
|
|
|
|
|
pub async fn send_message(client: &Client, message: &SendingMessage) -> bool {
|
2024-06-15 23:31:10 +08:00
|
|
|
let span = span!(Level::INFO, "tailchat send message");
|
|
|
|
let _enter = span.enter();
|
2024-06-28 23:19:10 +08:00
|
|
|
let mut value: Value = message.as_value();
|
2024-06-15 00:35:21 +08:00
|
|
|
if message.contain_file() {
|
|
|
|
// 处理文件
|
2024-06-15 23:31:10 +08:00
|
|
|
let mut header = reqwest::header::HeaderMap::new();
|
2024-06-28 23:19:10 +08:00
|
|
|
header.append(
|
|
|
|
"X-Token",
|
|
|
|
crate::MainStatus::global_tailchat_status().jwt_token.clone().parse().unwrap(),
|
|
|
|
);
|
2024-06-15 23:31:10 +08:00
|
|
|
let file_client = match reqwest::ClientBuilder::new().default_headers(header).build() {
|
|
|
|
Ok(client) => client,
|
|
|
|
Err(e) => {
|
|
|
|
event!(Level::ERROR, "file_client build failed:{}", format!("{:#?}", e).red());
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
// 感谢 https://stackoverflow.com/questions/65814450/how-to-post-a-file-using-reqwest
|
|
|
|
let upload_url =
|
|
|
|
format!("{}/upload", crate::MainStatus::global_config().tailchat().host.clone());
|
|
|
|
let file_body =
|
|
|
|
multipart::Part::stream(message.file.file_data()).file_name(message.file.file_name());
|
|
|
|
let form_data = multipart::Form::new().part("file", file_body);
|
|
|
|
|
|
|
|
event!(Level::INFO, "sending file message");
|
|
|
|
let data = match file_client.post(&upload_url).multipart(form_data).send().await {
|
|
|
|
Ok(resp) => {
|
|
|
|
if resp.status().is_success() {
|
|
|
|
match resp.text().await {
|
|
|
|
Ok(text) => match serde_json::from_str::<Value>(&text) {
|
|
|
|
Ok(json) => json,
|
|
|
|
Err(e) => {
|
|
|
|
event!(
|
|
|
|
Level::ERROR,
|
|
|
|
"file uploaded, but response parse failed:{}",
|
|
|
|
format!("{:#?}", e).red()
|
|
|
|
);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
Err(e) => {
|
|
|
|
event!(
|
|
|
|
Level::ERROR,
|
|
|
|
"file uploaded, but failed to get response:{}",
|
|
|
|
format!("{:#?}", e).red()
|
|
|
|
);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
event!(Level::ERROR, "file upload faild:{}", format!("{:#?}", resp).red());
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Err(e) => {
|
|
|
|
event!(
|
|
|
|
Level::ERROR,
|
|
|
|
"file upload failed while posting data:{}",
|
|
|
|
format!("{:#?}", e).red()
|
|
|
|
);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
};
|
2024-06-28 23:19:10 +08:00
|
|
|
let content = format!(
|
2024-06-29 00:56:40 +08:00
|
|
|
"{}{}",
|
2024-06-28 23:19:10 +08:00
|
|
|
message.content,
|
|
|
|
message.file.gen_markdown(data["url"].as_str().unwrap())
|
|
|
|
);
|
|
|
|
value["content"] = json!(content);
|
2024-06-15 00:35:21 +08:00
|
|
|
}
|
2024-06-04 23:48:01 +08:00
|
|
|
match client.emit("chat.message.sendMessage", value).await {
|
|
|
|
Ok(_) => {
|
2024-06-15 23:31:10 +08:00
|
|
|
event!(Level::DEBUG, "send message {}", format!("{:#?}", message).cyan());
|
2024-06-04 23:48:01 +08:00
|
|
|
true
|
|
|
|
}
|
|
|
|
Err(e) => {
|
2024-06-15 23:31:10 +08:00
|
|
|
event!(Level::WARN, "send message failed:{}", format!("{:#?}", e).red());
|
2024-06-04 23:48:01 +08:00
|
|
|
false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-06-10 16:05:24 +08:00
|
|
|
|
|
|
|
pub async fn emit_join_room(client: &Client) -> bool {
|
2024-06-15 23:31:10 +08:00
|
|
|
let span = span!(Level::INFO, "tailchat findAndJoinRoom");
|
|
|
|
let _enter = span.enter();
|
2024-06-10 16:05:24 +08:00
|
|
|
match client.emit("chat.converse.findAndJoinRoom", json!([])).await {
|
|
|
|
Ok(_) => {
|
2024-06-15 23:31:10 +08:00
|
|
|
event!(Level::INFO, "emiting join room");
|
2024-06-10 16:05:24 +08:00
|
|
|
true
|
|
|
|
}
|
|
|
|
Err(e) => {
|
2024-06-15 23:31:10 +08:00
|
|
|
event!(Level::WARN, "emit_join_room faild:{}", format!("{:#?}", e).red());
|
2024-06-10 16:05:24 +08:00
|
|
|
false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|