11from typing import Annotated , List
22
33from fastapi import APIRouter , Header , HTTPException , Request , status
4+ from fastapi .params import Depends
45from pydantic import BaseModel
56
7+ from app .routers .authentication import get_current_active_community
68from app .schemas import Library as LibrarySchema
79from app .schemas import LibraryNews
810from app .schemas import LibraryRequest as LibraryRequestSchema
911from app .schemas import Subscription as SubscriptionSchema
1012from app .services .database .models import Library , Subscription
13+ from app .services .database .models .communities import Community as DBCommunity
1114from app .services .database .models .libraries_request import LibraryRequest
1215from app .services .database .orm .library import (
1316 get_libraries_by_language ,
1619)
1720from app .services .database .orm .library_request import insert_library_request
1821from app .services .database .orm .subscription import upsert_multiple_subscription
22+ from app .services .limiter import limiter
1923
2024
2125class LibraryResponse (BaseModel ):
@@ -36,7 +40,14 @@ def setup():
3640 summary = "Get libraries by language" ,
3741 description = "Get libraries by language" ,
3842 )
39- async def get_by_language (request : Request , language : str ):
43+ @limiter .limit ("60/minute" )
44+ async def get_by_language (
45+ request : Request ,
46+ language : str ,
47+ current_community : Annotated [
48+ DBCommunity , Depends (get_current_active_community )
49+ ],
50+ ):
4051 try :
4152 libraryList = await get_libraries_by_language (
4253 language = language , session = request .app .db_session_factory
@@ -71,9 +82,13 @@ async def get_by_language(request: Request, language: str):
7182 summary = "Create a library" ,
7283 description = "Create a new library to follow" ,
7384 )
85+ @limiter .limit ("60/minute" )
7486 async def create_library (
7587 request : Request ,
7688 body : LibrarySchema ,
89+ current_community : Annotated [
90+ DBCommunity , Depends (get_current_active_community )
91+ ],
7792 ):
7893 library = Library (
7994 library_name = body .library_name ,
@@ -84,6 +99,7 @@ async def create_library(
8499 releases_doc_url = body .releases_doc_url ,
85100 fixed_release_url = body .fixed_release_url ,
86101 language = body .language ,
102+ community_id = current_community .id ,
87103 )
88104 try :
89105 await insert_library (library , request .app .db_session_factory )
@@ -104,10 +120,14 @@ async def create_library(
104120 "Subscribe to multiple libs and tags to receive libs updates"
105121 ),
106122 )
123+ @limiter .limit ("60/minute" )
107124 async def subscribe_libraries (
108125 request : Request ,
109126 body : SubscriptionSchema ,
110127 user_email : Annotated [str , Header (alias = "user-email" )],
128+ current_community : Annotated [
129+ DBCommunity , Depends (get_current_active_community )
130+ ],
111131 ):
112132 try :
113133 library_ids = await get_library_ids_by_multiple_names (
@@ -121,7 +141,10 @@ async def subscribe_libraries(
121141
122142 subscriptions = [
123143 Subscription (
124- user_email = user_email , tags = body .tags , library_id = id
144+ user_email = user_email ,
145+ tags = body .tags ,
146+ library_id = id ,
147+ community_id = current_community .id ,
125148 )
126149 for id in library_ids
127150 ]
@@ -145,16 +168,21 @@ async def subscribe_libraries(
145168 summary = "Request a library" ,
146169 description = "Request a library to follow" ,
147170 )
171+ @limiter .limit ("60/minute" )
148172 async def request_library (
149173 request : Request ,
150174 body : LibraryRequestSchema ,
151175 user_email : Annotated [str , Header (alias = "user-email" )],
176+ current_community : Annotated [
177+ DBCommunity , Depends (get_current_active_community )
178+ ],
152179 ):
153180 try :
154181 library_request = LibraryRequest (
155182 user_email = user_email ,
156183 library_name = body .library_name ,
157184 library_home_page = body .library_home_page ,
185+ community_id = current_community .id ,
158186 )
159187
160188 await insert_library_request (
0 commit comments