async def init_group(self, user_id): # Initializing a personal message # {"conv_id": 5, "member": [1, 2, 3], "group_name" : "Livescore", "description" : "A research group", "type": "init_group" } local_conv_id = self.message["conv_id"] mem = self.message["member"] group_name = self.message["group_name"] description = self.message["description"] # save the conversation id in the db session.add( GroupConvInit(user_id, group_name, description, self.timestamp)) # Get db conv_id db_conv_id = session.query(GroupConvInit.conv_id).filter( and_(GroupConvInit.timestamp == self.timestamp, GroupConvInit.creator_id == user_id)).first()[0] # send db_conv_id back to the sender so it can update it in their local db conv_id_update = { "local_conv_id": local_conv_id, "db_conv_id": db_conv_id, "type": "group" } await self.clients.get_client(str(user_id) ).send(json.dumps(conv_id_update)) # save the members of a conv for i in mem: session.add(GroupConvMem(db_conv_id, i)) session.commit()
async def init_personal(self, user_id): # Initializing a personal message {"conv_id": 5, "member": [1, 2], "type": "init_personal"} local_conv_id = self.message["conv_id"] mem = self.message["member"] # save the conversation id in the db session.add(PersonalConvInit(user_id, self.timestamp)) # Get db conv_id db_conv_id = session.query(PersonalConvInit.conv_id).filter( and_(PersonalConvInit.timestamp == self.timestamp, PersonalConvInit.initiator_id == user_id)).first()[0] # send db_conv_id back to the sender so it can update it in their local db conv_id_update = { "local_conv_id": local_conv_id, "db_conv_id": db_conv_id, "type": "personal" } await self.clients.get_client(str(user_id) ).send(json.dumps(conv_id_update)) # save the members of a conv for i in mem: session.add(PersonalConvMem(db_conv_id, i)) session.commit()
def save(self, message, typ, conv_id, ts, message_status): if typ == 'group': session.add(GroupMessage(self.id, conv_id, message, ts, message_status)) # In this case, recipient_id is the recipient_group_id elif typ == 'personal': session.add(PersonalMessage(self.id, conv_id, message, ts, message_status)) session.commit()
async def lec_class_assign(self, user_id): try: class_id = self.message['class_id'] session.add(ClassMem(class_id, int(user_id))) session.commit() await self.clients.get_client(str(user_id)).send( json.dumps({"Status": "Successfully joined"})) except: await self.clients.get_client(str(user_id)).send( json.dumps({"Status": "Not successful"}))
async def send_course_message(self, user_id): course_id = self.message['course_id'] title = self.message['title'] content = self.message['content'] files = self.message['attachments'] user_id = int(user_id) # Get message recepients recipient_id = session.query( CourseMem.member_id).filter_by(course_id=course_id).all() recipient_id = [item for t in recipient_id for item in t] recipient_id.remove(int(user_id)) # Save the message in the DB # session.add(CourseMessageSend(user_id, course_id, title, content, self.timestamp)) # session.commit() model = CourseMessageSend(user_id, course_id, title, content, self.timestamp) session.add(model) session.commit # Get the message id from the database message_id = session.query(CourseMessageSend.id).filter( and_(CourseMessageSend.timestamp == self.timestamp, CourseMessageSend.sender_id == user_id)).first()[0] self.message['message_id'] = message_id # Save files if there are any if files != []: for fil in files: session.add(CourseMessageSendAttach(message_id, fil)) session.commit() # Sends message id to the sender back await self.clients.get_client(str(user_id)).send( json.dumps({"message_id": self.message["message_id"]}, indent=4, sort_keys=True, default=str)) # Send message back to the recievers for receiver in recipient_id: try: online = self.clients.get_client(str(receiver)) await self.clients.get_client(str(receiver)).send( json.dumps(self.message, indent=4, sort_keys=True, default=str)) except: continue
async def reply_class_message(self, user_id): or_msg_id = self.message['message_id'] print(type(or_msg_id)) content = self.message['content'] files = self.message['attachments'] user_id = int(user_id) # Get message recepients class_id = session.query( ClassMessageSend.class_id).filter_by(id=or_msg_id).first()[0] recipient_id = session.query( ClassMem.member_id).filter_by(class_id=class_id).all() recipient_id = [item for t in recipient_id for item in t] recipient_id.remove(int(user_id)) print(recipient_id) # Save the message in the DB model = ClassMessageReply(or_msg_id, user_id, content, self.timestamp) session.add(model) session.commit() # Get the message id from the database message_id = session.query(ClassMessageReply.id).filter( and_(ClassMessageReply.timestamp == self.timestamp, ClassMessageReply.sender_id == user_id)).first()[0] self.message['message_id'] = message_id # Save files if there are any if files != []: for fil in files: session.add(ClassMessageReplyAttach(message_id, fil)) session.commit() # Sends message id to the sender back await self.clients.get_client(str(user_id)).send( json.dumps({"message_id": self.message["message_id"]}, indent=4, sort_keys=True, default=str)) # Send message back to the recievers for receiver in recipient_id: try: online = self.clients.get_client(str(receiver)) await self.clients.get_client(str(receiver)).send( json.dumps(self.message, indent=4, sort_keys=True, default=str)) except: continue
async def handler(self, websocket, path): # the client will send back user id on connection open user_id = str(await websocket.recv()) user_id_int = int(user_id) self.clients.add_client(Client(user_id, websocket)) await websocket.send("user id " + user_id + " received") # This step is to look for all undelivered message and deliver it to the client conv_ids = get_personal_conv_ids(user_id_int) for conv_id in conv_ids: messages = get_personal_messages(conv_id, user_id_int) if messages != []: await send_undelivered_personal_messages(messages, websocket) session.commit() #Commit all changes made to the database # This step is to look for all the undelivered message sent by lecturers to the class while True: try: message = await websocket.recv() ts = datetime.datetime.now() # Handle message message = json.loads(message) #Jsonify the message asyncio.gather( self.handleMessage(user_id, self.clients.get_client(user_id), message, ts)) #Handle the message print("got here") except exceptions.ConnectionClosedError as error: print("connection closed from client") self.clients.delete_client(user_id) break
async def group(self, user_id, typ, ts): conv_id = self.message['conv_id'] recipient_id = session.query( GroupConvMem.member_id).filter_by(conv_id=conv_id).all() recipient_id = [item for t in recipient_id for item in t] print(recipient_id) recipient_id.remove(int(user_id)) message_status = 'sent' #For the group message, all sent messages are saved as 'sent'. No updating for delivered as seen self.clients.save_client_message(user_id, self.message["msg"], typ, conv_id, ts, message_status) message_id = session.query(GroupMessage.id).filter( and_(GroupMessage.timestamp == ts, GroupMessage.sender_id == user_id)).first()[0] self.message["message_id"] = message_id #attach the message_is self.message["timestamp"] = ts #attach the timestamp await self.clients.get_client(str(user_id)).send( json.dumps({'message_id': self.message["message_id"]}) ) #Sends message id to the sender back for receiver in recipient_id: # sends the message to a receiver that is online. If not, pass. The person will receive the message later through synchronization. try: await self.clients.get_client(str(receiver)).send( json.dumps(self.message, indent=4, sort_keys=True, default=str)) session.query(GroupMessage).filter_by(id=message_id).update( {'message_status': 'delivered'}) session.commit() except: pass
def seen(self, message_id): session.query(PersonalMessage).filter_by(id=message_id).update( {'message_status': 'seen'}) session.commit()