示例#1
0
def first_message_to_user(content, email, device):
    try:
        user = db.session.query(UserModel).filter(
            UserModel.email == email).first()
        if user is None:
            raise e_account.UserNotFound
        elif not device.circle.has_member(user):
            raise e_cirle.UserNotPartOfCircle
        conversation = Conversation(device_access=True)
        conversation.circle = device.circle
        conversation.name = "NewConversation"
        link = UserToConversation(privilege="ADMIN")
        link.user = user
        link.conversation = conversation
        message = Message(content=content["text_message"]
                          if "text_message" in content else "",
                          is_user=False)
        message.conversation = conversation
        message.device = device
        media_list = []
        if "files" in content:
            for file in content["files"]:
                media = Media()
                media.identifier = file
                MessageToMedia(message=message, media=media)
                db.session.commit()
                media_list.append(media.get_simple_content())
        db.session.commit()
        sockets.notify_user(user, False, 'conversation', {
            "conversation_id": conversation.id,
            "event": 'invite'
        })
        info_and_message = "[" + conversation.name + "] " + device.name + " : " + str(
            message.text_content)
        try:
            messenger_conversation_model_send(0, conversation,
                                              info_and_message)
        except Exception:
            pass
        try:
            hangout_conversation_model_send(0, conversation, info_and_message)
        except Exception:
            pass
        response = {
            "data": {
                "success": True,
                'media_list': media_list,
                'message_id': message.id
            },
            "status_code": 200
        }
    except (e_cirle.CircleException, e_account.AccountException) as exception:
        response = {
            "data": {
                "success": False,
                "message": exception.message
            },
            "status_code": exception.status_code
        }
    return response
示例#2
0
def invite(conversation_id, email, client, is_device):
    try:
        if not is_device:
            link = db.session.query(UserToConversation).filter(
                UserToConversation.conversation_id == conversation_id,
                UserToConversation.user_id == client.id).first()
            if link is None:
                raise e_conversation.UserForbiddenAccess
            conversation = link.conversation
        else:
            conversation = db.session.query(Conversation).filter(
                Conversation.id == conversation_id).first()
        if conversation is None:
            raise e_conversation.ConversationNotFound
        recipient = db.session.query(UserModel).filter(
            UserModel.email == email).first()
        if recipient is None:
            raise e_conversation.SpecifiedUserNotFound
        temp = db.session.query(UserToConversation).filter(
            UserToConversation.conversation_id == conversation_id,
            UserToConversation.user_id == recipient.id).first()
        if temp is not None:
            raise e_conversation.UserAlreadyPartOfConversation
        if not conversation.circle.has_member(recipient):
            raise e_circle.UserNotPartOfCircle
        new_link = UserToConversation(privilege="STANDARD")
        new_link.user = recipient
        new_link.conversation = conversation
        db.session.commit()
        recipient.notify_me(title="Conversation",
                            body=("Vous êtes invité dans la conversation %s" %
                                  (conversation.name)))
        conversation.mobile_notification(
            title="Conversation",
            body=("%s à rejoins la conversation %s" %
                  (recipient.first_name, conversation.name)))
        sockets.notify_user(client=recipient,
                            is_device=False,
                            p1='conversation',
                            p2={
                                'event': 'invite',
                                'conversation_id': conversation_id
                            })
        response = {"data": {"success": True}, "status_code": 200}
    except (e_conversation.ConversationException,
            e_circle.CircleException) as exception:
        response = {
            "data": {
                "success": False,
                "message": exception.message
            },
            "status_code": exception.status_code
        }
    return response
示例#3
0
 def test_change_admin(self):
     utc = UserToConversation()
     utc.user = self.user3
     utc.conversation = self.conv
     db.session.commit()
     json_data = {
         "token": self.token1,
         "conversation_id": self.conv.id,
     }
     response = self.api.post('/conversation/quit',
                              data=json.dumps(json_data),
                              content_type='application/json')
     response_json = json.loads(response.data)
     assert response.status_code == 200
     assert response_json['success']
     assert (utc.privilege == "ADMIN" or self.utc2.privilege == "ADMIN")
示例#4
0
 def test_already_existing_user(self):
     utc = UserToConversation()
     utc.user = self.user2
     utc.conversation = self.conv
     db.session.commit()
     json_data = {
         "token": self.token1,
         "conversation_id": self.conv.id,
         "email": self.user2.email
     }
     response = self.api.post('/conversation/invite',
                              data=json.dumps(json_data),
                              content_type='application/json')
     response_json = json.loads(response.data)
     assert response.status_code != 200
     assert not response_json['success']
     assert len(self.conv.links) == 2
示例#5
0
def first_message_to_device(content, circle_id, user):
    try:
        circle = db.session.query(Circle).filter(
            Circle.id == circle_id).first()
        if circle is None:
            raise e_circle.CircleNotFound
        elif not circle.has_member(user):
            raise e_circle.UserNotPartOfCircle
        conversation = Conversation(
            name=content["conversation_name"]
            if "conversation_name" in content else circle.name,
            device_access=True)
        conversation.circle = circle
        link = UserToConversation(privilege="ADMIN")
        link.user = user
        link.conversation = conversation
        message = Message(content=content["text_message"] if "text_message" in
                          content else "")
        message.conversation = conversation
        message.link = link
        media_list = []
        if "files" in content:
            for file in content["files"]:
                media = Media()
                media.identifier = file
                MessageToMedia(message=message, media=media)
                db.session.commit()
                media_list.append(media.get_simple_content())
        db.session.commit()
        sockets.notify_user(circle.device, True, 'conversation', {
            "conversation_id": conversation.id,
            "event": 'invite'
        })
        message.conversation.mobile_notification(
            title="Conversation",
            body="Nouvelle conversation avec " + user.first_name)
        info_sender = "[" + conversation.name + "] " + user.first_name + " : "
        try:
            messenger_conversation_model_send(
                user.id, conversation, info_sender + message.text_content)
        except Exception:
            pass
        try:
            hangout_conversation_model_send(user.id, conversation,
                                            info_sender + message.text_content)
        except Exception:
            pass
        response = {
            "data": {
                "success": True,
                'media_list': media_list,
                'message_id': message.id
            },
            "status_code": 200
        }
    except (e_message.MessageException, e_circle.CircleException) as exception:
        response = {
            "data": {
                "success": False,
                "message": exception.message
            },
            "status_code": exception.status_code
        }
    return response