2020-07-05 21:35:56 +00:00
|
|
|
# Copyright (C) 2020 by Maike <maike@systemli.org>
|
|
|
|
#
|
|
|
|
# SPDX-License-Identifier: 0BSD
|
|
|
|
|
2020-07-06 14:33:07 +00:00
|
|
|
from fastapi import APIRouter, Depends, HTTPException, status
|
2020-07-05 21:35:56 +00:00
|
|
|
from kibicara.platforms.email.bot import spawner
|
2020-07-06 14:33:07 +00:00
|
|
|
from kibicara.platforms.email.model import Email, EmailRecipients
|
2020-07-05 21:35:56 +00:00
|
|
|
from kibicara.platformapi import Message
|
|
|
|
from kibicara.webapi.hoods import get_hood
|
2020-07-06 14:33:07 +00:00
|
|
|
from kibicara.webapi.admin import url
|
|
|
|
from kibicara.email import send_email
|
2020-07-05 21:35:56 +00:00
|
|
|
from ormantic.exceptions import NoMatch
|
|
|
|
from pydantic import BaseModel
|
|
|
|
from sqlite3 import IntegrityError
|
2020-07-06 14:33:07 +00:00
|
|
|
import jwt
|
2020-07-05 21:35:56 +00:00
|
|
|
|
|
|
|
|
|
|
|
class BodyMessage(BaseModel):
|
|
|
|
text: str
|
|
|
|
to: str
|
|
|
|
author: str
|
|
|
|
secret: str
|
|
|
|
|
|
|
|
|
2020-07-06 14:33:07 +00:00
|
|
|
class Recipient(BaseModel):
|
|
|
|
hood: int
|
|
|
|
email: str
|
|
|
|
|
|
|
|
|
2020-07-05 21:35:56 +00:00
|
|
|
async def get_email_bot(to, hood=Depends(get_hood)):
|
|
|
|
try:
|
|
|
|
return await Email.objects.get(hood=to)
|
|
|
|
except NoMatch:
|
|
|
|
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND)
|
|
|
|
|
|
|
|
|
|
|
|
router = APIRouter()
|
|
|
|
|
|
|
|
|
|
|
|
"""
|
2020-07-06 14:33:07 +00:00
|
|
|
# get Email row?
|
2020-07-05 21:35:56 +00:00
|
|
|
@router.get('/')
|
|
|
|
async def test_read_all(hood=Depends(get_hood)):
|
|
|
|
return await Email.objects.filter(hood=hood).all()
|
|
|
|
|
2020-07-06 14:33:07 +00:00
|
|
|
# create Email row
|
2020-07-05 21:35:56 +00:00
|
|
|
@router.post('/', status_code=status.HTTP_201_CREATED)
|
|
|
|
async def test_create(response: Response, hood=Depends(get_hood)):
|
|
|
|
try:
|
|
|
|
test = await Email.objects.create(hood=hood)
|
|
|
|
spawner.start(test)
|
|
|
|
response.headers['Location'] = '%d' % test.id
|
|
|
|
return test
|
|
|
|
except IntegrityError:
|
|
|
|
raise HTTPException(status_code=status.HTTP_409_CONFLICT)
|
2020-07-06 14:33:07 +00:00
|
|
|
"""
|
|
|
|
# delete Email row
|
2020-07-05 21:35:56 +00:00
|
|
|
|
|
|
|
|
2020-07-06 14:33:07 +00:00
|
|
|
@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
|
|
|
|
|
2020-07-05 21:35:56 +00:00
|
|
|
|
|
|
|
|
2020-07-06 13:14:10 +00:00
|
|
|
@router.post('/messages/')
|
2020-07-05 21:35:56 +00:00
|
|
|
async def email_message_create(message: BodyMessage):
|
|
|
|
# get bot via "To:" header
|
|
|
|
email_bot = await get_email_bot(message.to)
|
|
|
|
# check API secret
|
|
|
|
if message.secret is not email_bot.secret:
|
2020-07-06 14:33:07 +00:00
|
|
|
raise HTTPException(status_code=status.HTTP_401_UNAUTHORIZED)
|
2020-07-05 21:35:56 +00:00
|
|
|
# pass message.text to bot.py
|
2020-07-06 13:14:10 +00:00
|
|
|
if await spawner.get(email_bot).publish(Message(message.text)):
|
|
|
|
return status.HTTP_201_CREATED
|
|
|
|
else:
|
2020-07-06 14:33:07 +00:00
|
|
|
raise HTTPException(status_code=status.HTTP_451_UNAVAILABLE_FOR_LEGAL_REASONS)
|