File size: 3,554 Bytes
eeb0f9c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# auth/auth.py
import bcrypt
from auth.db import get_connection

def register_user(username, password):
    username = username.strip()
    password = password.strip()
    if not username or not password:
        return False, "Vui lòng nhập tài khoản và mật khẩu"
    conn = get_connection()
    cursor = conn.cursor()
    cursor.execute("SELECT * FROM users WHERE username = ?", (username,))
    if cursor.fetchone():
        conn.close()
        return False, "Tài khoản đã tồn tại"
    hashed = bcrypt.hashpw(password.encode(), bcrypt.gensalt())
    cursor.execute("INSERT INTO users (username, password_hash) VALUES (?, ?)", (username, hashed))
    conn.commit()
    conn.close()
    return True, "Đăng ký thành công"

def login_user(username, password):
    username = username.strip()
    password = password.strip()
    if not username or not password:
        return False, "Vui lòng nhập tài khoản và mật khẩu"
    conn = get_connection()
    cursor = conn.cursor()
    cursor.execute("SELECT password_hash FROM users WHERE username = ?", (username,))
    row = cursor.fetchone()
    conn.close()
    if row and bcrypt.checkpw(password.encode(), row[0]):
        return True, "Đăng nhập thành công"
    return False, "Sai tài khoản hoặc mật khẩu"

def save_message(username, message):
    if isinstance(message, (list, tuple)):
        message = "\n".join(str(m) for m in message)
    conn = get_connection()
    cursor = conn.cursor()
    cursor.execute("INSERT INTO chat_history (username, message) VALUES (?, ?)", (username, message))
    conn.commit()
    conn.close()

def clear_history(username):
    conn = get_connection()
    cursor = conn.cursor()
    cursor.execute("DELETE FROM chat_history WHERE username = ?", (username,))
    conn.commit()
    conn.close()

# def save_message(username, message, agent_type):
#     if isinstance(message, (list, tuple)):
#         message = "\n".join(str(m) for m in message)
#     conn = get_connection()
#     cursor = conn.cursor()
#     cursor.execute("INSERT INTO chat_history (username, message, agent_type) VALUES (?, ?, ?)", (username, message, agent_type))
#     conn.commit()
#     conn.close()


def load_history(username):
    conn = get_connection()
    cursor = conn.cursor()
    cursor.execute("SELECT message FROM chat_history WHERE username = ? ORDER BY timestamp ASC", (username,))
    rows = cursor.fetchall()
    conn.close()

    messages = [row[0] for row in rows]

    history = []
    for i in range(0, len(messages), 2):
        if i + 1 < len(messages):
            history.append([messages[i], messages[i + 1]])

    # Convert to ChatbotDataMessage format
    from utils.helpers import convert_list_to_chatbot_messages
    return convert_list_to_chatbot_messages(history)

# def load_history(username, agent_type):
#     conn = get_connection()
#     cursor = conn.cursor()
#     cursor.execute("SELECT message FROM chat_history WHERE username = ? ORDER BY timestamp ASC", (username,agent_type))
#     rows = cursor.fetchall()
#     conn.close()

#     messages = [row[0] for row in rows]

#     history = []
#     for i in range(0, len(messages), 2):
#         if i + 1 < len(messages):
#             history.append([messages[i], messages[i + 1]])
#     return history

def logout_user(state):
    state.value["user"] = None
    state.value["history"] = []
    return "Đã đăng xuất"

# def logout_user(state):
#     state.value["user"] = None
#     state.value["history"] = []
#     return "Đã đăng xuất"