Skip to content

Commit

Permalink
trying to fix swagger
Browse files Browse the repository at this point in the history
  • Loading branch information
OliverHappe committed Sep 29, 2023
1 parent 06d5852 commit bff9a03
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 23 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { ApiProperty, ApiPropertyOptional } from '@nestjs/swagger';
import { ApiExtraModels, ApiProperty, ApiPropertyOptional } from '@nestjs/swagger';
import { ContentElementType } from '@shared/domain';
import { OpenGraphData } from '@src/modules/board/service';
import { TimestampsResponse } from '../timestamps.response';

@ApiExtraModels(OpenGraphData)
export class LinkElementContent {
constructor({ url, openGraphData }: LinkElementContent) {
this.url = url;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export class LinkElementContentBody extends ElementContentBody {
type!: ContentElementType.LINK;

@ValidateNested()
@ApiProperty()
@ApiProperty({})
content!: LinkContentBody;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import {
CreateSubmissionItemBodyParams,
ExternalToolElementContentBody,
FileElementContentBody,
LinkElementContentBody,
MoveContentElementBody,
RichTextElementContentBody,
SubmissionContainerElementContentBody,
Expand Down Expand Up @@ -60,7 +61,8 @@ export class ElementController {
FileElementContentBody,
RichTextElementContentBody,
SubmissionContainerElementContentBody,
ExternalToolElementContentBody
ExternalToolElementContentBody,
LinkElementContentBody
)
@ApiResponse({ status: 204 })
@ApiResponse({ status: 400, type: ApiValidationError })
Expand Down
62 changes: 42 additions & 20 deletions apps/server/src/modules/board/service/open-graph-proxy.service.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,50 @@
import { Injectable } from '@nestjs/common';
import { ApiProperty, ApiPropertyOptional } from '@nestjs/swagger';
import { sortBy } from 'lodash';
import ogs from 'open-graph-scraper';
import { ImageObject } from 'open-graph-scraper/dist/lib/types';

export type OpenGraphData = {
title: string;
description: string;
image?: OpenGraphImageData;
url: string;
};
export class OpenGraphImageData {
constructor({ url, type, width, height }: OpenGraphImageData) {
this.url = url;
this.type = type;
this.width = width ? +width : undefined;
this.height = height ? +height : undefined;
}

export type OpenGraphImageData = {
@ApiProperty()
url: string;

@ApiPropertyOptional()
type?: string;

@ApiPropertyOptional()
width?: number;

@ApiPropertyOptional()
height?: number;
};
}

export class OpenGraphData {
constructor({ title, description, image, url }: OpenGraphData) {
this.title = title;
this.description = description;
this.image = image;
this.url = url;
}

@ApiProperty()
title: string;

@ApiProperty()
description: string;

@ApiPropertyOptional()
image?: OpenGraphImageData;

@ApiProperty()
url: string;
}

@Injectable()
export class OpenGraphProxyService {
Expand All @@ -26,29 +55,22 @@ export class OpenGraphProxyService {
const description = data.result.ogDescription ?? '';
const image = data.result.ogImage ? this.pickImage(data.result.ogImage) : undefined;

const result = {
const result = new OpenGraphData({
title,
description,
image,
url,
};
});

return result;
}

private pickImage(images: ImageObject[], minWidth = 400, maxWidth = 800) {
const imagesWithCorrectDimensions = images.map((i) => this.fixDimensionTypes(i));
private pickImage(images: ImageObject[], minWidth = 400, maxWidth = 800): OpenGraphImageData {
const imagesWithCorrectDimensions = images.map((i) => new OpenGraphImageData(i));
const sortedImages = sortBy(imagesWithCorrectDimensions, ['width', 'height']);
const biggestSmallEnoughImage = [...sortedImages].reverse().find((i) => i.width && i.width <= maxWidth);
const smallestBigEnoughImage = sortedImages.find((i) => i.width && i.width >= minWidth);
// return imagesWithCorrectDimensions[0];
return biggestSmallEnoughImage ?? smallestBigEnoughImage ?? sortedImages[0];
}

private fixDimensionTypes(image: { width?: number | string; height?: number | string; url: string; type?: string }) {
return {
...image,
width: image.width ? +image.width : undefined,
height: image.height ? +image.height : undefined,
};
}
}

0 comments on commit bff9a03

Please sign in to comment.