From 268e1d68534620da49f44080d94e8375a569bba5 Mon Sep 17 00:00:00 2001 From: Cathy Hu Date: Sat, 18 Jul 2020 13:53:11 +0200 Subject: [PATCH] [email] Add start, stop, status REST endpoints --- kibicara/model.py | 3 ++- kibicara/platforms/email/bot.py | 2 +- kibicara/platforms/email/webapi.py | 38 ++++++++++++++++++++---------- kibicara/webapi/hoods/__init__.py | 2 ++ 4 files changed, 31 insertions(+), 14 deletions(-) diff --git a/kibicara/model.py b/kibicara/model.py index 103a803..5876a4b 100644 --- a/kibicara/model.py +++ b/kibicara/model.py @@ -7,7 +7,7 @@ from databases import Database from kibicara.config import config -from ormantic import Integer, ForeignKey, Model, Text +from ormantic import Boolean, Integer, ForeignKey, Model, Text from sqlalchemy import create_engine, MetaData @@ -39,6 +39,7 @@ class Hood(Model): id: Integer(primary_key=True) = None name: Text(unique=True) landingpage: Text() + email_enabled: Boolean() = True class Mapping(Mapping): table_name = 'hoods' diff --git a/kibicara/platforms/email/bot.py b/kibicara/platforms/email/bot.py index a0aa306..30f76b4 100644 --- a/kibicara/platforms/email/bot.py +++ b/kibicara/platforms/email/bot.py @@ -20,6 +20,7 @@ logger = getLogger(__name__) class EmailBot(Censor): def __init__(self, hood): super().__init__(hood) + self.enabled = hood.email_enabled async def run(self): """ Loop which waits for new messages and sends emails to all subscribers. """ @@ -28,7 +29,6 @@ class EmailBot(Censor): logger.debug( 'Received message from censor (%s): %s' % (self.hood.name, message.text) ) - logger.debug('a') for subscriber in await EmailSubscribers.objects.filter( hood=self.hood ).all(): diff --git a/kibicara/platforms/email/webapi.py b/kibicara/platforms/email/webapi.py index 0fd1c04..25bacaa 100644 --- a/kibicara/platforms/email/webapi.py +++ b/kibicara/platforms/email/webapi.py @@ -15,7 +15,7 @@ from kibicara.webapi.hoods import get_hood, get_hood_unauthorized from logging import getLogger from ormantic.exceptions import NoMatch from os import urandom -from pydantic import BaseModel +from pydantic import BaseModel, validator from smtplib import SMTPException from sqlite3 import IntegrityError @@ -26,6 +26,12 @@ logger = getLogger(__name__) class BodyEmail(BaseModel): name: str + @validator('name') + def valid_prefix(cls, value): + if not value.startswith('kibicara-'): + raise ValueError('Recipient address didn\'t start with kibicara-') + return value + class BodyMessage(BaseModel): """ This model shows which values are supplied by the MDA listener script. """ @@ -76,32 +82,40 @@ async def email_create(values: BodyEmail, response: Response, hood=Depends(get_h :param hood: Hood row of the hood the Email bot is supposed to belong to. :return: Email row of the new email bot. """ - if not values.name.startswith('kibicara-'): - raise HTTPException( - status_code=status.HTTP_400_BAD_REQUEST, - detail='Recipient address didn\'t start with kibicara-', - ) try: email = await Email.objects.create( hood=hood, secret=urandom(32).hex(), **values.__dict__ ) - spawner.start(email) response.headers['Location'] = '%d' % hood.id return email except IntegrityError: raise HTTPException(status_code=status.HTTP_409_CONFLICT) +@router.get('/status', status_code=status.HTTP_200_OK) +async def email_status(hood=Depends(get_hood)): + return {'status': spawner.get(hood).status.name} + + +@router.post('/start', status_code=status.HTTP_200_OK) +async def email_start(hood=Depends(get_hood)): + await hood.update(email_enabled=True) + spawner.get(hood).start() + return {} + + +@router.post('/stop', status_code=status.HTTP_200_OK) +async def email_stop(hood=Depends(get_hood)): + await hood.update(email_enabled=False) + spawner.get(hood).stop() + return {} + + @router.get('/{email_id}') async def email_read(email=Depends(get_email)): return email -@router.put('/{email_id}', status_code=status.HTTP_204_NO_CONTENT) -async def email_update(email=Depends(get_email)): - await email.update() # TODO - - @router.delete('/{email_id}', status_code=status.HTTP_204_NO_CONTENT) async def email_delete(email=Depends(get_email)): """ Delete an Email bot. diff --git a/kibicara/webapi/hoods/__init__.py b/kibicara/webapi/hoods/__init__.py index 815c088..a5e2059 100644 --- a/kibicara/webapi/hoods/__init__.py +++ b/kibicara/webapi/hoods/__init__.py @@ -7,6 +7,7 @@ from fastapi import APIRouter, Depends, HTTPException, Response, status from kibicara.model import AdminHoodRelation, Hood +from kibicara.platforms.email.bot import spawner from kibicara.webapi.admin import get_admin from ormantic.exceptions import NoMatch from pydantic import BaseModel @@ -58,6 +59,7 @@ async def hood_create(values: BodyHood, response: Response, admin=Depends(get_ad try: hood = await Hood.objects.create(**values.__dict__) await AdminHoodRelation.objects.create(admin=admin.id, hood=hood.id) + spawner.start(hood) response.headers['Location'] = '%d' % hood.id return hood except IntegrityError: