search lol

This commit is contained in:
Flummi 2022-05-06 17:02:03 +02:00
parent 2162f1565c
commit f5737e6f49
3 changed files with 90 additions and 29 deletions

View File

@ -476,6 +476,11 @@ html, body {
font-size: 14px; font-size: 14px;
height: 100%; height: 100%;
} }
@supports (-moz-appearance:none) {
html, body {
height: auto !important;
}
}
.noscript-badge { .noscript-badge {
display: block; display: block;
@ -571,6 +576,7 @@ div#posts > a:hover::after {
} }
.navbar { .navbar {
position: -webkit-sticky;
position: sticky; position: sticky;
top: 0; top: 0;
padding: 0; padding: 0;
@ -1533,7 +1539,6 @@ table.table {
min-width: max-content; min-width: max-content;
} }
table.table thead tr { table.table thead tr {
text-align: left;
font-weight: bolder; font-weight: bolder;
border-bottom: 1px solid var(--accent); border-bottom: 1px solid var(--accent);
} }
@ -1542,6 +1547,7 @@ table.table tr {
} }
table.table th, table.table td { table.table th, table.table td {
padding: 7px 15px; padding: 7px 15px;
text-align: center;
} }
table.table tbody tr:nth-of-type(odd) { table.table tbody tr:nth-of-type(odd) {
background-color: var(--badge-tag); background-color: var(--badge-tag);

View File

@ -2,33 +2,81 @@ import db from "../sql.mjs";
import lib from "../lib.mjs"; import lib from "../lib.mjs";
import search from "../routeinc/search.mjs"; import search from "../routeinc/search.mjs";
const _eps = 20;
export default (router, tpl) => { export default (router, tpl) => {
router.get(/^\/search(\/)?$/, lib.auth, async (req, res) => { router.get(/^\/search(\/)?$/, lib.auth, async (req, res) => {
let ret; let ret;
let tag = req.url.qs?.tag; let tag = req.url.qs.tag ?? [];
if(Object.keys(req.url.qs).length > 0) { let page = req.url.qs.page ?? 1;
let rows; let total = 0;
let pagination, link;
if(tag.length > 1) {
if(tag.startsWith('src:')) {
total = (await db`
select count(*) as total
from "items"
where src ilike ${'%' + tag.substring(4) + '%'}
group by "items".id, "tags".tag
`).length;
}
else {
total = (await db`
select count(*) as total
from "tags"
left join "tags_assign" on "tags_assign".tag_id = "tags".id
left join "items" on "items".id = "tags_assign".item_id
where "tags".tag ilike ${'%' + tag + '%'}
group by "items".id, "tags".tag
`).length;
}
const pages = +Math.ceil(total / _eps);
const act_page = Math.min(pages, page || 1);
const offset = Math.max(0, (act_page - 1) * _eps);
if(tag.startsWith('src:')) { if(tag.startsWith('src:')) {
tag = tag.substring(4);
ret = await db` ret = await db`
select * select *
from "items" from "items"
where src ilike ${'%' + tag + '%'} where src ilike ${'%' + tag.substring(4) + '%'}
limit 500 group by "items".id, "tags".tag
offset ${offset}
limit ${_eps}
`; `;
} }
else { else {
rows = await db` const rows = await db`
select "items".id, "items".username, "items".mime, "tags".tag select "items".id, "items".username, "items".mime, "tags".tag
from "tags" from "tags"
left join "tags_assign" on "tags_assign".tag_id = "tags".id left join "tags_assign" on "tags_assign".tag_id = "tags".id
left join "items" on "items".id = "tags_assign".item_id left join "items" on "items".id = "tags_assign".item_id
where "tags".tag ilike ${'%' + tag + '%'} where "tags".tag ilike ${'%' + tag + '%'}
limit 500 group by "items".id, "tags".tag
offset ${offset}
limit ${_eps}
`; `;
ret = search(rows, tag); ret = search(rows, tag);
} }
const cheat = [];
for(let i = Math.max(1, act_page - 3); i <= Math.min(act_page + 3, pages); i++)
cheat.push(i);
pagination = {
start: 1,
end: pages,
prev: (act_page > 1) ? act_page - 1 : null,
next: (act_page < pages) ? act_page + 1 : null,
page: act_page,
cheat: cheat,
uff: false
};
link = {
main: `/search/?tag=${tag}`,
path: '&page='
};
} }
res.reply({ res.reply({
@ -36,7 +84,10 @@ export default (router, tpl) => {
result: ret, result: ret,
totals: await lib.countf0cks(), totals: await lib.countf0cks(),
searchstring: tag, searchstring: tag,
tmp: null count: total,
tmp: null,
pagination,
link
}, req) }, req)
}); });
}); });

View File

@ -5,26 +5,30 @@
</form> </form>
<div class="results"> <div class="results">
@if(result) @if(result)
<h1>{{ result.length }} f0cks given:</h1> <h1>{{ count }} f0cks given (page {{ pagination.page }} of {{ pagination.end }}):</h1>
<table style="width: 100%;" class="table"> <table style="width: 100%" class="table">
<tr> <thead>
<th>Thumbnail</th> <tr>
<th>ID</th> <th>Thumbnail</th>
<th>Tag</th> <th>ID</th>
<th>Mime</th> <th>Tag</th>
<th>Username</th> <th>Mime</th>
<th>Score</th> <th>Username</th>
</tr> <th>Score</th>
</tr>
</thead>
<tbody>
@each(result as line) @each(result as line)
<tr> <tr>
<td style="width: 128px;"><a href="/tag/{!! line.tag !!}/{{ line.id }}" target="_blank"><img src="/t/{{ line.id }}.webp" /></a></td> <td style="width: 128px;"><a href="/tag/{!! line.tag !!}/{{ line.id }}" target="_blank"><img src="/t/{{ line.id }}.webp" /></a></td>
<td style="text-align: center;"><a href="/tag/{!! line.tag !!}/{{ line.id }}" target="_blank">{{ line.id }}</a></td> <td><a href="/tag/{!! line.tag !!}/{{ line.id }}" target="_blank">{{ line.id }}</a></td>
<td style="text-align: center;"><a href="/tag/{!! line.tag !!}">{!! line.tag !!}</a></td> <td><a href="/tag/{!! line.tag !!}">{!! line.tag !!}</a></td>
<td style="text-align: center;">{{ line.mime }}</td> <td>{{ line.mime }}</td>
<td style="text-align: center;"><a href="/user/{!! line.username !!}/f0cks/{{ line.id }}">{!! line.username !!}</a></td> <td><a href="/user/{!! line.username !!}/f0cks/{{ line.id }}">{!! line.username !!}</a></td>
<td style="text-align: center;">{{ line.score.toFixed(2) }}</td> <td>{{ line.score.toFixed(2) }}</td>
</tr> </tr>
@endeach @endeach
</tbody>
</table> </table>
@endif @endif
</div> </div>