Skip to content

Commit

Permalink
Move tag handling to base website, update parser
Browse files Browse the repository at this point in the history
  • Loading branch information
aneillans committed Dec 13, 2023
1 parent 58ee4d6 commit d53c745
Show file tree
Hide file tree
Showing 37 changed files with 99 additions and 87 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export class ParserService {
private readonly fileManipulator: FileManipulationService,
websitesService: WebsitesService,
) {
this.descriptionParser = new DescriptionParser(customShortcuts, websitesService, settings);
this.descriptionParser = new DescriptionParser(customShortcuts, websitesService, settings, this);
}

public async parse(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import FormContent from 'src/server/utils/form-content.util';
import { Website } from 'src/server/websites/website.base';
import { WebsitesService } from 'src/server/websites/websites.service';
import SubmissionPartEntity from '../../submission-part/models/submission-part.entity';
import { ParserService } from '../parser.service';

interface ShortcutData {
originalText: string;
Expand All @@ -26,6 +27,7 @@ export class DescriptionParser {
private customShortcuts: CustomShortcutService,
private websitesService: WebsitesService,
private settings: SettingsService,
private readonly parserService: ParserService,
) {
this.websiteDescriptionShortcuts = websitesService.getUsernameShortcuts();
this.websiteDescriptionShortcuts = {
Expand All @@ -52,6 +54,7 @@ export class DescriptionParser {

if (description.length) {
// Insert {default}, {title}, {tags} shortcuts
let tags = await this.parserService.parseTags(website, defaultPart, websitePart);
description = this.insertDefaultShortcuts(description, [
{
name: 'default',
Expand All @@ -63,10 +66,7 @@ export class DescriptionParser {
},
{
name: 'tags',
content:
defaultPart.data.tags.value.map(t => '#' + t).join(' ') ??
websitePart.data.tags.value.map(t => '#' + t).join(' ') ??
'',
content: website.generateTagsString(tags, description),
},
]);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import FormContent from "../../utils/form-content.util";
@Injectable()
export class Artconomy extends Website {
readonly BASE_URL: string = 'https://artconomy.com';
readonly MAX_CHARS: number = 2000;
readonly acceptsFiles: string[] = [
'png',
'jpeg',
Expand Down Expand Up @@ -255,7 +256,7 @@ export class Artconomy extends Website {
FormContent.getDescription(defaultPart.data.description, submissionPart.data.description),
)

if (description.length > 2000) {
if (description.length > this.MAX_CHARS) {
problems.push('Description must be 2000 characters or fewer.')
}

Expand Down
1 change: 1 addition & 0 deletions electron-app/src/server/websites/aryion/aryion.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import { Website } from '../website.base';
@Injectable()
export class Aryion extends Website {
readonly BASE_URL = 'https://aryion.com';
readonly MAX_CHARS: number = -1; // No limit
readonly defaultDescriptionParser = BBCodeParser.parse;
readonly usernameShortcuts = [
{
Expand Down
27 changes: 11 additions & 16 deletions electron-app/src/server/websites/bluesky/bluesky.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -171,10 +171,6 @@ export class Bluesky extends Website {
).map(tag => `#${tag}`);
}

private appendRichTextTags(tags: string[], description: string): string {
return this.appendTags(this.formatTags(tags), description, this.MAX_CHARS, getRichTextLength);
}

private async uploadMedia(
agent: BskyAgent,
data: BlueskyAccountData,
Expand Down Expand Up @@ -229,8 +225,6 @@ export class Bluesky extends Website {
$type: 'app.bsky.embed.images',
};

const status = this.appendRichTextTags(data.tags, data.description);

let labelsRecord: ComAtprotoLabelDefs.SelfLabels | undefined;
if (data.options.label_rating) {
labelsRecord = {
Expand All @@ -239,7 +233,7 @@ export class Bluesky extends Website {
};
}

const rt = new RichText({ text: status });
const rt = new RichText({ text: data.description });
await rt.detectFacets(agent);

let postResult = await agent
Expand Down Expand Up @@ -286,7 +280,6 @@ export class Bluesky extends Website {
let profile = await agent.getProfile({actor: agent.session.did });

const reply = await this.getReplyRef(agent, data.options.replyToUrl);
const status = this.appendRichTextTags(data.tags, data.description);

let labelsRecord: ComAtprotoLabelDefs.SelfLabels | undefined;
if (data.options.label_rating) {
Expand All @@ -296,7 +289,7 @@ export class Bluesky extends Website {
};
}

const rt = new RichText({ text: status });
const rt = new RichText({ text: data.description });
await rt.detectFacets(agent);

let postResult = await agent.post({
Expand Down Expand Up @@ -423,13 +416,15 @@ export class Bluesky extends Website {
`Max description length allowed is ${this.MAX_CHARS} characters.`,
);
} else {
this.validateAppendTags(
warnings,
this.formatTags(FormContent.getTags(defaultPart.data.tags, submissionPart.data.tags)),
description,
this.MAX_CHARS,
getRichTextLength,
);
if (description.toLowerCase().indexOf('{tags}') > -1) {
this.validateInsertTags(
warnings,
this.formatTags(FormContent.getTags(defaultPart.data.tags, submissionPart.data.tags)),
description,
this.MAX_CHARS,
getRichTextLength,
);
}
}
}

Expand Down
1 change: 1 addition & 0 deletions electron-app/src/server/websites/custom/custom.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import { Website } from '../website.base';
@Injectable()
export class Custom extends Website {
readonly BASE_URL: string = '';
readonly MAX_CHARS: number = -1; // No limit
readonly acceptsFiles: string[] = []; // accepts all
readonly acceptsAdditionalFiles: boolean = true;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import { Website } from '../website.base';
@Injectable()
export class Derpibooru extends Website {
BASE_URL: string = 'https://derpibooru.org';
MAX_CHARS: number = -1; // No limit
acceptsFiles: string[] = ['jpeg', 'jpg', 'png', 'svg', 'gif', 'webm'];
acceptsSourceUrls: boolean = true;
enableAdvertisement: boolean = false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ interface DeviantArtFolder {
@Injectable()
export class DeviantArt extends Website {
readonly BASE_URL = 'https://www.deviantart.com';
readonly MAX_CHARS: number = -1; // No Limit
readonly acceptsFiles = [
'jpeg',
'jpg',
Expand Down
5 changes: 3 additions & 2 deletions electron-app/src/server/websites/discord/discord.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import { DiscordAccountData } from './discord.account.interface';
@Injectable()
export class Discord extends Website {
readonly BASE_URL: string = '';
readonly MAX_CHARS: number = 2000;
readonly acceptsFiles: string[] = []; // accepts all
readonly acceptsAdditionalFiles: boolean = true;
readonly enableAdvertisement: boolean = false;
Expand Down Expand Up @@ -163,7 +164,7 @@ export class Discord extends Website {
FormContent.getDescription(defaultPart.data.description, submissionPart.data.description),
);

if (description.length > 2000) {
if (description.length > this.MAX_CHARS) {
warnings.push('Max description length allowed is 2,000 characters.');
}

Expand All @@ -182,7 +183,7 @@ export class Discord extends Website {
FormContent.getDescription(defaultPart.data.description, submissionPart.data.description),
);

if (description.length > 2000) {
if (description.length > this.MAX_CHARS) {
warnings.push('Max description length allowed is 2,000 characters.');
}

Expand Down
1 change: 1 addition & 0 deletions electron-app/src/server/websites/e621/e621.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import { Website } from '../website.base';
@Injectable()
export class e621 extends Website {
readonly BASE_URL: string = 'https://e621.net';
readonly MAX_CHARS: number = -1; // No limit
readonly acceptsFiles: string[] = ['jpeg', 'jpg', 'png', 'gif', 'webm'];
readonly acceptsSourceUrls: boolean = true;
readonly defaultDescriptionParser = PlaintextParser.parse;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import _ from 'lodash';
export class FurAffinity extends Website {
readonly BASE_URL = 'https://www.furaffinity.net';
readonly waitBetweenPostsInterval = 70000;
readonly MAX_CHARS: number = -1; // No limit
readonly defaultDescriptionParser = BBCodeParser.parse;
readonly usernameShortcuts = [
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import { Website } from '../website.base';
@Injectable()
export class Furbooru extends Website {
BASE_URL: string = 'https://furbooru.org';
readonly MAX_CHARS: number = -1; // No Limit
acceptsFiles: string[] = ['jpeg', 'jpg', 'png', 'svg', 'gif', 'webm'];
acceptsSourceUrls: boolean = true;
enableAdvertisement: boolean = false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import _ from 'lodash';
@Injectable()
export class FurryNetwork extends Website {
readonly BASE_URL: string = 'https://furrynetwork.com';
readonly MAX_CHARS: number = -1; // No Limit
readonly refreshBeforePost: boolean = true;
readonly acceptsFiles: string[] = [
'png',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import { MarkdownParser } from 'src/server/description-parsing/markdown/markdown
@Injectable()
export class Furtastic extends Website {
readonly BASE_URL: string = 'https://api.furtastic.art';
readonly MAX_CHARS: number = -1; // No Limit
readonly acceptsFiles: string[] = ['jpeg', 'jpg', 'png', 'gif', 'webm', 'pdf'];
readonly defaultDescriptionParser = MarkdownParser.parse;
readonly enableAdvertisement: boolean = true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import _ from 'lodash';
@Injectable()
export class HentaiFoundry extends Website {
readonly BASE_URL = 'https://www.hentai-foundry.com';
readonly MAX_CHARS: number = -1; // No Limit
readonly acceptsFiles = ['jpeg', 'jpg', 'png', 'svg', 'gif'];
readonly defaultDescriptionParser = BBCodeParser.parse;
readonly usernameShortcuts = [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import { InkbunnyAccountData } from './inkbunny-account.interface';
@Injectable()
export class Inkbunny extends Website {
readonly BASE_URL: string = 'https://inkbunny.net';
readonly MAX_CHARS: number = -1; // No Limit
readonly acceptsFiles: string[] = [
'png',
'jpeg',
Expand Down
5 changes: 3 additions & 2 deletions electron-app/src/server/websites/itaku/itaku.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import _ from 'lodash';
@Injectable()
export class Itaku extends Website {
BASE_URL: string = 'https://itaku.ee';
readonly MAX_CHARS: number = 5000;
acceptsFiles: string[] = ['jpeg', 'jpg', 'png', 'gif', 'webp', 'mp4', 'mov', 'webm'];
acceptsAdditionalFiles: boolean = false;
readonly defaultDescriptionParser = PlaintextParser.parse;
Expand Down Expand Up @@ -236,7 +237,7 @@ export class Itaku extends Website {
FormContent.getDescription(defaultPart.data.description, submissionPart.data.description),
);

if (description.length > 5000) {
if (description.length > this.MAX_CHARS) {
problems.push(
`Max description length allowed is 5000 characters.`,
);
Expand Down Expand Up @@ -278,7 +279,7 @@ export class Itaku extends Website {
problems.push('Description required');
}

if (description.length > 5000) {
if (description.length > this.MAX_CHARS) {
problems.push(
`Max description length allowed is 5000 characters.`,
);
Expand Down
1 change: 1 addition & 0 deletions electron-app/src/server/websites/ko-fi/ko-fi.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import _ from 'lodash';
@Injectable()
export class KoFi extends Website {
readonly BASE_URL: string = 'https://ko-fi.com';
readonly MAX_CHARS: number = -1; // No Limit
readonly acceptsFiles: string[] = ['jpeg', 'jpg', 'png', 'gif'];
readonly acceptsAdditionalFiles: boolean = true;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import { Website } from '../website.base';
@Injectable()
export class Manebooru extends Website {
BASE_URL: string = 'https://manebooru.art';
readonly MAX_CHARS: number = -1; // No Limit
acceptsFiles: string[] = ['jpeg', 'jpg', 'png', 'svg', 'gif', 'webm'];
acceptsSourceUrls: boolean = true;
enableAdvertisement: boolean = false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ export class Mastodon extends Megalodon {
getInstanceSettings(accountId: string) {
const instanceInfo: MastodonInstanceInfo = this.getAccountInfo(accountId, INFO_KEY);

this.maxCharLength = instanceInfo?.configuration?.statuses?.max_characters ?? 500;
this.MAX_CHARS = instanceInfo?.configuration?.statuses?.max_characters ?? 500;
this.maxMediaCount = instanceInfo ? instanceInfo?.configuration?.statuses?.max_media_attachments : 4;
}

Expand Down
25 changes: 10 additions & 15 deletions electron-app/src/server/websites/megalodon/megalodon.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,10 @@ export abstract class Megalodon extends Website {
}

megalodonService: 'mastodon' | 'pleroma' | 'misskey' | 'friendica' = 'mastodon'; // Set this as appropriate in your constructor
maxCharLength = 500; // Set this off the instance information!
maxMediaCount = 4;

readonly BASE_URL: string;
MAX_CHARS: number = 500;
readonly enableAdvertisement = false;
readonly acceptsAdditionalFiles = true;
readonly defaultDescriptionParser = PlaintextParser.parse;
Expand Down Expand Up @@ -108,7 +108,7 @@ export abstract class Megalodon extends Website {
const chunks = _.chunk(uploadedMedias, this.maxMediaCount);
let status = `${data.options.useTitle && data.title ? `${data.title}\n` : ''}${
data.description
}`.substring(0, this.maxCharLength);
}`.substring(0, this.MAX_CHARS);
let lastId = '';
let source = '';
const replyToId = this.getPostIdFromUrl(data.options.replyToUrl);
Expand All @@ -131,8 +131,6 @@ export abstract class Megalodon extends Website {
statusOptions.spoiler_text = data.options.spoilerText;
}

status = this.appendTags(this.formatTags(data.tags), status, this.maxCharLength);

try {
const result = (await M.postStatus(status, statusOptions)).data as Entity.Status;
if (!source) source = result.url;
Expand Down Expand Up @@ -174,7 +172,6 @@ export abstract class Megalodon extends Website {
if (data.options.spoilerText) {
statusOptions.spoiler_text = data.options.spoilerText;
}
status = this.appendTags(this.formatTags(data.tags), status, this.maxCharLength);

const replyToId = this.getPostIdFromUrl(data.options.replyToUrl);
if (replyToId) {
Expand Down Expand Up @@ -212,16 +209,16 @@ export abstract class Megalodon extends Website {
FormContent.getDescription(defaultPart.data.description, submissionPart.data.description),
);

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

Expand Down Expand Up @@ -298,16 +295,16 @@ export abstract class Megalodon extends Website {
FormContent.getDescription(defaultPart.data.description, submissionPart.data.description),
);

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

Expand All @@ -329,7 +326,6 @@ export abstract class Megalodon extends Website {
file: PostFile,
altText: string,
): Promise<string> {
//): Promise<{ id: string }> {\
this.logger.log("Uploading media")
const M = generator(this.megalodonService, data.website, data.token);

Expand Down Expand Up @@ -359,7 +355,6 @@ export abstract class Megalodon extends Website {

this.logger.log("Image uploaded");

// return { id: upload.data.id };
return upload.data.id;
}

Expand Down
Loading

0 comments on commit d53c745

Please sign in to comment.