aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authormat <27899617+mat-1@users.noreply.github.com>2021-05-27 16:56:50 -0500
committerGitHub <noreply@github.com>2021-05-27 16:56:50 -0500
commitc9097dc2d0749fa63fd731423bc87a5452f0444d (patch)
tree50afe6b99cf3ed6bd7369e804316c05c7407b5ca /src
parenta3662fb5888bd031b27cc81028ba86b0271eb442 (diff)
downloadskyblock-api-c9097dc2d0749fa63fd731423bc87a5452f0444d.tar.gz
skyblock-api-c9097dc2d0749fa63fd731423bc87a5452f0444d.tar.bz2
skyblock-api-c9097dc2d0749fa63fd731423bc87a5452f0444d.zip
Profile customization (#45)
* add basic discord auth * add uuid dependency * add lastUpdated to sessions * add route to get session from id * add accounts collection * update build * add gm rank * add `customization` url parameter * add customization parameter to /player/:user * add route to get info from discord id * remove a console.log * Update database.js * Update package.json * fix tests
Diffstat (limited to 'src')
-rw-r--r--src/cleaners/rank.ts2
-rw-r--r--src/cleaners/skyblock/member.ts2
-rw-r--r--src/cleaners/skyblock/slayers.ts3
-rw-r--r--src/database.ts65
-rw-r--r--src/discord.ts64
-rw-r--r--src/hypixel.ts34
-rw-r--r--src/hypixelApi.ts2
-rw-r--r--src/index.ts55
8 files changed, 209 insertions, 18 deletions
diff --git a/src/cleaners/rank.ts b/src/cleaners/rank.ts
index 997888c..2d80e31 100644
--- a/src/cleaners/rank.ts
+++ b/src/cleaners/rank.ts
@@ -46,8 +46,8 @@ export function cleanRank({
name = newPackageRank?.replace('_PLUS', '+')
?? packageRank?.replace('_PLUS', '+')
- // MVP++ is called Superstar for some reason
switch (name) {
+ // MVP++ is called Superstar for some reason
case 'SUPERSTAR':
name = 'MVP++'
break
diff --git a/src/cleaners/skyblock/member.ts b/src/cleaners/skyblock/member.ts
index 7a57975..0ec2c0a 100644
--- a/src/cleaners/skyblock/member.ts
+++ b/src/cleaners/skyblock/member.ts
@@ -5,6 +5,7 @@ import { cleanObjectives, Objective } from './objectives'
import { CleanFullProfileBasicMembers } from './profile'
import { cleanProfileStats, StatItem } from './stats'
import { CleanMinion, cleanMinions } from './minions'
+import { AccountCustomization } from '../../database'
import { cleanSlayers, SlayerData } from './slayers'
import { cleanVisitedZones, Zone } from './zones'
import { cleanSkills, Skill } from './skills'
@@ -108,4 +109,5 @@ export interface CleanMemberProfilePlayer extends CleanPlayer {
export interface CleanMemberProfile {
member: CleanMemberProfilePlayer
profile: CleanFullProfileBasicMembers
+ customization: AccountCustomization
}
diff --git a/src/cleaners/skyblock/slayers.ts b/src/cleaners/skyblock/slayers.ts
index e9c5cb6..7892eaf 100644
--- a/src/cleaners/skyblock/slayers.ts
+++ b/src/cleaners/skyblock/slayers.ts
@@ -6,8 +6,7 @@ const SLAYER_NAMES = {
wolf: 'sven'
} as const
-type ApiSlayerName = keyof typeof SLAYER_NAMES
-type SlayerName = (typeof SLAYER_NAMES)[ApiSlayerName]
+type SlayerName = (typeof SLAYER_NAMES)[keyof typeof SLAYER_NAMES]
interface SlayerTier {
tier: number,
diff --git a/src/database.ts b/src/database.ts
index 42fb569..2c679b6 100644
--- a/src/database.ts
+++ b/src/database.ts
@@ -3,17 +3,19 @@
*/
import { categorizeStat, getStatUnit } from './cleaners/skyblock/stats'
-import { CleanBasicProfile, CleanFullProfile, CleanProfile } from './cleaners/skyblock/profile'
+import { CleanFullProfile } from './cleaners/skyblock/profile'
+import { slayerLevels } from './cleaners/skyblock/slayers'
import { CleanMember } from './cleaners/skyblock/member'
import { Collection, Db, MongoClient } from 'mongodb'
import { CleanPlayer } from './cleaners/player'
import * as cached from './hypixelCached'
import * as constants from './constants'
import { shuffle, sleep } from './util'
+import * as discord from './discord'
import NodeCache from 'node-cache'
import Queue from 'queue-promise'
import { debug } from '.'
-import { slayerLevels } from './cleaners/skyblock/slayers'
+import { v4 as uuid4 } from 'uuid'
// don't update the user for 3 minutes
const recentlyUpdated = new NodeCache({
@@ -57,8 +59,33 @@ const reversedLeaderboards = [
let client: MongoClient
let database: Db
+
+interface SessionSchema {
+ _id?: string
+ refresh_token: string
+ discord_user: {
+ id: string
+ name: string
+ }
+ lastUpdated: Date
+}
+
+export interface AccountCustomization {
+ backgroundUrl?: string
+ pack?: string
+}
+
+export interface AccountSchema {
+ _id?: string
+ discordId: string
+ minecraftUuid?: string
+ customization?: AccountCustomization
+}
+
let memberLeaderboardsCollection: Collection<any>
let profileLeaderboardsCollection: Collection<any>
+let sessionsCollection: Collection<SessionSchema>
+let accountsCollection: Collection<AccountSchema>
async function connect(): Promise<void> {
if (!process.env.db_uri)
@@ -69,6 +96,8 @@ async function connect(): Promise<void> {
database = client.db(process.env.db_name)
memberLeaderboardsCollection = database.collection('member-leaderboards')
profileLeaderboardsCollection = database.collection('profile-leaderboards')
+ sessionsCollection = database.collection('sessions')
+ accountsCollection = database.collection('accounts')
}
interface StringNumber {
@@ -620,6 +649,38 @@ async function fetchAllLeaderboards(fast?: boolean): Promise<void> {
if (debug) console.debug('Finished caching leaderboards!')
}
+export async function createSession(refreshToken: string, userData: discord.DiscordUser): Promise<string> {
+ const sessionId = uuid4()
+ await sessionsCollection.insertOne({
+ _id: sessionId,
+ refresh_token: refreshToken,
+ discord_user: {
+ id: userData.id,
+ name: userData.username + '#' + userData.discriminator
+ },
+ lastUpdated: new Date()
+ })
+ return sessionId
+}
+
+export async function fetchSession(sessionId: string): Promise<SessionSchema> {
+ return await sessionsCollection.findOne({ _id: sessionId })
+}
+
+export async function fetchAccount(minecraftUuid: string): Promise<AccountSchema> {
+ return await accountsCollection.findOne({ minecraftUuid })
+}
+
+export async function fetchAccountFromDiscord(discordId: string): Promise<AccountSchema> {
+ return await accountsCollection.findOne({ discordId })
+}
+
+export async function updateAccount(discordId: string, schema: AccountSchema) {
+ await accountsCollection.updateOne({
+ discordId
+ }, { $set: schema }, { upsert: true })
+}
+
// make sure it's not in a test
if (!globalThis.isTest) {
connect().then(() => {
diff --git a/src/discord.ts b/src/discord.ts
new file mode 100644
index 0000000..41f4c6e
--- /dev/null
+++ b/src/discord.ts
@@ -0,0 +1,64 @@
+import fetch from 'node-fetch'
+import { Agent } from 'https'
+
+const DISCORD_CLIENT_ID = '656634948148527107'
+
+const httpsAgent = new Agent({
+ keepAlive: true
+})
+
+export interface TokenResponse {
+ access_token: string
+ expires_in: number
+ refresh_token: string
+ scope: string
+ token_type: string
+}
+
+export interface DiscordUser {
+ id: string
+ username: string
+ avatar: string
+ discriminator: string
+ public_flags: number
+ flags: number
+ locale: string
+ mfa_enabled: boolean
+}
+
+export async function exchangeCode(redirectUri: string, code: string): Promise<TokenResponse> {
+ const API_ENDPOINT = 'https://discord.com/api/v6'
+ const CLIENT_SECRET = process.env.discord_client_secret
+ const data = {
+ 'client_id': DISCORD_CLIENT_ID,
+ 'client_secret': CLIENT_SECRET,
+ 'grant_type': 'authorization_code',
+ 'code': code,
+ 'redirect_uri': redirectUri,
+ 'scope': 'identify'
+ }
+ console.log(new URLSearchParams(data).toString())
+ const fetchResponse = await fetch(
+ API_ENDPOINT + '/oauth2/token',
+ {
+ method: 'POST',
+ agent: () => httpsAgent,
+ headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
+ body: new URLSearchParams(data).toString()
+ }
+ )
+ return await fetchResponse.json()
+}
+
+
+export async function getUser(accessToken: string): Promise<DiscordUser> {
+ const API_ENDPOINT = 'https://discord.com/api/v6'
+ const response = await fetch(
+ API_ENDPOINT + '/users/@me',
+ {
+ headers: {'Authorization': 'Bearer ' + accessToken},
+ agent: () => httpsAgent,
+ }
+ )
+ return response.json()
+}
diff --git a/src/hypixel.ts b/src/hypixel.ts
index 4d3e692..99bc671 100644
--- a/src/hypixel.ts
+++ b/src/hypixel.ts
@@ -2,14 +2,14 @@
* Fetch the clean Hypixel API
*/
-import { CleanPlayer, cleanPlayerResponse } from './cleaners/player'
-import { chooseApiKey, HypixelResponse, sendApiRequest } from './hypixelApi'
-import * as cached from './hypixelCached'
-import { CleanBasicMember, CleanMemberProfile } from './cleaners/skyblock/member'
import { cleanSkyblockProfileResponse, CleanProfile, CleanBasicProfile, CleanFullProfile, CleanFullProfileBasicMembers } from './cleaners/skyblock/profile'
+import { AccountCustomization, AccountSchema, fetchAccount, queueUpdateDatabaseMember, queueUpdateDatabaseProfile } from './database'
+import { CleanBasicMember, CleanMemberProfile } from './cleaners/skyblock/member'
+import { chooseApiKey, HypixelResponse, sendApiRequest } from './hypixelApi'
import { cleanSkyblockProfilesResponse } from './cleaners/skyblock/profiles'
+import { CleanPlayer, cleanPlayerResponse } from './cleaners/player'
+import * as cached from './hypixelCached'
import { debug } from '.'
-import { queueUpdateDatabaseMember, queueUpdateDatabaseProfile } from './database'
export type Included = 'profiles' | 'player' | 'stats' | 'inventories'
@@ -67,6 +67,7 @@ export interface CleanUser {
profiles?: CleanProfile[]
activeProfile?: string
online?: boolean
+ customization?: AccountCustomization
}
@@ -76,11 +77,12 @@ export interface CleanUser {
* @param included lets you choose what is returned, so there's less processing required on the backend
* used inclusions: player, profiles
*/
-export async function fetchUser({ user, uuid, username }: UserAny, included: Included[]=['player']): Promise<CleanUser> {
+export async function fetchUser({ user, uuid, username }: UserAny, included: Included[]=['player'], customization?: boolean): Promise<CleanUser> {
if (!uuid) {
// If the uuid isn't provided, get it
uuid = await cached.uuidFromUser(user || username)
}
+ const websiteAccountPromise = customization ? fetchAccount(uuid) : null
if (!uuid) {
// the user doesn't exist.
if (debug) console.debug('error:', user, 'doesnt exist')
@@ -118,11 +120,16 @@ export async function fetchUser({ user, uuid, username }: UserAny, included: Inc
}
}
}
+ let websiteAccount: AccountSchema = undefined
+
+ if (websiteAccountPromise)
+ websiteAccount = await websiteAccountPromise
return {
player: playerData ?? null,
profiles: profilesData ?? basicProfilesData,
activeProfile: includeProfiles ? activeProfile?.uuid : undefined,
- online: includeProfiles ? lastOnline > (Date.now() - saveInterval): undefined
+ online: includeProfiles ? lastOnline > (Date.now() - saveInterval): undefined,
+ customization: websiteAccount?.customization
}
}
@@ -131,9 +138,12 @@ export async function fetchUser({ user, uuid, username }: UserAny, included: Inc
* This is safe to use many times as the results are cached!
* @param user A username or uuid
* @param profile A profile name or profile uuid
+ * @param customization Whether stuff like the user's custom background will be returned
*/
-export async function fetchMemberProfile(user: string, profile: string): Promise<CleanMemberProfile> {
+export async function fetchMemberProfile(user: string, profile: string, customization: boolean): Promise<CleanMemberProfile> {
const playerUuid = await cached.uuidFromUser(user)
+ // we don't await the promise immediately so it can load while we do other stuff
+ const websiteAccountPromise = customization ? fetchAccount(playerUuid) : null
const profileUuid = await cached.fetchProfileUuid(user, profile)
// if the profile doesn't have an id, just return
@@ -158,6 +168,11 @@ export async function fetchMemberProfile(user: string, profile: string): Promise
cleanProfile.members = simpleMembers
+ let websiteAccount: AccountSchema = undefined
+
+ if (websiteAccountPromise)
+ websiteAccount = await websiteAccountPromise
+
return {
member: {
// the profile name is in member rather than profile since they sometimes differ for each member
@@ -167,7 +182,8 @@ export async function fetchMemberProfile(user: string, profile: string): Promise
// add all other data relating to the hypixel player, such as username, rank, etc
...player
},
- profile: cleanProfile
+ profile: cleanProfile,
+ customization: websiteAccount?.customization
}
}
diff --git a/src/hypixelApi.ts b/src/hypixelApi.ts
index fdb4535..72af1af 100644
--- a/src/hypixelApi.ts
+++ b/src/hypixelApi.ts
@@ -47,7 +47,7 @@ export function chooseApiKey(): string {
keyUsage.remaining = keyUsage.limit
// if this key has more uses remaining than the current known best one, save it
- if (!bestKeyUsage || keyUsage.remaining > bestKeyUsage.remaining) {
+ if (bestKeyUsage === null || keyUsage.remaining > bestKeyUsage.remaining) {
bestKeyUsage = keyUsage
bestKey = key
}
diff --git a/src/index.ts b/src/index.ts
index 1652428..3d6bf6f 100644
--- a/src/index.ts
+++ b/src/index.ts
@@ -1,13 +1,15 @@
-import { fetchAllLeaderboardsCategorized, fetchLeaderboard, fetchMemberLeaderboardSpots } from './database'
+import { createSession, fetchAccount, fetchAccountFromDiscord, fetchAllLeaderboardsCategorized, fetchLeaderboard, fetchMemberLeaderboardSpots, fetchSession, updateAccount } from './database'
import { fetchMemberProfile, fetchUser } from './hypixel'
import rateLimit from 'express-rate-limit'
import * as constants from './constants'
+import * as discord from './discord'
import express from 'express'
const app = express()
export const debug = false
+const mainSiteUrl = 'http://localhost:8081'
// 200 requests over 5 minutes
const limiter = rateLimit({
@@ -22,6 +24,7 @@ const limiter = rateLimit({
})
app.use(limiter)
+app.use(express.json())
app.use((req, res, next) => {
res.setHeader('Access-Control-Allow-Origin', '*')
next()
@@ -35,14 +38,21 @@ app.get('/player/:user', async(req, res) => {
res.json(
await fetchUser(
{ user: req.params.user },
- [req.query.basic as string === 'true' ? undefined : 'profiles', 'player']
+ [req.query.basic as string === 'true' ? undefined : 'profiles', 'player'],
+ req.query.customization as string === 'true'
)
)
})
+app.get('/discord/:id', async(req, res) => {
+ res.json(
+ await fetchAccountFromDiscord(req.params.id)
+ )
+})
+
app.get('/player/:user/:profile', async(req, res) => {
res.json(
- await fetchMemberProfile(req.params.user, req.params.profile)
+ await fetchMemberProfile(req.params.user, req.params.profile, req.query.customization as string === 'true')
)
})
@@ -75,6 +85,45 @@ app.get('/constants', async(req, res) => {
)
})
+app.post('/accounts/createsession', async(req, res) => {
+ try {
+ const { code } = req.body
+ const { access_token: accessToken, refresh_token: refreshToken } = await discord.exchangeCode(`${mainSiteUrl}/loggedin`, code)
+ if (!accessToken)
+ // access token is invalid :(
+ return res.json({ ok: false })
+ const userData = await discord.getUser(accessToken)
+ const sessionId = await createSession(refreshToken, userData)
+ res.json({ ok: true, session_id: sessionId })
+ } catch (err) {
+ res.json({ ok: false })
+ }
+})
+
+app.post('/accounts/session', async(req, res) => {
+ try {
+ const { uuid } = req.body
+ const session = await fetchSession(uuid)
+ const account = await fetchAccountFromDiscord(session.discord_user.id)
+ res.json({ session, account })
+ } catch (err) {
+ console.error(err)
+ res.json({ ok: false })
+ }
+})
+
+
+app.post('/accounts/update', async(req, res) => {
+ // it checks against the key, so it's kind of secure
+ if (req.headers.key !== process.env.key) return console.log('bad key!')
+ try {
+ await updateAccount(req.body.discordId, req.body)
+ res.json({ ok: true })
+ } catch (err) {
+ console.error(err)
+ res.json({ ok: false })
+ }
+})
// only run the server if it's not doing tests
if (!globalThis.isTest)