# Path: chat/encoding.py
# def serialize_record(record):
#     r = skyserialize(record)
#     if 'attachment' in r:
#         r['attachment']['$url'] = sign_asset_url(r['attachment']['$name'])
#     return r
# 
# Path: chat/hub.py
# class Hub:
# 
#     def __init__(self, end_point=None, api_key=None):
#         self.transport = 'websocket'
#         self.end_point = end_point or _get_default_pubsub_url()
#         self.api_key = api_key or options.apikey
# 
#     def publish(self, channels, data):
#         wsopts = {}
#         if self.api_key:
#             wsopts['header'] = [
#                     'X-Skygear-API-Key: {0}'.format(self.api_key)
#                     ]
#         conn = create_connection(self.end_point, **wsopts)
#         if isinstance(channels, str):
#             channels = [channels]
#         for channel in channels:
#             _data = encoder({
#                 'action': 'pub',
#                 'channel': channel,
#                 'data': data,
#             })
#             conn.send(_data)
#         conn.close()
# 
# Path: chat/utils.py
# def _get_channels_by_user_ids(user_ids):
#     # TODO: use database.query instead of raw SQL
#     with db.conn() as conn:
#         cur = conn.execute('''
#             SELECT name
#             FROM %(schema_name)s.user_channel
#             WHERE _owner_id in %(user_ids)s
#             LIMIT %(len)s;
#             ''', {
#             'schema_name': AsIs(_get_schema_name()),
#             'user_ids': tuple(user_ids),
#             'len': len(user_ids),
#         }
#         )
# 
#         results = []
#         for row in cur:
#             results.append(row[0])
# 
#         return results


from skygear.models import Record
from skygear.options import options as skyoptions
from .encoding import serialize_record
from .hub import Hub
from .utils import _get_channels_by_user_ids



def _publish_event(user_ids: [], event: str, data: dict = None) -> None:
    if not isinstance(user_ids, list):
        user_ids = user_ids
    if len(user_ids) == 0:
        return
    channel_names = _get_channels_by_user_ids(user_ids)
    if channel_names:

# Path: chat/encoding.py
# def serialize_record(record):
#     r = skyserialize(record)
#     if 'attachment' in r:
#         r['attachment']['$url'] = sign_asset_url(r['attachment']['$name'])
#     return r
# 
# Path: chat/hub.py
# class Hub:
# 
#     def __init__(self, end_point=None, api_key=None):
#         self.transport = 'websocket'
#         self.end_point = end_point or _get_default_pubsub_url()
#         self.api_key = api_key or options.apikey
# 
#     def publish(self, channels, data):
#         wsopts = {}
#         if self.api_key:
#             wsopts['header'] = [
#                     'X-Skygear-API-Key: {0}'.format(self.api_key)
#                     ]
#         conn = create_connection(self.end_point, **wsopts)
#         if isinstance(channels, str):
#             channels = [channels]
#         for channel in channels:
#             _data = encoder({
#                 'action': 'pub',
#                 'channel': channel,
#                 'data': data,
#             })
#             conn.send(_data)
#         conn.close()
# 
# Path: chat/utils.py
# def _get_channels_by_user_ids(user_ids):
#     # TODO: use database.query instead of raw SQL
#     with db.conn() as conn:
#         cur = conn.execute('''
#             SELECT name
#             FROM %(schema_name)s.user_channel
#             WHERE _owner_id in %(user_ids)s
#             LIMIT %(len)s;
#             ''', {
#             'schema_name': AsIs(_get_schema_name()),
#             'user_ids': tuple(user_ids),
#             'len': len(user_ids),
#         }
#         )
# 
#         results = []
#         for row in cur:
#             results.append(row[0])
# 
#         return results


# Path: chat/pubsub.py
from skygear.models import Record
from skygear.options import options as skyoptions
from .encoding import serialize_record
from .hub import Hub
from .utils import _get_channels_by_user_ids




def _publish_event(user_ids: [], event: str, data: dict = None) -> None:
    if not isinstance(user_ids, list):
        user_ids = user_ids
    if len(user_ids) == 0:
        return
    channel_names = _get_channels_by_user_ids(user_ids)
    if channel_names:

        hub = Hub(api_key=skyoptions.apikey)