Example #1
0
    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()
Example #2
0
    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()
Example #3
0
  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()
Example #4
0
    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"}))
Example #5
0
    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
Example #6
0
    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