Completed
Push — master ( ec5943...6b5d6f )
by Andrew
30s
created

MessagesCreator.invite_room_channel_message()   A

Complexity

Conditions 1

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
dl 0
loc 9
rs 9.6666
c 0
b 0
f 0
1
from chat.models import get_milliseconds
2
from chat.tornado.constants import VarNames, HandlerNames, Actions, RedisPrefix
3
4
5
class MessagesCreator(object):
6
7
	def default(self, content, event, handler):
8
		"""
9
		:return: {"action": event, "content": content, "time": "20:48:57"}
10
		"""
11
		return {
12
			VarNames.EVENT: event,
13
			VarNames.CONTENT: content,
14
			VarNames.USER_ID: self.user_id,
15
			VarNames.TIME: get_milliseconds(),
16
			VarNames.HANDLER_NAME: handler
17
		}
18
19
	@staticmethod
20
	def base_default(event, content, handler):
21
		return {
22
			VarNames.EVENT: event,
23
			VarNames.CONTENT: content,
24
			VarNames.HANDLER_NAME: handler
25
		}
26
27
	@staticmethod
28
	def set_ws_id(random, self_id):
29
		return {
30
			VarNames.HANDLER_NAME: HandlerNames.WS,
31
			VarNames.EVENT: Actions.SET_WS_ID,
32
			VarNames.CONTENT: random,
33
			VarNames.WEBRTC_OPPONENT_ID: self_id
34
		}
35
36
	@classmethod
37
	def set_room(cls, rooms, users, online):
38
		res = cls.base_default(Actions.ROOMS, rooms, HandlerNames.CHANNELS)
39
		res[VarNames.ROOM_USERS] = users
40
		res[VarNames.ONLINE] = online
41
		return res
42
43
	def room_online(self, online, event):
44
		"""
45
		:return: {"action": event, "content": content, "time": "20:48:57"}
46
		"""
47
		room_less = self.default(online, event, HandlerNames.CHANNELS)
48
		room_less[VarNames.USER] = self.sender_name
49
		room_less[VarNames.GENDER] = self.sex
50
		return room_less
51
52
	@classmethod
53
	def create_message(cls, message, files):
54
		res = {
55
			VarNames.USER_ID: message.sender_id,
56
			VarNames.CONTENT: message.content,
57
			VarNames.TIME: message.time,
58
			VarNames.MESSAGE_ID: message.id,
59
			VarNames.EDITED_TIMES: message.edited_times,
60
			VarNames.ROOM_ID: message.room_id,
61
		}
62
		if message.deleted:
63
			res[VarNames.DELETED] = True
64
		if files:
65
			res[VarNames.FILES] = files
66
		if message.symbol:
67
			res[VarNames.SYMBOL] = message.symbol
68
		if message.giphy:
69
			res[VarNames.GIPHY] = message.giphy
70
		return res
71
72
	@classmethod
73
	def create_send_message(cls, message, event, files, js_id):
74
		"""
75
		:type message: chat.models.Message
76
		:type imgs: dict
77
		:type event: Actions
78
		:return: "action": "joined", "content": {"v5bQwtWp": "alien", "tRD6emzs": "Alien"},
79
		"sex": "Alien", "user": "tRD6emzs", "time": "20:48:57"}
80
		"""
81
		res = cls.create_message(message, files)
82
		res[VarNames.EVENT] = event
83
		res[VarNames.JS_MESSAGE_ID] = js_id
84
		res[VarNames.HANDLER_NAME] = HandlerNames.CHAT
85
		return res
86
87
	@classmethod
88
	def append_images(cls, messages, files, prepare_img):
89
		"""
90
		:type messages: list[chat.models.Message] 
91
		:type files: list[chat.models.Image]
92
		"""
93
		res_mess = []
94
		for message in messages:
95
			res_mess.append(cls.create_message(message, prepare_img(files, message.id)))
96
		return res_mess
97
98
	@classmethod
99
	def get_messages(cls, messages, channel, files, prepare_img):
100
		"""
101
		:type images: list[chat.models.Image]
102
		:type messages: list[chat.models.Message]
103
		:type channel: str
104
		"""
105
		return {
106
			VarNames.CONTENT: cls.append_images(messages, files, prepare_img),
107
			VarNames.EVENT: Actions.GET_MESSAGES,
108
			VarNames.ROOM_ID: channel,
109
			VarNames.HANDLER_NAME: HandlerNames.CHAT
110
		}
111
112
	@staticmethod
113
	def ping_client(time):
114
		return {
115
			VarNames.EVENT: Actions.PING,
116
			VarNames.TIME: time,
117
			VarNames.HANDLER_NAME: HandlerNames.WS,
118
		}
119
120
	@staticmethod
121
	def prepare_img_video(files, message_id):
122
		"""
123
		:type message_id: int
124
		:type files: list[chat.models.Image]
125
		"""
126
		if files:
127
			return {x.symbol: {
128
				VarNames.FILE_URL: x.img.url,
129
				VarNames.FILE_TYPE: x.type,
130
				VarNames.PREVIEW: x.preview.url if x.preview else None,
131
				VarNames.IMAGE_ID: x.id
132
			} for x in files if x.message_id == message_id}
133
134
	@property
135
	def channel(self):
136
		return RedisPrefix.generate_user(self.user_id)
137
138
	def subscribe_direct_channel_message(self, room_id, users_id, notifications):
139
		return {
140
			VarNames.EVENT: Actions.CREATE_DIRECT_CHANNEL,
141
			VarNames.VOLUME: 2,
142
			VarNames.NOTIFICATIONS: notifications,
143
			VarNames.ROOM_ID: room_id,
144
			VarNames.TIME: get_milliseconds(),
145
			VarNames.ROOM_USERS: users_id,
146
			VarNames.HANDLER_NAME: HandlerNames.CHANNELS
147
		}
148
149
	def responde_pong(self, js_id):
150
		return {
151
			VarNames.EVENT: Actions.PONG,
152
			VarNames.HANDLER_NAME: HandlerNames.WS,
153
			VarNames.JS_MESSAGE_ID: js_id
154
		}
155
156
	def subscribe_room_channel_message(self, room_id, room_name):
157
		return {
158
			VarNames.EVENT: Actions.CREATE_ROOM_CHANNEL,
159
			VarNames.ROOM_ID: room_id,
160
			VarNames.ROOM_USERS: [self.user_id],
161
			VarNames.HANDLER_NAME: HandlerNames.CHANNELS,
162
			VarNames.VOLUME: 2,
163
			VarNames.NOTIFICATIONS: True,
164
			VarNames.ROOM_NAME: room_name
165
		}
166
167
	@staticmethod
168
	def add_user_to_room(channel, channel_name, inviter, invitee, all_users):
169
		return {
170
			VarNames.EVENT: Actions.INVITE_USER,
171
			VarNames.ROOM_ID: channel,
172
			VarNames.ROOM_NAME: channel_name,
173
			VarNames.INVITEE_USER_ID: invitee,
174
			VarNames.INVITER_USER_ID: inviter,
175
			VarNames.HANDLER_NAME: HandlerNames.CHANNELS,
176
			VarNames.ROOM_USERS: all_users,
177
			VarNames.TIME: get_milliseconds(),
178
		}
179
180
	def unsubscribe_direct_message(self, room_id):
181
		return {
182
			VarNames.EVENT: Actions.DELETE_ROOM,
183
			VarNames.ROOM_ID: room_id,
184
			VarNames.USER_ID: self.user_id,
185
			VarNames.HANDLER_NAME: HandlerNames.CHANNELS,
186
			VarNames.TIME: get_milliseconds()
187
		}
188
189
	@staticmethod
190
	def create_user_rooms(user_rooms):
191
		res = {room['id']: {
192
			VarNames.ROOM_NAME: room['name'],
193
			VarNames.NOTIFICATIONS: room['roomusers__notifications'],
194
			VarNames.VOLUME: room['roomusers__volume'],
195
			VarNames.ROOM_USERS: []
196
		} for room in user_rooms}
197
		return res
198
199
200
class WebRtcMessageCreator(object):
201
202
	def offer_webrtc(self, content, connection_id, room_id, action):
203
		"""
204
		:return: {"action": "call", "content": content, "time": "20:48:57"}
205
		"""
206
		return {
207
			VarNames.EVENT: action,
208
			VarNames.CONTENT: content,
209
			VarNames.USER_ID: self.user_id,
210
			VarNames.HANDLER_NAME: HandlerNames.WEBRTC,
211
			VarNames.USER: self.sender_name,
212
			VarNames.CONNECTION_ID: connection_id,
213
			VarNames.WEBRTC_OPPONENT_ID: self.id,
214
			VarNames.ROOM_ID: room_id
215
		}
216
217
	def set_webrtc_error(self, error, connection_id, qued_id=None):
218
		return {
219
			VarNames.EVENT: Actions.SET_WEBRTC_ERROR,
220
			VarNames.CONTENT: error,
221
			VarNames.USER_ID: self.user_id,
222
			VarNames.HANDLER_NAME: HandlerNames.PEER_CONNECTION,
223
			VarNames.CONNECTION_ID: connection_id,
224
			VarNames.WEBRTC_QUED_ID: qued_id
225
		}
226
227
	@staticmethod
228
	def set_connection_id(qued_id, connection_id):
229
		return {
230
			VarNames.EVENT: Actions.SET_WEBRTC_ID,
231
			VarNames.HANDLER_NAME: HandlerNames.WEBRTC,
232
			VarNames.CONNECTION_ID: connection_id,
233
			VarNames.WEBRTC_QUED_ID: qued_id
234
		}
235
236
	def get_close_file_sender_message(self, connection_id):
237
		return {
238
			VarNames.EVENT: Actions.CLOSE_FILE_CONNECTION,
239
			VarNames.CONNECTION_ID: connection_id,
240
			VarNames.WEBRTC_OPPONENT_ID: self.id,
241
			VarNames.HANDLER_NAME: HandlerNames.WEBRTC_TRANSFER,
242
		}
243
244
	def get_accept_file_message(self, connection_id, content):
245
		return {
246
			VarNames.EVENT: Actions.ACCEPT_FILE,
247
			VarNames.CONNECTION_ID: connection_id,
248
			VarNames.WEBRTC_OPPONENT_ID: self.id,
249
			VarNames.HANDLER_NAME: HandlerNames.PEER_CONNECTION,
250
			VarNames.CONTENT: content,
251
		}
252
253
	def reply_webrtc(self, event, connection_id, handler, content):
254
		return {
255
			VarNames.EVENT: event,
256
			VarNames.CONNECTION_ID: connection_id,
257
			VarNames.USER_ID: self.user_id,
258
			VarNames.USER: self.sender_name,
259
			VarNames.CONTENT: content,
260
			VarNames.WEBRTC_OPPONENT_ID: self.id,
261
			VarNames.HANDLER_NAME: handler,
262
		}
263
264
	def retry_file(self, connection_id):
265
		return {
266
			VarNames.EVENT: Actions.RETRY_FILE_CONNECTION,
267
			VarNames.CONNECTION_ID: connection_id,
268
			VarNames.WEBRTC_OPPONENT_ID: self.id,
269
			VarNames.HANDLER_NAME: HandlerNames.PEER_CONNECTION,
270
		}
271