def post_list(db, usernick= None, limit=50):
    """Return a list of posts ordered by date
    db is a database connection (as returned by COMP249Db())
    if usernick is not None, return only posts by this user
    return at most limit posts (default 50)

    Returns a list of tuples (id, timestamp, usernick, avatar,  content)
    """

    cursor = db.cursor()
    if usernick != None:
        sql = """select id, timestamp, posts.usernick, avatar, content from posts,
         users where posts.usernick = users.nick and posts.usernick = ? order by timestamp desc"""
        cursor.execute(sql, (usernick,))
        query = cursor.fetchall()
        length = len(query)
        for item in range(limit,length):
            query.pop()
        return query
    else:
        sql = """select id, timestamp, posts.usernick, avatar, content from posts,
        users where posts.usernick = users.nick order by timestamp desc"""
        cursor.execute(sql)
        query = cursor.fetchall()
        length1 = len(query)
        for item in range(limit,length1):
            query.pop()
        return query