def message_react(token, message_id, react_id):

    token = request.form.get('token')
    message_id = request.form.get('message_id')
    react_id = request.form.get("react_id")
    message_index = message_id - 1
    message_data = get_global_messages()
    user_data = get_global_users()
    channel_list = get_global_channel()

    position = position_in_user_data(token, user_data)

    check_valid_react_id(react_id)
    check_valid_message_id(message_id, message_data)
    is_user_in_channel_mid(position, user_data, channel_list, message_id, message_data)
    check_if_deleted(message_id, message_data)

    if user_data[position]['u_id'] in message_data[message_index]['reacts']['u_id']:
            message['reacts']['is_this_user_reacted'] = True
            raise InputError(description='Already reacted by requested user')
    else:
        message['reacts']['u_id'].append(user_data[position]['u_id'])
        message['reacts']['is_this_user_reacted'] = True    #This is the scary one

    return dumps({})
def message_send(token, channel_id, message):

    #Getting information from request
    token = request.form.get('token')
    channel_id = request.form.get('channel_id')
    message = request.form.get('message')
    message_data = get_global_messages()
    user_data = get_global_users()

    #Determinig which user in the list of diciontarys requested
    position = position_in_user_data(token, user_data)

    #Checking for correct input
    is_user_in_channel(position, user_data, channel_id, channel_list)
    check_valid_message(message)

    append_message = {'message_id': message_counter, 
            'u_id': user_data[position]['u_id'],
            'message': message, 
            'time_created': datetime.now(), 
            'reacts': [{
                'react_id': 1,
                'u_id': [],
                'is_this_user_reacted': False
            }], 
            #Cheating since theres only one react_id for now
            'is_pinned': False,
            'in_channel': channel_id
}
    message_data.append(append_message)

    message_counter += 1

    return dumps({'message_id': message_counter})
def message_unpin(token, message_id):

    token = request.form.get('token')
    message_id = request.form.get('message_id')
    message_data = get_global_messages()
    user_data = get_global_users()
    channel_list = get_global_channel()

    position = position_in_user_data(token, user_data)
    check_valid_message_id(message_id, message_data)
    check_user_is_owner(position, user_data, channel_list)
    is_user_in_channel_mid(position, user_data, channel_list, message_id, message_data)
    check_if_deleted(message_id, message_data)

    if message_data[message_index]['is_pinned'] == False:
        raise InputError(description='Message is already unpinned')
    else: 
        message_data[message_index]['is_pinned'] = False

    return dumps({})
def search(token, query_str):

    token = request.args.get('token')
    query_str = request.args.get('query_str')
    message_data = get_global_messages()
    user_data = get_global_users()

    position = position_in_user_data(token, user_data)
    u_id = user_data[position]['u_id']

    messages_return = []

    for message in message_data:
        if query_str in message['message']:
            if u_id in message['reacts']['u_id']:
                message['reacts']['is_this_user_reacted'] = True
            else:
                message['reacts']['is_this_user_reacted'] = False
        messages_return.append(message)

    return dumps(messages_return)
def message_remove(token, message_id):

    token = request.form.get('token')
    message_id = request.form.get('message_id')
    message_index = message_id - 1
    message_data = get_global_messages()
    user_data = get_global_users()
    channel_list = get_global_channel()

    position = position_in_user_data(token, user_data)
    check_valid_message_id(message_id, message_data)
    check_user_is_owner(position, user_data, channel_list)
    is_user_in_channel_mid(position, user_data, channel_list, message_id, message_data)
    check_if_deleted(message_id, message_data)

    if user_data[position]['permission_id'] != 1 and message_data[message_index]['u_id'] != user_data[position]['u_id']:
        raise AccessError(description='Permission denied')
    else:
        message_data[message_position]['message'] = None

    return dumps({})