diff --git a/src/dal/entity/file.entity.ts b/src/dal/entity/file.entity.ts index 2d14756..6c89fa9 100644 --- a/src/dal/entity/file.entity.ts +++ b/src/dal/entity/file.entity.ts @@ -6,8 +6,9 @@ import { type Ref, Unique, } from '@mikro-orm/core'; -import { ShareableId } from './shareableId.entity'; +import { OpenGraphTagValues, ShareableId } from './shareableId.entity'; import { User } from './user.entity'; +import { FastifyRequest } from 'fastify'; @Entity() export class File extends ShareableId { @@ -31,4 +32,18 @@ export class File extends ShareableId { nullable: true, }) public createdBy?: Ref; + + public async getOpenGraphTagValues( + req: FastifyRequest, + ): Promise { + const createdBy = await this?.createdBy?.load(); + return { + ogUrl: `${req.protocol}://${req.host}/file/${this.shareableId}`, + ogTitle: this?.fileName, + ogDescription: `From ${createdBy?.email}`, + ogImage: this.fileUrlService.getWatermarkedFileUrl(this.shareableId, req), + file: this, + createdBy, + } as OpenGraphTagValues; + } } diff --git a/src/dal/entity/shareableId.entity.ts b/src/dal/entity/shareableId.entity.ts index 5ddc568..ccae22d 100644 --- a/src/dal/entity/shareableId.entity.ts +++ b/src/dal/entity/shareableId.entity.ts @@ -1,5 +1,14 @@ import { BeforeCreate, Property } from '@mikro-orm/core'; import { randomUUID } from 'crypto'; +import type { FastifyRequest } from 'fastify'; + +export interface OpenGraphTagValues { + ogUrl: string; + ogTitle: string; + ogDescription: string; + ogImage: string; + file?: any; +} export abstract class ShareableId { @Property() @@ -17,4 +26,8 @@ export abstract class ShareableId { @Property({ nullable: true }) public banned?: boolean; + + public abstract getOpenGraphTagValues( + req?: FastifyRequest, + ): Promise; } diff --git a/src/dal/entity/user.entity.ts b/src/dal/entity/user.entity.ts index 5b4beec..6b7da72 100644 --- a/src/dal/entity/user.entity.ts +++ b/src/dal/entity/user.entity.ts @@ -11,8 +11,9 @@ import { } from '@mikro-orm/core'; import { File } from './file.entity'; import { PasswordReset } from './passwordReset.entity'; -import { ShareableId } from './shareableId.entity'; +import { OpenGraphTagValues, ShareableId } from './shareableId.entity'; import { UserDevice } from './userDevice.entity'; +import { FastifyRequest } from 'fastify'; @Entity() export class User extends ShareableId { @@ -46,4 +47,10 @@ export class User extends ShareableId { @OneToMany(() => File, (file) => file.createdBy) public fileUploads = new Collection(this); + + public getOpenGraphTagValues( + req?: FastifyRequest, + ): Promise { + throw Error(`Not implemented ${JSON.stringify(req)}`); + } } diff --git a/src/open-graph/open-graph.service.ts b/src/open-graph/open-graph.service.ts index f1359bf..0352b58 100644 --- a/src/open-graph/open-graph.service.ts +++ b/src/open-graph/open-graph.service.ts @@ -4,13 +4,7 @@ import { FileUrlService } from '../file/file-url/file-url.service'; import { InjectRepository } from '@mikro-orm/nestjs'; import { File } from '../dal/entity/file.entity'; import { EntityManager, EntityRepository } from '@mikro-orm/core'; - -export interface OpenGraphTagValues { - ogUrl: string; - ogTitle: string; - ogDescription: string; - ogImage: string; -} +import { OpenGraphTagValues } from '../dal/entity/shareableId.entity'; @Injectable() export class OpenGraphService { @@ -25,7 +19,7 @@ export class OpenGraphService { shareableId: string, type: string, req: FastifyRequest, - ) { + ): Promise { if (type == 'file') { const file = await this.fileRepository.findOne( { shareableId }, @@ -33,15 +27,7 @@ export class OpenGraphService { populate: ['createdBy'], }, ); - const createdBy = await file?.createdBy?.load(); - return { - ogUrl: `${req.protocol}://${req.host}/file/${shareableId}`, - ogTitle: file?.fileName, - ogDescription: `From ${createdBy?.email}`, - ogImage: this.fileUrlService.getWatermarkedFileUrl(shareableId, req), - file, - createdBy, - }; + return file.getOpenGraphTagValues(req); } } }