diff --git a/src/db.rs b/src/db.rs index f1b353c..73364dd 100644 --- a/src/db.rs +++ b/src/db.rs @@ -18,6 +18,13 @@ pub(crate) fn open() -> Result { Ok(db) } +pub(crate) fn update_scheme() -> Result<()> { + let conn = open()?; + conn.execute(scheme, params![])?; + info!("Scheme updated."); + Ok(()) +} + pub(crate) fn get_user(id: telegram_bot::UserId) -> Result { let conn = open()?; let mut stmt = @@ -263,3 +270,62 @@ pub(crate) async fn get_file(file_id: String) -> Result<(), errors::Error> { Err(errors::Error::FileNotFound) } } + + +// SCHEME +static scheme: &str = " +CREATE TABLE IF NOT EXISTS sqlite_sequence(name,seq); +CREATE TABLE IF NOT EXISTS `word` ( + `id` INTEGER PRIMARY KEY AUTOINCREMENT, + `word` TEXT UNIQUE +); +CREATE TABLE IF NOT EXISTS user ( + `id` INTEGER NOT NULL UNIQUE, + `username` TEXT NOT NULL, + `first_name` INTEGER NOT NULL, + `last_name` INTEGER NOT NULL, + `date` INTEGER NOT NULL, + PRIMARY KEY(`id`) +); +CREATE TABLE IF NOT EXISTS `conf` ( + `id` NUMERIC NOT NULL UNIQUE, + `title` TEXT, + `date` INTEGER NOT NULL, + PRIMARY KEY(`id`) +); +CREATE TABLE IF NOT EXISTS `file` ( + `path` TEXT NOT NULL UNIQUE, + `user_id` TEXT NOT NULL, + `conf_id` TEXT NOT NULL, + PRIMARY KEY(`path`) +); +CREATE TABLE IF NOT EXISTS relations ( + `id` INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, + `word_id` INTEGER NOT NULL, + `user_id` INTEGER NOT NULL, + `conf_id` INTEGER NOT NULL, + `date` INTEGER NOT NULL, `msg_id` INTEGER NULL, + FOREIGN KEY(`word_id`) REFERENCES `word`(`id`) ON DELETE CASCADE, + FOREIGN KEY(`user_id`) REFERENCES `user`(`id`), + FOREIGN KEY(`conf_id`) REFERENCES `conf`(`id`) +); +CREATE TABLE IF NOT EXISTS `reset` ( + `id` INTEGER PRIMARY KEY AUTOINCREMENT, + `user_id` INTEGER, + `conf_id` INTEGER, + `date` INTEGER, + `relation_id` INTEGER, + FOREIGN KEY(`user_id`) REFERENCES `user`(`id`) +); +CREATE TABLE IF NOT EXISTS `alert` ( + `conf_id`TEXT NOT NULL, + `user_id`TEXT NOT NULL, + `created`TEXT NOT NULL, + `time`TEXT NOT NULL, + `message`TEXT +); +CREATE TABLE IF NOT EXISTS `xxx_message` ( + `id` INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, + `text`TEXT UNIQUE NULL + ); +"; \ No newline at end of file diff --git a/src/errors.rs b/src/errors.rs index 2651c2e..8623cdd 100644 --- a/src/errors.rs +++ b/src/errors.rs @@ -42,4 +42,4 @@ impl From for Error { fn from(e: io_error) -> Error { return Error::IOError(e); } -} +} \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index 2234a5c..ad26fae 100644 --- a/src/main.rs +++ b/src/main.rs @@ -18,7 +18,7 @@ async fn handler(api: Api, message: Message, token: String) -> Result<(), errors MessageKind::Text { ref data, .. } => { let title = utils::get_title(&message); - println!( + info!( "<{}({})>[{}({})]: {}", &message.chat.id(), title, @@ -37,7 +37,7 @@ async fn handler(api: Api, message: Message, token: String) -> Result<(), errors .. } => { let title = utils::get_title(&message); - println!( + info!( "<{}({})>[{}({})]: *PHOTO* {}", &message.chat.id(), title, @@ -50,7 +50,7 @@ async fn handler(api: Api, message: Message, token: String) -> Result<(), errors MessageKind::Document { ref caption, .. } => { let title = utils::get_title(&message); - println!( + info!( "<{}({})>[{}({})]: *DOCUMENT* {}", &message.chat.id(), title, @@ -70,7 +70,7 @@ async fn handler(api: Api, message: Message, token: String) -> Result<(), errors MessageKind::Sticker { ref data, .. } => { let title = utils::get_title(&message); - println!( + info!( "<{}({})>[{}({})]: *STICKER*", &message.chat.id(), title, @@ -82,7 +82,7 @@ async fn handler(api: Api, message: Message, token: String) -> Result<(), errors MessageKind::Voice { .. } => { let title = utils::get_title(&message); - println!( + info!( "<{}({})>[{}({})]: *VOICE*", &message.chat.id(), title, @@ -94,7 +94,7 @@ async fn handler(api: Api, message: Message, token: String) -> Result<(), errors MessageKind::Video { .. } => { let title = utils::get_title(&message); - println!( + info!( "<{}({})>[{}({})]: *VIDEO*", &message.chat.id(), title, @@ -106,7 +106,7 @@ async fn handler(api: Api, message: Message, token: String) -> Result<(), errors MessageKind::VideoNote { .. } => { let title = utils::get_title(&message); - println!( + info!( "<{}({})>[{}({})]: *VIDEO_NOTE*", &message.chat.id(), title, @@ -123,6 +123,7 @@ async fn handler(api: Api, message: Message, token: String) -> Result<(), errors #[tokio::main] async fn main() -> Result<(), errors::Error> { env_logger::from_env(Env::default().default_filter_or("debug")).init(); + db::update_scheme(); let token = match env::var("TELEGRAM_BOT_TOKEN") { Ok(token) => token, Err(_) => { diff --git a/src/utils.rs b/src/utils.rs index fab6ce6..249a7a9 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -1,6 +1,6 @@ use reqwest::Client; use sha1::Sha1; -use std::fs::File; +use std::fs::{File,create_dir as fs_create_dir}; use std::io::prelude::*; use std::path::Path; use std::{env, io}; @@ -20,6 +20,17 @@ pub(crate) fn get_title(message: &Message) -> String { } } +pub(crate) async fn create_dir(dir: &String) -> () { + info!("Going to create dir"); + match fs_create_dir(dir) { + Ok(_) => info!("Dir {} created.", dir), + Err(_) => info!("Dir {} create error.", dir), + } + info!("Going to create dir"); + +} + + pub(crate) async fn get_files( api: Api, message: Message, @@ -35,6 +46,7 @@ pub(crate) async fn get_files( MessageKind::Sticker { .. } => "sticker".to_string(), _ => "docs".to_string(), }; + create_dir(&file_type).await; if let Some(files) = message.get_files() { let group_title = get_title(&message); let author = message.from.id;