|
| 1 | +use std::sync::Arc; |
| 2 | + |
| 3 | +use sqlx::SqlitePool; |
| 4 | +use teloxide::{requests::Requester, types::Message, Bot}; |
| 5 | + |
| 6 | +use crate::{config::config, HandlerResult}; |
| 7 | + |
| 8 | + |
| 9 | +pub async fn authenticate( |
| 10 | + bot: Bot, |
| 11 | + msg: Message, |
| 12 | + (token, name): (String, String), |
| 13 | + db: Arc<SqlitePool>, |
| 14 | +) -> HandlerResult { |
| 15 | + if token == config().admin_token { |
| 16 | + let id = msg.chat.id.to_string(); |
| 17 | + sqlx::query!( |
| 18 | + r#"INSERT INTO admins(telegram_id, "name") VALUES($1, $2)"#, |
| 19 | + id, |
| 20 | + name |
| 21 | + ) |
| 22 | + .execute(db.as_ref()) |
| 23 | + .await?; |
| 24 | + bot.send_message(msg.chat.id, "Authentification réussie !") |
| 25 | + .await?; |
| 26 | + } else { |
| 27 | + bot.send_message(msg.chat.id, "Le token est incorrect") |
| 28 | + .await?; |
| 29 | + } |
| 30 | + |
| 31 | + Ok(()) |
| 32 | +} |
| 33 | + |
| 34 | +pub async fn admin_list(bot: Bot, msg: Message, db: Arc<SqlitePool>) -> HandlerResult { |
| 35 | + let admins = sqlx::query!(r#"SELECT "name" FROM admins"#) |
| 36 | + .fetch_all(db.as_ref()) |
| 37 | + .await?; |
| 38 | + |
| 39 | + bot.send_message( |
| 40 | + msg.chat.id, |
| 41 | + format!( |
| 42 | + "Admin(s) actuel(s):\n{}", |
| 43 | + admins |
| 44 | + .into_iter() |
| 45 | + .map(|r| format!(" - {}", r.name)) |
| 46 | + .collect::<Vec<_>>() |
| 47 | + .join("\n"), |
| 48 | + ), |
| 49 | + ) |
| 50 | + .await?; |
| 51 | + |
| 52 | + Ok(()) |
| 53 | +} |
| 54 | + |
| 55 | +pub async fn admin_remove(bot: Bot, msg: Message, name: String, db: Arc<SqlitePool>) -> HandlerResult { |
| 56 | + let mut tx = db.begin().await?; |
| 57 | + |
| 58 | + if sqlx::query!("SELECT COUNT(*) AS count FROM admins WHERE name = $1", name) |
| 59 | + .fetch_one(tx.as_mut()) |
| 60 | + .await? |
| 61 | + .count |
| 62 | + == 0 |
| 63 | + { |
| 64 | + bot.send_message(msg.chat.id, format!("{} n'est pas admin", name)) |
| 65 | + .await?; |
| 66 | + return Ok(()); |
| 67 | + } |
| 68 | + |
| 69 | + sqlx::query!("DELETE FROM admins WHERE name = $1", name) |
| 70 | + .execute(tx.as_mut()) |
| 71 | + .await?; |
| 72 | + tx.commit().await?; |
| 73 | + |
| 74 | + bot.send_message(msg.chat.id, format!("{} a été retiré(e) des admins", name)) |
| 75 | + .await?; |
| 76 | + |
| 77 | + Ok(()) |
| 78 | +} |
| 79 | + |
| 80 | +pub async fn authorize(bot: Bot, msg: Message, command: String, db: Arc<SqlitePool>) -> HandlerResult { |
| 81 | + let mut tx = db.begin().await?; |
| 82 | + |
| 83 | + let chat_id_str = msg.chat.id.to_string(); |
| 84 | + let already_authorized = sqlx::query!( |
| 85 | + r#"SELECT COUNT(*) AS count FROM authorizations WHERE chat_id = $1 AND command = $2"#, |
| 86 | + chat_id_str, |
| 87 | + command |
| 88 | + ) |
| 89 | + .fetch_one(tx.as_mut()) |
| 90 | + .await?; |
| 91 | + |
| 92 | + if already_authorized.count == 0 { |
| 93 | + sqlx::query!( |
| 94 | + r#"INSERT INTO authorizations(command, chat_id) VALUES($1, $2)"#, |
| 95 | + command, |
| 96 | + chat_id_str |
| 97 | + ) |
| 98 | + .execute(tx.as_mut()) |
| 99 | + .await?; |
| 100 | + } |
| 101 | + |
| 102 | + tx.commit().await?; |
| 103 | + |
| 104 | + bot.send_message( |
| 105 | + msg.chat.id, |
| 106 | + format!("Ce groupe peut désormais utiliser la commande /{}", command), |
| 107 | + ) |
| 108 | + .await?; |
| 109 | + Ok(()) |
| 110 | +} |
| 111 | + |
| 112 | +pub async fn unauthorize( |
| 113 | + bot: Bot, |
| 114 | + msg: Message, |
| 115 | + command: String, |
| 116 | + db: Arc<SqlitePool>, |
| 117 | +) -> HandlerResult { |
| 118 | + let mut tx = db.begin().await?; |
| 119 | + |
| 120 | + let chat_id_str = msg.chat.id.to_string(); |
| 121 | + let already_authorized = sqlx::query!( |
| 122 | + r#"SELECT COUNT(*) AS count FROM authorizations WHERE chat_id = $1 AND command = $2"#, |
| 123 | + chat_id_str, |
| 124 | + command |
| 125 | + ) |
| 126 | + .fetch_one(tx.as_mut()) |
| 127 | + .await?; |
| 128 | + |
| 129 | + if already_authorized.count > 0 { |
| 130 | + sqlx::query!( |
| 131 | + r#"DELETE FROM authorizations WHERE command = $1 AND chat_id = $2"#, |
| 132 | + command, |
| 133 | + chat_id_str |
| 134 | + ) |
| 135 | + .execute(tx.as_mut()) |
| 136 | + .await?; |
| 137 | + } |
| 138 | + |
| 139 | + tx.commit().await?; |
| 140 | + |
| 141 | + bot.send_message( |
| 142 | + msg.chat.id, |
| 143 | + format!( |
| 144 | + "Ce groupe ne peut désormais plus utiliser la commande /{}", |
| 145 | + command |
| 146 | + ), |
| 147 | + ) |
| 148 | + .await?; |
| 149 | + Ok(()) |
| 150 | +} |
| 151 | + |
| 152 | +pub async fn authorizations(bot: Bot, msg: Message, db: Arc<SqlitePool>) -> HandlerResult { |
| 153 | + let chat_id_str = msg.chat.id.to_string(); |
| 154 | + let authorizations = sqlx::query!( |
| 155 | + r#"SELECT command FROM authorizations WHERE chat_id = $1"#, |
| 156 | + chat_id_str |
| 157 | + ) |
| 158 | + .fetch_all(db.as_ref()) |
| 159 | + .await?; |
| 160 | + |
| 161 | + bot.send_message( |
| 162 | + msg.chat.id, |
| 163 | + format!( |
| 164 | + "Ce groupe peut utiliser les commandes suivantes:\n{}", |
| 165 | + authorizations |
| 166 | + .into_iter() |
| 167 | + .map(|s| format!(" - {}", s.command)) |
| 168 | + .collect::<Vec<_>>() |
| 169 | + .join("\n") |
| 170 | + ), |
| 171 | + ) |
| 172 | + .await?; |
| 173 | + |
| 174 | + Ok(()) |
| 175 | +} |
0 commit comments