Completed
Push — master ( acef0c...5d47b2 )
by Andrew
32s
created

MessagesCreator.create_send_message()   A

Complexity

Conditions 1

Size

Total Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

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