Skip to content

Commit

Permalink
Changes to not clobber global vars
Browse files Browse the repository at this point in the history
  • Loading branch information
aneillans committed Dec 13, 2023
1 parent 82707cf commit 09fa2c6
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 20 deletions.
3 changes: 3 additions & 0 deletions commons/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,3 +102,6 @@ export * from './models/default-file-options.entity';
// Website Option Entities
import * as WebsiteOptions from './websites/websites';
export { WebsiteOptions };

// Doesn't really fit :P
export * from './websites/megalodon/megalodon.instancesettings';
5 changes: 5 additions & 0 deletions commons/src/websites/megalodon/megalodon.instancesettings.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export class MegalodonInstanceSettings {
maxChars?: number = undefined;
maxImages?: number = undefined;
}

9 changes: 7 additions & 2 deletions electron-app/src/server/websites/mastodon/mastodon.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ import { Injectable } from '@nestjs/common';
import {
FileRecord,
FileSubmissionType,
MegalodonInstanceSettings,
} from 'postybirb-commons';
import { ScalingOptions } from '../interfaces/scaling-options.interface';
import _ from 'lodash';
import { Megalodon } from '../megalodon/megalodon.service';
import { } from 'postybirb-commons';

const INFO_KEY = 'INSTANCE INFO';

Expand Down Expand Up @@ -60,8 +62,11 @@ export class Mastodon extends Megalodon {
getInstanceSettings(accountId: string) {
const instanceInfo: MastodonInstanceInfo = this.getAccountInfo(accountId, INFO_KEY);

this.MAX_CHARS = instanceInfo?.configuration?.statuses?.max_characters ?? 500;
this.maxMediaCount = instanceInfo ? instanceInfo?.configuration?.statuses?.max_media_attachments : 4;
let result = new MegalodonInstanceSettings();
result.maxChars = instanceInfo?.configuration?.statuses?.max_characters ?? 500;
result.maxImages = instanceInfo ? instanceInfo?.configuration?.statuses?.max_media_attachments : 4;

return result;
}

getScalingOptions(file: FileRecord, accountId: string): ScalingOptions {
Expand Down
29 changes: 15 additions & 14 deletions electron-app/src/server/websites/megalodon/megalodon.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
Submission,
SubmissionPart,
SubmissionRating,
MegalodonInstanceSettings,
} from 'postybirb-commons';
import { ScalingOptions } from '../interfaces/scaling-options.interface';
import UserAccountEntity from 'src/server//account/models/user-account.entity';
Expand Down Expand Up @@ -42,10 +43,10 @@ export abstract class Megalodon extends Website {
}

megalodonService: 'mastodon' | 'pleroma' | 'misskey' | 'friendica' = 'mastodon'; // Set this as appropriate in your constructor
maxMediaCount = 4;
maxMediaCount = undefined;

readonly BASE_URL: string;
MAX_CHARS: number = 500;
MAX_CHARS: number = undefined;
readonly enableAdvertisement = false;
readonly acceptsAdditionalFiles = true;
readonly defaultDescriptionParser = PlaintextParser.parse;
Expand Down Expand Up @@ -85,15 +86,15 @@ export abstract class Megalodon extends Website {

abstract getScalingOptions(file: FileRecord, accountId: string): ScalingOptions;

abstract getInstanceSettings(accountId: string);
abstract getInstanceSettings(accountId: string) : MegalodonInstanceSettings;

async postFileSubmission(
cancellationToken: CancellationToken,
data: FilePostData<MastodonFileOptions>,
accountData: MegalodonAccountData,
): Promise<PostResponse> {
this.logger.log("Posting a file")
this.getInstanceSettings(data.part.accountId);
const instanceSettings = this.getInstanceSettings(data.part.accountId);

const M = generator(this.megalodonService, accountData.website, accountData.token);

Expand All @@ -105,10 +106,10 @@ export abstract class Megalodon extends Website {
}

const isSensitive = data.rating !== SubmissionRating.GENERAL;
const chunks = _.chunk(uploadedMedias, this.maxMediaCount);
const chunks = _.chunk(uploadedMedias, instanceSettings.maxImages);
let status = `${data.options.useTitle && data.title ? `${data.title}\n` : ''}${
data.description
}`.substring(0, this.MAX_CHARS);
}`.substring(0, instanceSettings.maxChars);
let lastId = '';
let source = '';
const replyToId = this.getPostIdFromUrl(data.options.replyToUrl);
Expand Down Expand Up @@ -199,7 +200,7 @@ export abstract class Megalodon extends Website {
submissionPart: SubmissionPart<MastodonFileOptions>,
defaultPart: SubmissionPart<DefaultOptions>,
): ValidationParts {
this.getInstanceSettings(submissionPart.accountId);
const instanceSettings = this.getInstanceSettings(submissionPart.accountId);

const problems: string[] = [];
const warnings: string[] = [];
Expand All @@ -209,16 +210,16 @@ export abstract class Megalodon extends Website {
FormContent.getDescription(defaultPart.data.description, submissionPart.data.description),
);

if (description.length > this.MAX_CHARS) {
if (description.length > instanceSettings.maxChars) {
warnings.push(
`Max description length allowed is ${this.MAX_CHARS} characters.`,
`Max description length allowed is ${instanceSettings.maxChars} characters.`,
);
} else {
this.validateInsertTags(
warnings,
this.formatTags(FormContent.getTags(defaultPart.data.tags, submissionPart.data.tags)),
description,
this.MAX_CHARS,
instanceSettings.maxChars,
);
}

Expand Down Expand Up @@ -286,7 +287,7 @@ export abstract class Megalodon extends Website {
submissionPart: SubmissionPart<MastodonNotificationOptions>,
defaultPart: SubmissionPart<DefaultOptions>,
): ValidationParts {
this.getInstanceSettings(submissionPart.accountId);
const instanceSettings = this.getInstanceSettings(submissionPart.accountId);

const problems = [];
const warnings = [];
Expand All @@ -295,16 +296,16 @@ export abstract class Megalodon extends Website {
FormContent.getDescription(defaultPart.data.description, submissionPart.data.description),
);

if (description.length > this.MAX_CHARS) {
if (description.length > instanceSettings.maxChars) {
warnings.push(
`Max description length allowed is ${this.MAX_CHARS} characters.`,
`Max description length allowed is ${instanceSettings.maxChars} characters.`,
);
} else {
this.validateInsertTags(
warnings,
this.formatTags(FormContent.getTags(defaultPart.data.tags, submissionPart.data.tags)),
description,
this.MAX_CHARS,
instanceSettings.maxChars,
);
}

Expand Down
8 changes: 6 additions & 2 deletions electron-app/src/server/websites/pixelfed/pixelfed.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Injectable } from '@nestjs/common';
import {
FileRecord,
FileSubmissionType,
MegalodonInstanceSettings,
} from 'postybirb-commons';
import { ScalingOptions } from '../interfaces/scaling-options.interface';
import FileSize from 'src/server/utils/filesize.util';
Expand Down Expand Up @@ -35,8 +36,11 @@ export class Pixelfed extends Megalodon {
getInstanceSettings(accountId: string) {
const instanceInfo: PixelfedInstanceInfo = this.getAccountInfo(accountId, INFO_KEY);

this.MAX_CHARS = instanceInfo?.configuration?.statuses?.max_characters ?? 500;
this.maxMediaCount = instanceInfo ? instanceInfo?.configuration?.statuses?.max_media_attachments : 4;
let result = new MegalodonInstanceSettings();
result.maxChars = instanceInfo?.configuration?.statuses?.max_characters ?? 500;
result.maxImages = instanceInfo ? instanceInfo?.configuration?.statuses?.max_media_attachments : 4;

return result;
}

getScalingOptions(file: FileRecord, accountId: string): ScalingOptions {
Expand Down
8 changes: 6 additions & 2 deletions electron-app/src/server/websites/pleroma/pleroma.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Injectable } from '@nestjs/common';
import {
FileRecord,
FileSubmissionType,
MegalodonInstanceSettings,
} from 'postybirb-commons';
import { ScalingOptions } from '../interfaces/scaling-options.interface';
import FileSize from 'src/server/utils/filesize.util';
Expand Down Expand Up @@ -45,8 +46,11 @@ export class Pleroma extends Megalodon {
console.log(this.getAccountInfo(accountId, INFO_KEY));
const instanceInfo: PleromaInstanceInfo = this.getAccountInfo(accountId, INFO_KEY);

this.MAX_CHARS = instanceInfo?.max_toot_chars ?? 500;
this.maxMediaCount = instanceInfo?.max_media_attachments ?? 4;
let result = new MegalodonInstanceSettings();
result.maxChars = instanceInfo?.max_toot_chars ?? 500;
result.maxImages = instanceInfo?.max_media_attachments ?? 4;

return result;
}

getScalingOptions(file: FileRecord, accountId: string): ScalingOptions {
Expand Down

0 comments on commit 09fa2c6

Please sign in to comment.