diff --git a/kibicara/platforms/email/webapi.py b/kibicara/platforms/email/webapi.py index a226f34..05a326b 100644 --- a/kibicara/platforms/email/webapi.py +++ b/kibicara/platforms/email/webapi.py @@ -2,14 +2,17 @@ # # SPDX-License-Identifier: 0BSD -from fastapi import APIRouter, Depends, HTTPException, Response, status +from fastapi import APIRouter, Depends, HTTPException, status from kibicara.platforms.email.bot import spawner -from kibicara.platforms.email.model import Email +from kibicara.platforms.email.model import Email, EmailRecipients from kibicara.platformapi import Message from kibicara.webapi.hoods import get_hood +from kibicara.webapi.admin import url +from kibicara.email import send_email from ormantic.exceptions import NoMatch from pydantic import BaseModel from sqlite3 import IntegrityError +import jwt class BodyMessage(BaseModel): @@ -19,6 +22,11 @@ class BodyMessage(BaseModel): secret: str +class Recipient(BaseModel): + hood: int + email: str + + async def get_email_bot(to, hood=Depends(get_hood)): try: return await Email.objects.get(hood=to) @@ -30,11 +38,12 @@ router = APIRouter() """ +# get Email row? @router.get('/') async def test_read_all(hood=Depends(get_hood)): return await Email.objects.filter(hood=hood).all() - +# create Email row @router.post('/', status_code=status.HTTP_201_CREATED) async def test_create(response: Response, hood=Depends(get_hood)): try: @@ -44,12 +53,36 @@ async def test_create(response: Response, hood=Depends(get_hood)): return test except IntegrityError: raise HTTPException(status_code=status.HTTP_409_CONFLICT) - - -@router.get('/{test_id}') -async def test_read(test=Depends(get_email_bot)): - return test """ +# delete Email row + + +@router.post('/recipient/') +async def email_recipient_create(recipient: Recipient): + token = jwt.encode({ + 'email': recipient.email, + 'hood': recipient.hood, + }, Email.secret).decode('ascii') + confirm_link = url("/api/email/recipient/confirm/" + token) + hood_name = await get_hood(recipient.hood) + send_email(recipient.email, + "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/') +async def email_recipient_confirm(token): + json = jwt.decode(token, Email.secret) + try: + await EmailRecipients.objects.create(hood=json['hood'], email=json['email']) + return status.HTTP_201_CREATED + except IntegrityError: + raise HTTPException(status_code=status.HTTP_409_CONFLICT) + +# delete EmailRecipient + @router.post('/messages/') @@ -58,9 +91,9 @@ async def email_message_create(message: BodyMessage): email_bot = await get_email_bot(message.to) # check API secret if message.secret is not email_bot.secret: - return status.HTTP_401_UNAUTHORIZED + raise HTTPException(status_code=status.HTTP_401_UNAUTHORIZED) # pass message.text to bot.py if await spawner.get(email_bot).publish(Message(message.text)): return status.HTTP_201_CREATED else: - return status.HTTP_451_UNAVAILABLE_FOR_LEGAL_REASONS + raise HTTPException(status_code=status.HTTP_451_UNAVAILABLE_FOR_LEGAL_REASONS) diff --git a/setup.py b/setup.py index e2cd0e1..057aca2 100644 --- a/setup.py +++ b/setup.py @@ -28,5 +28,6 @@ setup( 'pytoml', 'requests', 'scrypt', + 'pyjwt', ], )