2023-12-16 15:24:30 +00:00
|
|
|
import os
|
|
|
|
import re
|
|
|
|
|
|
|
|
from starlette.endpoints import HTTPEndpoint
|
2024-04-08 07:38:58 +00:00
|
|
|
from starlette.exceptions import HTTPException
|
2023-12-16 15:24:30 +00:00
|
|
|
from starlette.requests import Request
|
|
|
|
from starlette.responses import JSONResponse
|
|
|
|
|
2024-05-18 10:57:30 +00:00
|
|
|
from sqlalchemy import select
|
|
|
|
|
2023-12-16 15:24:30 +00:00
|
|
|
from orm.author import Author
|
|
|
|
from services.db import local_session
|
2024-05-18 10:57:30 +00:00
|
|
|
from services.cache import cache_author
|
|
|
|
from resolvers.stat import get_with_stat
|
2023-12-16 15:24:30 +00:00
|
|
|
|
|
|
|
|
|
|
|
class WebhookEndpoint(HTTPEndpoint):
|
|
|
|
async def post(self, request: Request) -> JSONResponse:
|
|
|
|
try:
|
|
|
|
data = await request.json()
|
2024-03-03 13:59:15 +00:00
|
|
|
if not data:
|
2024-04-17 15:32:23 +00:00
|
|
|
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"):
|
2024-03-06 09:25:55 +00:00
|
|
|
raise HTTPException(
|
2024-04-17 15:32:23 +00:00
|
|
|
status_code=401, detail="Invalid Authorization header"
|
2024-03-06 09:25:55 +00:00
|
|
|
)
|
2024-03-04 18:08:01 +00:00
|
|
|
# logger.debug(data)
|
2024-04-17 15:32:23 +00:00
|
|
|
user = data.get("user")
|
2024-03-03 13:59:15 +00:00
|
|
|
if not isinstance(user, dict):
|
2024-03-06 09:25:55 +00:00
|
|
|
raise HTTPException(
|
2024-04-17 15:32:23 +00:00
|
|
|
status_code=400, detail="User data is not a dictionary"
|
2024-03-06 09:25:55 +00:00
|
|
|
)
|
2024-04-17 16:20:35 +00:00
|
|
|
#
|
2024-03-06 10:43:30 +00:00
|
|
|
name: str = (
|
|
|
|
f"{user.get('given_name', user.get('slug'))} {user.get('middle_name', '')}"
|
2024-04-17 15:31:11 +00:00
|
|
|
+ f"{user.get('family_name', '')}".strip()
|
2024-04-17 15:32:23 +00:00
|
|
|
) or "Аноним"
|
2024-04-17 16:54:38 +00:00
|
|
|
user_id: str = user.get("id", "")
|
2024-04-17 15:32:23 +00:00
|
|
|
email: str = user.get("email", "")
|
|
|
|
pic: str = user.get("picture", "")
|
2024-04-17 16:54:38 +00:00
|
|
|
if user_id:
|
|
|
|
with local_session() as session:
|
2024-04-17 17:30:05 +00:00
|
|
|
author = (
|
|
|
|
session.query(Author).filter(Author.user == user_id).first()
|
|
|
|
)
|
2024-04-17 16:54:38 +00:00
|
|
|
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)
|
|
|
|
while True:
|
|
|
|
author = (
|
2024-04-17 17:30:05 +00:00
|
|
|
session.query(Author)
|
|
|
|
.filter(Author.slug == slug)
|
|
|
|
.first()
|
2024-04-17 16:54:38 +00:00
|
|
|
)
|
|
|
|
if not author:
|
|
|
|
break
|
|
|
|
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()
|
2024-05-18 11:15:05 +00:00
|
|
|
author_query = select(Author).filter(Author.user == user_id)
|
|
|
|
[author_with_stat] = get_with_stat(author_query)
|
2024-05-18 10:57:30 +00:00
|
|
|
if author_with_stat:
|
|
|
|
await cache_author(author_with_stat)
|
2023-12-16 15:24:30 +00:00
|
|
|
|
2024-04-17 15:32:23 +00:00
|
|
|
return JSONResponse({"status": "success"})
|
2024-03-03 13:59:15 +00:00
|
|
|
except HTTPException as e:
|
2024-03-06 09:25:55 +00:00
|
|
|
return JSONResponse(
|
2024-04-17 15:32:23 +00:00
|
|
|
{"status": "error", "message": str(e.detail)}, status_code=e.status_code
|
2024-03-06 09:25:55 +00:00
|
|
|
)
|
2023-12-16 15:24:30 +00:00
|
|
|
except Exception as e:
|
|
|
|
import traceback
|
|
|
|
|
|
|
|
traceback.print_exc()
|
2024-04-17 15:32:23 +00:00
|
|
|
return JSONResponse({"status": "error", "message": str(e)}, status_code=500)
|