[core] Fix hood destruction process
This commit is contained in:
parent
a24e5ff4f9
commit
7d54375b43
|
@ -117,6 +117,14 @@ class Censor:
|
|||
"""
|
||||
pass
|
||||
|
||||
@classmethod
|
||||
async def destroy_hood(cls, hood):
|
||||
"""Removes all its database entries.
|
||||
|
||||
Note: Override this in the derived bot class.
|
||||
"""
|
||||
pass
|
||||
|
||||
async def publish(self, message):
|
||||
"""Distribute a message to the bots in a hood.
|
||||
|
||||
|
@ -187,6 +195,16 @@ class Spawner:
|
|||
for spawner in cls.__instances:
|
||||
await spawner._init()
|
||||
|
||||
@classmethod
|
||||
async def destroy_hood(cls, hood):
|
||||
for spawner in cls.__instances:
|
||||
for pk in list(spawner.__bots):
|
||||
bot = spawner.__bots[pk]
|
||||
if bot.hood.id == hood.id:
|
||||
del spawner.__bots[pk]
|
||||
bot.stop()
|
||||
await spawner.BotClass.destroy_hood(hood)
|
||||
|
||||
async def _init(self):
|
||||
for item in await self.ORMClass.objects.all():
|
||||
self.start(item)
|
||||
|
|
|
@ -8,7 +8,7 @@ from kibicara import email
|
|||
from kibicara.config import config
|
||||
from kibicara.model import Hood
|
||||
from kibicara.platformapi import Censor, Spawner
|
||||
from kibicara.platforms.email.model import EmailSubscribers
|
||||
from kibicara.platforms.email.model import Email, EmailSubscribers
|
||||
from kibicara.webapi.admin import to_token
|
||||
from logging import getLogger
|
||||
from smtplib import SMTPException
|
||||
|
@ -22,6 +22,14 @@ class EmailBot(Censor):
|
|||
super().__init__(hood)
|
||||
self.enabled = hood.email_enabled
|
||||
|
||||
@classmethod
|
||||
async def destroy_hood(cls, hood):
|
||||
"""Removes all its database entries."""
|
||||
for inbox in await Email.objects.filter(hood=hood).all():
|
||||
await inbox.delete()
|
||||
for subscriber in await EmailSubscribers.objects.filter(hood=hood).all():
|
||||
await subscriber.delete()
|
||||
|
||||
async def run(self):
|
||||
""" Loop which waits for new messages and sends emails to all subscribers. """
|
||||
while True:
|
||||
|
|
|
@ -92,7 +92,7 @@ async def email_read_all_public(hood=Depends(get_hood_unauthorized)):
|
|||
operation_id='get_emails',
|
||||
)
|
||||
async def email_read_all(hood=Depends(get_hood)):
|
||||
return await Email.objects.filter(hood=hood).all()
|
||||
return await Email.objects.filter(hood=hood).select_related('hood').all()
|
||||
|
||||
|
||||
@router.post(
|
||||
|
|
|
@ -20,6 +20,14 @@ class TelegramBot(Censor):
|
|||
self.telegram_model = telegram_model
|
||||
self.enabled = self.telegram_model.enabled
|
||||
|
||||
@classmethod
|
||||
async def destroy_hood(cls, hood):
|
||||
"""Removes all its database entries."""
|
||||
for telegram in await Telegram.objects.filter(hood=hood).all():
|
||||
for user in await TelegramUser.objects.filter(bot=telegram).all():
|
||||
await user.delete()
|
||||
await telegram.delete()
|
||||
|
||||
def _create_dispatcher(self):
|
||||
dp = Dispatcher(self.bot)
|
||||
dp.register_message_handler(self._send_welcome, commands=['start'])
|
||||
|
|
|
@ -6,7 +6,7 @@ from aiogram.bot.api import check_token
|
|||
from aiogram import exceptions
|
||||
from fastapi import APIRouter, Depends, HTTPException, Response, status
|
||||
from kibicara.platforms.telegram.bot import spawner
|
||||
from kibicara.platforms.telegram.model import Telegram
|
||||
from kibicara.platforms.telegram.model import Telegram, TelegramUser
|
||||
from kibicara.webapi.hoods import get_hood, get_hood_unauthorized
|
||||
from logging import getLogger
|
||||
from sqlite3 import IntegrityError
|
||||
|
@ -84,6 +84,8 @@ async def telegram_read(telegram=Depends(get_telegram)):
|
|||
)
|
||||
async def telegram_delete(telegram=Depends(get_telegram)):
|
||||
spawner.stop(telegram)
|
||||
for user in await TelegramUser.objects.filter(bot=telegram).all():
|
||||
await user.delete()
|
||||
await telegram.delete()
|
||||
|
||||
|
||||
|
|
|
@ -22,6 +22,12 @@ class TwitterBot(Censor):
|
|||
self.mentions_since_id = self.twitter_model.mentions_since_id
|
||||
self.dms_since_id = self.twitter_model.dms_since_id
|
||||
|
||||
@classmethod
|
||||
async def destroy_hood(cls, hood):
|
||||
"""Removes all its database entries."""
|
||||
for twitter in await Twitter.objects.filter(hood=hood).all():
|
||||
await twitter.delete()
|
||||
|
||||
async def run(self):
|
||||
try:
|
||||
if not self.twitter_model.verified:
|
||||
|
|
|
@ -6,7 +6,8 @@
|
|||
""" REST API Endpoints for managing hoods. """
|
||||
|
||||
from fastapi import APIRouter, Depends, HTTPException, Response, status
|
||||
from kibicara.model import AdminHoodRelation, Hood
|
||||
from kibicara.model import AdminHoodRelation, BadWord, Hood, Trigger
|
||||
from kibicara.platformapi import Spawner
|
||||
from kibicara.platforms.email.bot import spawner
|
||||
from kibicara.webapi.admin import get_admin
|
||||
from ormantic.exceptions import NoMatch
|
||||
|
@ -111,6 +112,11 @@ async def hood_update(values: BodyHood, hood=Depends(get_hood)):
|
|||
)
|
||||
async def hood_delete(hood=Depends(get_hood)):
|
||||
""" Deletes hood with id **hood_id**. """
|
||||
await Spawner.destroy_hood(hood)
|
||||
for trigger in await Trigger.objects.filter(hood=hood).all():
|
||||
await trigger.delete()
|
||||
for badword in await BadWord.objects.filter(hood=hood).all():
|
||||
await badword.delete()
|
||||
for relation in await AdminHoodRelation.objects.filter(hood=hood).all():
|
||||
await relation.delete()
|
||||
await hood.delete()
|
||||
|
|
Loading…
Reference in a new issue