Completed
Push — master ( 7a72c0...419771 )
by Andrew
28s
created

MessagesCreator.create_message()   B

Complexity

Conditions 5

Size

Total Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

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