Commit 624e0c2f authored by Donne Martin's avatar Donne Martin
Browse files

Simplify online chat solution

parent 992cd5b1
...@@ -69,8 +69,6 @@ ...@@ -69,8 +69,6 @@
"\n", "\n",
"class UserService(object):\n", "class UserService(object):\n",
"\n", "\n",
" __metaclass__ = Singleton\n",
"\n",
" 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",
...@@ -145,16 +143,7 @@ ...@@ -145,16 +143,7 @@
" UNREAD = 0\n", " UNREAD = 0\n",
" READ = 1\n", " READ = 1\n",
" ACCEPTED = 2\n", " ACCEPTED = 2\n",
" REJECTED = 3\n", " REJECTED = 3"
"\n",
"\n",
"class Singleton(type):\n",
"\n",
" _instances = {}\n",
" def __call__(cls, *args, **kwargs):\n",
" if cls not in cls._instances:\n",
" cls._instances[cls] = super(Singleton, cls).__call__(*args, **kwargs)\n",
" return cls._instances[cls]"
] ]
} }
], ],
......
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
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-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-primer).
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
# Design an online chat # Design an online chat
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
## Constraints and assumptions ## Constraints and assumptions
* Assume we'll focus on the following workflows: * Assume we'll focus on the following workflows:
* Text conversations only * Text conversations only
* Users * Users
* Add a user * Add a user
* Remove a user * Remove a user
* Update a user * Update a user
* Add to a user's friends list * Add to a user's friends list
* Add friend request * Add friend request
* Approve friend request * Approve friend request
* Reject friend request * Reject friend request
* Remove from a user's friends list * Remove from a user's friends list
* Create a group chat * Create a group chat
* Invite friends to a group chat * Invite friends to a group chat
* Post a message to a group chat * Post a message to a group chat
* Private 1-1 chat * Private 1-1 chat
* Invite a friend to a private chat * Invite a friend to a private chat
* Post a meesage to a private chat * Post a meesage to a private chat
* No need to worry about scaling initially * No need to worry about scaling initially
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
## Solution ## Solution
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
%%writefile online_chat.py %%writefile online_chat.py
from abc import ABCMeta from abc import ABCMeta
class UserService(object): class UserService(object):
__metaclass__ = Singleton
def __init__(self): def __init__(self):
self.users_by_id = {} # key: user id, value: User self.users_by_id = {} # key: user id, value: User
def add_user(self, user_id, name, pass_hash): # ... def add_user(self, user_id, name, pass_hash): # ...
def remove_user(self, user_id): # ... def remove_user(self, user_id): # ...
def add_friend_request(self, from_user_id, to_user_id): # ... def add_friend_request(self, from_user_id, to_user_id): # ...
def approve_friend_request(self, from_user_id, to_user_id): # ... def approve_friend_request(self, from_user_id, to_user_id): # ...
def reject_friend_request(self, from_user_id, to_user_id): # ... def reject_friend_request(self, from_user_id, to_user_id): # ...
class User(object): class User(object):
def __init__(self, user_id, name, pass_hash): def __init__(self, user_id, name, pass_hash):
self.user_id = user_id self.user_id = user_id
self.name = name self.name = name
self.pass_hash = pass_hash self.pass_hash = pass_hash
self.friends_by_id = {} # key: friend id, value: User self.friends_by_id = {} # key: friend id, value: User
self.friend_ids_to_private_chats = {} # key: friend id, value: private chats self.friend_ids_to_private_chats = {} # key: friend id, value: private chats
self.group_chats_by_id = {} # key: chat id, value: GroupChat self.group_chats_by_id = {} # key: chat id, value: GroupChat
self.received_friend_requests_by_friend_id = {} # key: friend id, value: AddRequest self.received_friend_requests_by_friend_id = {} # key: friend id, value: AddRequest
self.sent_friend_requests_by_friend_id = {} # key: friend id, value: AddRequest self.sent_friend_requests_by_friend_id = {} # key: friend id, value: AddRequest
def message_user(self, friend_id, message): # ... def message_user(self, friend_id, message): # ...
def message_group(self, group_id, message): # ... def message_group(self, group_id, message): # ...
def send_friend_request(self, friend_id): # ... def send_friend_request(self, friend_id): # ...
def receive_friend_request(self, friend_id): # ... def receive_friend_request(self, friend_id): # ...
def approve_friend_request(self, friend_id): # ... def approve_friend_request(self, friend_id): # ...
def reject_friend_request(self, friend_id): # ... def reject_friend_request(self, friend_id): # ...
class Chat(metaclass=ABCMeta): class Chat(metaclass=ABCMeta):
def __init__(self, chat_id): def __init__(self, chat_id):
self.chat_id = chat_id self.chat_id = chat_id
self.users = [] self.users = []
self.messages = [] self.messages = []
class PrivateChat(Chat): class PrivateChat(Chat):
def __init__(self, first_user, second_user): def __init__(self, first_user, second_user):
super(PrivateChat, self).__init__() super(PrivateChat, self).__init__()
self.users.append(first_user) self.users.append(first_user)
self.users.append(second_user) self.users.append(second_user)
class GroupChat(Chat): class GroupChat(Chat):
def add_user(self, user): # ... def add_user(self, user): # ...
def remove_user(self, user): # ... def remove_user(self, user): # ...
class Message(object): class Message(object):
def __init__(self, message_id, message, timestamp): def __init__(self, message_id, message, timestamp):
self.message_id = message_id self.message_id = message_id
self.message = message self.message = message
self.timestamp = timestamp self.timestamp = timestamp
class AddRequest(object): class AddRequest(object):
def __init__(self, from_user_id, to_user_id, request_status, timestamp): def __init__(self, from_user_id, to_user_id, request_status, timestamp):
self.from_user_id = from_user_id self.from_user_id = from_user_id
self.to_user_id = to_user_id self.to_user_id = to_user_id
self.request_status = request_status self.request_status = request_status
self.timestamp = timestamp self.timestamp = timestamp
class RequestStatus(Enum): class RequestStatus(Enum):
UNREAD = 0 UNREAD = 0
READ = 1 READ = 1
ACCEPTED = 2 ACCEPTED = 2
REJECTED = 3 REJECTED = 3
class Singleton(type):
_instances = {}
def __call__(cls, *args, **kwargs):
if cls not in cls._instances:
cls._instances[cls] = super(Singleton, cls).__call__(*args, **kwargs)
return cls._instances[cls]
``` ```
%%%% Output: stream %%%% Output: stream
Overwriting online_chat.py Overwriting online_chat.py
......
...@@ -3,8 +3,6 @@ from abc import ABCMeta ...@@ -3,8 +3,6 @@ from abc import ABCMeta
class UserService(object): class UserService(object):
__metaclass__ = Singleton
def __init__(self): def __init__(self):
self.users_by_id = {} # key: user id, value: User self.users_by_id = {} # key: user id, value: User
...@@ -38,8 +36,8 @@ class User(object): ...@@ -38,8 +36,8 @@ class User(object):
class Chat(metaclass=ABCMeta): class Chat(metaclass=ABCMeta):
def __init__(self, chat_id): def __init__(self, chat_id):
self.users = []
self.chat_id = chat_id self.chat_id = chat_id
self.users = []
self.messages = [] self.messages = []
...@@ -79,13 +77,4 @@ class RequestStatus(Enum): ...@@ -79,13 +77,4 @@ class RequestStatus(Enum):
UNREAD = 0 UNREAD = 0
READ = 1 READ = 1
ACCEPTED = 2 ACCEPTED = 2
REJECTED = 3 REJECTED = 3
\ No newline at end of file
class Singleton(type):
_instances = {}
def __call__(cls, *args, **kwargs):
if cls not in cls._instances:
cls._instances[cls] = super(Singleton, cls).__call__(*args, **kwargs)
return cls._instances[cls]
\ No newline at end of file
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment