Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Improve platform setup #51

Merged
merged 6 commits into from
Dec 9, 2023
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: 1 addition & 1 deletion src/platforms/Ayrshare/AsFacebook.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as fs from "fs";
import * as sharp from "sharp";

import Ayrshare from "../Ayrshare";
import Ayrshare from "./Ayrshare";
import Folder from "../../models/Folder";
import { PlatformId } from "..";
import Post from "../../models/Post";
Expand Down
2 changes: 1 addition & 1 deletion src/platforms/Ayrshare/AsInstagram.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as path from "path";
import * as sharp from "sharp";

import Ayrshare from "../Ayrshare";
import Ayrshare from "./Ayrshare";
import Folder from "../../models/Folder";
import Logger from "../../services/Logger";
import { PlatformId } from "..";
Expand Down
2 changes: 1 addition & 1 deletion src/platforms/Ayrshare/AsLinkedIn.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as fs from "fs";
import * as sharp from "sharp";

import Ayrshare from "../Ayrshare";
import Ayrshare from "./Ayrshare";
import Folder from "../../models/Folder";
import Logger from "../../services/Logger";
import { PlatformId } from "..";
Expand Down
4 changes: 2 additions & 2 deletions src/platforms/Ayrshare/AsReddit.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import Ayrshare from "../Ayrshare";
import Ayrshare from "./Ayrshare";
import Folder from "../../models/Folder";
import { PlatformId } from "..";
import Post from "../../models/Post";
Expand All @@ -13,7 +13,7 @@ export default class AsReddit extends Ayrshare {

constructor() {
super();
this.SUBREDDIT = Storage.get("settings", "AYRSHARE_SUBREDDIT");
this.SUBREDDIT = Storage.get("settings", "AYRSHARE_SUBREDDIT", "");
}

async preparePost(folder: Folder): Promise<Post | undefined> {
Expand Down
2 changes: 1 addition & 1 deletion src/platforms/Ayrshare/AsTikTok.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import Ayrshare from "../Ayrshare";
import Ayrshare from "./Ayrshare";
import Folder from "../../models/Folder";
import { PlatformId } from "..";
import Post from "../../models/Post";
Expand Down
2 changes: 1 addition & 1 deletion src/platforms/Ayrshare/AsTwitter.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as fs from "fs";
import * as sharp from "sharp";

import Ayrshare from "../Ayrshare";
import Ayrshare from "./Ayrshare";
import Folder from "../../models/Folder";
import Logger from "../../services/Logger";
import { PlatformId } from "..";
Expand Down
2 changes: 1 addition & 1 deletion src/platforms/Ayrshare/AsYouTube.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import Ayrshare from "../Ayrshare";
import Ayrshare from "./Ayrshare";
import Folder from "../../models/Folder";
import { PlatformId } from "..";
import Post from "../../models/Post";
Expand Down
14 changes: 7 additions & 7 deletions src/platforms/Ayrshare.ts → src/platforms/Ayrshare/Ayrshare.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import * as fs from "fs";
import * as path from "path";

import Folder from "../models/Folder";
import Logger from "../services/Logger";
import Platform from "../models/Platform";
import { PlatformId } from ".";
import Post from "../models/Post";
import { PostStatus } from "../models/Post";
import Storage from "../services/Storage";
import Folder from "../../models/Folder";
import Logger from "../../services/Logger";
import Platform from "../../models/Platform";
import { PlatformId } from "..";
import Post from "../../models/Post";
import { PostStatus } from "../../models/Post";
import Storage from "../../services/Storage";
import { randomUUID } from "crypto";

/**
Expand Down
171 changes: 19 additions & 152 deletions src/platforms/Facebook.ts → src/platforms/Facebook/Facebook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@ import * as fs from "fs";
import * as path from "path";
import * as sharp from "sharp";

import FacebookAuth from "../auth/FacebookAuth";
import Folder from "../models/Folder";
import Logger from "../services/Logger";
import Platform from "../models/Platform";
import { PlatformId } from ".";
import Post from "../models/Post";
import { PostStatus } from "../models/Post";
import Storage from "../services/Storage";
import FacebookApi from "./FacebookApi";
import FacebookAuth from "./FacebookAuth";
import Folder from "../../models/Folder";
import Logger from "../../services/Logger";
import Platform from "../../models/Platform";
import { PlatformId } from "..";
import Post from "../../models/Post";
import { PostStatus } from "../../models/Post";
import Storage from "../../services/Storage";

/**
* Facebook: support for facebook platform.
Expand All @@ -20,21 +21,23 @@ import Storage from "../services/Storage";
*/
export default class Facebook extends Platform {
id: PlatformId = PlatformId.FACEBOOK;
GRAPH_API_VERSION: string = "v18.0";
api: FacebookApi;
auth: FacebookAuth;

constructor() {
super();
this.auth = new FacebookAuth();
this.api = new FacebookApi();
}

/** @inheritdoc */
async setup() {
const auth = new FacebookAuth();
await auth.setup();
await this.auth.setup();
}

/** @inheritdoc */
async test() {
return this.get("me");
return this.api.get("me");
}

/** @inheritdoc */
Expand Down Expand Up @@ -67,7 +70,7 @@ export default class Facebook extends Platform {

/** @inheritdoc */
async publishPost(post: Post, dryrun: boolean = false): Promise<boolean> {
Logger.trace("Facebook.publishPost", post, dryrun);
Logger.trace("Facebook.publishPost", post.id, dryrun);

let response = dryrun
? { id: "-99" }
Expand Down Expand Up @@ -99,7 +102,7 @@ export default class Facebook extends Platform {
}
}
if (!dryrun) {
response = (await this.postJson("%PAGE%/feed", {
response = (await this.api.postJson("%PAGE%/feed", {
message: post.body,
published: Storage.get("settings", "FACEBOOK_PUBLISH_POSTS"),
attached_media: attachments,
Expand Down Expand Up @@ -154,7 +157,7 @@ export default class Facebook extends Platform {
body.set("published", published ? "true" : "false");
body.set("source", blob, path.basename(file));

const result = (await this.postFormData("%PAGE%/photos", body)) as {
const result = (await this.api.postFormData("%PAGE%/photos", body)) as {
id: "string";
};

Expand Down Expand Up @@ -190,7 +193,7 @@ export default class Facebook extends Platform {
body.set("published", Storage.get("settings", "FACEBOOK_PUBLISH_POSTS"));
body.set("source", blob, path.basename(file));

const result = (await this.postFormData("%PAGE%/videos", body)) as {
const result = (await this.api.postFormData("%PAGE%/videos", body)) as {
id: string;
};

Expand All @@ -199,140 +202,4 @@ export default class Facebook extends Platform {
}
return result;
}

// API implementation -------------------

/**
* Do a GET request on the graph.
* @param endpoint - the path to call
* @param query - query string as object
*/

private async get(
endpoint: string = "%PAGE%",
query: { [key: string]: string } = {},
): Promise<object> {
endpoint = endpoint.replace(
"%PAGE%",
Storage.get("settings", "FACEBOOK_PAGE_ID"),
);

const url = new URL("https://graph.facebook.com");
url.pathname = this.GRAPH_API_VERSION + "/" + endpoint;
url.search = new URLSearchParams(query).toString();
Logger.trace("GET", url.href);
return await fetch(url, {
method: "GET",
headers: {
Accept: "application/json",
Authorization:
"Bearer " + Storage.get("auth", "FACEBOOK_PAGE_ACCESS_TOKEN"),
},
})
.then((res) => this.handleApiResponse(res))
.catch((err) => this.handleApiError(err));
}

/**
* Do a Json POST request on the graph.
* @param endpoint - the path to call
* @param body - body as object
*/

private async postJson(
endpoint: string = "%PAGE%",
body = {},
): Promise<object> {
endpoint = endpoint.replace(
"%PAGE%",
Storage.get("settings", "FACEBOOK_PAGE_ID"),
);

const url = new URL("https://graph.facebook.com");
url.pathname = this.GRAPH_API_VERSION + "/" + endpoint;
Logger.trace("POST", url.href);
return await fetch(url, {
method: "POST",
headers: {
Accept: "application/json",
"Content-Type": "application/json",
Authorization:
"Bearer " + Storage.get("settings", "FACEBOOK_PAGE_ACCESS_TOKEN"),
},
body: JSON.stringify(body),
})
.then((res) => this.handleApiResponse(res))
.catch((err) => this.handleApiError(err));
}

/**
* Do a FormData POST request on the graph.
* @param endpoint - the path to call
* @param body - body as object
*/

private async postFormData(
endpoint: string,
body: FormData,
): Promise<object> {
endpoint = endpoint.replace(
"%PAGE%",
Storage.get("settings", "FACEBOOK_PAGE_ID"),
);

const url = new URL("https://graph.facebook.com");
url.pathname = this.GRAPH_API_VERSION + "/" + endpoint;
Logger.trace("POST", url.href);

return await fetch(url, {
method: "POST",
headers: {
Accept: "application/json",
Authorization:
"Bearer " + Storage.get("settings", "FACEBOOK_PAGE_ACCESS_TOKEN"),
},
body: body,
})
.then((res) => this.handleApiResponse(res))
.catch((err) => this.handleApiError(err));
}

/**
* Handle api response
* @param response - api response from fetch
* @returns parsed object from response
*/
private async handleApiResponse(response: Response): Promise<object> {
if (!response.ok) {
throw Logger.error(
"Facebook.handleApiResponse",
response,
response.status + ":" + response.statusText,
);
}
const data = await response.json();
if (data.error) {
const error =
response.status +
":" +
data.error.type +
"(" +
data.error.code +
"/" +
data.error.error_subcode +
") " +
data.error.message;
throw Logger.error("Facebook.handleApiResponse", error);
}
Logger.trace("Facebook.handleApiResponse", "success");
return data;
}

/**
* Handle api error
* @param error - the error returned from fetch
*/
private handleApiError(error: Error): never {
throw Logger.error("Facebook.handleApiError", error);
}
}
Loading