-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpersistence.py
92 lines (71 loc) · 3.38 KB
/
persistence.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
import socket
from datetime import datetime
from typing import List
from exceptions import UidNotFoundException, DuplicateUidException, OperationNotPermitted
class Client():
def __init__(self, uid: str, client_socket: socket):
self.uid: str = uid
self.socket: socket.SocketType = client_socket
self.is_online: bool = True
self.last_online: datetime.date = datetime.now()
self.buffered_messages: List[str] = []
class Group():
def __init__(self, uid: str, owner: Client):
self.uid: str = uid
self.owner: Client = owner
self.members: List[Client] = [owner]
def add_member(self, member: Client, client: Client):
if(client is not self.owner):
raise OperationNotPermitted(f"{client.uid} is not authorized to modify group {self.uid}")
if(member not in self.members):
self.members.append(member)
def remove_member(self, member: Client, client: Client):
if(client is not self.owner):
raise OperationNotPermitted(f"{client.uid} is not authorized to modify group {self.uid}")
if(member is self.owner):
raise OperationNotPermitted(f"Owner {client.uid} cannot be removed from group {self.uid}")
if(member in self.members):
self.members.remove(member)
class Persistence():
__clients = {}
__groups = {}
def __init__(self) -> None:
pass
def create_client(self, uid: str, client_socket: socket) -> Client:
if uid in self.__clients.keys():
raise DuplicateUidException(f"User name {uid} already exists")
self.__clients.update({uid: Client(uid, client_socket)})
return self.__clients.get(uid)
def get_client(self, uid: str) -> Client:
if uid not in self.__clients.keys():
raise UidNotFoundException(f"User {uid} cannot be found")
return self.__clients.get(uid)
def has_client(self, uid: str) -> bool:
return uid in self.__clients.keys()
def create_group(self, uid: str, owner: Client) -> Group:
if uid in self.__groups.keys():
raise DuplicateUidException(f"Group name {uid} already exists")
self.__groups.update({uid: Group(uid, owner)})
return self.__groups.get(uid)
def get_group(self, uid: str):
if uid not in self.__groups.keys():
raise UidNotFoundException(f"Group {uid} cannot be found")
return self.__groups.get(uid)
def delete_group(self, uid: str, client: Client):
_group: Group = self.get_group(uid)
if(self.__can_modify(client, _group)):
self.__groups.pop(uid)
def has_group(self, uid: str):
return uid in self.__groups.keys()
def rename_group(self, old_uid: str, new_uid: str, client: Client):
_group: Group = self.get_group(old_uid)
if(self.__can_modify(client, _group)):
if(new_uid in self.__groups.keys()):
raise DuplicateUidException(f"Group {new_uid} already exists")
_group.uid = new_uid
self.__groups.update({new_uid: _group})
self.__groups.pop(old_uid)
def __can_modify(self, client: Client, group: Group) -> bool:
if group.owner.uid is not client.uid:
raise OperationNotPermitted(f"{client.uid} is is not authorized to modify group {group.uid}")
return True