Skip to content

Commit 616710f

Browse files
committed
Add Online Chat solution
1 parent ae9832c commit 616710f

File tree

3 files changed

+273
-0
lines changed

3 files changed

+273
-0
lines changed

solutions/object_oriented_design/online_chat/__init__.py

Whitespace-only changes.
Lines changed: 182 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,182 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"metadata": {},
6+
"source": [
7+
"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)."
8+
]
9+
},
10+
{
11+
"cell_type": "markdown",
12+
"metadata": {},
13+
"source": [
14+
"# Design an online chat"
15+
]
16+
},
17+
{
18+
"cell_type": "markdown",
19+
"metadata": {},
20+
"source": [
21+
"## Constraints and assumptions\n",
22+
"\n",
23+
"* Assume we'll focus on the following workflows:\n",
24+
" * Text conversations only\n",
25+
" * Users\n",
26+
" * Add a user\n",
27+
" * Remove a user\n",
28+
" * Update a user\n",
29+
" * Add to a user's friends list\n",
30+
" * Add friend request\n",
31+
" * Approve friend request\n",
32+
" * Reject friend request\n",
33+
" * Remove from a user's friends list\n",
34+
" * Create a group chat\n",
35+
" * Invite friends to a group chat\n",
36+
" * Post a message to a group chat\n",
37+
" * Private 1-1 chat\n",
38+
" * Invite a friend to a private chat\n",
39+
" * Post a meesage to a private chat\n",
40+
"* No need to worry about scaling initially"
41+
]
42+
},
43+
{
44+
"cell_type": "markdown",
45+
"metadata": {},
46+
"source": [
47+
"## Solution"
48+
]
49+
},
50+
{
51+
"cell_type": "code",
52+
"execution_count": 1,
53+
"metadata": {
54+
"collapsed": false
55+
},
56+
"outputs": [
57+
{
58+
"name": "stdout",
59+
"output_type": "stream",
60+
"text": [
61+
"Overwriting online_chat.py\n"
62+
]
63+
}
64+
],
65+
"source": [
66+
"%%writefile online_chat.py\n",
67+
"from abc import ABCMeta\n",
68+
"\n",
69+
"\n",
70+
"class UserService(object):\n",
71+
"\n",
72+
" __metaclass__ = Singleton\n",
73+
"\n",
74+
" def __init__(self):\n",
75+
" self.users_by_id = {} # key: user id, value: User\n",
76+
"\n",
77+
" def add_user(self, user_id, name, pass_hash): # ...\n",
78+
" def remove_user(self, user_id): # ...\n",
79+
" def add_friend_request(self, from_user_id, to_user_id): # ...\n",
80+
" def approve_friend_request(self, from_user_id, to_user_id): # ...\n",
81+
" def reject_friend_request(self, from_user_id, to_user_id): # ...\n",
82+
"\n",
83+
"\n",
84+
"class User(object):\n",
85+
"\n",
86+
" def __init__(self, user_id, name, pass_hash):\n",
87+
" self.user_id = user_id\n",
88+
" self.name = name\n",
89+
" self.pass_hash = pass_hash\n",
90+
" self.friends_by_id = {} # key: friend id, value: User\n",
91+
" self.friend_ids_to_private_chats = {} # key: friend id, value: private chats\n",
92+
" self.group_chats_by_id = {} # key: chat id, value: GroupChat\n",
93+
" self.received_friend_requests_by_friend_id = {} # key: friend id, value: AddRequest\n",
94+
" self.sent_friend_requests_by_friend_id = {} # key: friend id, value: AddRequest\n",
95+
"\n",
96+
" def message_user(self, friend_id, message): # ...\n",
97+
" def message_group(self, group_id, message): # ...\n",
98+
" def send_friend_request(self, friend_id): # ...\n",
99+
" def receive_friend_request(self, friend_id): # ...\n",
100+
" def approve_friend_request(self, friend_id): # ...\n",
101+
" def reject_friend_request(self, friend_id): # ...\n",
102+
"\n",
103+
"\n",
104+
"class Chat(metaclass=ABCMeta):\n",
105+
"\n",
106+
" def __init__(self, chat_id):\n",
107+
" self.chat_id = chat_id\n",
108+
" self.users = []\n",
109+
" self.messages = []\n",
110+
"\n",
111+
"\n",
112+
"class PrivateChat(Chat):\n",
113+
"\n",
114+
" def __init__(self, first_user, second_user):\n",
115+
" super(PrivateChat, self).__init__()\n",
116+
" self.users.append(first_user)\n",
117+
" self.users.append(second_user)\n",
118+
"\n",
119+
"\n",
120+
"class GroupChat(Chat):\n",
121+
"\n",
122+
" def add_user(self, user): # ...\n",
123+
" def remove_user(self, user): # ... \n",
124+
"\n",
125+
"\n",
126+
"class Message(object):\n",
127+
"\n",
128+
" def __init__(self, message_id, message, timestamp):\n",
129+
" self.message_id = message_id\n",
130+
" self.message = message\n",
131+
" self.timestamp = timestamp\n",
132+
"\n",
133+
"\n",
134+
"class AddRequest(object):\n",
135+
"\n",
136+
" def __init__(self, from_user_id, to_user_id, request_status, timestamp):\n",
137+
" self.from_user_id = from_user_id\n",
138+
" self.to_user_id = to_user_id\n",
139+
" self.request_status = request_status\n",
140+
" self.timestamp = timestamp\n",
141+
"\n",
142+
"\n",
143+
"class RequestStatus(Enum):\n",
144+
"\n",
145+
" UNREAD = 0\n",
146+
" READ = 1\n",
147+
" ACCEPTED = 2\n",
148+
" REJECTED = 3\n",
149+
"\n",
150+
"\n",
151+
"class Singleton(type):\n",
152+
"\n",
153+
" _instances = {}\n",
154+
" def __call__(cls, *args, **kwargs):\n",
155+
" if cls not in cls._instances:\n",
156+
" cls._instances[cls] = super(Singleton, cls).__call__(*args, **kwargs)\n",
157+
" return cls._instances[cls]"
158+
]
159+
}
160+
],
161+
"metadata": {
162+
"kernelspec": {
163+
"display_name": "Python 3",
164+
"language": "python",
165+
"name": "python3"
166+
},
167+
"language_info": {
168+
"codemirror_mode": {
169+
"name": "ipython",
170+
"version": 3
171+
},
172+
"file_extension": ".py",
173+
"mimetype": "text/x-python",
174+
"name": "python",
175+
"nbconvert_exporter": "python",
176+
"pygments_lexer": "ipython3",
177+
"version": "3.4.3"
178+
}
179+
},
180+
"nbformat": 4,
181+
"nbformat_minor": 0
182+
}
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
from abc import ABCMeta
2+
3+
4+
class UserService(object):
5+
6+
__metaclass__ = Singleton
7+
8+
def __init__(self):
9+
self.users_by_id = {} # key: user id, value: User
10+
11+
def add_user(self, user_id, name, pass_hash): # ...
12+
def remove_user(self, user_id): # ...
13+
def add_friend_request(self, from_user_id, to_user_id): # ...
14+
def approve_friend_request(self, from_user_id, to_user_id): # ...
15+
def reject_friend_request(self, from_user_id, to_user_id): # ...
16+
17+
18+
class User(object):
19+
20+
def __init__(self, user_id, name, pass_hash):
21+
self.user_id = user_id
22+
self.name = name
23+
self.pass_hash = pass_hash
24+
self.friends_by_id = {} # key: friend id, value: User
25+
self.friend_ids_to_private_chats = {} # key: friend id, value: private chats
26+
self.group_chats_by_id = {} # key: chat id, value: GroupChat
27+
self.received_friend_requests_by_friend_id = {} # key: friend id, value: AddRequest
28+
self.sent_friend_requests_by_friend_id = {} # key: friend id, value: AddRequest
29+
30+
def message_user(self, friend_id, message): # ...
31+
def message_group(self, group_id, message): # ...
32+
def send_friend_request(self, friend_id): # ...
33+
def receive_friend_request(self, friend_id): # ...
34+
def approve_friend_request(self, friend_id): # ...
35+
def reject_friend_request(self, friend_id): # ...
36+
37+
38+
class Chat(metaclass=ABCMeta):
39+
40+
def __init__(self, chat_id):
41+
self.users = []
42+
self.chat_id = chat_id
43+
self.messages = []
44+
45+
46+
class PrivateChat(Chat):
47+
48+
def __init__(self, first_user, second_user):
49+
super(PrivateChat, self).__init__()
50+
self.users.append(first_user)
51+
self.users.append(second_user)
52+
53+
54+
class GroupChat(Chat):
55+
56+
def add_user(self, user): # ...
57+
def remove_user(self, user): # ...
58+
59+
60+
class Message(object):
61+
62+
def __init__(self, message_id, message, timestamp):
63+
self.message_id = message_id
64+
self.message = message
65+
self.timestamp = timestamp
66+
67+
68+
class AddRequest(object):
69+
70+
def __init__(self, from_user_id, to_user_id, request_status, timestamp):
71+
self.from_user_id = from_user_id
72+
self.to_user_id = to_user_id
73+
self.request_status = request_status
74+
self.timestamp = timestamp
75+
76+
77+
class RequestStatus(Enum):
78+
79+
UNREAD = 0
80+
READ = 1
81+
ACCEPTED = 2
82+
REJECTED = 3
83+
84+
85+
class Singleton(type):
86+
87+
_instances = {}
88+
def __call__(cls, *args, **kwargs):
89+
if cls not in cls._instances:
90+
cls._instances[cls] = super(Singleton, cls).__call__(*args, **kwargs)
91+
return cls._instances[cls]

0 commit comments

Comments
 (0)