This commit is contained in:
2024-04-17 18:32:23 +03:00
parent 937b154c6b
commit c25d7e3ab6
39 changed files with 986 additions and 926 deletions

View File

@@ -15,50 +15,50 @@ class WebhookEndpoint(HTTPEndpoint):
try:
data = await request.json()
if not data:
raise HTTPException(status_code=400, detail='Request body is empty')
auth = request.headers.get('Authorization')
if not auth or auth != os.environ.get('WEBHOOK_SECRET'):
raise HTTPException(status_code=400, detail="Request body is empty")
auth = request.headers.get("Authorization")
if not auth or auth != os.environ.get("WEBHOOK_SECRET"):
raise HTTPException(
status_code=401, detail='Invalid Authorization header'
status_code=401, detail="Invalid Authorization header"
)
# logger.debug(data)
user = data.get('user')
user = data.get("user")
if not isinstance(user, dict):
raise HTTPException(
status_code=400, detail='User data is not a dictionary'
status_code=400, detail="User data is not a dictionary"
)
user_id: str = user.get('id', '')
user_id: str = user.get("id", "")
name: str = (
f"{user.get('given_name', user.get('slug'))} {user.get('middle_name', '')}"
+ f"{user.get('family_name', '')}".strip()
) or 'Аноним'
email: str = user.get('email', '')
pic: str = user.get('picture', '')
) or "Аноним"
email: str = user.get("email", "")
pic: str = user.get("picture", "")
with local_session() as session:
author = session.query(Author).filter(Author.user == user_id).first()
if not author:
# If the author does not exist, create a new one
slug: str = email.split('@')[0].replace('.', '-').lower()
slug: str = re.sub('[^0-9a-z]+', '-', slug)
slug: str = email.split("@")[0].replace(".", "-").lower()
slug: str = re.sub("[^0-9a-z]+", "-", slug)
while True:
author = (
session.query(Author).filter(Author.slug == slug).first()
)
if not author:
break
slug = f'{slug}-{len(session.query(Author).filter(Author.email == email).all()) + 1}'
slug = f"{slug}-{len(session.query(Author).filter(Author.email == email).all()) + 1}"
author = Author(user=user_id, slug=slug, name=name, pic=pic)
session.add(author)
session.commit()
return JSONResponse({'status': 'success'})
return JSONResponse({"status": "success"})
except HTTPException as e:
return JSONResponse(
{'status': 'error', 'message': str(e.detail)}, status_code=e.status_code
{"status": "error", "message": str(e.detail)}, status_code=e.status_code
)
except Exception as e:
import traceback
traceback.print_exc()
return JSONResponse({'status': 'error', 'message': str(e)}, status_code=500)
return JSONResponse({"status": "error", "message": str(e)}, status_code=500)