-
-
Notifications
You must be signed in to change notification settings - Fork 845
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improve Khoj Chat and Settings UI (#630)
* Fix license in pyproject.toml. Remove unused utils.state import * Use single debug mode check function. Disable telemetry in debug mode - Use single logic to check if khoj is running in debug mode. Previously there were 3 different variants of the check - Do not log telemetry if KHOJ_DEBUG is set to true. Previously didn't log telemetry even if KHOJ_DEBUG set to false * Respect line breaks in user, khoj chat messages to improve formatting * Disable Whatsapp config section on web client if Twilio not configured Simplify Whatsapp configuration status checking js by standardizing external input to lower case * Disable Phone API when Twilio not setup and rate limit calls to it - Move phone api to separate router and only enable it if Twilio enabled - Add rate-limiting to OTP and verification calls * Add slugs for phone rate limiting --------- Co-authored-by: sabaimran <[email protected]>
- Loading branch information
Showing
13 changed files
with
133 additions
and
89 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
import logging | ||
from typing import Optional | ||
|
||
from fastapi import APIRouter, Depends, HTTPException, Request | ||
from starlette.authentication import requires | ||
|
||
from khoj.database import adapters | ||
from khoj.database.models import KhojUser | ||
from khoj.routers.helpers import ApiUserRateLimiter, update_telemetry_state | ||
from khoj.routers.twilio import create_otp, verify_otp | ||
|
||
api_phone = APIRouter() | ||
logger = logging.getLogger(__name__) | ||
|
||
|
||
@api_phone.post("", status_code=200) | ||
@requires(["authenticated"]) | ||
async def update_phone_number( | ||
request: Request, | ||
phone_number: str, | ||
client: Optional[str] = None, | ||
rate_limiter_per_day=Depends( | ||
ApiUserRateLimiter(requests=5, subscribed_requests=5, window=60 * 60 * 24, slug="update_phone") | ||
), | ||
): | ||
user = request.user.object | ||
|
||
await adapters.aset_user_phone_number(user, phone_number) | ||
create_otp(user) | ||
|
||
update_telemetry_state( | ||
request=request, | ||
telemetry_type="api", | ||
api="set_phone_number", | ||
client=client, | ||
metadata={"phone_number": phone_number}, | ||
) | ||
|
||
return {"status": "ok"} | ||
|
||
|
||
@api_phone.delete("", status_code=200) | ||
@requires(["authenticated"]) | ||
async def delete_phone_number( | ||
request: Request, | ||
client: Optional[str] = None, | ||
): | ||
user = request.user.object | ||
|
||
await adapters.aremove_phone_number(user) | ||
|
||
update_telemetry_state( | ||
request=request, | ||
telemetry_type="api", | ||
api="delete_phone_number", | ||
client=client, | ||
) | ||
|
||
return {"status": "ok"} | ||
|
||
|
||
@api_phone.post("/verify", status_code=200) | ||
@requires(["authenticated"]) | ||
async def verify_mobile_otp( | ||
request: Request, | ||
code: str, | ||
client: Optional[str] = None, | ||
rate_limiter_per_day=Depends( | ||
ApiUserRateLimiter(requests=5, subscribed_requests=5, window=60 * 60 * 24, slug="verify_phone") | ||
), | ||
): | ||
user: KhojUser = request.user.object | ||
|
||
update_telemetry_state( | ||
request=request, | ||
telemetry_type="api", | ||
api="verify_phone_number", | ||
client=client, | ||
) | ||
|
||
if not verify_otp(user, code): | ||
raise HTTPException(status_code=400, detail="Invalid OTP") | ||
|
||
user.verified_phone_number = True | ||
await user.asave() | ||
return {"status": "ok"} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters