Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 <payload>
fairpost: @userid get-source --source=xxx [--stage=xxx]
fairpost: @userid get-sources [--sources=xxx,xxx|--stage=xxx]
fairpost: @userid get-post --post=xxx:xxx
Expand Down
43 changes: 43 additions & 0 deletions src/models/User.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand All @@ -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
Expand Down Expand Up @@ -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<void> {
for (const key in payload) {
this.data.set("settings", key, payload[key]);
}
await this.data.save();
}
}
15 changes: 15 additions & 0 deletions src/models/User/UserData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
31 changes: 31 additions & 0 deletions src/services/Fairpost.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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 <payload>`,
`${cmd} @userid get-source --source=xxx [--stage=xxx] `,
`${cmd} @userid get-sources [--sources=xxx,xxx|--stage=xxx]`,
`${cmd} @userid get-post --post=xxx:xxx`,
Expand Down