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
// Copyright 2021 - 2021, Rupansh Sekar and the Kilogramme (TBD) contributors
// SPDX-License-Identifier: MPL-2.0
use crate::{
    errors::UserBotError,
    userbot::{CommandHandlerResult, UserBot},
    *,
};
use grammers_client::types::{Chat, InputMessage, Message};
use reusable_fmt::fmt;

/// Creates a Note
///
/// ## Scope
/// Anywhere
///
/// ## Usage(s)
/// `<reply> !note note-key`
///
//UserBotCmd !note
pub async fn add_note_handler(bot: &mut UserBot, message: &mut Message) -> CommandHandlerResult {
    let name = &bot.get_args_nr(message, true)?[0];
    let rep_id = message
        .reply_to_message_id()
        .ok_or(UserBotError::NoArguments)?;
    // TODO: I NEED PEERSELF HERE!!!
    let me = bot.client.get_me().await?;
    let fwd = bot
        .client
        .forward_messages(&Chat::User(me), &[rep_id], &message.chat())
        .await?;

    let fwd = fwd[0].as_ref().ok_or(UserBotError::MessageForwardFailed)?;

    helpers::db::add_note(&bot.db, name, fwd.id()).await?;

    message
        .edit(InputMessage::markdown(&fmt!(NOTE_SUCCESS, name)))
        .await?;
    Ok(())
}

/// Fetch a Note
///
/// ## Scope
/// Anywhere
///
/// ## Usage(s)
/// `!get note-key`
///
//UserBotCmd !get
pub async fn get_note_handler(bot: &mut UserBot, message: &mut Message) -> CommandHandlerResult {
    let name = bot.get_args_nr(message, true)?.remove(0);
    let note = helpers::db::find_note(&bot.db, &name)
        .await?
        .ok_or(UserBotError::NoteNotFound)?;

    // TODO: I NEED PEERSELF HERE!!!
    let me = bot.client.get_me().await?;
    bot.client
        .forward_messages(&message.chat(), &[note], &Chat::User(me))
        .await?;

    Ok(())
}

/// Delete a Note
///
/// ## Scope
/// Anywhere
///
/// ## Usage(s)
/// `!clear note-key`
///
//UserBotCmd !clear
pub async fn rm_notes_handler(bot: &mut UserBot, message: &mut Message) -> CommandHandlerResult {
    let name = bot.get_args_nr(message, true)?.remove(0);

    if helpers::db::remove_note(&bot.db, &name)
        .await?
        .deleted_count
        != 0
    {
        message
            .edit(InputMessage::markdown(fmt!(NOTE_DEL_SUCCESS, name)))
            .await?;
        Ok(())
    } else {
        Err(UserBotError::NoteNotFound)
    }
}

/// Get a list of all note keys
///
/// ## Scope
/// Anywhere
///
/// ## Usage(s)
/// `!notes`
///
//UserBotCmd !notes
pub async fn all_notes_handler(bot: &mut UserBot, message: &mut Message) -> CommandHandlerResult {
    let keys: Vec<String> = helpers::db::note_list(&bot.db).await?.collect();
    if !keys.is_empty() {
        message
            .edit(InputMessage::markdown(keys.join("\n")))
            .await?;
        Ok(())
    } else {
        Err(UserBotError::NotesEmpty)
    }
}