tg_admin 0.1.1

tg interface to change local structured data
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
use std::sync::{Arc, RwLock};

use serde::{Deserialize, Serialize};
use serde_json::Value;
use teloxide::{
	dispatching::{
		UpdateHandler,
		dialogue::{self, InMemStorage},
	},
	prelude::*,
	types::{CallbackQuery, InlineKeyboardButton, InlineKeyboardMarkup, Message, MessageId},
	utils::command::BotCommands,
};
use tracing::info;
use v_utils::prelude::*;

use crate::{
	config::LiveSettings,
	data::{Data, ValuePath},
	utils::{get_json_type, value_preview},
};

type MyDialogue = Dialogue<ChatState, InMemStorage<ChatState>>;
type HandlerResult = Result<(), Box<dyn std::error::Error + Send + Sync>>;

#[derive(Clone, Debug, Default, Deserialize, Eq, PartialEq, Serialize)]
enum ChatState {
	/// Most actions are prohibited from this state. Other states can be reached only through authorization from here.
	#[default]
	Unauthorized,
	/// Dummy state, only here to not have to spawn Navigation with random value on authorization.
	Authorized,
	Navigation {
		message_id: i32,
	},
	Input(ValueInput),
}
#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize, derive_new::new)]
struct ValueInput {
	input_type: InputValueType,
	value_path: ValuePath,
}
#[derive(Clone, Copy, Debug, Deserialize, Eq, PartialEq, Serialize)]
pub enum InputValueType {
	UpdateAt,
	AddTo,
	RemoveFrom,
}
#[derive(BotCommands, Clone, Debug)]
#[command(description = "Commands:", rename_rule = "lowercase")]
enum Command {
	#[command(description = "Display all commands")]
	Help,
	#[command(description = "Open admin panel at the top")]
	Admin,
}

#[tracing::instrument]
pub async fn run(settings: Arc<LiveSettings>, data: Arc<RwLock<Data>>) -> Result<()> {
	let token = settings.config()?.tg_token;
	let bot = Bot::new(token);
	info!("Starting telegram bot...");
	Dispatcher::builder(bot, schema())
		.dependencies(dptree::deps![data, settings, InMemStorage::<ChatState>::new()])
		.error_handler(LoggingErrorHandler::with_custom_text("An error has occurred in the dispatcher"))
		.enable_ctrlc_handler()
		.build()
		.dispatch()
		.await;
	Ok(())
}

fn schema() -> UpdateHandler<Box<dyn std::error::Error + Send + Sync + 'static>> {
	use dptree::case;

	let command_handler = teloxide::filter_command::<Command, _>()
		.branch(case![Command::Help].endpoint(help_handler))
		.branch(case![Command::Admin].endpoint(admin_handler));

	let message_handler = Update::filter_message()
		.branch(command_handler)
		.branch(case![ChatState::Input(value_input)].endpoint(value_input_handler))
		.branch(dptree::endpoint(invalid_state_handler));

	let callback_query_handler = Update::filter_callback_query().endpoint(callback_query_handler);

	let auth_handler = dptree::filter_map_async(|dialogue: MyDialogue, settings: Arc<LiveSettings>, update: Update| async move {
		match dialogue.get().await {
			Ok(Some(ChatState::Unauthorized)) => {
				if let Some(admin_list) = &settings.config().ok()?.admin_list {
					let user_id = update.from()?.id.0;
					if !admin_list.contains(&user_id) {
						return None; // Not authorized
					}
				}
				dialogue.update(ChatState::Authorized).await.ok()?;
				Some(()) // Authorized
			}
			Ok(Some(_)) => Some(()), // Already authorized
			_ => None,               // Error or no state, treat as unauthorized
		}
	});

	dialogue::enter::<Update, InMemStorage<ChatState>, ChatState, _>()
		.chain(auth_handler)
		.branch(message_handler)
		.branch(callback_query_handler)
}

async fn admin_handler(bot: Bot, msg: Message, dialogue: MyDialogue, data: Arc<RwLock<Data>>) -> HandlerResult {
	let value_path = ValuePath::default();
	let (header, markup) = {
		let data = data.read().unwrap();
		render_header_and_markup(&data, &value_path)
	};
	let sent_message = bot.send_message(msg.chat.id, &header).reply_markup(markup).await?;
	dialogue.update(ChatState::Navigation { message_id: sent_message.id.0 }).await?;
	Ok(())
}

async fn value_input_handler(bot: Bot, dialogue: MyDialogue, msg: Message, value_input: ValueInput, data: Arc<RwLock<Data>>) -> HandlerResult {
	match msg.text().map(ToOwned::to_owned) {
		Some(new_value) => {
			if let Ok(new_value) = serde_json::from_str::<Value>(&new_value) {
				let update_result = {
					let mut data_lock = data.write().unwrap();
					let result = data_lock.update_at(&value_input.value_path, new_value.clone(), value_input.input_type);
					data_lock.write().unwrap();
					result
				};

				match update_result {
					Ok(_) => {
						let affirmation_menu = match value_input.input_type {
							InputValueType::UpdateAt => {
								format!("Value of `{}` has been updated to `{}`", &value_input.value_path, &new_value.to_string())
							}
							InputValueType::AddTo => {
								format!("`{}` has been added to `{}`", &new_value.to_string(), &value_input.value_path)
							}
							InputValueType::RemoveFrom => {
								format!("`{}` has been removed from `{}`", &new_value.to_string(), &value_input.value_path)
							}
						};
						bot.send_message(msg.chat.id, affirmation_menu).await?;

						// Resend the nav menu
						let (header, markup) = {
							let data = data.read().unwrap();
							let new_path = match value_input.input_type {
								InputValueType::UpdateAt => value_input.value_path.parent(),
								InputValueType::AddTo | InputValueType::RemoveFrom => value_input.value_path,
							};
							render_header_and_markup(&data, &new_path)
						};
						let sent_message = bot.send_message(dialogue.chat_id(), &header).reply_markup(markup).await?;
						dialogue.update(ChatState::Navigation { message_id: sent_message.id.0 }).await?;
					}
					Err(e) => {
						bot.send_message(msg.chat.id, e).await?;
					}
				}
			} else {
				bot.send_message(msg.chat.id, "Invalid value. Input valid JSON value.").await?;
			}
		}
		None => {
			bot.send_message(msg.chat.id, "Please send the new value.").await?;
		}
	}
	Ok(())
}

async fn invalid_state_handler(bot: Bot, msg: Message) -> HandlerResult {
	bot.send_message(msg.chat.id, "Unable to handle the message. Type /help to see available commands.").await?;
	Ok(())
}
async fn help_handler(bot: Bot, msg: Message) -> HandlerResult {
	bot.send_message(msg.chat.id, Command::descriptions().to_string()).await?;
	Ok(())
}

async fn callback_query_handler(bot: Bot, dialogue: MyDialogue, q: CallbackQuery, data: Arc<RwLock<Data>>) -> HandlerResult {
	bot.answer_callback_query(q.id.clone()).await?; // normally this is done after, but I like how it stops for a moment before the action is performed. Otherwise looks cut.
	if let Some(j) = q.data {
		let action: CallbackAction = serde_json::from_str(&j).unwrap();
		match action {
			CallbackAction::Go(value_path) => {
				continue_navigation(bot.clone(), dialogue, data, value_path).await?;
			}
			CallbackAction::UpdateAt(value_path) => {
				dialogue.update(ChatState::Input(ValueInput::new(InputValueType::UpdateAt, value_path.clone()))).await?;
				bot.send_message(
					dialogue.chat_id(),
					format!("You're updating `{}: {}`.\n Insert the new value.", &value_path.basename(), {
						let data_lock = data.read().unwrap();
						get_json_type(&data_lock.at(&value_path).unwrap())
					}),
				)
				.await?;
			}
			CallbackAction::AddTo(value_path) => {
				dialogue.update(ChatState::Input(ValueInput::new(InputValueType::AddTo, value_path.clone()))).await?;
				bot.send_message(dialogue.chat_id(), format!("You're adding to {}.\n Provide the value to add.", value_path))
					.await?;
			}
			CallbackAction::RemoveFrom(value_path) => {
				dialogue.update(ChatState::Input(ValueInput::new(InputValueType::RemoveFrom, value_path.clone()))).await?;
				bot.send_message(dialogue.chat_id(), format!("You're removing from {}.\n Provide exact value to remove.", value_path))
					.await?;
			}
		}
	}
	Ok(())
}

async fn continue_navigation(bot: Bot, dialogue: MyDialogue, data: Arc<RwLock<Data>>, value_path: ValuePath) -> HandlerResult {
	let (header, markup) = {
		let data = data.read().unwrap();
		render_header_and_markup(&data, &value_path)
	};

	let state = dialogue.get().await.unwrap().unwrap();
	let message_id = match state {
		ChatState::Navigation { message_id } => message_id,
		_ => unreachable!(),
	};

	match bot.edit_message_text(dialogue.chat_id(), MessageId(message_id), &header).reply_markup(markup.clone()).await {
		Ok(_) => Ok(()),
		//TODO!: assert that the err is about message being too old, as it's the only recoverable one.
		Err(err) => {
			dbg!(err);
			let sent_message = bot.send_message(dialogue.chat_id(), &header).reply_markup(markup).await?;
			dialogue.update(ChatState::Navigation { message_id: sent_message.id.0 }).await?;
			Ok(())
		}
	}
}

#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize, derive_new::new)]
enum CallbackAction {
	Go(ValuePath),
	UpdateAt(ValuePath),
	AddTo(ValuePath),
	RemoveFrom(ValuePath),
}

fn render_header_and_markup(data: &Data, value_path: &ValuePath) -> (String, InlineKeyboardMarkup) {
	let mut keyboard = Vec::new();
	let current_value_at_path = &data.at(value_path).unwrap();
	let mut header = value_path.to_string();

	// Add parent navigation button if not at top level
	if !value_path.is_top() {
		let callback_action = CallbackAction::Go(value_path.parent());
		let button = InlineKeyboardButton::callback("..", serde_json::to_string(&callback_action).unwrap());
		keyboard.push(vec![button]);
	}

	match current_value_at_path {
		Value::Object(map) =>
			for (key, val) in map {
				let (display_text, callback_data) = match val {
					Value::Object(_) | Value::Array(_) => (value_preview(key, val), CallbackAction::Go(value_path.join(key))),
					_ => (value_preview(key, val), CallbackAction::UpdateAt(value_path.join(key))),
				};

				let button = InlineKeyboardButton::callback(display_text, serde_json::to_string(&callback_data).unwrap());
				keyboard.push(vec![button]);
			},
		Value::Array(arr) => {
			header.push_str(&format!(" [{}]", arr.len()));

			let start = arr.len().saturating_sub(25);
			let mut array_str = "\n```json\n".to_owned();
			for a in arr.iter().skip(start) {
				array_str.push_str(&format!("{a}\n"));
			}
			array_str.push_str("```");
			header += &array_str;

			let bottom_row = vec![
				InlineKeyboardButton::callback("Add", serde_json::to_string(&CallbackAction::AddTo(value_path.clone())).unwrap()),
				InlineKeyboardButton::callback("Remove", serde_json::to_string(&CallbackAction::RemoveFrom(value_path.clone())).unwrap()),
			];
			//TODO!: make doubled horizontally `<-` and `->` buttons that modify starting position of the count
			keyboard.push(bottom_row);
		}
		_ => {
			unreachable!();
		}
	}

	(header, InlineKeyboardMarkup::new(keyboard))
}

#[cfg(test)]
mod tests {
	use serde_json::json;

	use super::*;

	fn gen_data() -> (Data, ValuePath) {
		let json_value = json!({
			"name": "Alice",
			"age": 25,
			"address": {
			"street": "456 Another St",
			"city": "Elsewhere"
			},
			"emails": ["alice@example.com", "a@example.com"]
		});
		(Data::mock(json_value), ValuePath::default())
	}

	#[test]
	fn test_top_value_path_representation() {
		let (data, value_path) = gen_data();
		let (_h, r) = render_header_and_markup(&data, &value_path);

		insta::assert_json_snapshot!(
			r,
			@r###"
  {
    "inline_keyboard": [
      [
        {
          "text": "{} address",
          "callback_data": "{\"Go\":\"/address\"}"
        }
      ],
      [
        {
          "text": "age: 25",
          "callback_data": "{\"UpdateAt\":\"/age\"}"
        }
      ],
      [
        {
          "text": "[2] emails",
          "callback_data": "{\"Go\":\"/emails\"}"
        }
      ],
      [
        {
          "text": "name: \"Alice\"",
          "callback_data": "{\"UpdateAt\":\"/name\"}"
        }
      ]
    ]
  }
  "###
		);
	}

	#[test]
	fn test_nested_value_path_representation() {
		let (data, mut value_path) = gen_data();
		value_path.push("address");
		let (_h, r) = render_header_and_markup(&data, &value_path);
		insta::assert_json_snapshot!(
			r,
			@r###"
  {
    "inline_keyboard": [
      [
        {
          "text": "..",
          "callback_data": "{\"Go\":\"/\"}"
        }
      ],
      [
        {
          "text": "city: \"Elsewhere\"",
          "callback_data": "{\"UpdateAt\":\"/address/city\"}"
        }
      ],
      [
        {
          "text": "street: \"456 Another St\"",
          "callback_data": "{\"UpdateAt\":\"/address/street\"}"
        }
      ]
    ]
  }
  "###
		);
	}
	#[test]
	fn test_array_value_path_representation() {
		let (data, mut value_path) = gen_data();
		value_path.push("emails");
		let (h, r) = render_header_and_markup(&data, &value_path);

		insta::assert_snapshot!(h, "Admin Menu",);

		insta::assert_json_snapshot!(
			r,
			@r###"
  {
    "inline_keyboard": [
      [
        {
          "text": "..",
          "callback_data": "{\"Go\":\"/\"}"
        }
      ],
      [
        {
          "text": "Add",
          "callback_data": "{\"AddTo\":\"/emails\"}"
        },
        {
          "text": "Remove",
          "callback_data": "{\"RemoveFrom\":\"/emails\"}"
        }
      ]
    ]
  }
  "###
		);
	}
}