[email] Confirmation mail for message subscriptions

This commit is contained in:
maike 2020-07-06 16:33:07 +02:00 committed by dl6tom
parent 78b271c418
commit 71f80a2a96
2 changed files with 44 additions and 10 deletions

View file

@ -2,14 +2,17 @@
# #
# SPDX-License-Identifier: 0BSD # 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.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.platformapi import Message
from kibicara.webapi.hoods import get_hood 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 ormantic.exceptions import NoMatch
from pydantic import BaseModel from pydantic import BaseModel
from sqlite3 import IntegrityError from sqlite3 import IntegrityError
import jwt
class BodyMessage(BaseModel): class BodyMessage(BaseModel):
@ -19,6 +22,11 @@ class BodyMessage(BaseModel):
secret: str secret: str
class Recipient(BaseModel):
hood: int
email: str
async def get_email_bot(to, hood=Depends(get_hood)): async def get_email_bot(to, hood=Depends(get_hood)):
try: try:
return await Email.objects.get(hood=to) return await Email.objects.get(hood=to)
@ -30,11 +38,12 @@ router = APIRouter()
""" """
# get Email row?
@router.get('/') @router.get('/')
async def test_read_all(hood=Depends(get_hood)): async def test_read_all(hood=Depends(get_hood)):
return await Email.objects.filter(hood=hood).all() return await Email.objects.filter(hood=hood).all()
# create Email row
@router.post('/', status_code=status.HTTP_201_CREATED) @router.post('/', status_code=status.HTTP_201_CREATED)
async def test_create(response: Response, hood=Depends(get_hood)): async def test_create(response: Response, hood=Depends(get_hood)):
try: try:
@ -44,12 +53,36 @@ async def test_create(response: Response, hood=Depends(get_hood)):
return test return test
except IntegrityError: except IntegrityError:
raise HTTPException(status_code=status.HTTP_409_CONFLICT) 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/<token>')
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/') @router.post('/messages/')
@ -58,9 +91,9 @@ async def email_message_create(message: BodyMessage):
email_bot = await get_email_bot(message.to) email_bot = await get_email_bot(message.to)
# check API secret # check API secret
if message.secret is not email_bot.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 # pass message.text to bot.py
if await spawner.get(email_bot).publish(Message(message.text)): if await spawner.get(email_bot).publish(Message(message.text)):
return status.HTTP_201_CREATED return status.HTTP_201_CREATED
else: else:
return status.HTTP_451_UNAVAILABLE_FOR_LEGAL_REASONS raise HTTPException(status_code=status.HTTP_451_UNAVAILABLE_FOR_LEGAL_REASONS)

View file

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