def create_space(space):
    channels = space.channels.all()
    channels = map(lambda c: main_pb2.UUID(value=str(c.id)), channels)
    return main_pb2.Space(
        id=main_pb2.UUID(value=str(space.id)),
        creation_timestamp=space.creation_datetime.timestamp(),
        is_default=space.is_default,
        space_name=space.space_name,
        channels=channels,
        display_name=space.display_name)
def create_event(event, event_type=None):
    if event_type in ["HEALTH_CHECK", "CANCEL"]:
        print(event["id"])
        return main_pb2.Event(id=main_pb2.UUID(value=event["id"]),
                              type=event_type)
    elif event_type in ["ENTER_ROOM"]:
        print("create_event", event)
        return main_pb2.Event(
            id=main_pb2.UUID(value=event["id"]),
            request_id=main_pb2.UUID(value=event["request_id"]),
            user_id=main_pb2.UUID(value=event["user_id"]),
            room_id=main_pb2.UUID(value=event["room_id"]),
            type=event_type)
    elif event_type in ["CREATE_MESSAGE"]:
        message = Message.objects.get(id=event["id"])
        return main_pb2.Event(
            id=main_pb2.UUID(value=event["id"]),
            message=create_message(message),
            request_id=main_pb2.UUID(value=event["request_id"]),
            type="RECEIVE_MESSAGE")
    elif event_type in ["CREATE_CHANNEL"]:
        channel = Channel.objects.get(id=event["id"])
        return main_pb2.Event(
            id=main_pb2.UUID(value=event["id"]),
            channel=create_channel(channel),
            request_id=main_pb2.UUID(value=event["request_id"]),
            type="RECEIVE_CHANNEL")
    else:
        print("Unknown event_type", event)
def create_message(message):
    channel = message.channel
    source = message.source
    return main_pb2.Message(
        body=message.body,
        id=main_pb2.UUID(value=str(message.id)),
        request_id=main_pb2.UUID(value=str(message.request_id)),
        source_id=main_pb2.UUID(value=str(source.id)),
        channel_id=main_pb2.UUID(value=str(channel.id)),
        creation_datetime=message.creation_datetime.replace(
            tzinfo=datetime.timezone.utc).isoformat(),
        creation_timestamp=message.creation_datetime.timestamp())
    def get_user_twilio_token(self, user_id, context):
        try:
            user_id = user_id.value
            # print("get_user_twilio_token", user_id)

            TWILIO_ACCOUNT_SID = 'AC0cb509ad66d21f69f2fd6e4b566e2449'
            TWILIO_API_KEY_SID = 'SK6ea1006b0adabde2957b9077e0a25679'
            TWILIO_API_KEY_SECRET = 'AP6uPE2wJoVACZlWG2CSnDBI40MRaYfM'

            # Create an Access Token
            token = AccessToken(TWILIO_ACCOUNT_SID, TWILIO_API_KEY_SID,
                                TWILIO_API_KEY_SECRET)

            # Set the Identity of this token
            token.identity = user_id

            # Grant access to Video
            grant = VideoGrant(room='AwesomeRoom')
            token.add_grant(grant)

            # Serialize the token as a JWT
            jwt = token.to_jwt().decode('utf-8')

            print(jwt)
            print(user_id)
            print(token.payload["exp"])

            return main_pb2.JWTToken(id=main_pb2.UUID(value=str(user_id)),
                                     expiry_time=token.payload["exp"],
                                     jwt=jwt)

        except Exception as e:
            print("Error in get_user", e)
            traceback.print_exc(file=sys.stdout)
def create_channel(channel, request_id=None):

    message_count = channel.message_set.count()
    members = channel.members.all()
    members = map(lambda m: main_pb2.UUID(value=str(m.id)), members)

    channel = main_pb2.Channel(
        id=main_pb2.UUID(value=str(channel.id)),
        # preview_id=main_pb2.UUID(value=str(channel.preview.id)),
        members=members,
        channel_name=channel.channel_name,
        message_count=message_count,
        creation_datetime=channel.creation_datetime.replace(
            tzinfo=datetime.timezone.utc).isoformat(),
        creation_timestamp=channel.creation_datetime.timestamp())

    if request_id:
        channel.request_id.CopyFrom(main_pb2.UUID(value=str(request_id)))
    return channel
def create_user(user):
    connections = user.connections.all()
    organizations = user.organization_set.all()
    connections = map(lambda c: main_pb2.UUID(value=str(c.id)), connections)
    organizations = map(lambda o: main_pb2.UUID(value=str(o.id)),
                        organizations)

    return main_pb2.User(user_name=user.user_name,
                         space=main_pb2.UUID(value=str(user.space.id)),
                         home_room=main_pb2.UUID(value=str(user.home_room.id)),
                         display_name=user.display_name,
                         image_url=user.image_url,
                         connections=connections,
                         biography=user.biography,
                         organizations=organizations,
                         phone_number=str(user.phone_number),
                         id=main_pb2.UUID(value=str(user.id)),
                         creation_datetime=user.creation_datetime.replace(
                             tzinfo=datetime.timezone.utc).isoformat(),
                         creation_timestamp=user.creation_datetime.timestamp())
    def get_organization(self, org_id, context):
        try:
            org_id = org_id.value
            org = Organization.objects.get(id=org_id)
            members = org.members.all()
            members = map(lambda m: main_pb2.UUID(value=str(m.id)), members)
            return main_pb2.Organization(id=str(org.id),
                                         display_name=org.display_name,
                                         image_url=org.image_url,
                                         members=members)

        except Exception as e:
            print("Error in get_organization", e)
            traceback.print_exc(file=sys.stdout)