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
30 changes: 27 additions & 3 deletions src/platforms/Reddit/Reddit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,20 @@ export default class Reddit extends Platform {
set: ["managePlatforms"],
required: true,
},
REDDIT_FLAIR: {
type: "string",
label: "Flair to add to posts",
get: ["managePlatforms"],
set: ["managePlatforms"],
required: true,
},
REDDIT_FLAIR_TEXT: {
type: "string",
label: "Text to add to flair",
get: ["managePlatforms"],
set: ["managePlatforms"],
required: true,
},
REDDIT_PLUGIN_SETTINGS: {
type: "json",
label: "Reddit Plugin settings",
Expand All @@ -45,12 +59,16 @@ export default class Reddit extends Platform {
},
};
SUBREDDIT: string;
FLAIR: string;
FLAIR_TEXT: string;
api: RedditApi;
auth: RedditAuth;

constructor(user: User) {
super(user);
this.SUBREDDIT = this.user.data.get("settings", "REDDIT_SUBREDDIT", "");
this.FLAIR = this.user.data.get("settings", "REDDIT_FLAIR", "");
this.FLAIR_TEXT = this.user.data.get("settings", "REDDIT_FLAIR_TEXT", "");
this.api = new RedditApi(user);
this.auth = new RedditAuth(user);
this.mapper = new PlatformMapper(this);
Expand Down Expand Up @@ -226,7 +244,9 @@ export default class Reddit extends Platform {
const body = post.getCompiledBody("!title");
if (!dryrun) {
const response = (await this.api.post("submit", {
sr: this.SUBREDDIT,
sr: this.SUBREDDIT || undefined,
flair_id: this.FLAIR || undefined,
flair_text: this.FLAIR_TEXT || undefined,
kind: "self",
title: title,
text: body,
Expand Down Expand Up @@ -266,7 +286,9 @@ export default class Reddit extends Platform {
const imageUrl = await this.uploadFile(leash, file);
if (!dryrun) {
const response = (await this.api.post("submit", {
sr: this.SUBREDDIT,
sr: this.SUBREDDIT || undefined,
flair_id: this.FLAIR || undefined,
flair_text: this.FLAIR_TEXT || undefined,
kind: "image",
title: title,
url: imageUrl,
Expand Down Expand Up @@ -314,7 +336,9 @@ export default class Reddit extends Platform {
const videoUrl = await this.uploadFile(leash, file);
if (!dryrun) {
const response = (await this.api.post("submit", {
sr: this.SUBREDDIT,
sr: this.SUBREDDIT || undefined,
flair_id: this.FLAIR || undefined,
flair_text: this.FLAIR_TEXT || undefined,
kind: "video",
title: title,
url: videoUrl,
Expand Down
8 changes: 6 additions & 2 deletions src/platforms/Reddit/RedditApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ export default class RedditApi {

public async post(
endpoint: string,
body: { [key: string]: string },
body: { [key: string]: string | undefined },
): Promise<object> {
const url = new URL("https://oauth.reddit.com");
//url.pathname = "api/" + this.API_VERSION + "/" + endpoint;
Expand All @@ -66,6 +66,10 @@ export default class RedditApi {
const accessToken = this.user.data.get("auth", "REDDIT_ACCESS_TOKEN");
this.user.log.trace("POST", url.href);

const cleanBody = Object.fromEntries(
Object.entries(body).filter(([, v]) => v !== undefined),
) as { [key: string]: string };

return await fetch(url, {
method: "POST",
headers: {
Expand All @@ -74,7 +78,7 @@ export default class RedditApi {
Authorization: "Bearer " + accessToken,
"User-Agent": this.user.data.get("app", "OAUTH_USERAGENT"),
},
body: new URLSearchParams(body),
body: new URLSearchParams(cleanBody),
})
.then((res) => handleJsonResponse(res))
.catch((err) => this.handleRedditError(err))
Expand Down
Loading