Suggesting channels that names match, even without spaces.

This commit is contained in:
Roland 2024-02-19 14:55:53 -07:00
parent 6d5a56cacc
commit 85a1360a25
No known key found for this signature in database
GPG key ID: 9D6DBD3ACF11AB93
2 changed files with 22 additions and 5 deletions

View file

@ -1,7 +1,7 @@
from pathlib import Path
from typing import List, Dict, Union
from sqlalchemy import asc
from sqlalchemy import asc, or_
from sqlalchemy.orm import Session
from sqlalchemy.orm.exc import NoResultFound
@ -203,9 +203,14 @@ def download_channel(id_: int, reset_attempts: bool = False):
@optional_session
async def search_channels_by_name(name: str, limit: int = 5, session: Session = None) -> List[Channel]:
channels = session.query(Channel) \
.filter(Channel.name.ilike(f'%{name}%')) \
name = name or ''
name_no_spaces = ''.join(name.split(' '))
stmt = session.query(Channel) \
.filter(or_(
Channel.name.ilike(f'%{name}%'),
Channel.name.ilike(f'%{name_no_spaces}%'),
)) \
.order_by(asc(Channel.name)) \
.limit(limit) \
.all()
.limit(limit)
channels = stmt.all()
return channels

View file

@ -503,6 +503,18 @@ async def test_search_suggestions(test_session, test_async_client, channel_facto
1,
)
# Channel name "Fool" is matched because spaces are stripped in addition to only
await assert_results(
dict(search_str='foo l'),
[
{'directory': 'Fool',
'id': 2,
'name': 'Fool',
'url': 'https://example.com/Fool'}],
[],
0,
)
await assert_results(
dict(search_str='bar'),
[{'directory': 'Bar', 'id': 3, 'name': 'Bar', 'url': 'https://example.com/Bar'}],