| 1 |  |  | import json | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | import logging | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | import re | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  | import urllib | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | from django.core.exceptions import ValidationError | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | from django.db.models import Q | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | from tornado.gen import engine, Task | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | from tornado.httpclient import AsyncHTTPClient | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | from tornado.web import asynchronous | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | from tornadoredis import Client | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | from chat import settings | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | from chat.log_filters import id_generator | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | from chat.models import Message, Room, RoomUsers | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | from chat.py2_3 import str_type, quote | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | from chat.settings import ALL_ROOM_ID, TORNADO_REDIS_PORT, WEBRTC_CONNECTION, GIPHY_URL, GIPHY_REGEX | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | from chat.tornado.constants import VarNames, HandlerNames, Actions, RedisPrefix, WebRtcRedisStates | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | from chat.tornado.image_utils import process_images, prepare_img, save_images, get_message_images | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | from chat.tornado.message_creator import WebRtcMessageCreator, MessagesCreator | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | from chat.utils import get_max_key, do_db, validate_edit_message, get_or_create_room, \ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  | 	create_room | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  | parent_logger = logging.getLogger(__name__) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  | base_logger = logging.LoggerAdapter(parent_logger, { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  | 	'id': 0, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  | 	'ip': '000.000.000.000' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  | }) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  | # TODO https://github.com/leporo/tornado-redis#connection-pool-support | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  | # CONNECTION_POOL = tornadoredis.ConnectionPool( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  | # max_connections=500, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  | # wait_for_available=True) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  | GIPHY_API_KEY = getattr(settings, "GIPHY_API_KEY", None) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  | class MessagesHandler(MessagesCreator): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  | 	def __init__(self, *args, **kwargs): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  | 		self.closed_channels = None | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  | 		self.parsable_prefix = 'p' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  | 		super(MessagesHandler, self).__init__() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  | 		self.webrtc_ids = {} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  | 		self.id = None  # child init | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  | 		self.sex = None | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  | 		self.sender_name = None | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  | 		self.user_id = 0  # anonymous by default | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  | 		self.ip = None | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  | 		from chat import global_redis | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  | 		self.async_redis_publisher = global_redis.async_redis_publisher | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  | 		self.sync_redis = global_redis.sync_redis | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  | 		self.channels = [] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  | 		self._logger = None | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  | 		self.async_redis = Client(port=TORNADO_REDIS_PORT) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  | 		self.patch_tornadoredis() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  | 		self.pre_process_message = { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  | 			Actions.GET_MESSAGES: self.process_get_messages, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  | 			Actions.SEND_MESSAGE: self.process_send_message, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  | 			Actions.CREATE_DIRECT_CHANNEL: self.create_user_channel, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  | 			Actions.DELETE_ROOM: self.delete_channel, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  | 			Actions.EDIT_MESSAGE: self.edit_message, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  | 			Actions.CREATE_ROOM_CHANNEL: self.create_new_room, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  | 			Actions.INVITE_USER: self.invite_user, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  | 			Actions.PING: self.respond_ping | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  | 		self.post_process_message = { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  | 			Actions.CREATE_DIRECT_CHANNEL: self.send_client_new_channel, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  | 			Actions.CREATE_ROOM_CHANNEL: self.send_client_new_channel, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  | 			Actions.DELETE_ROOM: self.send_client_delete_channel, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  | 			Actions.INVITE_USER: self.send_client_new_channel | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  | 	def patch_tornadoredis(self):  # TODO remove this | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  | 		fabric = type(self.async_redis.connection.readline) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  | 		self.async_redis.connection.old_read = self.async_redis.connection.readline | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  | 		def new_read(new_self, callback=None): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  | 			try: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  | 				return new_self.old_read(callback=callback) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  | 			except Exception as e: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  | 				current_online = self.get_online_from_redis(RedisPrefix.DEFAULT_CHANNEL) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  | 				self.logger.error(e) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  | 				self.logger.error( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  | 					"Exception info: " | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  | 					"self.id: %s ;;; " | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  | 					"self.connected = '%s';;; " | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  | 					"Redis default channel online = '%s';;; " | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  | 					"self.channels = '%s';;; " | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  | 					"self.closed_channels  = '%s';;;", | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  | 					self.id, self.connected, current_online, self.channels, self.closed_channels | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  | 				) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  | 				raise e | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  | 		self.async_redis.connection.readline = fabric(new_read, self.async_redis.connection) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  | 	@property | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  | 	def connected(self): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  | 		raise NotImplemented | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  | 	@connected.setter | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  | 	def connected(self, value): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  | 		raise NotImplemented | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  | 	@engine | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  | 	def listen(self, channels): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  | 		yield Task( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  | 			self.async_redis.subscribe, channels) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  | 		self.async_redis.listen(self.pub_sub_message) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  | 	@property | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  | 	def logger(self): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  | 		return self._logger if self._logger else base_logger | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 112 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 113 |  |  | 	@engine | 
            
                                                                        
                            
            
                                    
            
            
                | 114 |  |  | 	def add_channel(self, channel): | 
            
                                                                        
                            
            
                                    
            
            
                | 115 |  |  | 		self.channels.append(channel) | 
            
                                                                        
                            
            
                                    
            
            
                | 116 |  |  | 		yield Task(self.async_redis.subscribe, (channel,)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  | 	def get_online_from_redis(self, channel): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  | 		return self.get_online_and_status_from_redis(channel)[1] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  | 	def get_online_and_status_from_redis(self, channel): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  | 		""" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  | 		:rtype : (bool, list) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  | 		""" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  | 		online = self.sync_redis.ssmembers(channel) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  | 		self.logger.debug('!! channel %s redis online: %s', channel, online) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  | 		return self.parse_redis_online(online) if online else (False, []) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  | 	def parse_redis_online(self, online): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  | 		""" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  | 		:rtype : (bool, list) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  | 		""" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  | 		result = set() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  | 		user_is_online = False | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  | 		for decoded in online:  # py2 iteritems | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  | 			# : char specified in cookies_middleware.py.create_id | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  | 			user_id = int(decoded.split(':')[0]) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  | 			if user_id == self.user_id and decoded != self.id: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  | 				user_is_online = True | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  | 			result.add(user_id) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  | 		return user_is_online, list(result) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  | 	def add_online_user(self, room_id, offline_messages=None): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  | 		""" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  | 		adds to redis | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  | 		online_users = { connection_hash1 = stored_redis_user1, connection_hash_2 = stored_redis_user2 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  | 		:return: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  | 		""" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 |  |  | 		self.async_redis_publisher.sadd(room_id, self.id) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 |  |  | 		# since we add user to online first, latest trigger will always show correct online | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 |  |  | 		is_online, online = self.get_online_and_status_from_redis(room_id) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 |  |  | 		if is_online:  # Send user names to self | 
            
                                                                                                            
                            
            
                                    
            
            
                | 153 |  |  | 			online_user_names_mes = self.room_online(online, Actions.REFRESH_USER, room_id) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 154 |  |  | 			self.logger.info('!! Second tab, retrieving online for self') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 155 |  |  | 			self.ws_write(online_user_names_mes) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 156 |  |  | 		else:  # if a new tab has been opened | 
            
                                                                                                            
                            
            
                                    
            
            
                | 157 |  |  | 			online.append(self.user_id) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 158 |  |  | 			online_user_names_mes = self.room_online(online, Actions.LOGIN, room_id) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 159 |  |  | 			self.logger.info('!! First tab, sending refresh online for all') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 160 |  |  | 			self.publish(online_user_names_mes, room_id) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 161 |  |  | 			if offline_messages: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 162 |  |  | 				self.ws_write(self.load_offline_message(offline_messages, room_id)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 163 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 164 |  |  | 	def publish(self, message, channel, parsable=False): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 165 |  |  | 		jsoned_mess = json.dumps(message) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 166 |  |  | 		self.logger.debug('<%s> %s', channel, jsoned_mess) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 167 |  |  | 		if parsable: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 168 |  |  | 			jsoned_mess = self.encode(jsoned_mess) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 169 |  |  | 		self.async_redis_publisher.publish(channel, jsoned_mess) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 170 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 171 |  |  | 	def encode(self, message): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 172 |  |  | 		""" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 173 |  |  | 		Marks message with prefix to specify that | 
            
                                                                                                            
                            
            
                                    
            
            
                | 174 |  |  | 		it should be decoded and proccesed before sending to client | 
            
                                                                                                            
                            
            
                                    
            
            
                | 175 |  |  | 		@param message: message to mark | 
            
                                                                                                            
                            
            
                                    
            
            
                | 176 |  |  | 		@return: marked message | 
            
                                                                                                            
                            
            
                                    
            
            
                | 177 |  |  | 		""" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 178 |  |  | 		return self.parsable_prefix + message | 
            
                                                                                                            
                            
            
                                    
            
            
                | 179 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 180 |  |  | 	def remove_parsable_prefix(self, message): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 181 |  |  | 		if message.startswith(self.parsable_prefix): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 182 |  |  | 			return message[1:] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 183 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 184 |  |  | 	def pub_sub_message(self, message): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 185 |  |  | 		data = message.body | 
            
                                                                                                            
                            
            
                                    
            
            
                | 186 |  |  | 		if isinstance(data, str_type):  # subscribe event | 
            
                                                                                                            
                            
            
                                    
            
            
                | 187 |  |  | 			prefixless_str = self.remove_parsable_prefix(data) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 188 |  |  | 			if prefixless_str: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 189 |  |  | 				dict_message = json.loads(prefixless_str) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 190 |  |  | 				res = self.post_process_message[dict_message[VarNames.EVENT]](dict_message) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 191 |  |  | 				if not res: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 192 |  |  | 					self.ws_write(prefixless_str) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 193 |  |  | 			else: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 194 |  |  | 				self.ws_write(data) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 195 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 196 |  |  | 	def ws_write(self, message): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 197 |  |  | 		raise NotImplementedError('WebSocketHandler implements') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 198 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 199 |  |  | 	@asynchronous | 
            
                                                                                                            
                            
            
                                    
            
            
                | 200 |  |  | 	def search_giphy(self, message, query, cb): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 201 |  |  | 		self.logger.debug("!! Asking giphy for: %s", query) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 202 |  |  | 		def on_giphy_reply(response): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 203 |  |  | 			try: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 204 |  |  | 				self.logger.debug("!! Got giphy response: " + str(response.body)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 205 |  |  | 				res =  json.loads(response.body) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 206 |  |  | 				giphy = res['data'][0]['embed_url'] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 207 |  |  | 			except: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 208 |  |  | 				giphy = None | 
            
                                                                                                            
                            
            
                                    
            
            
                | 209 |  |  | 			cb(message, giphy) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 210 |  |  | 		http_client = AsyncHTTPClient() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 211 |  |  | 		url = GIPHY_URL.format(GIPHY_API_KEY, quote(query, safe='')) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 212 |  |  | 		http_client.fetch(url, callback=on_giphy_reply) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 213 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 214 |  |  | 	def isGiphy(self, content): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 215 |  |  | 		if GIPHY_API_KEY is not None: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 216 |  |  | 			giphy_match = re.search(GIPHY_REGEX, content) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 217 |  |  | 			return giphy_match.group(1) if giphy_match is not None else None | 
            
                                                                                                            
                            
            
                                    
            
            
                | 218 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 219 |  |  | 	def process_send_message(self, message): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 220 |  |  | 		""" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 221 |  |  | 		:type message: dict | 
            
                                                                                                            
                            
            
                                    
            
            
                | 222 |  |  | 		""" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 223 |  |  | 		content = message.get(VarNames.CONTENT) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 224 |  |  | 		giphy_match = self.isGiphy(content) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 225 |  |  | 		def send_message(message, giphy=None): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 226 |  |  | 			raw_imgs = message.get(VarNames.IMG) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 227 |  |  | 			channel = message[VarNames.CHANNEL] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 228 |  |  | 			message_db = Message( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 229 |  |  | 				sender_id=self.user_id, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 230 |  |  | 				content=message[VarNames.CONTENT], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 231 |  |  | 				symbol=get_max_key(raw_imgs), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 232 |  |  | 				giphy=giphy | 
            
                                                                                                            
                            
            
                                    
            
            
                | 233 |  |  | 			) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 234 |  |  | 			message_db.room_id = channel | 
            
                                                                                                            
                            
            
                                    
            
            
                | 235 |  |  | 			do_db(message_db.save) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 236 |  |  | 			db_images = save_images(raw_imgs, message_db.id) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 237 |  |  | 			prepared_message = self.create_send_message( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 238 |  |  | 				message_db, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 239 |  |  | 				Actions.PRINT_MESSAGE, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 240 |  |  | 				prepare_img(db_images, message_db.id) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 241 |  |  | 			) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 242 |  |  | 			self.publish(prepared_message, channel) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 243 |  |  | 		if giphy_match is not None: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 244 |  |  | 			self.search_giphy(message, giphy_match, send_message) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 245 |  |  | 		else: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 246 |  |  | 			send_message(message) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 247 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 248 |  |  | 	def create_new_room(self, message): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 249 |  |  | 		room_name = message[VarNames.ROOM_NAME] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 250 |  |  | 		if not room_name or len(room_name) > 16: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 251 |  |  | 			raise ValidationError('Incorrect room name "{}"'.format(room_name)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 252 |  |  | 		room = Room(name=room_name) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 253 |  |  | 		do_db(room.save) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 254 |  |  | 		RoomUsers(room_id=room.id, user_id=self.user_id).save() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 255 |  |  | 		subscribe_message = self.subscribe_room_channel_message(room.id, room_name) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 256 |  |  | 		self.publish(subscribe_message, self.channel, True) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 257 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 258 |  |  | 	def invite_user(self, message): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 259 |  |  | 		room_id = message[VarNames.ROOM_ID] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 260 |  |  | 		user_id = message[VarNames.USER_ID] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 261 |  |  | 		room = get_or_create_room(self.channels, room_id, user_id) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 262 |  |  | 		users_in_room = { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 263 |  |  | 			user.id: RedisPrefix.set_js_user_structure(user.username, user.sex) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 264 |  |  | 			for user in room.users.all() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 265 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 266 |  |  | 		self.publish(self.add_user_to_room(room_id, user_id, users_in_room[user_id]), room_id) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 267 |  |  | 		subscribe_message = self.invite_room_channel_message(room_id, user_id, room.name, users_in_room) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 268 |  |  | 		self.publish(subscribe_message, RedisPrefix.generate_user(user_id), True) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 269 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 270 |  |  | 	def respond_ping(self, message): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 271 |  |  | 		self.ws_write(self.responde_pong()) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 272 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 273 |  |  | 	def create_user_channel(self, message): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 274 |  |  | 		user_id = message[VarNames.USER_ID] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 275 |  |  | 		room_id = create_room(self.user_id, user_id) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 276 |  |  | 		subscribe_message = self.subscribe_direct_channel_message(room_id, user_id) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 277 |  |  | 		self.publish(subscribe_message, self.channel, True) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 278 |  |  | 		other_channel = RedisPrefix.generate_user(user_id) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 279 |  |  | 		if self.channel != other_channel: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 280 |  |  | 			self.publish(subscribe_message, other_channel, True) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 281 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 282 |  |  | 	def delete_channel(self, message): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 283 |  |  | 		room_id = message[VarNames.ROOM_ID] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 284 |  |  | 		if room_id not in self.channels or room_id == ALL_ROOM_ID: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 285 |  |  | 			raise ValidationError('You are not allowed to exit this room') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 286 |  |  | 		room = do_db(Room.objects.get, id=room_id) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 287 |  |  | 		if room.disabled: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 288 |  |  | 			raise ValidationError('Room is already deleted') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 289 |  |  | 		if room.name is None:  # if private then disable | 
            
                                                                                                            
                            
            
                                    
            
            
                | 290 |  |  | 			room.disabled = True | 
            
                                                                                                            
                            
            
                                    
            
            
                | 291 |  |  | 		else:  # if public -> leave the room, delete the link | 
            
                                                                                                            
                            
            
                                    
            
            
                | 292 |  |  | 			RoomUsers.objects.filter(room_id=room.id, user_id=self.user_id).delete() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 293 |  |  | 			online = self.get_online_from_redis(room_id) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 294 |  |  | 			online.remove(self.user_id) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 295 |  |  | 			self.publish(self.room_online(online, Actions.LOGOUT, room_id), room_id) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 296 |  |  | 		room.save() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 297 |  |  | 		message = self.unsubscribe_direct_message(room_id) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 298 |  |  | 		self.publish(message, room_id, True) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 299 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 300 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 301 |  |  | 	def edit_message(self, data): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 302 |  |  | 		message_id = data[VarNames.MESSAGE_ID] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 303 |  |  | 		message = do_db(Message.objects.get, id=message_id) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 304 |  |  | 		validate_edit_message(self.user_id, message) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 305 |  |  | 		message.content = data[VarNames.CONTENT] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 306 |  |  | 		selector = Message.objects.filter(id=message_id) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 307 |  |  | 		giphy_match = self.isGiphy(data[VarNames.CONTENT]) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 308 |  |  | 		if message.content is None: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 309 |  |  | 			action = Actions.DELETE_MESSAGE | 
            
                                                                                                            
                            
            
                                    
            
            
                | 310 |  |  | 			prep_imgs = None | 
            
                                                                                                            
                            
            
                                    
            
            
                | 311 |  |  | 			selector.update(deleted=True) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 312 |  |  | 		elif giphy_match is not None: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 313 |  |  | 			def edit_glyphy(message, giphy): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 314 |  |  | 				do_db(selector.update, content=message.content, symbol=message.symbol, giphy=giphy) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 315 |  |  | 				message.giphy = giphy | 
            
                                                                                                            
                            
            
                                    
            
            
                | 316 |  |  | 				self.publish(self.create_send_message(message, Actions.EDIT_MESSAGE, None), message.room_id) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 317 |  |  | 			self.search_giphy(message, giphy_match, edit_glyphy) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 318 |  |  | 			return | 
            
                                                                                                            
                            
            
                                    
            
            
                | 319 |  |  | 		else: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 320 |  |  | 			action = Actions.EDIT_MESSAGE | 
            
                                                                                                            
                            
            
                                    
            
            
                | 321 |  |  | 			message.giphy = None | 
            
                                                                                                            
                            
            
                                    
            
            
                | 322 |  |  | 			prep_imgs = process_images(data.get(VarNames.IMG), message) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 323 |  |  | 			selector.update(content=message.content, symbol=message.symbol, giphy=None) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 324 |  |  | 		self.publish(self.create_send_message(message, action, prep_imgs), message.room_id) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 325 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 326 |  |  | 	def send_client_new_channel(self, message): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 327 |  |  | 		room_id = message[VarNames.ROOM_ID] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 328 |  |  | 		self.add_channel(room_id) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 329 |  |  | 		self.add_online_user(room_id) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 330 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 331 |  |  | 	def send_client_delete_channel(self, message): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 332 |  |  | 		room_id = message[VarNames.ROOM_ID] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 333 |  |  | 		self.async_redis.unsubscribe((room_id,)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 334 |  |  | 		self.async_redis_publisher.hdel(room_id, self.id) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 335 |  |  | 		self.channels.remove(room_id) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 336 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 337 |  |  | 	def process_get_messages(self, data): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 338 |  |  | 		""" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 339 |  |  | 		:type data: dict | 
            
                                                                                                            
                            
            
                                    
            
            
                | 340 |  |  | 		""" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 341 |  |  | 		header_id = data.get(VarNames.GET_MESSAGES_HEADER_ID, None) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 342 |  |  | 		count = int(data.get(VarNames.GET_MESSAGES_COUNT, 10)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 343 |  |  | 		room_id = data[VarNames.CHANNEL] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 344 |  |  | 		self.logger.info('!! Fetching %d messages starting from %s', count, header_id) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 345 |  |  | 		if header_id is None: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 346 |  |  | 			messages = Message.objects.filter(Q(room_id=room_id), Q(deleted=False)).order_by('-pk')[:count] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 347 |  |  | 		else: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 348 |  |  | 			messages = Message.objects.filter(Q(id__lt=header_id), Q(room_id=room_id), Q(deleted=False)).order_by('-pk')[:count] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 349 |  |  | 		images = do_db(get_message_images, messages) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 350 |  |  | 		response = self.get_messages(messages, room_id, images) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 351 |  |  | 		self.ws_write(response) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 352 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 353 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 354 |  |  | class WebRtcMessageHandler(MessagesHandler, WebRtcMessageCreator): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 355 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 356 |  |  | 	def __init__(self, *args, **kwargs): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 357 |  |  | 		super(WebRtcMessageHandler, self).__init__(*args, **kwargs) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 358 |  |  | 		self.pre_process_message.update({ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 359 |  |  | 			Actions.WEBRTC: self.proxy_webrtc, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 360 |  |  | 			Actions.CLOSE_FILE_CONNECTION: self.close_file_connection, | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 361 |  | View Code Duplication | 			Actions.CLOSE_CALL_CONNECTION: self.close_call_connection, | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 362 |  |  | 			Actions.CANCEL_CALL_CONNECTION: self.cancel_call_connection, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 363 |  |  | 			Actions.ACCEPT_CALL: self.accept_call, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 364 |  |  | 			Actions.ACCEPT_FILE: self.accept_file, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 365 |  |  | 			Actions.OFFER_FILE_CONNECTION: self.offer_webrtc_connection, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 366 |  |  | 			Actions.OFFER_CALL_CONNECTION: self.offer_webrtc_connection, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 367 |  |  | 			Actions.REPLY_FILE_CONNECTION: self.reply_file_connection, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 368 |  |  | 			Actions.RETRY_FILE_CONNECTION: self.retry_file_connection, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 369 |  |  | 			Actions.REPLY_CALL_CONNECTION: self.reply_call_connection, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 370 |  |  | 		}) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 371 |  |  | 		self.post_process_message.update({ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 372 |  |  | 			Actions.OFFER_FILE_CONNECTION: self.set_opponent_call_channel, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 373 |  |  | 			Actions.OFFER_CALL_CONNECTION: self.set_opponent_call_channel | 
            
                                                                                                            
                            
            
                                    
            
            
                | 374 |  |  | 		}) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 375 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 376 |  |  | 	def set_opponent_call_channel(self, message): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 377 |  |  | 		connection_id = message[VarNames.CONNECTION_ID] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 378 |  |  | 		if message[VarNames.WEBRTC_OPPONENT_ID] == self.id: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 379 |  |  | 			return True | 
            
                                                                                                            
                            
            
                                    
            
            
                | 380 |  |  | 		self.sync_redis.hset(connection_id, self.id, WebRtcRedisStates.OFFERED) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 381 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 382 |  |  | 	def offer_webrtc_connection(self, in_message): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 383 |  |  | 		room_id = in_message[VarNames.CHANNEL] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 384 |  |  | 		content = in_message.get(VarNames.CONTENT) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 385 |  |  | 		qued_id = in_message[VarNames.WEBRTC_QUED_ID] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 386 |  |  | 		connection_id = id_generator(RedisPrefix.CONNECTION_ID_LENGTH) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 387 |  |  | 		# use list because sets dont have 1st element which is offerer | 
            
                                                                                                            
                            
            
                                    
            
            
                | 388 |  |  | 		self.async_redis_publisher.hset(WEBRTC_CONNECTION, connection_id, self.id) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 389 |  |  | 		self.async_redis_publisher.hset(connection_id, self.id, WebRtcRedisStates.READY) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 390 |  |  | 		opponents_message = self.offer_webrtc(content, connection_id, room_id, in_message[VarNames.EVENT]) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 391 |  |  | 		self_message = self.set_connection_id(qued_id, connection_id) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 392 |  |  | 		self.ws_write(self_message) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 393 |  |  | 		self.logger.info('!! Offering a webrtc, connection_id %s', connection_id) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 394 |  |  | 		self.publish(opponents_message, room_id, True) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 395 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 396 |  |  | 	def retry_file_connection(self, in_message): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 397 |  |  | 		connection_id = in_message[VarNames.CONNECTION_ID] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 398 |  |  | 		opponent_ws_id = in_message[VarNames.WEBRTC_OPPONENT_ID] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 399 |  |  | 		sender_ws_id = self.sync_redis.shget(WEBRTC_CONNECTION, connection_id) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 400 |  |  | 		receiver_ws_status = self.sync_redis.shget(connection_id, opponent_ws_id) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 401 |  |  | 		if receiver_ws_status == WebRtcRedisStates.READY and self.id == sender_ws_id: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 402 |  |  | 			self.publish(self.retry_file(connection_id), opponent_ws_id) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 403 |  |  | 		else: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 404 |  |  | 			raise ValidationError("Invalid channel status.") | 
            
                                                                                                            
                            
            
                                    
            
            
                | 405 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 406 |  |  | 	def reply_file_connection(self, in_message): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 407 |  |  | 		connection_id = in_message[VarNames.CONNECTION_ID] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 408 |  |  | 		sender_ws_id = self.sync_redis.shget(WEBRTC_CONNECTION, connection_id) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 409 |  |  | 		sender_ws_status = self.sync_redis.shget(connection_id, sender_ws_id) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 410 |  |  | 		self_ws_status = self.sync_redis.shget(connection_id, self.id) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 411 |  |  | 		if sender_ws_status == WebRtcRedisStates.READY and self_ws_status == WebRtcRedisStates.OFFERED: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 412 |  |  | 			self.async_redis_publisher.hset(connection_id, self.id, WebRtcRedisStates.RESPONDED) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 413 |  |  | 			self.publish(self.reply_webrtc( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 414 |  |  | 				Actions.REPLY_FILE_CONNECTION, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 415 |  |  | 				connection_id, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 416 |  |  | 				HandlerNames.WEBRTC_TRANSFER, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 417 |  |  | 				in_message[VarNames.CONTENT] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 418 |  |  | 			), sender_ws_id) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 419 |  |  | 		else: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 420 |  |  | 			raise ValidationError("Invalid channel status.") | 
            
                                                                                                            
                            
            
                                    
            
            
                | 421 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 422 |  |  | 	def reply_call_connection(self, in_message): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 423 |  |  | 		self.send_call_answer( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 424 |  |  | 			in_message, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 425 |  |  | 			WebRtcRedisStates.RESPONDED, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 426 |  |  | 			Actions.REPLY_CALL_CONNECTION, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 427 |  |  | 			[WebRtcRedisStates.OFFERED], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 428 |  |  | 			HandlerNames.WEBRTC_TRANSFER | 
            
                                                                                                            
                            
            
                                    
            
            
                | 429 |  |  | 		) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 430 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 431 |  |  | 	def proxy_webrtc(self, in_message): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 432 |  |  | 		""" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 433 |  |  | 		:type in_message: dict | 
            
                                                                                                            
                            
            
                                    
            
            
                | 434 |  |  | 		""" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 435 |  |  | 		connection_id = in_message[VarNames.CONNECTION_ID] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 436 |  |  | 		channel = in_message.get(VarNames.WEBRTC_OPPONENT_ID) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 437 |  |  | 		self_channel_status = self.sync_redis.shget(connection_id, self.id) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 438 |  |  | 		opponent_channel_status = self.sync_redis.shget(connection_id, channel) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 439 |  |  | 		if not (self_channel_status == WebRtcRedisStates.READY and opponent_channel_status == WebRtcRedisStates.READY): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 440 |  |  | 			raise ValidationError('Error in connection status, your status is {} while opponent is {}'.format( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 441 |  |  | 				self_channel_status, opponent_channel_status | 
            
                                                                                                            
                            
            
                                    
            
            
                | 442 |  |  | 			))  # todo receiver should only accept proxy_webrtc from sender, sender can accept all | 
            
                                                                                                            
                            
            
                                    
            
            
                | 443 |  |  | 		# I mean somebody if there're 3 ppl in 1 channel and first is initing transfer to 2nd and 3rd, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 444 |  |  | 		# 2nd guy can fraud 3rd guy webrtc traffic, which is allowed during the call, but not while transering file | 
            
                                                                                                            
                            
            
                                    
            
            
                | 445 |  |  | 		in_message[VarNames.WEBRTC_OPPONENT_ID] = self.id | 
            
                                                                                                            
                            
            
                                    
            
            
                | 446 |  |  | 		in_message[VarNames.HANDLER_NAME] = HandlerNames.PEER_CONNECTION | 
            
                                                                                                            
                            
            
                                    
            
            
                | 447 |  |  | 		self.logger.debug( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 448 |  |  | 			"!! Forwarding message to channel %s, self %s, other status %s", | 
            
                                                                                                            
                            
            
                                    
            
            
                | 449 |  |  | 			channel, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 450 |  |  | 			self_channel_status, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 451 |  |  | 			opponent_channel_status | 
            
                                                                                                            
                            
            
                                    
            
            
                | 452 |  |  | 		) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 453 |  |  | 		self.publish(in_message, channel) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 454 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 455 |  |  | 	def close_file_connection(self, in_message): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 456 |  |  | 		connection_id = in_message[VarNames.CONNECTION_ID] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 457 |  |  | 		self_channel_status = self.sync_redis.shget(connection_id, self.id) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 458 |  |  | 		if not self_channel_status: | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 459 |  | View Code Duplication | 			raise Exception("Access Denied") | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 460 |  |  | 		if self_channel_status != WebRtcRedisStates.CLOSED: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 461 |  |  | 			sender_id = self.sync_redis.shget(WEBRTC_CONNECTION, connection_id) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 462 |  |  | 			if sender_id == self.id: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 463 |  |  | 				self.close_file_sender(connection_id) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 464 |  |  | 			else: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 465 |  |  | 				self.close_file_receiver(connection_id, in_message, sender_id) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 466 |  |  | 			self.async_redis_publisher.hset(connection_id, self.id, WebRtcRedisStates.CLOSED) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 467 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 468 |  |  | 	def close_call_connection(self, in_message): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 469 |  |  | 		self.send_call_answer( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 470 |  |  | 			in_message, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 471 |  |  | 			WebRtcRedisStates.CLOSED, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 472 |  |  | 			Actions.CLOSE_CALL_CONNECTION, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 473 |  |  | 			[WebRtcRedisStates.READY, WebRtcRedisStates.RESPONDED], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 474 |  |  | 			HandlerNames.PEER_CONNECTION | 
            
                                                                                                            
                            
            
                                    
            
            
                | 475 |  |  | 		) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 476 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 477 |  |  | 	def cancel_call_connection(self, in_message): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 478 |  |  | 		self.send_call_answer( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 479 |  |  | 			in_message, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 480 |  |  | 			WebRtcRedisStates.CLOSED, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 481 |  |  | 			Actions.CANCEL_CALL_CONNECTION, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 482 |  |  | 			[WebRtcRedisStates.OFFERED], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 483 |  |  | 			HandlerNames.WEBRTC_TRANSFER | 
            
                                                                                                            
                            
            
                                    
            
            
                | 484 |  |  | 		) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 485 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 486 |  |  | 	def close_file_receiver(self, connection_id, in_message, sender_id): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 487 |  |  | 		sender_status = self.sync_redis.shget(connection_id, sender_id) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 488 |  |  | 		if not sender_status: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 489 |  |  | 			raise Exception("Access denied") | 
            
                                                                                                            
                            
            
                                    
            
            
                | 490 |  |  | 		if sender_status != WebRtcRedisStates.CLOSED: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 491 |  |  | 			in_message[VarNames.WEBRTC_OPPONENT_ID] = self.id | 
            
                                                                                                            
                            
            
                                    
            
            
                | 492 |  |  | 			in_message[VarNames.HANDLER_NAME] = HandlerNames.PEER_CONNECTION | 
            
                                                                                                            
                            
            
                                    
            
            
                | 493 |  |  | 			self.publish(in_message, sender_id) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 494 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 495 |  |  | 	def close_file_sender(self, connection_id): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 496 |  |  | 		values = self.sync_redis.shgetall(connection_id) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 497 |  |  | 		del values[self.id] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 498 |  |  | 		message = self.get_close_file_sender_message(connection_id) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 499 |  |  | 		for ws_id in values: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 500 |  |  | 			if values[ws_id] == WebRtcRedisStates.CLOSED: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 501 |  |  | 				continue | 
            
                                                                                                            
                            
            
                                    
            
            
                | 502 |  |  | 			self.publish(message, ws_id) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 503 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 504 |  |  | 	def accept_file(self, in_message): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 505 |  |  | 		connection_id = in_message[VarNames.CONNECTION_ID] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 506 |  |  | 		content = in_message[VarNames.CONTENT] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 507 |  |  | 		sender_ws_id = self.sync_redis.shget(WEBRTC_CONNECTION, connection_id) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 508 |  |  | 		sender_ws_status = self.sync_redis.shget(connection_id, sender_ws_id) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 509 |  |  | 		self_ws_status = self.sync_redis.shget(connection_id, self.id) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 510 |  |  | 		if sender_ws_status == WebRtcRedisStates.READY \ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 511 |  |  | 				and self_ws_status in [WebRtcRedisStates.RESPONDED, WebRtcRedisStates.READY]: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 512 |  |  | 			self.async_redis_publisher.hset(connection_id, self.id, WebRtcRedisStates.READY) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 513 |  |  | 			self.publish(self.get_accept_file_message(connection_id, content), sender_ws_id) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 514 |  |  | 		else: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 515 |  |  | 			raise ValidationError("Invalid channel status") | 
            
                                                                                                            
                            
            
                                    
            
            
                | 516 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 517 |  |  | 	# todo | 
            
                                                                                                            
                            
            
                                    
            
            
                | 518 |  |  | 	# we can use channel_status = self.sync_redis.shgetall(connection_id) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 519 |  |  | 	# and then self.async_redis_publisher.hset(connection_id, self.id, WebRtcRedisStates.READY) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 520 |  |  | 	# if we shgetall and only then do async hset | 
            
                                                                                                            
                            
            
                                    
            
            
                | 521 |  |  | 	# we can catch an issue when 2 concurrent users accepted the call | 
            
                                                                                                            
                            
            
                                    
            
            
                | 522 |  |  | 	# but we didn't  send them ACCEPT_CALL as they both were in status 'offered' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 523 |  |  | 	def accept_call(self, in_message): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 524 |  |  | 		connection_id = in_message[VarNames.CONNECTION_ID] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 525 |  |  | 		self_status = self.sync_redis.shget(connection_id, self.id) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 526 |  |  | 		if self_status == WebRtcRedisStates.RESPONDED: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 527 |  |  | 			conn_users = self.sync_redis.shgetall(connection_id) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 528 |  |  | 			self.publish_call_answer( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 529 |  |  | 				conn_users, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 530 |  |  | 				connection_id, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 531 |  |  | 				HandlerNames.WEBRTC_TRANSFER, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 532 |  |  | 				Actions.ACCEPT_CALL, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 533 |  |  | 				WebRtcRedisStates.READY, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 534 |  |  | 				{} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 535 |  |  | 			) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 536 |  |  | 		else: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 537 |  |  | 			raise ValidationError("Invalid channel status") | 
            
                                                                                                            
                            
            
                                    
            
            
                | 538 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 539 |  |  | 	def send_call_answer(self, in_message, status_set, reply_action, allowed_state, message_handler): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 540 |  |  | 		connection_id = in_message[VarNames.CONNECTION_ID] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 541 |  |  | 		content = in_message[VarNames.CONTENT] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 542 |  |  | 		conn_users = self.sync_redis.shgetall(connection_id) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 543 |  |  | 		if conn_users[self.id] in allowed_state: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 544 |  |  | 			self.publish_call_answer(conn_users, connection_id, message_handler, reply_action, status_set, content) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 545 |  |  | 		else: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 546 |  |  | 			raise ValidationError("Invalid channel status.") | 
            
                                                                                                            
                            
            
                                    
            
            
                | 547 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 548 |  |  | 	def publish_call_answer(self, conn_users, connection_id, message_handler, reply_action, status_set, content): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 549 |  |  | 		self.async_redis_publisher.hset(connection_id, self.id, status_set) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 550 |  |  | 		del conn_users[self.id] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 551 |  |  | 		message = self.reply_webrtc(reply_action, connection_id, message_handler, content) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 552 |  |  | 		for user in conn_users: | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 553 |  |  | 			if conn_users[user] != WebRtcRedisStates.CLOSED: | 
            
                                                        
            
                                    
            
            
                | 554 |  |  | 				self.publish(message, user) |