Exemplo n.º 1
0
def getCompany(name):
    # name = request.args.get("name") # this is from query params ?...
    print(f"Requesting info for company {name}")

    namereg = re.compile(f"^{name}", re.IGNORECASE)

    # if the query param detailed is present, show all company info
    info = {
        "_id": 0,
        "name": 1,
        "home_url": 1,
        "email_address": 1,
        "founded_year": 1
    }

    # checkValidParams(["detailed"])

    if request.args.get("detailed") == "1":
        print("DETAILED INFO")
        info = {"_id": 0}
    else:
        print("BASIC INFO")

    company = companies.find_one({"name": namereg}, info)

    if not company:
        raise Error404("This company is not found on database")

    return data(company)
Exemplo n.º 2
0
def add_more_tochat(conversation_id):
    """    
    This function adds an existing user to a certain group chat.
    """
    if db.convo.find_one({"_id":ObjectId(conversation_id)}):
        new_user = request.args.get("user_name")
        add_user = db.convo.update({"_id":ObjectId(conversation_id)},{"$push":{"Participants":{"$each": new_user}}})
        return dumps(f"The user was succesfully added to the chat!!! user_id: {new_user}, conversation_id: {conversation_id} ")
    raise Error404("Sorry, that convo has not been created yet. Please be so kind as to try a different one.") 
Exemplo n.º 3
0
def listMessage(conversation_id):
    '''
    This function allows you to download and view the messages of a conversation through a request from the API
    '''
    print(f'Requesting to list all message in the chat-room {conversation_id}')
    chat = db.chatItem.find_one({'_id': ObjectId(conversation_id)})
    if chat:
        try:
            chat_json = json.loads(json_util.dumps(chat))
            return {
                'conversation_id': chat_json['_id'],
                'chat_name': chat_json['chat_name'],
                'messages': chat_json['messages']
            }
        except:
            raise Error404('List empty')
    raise APIError('Error. chat doesn\'t exist')
def sentimentAnalysis(conversation_id):
    '''
    This function receives an Id_conversation as a parameter, returning a sentiment analysis of all chat messages
    '''
    print(
        f'Requesting to a sentiment analysis from the chat-room {conversation_id}'
    )
    chat = db.chatItem.find_one({'_id': ObjectId(conversation_id)})
    if chat:
        chat_json = json.loads(json_util.dumps(chat))
        chat_mess = []
        for message in chat_json.items():
            for mess in message[1]:
                try:
                    chat_mess.append(mess['message'])
                except:
                    pass
        if len(chat_mess) > 0:
            return json.dumps(polarityBySentence(chat_mess))
        raise Error404('List empty')
    raise APIError('Error. chat doesn\'t exist')
Exemplo n.º 5
0
def addChatUser(chat_id, user_id=None):
    if user_id==None:
        user_id= request.args.get("user_id")
    if user_id!=None and chat_id!=None:
        #update of the chats document by adding the user id
        post=db_chats.find_one({'_id':ObjectId(chat_id)})
        if ObjectId(user_id) not in post['users_list']:
            post['users_list'].append(ObjectId(user_id))
        db_chats.update_one({'_id':ObjectId(chat_id)}, {"$set": post}, upsert=False)
        
        #update of the user permissions by adding the chats id
        post=db.find_one({'_id':ObjectId(user_id)})
        if ObjectId(chat_id) not in post['chats_list']:
            post['chats_list'].append(ObjectId(chat_id))
        db.update_one({'_id':ObjectId(user_id)}, {"$set": post}, upsert=False)
    elif not chat_id:
        print("ERROR")
        raise Error404("chat_id not found")
    elif not user_id:
        print("ERROR")
        raise APIError("You should send these query parameters ?user_id=<user_id>")

    return json.dumps({'chat_id': str(chat_id)})
Exemplo n.º 6
0
def ljkahlsd():
    raise Error404("Please provide a name")
Exemplo n.º 7
0
    """
    if db.convo.find_one({"_id":ObjectId(conversation_id)}):
        new_user = request.args.get("user_name")
        add_user = db.convo.update({"_id":ObjectId(conversation_id)},{"$push":{"Participants":{"$each": new_user}}})
        return dumps(f"The user was succesfully added to the chat!!! user_id: {new_user}, conversation_id: {conversation_id} ")
    raise Error404("Sorry, that convo has not been created yet. Please be so kind as to try a different one.") 


# Adding message to chat
# (form instead of args because the method is POST and not GET)

@app.route("/chat/<conversation_id>/addmessage/<user_id>",methods = ['POST'])
@errorHandler
def addmessage(conversation_id:
    """    
    This function adds a message to an existing chat.
    """
     if db.convo.find_one({"_id":ObjectId(conversation_id),"Participants":user_id}) :
        add_message= db.chatItem.insert({"conversation_id":ObjectId(conversation_id), "user_id": user, "message":f'{mensaje}'})
        return dumps(f"The message was succesfully added!! message_id: {add_message}")
    raise Error404("Something went wrong, please try again.") 


# Retrieving all messages from x chat

@app.route("/chat/<conversation_id>/list")
def retrieve_all(conversation_id):
    all_m = db.chatItem.find({"conversation_id": ObjectId(conversation_id)}, {"message":1, "_id":0})
    messages= dumps(db.chatItem.find({"conversation_id": ObjectId(conversation_id)}, {"user_id":1,"message":1, "_id":0}))
    return dumps(messages)