mirror of
https://gitlab.com/SIGBUS/nyaa.git
synced 2024-12-22 19:49:59 +00:00
Redirect searches with an info hash to torrent if found
Redirect will not happen on RSS or user page views (/searches). Adds a helper to models.Torrent to search with a hex hash Reformats "special search logic"
This commit is contained in:
parent
0e89d3f24b
commit
3a4280ccc5
|
@ -205,6 +205,11 @@ class Torrent(db.Model):
|
||||||
def by_info_hash(cls, info_hash):
|
def by_info_hash(cls, info_hash):
|
||||||
return cls.query.filter_by(info_hash=info_hash).first()
|
return cls.query.filter_by(info_hash=info_hash).first()
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def by_info_hash_hex(cls, info_hash_hex):
|
||||||
|
info_hash_bytes = bytearray.fromhex(info_hash_hex)
|
||||||
|
return cls.by_info_hash(info_hash_bytes)
|
||||||
|
|
||||||
|
|
||||||
class TorrentNameSearch(FullText, Torrent):
|
class TorrentNameSearch(FullText, Torrent):
|
||||||
__fulltext_columns__ = ('display_name',)
|
__fulltext_columns__ = ('display_name',)
|
||||||
|
|
|
@ -182,16 +182,25 @@ def home(rss):
|
||||||
flask.abort(404)
|
flask.abort(404)
|
||||||
user_id = user.id
|
user_id = user.id
|
||||||
|
|
||||||
user_query = {
|
special_results = {
|
||||||
'first_word_user': None,
|
'first_word_user': None,
|
||||||
'query_sans_user': None
|
'query_sans_user': None,
|
||||||
|
'infohash_torrent': None
|
||||||
}
|
}
|
||||||
if search_term:
|
# Add advanced features to searches (but not RSS or user searches)
|
||||||
|
if search_term and not render_as_rss and not user_id:
|
||||||
|
# Check if the first word of the search is an existing user
|
||||||
user_word_match = re.match(r'^([a-zA-Z0-9_-]+) *(.*|$)', search_term)
|
user_word_match = re.match(r'^([a-zA-Z0-9_-]+) *(.*|$)', search_term)
|
||||||
if user_word_match:
|
if user_word_match:
|
||||||
first_word_username = user_word_match.group(1)
|
special_results['first_word_user'] = models.User.by_username(user_word_match.group(1))
|
||||||
user_query['first_word_user'] = models.User.by_username(first_word_username)
|
special_results['query_sans_user'] = user_word_match.group(2)
|
||||||
user_query['query_sans_user'] = user_word_match.group(2)
|
|
||||||
|
# Check if search is a 40-char torrent hash
|
||||||
|
infohash_match = re.match(r'(?i)^([a-f0-9]{40})$', search_term)
|
||||||
|
if infohash_match:
|
||||||
|
# Check for info hash in database
|
||||||
|
matched_torrent = models.Torrent.by_info_hash_hex(infohash_match.group(1))
|
||||||
|
special_results['infohash_torrent'] = matched_torrent
|
||||||
|
|
||||||
query_args = {
|
query_args = {
|
||||||
'user': user_id,
|
'user': user_id,
|
||||||
|
@ -210,6 +219,14 @@ def home(rss):
|
||||||
if flask.g.user.is_moderator: # God mode
|
if flask.g.user.is_moderator: # God mode
|
||||||
query_args['admin'] = True
|
query_args['admin'] = True
|
||||||
|
|
||||||
|
infohash_torrent = special_results.get('infohash_torrent')
|
||||||
|
if infohash_torrent:
|
||||||
|
# infohash_torrent is only set if this is not RSS or userpage search
|
||||||
|
flask.flash(flask.Markup('You were redirected here because '
|
||||||
|
'the given hash matched this torrent.'), 'info')
|
||||||
|
# Redirect user from search to the torrent if we found one with the specific info_hash
|
||||||
|
return flask.redirect(flask.url_for('view_torrent', torrent_id=infohash_torrent.id))
|
||||||
|
|
||||||
# If searching, we get results from elastic search
|
# If searching, we get results from elastic search
|
||||||
use_elastic = app.config.get('USE_ELASTIC_SEARCH')
|
use_elastic = app.config.get('USE_ELASTIC_SEARCH')
|
||||||
if use_elastic and search_term:
|
if use_elastic and search_term:
|
||||||
|
@ -243,7 +260,7 @@ def home(rss):
|
||||||
torrent_query=query_results,
|
torrent_query=query_results,
|
||||||
search=query_args,
|
search=query_args,
|
||||||
rss_filter=rss_query_string,
|
rss_filter=rss_query_string,
|
||||||
user_query=user_query)
|
special_results=special_results)
|
||||||
else:
|
else:
|
||||||
# If ES is enabled, default to db search for browsing
|
# If ES is enabled, default to db search for browsing
|
||||||
if use_elastic:
|
if use_elastic:
|
||||||
|
@ -265,7 +282,7 @@ def home(rss):
|
||||||
torrent_query=query,
|
torrent_query=query,
|
||||||
search=query_args,
|
search=query_args,
|
||||||
rss_filter=rss_query_string,
|
rss_filter=rss_query_string,
|
||||||
user_query=user_query)
|
special_results=special_results)
|
||||||
|
|
||||||
|
|
||||||
@app.route('/user/<user_name>', methods=['GET', 'POST'])
|
@app.route('/user/<user_name>', methods=['GET', 'POST'])
|
||||||
|
|
|
@ -9,10 +9,10 @@
|
||||||
</th>
|
</th>
|
||||||
{% endmacro %}
|
{% endmacro %}
|
||||||
|
|
||||||
{% if user_query is defined %}
|
{% if special_results is defined and not search.user %}
|
||||||
{% if user_query.first_word_user and not search.user %}
|
{% if special_results.first_word_user %}
|
||||||
<div class="alert alert-info">
|
<div class="alert alert-info">
|
||||||
<a href="/user/{{ user_query.first_word_user.username }}{{ modify_query(q=user_query.query_sans_user)[1:] }}">Click here to see only results uploaded by {{ user_query.first_word_user.username }}</a>
|
<a href="/user/{{ special_results.first_word_user.username }}{{ modify_query(q=special_results.query_sans_user)[1:] }}">Click here to see only results uploaded by {{ special_results.first_word_user.username }}</a>
|
||||||
</div>
|
</div>
|
||||||
{% endif %}
|
{% endif %}
|
||||||
{% endif %}
|
{% endif %}
|
||||||
|
|
Loading…
Reference in a new issue