add sentto seach query for messages + make before and after work for user search

pull/136/head
Aevann 2023-03-03 04:04:09 +02:00
parent 01f671d19a
commit 97911f261b
2 changed files with 78 additions and 41 deletions

View File

@ -19,6 +19,7 @@ valid_params = [
'before',
'after',
'title',
'sentto',
search_operator_hole,
]
@ -345,6 +346,13 @@ def searchmessages(v:User):
except: abort(400)
comments = comments.filter(Comment.created_utc < before)
if 'sentto' in criteria:
sentto = criteria['sentto']
try: sentto = get_user(sentto, graceful=True)
except:
abort(400, "The `sentto` field must contain a user's username!")
comments = comments.filter(Comment.sentto == sentto.id)
comments = sort_objects(sort, comments, Comment)
total = comments.count()
@ -366,31 +374,51 @@ def searchmessages(v:User):
@limiter.limit(DEFAULT_RATELIMIT, key_func=get_ID)
@auth_required
def searchusers(v:User):
query = request.values.get("q", '').strip()
if not query:
abort(403, "Empty searches aren't allowed!")
try: page = max(1, int(request.values.get("page", 1)))
except: abort(400, "Invalid page input!")
sort = request.values.get("sort", "new").lower()
t = request.values.get('t', 'all').lower()
term=query.lstrip('@')
term = term.replace('\\','').replace('_','\_').replace('%','')
users = g.db.query(User)
users=g.db.query(User).filter(
or_(
User.username.ilike(f'%{term}%'),
User.original_username.ilike(f'%{term}%')
)
)
criteria = searchparse(query)
users=users.order_by(User.username.ilike(term).desc(), User.stored_subscriber_count.desc())
if 'after' in criteria:
after = criteria['after']
try: after = int(after)
except:
try: after = timegm(time.strptime(after, "%Y-%m-%d"))
except: abort(400)
users = users.filter(User.created_utc > after)
total=users.count()
if 'before' in criteria:
before = criteria['before']
try: before = int(before)
except:
try: before = timegm(time.strptime(before, "%Y-%m-%d"))
except: abort(400)
users = users.filter(User.created_utc < before)
if 'q' in criteria:
term = criteria['q'][0]
term = term.lstrip('@')
term = term.replace('\\','').replace('_','\_').replace('%','')
users = users.filter(
or_(
User.username.ilike(f'%{term}%'),
User.original_username.ilike(f'%{term}%')
)
).order_by(User.username.ilike(term).desc(), User.stored_subscriber_count.desc())
total = users.count()
users = users.offset(PAGE_SIZE * (page-1)).limit(PAGE_SIZE+1).all()
next_exists=(len(users)>PAGE_SIZE)
users=users[:PAGE_SIZE]
next_exists = (len(users)>PAGE_SIZE)
users = users[:PAGE_SIZE]
if v.client: return {"data": [x.json for x in users]}
return render_template("search_users.html", v=v, query=query, total=total, page=page, users=users, sort=sort, t=t, next_exists=next_exists)
return render_template("search_users.html", v=v, query=query, total=total, page=page, users=users, next_exists=next_exists)

View File

@ -28,18 +28,35 @@
</ul>
</div>
<div class="card-body pb-2">
<div class="pl-md-3">
{% if not request.path.startswith('/search/users') %}
<div id="searchparams-dropdown" class="dropdown dropdown-actions">
<button type="button" class="btn btn-secondary dropdown-toggle" id="dropdownMenuButton2" data-bs-toggle="dropdown" data-bs-display="static"><strong>Advanced search parameters (with examples)</strong></button>
<div class="dropdown-menu" x-placement="bottom-start" style="position: absolute; will-change: transform; top: 0px; left: 0px; transform: translate3d(0px, 31px, 0px);">
<div>
<div id="searchparams-dropdown" class="dropdown dropdown-actions">
<button type="button" class="btn btn-secondary dropdown-toggle" id="dropdownMenuButton2" data-bs-toggle="dropdown" data-bs-display="static"><strong>Advanced search parameters (with examples)</strong></button>
<div class="dropdown-menu" x-placement="bottom-start" style="position: absolute; will-change: transform; top: 0px; left: 0px; transform: translate3d(0px, 31px, 0px);">
<div>
<div style="display: inline-block; width: 150px; text-align: center;">Before Date:</div>
<button type="button" data-nonce="{{g.nonce}}" data-onclick="addParam()" class="searchparam mb-1">before:2022-12-30</button>
</div>
<div>
<div style="display: inline-block; width: 150px; text-align: center;">After Date:</div>
<button type="button" data-nonce="{{g.nonce}}" data-onclick="addParam()" class="searchparam mb-1">after:2022-12-30</button>
</div>
{% if not request.path.startswith('/search/users') %}
<div>
<div style="display: inline-block; width: 150px; text-align: center;">Author:</div>
<button type="button" data-nonce="{{g.nonce}}" data-onclick="addParam()" class="searchparam mb-1">author:quadnarca</button>
</div>
{% endif %}
{% if request.path.startswith('/search/messages') %}
<div>
<div style="display: inline-block; width: 150px; text-align: center;">Domain:</div>
<button type="button" data-nonce="{{g.nonce}}" data-onclick="addParam()" class="searchparam mb-1">domain:reddit.com</button>
<div style="display: inline-block; width: 150px; text-align: center;">Sent To:</div>
<button type="button" data-nonce="{{g.nonce}}" data-onclick="addParam()" class="searchparam mb-1">sentto:carpathianflorist</button>
</div>
{% elif request.path.startswith('/search/posts') or request.path.startswith('/search/comments') %}
<div>
<div style="display: inline-block; width: 150px; text-align: center;">{{HOLE_NAME | capitalize }}:</div>
<button type="button" data-nonce="{{g.nonce}}" data-onclick="addParam()" class="searchparam mb-1">{{HOLE_NAME}}:music</button>
</div>
{% if FEATURES['NSFW_MARKING'] %}
<div>
@ -47,35 +64,27 @@
<button type="button" data-nonce="{{g.nonce}}" data-onclick="addParam()" class="searchparam mb-1">over18:true</button>
</div>
{% endif %}
<div>
<div style="display: inline-block; width: 150px; text-align: center;">{{HOLE_NAME | capitalize }}:</div>
<button type="button" data-nonce="{{g.nonce}}" data-onclick="addParam()" class="searchparam mb-1">{{HOLE_NAME}}:music</button>
</div>
<div>
<div style="display: inline-block; width: 150px; text-align: center;">Before Date:</div>
<button type="button" data-nonce="{{g.nonce}}" data-onclick="addParam()" class="searchparam mb-1">before:2022-12-30</button>
</div>
<div>
<div style="display: inline-block; width: 150px; text-align: center;">After Date:</div>
<button type="button" data-nonce="{{g.nonce}}" data-onclick="addParam()" class="searchparam mb-1">after:2022-12-30</button>
</div>
{% if request.path.startswith('/search/comments') %}
<div>
<div style="display: inline-block; width: 150px; text-align: center;">Post ID:</div>
<button type="button" data-nonce="{{g.nonce}}" data-onclick="addParam()" class="searchparam mb-1">post:504</button>
</div>
{% endif %}
{% if request.path.startswith('/search/posts') %}
{% elif request.path.startswith('/search/posts') %}
<div>
<div style="display: inline-block; width: 150px; text-align: center;">Domain:</div>
<button type="button" data-nonce="{{g.nonce}}" data-onclick="addParam()" class="searchparam mb-1">domain:reddit.com</button>
</div>
<div>
<div style="display: inline-block; width: 150px; text-align: center;">Post Title Only:</div>
<button type="button" data-nonce="{{g.nonce}}" data-onclick="addParam()" class="searchparam mb-1">title:true</button>
</div>
{% endif %}
</div>
{% endif %}
</div>
<br>
<script defer src="{{'js/search.js' | asset}}"></script>
{% endif %}
</div>
<br>
<script defer src="{{'js/search.js' | asset}}"></script>
<div class="text-muted text-small mb-1">Showing {% block listinglength %}{{listing | length}}{% endblock %} of {{total}} result{{'s' if total != 1 else ''}} for</div>
<h4 class=" mb-0">{{query}}</h4>