refactored duplicate code in online_chat
parent
7e8f93e57d
commit
8fde953ccb
|
@ -4,7 +4,9 @@
|
|||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"This notebook was prepared by [Donne Martin](https://github.com/donnemartin). Source and license info is on [GitHub](https://github.com/donnemartin/system-design-primer)."
|
||||
"This notebook was prepared by [Donne Martin](https://github.com/donnemartin). Source and license info is on [GitHub](https://github.com/donnemartin/system-design-primer).\n",
|
||||
"\n",
|
||||
"This notebook was enhanced by [Parvez M Robin](https://github.com/parvezmrobin)."
|
||||
]
|
||||
},
|
||||
{
|
||||
|
@ -36,7 +38,7 @@
|
|||
" * Post a message to a group chat\n",
|
||||
" * Private 1-1 chat\n",
|
||||
" * Invite a friend to a private chat\n",
|
||||
" * Post a meesage to a private chat\n",
|
||||
" * Post a message to a private chat\n",
|
||||
"* No need to worry about scaling initially"
|
||||
]
|
||||
},
|
||||
|
@ -65,6 +67,7 @@
|
|||
"source": [
|
||||
"%%writefile online_chat.py\n",
|
||||
"from abc import ABCMeta\n",
|
||||
"from enum import Enum\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"class UserService(object):\n",
|
||||
|
@ -72,11 +75,20 @@
|
|||
" def __init__(self):\n",
|
||||
" self.users_by_id = {} # key: user id, value: User\n",
|
||||
"\n",
|
||||
" def add_user(self, user_id, name, pass_hash): # ...\n",
|
||||
" def remove_user(self, user_id): # ...\n",
|
||||
" def add_friend_request(self, from_user_id, to_user_id): # ...\n",
|
||||
" def approve_friend_request(self, from_user_id, to_user_id): # ...\n",
|
||||
" def reject_friend_request(self, from_user_id, to_user_id): # ...\n",
|
||||
" def add_user(self, user_id, name, pass_hash):\n",
|
||||
" pass\n",
|
||||
"\n",
|
||||
" def remove_user(self, user_id):\n",
|
||||
" pass\n",
|
||||
"\n",
|
||||
" def add_friend_request(self, from_user_id, to_user_id):\n",
|
||||
" pass\n",
|
||||
"\n",
|
||||
" def approve_friend_request(self, from_user_id, to_user_id):\n",
|
||||
" pass\n",
|
||||
"\n",
|
||||
" def reject_friend_request(self, from_user_id, to_user_id):\n",
|
||||
" pass\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"class User(object):\n",
|
||||
|
@ -91,34 +103,62 @@
|
|||
" self.received_friend_requests_by_friend_id = {} # key: friend id, value: AddRequest\n",
|
||||
" self.sent_friend_requests_by_friend_id = {} # key: friend id, value: AddRequest\n",
|
||||
"\n",
|
||||
" def message_user(self, friend_id, message): # ...\n",
|
||||
" def message_group(self, group_id, message): # ...\n",
|
||||
" def send_friend_request(self, friend_id): # ...\n",
|
||||
" def receive_friend_request(self, friend_id): # ...\n",
|
||||
" def approve_friend_request(self, friend_id): # ...\n",
|
||||
" def reject_friend_request(self, friend_id): # ...\n",
|
||||
" def message_user(self, friend_id, message):\n",
|
||||
" pass\n",
|
||||
"\n",
|
||||
" def message_group(self, group_id, message):\n",
|
||||
" pass\n",
|
||||
"\n",
|
||||
" def send_friend_request(self, friend_id):\n",
|
||||
" pass\n",
|
||||
"\n",
|
||||
" def check_pending_friend_requests(self):\n",
|
||||
" pass\n",
|
||||
"\n",
|
||||
" def approve_friend_request(self, friend_id):\n",
|
||||
" pass\n",
|
||||
"\n",
|
||||
" def reject_friend_request(self, friend_id):\n",
|
||||
" pass\n",
|
||||
"\n",
|
||||
" def remove_friend(self, friend_id):\n",
|
||||
" pass\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"class Chat(metaclass=ABCMeta):\n",
|
||||
"\n",
|
||||
" def __init__(self, chat_id):\n",
|
||||
" def __init__(self, chat_id, max_member):\n",
|
||||
" self.chat_id = chat_id\n",
|
||||
" self.users = []\n",
|
||||
" self.messages = []\n",
|
||||
" self.max_member = max_member\n",
|
||||
"\n",
|
||||
" def add_user(self, user):\n",
|
||||
" if len(self.users) == self.max_member:\n",
|
||||
" raise OverflowError('Chat {} already has maximum number of users'.format(self.chat_id))\n",
|
||||
" self.users.append(user)\n",
|
||||
"\n",
|
||||
" def remove_user(self, user):\n",
|
||||
" self.users.remove(user)\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"class PrivateChat(Chat):\n",
|
||||
"\n",
|
||||
" def __init__(self, first_user, second_user):\n",
|
||||
" super(PrivateChat, self).__init__()\n",
|
||||
" self.users.append(first_user)\n",
|
||||
" self.users.append(second_user)\n",
|
||||
" def __init__(self, chat_id, first_user, second_user):\n",
|
||||
" super(PrivateChat, self).__init__(chat_id, max_member=2)\n",
|
||||
" self.add_user(first_user)\n",
|
||||
" self.add_user(second_user)\n",
|
||||
"\n",
|
||||
" def add_user(self, user):\n",
|
||||
" if len(self.users) == self.max_member:\n",
|
||||
" raise OverflowError(\n",
|
||||
" 'PrivateChat {} cannot have more than {} members'.format(self.chat_id, self.max_member)\n",
|
||||
" )\n",
|
||||
" super(PrivateChat, self).add_user(user)\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"class GroupChat(Chat):\n",
|
||||
"\n",
|
||||
" def add_user(self, user): # ...\n",
|
||||
" def remove_user(self, user): # ... \n",
|
||||
" pass\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"class Message(object):\n",
|
||||
|
@ -139,7 +179,6 @@
|
|||
"\n",
|
||||
"\n",
|
||||
"class RequestStatus(Enum):\n",
|
||||
"\n",
|
||||
" UNREAD = 0\n",
|
||||
" READ = 1\n",
|
||||
" ACCEPTED = 2\n",
|
||||
|
|
|
@ -44,7 +44,7 @@ class User(object):
|
|||
def send_friend_request(self, friend_id):
|
||||
pass
|
||||
|
||||
def receive_friend_request(self, friend_id):
|
||||
def check_pending_friend_requests(self):
|
||||
pass
|
||||
|
||||
def approve_friend_request(self, friend_id):
|
||||
|
@ -53,30 +53,44 @@ class User(object):
|
|||
def reject_friend_request(self, friend_id):
|
||||
pass
|
||||
|
||||
def remove_friend(self, friend_id):
|
||||
pass
|
||||
|
||||
|
||||
class Chat(metaclass=ABCMeta):
|
||||
|
||||
def __init__(self, chat_id):
|
||||
def __init__(self, chat_id, max_member):
|
||||
self.chat_id = chat_id
|
||||
self.users = []
|
||||
self.messages = []
|
||||
self.max_member = max_member
|
||||
|
||||
def add_user(self, user):
|
||||
if len(self.users) == self.max_member:
|
||||
raise OverflowError('Chat {} already has maximum number of users'.format(self.chat_id))
|
||||
self.users.append(user)
|
||||
|
||||
def remove_user(self, user):
|
||||
self.users.remove(user)
|
||||
|
||||
|
||||
class PrivateChat(Chat):
|
||||
|
||||
def __init__(self, first_user, second_user):
|
||||
super(PrivateChat, self).__init__()
|
||||
self.users.append(first_user)
|
||||
self.users.append(second_user)
|
||||
def __init__(self, chat_id, first_user, second_user):
|
||||
super(PrivateChat, self).__init__(chat_id, max_member=2)
|
||||
self.add_user(first_user)
|
||||
self.add_user(second_user)
|
||||
|
||||
def add_user(self, user):
|
||||
if len(self.users) == self.max_member:
|
||||
raise OverflowError(
|
||||
'PrivateChat {} cannot have more than {} members'.format(self.chat_id, self.max_member)
|
||||
)
|
||||
super(PrivateChat, self).add_user(user)
|
||||
|
||||
|
||||
class GroupChat(Chat):
|
||||
|
||||
def add_user(self, user):
|
||||
pass
|
||||
|
||||
def remove_user(self, user):
|
||||
pass
|
||||
pass
|
||||
|
||||
|
||||
class Message(object):
|
||||
|
@ -97,7 +111,6 @@ class AddRequest(object):
|
|||
|
||||
|
||||
class RequestStatus(Enum):
|
||||
|
||||
UNREAD = 0
|
||||
READ = 1
|
||||
ACCEPTED = 2
|
||||
|
|
Loading…
Reference in New Issue