diff --git a/.vscode/settings.json b/.vscode/settings.json index 501360b..b3d6042 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -15,6 +15,7 @@ "rundgelutschte", "Törnplanung", "Trintella", + "uuidv", "vergenusswurzeln" ], "python.testing.pytestArgs": [ diff --git a/backend/src/backend/main.py b/backend/src/backend/main.py index bf81075..af9028b 100644 --- a/backend/src/backend/main.py +++ b/backend/src/backend/main.py @@ -1,7 +1,7 @@ from dotenv.main import load_dotenv load_dotenv() -from .routers import users, comments, newsletters, health +from .routers import users, comments, newsletters, health, reactions from backend.internal.database import create_db_and_tables from fastapi import FastAPI from fastapi_crons import Crons @@ -22,5 +22,6 @@ def on_startup(): app.include_router(users.router) app.include_router(comments.router) +app.include_router(reactions.router) app.include_router(newsletters.router) app.include_router(health.router) \ No newline at end of file diff --git a/backend/src/backend/models/reaction.py b/backend/src/backend/models/reaction.py index 3ca312e..f4485bb 100644 --- a/backend/src/backend/models/reaction.py +++ b/backend/src/backend/models/reaction.py @@ -3,3 +3,17 @@ #Reference: https://isaacfei.com/posts/simple-like-system/ +from sqlmodel import UUID +import uuid +from sqlmodel import table +from dataclasses import dataclass +from sqlmodel import Field +from sqlmodel import SQLModel + +class BaseReaction(SQLModel): + pass + +@dataclass +class Reaction(BaseReaction, table=True): + slug: str = Field(primary_key=True) + browser_id: str = Field(primary_key=True) \ No newline at end of file diff --git a/backend/src/backend/routers/health.py b/backend/src/backend/routers/health.py index fcfe611..117d77a 100644 --- a/backend/src/backend/routers/health.py +++ b/backend/src/backend/routers/health.py @@ -1,6 +1,6 @@ from fastapi import APIRouter -router = APIRouter() +router = APIRouter(tags=["health"]) @router.get("/health", include_in_schema=False) diff --git a/backend/src/backend/routers/newsletters.py b/backend/src/backend/routers/newsletters.py index b9dc80d..1857911 100644 --- a/backend/src/backend/routers/newsletters.py +++ b/backend/src/backend/routers/newsletters.py @@ -22,7 +22,8 @@ router = APIRouter( ) @router.post( - path="/approve", + path="/approve", + tags=["newsletter"], status_code=201 ) def approve_newsletter(secret: str = Form(), session: Session = Depends(get_session)): diff --git a/backend/src/backend/routers/reactions.py b/backend/src/backend/routers/reactions.py index 5037216..07b88d9 100644 --- a/backend/src/backend/routers/reactions.py +++ b/backend/src/backend/routers/reactions.py @@ -1,5 +1,50 @@ +from os import stat +from sqlmodel import select +from starlette.status import HTTP_400_BAD_REQUEST +from fastapi import HTTPException +from backend.models.reaction import Reaction +from backend.internal.database import get_session +from fastapi import Depends +from sqlmodel import Session +from fastapi import APIRouter +router = APIRouter( + prefix="/reactions", + tags=["reactions"] +) + +@router.post("", status_code=201) +def create_reaction(reaction: Reaction, session: Session = Depends(get_session)): + print(reaction) + try: + session.add(reaction) + session.commit() + except Exception as e: + print(e) + session.rollback() + raise HTTPException(HTTP_400_BAD_REQUEST) -# addReaction -# delete Reaction -# get Reactions by slug \ No newline at end of file +@router.get("/{slug}", status_code=200) +def get_reaction_count_for_post(slug: str, session: Session = Depends(get_session))-> int: + reactions = session.exec(select(Reaction).where(Reaction.slug == slug)).all() + print(reactions) + return len(reactions) + +@router.get("/", status_code=200) +def have_i_reacted(slug:str, browser_id: str, session: Session = Depends(get_session)) -> bool: + result = session.exec(select(Reaction).where(Reaction.slug == slug, Reaction.browser_id == browser_id)).one_or_none() + if (result): return True + else: return False + +@router.delete("", status_code=200) +def delete_reaction(reaction: Reaction, session: Session = Depends(get_session)) : + db_reaction = session.exec(select(Reaction).where(Reaction.slug == reaction.slug, Reaction.browser_id == reaction.browser_id)).one_or_none() + try: + session.delete(db_reaction) + session.commit() + except Exception as e: + print(e) + + session.rollback() + raise HTTPException(HTTP_400_BAD_REQUEST) + diff --git a/backend/src/backend/routers/users.py b/backend/src/backend/routers/users.py index 404bdfe..fa18749 100644 --- a/backend/src/backend/routers/users.py +++ b/backend/src/backend/routers/users.py @@ -26,7 +26,7 @@ from fastapi_limiter.depends import RateLimiter router = APIRouter( prefix="/users", - dependencies=[], + tags=["users"], responses={404: {"description": "Not found"}}, ) @@ -73,7 +73,7 @@ def register_user(new_user: NewUser, session: Session = Depends(get_session)): @router.post( path="/login", - status_code=201, + status_code=200, dependencies=[Depends(RateLimiter(limiter=Limiter(Rate(3, Duration.SECOND * 30))))] ) def login(user_code: str = Form(), session: Session = Depends(get_session)) -> str: @@ -94,18 +94,18 @@ def forgot_user_code(email: str = Form(), session: Session = Depends(get_session pass # TODO: send Mail -@router.get(path="/me", status_code=201) +@router.get(path="/me", status_code=200) def get_current_user_info(current_user: User = Depends(get_current_user)) -> UserInfo: return UserInfo.model_validate(current_user) -@router.delete(path="/me", status_code=201) +@router.delete(path="/me", status_code=200) def delete(current_user: User = Depends(get_current_user), session: Session = Depends(get_session)) : session.delete(current_user) session.commit() -@router.put(path="/me/subscription", status_code=201) +@router.put(path="/me/subscription", status_code=200) def change_subscription( subscription: bool = Form(), current_user: User = Depends(get_current_user), diff --git a/blog/package-lock.json b/blog/package-lock.json index e8e56e3..9ce7f11 100644 --- a/blog/package-lock.json +++ b/blog/package-lock.json @@ -10,9 +10,11 @@ "@astrojs/mdx": "^7.0.8", "@astrojs/preact": "^6.0.5", "@astrojs/rss": "^4.0.19", + "@types/uuid": "^10.0.0", "astro": "^7.2.2", "jwt-decode": "^4.0.0", - "preact": "^10.29.8" + "preact": "^10.29.8", + "uuid": "^14.0.2" }, "devDependencies": { "@types/jsonwebtoken": "^9.0.10" @@ -2219,6 +2221,12 @@ "integrity": "sha512-ko/gIFJRv177XgZsZcBwnqJN5x/Gien8qNOn0D5bQU/zAzVf9Zt3BlcUiLqhV9y4ARk0GbT3tnUiPNgnTXzc/Q==", "license": "MIT" }, + "node_modules/@types/uuid": { + "version": "10.0.0", + "resolved": "https://registry.npmjs.org/@types/uuid/-/uuid-10.0.0.tgz", + "integrity": "sha512-7gqG38EyHgyP1S+7+xomFtL+ZNHcKv6DwNaCZmJmo1vgMugyF3TCnXVg4t1uk89mLNwnLtnY3TpOpCOyp1/xHQ==", + "license": "MIT" + }, "node_modules/@ungap/structured-clone": { "version": "1.3.3", "resolved": "https://registry.npmjs.org/@ungap/structured-clone/-/structured-clone-1.3.3.tgz", @@ -6512,262 +6520,272 @@ "engines": { "node": ">=18.12.0" }, - "funding": { - "url": "https://github.com/sponsors/sxzz" - } - }, - "node_modules/vfile": { - "version": "6.0.3", - "resolved": "https://registry.npmjs.org/vfile/-/vfile-6.0.3.tgz", - "integrity": "sha512-KzIbH/9tXat2u30jf+smMwFCsno4wHVdNmzFyL+T/L3UGqqk6JKfVqOFOZEpZSHADH1k40ab6NUIXZq422ov3Q==", - "license": "MIT", - "dependencies": { - "@types/unist": "^3.0.0", - "vfile-message": "^4.0.0" - }, - "funding": { - "type": "opencollective", - "url": "https://opencollective.com/unified" - } - }, - "node_modules/vfile-location": { - "version": "5.0.3", - "resolved": "https://registry.npmjs.org/vfile-location/-/vfile-location-5.0.3.tgz", - "integrity": "sha512-5yXvWDEgqeiYiBe1lbxYF7UMAIm/IcopxMHrMQDq3nvKcjPKIhZklUKL+AE7J7uApI4kwe2snsK+eI6UTj9EHg==", - "license": "MIT", - "dependencies": { - "@types/unist": "^3.0.0", - "vfile": "^6.0.0" - }, - "funding": { - "type": "opencollective", - "url": "https://opencollective.com/unified" - } - }, - "node_modules/vfile-message": { - "version": "4.0.3", - "resolved": "https://registry.npmjs.org/vfile-message/-/vfile-message-4.0.3.tgz", - "integrity": "sha512-QTHzsGd1EhbZs4AsQ20JX1rC3cOlt/IWJruk893DfLRr57lcnOeMaWG4K0JrRta4mIJZKth2Au3mM3u03/JWKw==", - "license": "MIT", - "dependencies": { - "@types/unist": "^3.0.0", - "unist-util-stringify-position": "^4.0.0" - }, - "funding": { - "type": "opencollective", - "url": "https://opencollective.com/unified" - } - }, - "node_modules/vite": { - "version": "8.3.5", - "resolved": "https://registry.npmjs.org/vite/-/vite-8.3.5.tgz", - "integrity": "sha512-z1YpjieJ9g0ebTmQpLBv/oq9E3F9EIlOe+7LahW8SrE3LTiG3udBRUXQOlYXP9b+IC2q2phHeVxj9xGlw34OEQ==", - "license": "MIT", - "dependencies": { - "lightningcss": "^1.33.0", - "picomatch": "^4.0.7", - "postcss": "^8.5.30", - "rolldown": "~1.2.14", - "tinyglobby": "^0.2.17" - }, - "bin": { - "vite": "bin/vite.js" - }, - "engines": { - "node": "^20.19.0 || >=22.12.0" - }, - "funding": { - "url": "https://github.com/vitejs/vite?sponsor=1" - }, - "optionalDependencies": { - "fsevents": "~2.3.3" - }, - "peerDependencies": { - "@types/node": "^20.19.0 || >=22.12.0", - "@vitejs/devtools": "^0.7.1 || ^0.8.0", - "esbuild": "^0.27.0 || ^0.28.0", - "jiti": ">=1.21.0", - "less": "^4.0.0", - "sass": "^1.70.0", - "sass-embedded": "^1.70.0", - "stylus": ">=0.54.8", - "sugarss": "^5.0.0", - "terser": "^5.16.0", - "tsx": "^4.8.1", - "yaml": "^2.4.2" - }, - "peerDependenciesMeta": { - "@types/node": { - "optional": true - }, - "@vitejs/devtools": { - "optional": true - }, - "esbuild": { - "optional": true - }, - "jiti": { - "optional": true - }, - "less": { - "optional": true - }, - "sass": { - "optional": true - }, - "sass-embedded": { - "optional": true - }, - "stylus": { - "optional": true - }, - "sugarss": { - "optional": true - }, - "terser": { - "optional": true - }, - "tsx": { - "optional": true - }, - "yaml": { - "optional": true + "node_modules/uuid": { + "version": "14.0.2", + "resolved": "https://registry.npmjs.org/uuid/-/uuid-14.0.2.tgz", + "integrity": "sha512-xZe/16rV4aa+HGSOCiY2YeLT1OybRLrrkL/Rqaq7p7GMVXjFh+6wN4oMYgjFmnSnhY8t6Xpdl2l9qmnHYuMHwQ==", + "funding": [ + "https://github.com/sponsors/broofa", + "https://github.com/sponsors/ctavan" + ], + "license": "MIT", + "bin": { + "uuid": "dist-node/bin/uuid" } - } - }, - "node_modules/vite-prerender-plugin": { - "version": "0.5.13", - "resolved": "https://registry.npmjs.org/vite-prerender-plugin/-/vite-prerender-plugin-0.5.13.tgz", - "integrity": "sha512-IKSpYkzDBsKAxa05naRbj7GvNVMSdww/Z/E89oO3xndz+gWnOBOKOAbEXv7qDhktY/j3vHgJmoV1pPzqU2tx9g==", - "license": "MIT", - "dependencies": { - "kolorist": "^1.8.0", - "magic-string": "0.x >= 0.26.0", - "node-html-parser": "^6.1.12", - "simple-code-frame": "^1.3.0", - "source-map": "^0.7.4", - "stack-trace": "^1.0.0-pre2" }, - "peerDependencies": { - "vite": "5.x || 6.x || 7.x || 8.x" - } - }, - "node_modules/vitefu": { - "version": "1.1.3", - "resolved": "https://registry.npmjs.org/vitefu/-/vitefu-1.1.3.tgz", - "integrity": "sha512-ub4okH7Z5KLjb6hDyjqrGXqWtWvoYdU3IGm/NorpgHncKoLTCfRIbvlhBm7r0YstIaQRYlp4yEbFqDcKSzXSSg==", - "license": "MIT", - "workspaces": [ - "tests/deps/*", - "tests/projects/*", - "tests/projects/workspace/packages/*" - ], - "peerDependencies": { - "vite": "^3.0.0 || ^4.0.0 || ^5.0.0 || ^6.0.0 || ^7.0.0 || ^8.0.0" - }, - "peerDependenciesMeta": { - "vite": { - "optional": true + "node_modules/vfile": { + "version": "6.0.3", + "resolved": "https://registry.npmjs.org/vfile/-/vfile-6.0.3.tgz", + "integrity": "sha512-KzIbH/9tXat2u30jf+smMwFCsno4wHVdNmzFyL+T/L3UGqqk6JKfVqOFOZEpZSHADH1k40ab6NUIXZq422ov3Q==", + "license": "MIT", + "dependencies": { + "@types/unist": "^3.0.0", + "vfile-message": "^4.0.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/unified" } - } - }, - "node_modules/web-namespaces": { - "version": "2.0.1", - "resolved": "https://registry.npmjs.org/web-namespaces/-/web-namespaces-2.0.1.tgz", - "integrity": "sha512-bKr1DkiNa2krS7qxNtdrtHAmzuYGFQLiQ13TsorsdT6ULTkPLKuu5+GsFpDlg6JFjUTwX2DyhMPG2be8uPrqsQ==", - "license": "MIT", - "funding": { - "type": "github", - "url": "https://github.com/sponsors/wooorm" - } - }, - "node_modules/xml-naming": { - "version": "0.1.0", - "resolved": "https://registry.npmjs.org/xml-naming/-/xml-naming-0.1.0.tgz", - "integrity": "sha512-k8KO9hrMyNk6tUWqUfkTEZbezRRpONVOzUTnc97VnCvyj6Tf9lyUR9EDAIeiVLv56jsMcoXEwjW8Kv5yPY52lw==", - "funding": [ - { + }, + "node_modules/vfile-location": { + "version": "5.0.3", + "resolved": "https://registry.npmjs.org/vfile-location/-/vfile-location-5.0.3.tgz", + "integrity": "sha512-5yXvWDEgqeiYiBe1lbxYF7UMAIm/IcopxMHrMQDq3nvKcjPKIhZklUKL+AE7J7uApI4kwe2snsK+eI6UTj9EHg==", + "license": "MIT", + "dependencies": { + "@types/unist": "^3.0.0", + "vfile": "^6.0.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/unified" + } + }, + "node_modules/vfile-message": { + "version": "4.0.3", + "resolved": "https://registry.npmjs.org/vfile-message/-/vfile-message-4.0.3.tgz", + "integrity": "sha512-QTHzsGd1EhbZs4AsQ20JX1rC3cOlt/IWJruk893DfLRr57lcnOeMaWG4K0JrRta4mIJZKth2Au3mM3u03/JWKw==", + "license": "MIT", + "dependencies": { + "@types/unist": "^3.0.0", + "unist-util-stringify-position": "^4.0.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/unified" + } + }, + "node_modules/vite": { + "version": "8.3.5", + "resolved": "https://registry.npmjs.org/vite/-/vite-8.3.5.tgz", + "integrity": "sha512-z1YpjieJ9g0ebTmQpLBv/oq9E3F9EIlOe+7LahW8SrE3LTiG3udBRUXQOlYXP9b+IC2q2phHeVxj9xGlw34OEQ==", + "license": "MIT", + "dependencies": { + "lightningcss": "^1.33.0", + "picomatch": "^4.0.7", + "postcss": "^8.5.30", + "rolldown": "~1.2.14", + "tinyglobby": "^0.2.17" + }, + "bin": { + "vite": "bin/vite.js" + }, + "engines": { + "node": "^20.19.0 || >=22.12.0" + }, + "funding": { + "url": "https://github.com/vitejs/vite?sponsor=1" + }, + "optionalDependencies": { + "fsevents": "~2.3.3" + }, + "peerDependencies": { + "@types/node": "^20.19.0 || >=22.12.0", + "@vitejs/devtools": "^0.7.1 || ^0.8.0", + "esbuild": "^0.27.0 || ^0.28.0", + "jiti": ">=1.21.0", + "less": "^4.0.0", + "sass": "^1.70.0", + "sass-embedded": "^1.70.0", + "stylus": ">=0.54.8", + "sugarss": "^5.0.0", + "terser": "^5.16.0", + "tsx": "^4.8.1", + "yaml": "^2.4.2" + }, + "peerDependenciesMeta": { + "@types/node": { + "optional": true + }, + "@vitejs/devtools": { + "optional": true + }, + "esbuild": { + "optional": true + }, + "jiti": { + "optional": true + }, + "less": { + "optional": true + }, + "sass": { + "optional": true + }, + "sass-embedded": { + "optional": true + }, + "stylus": { + "optional": true + }, + "sugarss": { + "optional": true + }, + "terser": { + "optional": true + }, + "tsx": { + "optional": true + }, + "yaml": { + "optional": true + } + } + }, + "node_modules/vite-prerender-plugin": { + "version": "0.5.13", + "resolved": "https://registry.npmjs.org/vite-prerender-plugin/-/vite-prerender-plugin-0.5.13.tgz", + "integrity": "sha512-IKSpYkzDBsKAxa05naRbj7GvNVMSdww/Z/E89oO3xndz+gWnOBOKOAbEXv7qDhktY/j3vHgJmoV1pPzqU2tx9g==", + "license": "MIT", + "dependencies": { + "kolorist": "^1.8.0", + "magic-string": "0.x >= 0.26.0", + "node-html-parser": "^6.1.12", + "simple-code-frame": "^1.3.0", + "source-map": "^0.7.4", + "stack-trace": "^1.0.0-pre2" + }, + "peerDependencies": { + "vite": "5.x || 6.x || 7.x || 8.x" + } + }, + "node_modules/vitefu": { + "version": "1.1.3", + "resolved": "https://registry.npmjs.org/vitefu/-/vitefu-1.1.3.tgz", + "integrity": "sha512-ub4okH7Z5KLjb6hDyjqrGXqWtWvoYdU3IGm/NorpgHncKoLTCfRIbvlhBm7r0YstIaQRYlp4yEbFqDcKSzXSSg==", + "license": "MIT", + "workspaces": [ + "tests/deps/*", + "tests/projects/*", + "tests/projects/workspace/packages/*" + ], + "peerDependencies": { + "vite": "^3.0.0 || ^4.0.0 || ^5.0.0 || ^6.0.0 || ^7.0.0 || ^8.0.0" + }, + "peerDependenciesMeta": { + "vite": { + "optional": true + } + } + }, + "node_modules/web-namespaces": { + "version": "2.0.1", + "resolved": "https://registry.npmjs.org/web-namespaces/-/web-namespaces-2.0.1.tgz", + "integrity": "sha512-bKr1DkiNa2krS7qxNtdrtHAmzuYGFQLiQ13TsorsdT6ULTkPLKuu5+GsFpDlg6JFjUTwX2DyhMPG2be8uPrqsQ==", + "license": "MIT", + "funding": { "type": "github", - "url": "https://github.com/sponsors/NaturalIntelligence" + "url": "https://github.com/sponsors/wooorm" } - ], - "license": "MIT", - "engines": { - "node": ">=16.0.0" - } - }, - "node_modules/xxhash-wasm": { - "version": "1.1.0", - "resolved": "https://registry.npmjs.org/xxhash-wasm/-/xxhash-wasm-1.1.0.tgz", - "integrity": "sha512-147y/6YNh+tlp6nd/2pWq38i9h6mz/EuQ6njIrmW8D1BS5nCqs0P6DG+m6zTGnNz5I+uhZ0SHxBs9BsPrwcKDA==", - "license": "MIT" - }, - "node_modules/yallist": { - "version": "3.1.1", - "resolved": "https://registry.npmjs.org/yallist/-/yallist-3.1.1.tgz", - "integrity": "sha512-a4UGQaWPH59mOXUYnAG2ewncQS4i4F43Tv3JoAM+s2VDAmS9NsK8GpDMLrCHPksFT7h3K6TOoUNn2pb7RoXx4g==", - "license": "ISC" - }, - "node_modules/yaml": { - "version": "2.9.1", - "resolved": "https://registry.npmjs.org/yaml/-/yaml-2.9.1.tgz", - "integrity": "sha512-3NxN8+78OdzbT7C/WjGsyfPAtJaN3FNDsWxv7Y7mcDsT/oOmgW8BpyQQFFBnvZE3j9Y2Sdz1ULFLezL7Eb2yFw==", - "license": "ISC", - "bin": { - "yaml": "bin.mjs" }, - "engines": { - "node": ">= 14.6" + "node_modules/xml-naming": { + "version": "0.1.0", + "resolved": "https://registry.npmjs.org/xml-naming/-/xml-naming-0.1.0.tgz", + "integrity": "sha512-k8KO9hrMyNk6tUWqUfkTEZbezRRpONVOzUTnc97VnCvyj6Tf9lyUR9EDAIeiVLv56jsMcoXEwjW8Kv5yPY52lw==", + "funding": [ + { + "type": "github", + "url": "https://github.com/sponsors/NaturalIntelligence" + } + ], + "license": "MIT", + "engines": { + "node": ">=16.0.0" + } }, - "funding": { - "url": "https://github.com/sponsors/eemeli" - } - }, - "node_modules/yargs-parser": { - "version": "22.0.0", - "resolved": "https://registry.npmjs.org/yargs-parser/-/yargs-parser-22.0.0.tgz", - "integrity": "sha512-rwu/ClNdSMpkSrUb+d6BRsSkLUq1fmfsY6TOpYzTwvwkg1/NRG85KBy3kq++A8LKQwX6lsu+aWad+2khvuXrqw==", - "license": "ISC", - "engines": { - "node": "^20.19.0 || ^22.12.0 || >=23" - } - }, - "node_modules/yocto-queue": { - "version": "1.2.2", - "resolved": "https://registry.npmjs.org/yocto-queue/-/yocto-queue-1.2.2.tgz", - "integrity": "sha512-4LCcse/U2MHZ63HAJVE+v71o7yOdIe4cZ70Wpf8D/IyjDKYQLV5GD46B+hSTjJsvV5PztjvHoU580EftxjDZFQ==", - "license": "MIT", - "engines": { - "node": ">=12.20" + "node_modules/xxhash-wasm": { + "version": "1.1.0", + "resolved": "https://registry.npmjs.org/xxhash-wasm/-/xxhash-wasm-1.1.0.tgz", + "integrity": "sha512-147y/6YNh+tlp6nd/2pWq38i9h6mz/EuQ6njIrmW8D1BS5nCqs0P6DG+m6zTGnNz5I+uhZ0SHxBs9BsPrwcKDA==", + "license": "MIT" }, - "funding": { - "url": "https://github.com/sponsors/sindresorhus" - } - }, - "node_modules/zimmerframe": { - "version": "1.1.4", - "resolved": "https://registry.npmjs.org/zimmerframe/-/zimmerframe-1.1.4.tgz", - "integrity": "sha512-B58NGBEoc8Y9MWWCQGl/gq9xBCe4IiKM0a2x7GZdQKOW5Exr8S1W24J6OgM1njK8xCRGvAJIL/MxXHf6SkmQKQ==", - "license": "MIT" - }, - "node_modules/zod": { - "version": "4.6.7", - "resolved": "https://registry.npmjs.org/zod/-/zod-4.6.7.tgz", - "integrity": "sha512-g8W2D2kU6a8untc053fX1uVBKzM7yqAxW/3zwk83+6RyF2+SKOuV+c0wBDhUwzV1PaGapmZH+rmcUzPf+3CGwg==", - "license": "MIT", - "funding": { - "url": "https://github.com/sponsors/colinhacks" - } - }, - "node_modules/zwitch": { - "version": "2.0.4", - "resolved": "https://registry.npmjs.org/zwitch/-/zwitch-2.0.4.tgz", - "integrity": "sha512-bXE4cR/kVZhKZX/RjPEflHaKVhUVl85noU3v6b8apfQEc1x4A+zBxjZ4lN8LqGd6WZ3dl98pY4o717VFmoPp+A==", - "license": "MIT", - "funding": { - "type": "github", - "url": "https://github.com/sponsors/wooorm" + "node_modules/yallist": { + "version": "3.1.1", + "resolved": "https://registry.npmjs.org/yallist/-/yallist-3.1.1.tgz", + "integrity": "sha512-a4UGQaWPH59mOXUYnAG2ewncQS4i4F43Tv3JoAM+s2VDAmS9NsK8GpDMLrCHPksFT7h3K6TOoUNn2pb7RoXx4g==", + "license": "ISC" + }, + "node_modules/yaml": { + "version": "2.9.1", + "resolved": "https://registry.npmjs.org/yaml/-/yaml-2.9.1.tgz", + "integrity": "sha512-3NxN8+78OdzbT7C/WjGsyfPAtJaN3FNDsWxv7Y7mcDsT/oOmgW8BpyQQFFBnvZE3j9Y2Sdz1ULFLezL7Eb2yFw==", + "license": "ISC", + "bin": { + "yaml": "bin.mjs" + }, + "engines": { + "node": ">= 14.6" + }, + "funding": { + "url": "https://github.com/sponsors/eemeli" + } + }, + "node_modules/yargs-parser": { + "version": "22.0.0", + "resolved": "https://registry.npmjs.org/yargs-parser/-/yargs-parser-22.0.0.tgz", + "integrity": "sha512-rwu/ClNdSMpkSrUb+d6BRsSkLUq1fmfsY6TOpYzTwvwkg1/NRG85KBy3kq++A8LKQwX6lsu+aWad+2khvuXrqw==", + "license": "ISC", + "engines": { + "node": "^20.19.0 || ^22.12.0 || >=23" + } + }, + "node_modules/yocto-queue": { + "version": "1.2.2", + "resolved": "https://registry.npmjs.org/yocto-queue/-/yocto-queue-1.2.2.tgz", + "integrity": "sha512-4LCcse/U2MHZ63HAJVE+v71o7yOdIe4cZ70Wpf8D/IyjDKYQLV5GD46B+hSTjJsvV5PztjvHoU580EftxjDZFQ==", + "license": "MIT", + "engines": { + "node": ">=12.20" + }, + "funding": { + "url": "https://github.com/sponsors/sindresorhus" + } + }, + "node_modules/zimmerframe": { + "version": "1.1.4", + "resolved": "https://registry.npmjs.org/zimmerframe/-/zimmerframe-1.1.4.tgz", + "integrity": "sha512-B58NGBEoc8Y9MWWCQGl/gq9xBCe4IiKM0a2x7GZdQKOW5Exr8S1W24J6OgM1njK8xCRGvAJIL/MxXHf6SkmQKQ==", + "license": "MIT" + }, + "node_modules/zod": { + "version": "4.6.7", + "resolved": "https://registry.npmjs.org/zod/-/zod-4.6.7.tgz", + "integrity": "sha512-g8W2D2kU6a8untc053fX1uVBKzM7yqAxW/3zwk83+6RyF2+SKOuV+c0wBDhUwzV1PaGapmZH+rmcUzPf+3CGwg==", + "license": "MIT", + "funding": { + "url": "https://github.com/sponsors/colinhacks" + } + }, + "node_modules/zwitch": { + "version": "2.0.4", + "resolved": "https://registry.npmjs.org/zwitch/-/zwitch-2.0.4.tgz", + "integrity": "sha512-bXE4cR/kVZhKZX/RjPEflHaKVhUVl85noU3v6b8apfQEc1x4A+zBxjZ4lN8LqGd6WZ3dl98pY4o717VFmoPp+A==", + "license": "MIT", + "funding": { + "type": "github", + "url": "https://github.com/sponsors/wooorm" + } } } } -} +} \ No newline at end of file diff --git a/blog/package.json b/blog/package.json index 15173cc..0b17a9f 100644 --- a/blog/package.json +++ b/blog/package.json @@ -15,9 +15,11 @@ "@astrojs/mdx": "^7.0.8", "@astrojs/preact": "^6.0.5", "@astrojs/rss": "^4.0.19", + "@types/uuid": "^10.0.0", "astro": "^7.2.2", "jwt-decode": "^4.0.0", - "preact": "^10.29.8" + "preact": "^10.29.8", + "uuid": "^14.0.2" }, "devDependencies": { "@types/jsonwebtoken": "^9.0.10" diff --git a/blog/src/islands/Reaction.tsx b/blog/src/islands/Reaction.tsx new file mode 100644 index 0000000..16ea236 --- /dev/null +++ b/blog/src/islands/Reaction.tsx @@ -0,0 +1,67 @@ +import { useEffect, useState } from "preact/hooks" +import { api, BROWSER_ID, type Reaction } from "../lib/api" +import { v7 as uuidv7 } from 'uuid'; + +export default function Reaction({ slug }: { slug: string }) { + const [reaction, setReaction] = useState({ slug: slug, browser_id: "" }) + const [reactionCount, setReactionCount] = useState(0) + const [haveReacted, setHaveReacted] = useState(false) + + useEffect(() => { + let browserId = localStorage.getItem(BROWSER_ID) + + if (!browserId) { + browserId = uuidv7() + localStorage.setItem(BROWSER_ID, browserId) + } + + api.haveIReacted({ ...reaction, browser_id: browserId }).then((response: boolean) => { + setHaveReacted(response) + }) + + api.reactionCount(slug).then((count) => { + setReactionCount(count) + }) + + setReaction(prev => ({ ...prev, browser_id: browserId })) + }, []) + + const handleReaction = () => { + let hasError = false + if (!haveReacted) { + try { api.createReaction(reaction) } + catch (e) { hasError = true } + finally { + if (!hasError) { + setHaveReacted(true) + setReactionCount(prev => prev + 1) + } + } + } else { + try { api.deleteReaction(reaction) } + catch (e) { hasError = true } + finally { + if (!hasError) { + setHaveReacted(false) + setReactionCount(prev => prev - 1) + } + } + } + } + + + return ( + + ) +} \ No newline at end of file diff --git a/blog/src/islands/RegisterForm.tsx b/blog/src/islands/RegisterForm.tsx index a66111d..556dfd6 100644 --- a/blog/src/islands/RegisterForm.tsx +++ b/blog/src/islands/RegisterForm.tsx @@ -1,10 +1,9 @@ import { useEffect, useState } from 'preact/hooks'; import { api, type User } from '../lib/api'; -import React from 'preact/compat'; import { navigateBack, isEmailValid } from './Util'; export default function RegisterForm() { - const [user, setUser] = React.useState({ 'email': '', 'name': '', 'subscription': true }); + const [user, setUser] = useState({ 'email': '', 'name': '', 'subscription': true }); const [error, setError] = useState(''); const handleRegistration = async () => { diff --git a/blog/src/layouts/BlogPostLayout.astro b/blog/src/layouts/BlogPostLayout.astro index ec7b725..3a5914a 100644 --- a/blog/src/layouts/BlogPostLayout.astro +++ b/blog/src/layouts/BlogPostLayout.astro @@ -5,6 +5,7 @@ import Main from "./Main.astro"; import { formatLocalizedDateString } from "../lib/formatDate"; import type { PostData } from "../content.config"; import NavBack from "../islands/NavBack"; +import Reaction from "../islands/Reaction"; interface Props { postData: PostData; @@ -34,6 +35,9 @@ const localeDate = formatLocalizedDateString(postData.date);
+
+ +

Disclaimer: Dieser Artikel ist ausschließlich privat und wird nicht diff --git a/blog/src/lib/api.ts b/blog/src/lib/api.ts index b14a997..3a1b193 100644 --- a/blog/src/lib/api.ts +++ b/blog/src/lib/api.ts @@ -1,12 +1,20 @@ +import { v7 as uuidv7, type UUIDTypes } from 'uuid'; + export interface User { email: string; name: string; subscription: boolean; } +export interface Reaction { + slug: string, + browser_id: UUIDTypes +} + const API_BASE = import.meta.env.PUBLIC_API_URL; export const AUTH_TOKEN_KEY = 'authToken' export const USER_KEY = 'user' +export const BROWSER_ID = 'browser_id' @@ -91,6 +99,28 @@ class BlauwasserAPI { body: formData }) } + + async createReaction(reaction: Reaction): Promise { + return await this.fetchAPI('/reactions', { + method: 'POST', + body: JSON.stringify(reaction) + }) + } + + async deleteReaction(reaction: Reaction): Promise { + return await this.fetchAPI('/reactions', { + method: 'DELETE', + body: JSON.stringify(reaction) + }) + } + + async reactionCount(slug: string): Promise { + return await this.fetchAPI(`/reactions/${slug}`, { method: 'GET' }) + } + + async haveIReacted(reaction: Reaction): Promise { + return await this.fetchAPI(`/reactions/?slug=${reaction.slug}&browser_id=${reaction.browser_id}`, {method: 'GET'}) + } }