import json 

from snek.app import app


def dump_public_channels():
    result = {'channels':{}}

    for channel in app.db['channel'].find(is_private=False,is_listed=True):
        result['channels'][channel['label']] = dict(channel)
        result['channels'][channel['label']]['messages'] = list(dict(record) for record in app.db['channel_message'].find(channel_uid=channel['uid']))

    print(json.dumps(result, sort_keys=True, indent=4,default=str),end='',flush=True)
    

if __name__ == '__main__':
    dump_public_channels()
