Skip to content

Commit

Permalink
feat: support altering post using user defined metadata(dimensions/ig…
Browse files Browse the repository at this point in the history
…nore websites) (#268)
  • Loading branch information
mvdicarlo authored Mar 6, 2024
1 parent b09c6ff commit 800d192
Show file tree
Hide file tree
Showing 6 changed files with 101 additions and 34 deletions.
24 changes: 20 additions & 4 deletions apps/client-server/src/app/post/models/posting-file.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import { FileType, IFileBuffer } from '@postybirb/types';
import {
FileMetadataFields,
FileType,
IFileBuffer,
SubmissionFileId,
} from '@postybirb/types';
import { getFileType } from '@postybirb/utils/file-type';
import { parse } from 'path';

Expand All @@ -8,6 +13,8 @@ export type ThumbnailOptions = {
};

export class PostingFile {
public readonly id: SubmissionFileId;

public readonly buffer: Buffer;

public readonly mimeType: string;
Expand All @@ -16,21 +23,30 @@ export class PostingFile {

public readonly fileName: string;

public readonly id: string;
public metadata: FileMetadataFields;

public readonly thumbnail?: ThumbnailOptions;

public constructor(file: IFileBuffer, thumbnail?: ThumbnailOptions) {
public constructor(
id: SubmissionFileId,
file: IFileBuffer,
thumbnail?: ThumbnailOptions
) {
this.id = id;
this.buffer = file.buffer;
this.mimeType = file.mimeType;
this.fileType = getFileType(file.fileName);
this.fileName = this.normalizeFileName(file);
this.id = file.id;
this.thumbnail = thumbnail;
}

private normalizeFileName(file: IFileBuffer): string {
const { ext } = parse(file.fileName);
return `${file.id}${ext}`;
}

public withMetadata(metadata: FileMetadataFields): PostingFile {
this.metadata = metadata;
return this;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ export class PostFileResizerService {
}

return new PostingFile(
file.id,
await this.processPrimaryFile(file, resize),
await this.processThumbnailFile(file)
);
Expand Down
84 changes: 63 additions & 21 deletions apps/client-server/src/app/post/post-manager.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { BadRequestException, Injectable } from '@nestjs/common';
import { Logger } from '@postybirb/logger';
import {
EntityId,
FileMetadataFields,
FileSubmission,
FileType,
ISubmissionFile,
Expand Down Expand Up @@ -337,11 +338,18 @@ export class PostManagerService {
// Order files based on submission order
const fileBatchSize = Math.max(instance.metadata.fileBatchSize ?? 1, 1);
const orderedFiles: Loaded<ISubmissionFile[]> = [];
const files = submission.files.getItems().filter(
// Only post files that haven't been posted
// Ensures CONTINUED posts don't post files that have already been posted.
(f) => websitePostRecord.metadata.postedFiles.indexOf(f.id) === -1
);
const metadata = submission.metadata.fileMetadata;
const files = submission.files
.getItems()
.filter(
// Filter out files that have been marked by the user as ignored for this website.
(f) => !metadata[f.id]?.ignoredWebsites?.includes(instance.accountId)
)
.filter(
// Only post files that haven't been posted
// Ensures CONTINUED posts don't post files that have already been posted.
(f) => websitePostRecord.metadata.postedFiles.indexOf(f.id) === -1
);
submission.metadata.order.forEach((fileId) => {
const file = files.find((f) => f.id === fileId);
if (file) {
Expand All @@ -357,23 +365,57 @@ export class PostManagerService {
this.cancelToken.throwIfCancelled();

// Resize files if necessary
const processedFiles: PostingFile[] = await Promise.all(
batch.map((f) => {
let resizeParams: ImageResizeProps | undefined;
const fileType = getFileType(f.mimeType);
if (fileType === FileType.IMAGE) {
resizeParams = this.getResizeParameters(
submission,
filePostableInstance,
f
);
const processedFiles: PostingFile[] = (
await Promise.all(
batch.map((f) => {
const fileMetadata: FileMetadataFields = submission.metadata[f.id];
let resizeParams: ImageResizeProps | undefined;
const fileType = getFileType(f.mimeType);
if (fileType === FileType.IMAGE) {
resizeParams = this.getResizeParameters(
submission,
filePostableInstance,
f
);

// User defined dimensions
const userDefinedDimensions =
// eslint-disable-next-line @typescript-eslint/dot-notation
fileMetadata?.dimensions['default'] ??
fileMetadata?.dimensions[instance.accountId];
if (userDefinedDimensions) {
if (
userDefinedDimensions.width &&
userDefinedDimensions.height
) {
resizeParams = resizeParams ?? {};
if (
userDefinedDimensions.width > resizeParams.width &&
userDefinedDimensions.height > resizeParams.height
) {
resizeParams = {
...resizeParams,
width: userDefinedDimensions.width,
height: userDefinedDimensions.height,
};
}
}
}
}

return this.resizerService.resize({
file: f,
resize: resizeParams,
});
})
)
).map((f) =>
f.withMetadata(
metadata[f.id] ?? {
ignoredWebsites: [],
dimensions: null,
}

return this.resizerService.resize({
file: f,
resize: resizeParams,
});
})
)
);

// Post
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,11 @@ import {
} from '@elastic/eui';
import { Trans, msg } from '@lingui/macro';
import { useLingui } from '@lingui/react';
import { FileSubmissionMetadata, ISubmissionFileDto } from '@postybirb/types';
import {
AccountId,
FileSubmissionMetadata,
ISubmissionFileDto,
} from '@postybirb/types';
import { isImage } from '@postybirb/utils/file-type';
import { filesize } from 'filesize';
import { ReactNode, useCallback, useMemo, useState } from 'react';
Expand Down Expand Up @@ -69,7 +73,7 @@ function SharedDetails(props: FileDetailsProps) {
const providedIgnoredWebsites = fileMetadata.ignoredWebsites ?? [];

const [altText, setAltText] = useState<string>(providedAltText);
const [ignoredWebsites, setIgnoredWebsites] = useState<string[]>(
const [ignoredWebsites, setIgnoredWebsites] = useState<AccountId[]>(
providedIgnoredWebsites
);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { EuiComboBox, EuiComboBoxOptionOption } from '@elastic/eui';
import { msg } from '@lingui/macro';
import { useLingui } from '@lingui/react';
import { AccountId } from '@postybirb/types';
import { useEffect, useState } from 'react';
import { SubmissionDto } from '../../../../../models/dtos/submission.dto';
import { AccountStore } from '../../../../../stores/account.store';
Expand All @@ -9,7 +10,7 @@ import { useStore } from '../../../../../stores/use-store';
type SimpleWebsiteSelectProps = {
onChange: (websitesIds: string[]) => void;
submission: SubmissionDto;
selected: string[]; // AccountIds
selected: AccountId[];
};

export function SimpleWebsiteSelect(props: SimpleWebsiteSelectProps) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { WebsiteId } from '../../website/website.type';
import { AccountId } from '../../account/account.interface';
import { ModifiedFileDimension } from './modified-file-dimension.type';

/**
Expand All @@ -13,15 +13,18 @@ export type FileMetadataFields = {
* The alternative text for the file.
* @type {string}
*/
altText: string;
altText?: string;

/**
* The dimensions of the file for different websites.
* @type {Record<WebsiteId, ModifiedFileDimension>}
* Currently only supports 'default'
* @type {Record<AccountId, ModifiedFileDimension>}
*/
dimensions: Record<WebsiteId, ModifiedFileDimension>;
dimensions: Record<AccountId, ModifiedFileDimension>;

/**
* The list of websites where the file is ignored.
* @type {WebsiteId[]}
* @type {AccountId[]}
*/
ignoredWebsites: WebsiteId[];
ignoredWebsites: AccountId[];
};

0 comments on commit 800d192

Please sign in to comment.