Pagination added and search and filtering moved to backend
Build LinkLog Development Image / development-image (push) Successful in 22s
Build LinkLog Development Image / development-image (push) Successful in 22s
This commit is contained in:
@@ -3,6 +3,8 @@
|
||||
|
||||
from datetime import datetime, timezone
|
||||
import json
|
||||
import re
|
||||
from urllib.parse import urlparse
|
||||
from uuid import uuid4
|
||||
|
||||
from backend.app.core.security import clean_url
|
||||
@@ -136,23 +138,93 @@ def find_owned_link_by_title(user_id: str, title: str) -> dict | None:
|
||||
return record
|
||||
|
||||
|
||||
def list_public_links(username: str | None = None):
|
||||
def _tokenize_search(value: str) -> list[str]:
|
||||
return [group1 or group2 for group1, group2 in re.findall(r'"([^"]+)"|(\S+)', value or '')]
|
||||
|
||||
|
||||
def _parse_search_query(value: str) -> dict:
|
||||
groups = [[]]
|
||||
excluded = []
|
||||
sites = []
|
||||
for token in _tokenize_search(value):
|
||||
if token.upper() == 'OR':
|
||||
groups.append([])
|
||||
continue
|
||||
is_excluded = token.startswith('-') and len(token) > 1
|
||||
term = token[1:] if is_excluded else token
|
||||
site_match = re.match(r'^site:(\S+)$', term, re.IGNORECASE)
|
||||
if site_match and not is_excluded:
|
||||
sites.append(site_match.group(1).lower())
|
||||
continue
|
||||
if is_excluded:
|
||||
excluded.append(term.lower())
|
||||
else:
|
||||
groups[-1].append(term.lower())
|
||||
return {'groups': [group for group in groups if group], 'excluded': excluded, 'sites': sites}
|
||||
|
||||
|
||||
def _searchable_text(record: dict) -> str:
|
||||
parts = [record.get('title'), record.get('url'), record.get('comment'), record.get('username'), *(record.get('tags') or [])]
|
||||
return ' '.join(part for part in parts if part).lower()
|
||||
|
||||
|
||||
def _matches_search(record: dict, query: str) -> bool:
|
||||
if not query or not query.strip():
|
||||
return True
|
||||
parsed = _parse_search_query(query)
|
||||
text = _searchable_text(record)
|
||||
|
||||
def site_matches(site: str) -> bool:
|
||||
hostname = (urlparse(record.get('url') or '').hostname or '').lower()
|
||||
return hostname == site or hostname.endswith(f'.{site}')
|
||||
|
||||
matches_site = all(site_matches(site) for site in parsed['sites'])
|
||||
matches_group = not parsed['groups'] or any(all(term in text for term in group) for group in parsed['groups'])
|
||||
excludes_term = any(term in text for term in parsed['excluded'])
|
||||
return matches_site and matches_group and not excludes_term
|
||||
|
||||
|
||||
def list_public_links(
|
||||
username: str | None = None,
|
||||
tag: str | None = None,
|
||||
search: str | None = None,
|
||||
sort: str = 'newest',
|
||||
page: int = 1,
|
||||
page_size: int | None = None,
|
||||
) -> dict:
|
||||
order = 'ASC' if sort == 'oldest' else 'DESC'
|
||||
with get_connection() as conn:
|
||||
rows = conn.execute(
|
||||
'''
|
||||
f'''
|
||||
SELECT links.*, users.username, users.avatar_url, users.bio
|
||||
FROM links
|
||||
JOIN users ON users.id = links.user_id
|
||||
WHERE links.is_public = 1
|
||||
AND (? IS NULL OR users.username = ?)
|
||||
ORDER BY created_at DESC
|
||||
AND (
|
||||
? IS NULL OR links.id IN (
|
||||
SELECT link_tags.link_id FROM link_tags
|
||||
JOIN tags ON tags.id = link_tags.tag_id
|
||||
WHERE lower(tags.name) = lower(?)
|
||||
)
|
||||
)
|
||||
ORDER BY links.created_at {order}
|
||||
''',
|
||||
(username, username),
|
||||
(username, username, tag, tag),
|
||||
).fetchall()
|
||||
records = [dict(row) for row in rows]
|
||||
for record in records:
|
||||
record['tags'] = get_link_tags(conn, record['id'])
|
||||
return records
|
||||
|
||||
if search:
|
||||
records = [record for record in records if _matches_search(record, search)]
|
||||
|
||||
total = len(records)
|
||||
if page_size:
|
||||
start = max(page - 1, 0) * page_size
|
||||
records = records[start:start + page_size]
|
||||
|
||||
return {'items': records, 'total': total}
|
||||
|
||||
|
||||
def get_public_profile(username: str) -> dict | None:
|
||||
|
||||
Reference in New Issue
Block a user