Favorites is fixed.

This commit is contained in:
retoor 2025-08-04 00:40:37 +02:00
parent efcc4a14b6
commit 2960bbfd61
2 changed files with 138 additions and 106 deletions

71
main.py
View File

@ -145,7 +145,7 @@ def hash_password(password: str) -> str:
def generate_token() -> str:
return secrets.token_urlsafe(32)
async def get_current_user(token_id: Optional[int] = Form(None),
async def DELETE_get_current_user(token_id: Optional[int] = Form(None),
token_key: Optional[str] = Form(None),
user_id: Optional[int] = Form(None)):
if not all([token_id, token_key, user_id]):
@ -328,7 +328,7 @@ async def get_rants(
token_key: Optional[str] = None,
user_id: Optional[int] = None
):
current_user_id = await get_current_user(token_id, token_key, user_id) if token_id else None
current_user_id = await authenticate_user(token_id, token_key, user_id) if token_id else None
# Get rants with user info
order_by = "r.created_time DESC" if sort == "recent" else "r.score DESC"
@ -393,7 +393,7 @@ async def get_rant(
token_key: Optional[str] = None,
user_id: Optional[int] = None
):
current_user_id = await get_current_user(token_id, token_key, user_id) if token_id else None
current_user_id = await authenticate_user(token_id, token_key, user_id) if token_id else None
# Get rant with user info
rant_row = await db.query_one(
@ -485,7 +485,7 @@ async def create_rant(
user_id: int = Form(...),
image: Optional[UploadFile] = File(None)
):
current_user_id = await get_current_user(token_id, token_key, user_id)
current_user_id = await authenticate_user(token_id, token_key, user_id)
if not current_user_id:
return {"success": False, "error": "Authentication required"}
@ -539,7 +539,7 @@ async def update_rant(
token_key: str = Form(...),
user_id: int = Form(...)
):
current_user_id = await get_current_user(token_id, token_key, user_id)
current_user_id = await authenticate_user(token_id, token_key, user_id)
if not current_user_id:
return {"success": False, "error": "Authentication required"}
@ -568,7 +568,7 @@ async def delete_rant(
token_key: str = None,
user_id: int = None
):
current_user_id = await get_current_user(token_id, token_key, user_id)
current_user_id = await authenticate_user(token_id, token_key, user_id)
if not current_user_id:
return {"success": False, "error": "Authentication required"}
@ -599,7 +599,7 @@ async def vote_rant(
token_key: str = Form(...),
user_id: int = Form(...)
):
current_user_id = await get_current_user(token_id, token_key, user_id)
current_user_id = await authenticate_user(token_id, token_key, user_id)
if not current_user_id:
return {"success": False, "error": "Authentication required"}
@ -696,7 +696,7 @@ async def favorite_rant(
token_key: str = Form(...),
user_id: int = Form(...)
):
current_user_id = await get_current_user(token_id, token_key, user_id)
current_user_id = await authenticate_user(token_id, token_key, user_id)
if not current_user_id:
return {"success": False, "error": "Authentication required"}
@ -717,7 +717,7 @@ async def unfavorite_rant(
token_key: str = Form(...),
user_id: int = Form(...)
):
current_user_id = await get_current_user(token_id, token_key, user_id)
current_user_id = await authenticate_user(token_id, token_key, user_id)
if not current_user_id:
return {"success": False, "error": "Authentication required"}
@ -738,7 +738,7 @@ async def create_comment(
user_id: int = Form(...),
image: Optional[UploadFile] = File(None)
):
current_user_id = await get_current_user(token_id, token_key, user_id)
current_user_id = await authenticate_user(token_id, token_key, user_id)
if not current_user_id:
return {"success": False, "confirmed": False}
@ -792,7 +792,7 @@ async def get_comment(
token_key: Optional[str] = None,
user_id: Optional[int] = None
):
current_user_id = await get_current_user(token_id, token_key, user_id) if token_id else None
current_user_id = await authenticate_user(token_id, token_key, user_id) if token_id else None
row = await db.query_one(
"""SELECT c.*, u.id as user_id, u.username, u.score as user_score,
@ -835,7 +835,7 @@ async def update_comment(
token_key: str = Form(...),
user_id: int = Form(...)
):
current_user_id = await get_current_user(token_id, token_key, user_id)
current_user_id = await authenticate_user(token_id, token_key, user_id)
if not current_user_id:
return {"success": False, "error": "Authentication required"}
@ -864,7 +864,7 @@ async def delete_comment(
token_key: str = None,
user_id: int = None
):
current_user_id = await get_current_user(token_id, token_key, user_id)
current_user_id = await authenticate_user(token_id, token_key, user_id)
if not current_user_id:
return {"success": False, "error": "Authentication required"}
@ -893,7 +893,7 @@ async def vote_comment(
token_key: str = Form(...),
user_id: int = Form(...)
):
current_user_id = await get_current_user(token_id, token_key, user_id)
current_user_id = await authenticate_user(token_id, token_key, user_id)
if not current_user_id:
return {"success": False, "error": "Authentication required"}
@ -952,7 +952,7 @@ async def get_profile(
token_key: Optional[str] = None,
auth_user_id: Optional[int] = None
):
current_user_id = await get_current_user(token_id, token_key, auth_user_id) if token_id else None
current_user_id = await authenticate_user(token_id, token_key, auth_user_id) if token_id else None
# Get user
user = await db.get("users", {"id": user_id})
@ -1105,7 +1105,7 @@ async def search(
token_key: Optional[str] = None,
user_id: Optional[int] = None
):
current_user_id = await get_current_user(token_id, token_key, user_id) if token_id else None
current_user_id = await authenticate_user(token_id, token_key, user_id) if token_id else None
# Search rants
rows = await db.query_raw(
@ -1148,11 +1148,12 @@ async def get_notifications(
ext_prof: int = 1,
last_time: Optional[int] = None,
app: int = 3,
token_id: int = None,
token_key: str = None,
user_id: int = None
token_id: Optional[int] = None,
token_key: Optional[str] = None,
user_id: Optional[int] = None
):
current_user_id = await get_current_user(token_id, token_key, user_id)
# Use the generic authenticate_user function
current_user_id = await authenticate_user(token_id, token_key, user_id)
if not current_user_id:
return {"success": False, "error": "Authentication required"}
@ -1185,7 +1186,8 @@ async def get_notifications(
if not row['read']:
unread_count += 1
# Mark as read
# Mark notifications as read
if rows: # Only update if there are notifications
await db.update("notifications", {"read": 1}, {"user_id": current_user_id})
return {
@ -1206,6 +1208,25 @@ async def get_notifications(
}
}
async def authenticate_user(token_id: Optional[int] = None,
token_key: Optional[str] = None,
user_id: Optional[int] = None):
"""Generic authentication function that works with any parameter source"""
if not all([token_id, token_key, user_id]):
return None
token = await db.get("auth_tokens", {
"id": token_id,
"token_key": token_key,
"user_id": user_id
})
if not token or token['expire_time'] <= int(datetime.now().timestamp()):
return None
return user_id
@app.delete("/api/users/me/notif-feed")
async def clear_notifications(
app: int = Form(3),
@ -1213,7 +1234,7 @@ async def clear_notifications(
token_key: str = Form(...),
user_id: int = Form(...)
):
current_user_id = await get_current_user(token_id, token_key, user_id)
current_user_id = await authenticate_user(token_id, token_key, user_id)
if not current_user_id:
return {"success": False, "error": "Authentication required"}
@ -1233,7 +1254,7 @@ async def edit_profile(
token_key: str = Form(...),
user_id: int = Form(...)
):
current_user_id = await get_current_user(token_id, token_key, user_id)
current_user_id = await authenticate_user(token_id, token_key, user_id)
if not current_user_id:
return {"success": False, "error": "Authentication required"}
@ -1262,7 +1283,7 @@ async def resend_confirmation(
token_key: str = Form(...),
user_id: int = Form(...)
):
current_user_id = await get_current_user(token_id, token_key, user_id)
current_user_id = await authenticate_user(token_id, token_key, user_id)
if not current_user_id:
return {"success": False, "error": "Authentication required"}
@ -1277,7 +1298,7 @@ async def mark_news_read(
token_key: str = Form(...),
user_id: int = Form(...)
):
current_user_id = await get_current_user(token_id, token_key, user_id)
current_user_id = await authenticate_user(token_id, token_key, user_id)
if not current_user_id:
return {"success": False, "error": "Authentication required"}

View File

@ -636,8 +636,9 @@
document.getElementById('createRantBtn').style.display = isLoggedIn ? 'flex' : 'none';
}
async function apiCall(endpoint, options = {}) {
const url = `${API_URL}${endpoint}`;
let url = `${API_URL}${endpoint}`;
// Add auth to FormData or URLSearchParams if logged in
if (currentUser && options.body) {
@ -657,7 +658,7 @@
// Add auth to query params for GET requests
if (currentUser && (options.method === 'GET' || !options.method)) {
const separator = endpoint.includes('?') ? '&' : '?';
endpoint += `${separator}app=${APP_ID}&token_id=${currentUser.token_id}&token_key=${currentUser.token_key}&user_id=${currentUser.id}`;
url += `${separator}app=${APP_ID}&token_id=${currentUser.token_id}&token_key=${currentUser.token_key}&user_id=${currentUser.id}`;
}
try {
@ -670,6 +671,7 @@
}
}
// View functions
async function showFeed(sort = 'recent') {
currentView = 'feed';
@ -994,9 +996,9 @@
const params = new URLSearchParams({
ext_prof: 1,
last_time: Math.floor(Date.now() / 1000) - 86400,
app: APP_ID
last_time: Math.floor(Date.now() / 1000) - 86400
});
const data = await apiCall(`/users/me/notif-feed?${params}`);
if (data.success) {
@ -1006,7 +1008,7 @@
<h2>Notifications</h2>
${items.length === 0 ? '<p style="text-align: center; color: var(--text-dim); margin-top: 2rem;">No notifications</p>' : ''}
${items.map(notif => `
<div class="rant-card" onclick="showRant(${notif.rant_id})">
<div class="rant-card" onclick="showRant(${notif.rant_id})" style="cursor: pointer;">
<p><strong>${notif.username}</strong> ${notif.type === 'comment' ? 'commented on your rant' : 'mentioned you'}</p>
<p style="color: var(--text-dim); font-size: 0.9rem;">${formatTime(notif.created_time)}</p>
</div>
@ -1015,17 +1017,25 @@
// Update notification count
updateNotificationCount(0);
} else {
content.innerHTML = `
<h2>Notifications</h2>
<p style="text-align: center; color: var(--error); margin-top: 2rem;">Failed to load notifications: ${data.error || 'Unknown error'}</p>
`;
}
}
async function checkNotifications() {
if (!currentUser) return;
const params = new URLSearchParams({
ext_prof: 1,
last_time: Math.floor(Date.now() / 1000) - 86400,
app: APP_ID
last_time: Math.floor(Date.now() / 1000) - 86400
});
const data = await apiCall(`/users/me/notif-feed?${params}`);
if (data.success) {
@ -1033,6 +1043,7 @@
}
}
function updateNotificationCount(count) {
const notifCount = document.getElementById('notifCount');
if (count > 0) {