Completed
Push — master ( 226f71...6d1fe2 )
by Andrew
34s
created

MessagesCreator.ping_client()   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 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
			} for x in files if x.message_id == message_id}
113
114
	@property
115
	def channel(self):
116
		return RedisPrefix.generate_user(self.user_id)
117
118
	def subscribe_direct_channel_message(self, room_id, other_user_id, notifications):
119
		return {
120
			VarNames.EVENT: Actions.CREATE_DIRECT_CHANNEL,
121
			VarNames.VOLUME: 2,
122
			VarNames.NOTIFICATIONS: notifications,
123
			VarNames.ROOM_ID: room_id,
124
			VarNames.ROOM_USERS: [self.user_id, other_user_id],
125
			VarNames.HANDLER_NAME: HandlerNames.CHANNELS
126
		}
127
128
	def responde_pong(self, js_id):
129
		return {
130
			VarNames.EVENT: Actions.PONG,
131
			VarNames.HANDLER_NAME: HandlerNames.WS,
132
			VarNames.JS_MESSAGE_ID: js_id
133
		}
134
135
	def subscribe_room_channel_message(self, room_id, room_name):
136
		return {
137
			VarNames.EVENT: Actions.CREATE_ROOM_CHANNEL,
138
			VarNames.ROOM_ID: room_id,
139
			VarNames.ROOM_USERS: [self.user_id],
140
			VarNames.HANDLER_NAME: HandlerNames.CHANNELS,
141
			VarNames.VOLUME: 2,
142
			VarNames.NOTIFICATIONS: True,
143
			VarNames.ROOM_NAME: room_name
144
		}
145
146
	@staticmethod
147
	def invite_room_channel_message(room_id, user_id, room_name, users):
148
		return {
149
			VarNames.EVENT: Actions.INVITE_USER,
150
			VarNames.ROOM_ID: room_id,
151
			VarNames.USER_ID: user_id,
152
			VarNames.HANDLER_NAME: HandlerNames.CHANNELS,
153
			VarNames.ROOM_NAME: room_name,
154
			VarNames.CONTENT: users
155
		}
156
157
	@staticmethod
158
	def add_user_to_room(channel, user_id, content):
159
		return {
160
			VarNames.EVENT: Actions.ADD_USER,
161
			VarNames.CHANNEL: channel,
162
			VarNames.USER_ID: user_id,
163
			VarNames.HANDLER_NAME: HandlerNames.CHAT,
164
			VarNames.GENDER: content[VarNames.GENDER],  # SEX: 'Alien', USER: 'Andrew'
165
			VarNames.USER: content[VarNames.USER]  # SEX: 'Alien', USER: 'Andrew'
166
		}
167
168
	def unsubscribe_direct_message(self, room_id):
169
		return {
170
			VarNames.EVENT: Actions.DELETE_ROOM,
171
			VarNames.ROOM_ID: room_id,
172
			VarNames.USER_ID: self.user_id,
173
			VarNames.HANDLER_NAME: HandlerNames.CHANNELS,
174
			VarNames.TIME: get_milliseconds()
175
		}
176
177
	def load_offline_message(self, offline_messages, history_messages, channel_key):
178
		res = self.default({
179
			VarNames.LOAD_MESSAGES_OFFLINE: offline_messages,
180
			VarNames.LOAD_MESSAGES_HISTORY: history_messages
181
		},
182
			Actions.OFFLINE_MESSAGES,
183
			HandlerNames.CHAT)
184
		res[VarNames.CHANNEL] = channel_key
185
		return res
186
187
188
class WebRtcMessageCreator(object):
189
190
	def offer_webrtc(self, content, connection_id, room_id, action):
191
		"""
192
		:return: {"action": "call", "content": content, "time": "20:48:57"}
193
		"""
194
		return {
195
			VarNames.EVENT: action,
196
			VarNames.CONTENT: content,
197
			VarNames.USER_ID: self.user_id,
198
			VarNames.HANDLER_NAME: HandlerNames.WEBRTC,
199
			VarNames.USER: self.sender_name,
200
			VarNames.CONNECTION_ID: connection_id,
201
			VarNames.WEBRTC_OPPONENT_ID: self.id,
202
			VarNames.CHANNEL: room_id
203
		}
204
205
	def set_webrtc_error(self, error, connection_id, qued_id=None):
206
		return {
207
			VarNames.EVENT: Actions.SET_WEBRTC_ERROR,
208
			VarNames.CONTENT: error,
209
			VarNames.USER_ID: self.user_id,
210
			VarNames.HANDLER_NAME: HandlerNames.PEER_CONNECTION,
211
			VarNames.CONNECTION_ID: connection_id,
212
			VarNames.WEBRTC_QUED_ID: qued_id
213
		}
214
215
	@staticmethod
216
	def set_connection_id(qued_id, connection_id):
217
		return {
218
			VarNames.EVENT: Actions.SET_WEBRTC_ID,
219
			VarNames.HANDLER_NAME: HandlerNames.WEBRTC,
220
			VarNames.CONNECTION_ID: connection_id,
221
			VarNames.WEBRTC_QUED_ID: qued_id
222
		}
223
224
	def get_close_file_sender_message(self, connection_id):
225
		return {
226
			VarNames.EVENT: Actions.CLOSE_FILE_CONNECTION,
227
			VarNames.CONNECTION_ID: connection_id,
228
			VarNames.WEBRTC_OPPONENT_ID: self.id,
229
			VarNames.HANDLER_NAME: HandlerNames.WEBRTC_TRANSFER,
230
		}
231
232
	def get_accept_file_message(self, connection_id, content):
233
		return {
234
			VarNames.EVENT: Actions.ACCEPT_FILE,
235
			VarNames.CONNECTION_ID: connection_id,
236
			VarNames.WEBRTC_OPPONENT_ID: self.id,
237
			VarNames.HANDLER_NAME: HandlerNames.PEER_CONNECTION,
238
			VarNames.CONTENT: content,
239
		}
240
241
	def reply_webrtc(self, event, connection_id, handler, content):
242
		return {
243
			VarNames.EVENT: event,
244
			VarNames.CONNECTION_ID: connection_id,
245
			VarNames.USER_ID: self.user_id,
246
			VarNames.USER: self.sender_name,
247
			VarNames.CONTENT: content,
248
			VarNames.WEBRTC_OPPONENT_ID: self.id,
249
			VarNames.HANDLER_NAME: handler,
250
		}
251
252
	def retry_file(self, connection_id):
253
		return {
254
			VarNames.EVENT: Actions.RETRY_FILE_CONNECTION,
255
			VarNames.CONNECTION_ID: connection_id,
256
			VarNames.WEBRTC_OPPONENT_ID: self.id,
257
			VarNames.HANDLER_NAME: HandlerNames.PEER_CONNECTION,
258
		}
259