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, usernick, avatar, content
        FROM posts
        JOIN users ON posts.usernick=users.nick
        WHERE usernick=(?)
        ORDER by timestamp DESC
        LIMIT (?);"""
        cursor.execute(sql,(usernick, limit))
    else:
        sql = """SELECT id, timestamp, usernick, avatar, content
        FROM posts
        JOIN users ON posts.usernick=users.nick
        ORDER by timestamp DESC
        LIMIT (?);"""
        cursor.execute(sql, (limit,))

    result = []

    for row in cursor:
        result.append(row)

    return result