[email] Activate routes

This commit is contained in:
maike 2020-07-06 19:14:12 +02:00 committed by dl6tom
parent 261e41c6a9
commit 09758fa8e8
4 changed files with 31 additions and 23 deletions

View file

@ -26,7 +26,13 @@ class EmailBot(Censor):
'hood': self.model.hood, 'hood': self.model.hood,
} }
token = jwt.encode(json, self.model.secret).decode('ascii') 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 += ( message.text += (
"\n\n--\nIf you want to stop receiving these mails, " "\n\n--\nIf you want to stop receiving these mails, "
"follow this link: " + unsubscribe_link "follow this link: " + unsubscribe_link

View file

@ -25,7 +25,6 @@ class BodyMessage(BaseModel):
class Recipient(BaseModel): class Recipient(BaseModel):
hood_name: str
email: str email: str
@ -38,7 +37,8 @@ async def get_email_bot(to):
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND) 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)): async def email_create(hood=Depends(get_hood)):
try: try:
emailbot = await Email.objects.create(hood=hood, secret=urandom(32)) 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) raise HTTPException(status_code=status.HTTP_409_CONFLICT)
@router.delete('/{hood_name}', status_code=status.HTTP_200_OK) @hood_router.delete('/', status_code=status.HTTP_200_OK)
async def email_delete(hood_name): async def email_delete(hood=Depends(get_hood)):
# who calls this function usually? # who calls this function usually?
hood = await Hood.objects.get(name=hood_name)
email_bot = await Email.objects.get(hood=hood) email_bot = await Email.objects.get(hood=hood)
spawner.stop(email_bot) spawner.stop(email_bot)
await EmailRecipients.objects.delete_many(hood=hood) await EmailRecipients.objects.delete_many(hood=hood)
await email_bot.delete() await email_bot.delete()
@router.post('/recipient/') @hood_router.post('/recipient/')
async def email_recipient_create(recipient: Recipient): async def email_recipient_create(recipient: Recipient, hood=Depends(get_hood)):
token = jwt.encode( token = jwt.encode({'email': recipient.email}, Email.secret).decode('ascii')
{'email': recipient.email, 'hood_name': recipient.hood_name,}, Email.secret confirm_link = (
).decode('ascii') config['root_url'] + "api/" + hood.id + "/email/recipient/confirm/" + token
confirm_link = config['root_url'] + "api/email/recipient/confirm/" + token )
send_email( send_email(
recipient.email, recipient.email,
"Subscribe to Kibicara " + recipient.hood_name, "Subscribe to Kibicara " + hood.name,
sender=recipient.hood_name, sender=hood.name,
body="To confirm your subscription, follow this link: " + confirm_link, body="To confirm your subscription, follow this link: " + confirm_link,
) )
return status.HTTP_200_OK return status.HTTP_200_OK
@router.post('/recipient/confirm/{token}') @hood_router.post('/recipient/confirm/{token}')
async def email_recipient_confirm(token): async def email_recipient_confirm(token, hood=Depends(get_hood)):
json = jwt.decode(token, Email.secret) json = jwt.decode(token, Email.secret)
hood = await Hood.objects.get(name=json['hood_name'])
try: try:
await EmailRecipients.objects.create(hood=hood.id, email=json['email']) await EmailRecipients.objects.create(hood=hood.id, email=json['email'])
return status.HTTP_201_CREATED return status.HTTP_201_CREATED
@ -95,14 +93,15 @@ async def email_recipient_confirm(token):
raise HTTPException(status_code=status.HTTP_409_CONFLICT) raise HTTPException(status_code=status.HTTP_409_CONFLICT)
# delete EmailRecipient @hood_router.get('/unsubscribe/{token}', status_code=status.HTTP_200_OK)
@router.get('/unsubscribe/{token}', status_code=status.HTTP_200_OK) async def email_recipient_unsubscribe(token, hood=Depends(get_hood)):
async def email_recipient_unsubscribe(token):
json = jwt.decode(token) 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']) 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): async def email_message_create(message: BodyMessage):
# get bot via "To:" header # get bot via "To:" header
email_bot = await get_email_bot(message.to) email_bot = await get_email_bot(message.to)

View file

@ -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.telegram.webapi import router as telegram_router
from kibicara.platforms.twitter.webapi import router as twitter_router from kibicara.platforms.twitter.webapi import router as twitter_router
from kibicara.platforms.twitter.webapi import twitter_callback_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.admin import router as admin_router
from kibicara.webapi.hoods import router as hoods_router from kibicara.webapi.hoods import router as hoods_router
from kibicara.webapi.hoods.badwords import router as badwords_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 = APIRouter()
router.include_router(admin_router, prefix='/admin', tags=['admin']) 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(triggers_router, prefix='/{hood_id}/triggers')
hoods_router.include_router(badwords_router, prefix='/{hood_id}/badwords') hoods_router.include_router(badwords_router, prefix='/{hood_id}/badwords')
hoods_router.include_router(test_router, prefix='/{hood_id}/test', tags=['test']) 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'] twitter_router, prefix='/{hood_id}/twitter', tags=['twitter']
) )
router.include_router(twitter_callback_router, prefix='/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']) router.include_router(hoods_router, prefix='/hoods', tags=['hoods'])

View file

@ -28,6 +28,6 @@ setup(
'pytoml', 'pytoml',
'requests', 'requests',
'scrypt', 'scrypt',
'pyjwt', 'PyJWT',
], ],
) )