mirror of
https://github.com/PreMiD/PreMiD.git
synced 2026-04-06 04:41:58 +02:00
Compare commits
65 Commits
api-worker
...
api-worker
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
416b65f0d4 | ||
|
|
f8e9fc832d | ||
|
|
86b0f07216 | ||
|
|
9eb5c03877 | ||
|
|
e63e1270aa | ||
|
|
f730e71bbf | ||
|
|
8b68bf85c8 | ||
|
|
e4c794a9ad | ||
|
|
6e8258d76f | ||
|
|
56b796c621 | ||
|
|
0de59c48b4 | ||
|
|
60056e069d | ||
|
|
b6bad90919 | ||
|
|
ee21bb9dec | ||
|
|
6efac4fef1 | ||
|
|
93424793bd | ||
|
|
affcb6a0cf | ||
|
|
bb56949dfb | ||
|
|
c06fe04b65 | ||
|
|
ef976341ba | ||
|
|
38893891af | ||
|
|
63eeeefda7 | ||
|
|
056db21cb0 | ||
|
|
d8dc08c6c3 | ||
|
|
634391b6e3 | ||
|
|
c46cf6975a | ||
|
|
68c6b4fcdc | ||
|
|
55fa07d5b5 | ||
|
|
903c238b33 | ||
|
|
acd9afb2b1 | ||
|
|
4bd42390eb | ||
|
|
c014504464 | ||
|
|
24fe349b60 | ||
|
|
ee5428ce08 | ||
|
|
e4b1010160 | ||
|
|
34c42d59ed | ||
|
|
d9267361aa | ||
|
|
0d5382fd50 | ||
|
|
e9015b1204 | ||
|
|
cea36426ab | ||
|
|
48c141094e | ||
|
|
e67fb97e14 | ||
|
|
0bd0d759f6 | ||
|
|
60b7f63409 | ||
|
|
78b482be4f | ||
|
|
9db9e931b6 | ||
|
|
665263e9b5 | ||
|
|
60257dbe53 | ||
|
|
411a70f567 | ||
|
|
4d1b092ee5 | ||
|
|
aa41f1cdae | ||
|
|
04b5d54697 | ||
|
|
fb096bc4be | ||
|
|
6e9e4ae1b6 | ||
|
|
d8f73202b9 | ||
|
|
b175a08dce | ||
|
|
2284ee94ad | ||
|
|
78a3311342 | ||
|
|
a1fabd3fd6 | ||
|
|
93c62cc38f | ||
|
|
8553613593 | ||
|
|
bf83dc4452 | ||
|
|
91bf2237c2 | ||
|
|
ae9b579e84 | ||
|
|
2488d98ede |
8
.github/workflows/cd.yaml
vendored
8
.github/workflows/cd.yaml
vendored
@@ -39,7 +39,7 @@ jobs:
|
||||
|
||||
- name: Build and Push website
|
||||
uses: docker/build-push-action@v6
|
||||
if: matrix.target == 'website'
|
||||
if: github.ref_type == 'branch'
|
||||
with:
|
||||
push: true
|
||||
platforms: linux/amd64,linux/arm64
|
||||
@@ -50,14 +50,14 @@ jobs:
|
||||
tags: ghcr.io/premid/${{ matrix.target }}:beta-${{ github.sha }}-${{ github.run_number }}
|
||||
|
||||
- name: Get package.json version
|
||||
if: matrix.target != 'website'
|
||||
id: get_version
|
||||
run: echo ::set-output name=version::$(node -p "require('./apps/${{ matrix.target }}/package.json').version")
|
||||
run: echo ::set-output name=version::$(echo "${GITHUB_REF##*/}" | sed -E 's/^.+-v([0-9]+\.[0-9]+\.[0-9]+)$/\1/')
|
||||
shell: bash
|
||||
if: startsWith(github.ref, 'refs/tags/')
|
||||
|
||||
- name: Build and push other images
|
||||
uses: docker/build-push-action@v6
|
||||
if: matrix.target != 'website' && startsWith(github.ref, 'refs/tags/')
|
||||
if: startsWith(github.ref, 'refs/tags/')
|
||||
with:
|
||||
push: true
|
||||
platforms: linux/amd64,linux/arm64
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
{
|
||||
"name": "@premid/api-master",
|
||||
"type": "module",
|
||||
"version": "0.0.1",
|
||||
"version": "0.0.22",
|
||||
"private": true,
|
||||
"description": "PreMiD's api master",
|
||||
"license": "MPL-2.0",
|
||||
@@ -14,10 +14,18 @@
|
||||
"dev": "node --watch --env-file .env --enable-source-maps ."
|
||||
},
|
||||
"dependencies": {
|
||||
"@discordjs/rest": "^2.3.0",
|
||||
"@envelop/sentry": "^9.0.0",
|
||||
"@opentelemetry/api": "^1.9.0",
|
||||
"@opentelemetry/exporter-prometheus": "^0.52.1",
|
||||
"@opentelemetry/node": "^0.24.0",
|
||||
"@sentry/node": "^8.17.0",
|
||||
"cron": "^3.1.7",
|
||||
"ioredis": "^5.3.2"
|
||||
"debug": "^4.3.6",
|
||||
"ioredis": "^5.3.2",
|
||||
"ky": "^1.7.2",
|
||||
"p-limit": "^6.1.0"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@types/debug": "^4.1.12"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,35 +1,105 @@
|
||||
import { REST } from "@discordjs/rest";
|
||||
import { redis } from "../index.js";
|
||||
import pLimit from "p-limit";
|
||||
import ky, { HTTPError, TimeoutError } from "ky";
|
||||
import { mainLog, redis } from "../index.js";
|
||||
|
||||
export async function clearOldSesssions() {
|
||||
const sessions = await redis.hgetall("pmd-api.sessions");
|
||||
const now = Date.now();
|
||||
|
||||
for (const [key, value] of Object.entries(sessions)) {
|
||||
const session = JSON.parse(value) as {
|
||||
token: string;
|
||||
session: string;
|
||||
lastUpdated: number;
|
||||
};
|
||||
|
||||
// ? If the session is younger than 30seconds, skip it
|
||||
if (now - session.lastUpdated < 30000)
|
||||
continue;
|
||||
|
||||
// ? Delete the session
|
||||
try {
|
||||
const discord = new REST({ version: "10", authPrefix: "Bearer" });
|
||||
discord.setToken(session.token);
|
||||
await discord.post("/users/@me/headless-sessions/delete", {
|
||||
body: {
|
||||
token: session.session,
|
||||
},
|
||||
});
|
||||
}
|
||||
catch (error) {
|
||||
console.error(error);
|
||||
}
|
||||
|
||||
await redis.hdel("pmd-api.sessions", key);
|
||||
let inProgress = false;
|
||||
export async function clearOldSessions() {
|
||||
if (inProgress) {
|
||||
mainLog("Session cleanup already in progress");
|
||||
return;
|
||||
}
|
||||
|
||||
inProgress = true;
|
||||
const now = Date.now();
|
||||
let cursor = "0";
|
||||
let totalSessions = 0;
|
||||
let cleared = 0;
|
||||
const batchSize = 100;
|
||||
let keysToDelete: string[] = [];
|
||||
|
||||
mainLog("Starting session cleanup");
|
||||
|
||||
const limit = pLimit(100); // Create a limit of 100 concurrent operations
|
||||
|
||||
do {
|
||||
const [nextCursor, result] = await redis.hscan("pmd-api.sessions", cursor, "COUNT", batchSize);
|
||||
cursor = nextCursor;
|
||||
totalSessions += result.length / 2;
|
||||
|
||||
const deletePromises = [];
|
||||
|
||||
for (let i = 0; i < result.length; i += 2) {
|
||||
const key = result[i];
|
||||
const value = result[i + 1];
|
||||
|
||||
if (!key || !value) {
|
||||
continue;
|
||||
}
|
||||
|
||||
const session = JSON.parse(value) as {
|
||||
token: string;
|
||||
session: string;
|
||||
lastUpdated: number;
|
||||
};
|
||||
|
||||
if (now - session.lastUpdated < 30000)
|
||||
continue;
|
||||
|
||||
deletePromises.push(limit(() => deleteSession(session, key)));
|
||||
}
|
||||
|
||||
const results = await Promise.allSettled(deletePromises);
|
||||
results.forEach((result) => {
|
||||
if (result.status === "fulfilled" && result.value) {
|
||||
keysToDelete.push(result.value);
|
||||
cleared++;
|
||||
}
|
||||
});
|
||||
|
||||
if (keysToDelete.length >= batchSize) {
|
||||
await redis.hdel("pmd-api.sessions", ...keysToDelete);
|
||||
keysToDelete = [];
|
||||
}
|
||||
} while (cursor !== "0");
|
||||
|
||||
if (keysToDelete.length > 0) {
|
||||
await redis.hdel("pmd-api.sessions", ...keysToDelete);
|
||||
}
|
||||
|
||||
if (totalSessions === 0) {
|
||||
mainLog("No sessions to clear");
|
||||
}
|
||||
else {
|
||||
mainLog(`Checked ${totalSessions} sessions, cleared ${cleared}`);
|
||||
}
|
||||
|
||||
inProgress = false;
|
||||
}
|
||||
|
||||
async function deleteSession(session: { token: string; session: string }, key: string): Promise<string> {
|
||||
try {
|
||||
await ky.post("https://discord.com/api/v10/users/@me/headless-sessions/delete", {
|
||||
json: {
|
||||
token: session.session,
|
||||
},
|
||||
headers: {
|
||||
Authorization: `Bearer ${session.token}`,
|
||||
},
|
||||
retry: 3,
|
||||
timeout: 5000,
|
||||
});
|
||||
}
|
||||
catch (error) {
|
||||
if (error instanceof TimeoutError) {
|
||||
mainLog(`Session deletion aborted due to timeout for key ${key}`);
|
||||
}
|
||||
else if (error instanceof HTTPError) {
|
||||
mainLog(`Failed to delete session for key ${key}: [${error.name}] ${error.message} ${JSON.stringify(await error.response.json())}`);
|
||||
}
|
||||
else {
|
||||
mainLog(`Failed to delete session for key ${key}: Unknown error`);
|
||||
}
|
||||
}
|
||||
|
||||
return key;
|
||||
}
|
||||
|
||||
13
apps/api-master/src/functions/setCounter.ts
Normal file
13
apps/api-master/src/functions/setCounter.ts
Normal file
@@ -0,0 +1,13 @@
|
||||
import { redis } from "../index.js";
|
||||
import { counter } from "../tracing.js";
|
||||
|
||||
let activeActivities = 0;
|
||||
counter.add(0);
|
||||
export async function setCounter() {
|
||||
const length = await redis.hlen("pmd-api.sessions");
|
||||
if (length === activeActivities)
|
||||
return;
|
||||
const diff = length - activeActivities;
|
||||
activeActivities = length;
|
||||
counter.add(diff);
|
||||
}
|
||||
40
apps/api-master/src/functions/updateActivePresenceGauge.ts
Normal file
40
apps/api-master/src/functions/updateActivePresenceGauge.ts
Normal file
@@ -0,0 +1,40 @@
|
||||
import { redis } from "../index.js";
|
||||
import { activePresenceGauge } from "../tracing.js";
|
||||
|
||||
//* Track previously recorded services
|
||||
const previousServices = new Set<string>();
|
||||
|
||||
//* Function to update the gauge with per-service counts
|
||||
export async function updateActivePresenceGauge() {
|
||||
const pattern = "pmd-api.heartbeatUpdates.*.*";
|
||||
let cursor: string = "0";
|
||||
const serviceCounts = new Map<string, number>();
|
||||
|
||||
do {
|
||||
const [newCursor, keys] = await redis.scan(cursor, "MATCH", pattern, "COUNT", 1000); //* Use SCAN with COUNT for memory efficiency
|
||||
cursor = newCursor;
|
||||
for (const key of keys) {
|
||||
const parts = key.split(".");
|
||||
const service = parts[parts.length - 1]!;
|
||||
const hash = await redis.hgetall(key);
|
||||
const version = hash.version; //* Get version from hash
|
||||
serviceCounts.set(`${service}:${version}`, (serviceCounts.get(`${service}:${version}`) || 0) + 1);
|
||||
}
|
||||
} while (cursor !== "0");
|
||||
|
||||
// Set current counts and remove from previousServices
|
||||
serviceCounts.forEach((count, serviceVersion) => {
|
||||
const [service, version] = serviceVersion.split(":");
|
||||
activePresenceGauge.record(count, { service, version }); //* Include version in labels
|
||||
previousServices.delete(serviceVersion);
|
||||
});
|
||||
|
||||
// Set gauge to 0 for services that are no longer active
|
||||
previousServices.forEach((serviceVersion) => {
|
||||
const [service, version] = serviceVersion.split(":");
|
||||
activePresenceGauge.record(0, { service, version });
|
||||
});
|
||||
|
||||
// Update the set of previous services
|
||||
serviceCounts.forEach((_, serviceVersion) => previousServices.add(serviceVersion));
|
||||
}
|
||||
@@ -1,15 +1,43 @@
|
||||
import { CronJob } from "cron";
|
||||
|
||||
import debug from "debug";
|
||||
import { clearOldSessions } from "./functions/clearOldSessions.js";
|
||||
import createRedis from "./functions/createRedis.js";
|
||||
import { clearOldSesssions } from "./functions/clearOldSessions.js";
|
||||
import { setCounter } from "./functions/setCounter.js";
|
||||
import "./tracing.js";
|
||||
import { updateActivePresenceGauge } from "./functions/updateActivePresenceGauge.js"; //* Added import
|
||||
|
||||
export const redis = createRedis();
|
||||
|
||||
export const mainLog = debug("api-master");
|
||||
|
||||
debug("Starting cron jobs");
|
||||
|
||||
void new CronJob(
|
||||
// Every 5 seconds
|
||||
"*/5 * * * * *",
|
||||
async () => {
|
||||
clearOldSesssions();
|
||||
() => {
|
||||
clearOldSessions();
|
||||
},
|
||||
undefined,
|
||||
true,
|
||||
);
|
||||
|
||||
void new CronJob(
|
||||
// Every second
|
||||
"* * * * * *",
|
||||
() => {
|
||||
setCounter();
|
||||
},
|
||||
undefined,
|
||||
true,
|
||||
);
|
||||
|
||||
void new CronJob(
|
||||
// Every 5 seconds
|
||||
"*/5 * * * * *",
|
||||
() => {
|
||||
updateActivePresenceGauge();
|
||||
},
|
||||
undefined,
|
||||
true,
|
||||
|
||||
24
apps/api-master/src/tracing.ts
Normal file
24
apps/api-master/src/tracing.ts
Normal file
@@ -0,0 +1,24 @@
|
||||
import { ValueType } from "@opentelemetry/api";
|
||||
import { PrometheusExporter } from "@opentelemetry/exporter-prometheus";
|
||||
import { MeterProvider } from "@opentelemetry/sdk-metrics";
|
||||
|
||||
const prometheusExporter = new PrometheusExporter();
|
||||
|
||||
const provider = new MeterProvider({
|
||||
readers: [prometheusExporter],
|
||||
});
|
||||
|
||||
const meter = provider.getMeter("nice");
|
||||
|
||||
export const counter = meter.createUpDownCounter("active_activites", {
|
||||
description: "Number of active activities",
|
||||
valueType: ValueType.INT,
|
||||
});
|
||||
|
||||
// * Replace Observable Gauge with regular Gauge
|
||||
export const activePresenceGauge = meter.createGauge("active_presence_names", {
|
||||
description: "Number of active presence names per service",
|
||||
valueType: ValueType.INT,
|
||||
});
|
||||
|
||||
prometheusExporter.startServer();
|
||||
1
apps/api-worker/environment.d.ts
vendored
1
apps/api-worker/environment.d.ts
vendored
@@ -2,5 +2,6 @@ declare namespace NodeJS {
|
||||
export interface ProcessEnv {
|
||||
NODE_ENV?: "development" | "production" | "test";
|
||||
DATABASE_URL?: string;
|
||||
SESSION_KEEP_ALIVE_INTERVAL?: string;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
{
|
||||
"name": "@premid/api-worker",
|
||||
"type": "module",
|
||||
"version": "0.0.2",
|
||||
"version": "0.0.12",
|
||||
"private": true,
|
||||
"description": "PreMiD's api",
|
||||
"license": "MPL-2.0",
|
||||
@@ -23,9 +23,13 @@
|
||||
"@escape.tech/graphql-armor-max-directives": "^2.2.0",
|
||||
"@escape.tech/graphql-armor-max-tokens": "^2.4.0",
|
||||
"@fastify/websocket": "^10.0.1",
|
||||
"@opentelemetry/api": "^1.9.0",
|
||||
"@opentelemetry/exporter-prometheus": "^0.52.1",
|
||||
"@opentelemetry/node": "^0.24.0",
|
||||
"@premid/db": "workspace:*",
|
||||
"@sentry/node": "^8.17.0",
|
||||
"arktype": "2.0.0-beta.2",
|
||||
"arktype": "2.0.0-rc.6",
|
||||
"defu": "^6.1.4",
|
||||
"discord-api-types": "^0.37.92",
|
||||
"fastify": "^4.28.1",
|
||||
"graphql": "^16.9.0",
|
||||
|
||||
@@ -1,35 +1,37 @@
|
||||
import { scope, type } from "arktype";
|
||||
import type { FastifyRequest } from "fastify";
|
||||
import WebSocket from "ws";
|
||||
import type { RawData } from "ws";
|
||||
import { REST } from "@discordjs/rest";
|
||||
import { scope, type } from "arktype";
|
||||
import { Routes } from "discord-api-types/v10";
|
||||
import WebSocket from "ws";
|
||||
import type { FastifyRequest } from "fastify";
|
||||
import type { RawData } from "ws";
|
||||
import { redis } from "../functions/createServer.js";
|
||||
import { counter } from "../tracing.js";
|
||||
|
||||
const schema = scope({
|
||||
token: {
|
||||
"+": "delete",
|
||||
"type": "'token'",
|
||||
"token": "format.trim",
|
||||
"expires": "unixTimestamp",
|
||||
"token": "string.trim",
|
||||
"expires": "number.epoch",
|
||||
},
|
||||
session: {
|
||||
"+": "delete",
|
||||
"type": "'session'",
|
||||
"token": "format.trim",
|
||||
"token": "string.trim",
|
||||
},
|
||||
validMessages: "token | session",
|
||||
}).export();
|
||||
|
||||
export class Socket {
|
||||
currentToken: typeof schema.token.infer | undefined;
|
||||
currentSesssion: typeof schema.session.infer | undefined;
|
||||
currentSession: typeof schema.session.infer | undefined;
|
||||
discord = new REST({ version: "10", authPrefix: "Bearer" });
|
||||
|
||||
constructor(
|
||||
public readonly socket: WebSocket.WebSocket,
|
||||
public readonly request: FastifyRequest,
|
||||
) {
|
||||
counter.add(1);
|
||||
socket.on("message", this.onMessage.bind(this));
|
||||
socket.on("close", () => this.onClose());
|
||||
}
|
||||
@@ -53,7 +55,7 @@ export class Socket {
|
||||
}
|
||||
case "session": {
|
||||
await redis.hdel("pmd-api.sessions", out.token);
|
||||
this.currentSesssion = out;
|
||||
this.currentSession = out;
|
||||
break;
|
||||
}
|
||||
}
|
||||
@@ -65,14 +67,16 @@ export class Socket {
|
||||
}
|
||||
|
||||
async onClose() {
|
||||
if (!this.currentToken || !this.currentSesssion)
|
||||
counter.add(-1);
|
||||
|
||||
if (!this.currentToken || !this.currentSession)
|
||||
return;
|
||||
|
||||
await redis.hset(
|
||||
"pmd-api.sessions",
|
||||
this.currentSesssion.token,
|
||||
this.currentSession.token,
|
||||
JSON.stringify({
|
||||
session: this.currentSesssion.token,
|
||||
session: this.currentSession.token,
|
||||
token: this.currentToken.token,
|
||||
lastUpdated: Date.now(),
|
||||
}),
|
||||
|
||||
10
apps/api-worker/src/constants.ts
Normal file
10
apps/api-worker/src/constants.ts
Normal file
@@ -0,0 +1,10 @@
|
||||
import process from "node:process";
|
||||
import { defu } from "defu";
|
||||
|
||||
const disabledFlags = process.env.DISABLED_FEATURE_FLAGS?.split(",") ?? [];
|
||||
const flags = Object.fromEntries(disabledFlags.map(flag => [flag, false]));
|
||||
|
||||
export const featureFlags = defu(flags, {
|
||||
WebSocketManager: true,
|
||||
SessionKeepAlive: true,
|
||||
});
|
||||
@@ -1,18 +1,19 @@
|
||||
import { readFile } from "node:fs/promises";
|
||||
import { resolve } from "node:path";
|
||||
|
||||
import { useSentry } from "@envelop/sentry";
|
||||
import { maxAliasesPlugin } from "@escape.tech/graphql-armor-max-aliases";
|
||||
import { maxDepthPlugin } from "@escape.tech/graphql-armor-max-depth";
|
||||
import { maxDirectivesPlugin } from "@escape.tech/graphql-armor-max-directives";
|
||||
import { maxTokensPlugin } from "@escape.tech/graphql-armor-max-tokens";
|
||||
import type { FastifyReply, FastifyRequest } from "fastify";
|
||||
import fastify from "fastify";
|
||||
import { createSchema, createYoga } from "graphql-yoga";
|
||||
|
||||
import fastifyWebsocket from "@fastify/websocket";
|
||||
import { resolvers } from "../graphql/resolvers/v5/index.js";
|
||||
import fastify from "fastify";
|
||||
|
||||
import { createSchema, createYoga } from "graphql-yoga";
|
||||
import type { FastifyReply, FastifyRequest } from "fastify";
|
||||
import { Socket } from "../classes/Socket.js";
|
||||
import { resolvers } from "../graphql/resolvers/v5/index.js";
|
||||
import { sessionKeepAlive } from "../routes/sessionKeepAlive.js";
|
||||
import { featureFlags } from "../constants.js";
|
||||
import createRedis from "./createRedis.js";
|
||||
|
||||
export interface FastifyContext {
|
||||
@@ -46,7 +47,7 @@ export default async function createServer() {
|
||||
maxDepthPlugin(),
|
||||
maxDirectivesPlugin(),
|
||||
maxTokensPlugin(),
|
||||
useSentry(),
|
||||
/* useSentry(), */
|
||||
],
|
||||
schema: createSchema<FastifyContext>({
|
||||
resolvers,
|
||||
@@ -84,6 +85,12 @@ export default async function createServer() {
|
||||
});
|
||||
});
|
||||
|
||||
app.get("/v5/feature-flags", async (request, reply) => {
|
||||
void reply.send(featureFlags);
|
||||
});
|
||||
|
||||
app.post("/v5/session-keep-alive", sessionKeepAlive);
|
||||
|
||||
return app;
|
||||
}
|
||||
|
||||
|
||||
@@ -1,13 +1,13 @@
|
||||
import { type } from "arktype";
|
||||
import type { MutationResolvers } from "../../../../generated/graphql-v5.js";
|
||||
import { redis } from "../../../../functions/createServer.js";
|
||||
import type { MutationResolvers } from "../../../../generated/graphql-v5.js";
|
||||
|
||||
const addScienceSchema = type({
|
||||
identifier: "uuid & format.lowercase",
|
||||
presences: "format.trim[]",
|
||||
identifier: "string.uuid & string.lower",
|
||||
presences: "string.trim[]",
|
||||
platform: {
|
||||
arch: "format.trim", // ? "format.trim" is just a string that is trimmed
|
||||
os: "format.trim",
|
||||
arch: "string.trim",
|
||||
os: "string.trim",
|
||||
},
|
||||
});
|
||||
|
||||
|
||||
@@ -1,19 +1,20 @@
|
||||
import { type } from "arktype";
|
||||
import type { MutationResolvers } from "../../../../generated/graphql-v5.js";
|
||||
import { redis } from "../../../../functions/createServer.js";
|
||||
|
||||
const heartbeatSchema = type({
|
||||
identifier: "uuid & format.lowercase",
|
||||
presences: {
|
||||
service: "format.trim",
|
||||
version: "semver",
|
||||
language: "format.trim",
|
||||
since: "unixTimestamp",
|
||||
identifier: "string.uuid & string.lower",
|
||||
presence: {
|
||||
service: "string.trim",
|
||||
version: "string.semver",
|
||||
language: "string.trim",
|
||||
since: "number.epoch",
|
||||
},
|
||||
extension: {
|
||||
"version": "semver",
|
||||
"language": "format.trim",
|
||||
"version": "string.semver",
|
||||
"language": "string.trim",
|
||||
"connected?": {
|
||||
app: "integer",
|
||||
app: "number.integer",
|
||||
discord: "boolean",
|
||||
},
|
||||
},
|
||||
@@ -25,13 +26,19 @@ const mutation: MutationResolvers["heartbeat"] = async (_parent, input) => {
|
||||
if (out instanceof type.errors)
|
||||
throw new Error(out.summary);
|
||||
|
||||
// ! Disabled for now
|
||||
/* await redis.setex(
|
||||
`pmd-api.heartbeatUpdates.${data.identifier}`,
|
||||
// 5 minutes
|
||||
300,
|
||||
JSON.stringify(data)
|
||||
); */
|
||||
// * Use Redis Hash with 'service' in the key to store heartbeat data
|
||||
const redisKey = `pmd-api.heartbeatUpdates.${out.identifier}.${out.presence.service}`;
|
||||
await redis.hset(redisKey, {
|
||||
service: out.presence.service,
|
||||
version: out.presence.version,
|
||||
language: out.presence.language,
|
||||
since: out.presence.since.toString(),
|
||||
extension_version: out.extension.version,
|
||||
extension_language: out.extension.language,
|
||||
extension_connected_app: out.extension.connected?.app?.toString() || "",
|
||||
extension_connected_discord: out.extension.connected?.discord?.toString() || "",
|
||||
});
|
||||
await redis.expire(redisKey, 300);
|
||||
|
||||
return {
|
||||
__typename: "HeartbeatResult",
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import { Presence } from "@premid/db";
|
||||
import type { PresenceSchema } from "@premid/db/Presence.js";
|
||||
import { parseResolveInfo } from "graphql-parse-resolve-info";
|
||||
import type { PresenceSchema } from "@premid/db/Presence.js";
|
||||
import type { FilterQuery } from "mongoose";
|
||||
|
||||
import type { QueryResolvers } from "../../../../generated/graphql-v5.js";
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import type { Resolvers } from "../../../generated/graphql-v5.js";
|
||||
import { Query } from "./Query/index.js";
|
||||
import { Mutation } from "./Mutation/index.js";
|
||||
import { Query } from "./Query/index.js";
|
||||
|
||||
export const resolvers: Resolvers = {
|
||||
Query,
|
||||
|
||||
@@ -1,9 +1,5 @@
|
||||
type Mutation {
|
||||
addScience(
|
||||
identifier: String!
|
||||
presences: [String!]!
|
||||
platform: PlatformInput!
|
||||
): AddScienceResult
|
||||
addScience(identifier: String!, presences: [String!]!, platform: PlatformInput!): AddScienceResult
|
||||
}
|
||||
|
||||
input PlatformInput {
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
import process from "node:process";
|
||||
import * as Sentry from "@sentry/node";
|
||||
import { connect } from "mongoose";
|
||||
|
||||
import "./tracing.js";
|
||||
import createServer from "./functions/createServer.js";
|
||||
|
||||
// TODO SETUP SENTRY
|
||||
|
||||
64
apps/api-worker/src/routes/sessionKeepAlive.ts
Normal file
64
apps/api-worker/src/routes/sessionKeepAlive.ts
Normal file
@@ -0,0 +1,64 @@
|
||||
import process from "node:process";
|
||||
import { REST } from "@discordjs/rest";
|
||||
import { type } from "arktype";
|
||||
import { Routes } from "discord-api-types/v10";
|
||||
import type { FastifyReply, FastifyRequest } from "fastify";
|
||||
import { redis } from "../functions/createServer.js";
|
||||
import { featureFlags } from "../constants.js";
|
||||
|
||||
const schema = type({
|
||||
token: "string.trim",
|
||||
session: "string.trim",
|
||||
version: "string.semver & string.trim",
|
||||
scienceId: "string.trim",
|
||||
});
|
||||
|
||||
export async function sessionKeepAlive(request: FastifyRequest, reply: FastifyReply) {
|
||||
if (!featureFlags.SessionKeepAlive)
|
||||
return reply.status(202).send();
|
||||
|
||||
//* Get the headers
|
||||
const out = schema({
|
||||
token: request.headers["x-token"],
|
||||
session: request.headers["x-session"],
|
||||
version: request.headers["x-version"] ?? "2.6.8",
|
||||
scienceId: request.headers["x-science-id"] ?? request.headers["x-token"],
|
||||
});
|
||||
|
||||
if (out instanceof type.errors)
|
||||
return reply.status(400).send({ code: "MISSING_HEADERS", message: out.message });
|
||||
|
||||
if (!await isTokenValid(out.token))
|
||||
return reply.status(400).send({ code: "INVALID_TOKEN", message: "The token is invalid" });
|
||||
|
||||
await redis.hset(
|
||||
"pmd-api.sessions",
|
||||
out.scienceId,
|
||||
JSON.stringify({
|
||||
session: out.session,
|
||||
token: out.token,
|
||||
lastUpdated: Date.now(),
|
||||
}),
|
||||
);
|
||||
|
||||
const interval = Number.parseInt(process.env.SESSION_KEEP_ALIVE_INTERVAL ?? "5000");
|
||||
|
||||
return reply.status(200).send({
|
||||
code: "OK",
|
||||
message: "Session updated",
|
||||
nextUpdate: interval,
|
||||
});
|
||||
}
|
||||
|
||||
async function isTokenValid(token: string) {
|
||||
const discord = new REST({ version: "10", authPrefix: "Bearer" });
|
||||
|
||||
discord.setToken(token);
|
||||
try {
|
||||
await discord.get(Routes.user());
|
||||
return true;
|
||||
}
|
||||
catch {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
18
apps/api-worker/src/tracing.ts
Normal file
18
apps/api-worker/src/tracing.ts
Normal file
@@ -0,0 +1,18 @@
|
||||
import { ValueType } from "@opentelemetry/api";
|
||||
import { PrometheusExporter } from "@opentelemetry/exporter-prometheus";
|
||||
import { MeterProvider } from "@opentelemetry/sdk-metrics";
|
||||
|
||||
const prometheusExporter = new PrometheusExporter();
|
||||
|
||||
const provider = new MeterProvider({
|
||||
readers: [prometheusExporter],
|
||||
});
|
||||
|
||||
const meter = provider.getMeter("nice");
|
||||
|
||||
export const counter = meter.createUpDownCounter("active_activites", {
|
||||
description: "Number of active activities",
|
||||
valueType: ValueType.INT,
|
||||
});
|
||||
|
||||
prometheusExporter.startServer();
|
||||
@@ -1,9 +1,9 @@
|
||||
import crypto from "node:crypto";
|
||||
|
||||
import process from "node:process";
|
||||
import type { RouteHandlerMethod } from "fastify";
|
||||
import mime from "mime-types";
|
||||
import { nanoid } from "nanoid";
|
||||
import type { RouteHandlerMethod } from "fastify";
|
||||
|
||||
import keyv from "../keyv.js";
|
||||
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
import { Buffer } from "node:buffer";
|
||||
import { readFile } from "node:fs/promises";
|
||||
|
||||
import type { RequestOptions } from "node:http";
|
||||
import type { AddressInfo } from "node:net";
|
||||
|
||||
import { Buffer } from "node:buffer";
|
||||
import { afterAll, beforeAll, describe, it } from "vitest";
|
||||
|
||||
import { createServer } from "../functions/createServer.js";
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
import crypto from "node:crypto";
|
||||
|
||||
import process from "node:process";
|
||||
import type { RouteHandlerMethod } from "fastify";
|
||||
import { fileTypeFromBuffer } from "file-type";
|
||||
import { nanoid } from "nanoid";
|
||||
import type { RouteHandlerMethod } from "fastify";
|
||||
|
||||
import keyv from "../keyv.js";
|
||||
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
import crypto from "node:crypto";
|
||||
|
||||
import process from "node:process";
|
||||
import type { RouteHandlerMethod } from "fastify";
|
||||
import { nanoid } from "nanoid";
|
||||
import type { RouteHandlerMethod } from "fastify";
|
||||
|
||||
import keyv from "../keyv.js";
|
||||
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import { readFile } from "node:fs/promises";
|
||||
|
||||
import { Buffer } from "node:buffer";
|
||||
|
||||
import { readFile } from "node:fs/promises";
|
||||
import { afterAll, beforeAll, describe, expect, it, vi } from "vitest";
|
||||
|
||||
import { createServer } from "../functions/createServer.js";
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import crypto from "node:crypto";
|
||||
|
||||
import { Buffer } from "node:buffer";
|
||||
|
||||
import crypto from "node:crypto";
|
||||
import type { RouteHandlerMethod } from "fastify";
|
||||
|
||||
import isInCIDRRange from "../functions/isInCidRange.js";
|
||||
|
||||
@@ -2,9 +2,9 @@ import { extname, resolve } from "node:path";
|
||||
|
||||
import process from "node:process";
|
||||
import helmet from "@fastify/helmet";
|
||||
import type { RequestGenericInterface } from "fastify";
|
||||
import fastify from "fastify";
|
||||
import { globby } from "globby";
|
||||
import type { RequestGenericInterface } from "fastify";
|
||||
|
||||
export const app = fastify();
|
||||
|
||||
|
||||
@@ -99,10 +99,10 @@ export default defineNuxtConfig({
|
||||
"@nuxtjs/seo",
|
||||
"floating-vue/nuxt",
|
||||
"@nuxtjs/device",
|
||||
/* "@nuxt/scripts", */
|
||||
"nuxt-typed-router",
|
||||
"nuxt-security",
|
||||
"@pinia/nuxt",
|
||||
"@nuxt/scripts",
|
||||
],
|
||||
runtimeConfig: {
|
||||
// Use NUXT_ prefixed env vars for nuxt config
|
||||
|
||||
@@ -11,41 +11,41 @@
|
||||
"postinstall": "nuxt prepare"
|
||||
},
|
||||
"dependencies": {
|
||||
"@discord-user-card/vue": "^0.0.8",
|
||||
"@discordjs/rest": "^2.3.0",
|
||||
"@discord-user-card/vue": "^0.0.9",
|
||||
"@discordjs/rest": "^2.4.0",
|
||||
"@fortawesome/fontawesome-svg-core": "^6.6.0",
|
||||
"@fortawesome/free-brands-svg-icons": "^6.6.0",
|
||||
"@fortawesome/free-solid-svg-icons": "^6.6.0",
|
||||
"@fortawesome/vue-fontawesome": "^3.0.8",
|
||||
"@nuxt/fonts": "^0.7.1",
|
||||
"@nuxt/image": "^1.7.0",
|
||||
"@nuxt/kit": "^3.12.3",
|
||||
"@nuxt/scripts": "^0.6.3",
|
||||
"@nuxtjs/device": "^3.1.1",
|
||||
"@nuxt/fonts": "^0.7.2",
|
||||
"@nuxt/image": "^1.8.0",
|
||||
"@nuxt/kit": "^3.13.1",
|
||||
"@nuxt/scripts": "^0.8.5",
|
||||
"@nuxtjs/device": "^3.2.2",
|
||||
"@nuxtjs/google-fonts": "^3.2.0",
|
||||
"@nuxtjs/i18n": "^8.3.1",
|
||||
"@nuxtjs/seo": "2.0.0-rc.15",
|
||||
"@pinia/nuxt": "^0.5.1",
|
||||
"@rollup/rollup-linux-arm64-gnu": "^4.18.1",
|
||||
"@nuxtjs/i18n": "^8.5.2",
|
||||
"@nuxtjs/seo": "2.0.0-rc.21",
|
||||
"@pinia/nuxt": "^0.5.4",
|
||||
"@rollup/rollup-linux-arm64-gnu": "^4.21.2",
|
||||
"@types/lodash": "^4.17.7",
|
||||
"@types/tinycolor2": "^1.4.6",
|
||||
"@unocss/nuxt": "^0.61.5",
|
||||
"@unocss/reset": "^0.61.5",
|
||||
"@unocss/transformer-directives": "^0.61.5",
|
||||
"@vueuse/core": "^10.11.0",
|
||||
"@vueuse/nuxt": "^10.11.0",
|
||||
"@unocss/nuxt": "^0.62.3",
|
||||
"@unocss/reset": "^0.62.3",
|
||||
"@unocss/transformer-directives": "^0.62.3",
|
||||
"@vueuse/core": "^11.0.3",
|
||||
"@vueuse/nuxt": "^11.0.3",
|
||||
"bowser": "^2.11.0",
|
||||
"discord-api-types": "^0.37.92",
|
||||
"discord-api-types": "^0.37.100",
|
||||
"floating-vue": "^5.2.2",
|
||||
"lodash": "^4.17.21",
|
||||
"nuxt": "^3.12.3",
|
||||
"nuxt": "^3.13.1",
|
||||
"nuxt-graphql-client": "^0.2.35",
|
||||
"nuxt-security": "2.0.0-rc.9",
|
||||
"nuxt-typed-router": "^3.6.5",
|
||||
"sass": "^1.77.8",
|
||||
"sass": "^1.78.0",
|
||||
"tinycolor2": "^1.6.0",
|
||||
"vue": "^3.4.32",
|
||||
"vue-router": "^4.4.0",
|
||||
"vue-tsc": "^2.0.26"
|
||||
"vue": "^3.5.3",
|
||||
"vue-router": "^4.4.3",
|
||||
"vue-tsc": "^2.1.6"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
<script setup lang="ts">
|
||||
import type { LocationQuery } from "vue-router";
|
||||
import { useExtensionStore } from "~/stores/useExtension";
|
||||
import breakpoints from "~/breakpoints";
|
||||
import { useExtensionStore } from "~/stores/useExtension";
|
||||
|
||||
const extension = useExtensionStore();
|
||||
|
||||
@@ -19,17 +19,23 @@ const selectedCategory = ref("");
|
||||
|
||||
const sortBy = ref<string>("");
|
||||
const pageSize = ref(9);
|
||||
const totalPages = computed(() => Math.ceil(data.value.presences.length / pageSize.value));
|
||||
const totalPages = computed(() =>
|
||||
Math.ceil(data.value.presences.length / pageSize.value),
|
||||
);
|
||||
|
||||
const presences = computed(() => {
|
||||
const startIndex = (currentPage.value - 1) * pageSize.value;
|
||||
const endIndex = startIndex + pageSize.value;
|
||||
const sortedPresences = (
|
||||
selectedCategory.value
|
||||
? data.value.presences.filter(p => p.metadata.category === selectedCategory.value)
|
||||
? data.value.presences.filter(
|
||||
p => p.metadata.category === selectedCategory.value,
|
||||
)
|
||||
: data.value.presences
|
||||
)
|
||||
.filter(p => p.metadata.service.toLowerCase().includes(searchTerm.value.toLowerCase()))
|
||||
.filter(p =>
|
||||
p.metadata.service.toLowerCase().includes(searchTerm.value.toLowerCase()),
|
||||
)
|
||||
.sort((a, b) => {
|
||||
if (sortBy.value === t("component.searchBar.sort.mostUsed"))
|
||||
return b.users - a.users;
|
||||
@@ -37,7 +43,7 @@ const presences = computed(() => {
|
||||
return a.metadata.service.localeCompare(b.metadata.service);
|
||||
return 0;
|
||||
})
|
||||
.sort(a => extension.presences.includes(a.metadata.service) ? -1 : 1);
|
||||
.sort(a => (extension.presences.includes(a.metadata.service) ? -1 : 1));
|
||||
|
||||
return {
|
||||
data: sortedPresences.slice(startIndex, endIndex),
|
||||
@@ -48,7 +54,9 @@ const presences = computed(() => {
|
||||
|
||||
async function handleQuery(query: LocationQuery) {
|
||||
const pageQuery = query.page?.toString() || "1";
|
||||
const parsedPage = Number.parseInt(Number.isNaN(Number(pageQuery)) ? "1" : pageQuery);
|
||||
const parsedPage = Number.parseInt(
|
||||
Number.isNaN(Number(pageQuery)) ? "1" : pageQuery,
|
||||
);
|
||||
currentPage.value = Math.max(1, Math.min(parsedPage, totalPages.value));
|
||||
searchTerm.value = query.search?.toString() || "";
|
||||
selectedCategory.value = query.category?.toString() || "";
|
||||
@@ -71,10 +79,16 @@ onUnmounted(() => {
|
||||
window.removeEventListener("resize", resizePageSize);
|
||||
});
|
||||
|
||||
function getLinkProperties({ page = currentPage.value, search = searchTerm.value, category = selectedCategory.value }) {
|
||||
function getLinkProperties({
|
||||
page = currentPage.value,
|
||||
search = searchTerm.value,
|
||||
category = selectedCategory.value,
|
||||
}) {
|
||||
const query = { category, page, search };
|
||||
return {
|
||||
query: Object.fromEntries(Object.entries(query).filter(([, value]) => value !== "")),
|
||||
query: Object.fromEntries(
|
||||
Object.entries(query).filter(([, value]) => value !== ""),
|
||||
),
|
||||
};
|
||||
}
|
||||
|
||||
@@ -101,18 +115,35 @@ onMounted(() => {
|
||||
<div>
|
||||
<StoreSearchBar v-model:sort-order="sortBy" />
|
||||
<!-- Presences Grid or Empty State -->
|
||||
<div v-if="presences.data.length === 0" class="flex justify-center items-center rounded-lg h-50">
|
||||
<div class="flex flex-col items-center justify-center p-5 text-lg text-primary-highlight">
|
||||
<div
|
||||
v-if="presences.data.length === 0"
|
||||
class="flex justify-center items-center rounded-lg h-50"
|
||||
>
|
||||
<div
|
||||
class="flex flex-col items-center justify-center p-5 text-lg text-primary-highlight"
|
||||
>
|
||||
<FAIcon :icon="['fa', 'frown']" class="mb-2 text-3xl" />
|
||||
<p>{{ $t("page.store.noPresence") }}</p>
|
||||
</div>
|
||||
</div>
|
||||
<div v-if="presences.data.length > 0" class="items-center mt-5 flex-col flex sm:mt-10 min-h-688px">
|
||||
<div class="gap-4 grid grid-cols-[fit-content(0%)] sm-md:grid-cols-[repeat(2,fit-content(0%))] lg:grid-cols-[repeat(3,fit-content(0%))] overflow-unset">
|
||||
<StoreCard v-for="presence in presences.data" :key="presence.metadata.service" :presence="presence" />
|
||||
<div
|
||||
v-if="presences.data.length > 0"
|
||||
class="items-center mt-5 flex-col flex sm:mt-10 min-h-688px"
|
||||
>
|
||||
<div
|
||||
class="gap-4 grid grid-cols-[fit-content(0%)] sm-md:grid-cols-[repeat(2,fit-content(0%))] lg:grid-cols-[repeat(3,fit-content(0%))] overflow-unset"
|
||||
>
|
||||
<StoreCard
|
||||
v-for="presence in presences.data"
|
||||
:key="presence.metadata.service"
|
||||
:presence="presence"
|
||||
/>
|
||||
</div>
|
||||
<!-- Pagination -->
|
||||
<div v-if="presences.data.length > 0" class="flex mt-5 mb-10 flex-wrap justify-center sticky z-40">
|
||||
<div
|
||||
v-if="presences.data.length > 0"
|
||||
class="flex mt-5 mb-10 flex-wrap justify-center sticky z-40"
|
||||
>
|
||||
<NuxtLink
|
||||
:to="getLinkProperties({ page: 1 })"
|
||||
:replace="true"
|
||||
@@ -148,7 +179,7 @@ onMounted(() => {
|
||||
</template>
|
||||
|
||||
<style scoped>
|
||||
#filters {
|
||||
#filters {
|
||||
&::-webkit-scrollbar {
|
||||
width: 0.4rem;
|
||||
height: 100%;
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import { library } from "@fortawesome/fontawesome-svg-core";
|
||||
import { faBrave, faChrome, faDiscord, faEdge, faFirefox, faGithub, faOpera, faPatreon, faSafari, faXTwitter } from "@fortawesome/free-brands-svg-icons";
|
||||
import {
|
||||
faBars,
|
||||
faBolt,
|
||||
@@ -33,7 +34,6 @@ import {
|
||||
faUsers,
|
||||
faVideo,
|
||||
} from "@fortawesome/free-solid-svg-icons";
|
||||
import { faBrave, faChrome, faDiscord, faEdge, faFirefox, faGithub, faOpera, faPatreon, faSafari, faXTwitter } from "@fortawesome/free-brands-svg-icons";
|
||||
|
||||
export default defineNuxtPlugin(() => {
|
||||
library.add(
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
import { ActivityType, PresenceUpdateStatus, flagsToBadges } from "@discord-user-card/vue";
|
||||
import { REST } from "@discordjs/rest";
|
||||
import type { RESTGetAPIGuildMemberResult, RESTGetAPIGuildRolesResult } from "discord-api-types/v10";
|
||||
import { Routes } from "discord-api-types/v10";
|
||||
import type { DiscordUserCardActivity, DiscordUserCardUser } from "@discord-user-card/vue";
|
||||
import { ActivityType, PresenceUpdateStatus, flagsToBadges } from "@discord-user-card/vue";
|
||||
import type { RESTGetAPIGuildMemberResult, RESTGetAPIGuildRolesResult } from "discord-api-types/v10";
|
||||
|
||||
const { discord_bot_token } = useRuntimeConfig();
|
||||
const rest = new REST({ version: "10" }).setToken(discord_bot_token);
|
||||
@@ -61,7 +61,8 @@ const activities: Map<string, DiscordUserCardActivity[]> = new Map()
|
||||
},
|
||||
],
|
||||
} satisfies DiscordUserCardActivity,
|
||||
]).set("193714715631812608", [
|
||||
])
|
||||
.set("193714715631812608", [
|
||||
{
|
||||
type: ActivityType.Playing,
|
||||
name: "Kotobade Asobou",
|
||||
@@ -73,7 +74,8 @@ const activities: Map<string, DiscordUserCardActivity[]> = new Map()
|
||||
],
|
||||
startTimestamp: Date.now(),
|
||||
} satisfies DiscordUserCardActivity,
|
||||
]).set("205984221859151873", [
|
||||
])
|
||||
.set("205984221859151873", [
|
||||
{
|
||||
type: ActivityType.Playing,
|
||||
name: "BeatLeader",
|
||||
@@ -90,7 +92,8 @@ const activities: Map<string, DiscordUserCardActivity[]> = new Map()
|
||||
},
|
||||
],
|
||||
} satisfies DiscordUserCardActivity,
|
||||
]).set("152155870917033985", [
|
||||
])
|
||||
.set("152155870917033985", [
|
||||
{
|
||||
type: ActivityType.Watching,
|
||||
name: "YouTube",
|
||||
|
||||
@@ -22,6 +22,7 @@ export const useExtensionStore = defineStore("extension", () => {
|
||||
}
|
||||
}
|
||||
|
||||
// eslint-disable-next-line unicorn/consistent-function-scoping
|
||||
function fetchPresences() {
|
||||
window.dispatchEvent(new CustomEvent("PreMiD_GetPresenceList"));
|
||||
}
|
||||
|
||||
8371
pnpm-lock.yaml
generated
8371
pnpm-lock.yaml
generated
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user