Completed
Push — master ( 0e978b...bede48 )
by Andrew
31s
created

MessagesCreator.base_default()   A

Complexity

Conditions 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

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