From 09fa2c6f92916ee2f707a9fb586561b55bc17843 Mon Sep 17 00:00:00 2001 From: Andy Neillans Date: Wed, 13 Dec 2023 19:29:37 +0000 Subject: [PATCH] Changes to not clobber global vars --- commons/src/index.ts | 3 ++ .../megalodon/megalodon.instancesettings.ts | 5 ++++ .../websites/mastodon/mastodon.service.ts | 9 ++++-- .../websites/megalodon/megalodon.service.ts | 29 ++++++++++--------- .../websites/pixelfed/pixelfed.service.ts | 8 +++-- .../websites/pleroma/pleroma.service.ts | 8 +++-- 6 files changed, 42 insertions(+), 20 deletions(-) create mode 100644 commons/src/websites/megalodon/megalodon.instancesettings.ts diff --git a/commons/src/index.ts b/commons/src/index.ts index f977f505..adddb517 100644 --- a/commons/src/index.ts +++ b/commons/src/index.ts @@ -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'; \ No newline at end of file diff --git a/commons/src/websites/megalodon/megalodon.instancesettings.ts b/commons/src/websites/megalodon/megalodon.instancesettings.ts new file mode 100644 index 00000000..1171e3f4 --- /dev/null +++ b/commons/src/websites/megalodon/megalodon.instancesettings.ts @@ -0,0 +1,5 @@ +export class MegalodonInstanceSettings { + maxChars?: number = undefined; + maxImages?: number = undefined; +} + \ No newline at end of file diff --git a/electron-app/src/server/websites/mastodon/mastodon.service.ts b/electron-app/src/server/websites/mastodon/mastodon.service.ts index b1ac740f..8cd22f73 100644 --- a/electron-app/src/server/websites/mastodon/mastodon.service.ts +++ b/electron-app/src/server/websites/mastodon/mastodon.service.ts @@ -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'; @@ -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 { diff --git a/electron-app/src/server/websites/megalodon/megalodon.service.ts b/electron-app/src/server/websites/megalodon/megalodon.service.ts index 16c6546d..43b3b9db 100644 --- a/electron-app/src/server/websites/megalodon/megalodon.service.ts +++ b/electron-app/src/server/websites/megalodon/megalodon.service.ts @@ -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'; @@ -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; @@ -85,7 +86,7 @@ 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, @@ -93,7 +94,7 @@ export abstract class Megalodon extends Website { accountData: MegalodonAccountData, ): Promise { 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); @@ -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); @@ -199,7 +200,7 @@ export abstract class Megalodon extends Website { submissionPart: SubmissionPart, defaultPart: SubmissionPart, ): ValidationParts { - this.getInstanceSettings(submissionPart.accountId); + const instanceSettings = this.getInstanceSettings(submissionPart.accountId); const problems: string[] = []; const warnings: string[] = []; @@ -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, ); } @@ -286,7 +287,7 @@ export abstract class Megalodon extends Website { submissionPart: SubmissionPart, defaultPart: SubmissionPart, ): ValidationParts { - this.getInstanceSettings(submissionPart.accountId); + const instanceSettings = this.getInstanceSettings(submissionPart.accountId); const problems = []; const warnings = []; @@ -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, ); } diff --git a/electron-app/src/server/websites/pixelfed/pixelfed.service.ts b/electron-app/src/server/websites/pixelfed/pixelfed.service.ts index 3aee1b2c..3da49426 100644 --- a/electron-app/src/server/websites/pixelfed/pixelfed.service.ts +++ b/electron-app/src/server/websites/pixelfed/pixelfed.service.ts @@ -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'; @@ -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 { diff --git a/electron-app/src/server/websites/pleroma/pleroma.service.ts b/electron-app/src/server/websites/pleroma/pleroma.service.ts index c76b14dc..54097e4d 100644 --- a/electron-app/src/server/websites/pleroma/pleroma.service.ts +++ b/electron-app/src/server/websites/pleroma/pleroma.service.ts @@ -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'; @@ -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 {