def list_users():
    con = connect()
    content = get_json(request)
    required_data = ["forum"]    
    optional = intersection(content, ["limit", "order", "since"])    
    try:
        check_data(content, required_data)
        users_l = query.users_list(con, content["forum"], optional)
    except Exception as e:
         con.close()
         return json.dumps({"code": 1, "response": (e.message)})
    con.close()
    return json.dumps({"code": 0, "response": users_l})
def list_following():
    con = connect()
    request_data = get_json(request)
    required_data = ["user"]
    followers_param = intersection(request=request_data, values=["limit", "order", "since_id"])
    try:
        check_data(data=request_data, required=required_data)
        follower_l = query.followers_list(connect=con, email=request_data["user"], type="follow", params=followers_param)
    except Exception as e:
        con.close()
        return json.dumps({"code": 1, "response": (e.message)})
    con.close()
    return json.dumps({"code": 0, "response": follower_l})
def user_posts():
    con = connect()
    content = get_json(request)
    required_data = ["user"]
    optional = intersection(content, ["limit", "order", "since"])    
    try:
        check_data(content, required_data)
        posts_l = posts.posts_list(con, "user", optional, content["user"], [])
    except Exception as e:
         con.close()
         return json.dumps({"code": 1, "response": (e.message)})
    con.close()
    return json.dumps({"code": 0, "response": posts_l})
def list_threads():
    con = connect()
    content = get_json(request)
    required_data = ["forum"]
    related = related_exists(content)
    optional = intersection(content, ["limit", "order", "since"])
    try:
        check_data(content, required_data)
        threads_l = threads.threads_list(con, "forum", optional, content["forum"], related)
    except Exception as e:
        con.close()
        return json.dumps({"code": 1, "response": (e.message)})
    con.close()
    return json.dumps({"code": 0, "response": threads_l})
def create():
    con = connect()     
    content = request.json
    required_data = ["date", "thread", "message", "user", "forum"]
    optional = intersection(content, ["parent", "isApproved", "isHighlighted", "isEdited", "isSpam", "isDeleted"])
    try:
        check_data(content, required_data)
        post = query.save_post(con , content["date"], content["thread"], content["message"], content["user"], content["forum"], optional)
    except Exception as e:
        con.close()
        if e.message == "Exist":
            return json.dumps({"code": 4, "response": (e.message)}) 
        if e.message == "KeyError":
            return json.dumps({"code": 3, "response": (e.message)}) 
        if e.message == "ValueError":
            return json.dumps({"code": 2, "response": (e.message)}) 
        return json.dumps({"code": 1, "response": (e.message)})
    con.close()    
    return json.dumps({"code": 0, "response": post})
def create():
    con = connect()     
    content = request.json
    required_data = ["username", "about", "name", "email"]
    optional = intersection(request = content, values=["isAnonymous"])
    try:
        check_data(data=content, required=required_data)
        user = query.save_user(connect = con , username = content["username"], about = content["about"],name =  content["name"], email =content["email"], optional = optional)
    except Exception as e:        
        con.close()
        if e.message == "Exist":            
            return json.dumps({"code": 5, "response": (e.message)}) 
        if e.message == "KeyError":            
            return json.dumps({"code": 3, "response": (e.message)}) 
        if e.message == "ValueError":            
            return json.dumps({"code": 2, "response": (e.message)}) 
                    
        return json.dumps({"code": 1, "response": (e.message)})
    con.close()    
    return json.dumps({"code": 0, "response": user})
def post_list():
    con = connect()
    content = get_json(request)
    try:
        identifier = content["forum"]
        entity = "forum"
    except KeyError:
        try:
            identifier = content["thread"]
            entity = "thread"
        except Exception as e:
            con.close()
            return json.dumps({"code": 1, "response": (e.message)})
    optional = intersection(content, ["limit", "order", "since"])    
    try:
        posts_l = query.posts_list(con, entity, optional, identifier, [])
    except Exception as e:
         con.close()
         return json.dumps({"code": 1, "response": (e.message)})
    con.close()
    return json.dumps({"code": 0, "response": posts_l})