mirror of
https://github.com/Drop-OSS/drop.git
synced 2025-11-11 04:52:06 +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())
|
timestamp DateTime @id @default(now())
|
||||||
|
|
||||||
enabledAuthencationMechanisms AuthMec[]
|
enabledAuthencationMechanisms AuthMec[]
|
||||||
|
metadataProviders String[]
|
||||||
}
|
}
|
||||||
|
|
||||||
enum Platform {
|
enum Platform {
|
||||||
|
|||||||
@ -3,44 +3,29 @@ import prisma from "../db/database";
|
|||||||
|
|
||||||
class ApplicationConfiguration {
|
class ApplicationConfiguration {
|
||||||
// Reference to the currently selected application configuration
|
// Reference to the currently selected application configuration
|
||||||
private currentApplicationSettings: ApplicationSettings;
|
private currentApplicationSettings: ApplicationSettings = {
|
||||||
|
timestamp: new Date(),
|
||||||
|
enabledAuthencationMechanisms: [],
|
||||||
|
metadataProviders: [],
|
||||||
|
};
|
||||||
private applicationStateProxy: object;
|
private applicationStateProxy: object;
|
||||||
private dirty: boolean = false;
|
private dirty: boolean = false;
|
||||||
private dirtyPromise: Promise<any> | undefined = undefined;
|
private dirtyPromise: Promise<any> | undefined = undefined;
|
||||||
|
|
||||||
constructor() {
|
constructor() {
|
||||||
// @ts-expect-error
|
|
||||||
this.currentApplicationSettings = {};
|
|
||||||
this.applicationStateProxy = {};
|
this.applicationStateProxy = {};
|
||||||
}
|
}
|
||||||
|
|
||||||
private buildApplicationSettingsProxy() {
|
private async save() {
|
||||||
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;
|
|
||||||
|
|
||||||
const deepAppConfigCopy: Omit<ApplicationSettings, "timestamp"> & {
|
const deepAppConfigCopy: Omit<ApplicationSettings, "timestamp"> & {
|
||||||
timestamp?: Date;
|
timestamp?: Date;
|
||||||
} = JSON.parse(JSON.stringify(appConfig.currentApplicationSettings));
|
} = JSON.parse(JSON.stringify(this.currentApplicationSettings));
|
||||||
|
|
||||||
delete deepAppConfigCopy["timestamp"];
|
delete deepAppConfigCopy["timestamp"];
|
||||||
|
|
||||||
appConfig.dirty = true;
|
await prisma.applicationSettings.create({
|
||||||
appConfig.dirtyPromise = prisma.applicationSettings.create({
|
|
||||||
data: deepAppConfigCopy,
|
data: deepAppConfigCopy,
|
||||||
});
|
});
|
||||||
return true;
|
|
||||||
},
|
|
||||||
deleteProperty: (target, key: keyof ApplicationSettings) => {
|
|
||||||
return false;
|
|
||||||
},
|
|
||||||
});
|
|
||||||
this.applicationStateProxy = proxy;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Default application configuration
|
// Default application configuration
|
||||||
@ -48,11 +33,11 @@ class ApplicationConfiguration {
|
|||||||
const initialState = await prisma.applicationSettings.create({
|
const initialState = await prisma.applicationSettings.create({
|
||||||
data: {
|
data: {
|
||||||
enabledAuthencationMechanisms: [AuthMec.Simple],
|
enabledAuthencationMechanisms: [AuthMec.Simple],
|
||||||
|
metadataProviders: [],
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
this.currentApplicationSettings = initialState;
|
this.currentApplicationSettings = initialState;
|
||||||
this.buildApplicationSettingsProxy();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async pullConfiguration() {
|
async pullConfiguration() {
|
||||||
@ -65,17 +50,21 @@ class ApplicationConfiguration {
|
|||||||
if (!latestState) throw new Error("No application configuration to pull");
|
if (!latestState) throw new Error("No application configuration to pull");
|
||||||
|
|
||||||
this.currentApplicationSettings = latestState;
|
this.currentApplicationSettings = latestState;
|
||||||
this.buildApplicationSettingsProxy();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async waitForWrite() {
|
async set<T extends keyof ApplicationSettings>(
|
||||||
if (this.dirty) {
|
key: T,
|
||||||
await this.dirtyPromise;
|
value: ApplicationSettings[T]
|
||||||
|
) {
|
||||||
|
if (this.currentApplicationSettings[key] !== value) {
|
||||||
|
this.currentApplicationSettings[key] = value;
|
||||||
|
|
||||||
|
await this.save();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
useApplicationConfiguration(): ApplicationSettings {
|
get<T extends keyof ApplicationSettings>(key: T): ApplicationSettings[T] {
|
||||||
return this.applicationStateProxy as ApplicationSettings;
|
return this.currentApplicationSettings[key];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -46,6 +46,18 @@ export class MetadataHandler {
|
|||||||
this.providers.push(provider, priority);
|
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) {
|
async search(query: string) {
|
||||||
const promises: Promise<InternalGameMetadataResult[]>[] = [];
|
const promises: Promise<InternalGameMetadataResult[]>[] = [];
|
||||||
for (const provider of this.providers.values()) {
|
for (const provider of this.providers.values()) {
|
||||||
@ -236,17 +248,3 @@ export class MetadataHandler {
|
|||||||
|
|
||||||
export const metadataHandler = new MetadataHandler();
|
export const metadataHandler = new MetadataHandler();
|
||||||
export default 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 { Developer, Publisher } from "@prisma/client";
|
||||||
import { ObjectReference } from "../objects";
|
|
||||||
import { ObjectTransactionalHandler, TransactionDataType } from "../objects/transactional";
|
import { ObjectTransactionalHandler, TransactionDataType } from "../objects/transactional";
|
||||||
|
import { ObjectReference } from "../objects/objectHandler";
|
||||||
|
|
||||||
export interface GameMetadataSearchResult {
|
export interface GameMetadataSearchResult {
|
||||||
id: string;
|
id: string;
|
||||||
|
|||||||
@ -8,10 +8,7 @@ export default defineNitroPlugin(async (nitro) => {
|
|||||||
} else {
|
} else {
|
||||||
await applicationSettings.initialiseConfiguration();
|
await applicationSettings.initialiseConfiguration();
|
||||||
}
|
}
|
||||||
|
console.log("initalised application config");
|
||||||
nitro.hooks.hookOnce("close", async () => {
|
|
||||||
await applicationSettings.waitForWrite();
|
|
||||||
});
|
|
||||||
|
|
||||||
// Ensure system user exists
|
// Ensure system user exists
|
||||||
// The system user owns any user-based code
|
// 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