-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjson_storage.py
More file actions
198 lines (167 loc) · 6.07 KB
/
json_storage.py
File metadata and controls
198 lines (167 loc) · 6.07 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
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
"""
JSON storage module for the 0xC Chat application.
This module provides functions to read and write data to JSON files,
serving as a simple persistence layer for the application.
"""
import json
import os
from datetime import datetime
from typing import Dict, List, Any, Optional
from env import DATA_DIR
# File paths for JSON storage
USERS_FILE = os.path.join(DATA_DIR, "users.json")
MESSAGES_FILE = os.path.join(DATA_DIR, "messages.json")
TOKENS_FILE = os.path.join(DATA_DIR, "tokens.json")
# Initialize empty data structures if files don't exist
def init_storage():
"""Initialize the JSON storage files if they don't exist."""
# Ensure data directory exists
os.makedirs(DATA_DIR, exist_ok=True)
if not os.path.exists(USERS_FILE):
with open(USERS_FILE, 'w') as f:
json.dump([], f)
if not os.path.exists(MESSAGES_FILE):
with open(MESSAGES_FILE, 'w') as f:
json.dump([], f)
if not os.path.exists(TOKENS_FILE):
with open(TOKENS_FILE, 'w') as f:
json.dump([], f)
# User storage functions
def get_users() -> List[Dict[str, Any]]:
"""Get all users from the JSON file."""
# Ensure data directory exists
os.makedirs(DATA_DIR, exist_ok=True)
try:
with open(USERS_FILE, 'r') as f:
return json.load(f)
except (FileNotFoundError, json.JSONDecodeError):
# If file doesn't exist or is invalid, initialize it
with open(USERS_FILE, 'w') as f:
json.dump([], f)
return []
def save_users(users: List[Dict[str, Any]]) -> None:
"""Save users to the JSON file."""
# Ensure data directory exists
os.makedirs(DATA_DIR, exist_ok=True)
with open(USERS_FILE, 'w') as f:
json.dump(users, f, indent=2)
def get_user_by_id(user_id: str) -> Optional[Dict[str, Any]]:
"""Get a user by ID."""
users = get_users()
for user in users:
if user['id'] == user_id:
return user
return None
def get_user_by_username(username: str) -> Optional[Dict[str, Any]]:
"""Get a user by username."""
users = get_users()
for user in users:
if user['username'] == username:
return user
return None
def add_user(user: Dict[str, Any]) -> Dict[str, Any]:
"""Add a new user."""
users = get_users()
users.append(user)
save_users(users)
return user
def update_user(user_id: str, updated_data: Dict[str, Any]) -> Optional[Dict[str, Any]]:
"""Update a user's data."""
users = get_users()
for i, user in enumerate(users):
if user['id'] == user_id:
users[i].update(updated_data)
save_users(users)
return users[i]
return None
# Message storage functions
def get_messages() -> List[Dict[str, Any]]:
"""Get all messages from the JSON file."""
# Ensure data directory exists
os.makedirs(DATA_DIR, exist_ok=True)
try:
with open(MESSAGES_FILE, 'r') as f:
return json.load(f)
except (FileNotFoundError, json.JSONDecodeError):
# If file doesn't exist or is invalid, initialize it
with open(MESSAGES_FILE, 'w') as f:
json.dump([], f)
return []
def save_messages(messages: List[Dict[str, Any]]) -> None:
"""Save messages to the JSON file."""
# Ensure data directory exists
os.makedirs(DATA_DIR, exist_ok=True)
with open(MESSAGES_FILE, 'w') as f:
json.dump(messages, f, indent=2)
def get_message_by_id(message_id: str) -> Optional[Dict[str, Any]]:
"""Get a message by ID."""
messages = get_messages()
for message in messages:
if message['id'] == message_id:
return message
return None
def get_messages_by_user(user_id: str) -> List[Dict[str, Any]]:
"""Get all messages by a specific user."""
messages = get_messages()
return [message for message in messages if message['user_id'] == user_id]
def add_message(message: Dict[str, Any]) -> Dict[str, Any]:
"""Add a new message."""
messages = get_messages()
messages.append(message)
save_messages(messages)
return message
def delete_message(message_id: str, user_id: Optional[str] = None) -> Optional[Dict[str, Any]]:
"""Delete a message by ID, optionally checking user ownership."""
messages = get_messages()
for i, message in enumerate(messages):
if message['id'] == message_id:
# If user_id is provided, check ownership
if user_id and message['user_id'] != user_id:
return None
deleted_message = messages.pop(i)
save_messages(messages)
return deleted_message
return None
# Token storage functions
def get_tokens() -> List[Dict[str, Any]]:
"""Get all refresh tokens from the JSON file."""
# Ensure data directory exists
os.makedirs(DATA_DIR, exist_ok=True)
try:
with open(TOKENS_FILE, 'r') as f:
return json.load(f)
except (FileNotFoundError, json.JSONDecodeError):
# If file doesn't exist or is invalid, initialize it
with open(TOKENS_FILE, 'w') as f:
json.dump([], f)
return []
def save_tokens(tokens: List[Dict[str, Any]]) -> None:
"""Save refresh tokens to the JSON file."""
# Ensure data directory exists
os.makedirs(DATA_DIR, exist_ok=True)
with open(TOKENS_FILE, 'w') as f:
json.dump(tokens, f, indent=2)
def get_token_by_id(token_id: str) -> Optional[Dict[str, Any]]:
"""Get a refresh token by ID."""
tokens = get_tokens()
for token in tokens:
if token['id'] == token_id:
return token
return None
def add_token(token: Dict[str, Any]) -> Dict[str, Any]:
"""Add a new refresh token."""
tokens = get_tokens()
tokens.append(token)
save_tokens(tokens)
return token
def delete_token(token_id: str) -> Optional[Dict[str, Any]]:
"""Delete a refresh token by ID."""
tokens = get_tokens()
for i, token in enumerate(tokens):
if token['id'] == token_id:
deleted_token = tokens.pop(i)
save_tokens(tokens)
return deleted_token
return None
# Initialize storage on module import
init_storage()