Ejemplo n.º 1
0
def init(request):
    if not request.body:
        return JsonResponse(status=200, data={'message': 'No request body'})
    body = json.loads(bytes(request.body).decode('utf-8'))

    if 'username' not in body:
        return JsonResponse(status=400, data={'message': 'Username is required to join the channel'})

    username = body['username']
    client = StreamChat(api_key=settings.STREAM_API_KEY,
                        api_secret=settings.STREAM_API_SECRET)
    channel = client.channel('messaging', 'General')

    try:
        member = Member.objects.get(username=username)
        token = bytes(client.create_token(
            user_id=member.username)).decode('utf-8')
        return JsonResponse(status=200, data={"username": member.username, "token": token, "apiKey": settings.STREAM_API_KEY})

    except Member.DoesNotExist:
        member = Member(username=username)
        member.save()
        token = bytes(client.create_token(
            user_id=username)).decode('utf-8')
        client.update_user({"id": username, "role": "admin"})
        channel.add_members([username])

        return JsonResponse(status=200, data={"username": member.username, "token": token, "apiKey": settings.STREAM_API_KEY})
Ejemplo n.º 2
0
    def get_stream_token(self, obj):
        """
        docstring
        """
        client = StreamChat(api_key=settings.STREAM_API_KEY,
                            api_secret=settings.STREAM_API_SECRET)
        token = client.create_token(obj.user.id)

        return token
Ejemplo n.º 3
0
async def create_token(user: User, username: str = Depends(get_current_username)):
    STREAM_API_KEY = os.getenv("STREAM_API_KEY")
    STREAM_API_SECRET = os.getenv("STREAM_API_SECRET")

    chat = StreamChat(api_key=STREAM_API_KEY, api_secret=STREAM_API_SECRET)

    data = {
        "id": user.id,
        "name": user.name,
        "role": "admin",
        "image": f"https://robohash.org/{user.id}",
    }

    token = chat.create_token(user.id)
    chat.update_user({"id": user.id, "name": user.name})

    return JSONResponse({"user": data, "token": token, "apiKey": STREAM_API_KEY})
Ejemplo n.º 4
0
 def get_stream_token(self, obj):
     '''get stream token'''
     chat_client = StreamChat(api_key=settings.STREAM_KEY,
                              api_secret=settings.STREAM_SECRET)
     token = chat_client.create_token(f'user{obj.user.id}')
     return token.decode('ascii')
Ejemplo n.º 5
0
from stream_chat import StreamChat
import asyncio

chat = StreamChat(
    api_key="5ee4bvpd9hk5",
    api_secret=
    "va9mfdyrqgdcj3ktz2d46bghs3h6fvk8dynnvv2s9uqqy4z6drbsc2w8df9g8kn7")

# add a user
chat.update_user({"id": "5ec26c75dde4210042211259", "name": "Chuck"})
token = chat.create_token("5ec26c75dde4210042211259")
print(token)

# create a channel about kung-fu
channel = chat.channel("messaging", "5ec26c75dde4210042211259")
channel.create("5ec26c75dde4210042211259")

msg = channel.send_message({"text": "what are the scholarships"}, "Chuck")
Ejemplo n.º 6
0
 def test_create_token(self, client: StreamChat):
     token = client.create_token("tommaso")
     assert type(token) is str
     payload = jwt.decode(token, client.api_secret, algorithms=["HS256"])
     assert payload.get("user_id") == "tommaso"