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

# feed settings
Expand Down
105 changes: 61 additions & 44 deletions src/platforms/Ayrshare/Ayrshare.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
import * as fs from "fs";
import * as path from "path";

import {
ApiResponseError,
handleApiError,
handleJsonResponse,
} from "../../utilities";

import Folder from "../../models/Folder";
import Logger from "../../services/Logger";
import Platform from "../../models/Platform";
Expand Down Expand Up @@ -35,6 +41,21 @@ export default abstract class Ayrshare extends Platform {
super();
}

/** @inheritdoc */
async test() {
const APIKEY = Storage.get("settings", "AYRSHARE_API_KEY");
return await fetch("https://app.ayrshare.com/api/user", {
method: "GET",
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${APIKEY}`,
},
})
.then((res) => handleJsonResponse(res))
.catch((err) => this.handleAyrshareError(err))
.catch((err) => handleApiError(err));
}

/** @inheritdoc */
async preparePost(folder: Folder): Promise<Post | undefined> {
return super.preparePost(folder);
Expand Down Expand Up @@ -114,8 +135,9 @@ export default abstract class Ayrshare extends Platform {
},
},
)
.then((res) => this.handleApiResponse(res))
.catch((err) => this.handleApiError(err))) as {
.then((res) => handleJsonResponse(res))
.catch((err) => this.handleAyrshareError(err))
.catch((err) => handleApiError(err))) as {
uploadUrl: string;
contentType: string;
accessUrl: string;
Expand Down Expand Up @@ -180,8 +202,9 @@ export default abstract class Ayrshare extends Platform {
},
body: body,
})
.then((res) => this.handleApiResponse(res))
.catch((err) => this.handleApiError(err))) as {
.then((res) => handleJsonResponse(res))
.catch((err) => this.handleAyrshareError(err))
.catch((err) => handleApiError(err))) as {
id: string;
status?: string;
};
Expand All @@ -197,49 +220,43 @@ export default abstract class Ayrshare extends Platform {
}

/**
* Handle api response
* @param response - the api response
* @returns parsed data from response
* Handle api error
*
* Improve error message and rethrow it.
* @param error - ApiResponseError
*/
private async handleApiResponse(response: Response): Promise<object> {
if (!response.ok) {
throw Logger.error(
"Ayrshare.handleApiResponse",
response,
response.status + ":" + response.statusText,
);
}
const data = await response.json();
if (data.status === "error") {
let error = response.status + ":";
data.status.errors.forEach(
(err: {
action: string;
platform: string;
code: number;
message: string;
}) => {
error +=
err.action +
private async handleAyrshareError(error: ApiResponseError): Promise<never> {
if (error.responseData) {
if (error.responseData.status === "error") {
error.message += ": ";
if (error.responseData.errors) {
error.responseData.errors.forEach(
(err: {
action: string;
platform: string;
code: number;
message: string;
}) => {
error.message +=
err.action +
"(" +
err.code +
"/" +
err.platform +
") " +
err.message;
},
);
} else {
error.message +=
error.responseData.action +
"(" +
err.code +
"/" +
err.platform +
error.responseData.code +
") " +
err.message;
},
);
throw Logger.error("Ayrshare.handleApiResponse", error);
error.responseData.message;
}
}
}
Logger.trace("Ayrshare.handleApiResponse", "success");
return data;
}

/**
* Handle api error
* @param error - the error thrown from the api
*/
private handleApiError(error: Error) {
throw Logger.error("Ayrshare.handleApiError", error);
throw error;
}
}
5 changes: 3 additions & 2 deletions src/platforms/Facebook/Facebook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ export default class Facebook extends Platform {

/** @inheritdoc */
async preparePost(folder: Folder): Promise<Post | undefined> {
Logger.trace("Facebook.preparePost", folder.id);
const post = await super.preparePost(folder);
if (post && post.files) {
// facebook: video post can only contain 1 video
Expand Down Expand Up @@ -157,7 +158,7 @@ export default class Facebook extends Platform {
body.set("published", published ? "true" : "false");
body.set("source", blob, path.basename(file));

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

Expand Down Expand Up @@ -193,7 +194,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.api.postFormData("%PAGE%/videos", body)) as {
const result = (await this.api.postForm("%PAGE%/videos", body)) as {
id: string;
};

Expand Down
79 changes: 36 additions & 43 deletions src/platforms/Facebook/FacebookApi.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
import {
ApiResponseError,
handleApiError,
handleJsonResponse,
} from "../../utilities";

import Logger from "../../services/Logger";
import Storage from "../../services/Storage";

Expand Down Expand Up @@ -35,8 +41,9 @@ export default class FacebookApi {
"Bearer " + Storage.get("auth", "FACEBOOK_PAGE_ACCESS_TOKEN"),
},
})
.then((res) => this.handleApiResponse(res))
.catch((err) => this.handleApiError(err));
.then((res) => handleJsonResponse(res))
.catch((err) => this.handleFacebookError(err))
.catch((err) => handleApiError(err));
}

/**
Expand All @@ -63,12 +70,13 @@ export default class FacebookApi {
Accept: "application/json",
"Content-Type": "application/json",
Authorization:
"Bearer " + Storage.get("settings", "FACEBOOK_PAGE_ACCESS_TOKEN"),
"Bearer " + Storage.get("auth", "FACEBOOK_PAGE_ACCESS_TOKEN"),
},
body: JSON.stringify(body),
})
.then((res) => this.handleApiResponse(res))
.catch((err) => this.handleApiError(err));
.then((res) => handleJsonResponse(res))
.catch((err) => this.handleFacebookError(err))
.catch((err) => handleApiError(err));
}

/**
Expand All @@ -77,7 +85,7 @@ export default class FacebookApi {
* @param body - body as object
*/

public async postFormData(endpoint: string, body: FormData): Promise<object> {
public async postForm(endpoint: string, body: FormData): Promise<object> {
endpoint = endpoint.replace(
"%PAGE%",
Storage.get("settings", "FACEBOOK_PAGE_ID"),
Expand All @@ -92,50 +100,35 @@ export default class FacebookApi {
headers: {
Accept: "application/json",
Authorization:
"Bearer " + Storage.get("settings", "FACEBOOK_PAGE_ACCESS_TOKEN"),
"Bearer " + Storage.get("auth", "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;
.then((res) => handleJsonResponse(res))
.catch((err) => this.handleFacebookError(err))
.catch((err) => handleApiError(err));
}

/**
* Handle api error
* @param error - the error returned from fetch
*
* Improve error message and rethrow it.
* @param error - ApiResponseError
*/
private handleApiError(error: Error): never {
throw Logger.error("Facebook.handleApiError", error);
private async handleFacebookError(error: ApiResponseError): Promise<never> {
if (error.responseData) {
if (error.responseData.error) {
error.message +=
": " +
error.responseData.error.type +
" (" +
error.responseData.error.code +
"/" +
(error.responseData.error.error_subcode || "0") +
"): " +
error.responseData.error.message;
}
}
throw error;
}
}
Loading