File size: 1,397 Bytes
eb29a95
17aecfb
8fce765
 
98b0aa6
 
 
 
8fce765
 
 
 
 
 
b0186cb
8fce765
 
 
98b0aa6
b34e9b1
 
1c0590e
b34e9b1
17aecfb
 
8fce765
17aecfb
eb29a95
17aecfb
 
 
b0186cb
17aecfb
1c0590e
 
17aecfb
 
b1a4d81
 
8fce765
b1a4d81
eb29a95
b1a4d81
 
 
b34e9b1
98b0aa6
 
17aecfb
98b0aa6
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import { json, type RequestEvent } from '@sveltejs/kit';
import prisma from '$lib/prisma';

import { tokenIsAvailable } from '$lib/utils';

/** @type {import('./$types').RequestHandler} */

export async function GET(request : RequestEvent) {
  const token = request.cookies.get('hf_access_token')
  let IS_ADMIN = !!token

  if (token) {
    const user = await tokenIsAvailable(token)
    if (user) {
      IS_ADMIN = process?.env?.SECRET_HF_ADMIN ? process?.env?.SECRET_HF_ADMIN.includes(user.sub) : false
    }
  }

  const page = parseInt(request.url.searchParams.get('page') || '0')
  const filter = request.url.searchParams.get('filter') || 'hotest'
  const search = request.url.searchParams.get('search') || ''
  const limit = parseInt(request.url.searchParams.get('limit') || '20')

  const cards = await prisma.model.findMany({
    where: {
      ...(IS_ADMIN ? {} : { isPublic: true }),
      OR: [
        { id: { contains: search } },
      ]
    },
    orderBy: {
      ...(filter === 'hotest' ? { downloads: 'desc' } : filter === 'likes' ? { likes: 'desc' } : { createdAt: 'desc' })
    },
    skip: limit * page,
    take: limit,
  })

  const total_reposId = await prisma.model.count({
    where: {
      ...(IS_ADMIN ? {} : { isPublic: true }),
      OR: [
        { id: { contains: search } },
      ]
    },
  })

  return json({
    cards,
    total_items: total_reposId
  })
}