fix random

This commit is contained in:
Flummi 2022-01-02 14:37:44 +01:00
parent d781290430
commit 0a983b140e
2 changed files with 36 additions and 20 deletions

View File

@ -107,18 +107,20 @@ export default {
.limit(cfg.websrv.eps); .limit(cfg.websrv.eps);
} }
if(tag) rows = rows if(tag) {
.innerJoin( rows = rows
sql("tags") .innerJoin(
.select("tags_assign.item_id", "tags.tag") sql("tags")
.leftJoin("tags_assign", "tags_assign.tag_id", "tags.id") .select("tags_assign.item_id", "tags.tag")
.where("tags.tag", "ilike", "%"+tag+"%") .leftJoin("tags_assign", "tags_assign.tag_id", "tags.id")
.groupBy("tags_assign.item_id", "tags.tag") .where("tags.tag", "ilike", "%"+tag+"%")
.as("st"), .groupBy("tags_assign.item_id", "tags.tag")
"st.item_id", "items.id" .as("st"),
) "st.item_id", "items.id"
.groupBy('st.item_id'); )
.groupBy('st.item_id', 'items.id', 'tags_assign.tag_id');
}
rows = await rows; rows = await rows;
@ -183,7 +185,6 @@ export default {
.leftJoin('user', 'user.id', 'favorites.user_id') .leftJoin('user', 'user.id', 'favorites.user_id')
.whereRaw(modequery) .whereRaw(modequery)
.andWhere('user.user', 'ilike', user) .andWhere('user.user', 'ilike', user)
.groupBy('items.id');
} }
else { else {
items = sql("items") items = sql("items")
@ -282,7 +283,22 @@ export default {
const modequery = mime == "audio" ? lib.getMode(0) : lib.getMode(o.mode ?? 0); const modequery = mime == "audio" ? lib.getMode(0) : lib.getMode(o.mode ?? 0);
let item = sql("items").select("*").orderByRaw("random()").whereRaw(modequery); let item;
if(o.fav) {
item = sql("favorites")
.select('items.*')
.leftJoin('items', 'items.id', 'favorites.item_id')
.leftJoin('user', 'user.id', 'favorites.user_id')
.whereRaw(modequery)
.orderByRaw("random()")
.andWhere('user.user', 'ilike', user);
}
else {
item = sql("items")
.select("*")
.orderByRaw("random()")
.whereRaw(modequery);
}
if(tag) { if(tag) {
item = item.innerJoin( item = item.innerJoin(
@ -290,11 +306,11 @@ export default {
.select("tags_assign.item_id", "tags.tag") .select("tags_assign.item_id", "tags.tag")
.leftJoin("tags_assign", "tags_assign.tag_id", "tags.id") .leftJoin("tags_assign", "tags_assign.tag_id", "tags.id")
.where("tags.tag", "ilike", "%"+tag+"%") .where("tags.tag", "ilike", "%"+tag+"%")
.groupBy("tags_assign.item_id") .groupBy("tags_assign.item_id", "tags.tag")
.as("st"), .as("st"),
"st.item_id", "items.id" "st.item_id", "items.id"
) )
.groupBy('st.item_id'); .groupBy('st.item_id', 'st.tag', 'items.id');
} }
if(user) if(user)
item = item.andWhere("username", "ilike", "%" + user + "%"); item = item.andWhere("username", "ilike", "%" + user + "%");
@ -310,7 +326,7 @@ export default {
}; };
} }
const link = lib.genLink({ user, tag, mime }); const link = lib.genLink({ user, tag, mime, type: o.fav ? 'favs' : 'f0cks' });
return { return {
success: true, success: true,

View File

@ -14,12 +14,12 @@ export default async bot => {
for(let i = 0; i < args.length; i++) { for(let i = 0; i < args.length; i++) {
if(args[i].charAt(0) === "!") if(args[i].charAt(0) === "!")
rows = rows.where("username", "not like", args[i].slice(1)); rows = rows.where("username", "not ilike", args[i].slice(1));
else else
rows = rows.where("username", "like", args[i]); rows = rows.where("username", "ilike", args[i]);
} }
rows = await rows.orderByRaw("rand()").limit(1); rows = await rows.orderByRaw("random()").limit(1);
if(rows.length === 0) if(rows.length === 0)
return e.reply("nothing found, f0cker"); return e.reply("nothing found, f0cker");