From 44dbda30a7c7d632bd919406e18c460d4a124740 Mon Sep 17 00:00:00 2001 From: Fairpost Date: Mon, 22 Sep 2025 09:53:16 +0200 Subject: [PATCH] feat: Support for get-settings, put-settings --- README.md | 2 ++ src/models/User.ts | 43 +++++++++++++++++++++++++++++++++++++ src/models/User/UserData.ts | 15 +++++++++++++ src/services/Fairpost.ts | 31 ++++++++++++++++++++++++++ 4 files changed, 91 insertions(+) diff --git a/README.md b/README.md index a0b449e..17b96d5 100644 --- a/README.md +++ b/README.md @@ -155,6 +155,8 @@ fairpost: @userid refresh-platform --platform=xxx fairpost: @userid refresh-platforms [--platforms=xxx,xxx] fairpost: @userid get-platform --platform=xxx fairpost: @userid get-platforms [--platforms=xxx,xxx] +fairpost: @userid get-settings +fairpost: @userid put-settings fairpost: @userid get-source --source=xxx [--stage=xxx] fairpost: @userid get-sources [--sources=xxx,xxx|--stage=xxx] fairpost: @userid get-post --post=xxx:xxx diff --git a/src/models/User.ts b/src/models/User.ts index 232ece7..580effc 100644 --- a/src/models/User.ts +++ b/src/models/User.ts @@ -1,4 +1,5 @@ import { basename } from "path"; +import * as readline from "node:readline/promises"; import * as platformClasses from "../platforms/index.ts"; import { PlatformId } from "../platforms/index.ts"; @@ -12,6 +13,7 @@ import UserData from "./User/UserData.ts"; import UserFiles from "./User/UserFiles.ts"; import UserLog from "./User/UserLog.ts"; import UserMapper from "../mappers/UserMapper.ts"; +import { FieldMapping } from "../types/index.ts"; /** * User - represents one fairpost user @@ -312,4 +314,45 @@ export default class User { throw this.log.error("removePlatform: no such platform", platformId); } } + + /** + * @returns all data from the settings store + */ + public getSettings(): { [key: string]: string } { + return this.data.getStore("settings"); + } + + public async promptCliFields( + fields: FieldMapping, + ): Promise<{ [key: string]: string }> { + const settings = {} as { [key: string]: string }; + const reader = readline.createInterface({ + input: process.stdin, + output: process.stdout, + }); + for (const key in fields) { + const current = this.data.get( + "settings", + key, + String(fields[key].default ?? ""), + ); + const value = + (await reader.question(`${fields[key].label} ( ${current} ): `)) || + current; + settings[key] = value; + } + reader.close(); + return settings; + } + + /** + * Update settings with values from payload + * @param payload - key/value object to save under settings store + */ + public async putSettings(payload: { [key: string]: string }): Promise { + for (const key in payload) { + this.data.set("settings", key, payload[key]); + } + await this.data.save(); + } } diff --git a/src/models/User/UserData.ts b/src/models/User/UserData.ts index b0c1b02..cb8d672 100644 --- a/src/models/User/UserData.ts +++ b/src/models/User/UserData.ts @@ -77,6 +77,21 @@ export default class UserData { } } + public getStore(storeName: StorageType): { [key: string]: string } { + const storageKey = StorageKeys[storeName]; + const storage = process.env[storageKey] ?? "none"; + const jsonStore = this.jsonData[storeName]; + switch (storage) { + case "json-env": + case "json": + return jsonStore; + default: + throw new Error( + "UserData.getStore: Storage " + storage + " not implemented", + ); + } + } + public getObject(store: StorageType, key: string, def?: object): object { const value = this.get(store, key, JSON.stringify(def)); try { diff --git a/src/services/Fairpost.ts b/src/services/Fairpost.ts index b0f3c92..efbd632 100644 --- a/src/services/Fairpost.ts +++ b/src/services/Fairpost.ts @@ -158,6 +158,35 @@ class Fairpost { break; } + case "get-settings": { + if (!permissions.manageAccount) { + throw new Error("Missing permissions for command " + command); + } + if (!user) { + throw new Error("user is required for command " + command); + } + output = user.getSettings(); + break; + } + + case "put-settings": { + if (!permissions.manageAccount) { + throw new Error("Missing permissions for command " + command); + } + if (!user) { + throw new Error("user is required for command " + command); + } + if (!args.payload) { + throw user.log.error( + "CommandHandler " + command, + "Missing argument: payload", + ); + } + await user.putSettings(args.payload as { [key: string]: string }); + output = { success: true }; + break; + } + case "refresh-token": { if (!permissions.manageAccount) { throw new Error("Missing permissions for command " + command); @@ -910,6 +939,8 @@ class Fairpost { `${cmd} @userid refresh-platforms [--platforms=xxx,xxx]`, `${cmd} @userid get-platform --platform=xxx`, `${cmd} @userid get-platforms [--platforms=xxx,xxx]`, + `${cmd} @userid get-settings`, + `${cmd} @userid put-settings `, `${cmd} @userid get-source --source=xxx [--stage=xxx] `, `${cmd} @userid get-sources [--sources=xxx,xxx|--stage=xxx]`, `${cmd} @userid get-post --post=xxx:xxx`,