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
7 changes: 3 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -148,15 +148,14 @@ to get a new pair of tokens.
fairpost: help
fairpost: @userid get-user
fairpost: @userid get-feed
fairpost: @userid get-platform --platform=xxx
fairpost: @userid put-platform --platform=xxx << payload
fairpost: @userid get-platforms [--platforms=xxx,xxx]
fairpost: @userid setup-platform --platform=xxx
fairpost: @userid test-platform --platform=xxx
fairpost: @userid test-platforms [--platforms=xxx,xxx]
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
2 changes: 1 addition & 1 deletion src/mappers/AbstractMapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ export default abstract class AbstractMapper<ModelDto> {
* @param dto
* @returns boolean success
*/
abstract setDto(operator: Operator, dto: ModelDto): Promise<boolean>;
abstract putDto(operator: Operator, dto: ModelDto): Promise<boolean>;

protected getDtoFields(
operator: Operator,
Expand Down
6 changes: 3 additions & 3 deletions src/mappers/FeedMapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,13 +75,13 @@ export default class FeedMapper extends AbstractMapper<FeedDto> {
* @param dto
* @returns boolean success
*/
async setDto(operator: Operator, dto: FeedDto): Promise<boolean> {
async putDto(operator: Operator, dto: FeedDto): Promise<boolean> {
const fields = this.getDtoFields(operator, "set");
for (const field in dto) {
if (field in fields) {
if (fields.includes(field)) {
// there are no settable fields
} else {
throw this.user.log.error("Unknown field: " + field);
this.user.log.trace("Ignoring field: " + field);
}
}
return true;
Expand Down
6 changes: 3 additions & 3 deletions src/mappers/PlatformMapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,10 +108,10 @@ export default class PlatformMapper extends AbstractMapper<PlatformDto> {
* @param dto
* @returns boolean success
*/
async setDto(operator: Operator, dto: PlatformDto): Promise<boolean> {
async putDto(operator: Operator, dto: PlatformDto): Promise<boolean> {
const fields = this.getDtoFields(operator, "set");
for (const field in dto) {
if (field in fields) {
if (fields.includes(field)) {
switch (field) {
case "active":
if (dto[field]) await this.user.addPlatform(this.platform.id);
Expand Down Expand Up @@ -149,7 +149,7 @@ export default class PlatformMapper extends AbstractMapper<PlatformDto> {
}
}
} else {
throw this.user.log.error("Unknown field: " + field);
this.user.log.trace("Ignoring field: " + field);
}
}
await this.user.data.save();
Expand Down
6 changes: 3 additions & 3 deletions src/mappers/PostMapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -201,10 +201,10 @@ export default class PostMapper extends AbstractMapper<PostDto> {
* @param dto
* @returns boolean success
*/
async setDto(operator: Operator, dto: PostDto): Promise<boolean> {
async putDto(operator: Operator, dto: PostDto): Promise<boolean> {
const fields = this.getDtoFields(operator, "set");
for (const field in dto) {
if (field in fields) {
if (fields.includes(field)) {
switch (field) {
case "scheduled":
this.post.scheduled = new Date((dto.scheduled as string) ?? "");
Expand Down Expand Up @@ -232,7 +232,7 @@ export default class PostMapper extends AbstractMapper<PostDto> {
break;
}
} else {
throw this.user.log.error("Unknown field: " + field);
this.user.log.trace("Ignoring field: " + field);
}
}
return true;
Expand Down
6 changes: 3 additions & 3 deletions src/mappers/SourceMapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,18 +101,18 @@ export default class SourceMapper extends AbstractMapper<SourceDto> {
* @param dto
* @returns boolean success
*/
async setDto(operator: Operator, dto: SourceDto): Promise<boolean> {
async putDto(operator: Operator, dto: SourceDto): Promise<boolean> {
const fields = this.getDtoFields(operator, "set");
for (const field in dto) {
if (field in fields) {
if (fields.includes(field)) {
switch (field) {
// upload here ?
case "files":
this.source.files = (dto[field] as FileInfo[]) ?? [];
break;
}
} else {
throw this.user.log.error("Unknown field: " + field);
this.user.log.trace("Ignoring field: " + field);
}
}
return true;
Expand Down
6 changes: 3 additions & 3 deletions src/mappers/UserMapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,10 @@ export default class UserMapper extends AbstractMapper<UserDto> {
* @param dto
* @returns boolean success
*/
async setDto(operator: Operator, dto: UserDto): Promise<boolean> {
async putDto(operator: Operator, dto: UserDto): Promise<boolean> {
const fields = this.getDtoFields(operator, "set");
for (const field in dto) {
if (field in fields) {
if (fields.includes(field)) {
switch (field) {
case "id":
// todo - there should be a rename-user command instead
Expand All @@ -91,7 +91,7 @@ export default class UserMapper extends AbstractMapper<UserDto> {
break;
}
} else {
throw this.user.log.error("Unknown field: " + field);
this.user.log.trace("Ignoring field: " + field);
}
}
await this.user.data.save();
Expand Down
25 changes: 19 additions & 6 deletions src/models/User.ts
Original file line number Diff line number Diff line change
Expand Up @@ -242,11 +242,22 @@ export default class User {
if (this.platforms === undefined) {
this.loadPlatforms();
}
const platform = this.platforms?.[platformId];
if (!platform) {
throw this.log.error("Unknown or disabled platform: " + platformId);
let platform = this.platforms?.[platformId];
if (platform) {
return platform;
}
return platform;

Object.values(platformClasses).forEach((platformClass) => {
if (typeof platformClass === "function") {
if (platformClass.id() === platformId) {
platform = new platformClass(this);
}
}
});
if (platform) {
return platform;
}
throw this.log.error("Unknown platform: " + platformId);
}

/**
Expand Down Expand Up @@ -317,10 +328,11 @@ export default class User {

/**
* @returns all data from the settings store
*/

public getSettings(): { [key: string]: string } {
return this.data.getStore("settings");
}
*/

public async promptCliFields(
fields: FieldMapping,
Expand Down Expand Up @@ -348,11 +360,12 @@ export default class User {
/**
* 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();
}
*/
}
4 changes: 2 additions & 2 deletions src/models/User/UserData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ export default class UserData {
}
}

public getStore(storeName: StorageType): { [key: string]: string } {
/*public getStore(storeName: StorageType): { [key: string]: string } {
const storageKey = StorageKeys[storeName];
const storage = process.env[storageKey] ?? "none";
const jsonStore = this.jsonData[storeName];
Expand All @@ -90,7 +90,7 @@ export default class UserData {
"UserData.getStore: Storage " + storage + " not implemented",
);
}
}
}*/

public getObject(store: StorageType, key: string, def?: object): object {
const value = this.get(store, key, JSON.stringify(def));
Expand Down
55 changes: 47 additions & 8 deletions src/services/Fairpost.ts
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ class Fairpost {
break;
}

case "get-settings": {
/*case "get-settings": {
if (!permissions.manageAccount) {
throw new Error("Missing permissions for command " + command);
}
Expand Down Expand Up @@ -186,6 +186,7 @@ class Fairpost {
output = { success: true };
break;
}
*/

case "refresh-token": {
if (!permissions.manageAccount) {
Expand Down Expand Up @@ -228,7 +229,7 @@ class Fairpost {
break;
}

case "add-platform": {
/*case "add-platform": {
if (!permissions.manageFeed) {
throw new Error("Missing permissions for command " + command);
}
Expand Down Expand Up @@ -272,6 +273,7 @@ class Fairpost {
};
break;
}
*/

case "setup-platform": {
if (!permissions.manageFeed) {
Expand Down Expand Up @@ -322,6 +324,46 @@ class Fairpost {
output = await platform.mapper.getDto(operator);
break;
}
case "put-platform": {
if (!permissions.managePlatforms) {
throw new Error("Missing permissions for command " + command);
}
if (!user) {
throw new Error("user is required for command " + command);
}
if (!args.platform) {
throw user.log.error(
"CommandHandler " + command,
"Missing argument: platform",
);
}
if (!args.payload) {
throw user.log.error(
"CommandHandler " + command,
"Missing payload",
);
}
if (
Buffer.isBuffer(args.payload || typeof args.payload === "string")
) {
throw user.log.error(
"CommandHandler " + command,
"Payload must be an object",
);
}
const platform = user.getPlatform(args.platform);
output = {
[args.platform]: {
success: await platform.mapper.putDto(
operator,
args.payload as PlatformDto,
),
result: await platform.mapper.getDto(operator),
},
};
break;
}

case "get-platforms": {
if (!permissions.managePlatforms) {
throw new Error("Missing permissions for command " + command);
Expand Down Expand Up @@ -930,17 +972,14 @@ class Fairpost {
`${cmd} help`,
`${cmd} @userid get-user`,
`${cmd} @userid get-feed`,
`${cmd} @userid add-platform --platform=xxx`,
`${cmd} @userid remove-platform --platform=xxx`,
`${cmd} @userid get-platform --platform=xxx`,
`${cmd} @userid put-platform --platform=xxx << payload`,
`${cmd} @userid get-platforms [--platforms=xxx,xxx]`,
`${cmd} @userid setup-platform --platform=xxx`,
`${cmd} @userid test-platform --platform=xxx`,
`${cmd} @userid test-platforms [--platforms=xxx,xxx]`,
`${cmd} @userid refresh-platform --platform=xxx`,
`${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
8 changes: 1 addition & 7 deletions src/utilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,7 @@ export async function parsePayload(

// Heuristic fallback: Try JSON first
try {
const trimmed = str.trim();
if (
(trimmed.startsWith("{") && trimmed.endsWith("}")) ||
(trimmed.startsWith("[") && trimmed.endsWith("]"))
) {
return JSON.parse(trimmed);
}
return JSON.parse(str);
} catch {
// Ignore, not valid JSON
}
Expand Down