-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathdb.py
91 lines (57 loc) · 3.39 KB
/
db.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
from datetime import datetime
from bson import ObjectId
from pymongo import MongoClient, DESCENDING
from werkzeug.security import generate_password_hash
from user import User
client = MongoClient("mongodb+srv://test:test@chatapp-q7wkc.mongodb.net/test?retryWrites=true&w=majority")
chat_db = client.get_database("ChatDB")
users_collection = chat_db.get_collection("users")
rooms_collection = chat_db.get_collection("rooms")
room_members_collection = chat_db.get_collection("room_members")
messages_collection = chat_db.get_collection("messages")
def save_user(username, email, password):
password_hash = generate_password_hash(password)
users_collection.insert_one({'_id': username, 'email': email, 'password': password_hash})
def get_user(username):
user_data = users_collection.find_one({'_id': username})
return User(user_data['_id'], user_data['email'], user_data['password']) if user_data else None
def save_room(room_name, created_by):
room_id = rooms_collection.insert_one(
{'name': room_name, 'created_by': created_by, 'created_at': datetime.now()}).inserted_id
add_room_member(room_id, room_name, created_by, created_by, is_room_admin=True)
return room_id
def update_room(room_id, room_name):
rooms_collection.update_one({'_id': ObjectId(room_id)}, {'$set': {'name': room_name}})
room_members_collection.update_many({'_id.room_id': ObjectId(room_id)}, {'$set': {'room_name': room_name}})
def get_room(room_id):
return rooms_collection.find_one({'_id': ObjectId(room_id)})
def add_room_member(room_id, room_name, username, added_by, is_room_admin=False):
room_members_collection.insert_one(
{'_id': {'room_id': ObjectId(room_id), 'username': username}, 'room_name': room_name, 'added_by': added_by,
'added_at': datetime.now(), 'is_room_admin': is_room_admin})
def add_room_members(room_id, room_name, usernames, added_by):
room_members_collection.insert_many(
[{'_id': {'room_id': ObjectId(room_id), 'username': username}, 'room_name': room_name, 'added_by': added_by,
'added_at': datetime.now(), 'is_room_admin': False} for username in usernames])
def remove_room_members(room_id, usernames):
room_members_collection.delete_many(
{'_id': {'$in': [{'room_id': ObjectId(room_id), 'username': username} for username in usernames]}})
def get_room_members(room_id):
return list(room_members_collection.find({'_id.room_id': ObjectId(room_id)}))
def get_rooms_for_user(username):
return list(room_members_collection.find({'_id.username': username}))
def is_room_member(room_id, username):
return room_members_collection.count_documents({'_id': {'room_id': ObjectId(room_id), 'username': username}})
def is_room_admin(room_id, username):
return room_members_collection.count_documents(
{'_id': {'room_id': ObjectId(room_id), 'username': username}, 'is_room_admin': True})
def save_message(room_id, text, sender):
messages_collection.insert_one({'room_id': room_id, 'text': text, 'sender': sender, 'created_at': datetime.now()})
MESSAGE_FETCH_LIMIT = 3
def get_messages(room_id, page=0):
offset = page * MESSAGE_FETCH_LIMIT
messages = list(
messages_collection.find({'room_id': room_id}).sort('_id', DESCENDING).limit(MESSAGE_FETCH_LIMIT).skip(offset))
for message in messages:
message['created_at'] = message['created_at'].strftime("%d %b, %H:%M")
return messages[::-1]