-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
165 lines (115 loc) · 4.3 KB
/
Copy pathutils.py
File metadata and controls
165 lines (115 loc) · 4.3 KB
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
import time
import secrets
import hashlib
import re
from datetime import datetime
def timestamp():
timestamp = int(time.time())
return timestamp
def hash_password(password, salt=None):
if salt is None:
salt = secrets.token_hex(32)
pass_salt = password + salt
pass_hash = hashlib.sha256(pass_salt.encode()).hexdigest()
return pass_hash, salt
def validate_username(username):
if not username:
return False, "Username cannot be empty."
if len(username) < 3:
return False, "Username must be at least 3 characters long."
if len(username) > 20:
return False, "Username cannot be longer than 20 characters."
if not re.match(r"^[a-zA-Z0-9_]+$", username):
return False, "Username can only contain letters, numbers, and underscores."
return True, ""
def validate_password(password):
if not password:
return False, "Password cannot be empty."
if len(password) < 6:
return False, "Password must be at least 6 characters long."
return True, ""
def verify_password(password, hashed_password, salt):
new_hash, _ = hash_password(password, salt)
if new_hash == hashed_password:
return True
return False
def generate_token():
return secrets.token_hex(32)
def generate_invite_code():
return secrets.token_urlsafe(8)
def format_timestamp(timestamp):
return time.strftime("%H:%M", time.localtime(timestamp))
def get_display_name(user):
if user.get("display_name"):
return user["display_name"]
else:
return user["username"]
def get_display_name_markup(user):
name = get_display_name(user)
color = user.get("name_color", "")
if color and color in ["white", "cyan", "green", "yellow", "magenta", "red", "blue", "bright_cyan", "bright_green", "bright_yellow", "bright_magenta", "bright_red"]:
bugged_colors = {
"white": "bright_white",
"cyan": "#00bcd4",
"green": "#4caf50",
"yellow": "#ffeb3b",
"magenta": "#e91e63",
"red": "#f44336",
"blue": "#2196f3",
"bright_cyan": "#00e5ff",
"bright_green": "#69ff47",
"bright_yellow": "#ffe066",
"bright_magenta":"#ff6ef7",
"bright_red": "#ff6b6b",
}
rich_color = bugged_colors.get(color, color)
return f"[bold {rich_color}]{name}[/bold {rich_color}]"
return f"[bold]{name}[/bold]"
def get_accent_color(user):
color = user.get("accent_color", "dark_blue")
if color not in ["dark_blue", "dark_green", "dark_red", "dark_magenta", "dark_cyan"]:
color = "dark_blue"
return color
def get_presence_indicator(user):
presence = user.get("presence", "online")
colors = {
"online": "green",
"dnd": "red",
"invisible": "dim",
"offline": "dim"
}
color_chosen = colors.get(presence, "green")
return f"[{color_chosen}]●[/{color_chosen}]"
def day_label(timestamp):
datee = datetime.fromtimestamp(timestamp).date()
today = datetime.now().date()
delta = (today - datee).days
if delta == 0:
return "Today"
elif delta == 1:
return "Yesterday"
else:
return datetime.fromtimestamp(timestamp).strftime("%B %d, %Y")
def should_compact(previous_message, current_message):
if previous_message is None:
return False
if previous_message["sender_id"] != current_message["sender_id"]:
return False
if not datetime.fromtimestamp(previous_message["created"]).date() == datetime.fromtimestamp(current_message["created"]).date():
return False
time_diff = current_message["created"] - previous_message["created"]
if time_diff > 300: # 5 minutes
return False
return True
def highlight_mention(content, username):
def replace(match):
mention = match.group(1)
if mention.lower() == username.lower():
return f"[bold white on #5865F2] @{mention} [/bold white on #5865F2]"
return f"[bold cyan]@{mention}[/bold cyan]"
return re.sub(r"@(\w+)", replace, content.replace("[", "\\["))
# def apply_theme(self, theme):
# if theme == "light":
# self.app.add_class("light-mode")
# else:
# self.app.remove_class("light-mode")