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
3 changes: 2 additions & 1 deletion .env.dist
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ FAIRPOST_LOGGER_CATEGORY=default
FAIRPOST_LOGGER_LEVEL=trace
FAIRPOST_LOGGER_CONSOLE=false
FAIRPOST_STORAGE_SETTINGS=env
FAIRPOST_STORAGE_AUTH=env
FAIRPOST_STORAGE_AUTH=json
FAIRPOST_STORAGE_JSON=var/run/storage.json
FAIRPOST_USER_AGENT=Fairpost 1.0

# feed settings
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,8 @@ fairpost.js setup-platform --platform=xxx
fairpost.js setup-platforms [--platforms=xxx,xxx]
fairpost.js test-platform --platform=xxx
fairpost.js test-platforms [--platforms=xxx,xxx]
fairpost.js refresh-platform --platform=xxx
fairpost.js refresh-platforms [--platforms=xxx,xxx]
fairpost.js get-platform --platform=xxx
fairpost.js get-platforms [--platforms=xxx,xxx]
fairpost.js get-folder --folder=xxx
Expand Down
12 changes: 12 additions & 0 deletions src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,16 @@ async function main() {
report = "Result: \n" + JSON.stringify(result, null, "\t");
break;
}
case "refresh-platform": {
result = await feed.refreshPlatform(PLATFORM);
report = "Result: \n" + JSON.stringify(result, null, "\t");
break;
}
case "refresh-platforms": {
result = await feed.refreshPlatforms(PLATFORMS);
report = "Result: \n" + JSON.stringify(result, null, "\t");
break;
}
case "get-folder": {
const folder = feed.getFolder(FOLDER);
report += folder.report() + "\n";
Expand Down Expand Up @@ -221,6 +231,8 @@ async function main() {
`${cmd} setup-platforms [--platforms=xxx,xxx]`,
`${cmd} test-platform --platform=xxx`,
`${cmd} test-platforms [--platforms=xxx,xxx]`,
`${cmd} refresh-platform --platform=xxx`,
`${cmd} refresh-platforms [--platforms=xxx,xxx]`,
`${cmd} get-platform --platform=xxx`,
`${cmd} get-platforms [--platforms=xxx,xxx]`,
`${cmd} get-folder --folder=xxx`,
Expand Down
35 changes: 32 additions & 3 deletions src/models/Feed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,8 @@ export default class Feed {
): Promise<{ [id: string]: unknown }> {
Logger.trace("Feed", "setupPlatforms", platformsIds);
const results = {};
for (const platformId of platformsIds) {
for (const platformId of platformsIds ??
(Object.keys(this.platforms) as PlatformId[])) {
results[platformId] = await this.setupPlatform(platformId);
}
return results;
Expand Down Expand Up @@ -125,7 +126,7 @@ export default class Feed {
}

/**
* Test more platforms
* Test multiple platforms
* @param platformsIds - the slugs of the platforms
* @returns the test results indexed by platform ids
*/
Expand All @@ -134,12 +135,40 @@ export default class Feed {
): Promise<{ [id: string]: unknown }> {
Logger.trace("Feed", "testPlatforms", platformsIds);
const results = {};
for (const platformId of platformsIds) {
for (const platformId of platformsIds ??
(Object.keys(this.platforms) as PlatformId[])) {
results[platformId] = await this.testPlatform(platformId);
}
return results;
}

/**
* Refresh one platform
* @param platformId - the slug of the platform
* @returns the refresh result
*/
async refreshPlatform(platformId: PlatformId): Promise<boolean> {
Logger.trace("Feed", "refreshPlatform", platformId);
return await this.getPlatform(platformId).refresh();
}

/**
* Refresh multiple platforms
* @param platformsIds - the slugs of the platforms
* @returns the refresh results indexed by platform ids
*/
async refreshPlatforms(
platformsIds?: PlatformId[],
): Promise<{ [id: string]: boolean }> {
Logger.trace("Feed", "refreshPlatforms", platformsIds);
const results = {};
for (const platformId of platformsIds ??
(Object.keys(this.platforms) as PlatformId[])) {
results[platformId] = await this.refreshPlatform(platformId);
}
return results;
}

/**
* Get all folders
* @returns all folder in the feed
Expand Down
68 changes: 40 additions & 28 deletions src/models/Platform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,46 @@ export default class Platform {
return report;
}

/**
* setup
*
* Set the platform up. Get the required keys and tokens.
* This may involve starting a webserver and/or communicating
* via the CLI.
* @returns - any object
*/
async setup() {
throw Logger.error(
"No setup implemented for " +
this.id +
". Read the docs in the docs folder.",
);
}

/**
* test
*
* Test the platform installation. This should not post
* anything, but test access tokens et al. It can return
* anything.
* @returns - any object
*/
async test(): Promise<unknown> {
return "No tests implemented for " + this.id;
}

/**
* refresh
*
* Refresh the platform installation. This usually refreshes
* access tokens if required. It can throw errors
* @returns - true if refreshed
*/
async refresh(): Promise<boolean> {
Logger.trace("Refresh not implemented for " + this.id);
return false;
}

/**
* getAssetsFolderName
* @returns the relative path to a folder used
Expand Down Expand Up @@ -159,32 +199,4 @@ export default class Platform {
post.save();
return false;
}

/**
* setup
*
* Set the platform up. Get the required keys and tokens.
* This may involve starting a webserver and/or communicating
* via the CLI.
* @returns - any object
*/
async setup() {
throw Logger.error(
"No setup implemented for " +
this.id +
". Read the docs in the docs folder.",
);
}

/**
* test
*
* Test the platform installation. This should not post
* anything, but test access tokens et al. It can return
* anything.
* @returns - any object
*/
async test(): Promise<unknown> {
return "No tests implemented for " + this.id;
}
}
6 changes: 6 additions & 0 deletions src/platforms/Reddit/Reddit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,12 @@ export default class Reddit extends Platform {
};
}

/** @inheritdoc */
async refresh(): Promise<boolean> {
await this.auth.refreshAccessToken();
return true;
}

async preparePost(folder: Folder): Promise<Post | undefined> {
const post = await super.preparePost(folder);
if (post) {
Expand Down
23 changes: 9 additions & 14 deletions src/platforms/Reddit/RedditAuth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,11 @@ export default class RedditAuth {
Storage.set("auth", "REDDIT_REFRESH_TOKEN", tokens["refresh_token"]);
}

public async getAccessToken(): Promise<string> {
return await this.refreshAccessToken();
}

/**
* Get Reddit Access token
* Refresh Reddit Access token
*
* Reddits access token expire in 24 hours. Instead
* of using an access token from the Storage, the Reddit
* platform gets its token from here, which refreshes
* it if needed using the refresh_token
*
* ~~ TODO the api cant access these
* Reddits access token expire in 24 hours.
* Refresh this regularly.
* @returns The access token
*/
public async refreshAccessToken(): Promise<string> {
Expand All @@ -35,15 +27,18 @@ export default class RedditAuth {
}
const result = await this.post("access_token", {
grant_type: "refresh_token",
refresh_token: Storage.get("settings", "REDDIT_REFRESH_TOKEN"),
refresh_token: Storage.get("auth", "REDDIT_REFRESH_TOKEN"),
});

if (!result["access_token"]) {
const msg = "Remote response did not return a access_token";
throw Logger.error(msg, result);
}
this.accessToken = result["access_token"];
return this.accessToken;
const accessToken = result["access_token"];
if (!accessToken) {
throw new Error("RedditAuth: refresh failed - no access token");
}
Storage.set("auth", "REDDIT_ACCESS_TOKEN", accessToken);
}

protected async requestCode(): Promise<string> {
Expand Down
3 changes: 1 addition & 2 deletions src/services/OAuth2Service.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import * as fs from "fs";
import * as http from "http";
import * as url from "url";

import Storage from "./Storage";

class DeferredResponseQuery {
Expand Down Expand Up @@ -39,8 +40,6 @@ export default class OAuth2Service {
* resolves the query passed.
* @param serviceName - the name of the remote platform
* @param serviceLink - the uri to the remote platform
* @param clientHost - the host name to serve the local page on
* @param clientPort - the port to serve the local page on
* @returns a flat object of returned query
*/

Expand Down
Loading