def connect(cls, user): u = ChatUser.objects.get(id=user.id) if u.connected == True: sleep(2) u = ChatUser.objects.get(id=user.id) u.connected = True u.nb_ws += 1 u.save() user_cmptrs = [c[0] for c in ComptoirListRequest._comptoir_list(user)] audience = dict() for c in user_cmptrs: audience[c.id] = list() for u in ChatUser.objects.all(): if not u.connected or u == user: continue u = User.objects.get(id = u.id) u_cmptrs = [c[0].id for c in ComptoirListRequest._comptoir_list(u)] for cid in u_cmptrs: if cid in audience.keys(): audience[cid].append(u) publisher = RedisPublisher(facility="fsp", users=[u]) notif_msg = ConnectionMessage(user.username, cid) publisher.publish_message(notif_msg.redis()) for cid, connected in audience.items(): for u in connected: publisher = RedisPublisher(facility="fsp", users=[user]) notif_msg = ConnectedMessage(u.username, cid) publisher.publish_message(notif_msg.redis()) return cls.connected_users.append(user) users_to_notify = list() online_users = dict() for cmptr in user_cmptrs: if cmptr.id not in cls.audience.keys(): cls.audience[cmptr.id] = list() publisher = RedisPublisher(facility="fsp", users=cls.audience[cmptr.id]) notif_msg = ConnectionMessage(user.username, cmptr.id) publisher.publish_message(notif_msg.redis()) for u in cls.audience[cmptr.id]: publisher = RedisPublisher(facility="fsp", users=[user]) notif_msg = ConnectedMessage(u.username, cmptr.id) publisher.publish_message(notif_msg.redis()) cls.audience[cmptr.id].append(user) return
def get_audience(cls, cids): audience = dict() for cid in cids: audience[cid] = list() for u in ChatUser.objects.all(): if not u.connected: u = User.objects.get(id = u.id) continue u = User.objects.get(id = u.id) u_cmptrs = [c[0].id for c in ComptoirListRequest._comptoir_list(u)] for cid in u_cmptrs: if cid in cids: audience[cid].append(u) return audience
def disconnect(cls, user): sleep(2) u = ChatUser.objects.get(id=user.id) u.nb_ws -= 1 u.save() if u.nb_ws <= 0: u.nb_ws = 0 u.connected = False u.save() else: return user_cmptrs = [c[0].id for c in ComptoirListRequest._comptoir_list(user)] audience = cls.get_audience(user_cmptrs) for u in set(reduce(lambda a,b: a+b, audience.values(), [])): publisher = RedisPublisher(facility="fsp", users=[u]) notif_msg = DisconnectionMessage(user.username) publisher.publish_message(notif_msg.redis()) return
def identify(socket, session_key): # Getting session object from session key session = Session.objects.get(session_key=session_key) # Getting user corresponding to the session key uid = session.get_decoded().get('_auth_user_id') user = User.objects.get(pk=uid) # If the user is not logged in, return if not user.is_authenticated() or user.is_anonymous(): return # Get all comptoirs related to the user user_cmptrs = [c[0] for c in ComptoirListRequest._comptoir_list(user)] # If the user is already present with another socket if user in [ui[1] for ui in connected_users.values()]: connected_users[session_key] = ((socket, user, user_cmptrs, None)) # Updating the socket else: # Creation of a new entry in the dictionary connected_users[session_key] = ((socket, user, user_cmptrs, None)) return