webhook-fix
All checks were successful
Deploy to core / deploy (push) Successful in 1m28s

This commit is contained in:
Untone 2024-02-22 21:18:20 +03:00
parent ce736e2624
commit ebf342c73b

View File

@ -8,6 +8,7 @@ from starlette.responses import JSONResponse
from orm.author import Author from orm.author import Author
from resolvers.author import create_author from resolvers.author import create_author
from services.db import local_session from services.db import local_session
from services.logger import root_logger as logger
class WebhookEndpoint(HTTPEndpoint): class WebhookEndpoint(HTTPEndpoint):
@ -18,19 +19,22 @@ class WebhookEndpoint(HTTPEndpoint):
auth = request.headers.get('Authorization') auth = request.headers.get('Authorization')
if auth: if auth:
if auth == os.environ.get('WEBHOOK_SECRET'): if auth == os.environ.get('WEBHOOK_SECRET'):
user_id: str = data['user']['id'] logger.debug(data)
name: str = data['user']['given_name'] user = data.get('user')
slug: str = data['user']['email'].split('@')[0] if isinstance(user, dict):
slug: str = re.sub('[^0-9a-z]+', '-', slug.lower()) user_id: str = user.get('id')
with local_session() as session: name: str = user.get('given_name', user.get('slug'))
author = ( slug: str = user.get('email', '').split('@')[0]
session.query(Author) slug: str = re.sub('[^0-9a-z]+', '-', slug.lower())
.filter(Author.slug == slug) with local_session() as session:
.first() author = (
) session.query(Author)
if author: .filter(Author.slug == slug)
slug = slug + '-' + user_id.split('-').pop() .first()
await create_author(user_id, slug, name) )
if author:
slug = slug + '-' + user_id.split('-').pop()
await create_author(user_id, slug, name)
return JSONResponse({'status': 'success'}) return JSONResponse({'status': 'success'})
except Exception as e: except Exception as e: