mirror of
https://github.com/Drop-OSS/drop.git
synced 2025-11-09 20:12:10 +10:00
feat: database level metadata provider init
This commit is contained in:
@ -0,0 +1,2 @@
|
||||
-- AlterTable
|
||||
ALTER TABLE "ApplicationSettings" ADD COLUMN "metadataProviders" TEXT[];
|
||||
@ -2,6 +2,7 @@ model ApplicationSettings {
|
||||
timestamp DateTime @id @default(now())
|
||||
|
||||
enabledAuthencationMechanisms AuthMec[]
|
||||
metadataProviders String[]
|
||||
}
|
||||
|
||||
enum Platform {
|
||||
|
||||
@ -3,44 +3,29 @@ import prisma from "../db/database";
|
||||
|
||||
class ApplicationConfiguration {
|
||||
// Reference to the currently selected application configuration
|
||||
private currentApplicationSettings: ApplicationSettings;
|
||||
private currentApplicationSettings: ApplicationSettings = {
|
||||
timestamp: new Date(),
|
||||
enabledAuthencationMechanisms: [],
|
||||
metadataProviders: [],
|
||||
};
|
||||
private applicationStateProxy: object;
|
||||
private dirty: boolean = false;
|
||||
private dirtyPromise: Promise<any> | undefined = undefined;
|
||||
|
||||
constructor() {
|
||||
// @ts-expect-error
|
||||
this.currentApplicationSettings = {};
|
||||
this.applicationStateProxy = {};
|
||||
}
|
||||
|
||||
private buildApplicationSettingsProxy() {
|
||||
const appConfig = this;
|
||||
const proxy = new Proxy(this.currentApplicationSettings, {
|
||||
get: (target, key: keyof ApplicationSettings) => {
|
||||
return appConfig.currentApplicationSettings[key];
|
||||
},
|
||||
set: (target, key: keyof ApplicationSettings, value) => {
|
||||
if (JSON.stringify(value) === JSON.stringify(appConfig.currentApplicationSettings[key])) return true;
|
||||
appConfig.currentApplicationSettings[key] = value;
|
||||
private async save() {
|
||||
const deepAppConfigCopy: Omit<ApplicationSettings, "timestamp"> & {
|
||||
timestamp?: Date;
|
||||
} = JSON.parse(JSON.stringify(this.currentApplicationSettings));
|
||||
|
||||
const deepAppConfigCopy: Omit<ApplicationSettings, "timestamp"> & {
|
||||
timestamp?: Date;
|
||||
} = JSON.parse(JSON.stringify(appConfig.currentApplicationSettings));
|
||||
delete deepAppConfigCopy["timestamp"];
|
||||
|
||||
delete deepAppConfigCopy["timestamp"];
|
||||
|
||||
appConfig.dirty = true;
|
||||
appConfig.dirtyPromise = prisma.applicationSettings.create({
|
||||
data: deepAppConfigCopy,
|
||||
});
|
||||
return true;
|
||||
},
|
||||
deleteProperty: (target, key: keyof ApplicationSettings) => {
|
||||
return false;
|
||||
},
|
||||
await prisma.applicationSettings.create({
|
||||
data: deepAppConfigCopy,
|
||||
});
|
||||
this.applicationStateProxy = proxy;
|
||||
}
|
||||
|
||||
// Default application configuration
|
||||
@ -48,11 +33,11 @@ class ApplicationConfiguration {
|
||||
const initialState = await prisma.applicationSettings.create({
|
||||
data: {
|
||||
enabledAuthencationMechanisms: [AuthMec.Simple],
|
||||
metadataProviders: [],
|
||||
},
|
||||
});
|
||||
|
||||
this.currentApplicationSettings = initialState;
|
||||
this.buildApplicationSettingsProxy();
|
||||
}
|
||||
|
||||
async pullConfiguration() {
|
||||
@ -65,18 +50,22 @@ class ApplicationConfiguration {
|
||||
if (!latestState) throw new Error("No application configuration to pull");
|
||||
|
||||
this.currentApplicationSettings = latestState;
|
||||
this.buildApplicationSettingsProxy();
|
||||
}
|
||||
|
||||
async waitForWrite() {
|
||||
if (this.dirty) {
|
||||
await this.dirtyPromise;
|
||||
async set<T extends keyof ApplicationSettings>(
|
||||
key: T,
|
||||
value: ApplicationSettings[T]
|
||||
) {
|
||||
if (this.currentApplicationSettings[key] !== value) {
|
||||
this.currentApplicationSettings[key] = value;
|
||||
|
||||
await this.save();
|
||||
}
|
||||
}
|
||||
|
||||
useApplicationConfiguration(): ApplicationSettings {
|
||||
return this.applicationStateProxy as ApplicationSettings;
|
||||
get<T extends keyof ApplicationSettings>(key: T): ApplicationSettings[T] {
|
||||
return this.currentApplicationSettings[key];
|
||||
}
|
||||
}
|
||||
|
||||
export const applicationSettings = new ApplicationConfiguration();
|
||||
export const applicationSettings = new ApplicationConfiguration();
|
||||
|
||||
@ -46,6 +46,18 @@ export class MetadataHandler {
|
||||
this.providers.push(provider, priority);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Returns provider IDs, used to save to applicationConfig
|
||||
* @returns The provider IDs in order, missing manual
|
||||
*/
|
||||
fetchProviderIdsInOrder() {
|
||||
return this.providers
|
||||
.values()
|
||||
.map((e) => e.id())
|
||||
.filter((e) => e !== "manual");
|
||||
}
|
||||
|
||||
async search(query: string) {
|
||||
const promises: Promise<InternalGameMetadataResult[]>[] = [];
|
||||
for (const provider of this.providers.values()) {
|
||||
@ -236,17 +248,3 @@ export class MetadataHandler {
|
||||
|
||||
export const metadataHandler = new MetadataHandler();
|
||||
export default metadataHandler;
|
||||
|
||||
export const enabledMedadataProviders: string[] = [];
|
||||
const metadataProviders = [GiantBombProvider, ManualMetadataProvider];
|
||||
|
||||
for(const provider of metadataProviders){
|
||||
try {
|
||||
const prov = new provider;
|
||||
metadataHandler.addProvider(prov);
|
||||
enabledMedadataProviders.push(prov.id());
|
||||
console.log(`enabled metadata provider: ${prov.name()}`)
|
||||
}catch(e){
|
||||
console.error(`skipping metadata provider setup: ${e}`);
|
||||
}
|
||||
}
|
||||
2
server/internal/metadata/types.d.ts
vendored
2
server/internal/metadata/types.d.ts
vendored
@ -1,6 +1,6 @@
|
||||
import { Developer, Publisher } from "@prisma/client";
|
||||
import { ObjectReference } from "../objects";
|
||||
import { ObjectTransactionalHandler, TransactionDataType } from "../objects/transactional";
|
||||
import { ObjectReference } from "../objects/objectHandler";
|
||||
|
||||
export interface GameMetadataSearchResult {
|
||||
id: string;
|
||||
|
||||
@ -8,10 +8,7 @@ export default defineNitroPlugin(async (nitro) => {
|
||||
} else {
|
||||
await applicationSettings.initialiseConfiguration();
|
||||
}
|
||||
|
||||
nitro.hooks.hookOnce("close", async () => {
|
||||
await applicationSettings.waitForWrite();
|
||||
});
|
||||
console.log("initalised application config");
|
||||
|
||||
// Ensure system user exists
|
||||
// The system user owns any user-based code
|
||||
49
server/plugins/03.metadata-init.ts
Normal file
49
server/plugins/03.metadata-init.ts
Normal file
@ -0,0 +1,49 @@
|
||||
import { applicationSettings } from "../internal/config/application-configuration";
|
||||
import metadataHandler, { MetadataProvider } from "../internal/metadata";
|
||||
import { GiantBombProvider } from "../internal/metadata/giantbomb";
|
||||
import { ManualMetadataProvider } from "../internal/metadata/manual";
|
||||
|
||||
export default defineNitroPlugin(async (nitro) => {
|
||||
const metadataProviders = [GiantBombProvider];
|
||||
|
||||
const providers: { [key: string]: MetadataProvider } = {};
|
||||
|
||||
for (const provider of metadataProviders) {
|
||||
try {
|
||||
const prov = new provider();
|
||||
const id = prov.id();
|
||||
providers[id] = prov;
|
||||
|
||||
console.log(`enabled metadata provider: ${prov.name()}`);
|
||||
} catch (e) {
|
||||
console.warn(`skipping metadata provider setup: ${e}`);
|
||||
}
|
||||
}
|
||||
|
||||
// Add providers based on their position in the application settings
|
||||
const configuredProviderList = applicationSettings.get("metadataProviders");
|
||||
const max = configuredProviderList.length;
|
||||
for (const [index, providerId] of configuredProviderList.entries()) {
|
||||
const priority = max * 2 - index; // Offset by the length --- (max - index) + max
|
||||
const provider = providers[providerId];
|
||||
if (!provider) {
|
||||
console.warn(`failed to add existing metadata provider: ${providerId}`);
|
||||
continue;
|
||||
}
|
||||
metadataHandler.addProvider(provider, priority);
|
||||
delete providers[providerId];
|
||||
}
|
||||
|
||||
// Add the rest with no position
|
||||
for (const [providerId, provider] of Object.entries(providers)) {
|
||||
metadataHandler.addProvider(provider);
|
||||
}
|
||||
|
||||
metadataHandler.addProvider(new ManualMetadataProvider(), -1000);
|
||||
|
||||
// Update the applicatonConfig
|
||||
await applicationSettings.set(
|
||||
"metadataProviders",
|
||||
metadataHandler.fetchProviderIdsInOrder()
|
||||
);
|
||||
});
|
||||
Reference in New Issue
Block a user