online_chat.py 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. from abc import ABCMeta
  2. class UserService(object):
  3. def __init__(self):
  4. self.users_by_id = {} # key: user id, value: User
  5. def add_user(self, user_id, name, pass_hash): # ...
  6. def remove_user(self, user_id): # ...
  7. def add_friend_request(self, from_user_id, to_user_id): # ...
  8. def approve_friend_request(self, from_user_id, to_user_id): # ...
  9. def reject_friend_request(self, from_user_id, to_user_id): # ...
  10. class User(object):
  11. def __init__(self, user_id, name, pass_hash):
  12. self.user_id = user_id
  13. self.name = name
  14. self.pass_hash = pass_hash
  15. self.friends_by_id = {} # key: friend id, value: User
  16. self.friend_ids_to_private_chats = {} # key: friend id, value: private chats
  17. self.group_chats_by_id = {} # key: chat id, value: GroupChat
  18. self.received_friend_requests_by_friend_id = {} # key: friend id, value: AddRequest
  19. self.sent_friend_requests_by_friend_id = {} # key: friend id, value: AddRequest
  20. def message_user(self, friend_id, message): # ...
  21. def message_group(self, group_id, message): # ...
  22. def send_friend_request(self, friend_id): # ...
  23. def receive_friend_request(self, friend_id): # ...
  24. def approve_friend_request(self, friend_id): # ...
  25. def reject_friend_request(self, friend_id): # ...
  26. class Chat(metaclass=ABCMeta):
  27. def __init__(self, chat_id):
  28. self.chat_id = chat_id
  29. self.users = []
  30. self.messages = []
  31. class PrivateChat(Chat):
  32. def __init__(self, first_user, second_user):
  33. super(PrivateChat, self).__init__()
  34. self.users.append(first_user)
  35. self.users.append(second_user)
  36. class GroupChat(Chat):
  37. def add_user(self, user): # ...
  38. def remove_user(self, user): # ...
  39. class Message(object):
  40. def __init__(self, message_id, message, timestamp):
  41. self.message_id = message_id
  42. self.message = message
  43. self.timestamp = timestamp
  44. class AddRequest(object):
  45. def __init__(self, from_user_id, to_user_id, request_status, timestamp):
  46. self.from_user_id = from_user_id
  47. self.to_user_id = to_user_id
  48. self.request_status = request_status
  49. self.timestamp = timestamp
  50. class RequestStatus(Enum):
  51. UNREAD = 0
  52. READ = 1
  53. ACCEPTED = 2
  54. REJECTED = 3