featured-id-patch
All checks were successful
Deploy to core / deploy (push) Successful in 1m40s

This commit is contained in:
2024-02-02 15:03:44 +03:00
parent bd5f910f8c
commit c00361b2ec
19 changed files with 640 additions and 798 deletions

46
services/diff.py Normal file
View File

@@ -0,0 +1,46 @@
import re
from difflib import ndiff
def get_diff(original, modified):
"""
Get the difference between two strings using difflib.
Parameters:
- original: The original string.
- modified: The modified string.
Returns:
A list of differences.
"""
diff = list(ndiff(original.split(), modified.split()))
return diff
def apply_diff(original, diff):
"""
Apply the difference to the original string.
Parameters:
- original: The original string.
- diff: The difference obtained from get_diff function.
Returns:
The modified string.
"""
result = []
pattern = re.compile(r'^(\+|-) ')
for line in diff:
match = pattern.match(line)
if match:
op = match.group(1)
content = line[2:]
if op == '+':
result.append(content)
elif op == '-':
# Ignore deleted lines
pass
else:
result.append(line)
return ' '.join(result)

36
services/webhook.py Normal file
View File

@@ -0,0 +1,36 @@
import os
import re
from starlette.endpoints import HTTPEndpoint
from starlette.requests import Request
from starlette.responses import JSONResponse
from orm.author import Author
from resolvers.author import create_author
from services.db import local_session
class WebhookEndpoint(HTTPEndpoint):
async def post(self, request: Request) -> JSONResponse:
try:
data = await request.json()
if data:
auth = request.headers.get('Authorization')
if auth:
if auth == os.environ.get('WEBHOOK_SECRET'):
user_id: str = data['user']['id']
name: str = data['user']['given_name']
slug: str = data['user']['email'].split('@')[0]
slug: str = re.sub('[^0-9a-z]+', '-', slug.lower())
with local_session() as session:
author = session.query(Author).filter(Author.slug == slug).first()
if author:
slug = slug + '-' + user_id.split('-').pop()
await create_author(user_id, slug, name)
return JSONResponse({'status': 'success'})
except Exception as e:
import traceback
traceback.print_exc()
return JSONResponse({'status': 'error', 'message': str(e)}, status_code=500)