-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatabase.py
530 lines (458 loc) · 19.4 KB
/
database.py
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
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
import sqlite3
import helpers
import hashlib
class Database():
FILE = ""
def __init__(self, filename: str):
self.FILE = filename
print("db: setupdb - Creating/Updating tables")
conn, cursor = self.handle()
# roles table for permission management
# ID, Name (e.g., 'user', 'moderator', 'admin')
cursor.execute('''CREATE TABLE IF NOT EXISTS roles (
role_id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT UNIQUE NOT NULL
)''')
# boards table for categorizing posts
# ID, Name, Description
# Note: Simple independent boards. If a hierarchy is needed, add a parent_board_id
cursor.execute('''CREATE TABLE IF NOT EXISTS boards (
board_id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT UNIQUE NOT NULL,
description TEXT
)''')
# users table
# Added role_id foreign key
cursor.execute('''CREATE TABLE IF NOT EXISTS users (
user_id INTEGER PRIMARY KEY AUTOINCREMENT,
username TEXT UNIQUE NOT NULL,
password TEXT NOT NULL, -- Should store password hash
cookie TEXT UNIQUE, -- For remember me functionality, optional
role_id INTEGER DEFAULT 1, -- Default role_id, e.g., 1 for 'user'
FOREIGN KEY (role_id) REFERENCES roles (role_id)
)''')
# posts table
# Changed board TEXT to board_id INTEGER FOREIGN KEY
# Removed Reputation (JSON) and Lastrep
# Added total_upvotes for quick access
cursor.execute('''CREATE TABLE IF NOT EXISTS posts (
post_id INTEGER PRIMARY KEY AUTOINCREMENT,
board_id INTEGER NOT NULL, -- Link to the boards table
owner_id INTEGER, -- Link to the users table (can be NULL for anonymous)
title TEXT,
description TEXT, -- Or content for the main post body
image_url TEXT, -- Added field for image URL/path
created_at INTEGER NOT NULL,
updated_at INTEGER NOT NULL, -- To track last activity
FOREIGN KEY (board_id) REFERENCES boards (board_id),
FOREIGN KEY (owner_id) REFERENCES users (user_id)
)''')
# Note: You might want a separate table for post images if multiple images per post are allowed.
# comments table
cursor.execute('''CREATE TABLE IF NOT EXISTS comments (
comment_id INTEGER PRIMARY KEY AUTOINCREMENT,
post_id INTEGER NOT NULL, -- Link to the posts table
owner_id INTEGER, -- Link to the users table (can be NULL for anonymous)
parent_comment_id INTEGER, -- Link to parent comment for threading
content TEXT NOT NULL,
created_at INTEGER NOT NULL,
updated_at INTEGER NOT NULL, -- To track last activity
FOREIGN KEY (post_id) REFERENCES posts (post_id),
FOREIGN KEY (owner_id) REFERENCES users (user_id),
FOREIGN KEY (parent_comment_id) REFERENCES comments (comment_id)
)''')
# for captchas
cursor.execute('''CREATE TABLE IF NOT EXISTS captchas (
captcha_id INTEGER PRIMARY KEY AUTOINCREMENT,
captcha_token TEXT NOT NULL,
created_at INTEGER NOT NULL,
wave INTEGER NOT NULL,
wave_two_solution TEXT NOT NULL
)''')
conn.commit()
print("db: Tables checked/created successfully.")
conn.close()
if self.is_first_setup():
print("Is first setup? Yes.")
self.first_setup()
def is_first_setup(self):
conn, cursor = self.handle()
cursor.execute("SELECT COUNT(*) FROM boards;")
yes = cursor.fetchone()[0] == 0
conn.close()
return yes
def first_setup(self):
conn, cursor = self.handle()
# Roles
cursor.execute('INSERT INTO roles (name) VALUES ("member")')
cursor.execute('INSERT INTO roles (name) VALUES ("moderator")')
cursor.execute('INSERT INTO roles (name) VALUES ("administator")')
# Boards
cursor.execute('INSERT INTO boards (name, description) VALUES ("random", "the default board, discussion about anything")')
cursor.execute('INSERT INTO boards (name, description) VALUES ("qna", "board for questions and answers only")')
# Finish
conn.commit()
conn.close()
def handle(self):
"""
Get a handle to the database.
Returns: Connection Object, Cursor Object
"""
conn = sqlite3.connect(self.FILE)
cursor = conn.cursor()
cursor.execute('PRAGMA foreign_keys = ON;')
return conn, cursor
def board_id_from_name(self, name):
conn, cursor = self.handle()
cursor.execute("SELECT board_id FROM boards WHERE name = ? LIMIT 1", (name,))
id = cursor.fetchone()[0]
return id
def new_post(self, board, title, description, image_url = None, owner = None):
conn, cursor = self.handle()
board_id = self.board_id_from_name(board)
created_at, updated_at = helpers.timestamp(), helpers.timestamp()
cursor.execute("""INSERT INTO posts (board_id, owner_id, title, description, image_url, created_at, updated_at)
VALUES (?, ?, ?, ?, ?, ?, ?)""",
(board_id, owner, title, description, image_url, created_at, updated_at))
conn.commit()
conn.close()
def list_boards(self):
conn, cursor = self.handle()
cursor.execute("SELECT name FROM boards ORDER BY board_id ASC")
boards = [board[0] for board in cursor.fetchall()]
conn.close()
return boards # board id 0 first
def all_newest_posts(self, page = 0, top = 25):
conn, cursor = self.handle()
sql = """
SELECT
p.post_id,
p.title,
p.description,
p.image_url,
p.created_at,
p.updated_at,
b.name AS board_name,
u.username AS owner_username
FROM
posts p
JOIN
boards b ON p.board_id = b.board_id
LEFT JOIN
users u ON p.owner_id = u.user_id
ORDER BY
p.updated_at DESC
LIMIT ? OFFSET ?
"""
posts_list = []
try:
cursor.execute(sql, (top, page*top))
rows = cursor.fetchall()
for row in rows:
post_dict = {
'post_id': row[0], # p.post_id
'title': row[1], # p.title
'description': row[2], # p.description
'image_url': row[3], # p.image_url
'created_at': helpers.time_ago(row[4]), # p.created_at
'updated_at': row[5], # p.updated_at
'board': row[6], # b.name (board_name alias)
'owner': row[7] if row[7] is not None else 'Anonymous'
}
posts_list.append(post_dict)
except Exception as e:
posts_list = []
finally:
conn.close()
return posts_list
def newest_posts(self, board, page = 0, top = 25):
conn, cursor = self.handle()
board_id = self.board_id_from_name(board)
if board_id is None:
print(f"Info: Board '{board}' not found.")
conn.close()
return []
sql = """
SELECT
p.post_id,
p.title,
p.description,
p.image_url,
p.created_at,
p.updated_at,
b.name AS board_name,
u.username AS owner_username
FROM
posts p
JOIN
boards b ON p.board_id = b.board_id
LEFT JOIN
users u ON p.owner_id = u.user_id
WHERE
p.board_id = ?
ORDER BY
p.updated_at DESC
LIMIT ? OFFSET ?
"""
posts_list = []
try:
cursor.execute(sql, (board_id, top, page*top))
rows = cursor.fetchall()
for row in rows:
post_dict = {
'post_id': row[0], # p.post_id
'title': row[1], # p.title
'description': row[2], # p.description
'image_url': row[3], # p.image_url
'created_at': helpers.time_ago(row[4]), # p.created_at
'updated_at': row[5], # p.updated_at
'board': row[6], # b.name (board_name alias)
'owner': row[7] if row[7] is not None else 'Anonymous'
}
posts_list.append(post_dict)
except Exception as e:
posts_list = []
finally:
conn.close()
return posts_list
#### CAPTCHA ####
def captcha_create(self):
conn, cursor = self.handle()
wave = 1
captcha_token = helpers.generate_uuid()
wave_two_solution = helpers.generate_short_code()
created_at = helpers.timestamp()
cursor.execute("INSERT INTO captchas (captcha_token, created_at, wave, wave_two_solution) VALUES (?, ?, ?, ?)",
(captcha_token, created_at, wave, wave_two_solution))
conn.commit()
conn.close()
# could be put anywhere but here is a good place i think
self.captcha_purge()
return captcha_token
def captcha_delete(self, captcha_token):
conn, cursor = self.handle()
cursor.execute("DELETE FROM captchas WHERE captcha_token = ?", (captcha_token,))
conn.commit()
conn.close()
def captcha_purge(self):
conn, cursor = self.handle()
created = helpers.timestamp()
# remove old captchas, solved 10min+ ago
time_threshold = helpers.timestamp() - 600
cursor.execute("DELETE FROM captchas WHERE created_at < ?", (time_threshold,))
conn.commit()
conn.close()
def captcha_check_wave_1(self, captcha_token, nonce, difficulty=15):
conn, cursor = self.handle()
# check if the challenge is even valid, if not, spoofed solution
cursor.execute("SELECT COUNT(*) FROM captchas WHERE captcha_token = ?", (captcha_token,))
count = cursor.fetchone()[0]
if count == 0:
conn.close()
return False # captcha was never even created
# captchas already on wave 2 are also invalid
cursor.execute("SELECT wave FROM captchas WHERE captcha_token = ?", (captcha_token,))
wave = cursor.fetchone()[0]
if wave == 2:
conn.close()
return False
# compute hash challenge
challenge = "popcap-" + captcha_token + "-popcap-" + nonce + "-popcap"
completed = hashlib.sha256(challenge.encode('utf-8')).hexdigest()
# validity check
is_valid = completed.count('0') >= difficulty
# if valid, mark captcha as wave 2, if not, remove captcha from database
if is_valid:
cursor.execute("UPDATE captchas SET wave = 2 WHERE captcha_token = ?", (captcha_token,))
else:
self.captcha_delete(captcha_token)
conn.commit()
conn.close()
return is_valid
def captcha_check_wave_2(self, captcha_token, wave_two_input):
conn, cursor = self.handle()
# get the w2 solution from db
cursor.execute("SELECT wave, wave_two_solution FROM captchas WHERE captcha_token = ?", (captcha_token,))
result = cursor.fetchone()
if result is None: return False
wave, wave_two_solution = result
# do a check if wave 1 was actually solved
if wave == 1:
self.captcha_delete(captcha_token)
return False # wave 1 was not solved, so captcha is invalid
# check if solution is correct
is_valid = wave_two_solution == wave_two_input
# wether valid or not, captcha is no longer needed
self.captcha_delete(captcha_token)
# return state
return is_valid
def captcha_get_wave_two_solution(self, captcha_token):
conn, cursor = self.handle()
cursor.execute("SELECT wave_two_solution FROM captchas WHERE captcha_token = ?", (captcha_token,))
wave2_sol = cursor.fetchone()[0]
conn.close()
return wave2_sol
#### ENDOFCAPTCHA ####
def board_description(self, board):
conn, cursor = self.handle()
cursor.execute("SELECT description FROM boards WHERE name = ? LIMIT 1", (board,))
desc = cursor.fetchone()[0]
conn.close()
return desc
def get_post_by_id(self, post_id):
conn, cursor = self.handle()
sql = """
SELECT
p.post_id,
p.title,
p.description,
p.image_url,
p.created_at,
p.updated_at,
b.name AS board_name,
u.username AS owner_username
FROM
posts p
JOIN
boards b ON p.board_id = b.board_id
LEFT JOIN
users u ON p.owner_id = u.user_id
WHERE
p.post_id = ?
LIMIT 1
"""
cursor.execute(sql, (post_id,))
row = cursor.fetchone()
post_dict = {
'post_id': row[0], # p.post_id
'title': row[1], # p.title
'description': row[2], # p.description
'image_url': row[3], # p.image_url
'created_at': helpers.time_ago(row[4]), # p.created_at
'updated_at': row[5], # p.updated_at
'board': row[6], # b.name (board_name alias)
'owner': row[7] if row[7] is not None else 'Anonymous'
}
conn.close()
return post_dict
def get_comments_by_post_id(self, post_id):
conn, cursor = self.handle()
sql = """
SELECT
c.comment_id,
c.post_id,
c.owner_id,
c.parent_comment_id,
c.content,
c.created_at,
c.updated_at,
u.username AS owner_username
FROM
comments c
LEFT JOIN
users u ON c.owner_id = u.user_id
WHERE
c.post_id = ?
ORDER BY
c.created_at DESC
"""
comments_list = []
try:
cursor.execute(sql, (post_id,))
rows = cursor.fetchall()
for row in rows:
comment_dict = {
'comment_id': row[0],
'post_id': row[1],
'owner_id': row[2],
'parent_comment_id': row[3],
'content': row[4],
'created_at': helpers.time_ago(row[5]), # Format timestamp
'updated_at': row[6],
'owner': row[7] if row[7] is not None else 'Anonymous',
'replies': [] # Initialize replies list for tree building
}
comments_list.append(comment_dict)
except Exception as e:
print(f"Error fetching comments: {e}")
comments_list = []
finally:
conn.close()
return comments_list
def new_comment(self, post_id, content, owner_id=None, parent_comment_id=None):
conn, cursor = self.handle()
created_at, updated_at = helpers.timestamp(), helpers.timestamp()
try:
cursor.execute("""INSERT INTO comments (post_id, owner_id, parent_comment_id, content, created_at, updated_at)
VALUES (?, ?, ?, ?, ?, ?)""",
(post_id, owner_id, parent_comment_id, content, created_at, updated_at))
cursor.execute("UPDATE posts SET updated_at=? WHERE post_id=?", (updated_at, post_id))
conn.commit()
print(f"New comment created for post {post_id}.")
except Exception as e:
print(f"Error creating comment: {e}")
conn.rollback()
finally:
conn.close()
# Helper function to build a threaded comment tree from a flat list
def build_comment_tree(self, comments_list):
comment_dict = {c['comment_id']: c for c in comments_list}
root_comments = []
for comment in comments_list:
if comment['parent_comment_id'] is None:
root_comments.append(comment)
else:
parent = comment_dict.get(comment['parent_comment_id'])
if parent is not None:
parent['replies'].append(comment)
# Handle orphaned replies if parent_comment_id exists but parent doesn't exist in list
# For this implementation, we'll just ignore them or add them as root if needed,
# but typically you'd ensure data integrity or handle this case.
# Appending to root if parent not found:
# else:
# root_comments.append(comment)
# Optional: Sort replies by creation date
for comment in comment_dict.values():
if 'replies' in comment:
comment['replies'].sort(key=lambda x: x['updated_at']) # Sorting by updated_at as created_at is already formatted
# Sort root comments by creation date
root_comments.sort(key=lambda x: x['updated_at'])
return root_comments
# AUTH
def get_id_from_cookie(self, cookie):
conn, cursor = self.handle()
cursor.execute("SELECT user_id FROM users WHERE cookie = ?", (cookie,))
user_id = cursor.fetchone()
conn.close()
return user_id[0] if user_id else None
def get_name_from_cookie(self, cookie):
conn, cursor = self.handle()
cursor.execute("SELECT username FROM users WHERE cookie = ?", (cookie,))
user_name = cursor.fetchone()
conn.close()
return user_name[0] if user_name else None
def account_exists(self, username):
conn, cursor = self.handle()
cursor.execute("SELECT COUNT(*) FROM users WHERE username = ?", (username,))
count = cursor.fetchone()[0]
conn.close()
return count != 0
def new_account(self, username, password):
conn, cursor = self.handle()
cookie = helpers.generate_cookie_code()
cursor.execute("INSERT INTO users (username, password, cookie) VALUES (?, ?, ?)", (username, password, cookie))
conn.commit()
conn.close()
return cookie
def account_login(self, username, password):
conn, cursor = self.handle()
# check if password is valid
cursor.execute("SELECT password FROM users WHERE username = ?", (username,))
real_password = cursor.fetchone()[0]
if password != real_password:
return False
cookie = helpers.generate_cookie_code()
cursor.execute("UPDATE users SET cookie = ? WHERE username = ?", (cookie, username))
conn.commit()
conn.close()
return cookie