File size: 1,746 Bytes
38d787b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
"use server"

import { WhoAmIUser, whoAmI } from "@/huggingface/hub/src"
import {  UserInfo } from "@/types"
import { adminApiKey } from "./config"
import { redis } from "./redis"

export async function getCurrentUser(apiKey: string): Promise<UserInfo> {
  if (!apiKey) {
    throw new Error(`the apiKey is required`)
  }

  const credentials = { accessToken: apiKey }
  
  const huggingFaceUser = await whoAmI({ credentials }) as unknown as WhoAmIUser

  const id = huggingFaceUser.id

  const user: UserInfo = {
    id,
    type: apiKey === adminApiKey ? "admin" : "normal",
    userName: huggingFaceUser.name,
    fullName: huggingFaceUser.fullname,
    thumbnail: huggingFaceUser.avatarUrl,
    channels: [],
    hfApiToken: apiKey,
  }
  
  await redis.set(`users:${id}`, user)
  
  return user
}

export async function getUser(id: string): Promise<UserInfo | undefined> {
  const maybeUser = await redis.get<UserInfo>(id)

  if (maybeUser?.id) {
    const publicFacingUser: UserInfo = {
      ...maybeUser,
      hfApiToken: undefined, // <-- important!
    }
    delete publicFacingUser.hfApiToken
    return publicFacingUser
  }

  return undefined
}

export async function getUsers(ids: string[]): Promise<Record<string, UserInfo>> {
  try {
    const maybeUsers = await redis.mget<UserInfo[]>(ids.map(userId => `users:${userId}`))

    const usersById: Record<string, UserInfo> = {}

    maybeUsers.forEach((user, index) => {
      if (user?.id) {
        const publicFacingUser: UserInfo = {
          ...user,
          hfApiToken: undefined, // <-- important!
        }
        delete publicFacingUser.hfApiToken
        usersById[user.id] = publicFacingUser
      }
    })

    return usersById
  } catch (err) {
    return {}
  }
}