From 09758fa8e85e0fb5a14f4f10c321bb0049d6ea8c Mon Sep 17 00:00:00 2001 From: maike Date: Mon, 6 Jul 2020 19:14:12 +0200 Subject: [PATCH] [email] Activate routes --- kibicara/platforms/email/bot.py | 8 +++++- kibicara/platforms/email/webapi.py | 41 +++++++++++++++--------------- kibicara/webapi/__init__.py | 3 +++ setup.py | 2 +- 4 files changed, 31 insertions(+), 23 deletions(-) diff --git a/kibicara/platforms/email/bot.py b/kibicara/platforms/email/bot.py index ec65def..18ae272 100644 --- a/kibicara/platforms/email/bot.py +++ b/kibicara/platforms/email/bot.py @@ -26,7 +26,13 @@ class EmailBot(Censor): 'hood': self.model.hood, } token = jwt.encode(json, self.model.secret).decode('ascii') - unsubscribe_link = config['root_url'] + 'api/email/unsubscribe/' + token + unsubscribe_link = ( + config['root_url'] + + 'api/' + + self.model.id + + '/email/unsubscribe/' + + token + ) message.text += ( "\n\n--\nIf you want to stop receiving these mails, " "follow this link: " + unsubscribe_link diff --git a/kibicara/platforms/email/webapi.py b/kibicara/platforms/email/webapi.py index 0f0ad9a..c772305 100644 --- a/kibicara/platforms/email/webapi.py +++ b/kibicara/platforms/email/webapi.py @@ -25,7 +25,6 @@ class BodyMessage(BaseModel): class Recipient(BaseModel): - hood_name: str email: str @@ -38,7 +37,8 @@ async def get_email_bot(to): raise HTTPException(status_code=status.HTTP_404_NOT_FOUND) -router = APIRouter() +hood_router = APIRouter() +mailbox_router = APIRouter() """ @@ -49,7 +49,7 @@ async def test_read_all(hood=Depends(get_hood)): """ -@router.post('/', status_code=status.HTTP_201_CREATED) +@hood_router.post('/', status_code=status.HTTP_201_CREATED) async def email_create(hood=Depends(get_hood)): try: emailbot = await Email.objects.create(hood=hood, secret=urandom(32)) @@ -59,35 +59,33 @@ async def email_create(hood=Depends(get_hood)): raise HTTPException(status_code=status.HTTP_409_CONFLICT) -@router.delete('/{hood_name}', status_code=status.HTTP_200_OK) -async def email_delete(hood_name): +@hood_router.delete('/', status_code=status.HTTP_200_OK) +async def email_delete(hood=Depends(get_hood)): # who calls this function usually? - hood = await Hood.objects.get(name=hood_name) email_bot = await Email.objects.get(hood=hood) spawner.stop(email_bot) await EmailRecipients.objects.delete_many(hood=hood) await email_bot.delete() -@router.post('/recipient/') -async def email_recipient_create(recipient: Recipient): - token = jwt.encode( - {'email': recipient.email, 'hood_name': recipient.hood_name,}, Email.secret - ).decode('ascii') - confirm_link = config['root_url'] + "api/email/recipient/confirm/" + token +@hood_router.post('/recipient/') +async def email_recipient_create(recipient: Recipient, hood=Depends(get_hood)): + token = jwt.encode({'email': recipient.email}, Email.secret).decode('ascii') + confirm_link = ( + config['root_url'] + "api/" + hood.id + "/email/recipient/confirm/" + token + ) send_email( recipient.email, - "Subscribe to Kibicara " + recipient.hood_name, - sender=recipient.hood_name, + "Subscribe to Kibicara " + hood.name, + sender=hood.name, body="To confirm your subscription, follow this link: " + confirm_link, ) return status.HTTP_200_OK -@router.post('/recipient/confirm/{token}') -async def email_recipient_confirm(token): +@hood_router.post('/recipient/confirm/{token}') +async def email_recipient_confirm(token, hood=Depends(get_hood)): json = jwt.decode(token, Email.secret) - hood = await Hood.objects.get(name=json['hood_name']) try: await EmailRecipients.objects.create(hood=hood.id, email=json['email']) return status.HTTP_201_CREATED @@ -95,14 +93,15 @@ async def email_recipient_confirm(token): raise HTTPException(status_code=status.HTTP_409_CONFLICT) -# delete EmailRecipient -@router.get('/unsubscribe/{token}', status_code=status.HTTP_200_OK) -async def email_recipient_unsubscribe(token): +@hood_router.get('/unsubscribe/{token}', status_code=status.HTTP_200_OK) +async def email_recipient_unsubscribe(token, hood=Depends(get_hood)): json = jwt.decode(token) + if hood.id is not json['hood']: + raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST) await EmailRecipients.objects.delete_many(hood=json['hood'], email=json['email']) -@router.post('/messages/') +@mailbox_router.post('/messages/') async def email_message_create(message: BodyMessage): # get bot via "To:" header email_bot = await get_email_bot(message.to) diff --git a/kibicara/webapi/__init__.py b/kibicara/webapi/__init__.py index 0b5cd57..ee113ce 100644 --- a/kibicara/webapi/__init__.py +++ b/kibicara/webapi/__init__.py @@ -14,6 +14,7 @@ from kibicara.platforms.test.webapi import router as test_router from kibicara.platforms.telegram.webapi import router as telegram_router from kibicara.platforms.twitter.webapi import router as twitter_router from kibicara.platforms.twitter.webapi import twitter_callback_router +from kibicara.platforms.email.webapi import mailbox_router, hood_router as email_router from kibicara.webapi.admin import router as admin_router from kibicara.webapi.hoods import router as hoods_router from kibicara.webapi.hoods.badwords import router as badwords_router @@ -22,6 +23,7 @@ from kibicara.webapi.hoods.triggers import router as triggers_router router = APIRouter() router.include_router(admin_router, prefix='/admin', tags=['admin']) +router.include_router(mailbox_router, prefix='/email', tags=['email']) hoods_router.include_router(triggers_router, prefix='/{hood_id}/triggers') hoods_router.include_router(badwords_router, prefix='/{hood_id}/badwords') hoods_router.include_router(test_router, prefix='/{hood_id}/test', tags=['test']) @@ -32,4 +34,5 @@ hoods_router.include_router( twitter_router, prefix='/{hood_id}/twitter', tags=['twitter'] ) router.include_router(twitter_callback_router, prefix='/twitter', tags=['twitter']) +hoods_router.include_router(email_router, prefix='/{hood_id}/email', tags=['email']) router.include_router(hoods_router, prefix='/hoods', tags=['hoods']) diff --git a/setup.py b/setup.py index 057aca2..50af4c9 100644 --- a/setup.py +++ b/setup.py @@ -28,6 +28,6 @@ setup( 'pytoml', 'requests', 'scrypt', - 'pyjwt', + 'PyJWT', ], )