Skip to content

Commit

Permalink
feat: filter-servers (#564)
Browse files Browse the repository at this point in the history
* chore: update query

* feat: filter outdated servers
  • Loading branch information
skinmaker1345 authored May 29, 2023
1 parent 2b3fe7d commit 013b9b7
Showing 1 changed file with 16 additions and 8 deletions.
24 changes: 16 additions & 8 deletions utils/Query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,9 @@ async function getServer(id: string, topLevel=true): Promise<Server> {
.orWhereRaw(`(flags & ${ServerFlags.partnered}) and vanity=?`, [id])
if (res[0]) {
const data = await getServerData(res[0].id)
if(!data || (+new Date() - +new Date(data.updatedAt)) > 3 * 60 * 1000) res[0].state = 'unreachable'
else {
if(!data || (+new Date() - +new Date(data.updatedAt)) > 3 * 60 * 1000) {
if(res[0].state === 'ok') res[0].state = 'unreachable'
} else {
res[0].flags = res[0].flags | (data.features.includes(GuildFeature.Partnered) && ServerFlags.discord_partnered) | (data.features.includes(GuildFeature.Verified) && ServerFlags.verified)
if(res[0].owners !== JSON.stringify([data.owner, ...data.admins]) || res[0].name !== data.name)
await knex('servers').update({ name: data.name, owners: JSON.stringify([data.owner, ...data.admins]) })
Expand Down Expand Up @@ -245,8 +246,8 @@ async function getBotList(type: ListType, page = 1, query?: string):Promise<List
.select(['id'])
} else if (type === 'SEARCH') {
if (!query) throw new Error('쿼리가 누락되었습니다.')
count = (await knex.raw('SELECT count(*) FROM bots WHERE MATCH(`name`, `intro`, `desc`) AGAINST(? in boolean mode)', [decodeURI(query) + '*']))[0][0]['count(*)']
res = (await knex.raw('SELECT id, votes, MATCH(`name`, `intro`, `desc`) AGAINST(? in boolean mode) as relevance FROM bots WHERE MATCH(`name`, `intro`, `desc`) AGAINST(? in boolean mode) ORDER BY relevance DESC, votes DESC LIMIT 16 OFFSET ?', [decodeURI(query) + '*', decodeURI(query) + '*', ((page ? Number(page) : 1) - 1) * 16]))[0]
count = (await knex.raw('SELECT count(*) FROM bots WHERE `state` != "blocked" AND MATCH(`name`, `intro`, `desc`) AGAINST(? in boolean mode)', [decodeURI(query) + '*']))[0][0]['count(*)']
res = (await knex.raw('SELECT id, votes, MATCH(`name`, `intro`, `desc`) AGAINST(? in boolean mode) as relevance FROM bots WHERE `state` != "blocked" AND MATCH(`name`, `intro`, `desc`) AGAINST(? in boolean mode) ORDER BY relevance DESC, votes DESC LIMIT 16 OFFSET ?', [decodeURI(query) + '*', decodeURI(query) + '*', ((page ? Number(page) : 1) - 1) * 16]))[0]
} else {
count = 1
res = []
Expand All @@ -265,12 +266,14 @@ async function getServerList(type: ListType, page = 1, query?: string):Promise<L
.limit(16)
.offset(((page ? Number(page) : 1) - 1) * 16)
.select(['id'])
.where('last_updated', '>=', new Date(Date.now() - 24 * 60 * 60 * 1000).toISOString())
.whereNot({ state: 'blocked' })
} else if (type === 'TRUSTED') {
count = (
await knex('servers')
.whereRaw(`flags & ${ServerFlags.trusted}`)
.count()
.where('last_updated', '>=', new Date(Date.now() - 24 * 60 * 60 * 1000).toISOString())
.whereNot({ state: 'blocked' })
)[0]['count(*)']
res = await knex('servers').whereNot({ state: 'blocked' })
Expand All @@ -279,6 +282,7 @@ async function getServerList(type: ListType, page = 1, query?: string):Promise<L
.limit(16)
.offset(((page ? Number(page) : 1) - 1) * 16)
.select(['id'])
.where('last_updated', '>=', new Date(Date.now() - 24 * 60 * 60 * 1000).toISOString())
.whereNot({ state: 'blocked' })
} else if (type === 'NEW') {
count = (
Expand All @@ -290,16 +294,17 @@ async function getServerList(type: ListType, page = 1, query?: string):Promise<L
.limit(16)
.offset(((page ? Number(page) : 1) - 1) * 16)
.select(['id'])
.where('last_updated', '>=', new Date(Date.now() - 24 * 60 * 60 * 1000).toISOString())
.whereNot({ state: 'blocked' })
} else if (type === 'PARTNERED') {
count = (
await knex('servers')
.whereRaw(`flags & ${ServerFlags.partnered}`)
.whereRaw(`flags & ${ServerFlags.partnered} AND last_updated >= ?`, [new Date(Date.now() - 24 * 60 * 60 * 1000).toISOString()])
.andWhereNot({ state: 'blocked' })
.count()
)[0]['count(*)']
res = await knex('servers')
.whereRaw(`flags & ${ServerFlags.partnered}`)
.whereRaw(`flags & ${ServerFlags.partnered} AND last_updated >= ?`, [new Date(Date.now() - 24 * 60 * 60 * 1000).toISOString()])
.andWhereNot({ state: 'blocked' })
.orderByRaw('RAND()')
.limit(16)
Expand All @@ -310,21 +315,23 @@ async function getServerList(type: ListType, page = 1, query?: string):Promise<L
if (!serverCategories.includes(query)) throw new Error('알 수 없는 카테고리입니다.')
count = (
await knex('servers')
.where('last_updated', '>=', new Date(Date.now() - 24 * 60 * 60 * 1000).toISOString())
.where('category', 'like', `%${decodeURI(query)}%`)
.andWhereNot({ state: 'blocked' })
.count()
)[0]['count(*)']
res = await knex('servers')
.where('category', 'like', `%${decodeURI(query)}%`)
.where('last_updated', '>=', new Date(Date.now() - 24 * 60 * 60 * 1000).toISOString())
.andWhereNot({ state: 'blocked' })
.orderBy('votes', 'desc')
.limit(16)
.offset(((page ? Number(page) : 1) - 1) * 16)
.select(['id'])
} else if (type === 'SEARCH') {
if (!query) throw new Error('쿼리가 누락되었습니다.')
count = (await knex.raw('SELECT count(*) FROM servers WHERE MATCH(`name`, `intro`, `desc`) AGAINST(? in boolean mode)', [decodeURI(query) + '*']))[0][0]['count(*)']
res = (await knex.raw('SELECT id, votes, MATCH(`name`, `intro`, `desc`) AGAINST(? in boolean mode) as relevance FROM servers WHERE MATCH(`name`, `intro`, `desc`) AGAINST(? in boolean mode) ORDER BY relevance DESC, votes DESC LIMIT 16 OFFSET ?', [decodeURI(query) + '*', decodeURI(query) + '*', ((page ? Number(page) : 1) - 1) * 16]))[0]
count = (await knex.raw('SELECT count(*) FROM servers WHERE `state` != "blocked" AND last_updated >= ? AND MATCH(`name`, `intro`, `desc`) AGAINST(? in boolean mode)', [new Date(Date.now() - 24 * 60 * 60 * 1000).toISOString(), decodeURI(query) + '*']))[0][0]['count(*)']
res = (await knex.raw('SELECT id, votes, MATCH(`name`, `intro`, `desc`) AGAINST(? in boolean mode) as relevance FROM servers WHERE `state` != "blocked" AND last_updated >= ? AND MATCH(`name`, `intro`, `desc`) AGAINST(? in boolean mode) ORDER BY relevance DESC, votes DESC LIMIT 16 OFFSET ?', [decodeURI(query) + '*', new Date(Date.now() - 24 * 60 * 60 * 1000).toISOString(), decodeURI(query) + '*', ((page ? Number(page) : 1) - 1) * 16]))[0]
} else {
count = 1
res = []
Expand Down Expand Up @@ -470,6 +477,7 @@ async function submitServer(userID: string, id: string, data: AddServerSubmit):
category: JSON.stringify(data.category),
invite: data.invite,
token: sign({ id }),
last_updated: new Date().toISOString()
})
get.server.clear(id)
return true
Expand Down

0 comments on commit 013b9b7

Please sign in to comment.