refactored duplicate code in online_chat
parent
7e8f93e57d
commit
8fde953ccb
|
@ -4,7 +4,9 @@
|
||||||
"cell_type": "markdown",
|
"cell_type": "markdown",
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"source": [
|
"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",
|
" * Post a message to a group chat\n",
|
||||||
" * Private 1-1 chat\n",
|
" * Private 1-1 chat\n",
|
||||||
" * Invite a friend to a private 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"
|
"* No need to worry about scaling initially"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
|
@ -65,6 +67,7 @@
|
||||||
"source": [
|
"source": [
|
||||||
"%%writefile online_chat.py\n",
|
"%%writefile online_chat.py\n",
|
||||||
"from abc import ABCMeta\n",
|
"from abc import ABCMeta\n",
|
||||||
|
"from enum import Enum\n",
|
||||||
"\n",
|
"\n",
|
||||||
"\n",
|
"\n",
|
||||||
"class UserService(object):\n",
|
"class UserService(object):\n",
|
||||||
|
@ -72,11 +75,20 @@
|
||||||
" def __init__(self):\n",
|
" def __init__(self):\n",
|
||||||
" self.users_by_id = {} # key: user id, value: User\n",
|
" self.users_by_id = {} # key: user id, value: User\n",
|
||||||
"\n",
|
"\n",
|
||||||
" def add_user(self, user_id, name, pass_hash): # ...\n",
|
" def add_user(self, user_id, name, pass_hash):\n",
|
||||||
" def remove_user(self, user_id): # ...\n",
|
" pass\n",
|
||||||
" def add_friend_request(self, from_user_id, to_user_id): # ...\n",
|
"\n",
|
||||||
" def approve_friend_request(self, from_user_id, to_user_id): # ...\n",
|
" def remove_user(self, user_id):\n",
|
||||||
" def reject_friend_request(self, from_user_id, to_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",
|
||||||
"\n",
|
"\n",
|
||||||
"class User(object):\n",
|
"class User(object):\n",
|
||||||
|
@ -91,34 +103,62 @@
|
||||||
" self.received_friend_requests_by_friend_id = {} # key: friend id, value: AddRequest\n",
|
" 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",
|
" self.sent_friend_requests_by_friend_id = {} # key: friend id, value: AddRequest\n",
|
||||||
"\n",
|
"\n",
|
||||||
" def message_user(self, friend_id, message): # ...\n",
|
" def message_user(self, friend_id, message):\n",
|
||||||
" def message_group(self, group_id, message): # ...\n",
|
" pass\n",
|
||||||
" def send_friend_request(self, friend_id): # ...\n",
|
"\n",
|
||||||
" def receive_friend_request(self, friend_id): # ...\n",
|
" def message_group(self, group_id, message):\n",
|
||||||
" def approve_friend_request(self, friend_id): # ...\n",
|
" pass\n",
|
||||||
" def reject_friend_request(self, friend_id): # ...\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",
|
||||||
"\n",
|
"\n",
|
||||||
"class Chat(metaclass=ABCMeta):\n",
|
"class Chat(metaclass=ABCMeta):\n",
|
||||||
"\n",
|
"\n",
|
||||||
" def __init__(self, chat_id):\n",
|
" def __init__(self, chat_id, max_member):\n",
|
||||||
" self.chat_id = chat_id\n",
|
" self.chat_id = chat_id\n",
|
||||||
" self.users = []\n",
|
" self.users = []\n",
|
||||||
" self.messages = []\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",
|
||||||
"\n",
|
"\n",
|
||||||
"class PrivateChat(Chat):\n",
|
"class PrivateChat(Chat):\n",
|
||||||
"\n",
|
"\n",
|
||||||
" def __init__(self, first_user, second_user):\n",
|
" def __init__(self, chat_id, first_user, second_user):\n",
|
||||||
" super(PrivateChat, self).__init__()\n",
|
" super(PrivateChat, self).__init__(chat_id, max_member=2)\n",
|
||||||
" self.users.append(first_user)\n",
|
" self.add_user(first_user)\n",
|
||||||
" self.users.append(second_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",
|
||||||
"\n",
|
"\n",
|
||||||
"class GroupChat(Chat):\n",
|
"class GroupChat(Chat):\n",
|
||||||
"\n",
|
" pass\n",
|
||||||
" def add_user(self, user): # ...\n",
|
|
||||||
" def remove_user(self, user): # ... \n",
|
|
||||||
"\n",
|
"\n",
|
||||||
"\n",
|
"\n",
|
||||||
"class Message(object):\n",
|
"class Message(object):\n",
|
||||||
|
@ -139,7 +179,6 @@
|
||||||
"\n",
|
"\n",
|
||||||
"\n",
|
"\n",
|
||||||
"class RequestStatus(Enum):\n",
|
"class RequestStatus(Enum):\n",
|
||||||
"\n",
|
|
||||||
" UNREAD = 0\n",
|
" UNREAD = 0\n",
|
||||||
" READ = 1\n",
|
" READ = 1\n",
|
||||||
" ACCEPTED = 2\n",
|
" ACCEPTED = 2\n",
|
||||||
|
|
|
@ -44,7 +44,7 @@ class User(object):
|
||||||
def send_friend_request(self, friend_id):
|
def send_friend_request(self, friend_id):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
def receive_friend_request(self, friend_id):
|
def check_pending_friend_requests(self):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
def approve_friend_request(self, friend_id):
|
def approve_friend_request(self, friend_id):
|
||||||
|
@ -53,30 +53,44 @@ class User(object):
|
||||||
def reject_friend_request(self, friend_id):
|
def reject_friend_request(self, friend_id):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
def remove_friend(self, friend_id):
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
class Chat(metaclass=ABCMeta):
|
class Chat(metaclass=ABCMeta):
|
||||||
|
|
||||||
def __init__(self, chat_id):
|
def __init__(self, chat_id, max_member):
|
||||||
self.chat_id = chat_id
|
self.chat_id = chat_id
|
||||||
self.users = []
|
self.users = []
|
||||||
self.messages = []
|
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):
|
class PrivateChat(Chat):
|
||||||
|
|
||||||
def __init__(self, first_user, second_user):
|
def __init__(self, chat_id, first_user, second_user):
|
||||||
super(PrivateChat, self).__init__()
|
super(PrivateChat, self).__init__(chat_id, max_member=2)
|
||||||
self.users.append(first_user)
|
self.add_user(first_user)
|
||||||
self.users.append(second_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):
|
class GroupChat(Chat):
|
||||||
|
pass
|
||||||
def add_user(self, user):
|
|
||||||
pass
|
|
||||||
|
|
||||||
def remove_user(self, user):
|
|
||||||
pass
|
|
||||||
|
|
||||||
|
|
||||||
class Message(object):
|
class Message(object):
|
||||||
|
@ -97,7 +111,6 @@ class AddRequest(object):
|
||||||
|
|
||||||
|
|
||||||
class RequestStatus(Enum):
|
class RequestStatus(Enum):
|
||||||
|
|
||||||
UNREAD = 0
|
UNREAD = 0
|
||||||
READ = 1
|
READ = 1
|
||||||
ACCEPTED = 2
|
ACCEPTED = 2
|
||||||
|
|
Loading…
Reference in New Issue