Skip to content

Commit

Permalink
feat: removes queue logic from post record and splits it out into pos…
Browse files Browse the repository at this point in the history
…t-queue (#322)

* feat: post queue separated from post record ledger

* feat: place most post record logic into post queue

* test: adds tests for post queue

* test: some fixes, improvements, and tests for post queue
  • Loading branch information
mvdicarlo authored Dec 13, 2024
1 parent ecf54f9 commit 9ff7d80
Show file tree
Hide file tree
Showing 39 changed files with 781 additions and 306 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
DirectoryWatcherDto,
DirectoryWatcherImportAction,
IDirectoryWatcher,
ISubmission,
} from '@postybirb/types';
import { PostyBirbRepository } from '../repositories/postybirb-repository';
import { PostyBirbEntity } from './postybirb-entity';
Expand All @@ -36,13 +37,13 @@ export class DirectoryWatcher
serializer: (s) => s?.id,
cascade: [],
})
template?: Rel<Submission>;
template?: Rel<ISubmission>;

constructor(directoryWatcher: IDirectoryWatcher) {
super();
this.path = directoryWatcher.path;
this.importAction = directoryWatcher.importAction;
this.template = directoryWatcher.template as Submission;
this.template = directoryWatcher.template;
}

toJSON(): DirectoryWatcherDto {
Expand Down
2 changes: 2 additions & 0 deletions apps/client-server/src/app/database/entities/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export * from './account.entity';
export * from './directory-watcher.entity';
export * from './post-queue-record.entity';
export * from './post-record.entity';
export * from './settings.entity';
export * from './submission-file.entity';
Expand All @@ -10,3 +11,4 @@ export * from './user-specified-website-options.entity';
export * from './website-data.entity';
export * from './website-options.entity';
export * from './website-post-record.entity';

Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { Entity, OneToOne, Rel, serialize } from '@mikro-orm/core';
import {
IPostQueueRecord,
IPostRecord,
ISubmission,
PostQueueRecordDto,
} from '@postybirb/types';
import { PostRecord } from './post-record.entity';
import { PostyBirbEntity } from './postybirb-entity';
import { Submission } from './submission.entity';

@Entity()
export class PostQueueRecord
extends PostyBirbEntity
implements IPostQueueRecord
{
@OneToOne(() => PostRecord, {
nullable: true,
orphanRemoval: false,
serializer: (pr) => pr.id,
eager: true,
inversedBy: 'postQueueRecord',
})
postRecord?: Rel<IPostRecord>;

@OneToOne({
entity: () => Submission,
nullable: false,
serializer: (s) => s.id,
mappedBy: 'postQueueRecord',
orphanRemoval: false,
eager: true,
})
submission: Rel<ISubmission>;

toJSON(): PostQueueRecordDto {
return serialize(this) as PostQueueRecordDto;
}
}
17 changes: 15 additions & 2 deletions apps/client-server/src/app/database/entities/post-record.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@ import {
EntityRepositoryType,
ManyToOne,
OneToMany,
OneToOne,
Property,
Rel,
serialize,
} from '@mikro-orm/core';
import {
IPostQueueRecord,
IPostRecord,
ISubmission,
IWebsitePostRecord,
Expand All @@ -17,6 +19,7 @@ import {
PostRecordState,
} from '@postybirb/types';
import { PostyBirbRepository } from '../repositories/postybirb-repository';
import { PostQueueRecord } from './post-queue-record.entity';
import { PostyBirbEntity } from './postybirb-entity';
import { Submission } from './submission.entity';
import { WebsitePostRecord } from './website-post-record.entity';
Expand Down Expand Up @@ -52,6 +55,14 @@ export class PostRecord extends PostyBirbEntity implements IPostRecord {
})
children: Collection<IWebsitePostRecord>;

@OneToOne(() => PostQueueRecord, {
orphanRemoval: true,
eager: true,
nullable: true,
mappedBy: 'postRecord',
})
postQueueRecord?: Rel<IPostQueueRecord>;

@Property({ type: 'string', nullable: false })
state: PostRecordState = PostRecordState.PENDING;

Expand All @@ -61,7 +72,7 @@ export class PostRecord extends PostyBirbEntity implements IPostRecord {
constructor(
postRecord: Pick<
IPostRecord,
'parent' | 'completedAt' | 'state' | 'resumeMode'
'parent' | 'completedAt' | 'state' | 'resumeMode' | 'postQueueRecord'
>,
) {
super();
Expand All @@ -73,6 +84,8 @@ export class PostRecord extends PostyBirbEntity implements IPostRecord {

toJSON(): PostRecordDto {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
return serialize(this as any, { populate: ['children'] }) as PostRecordDto;
return serialize(this as any, {
populate: ['children', 'postQueueRecord'],
}) as PostRecordDto;
}
}
21 changes: 18 additions & 3 deletions apps/client-server/src/app/database/entities/submission.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ import {
Entity,
EntityRepositoryType,
OneToMany,
OneToOne,
Property,
serialize,
serialize
} from '@mikro-orm/core';
import {
IPostRecord,
Expand All @@ -14,11 +15,12 @@ import {
ISubmissionMetadata,
ISubmissionScheduleInfo,
IWebsiteFormFields,
SubmissionType,
SubmissionType
} from '@postybirb/types';

import { PostyBirbRepository } from '../repositories/postybirb-repository';
import { DirectoryWatcher } from './directory-watcher.entity';
import { PostQueueRecord } from './post-queue-record.entity';
import { PostRecord } from './post-record.entity';
import { PostyBirbEntity } from './postybirb-entity';
import { SubmissionFile } from './submission-file.entity';
Expand Down Expand Up @@ -73,6 +75,13 @@ export class Submission<T extends ISubmissionMetadata = ISubmissionMetadata>
})
posts: Collection<IPostRecord>;

@OneToOne(() => PostQueueRecord, {
orphanRemoval: true,
eager: true,
nullable: true,
})
postQueueRecord?: PostQueueRecord;

@Property({ type: 'number', nullable: true })
order: number;

Expand All @@ -87,7 +96,13 @@ export class Submission<T extends ISubmissionMetadata = ISubmissionMetadata>
toJSON(): ISubmissionDto<T> {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
return serialize(this as any, {
populate: ['files', 'options', 'options.account', 'posts'],
populate: [
'files',
'options',
'options.account',
'posts',
'postQueueRecord',
],
}) as ISubmissionDto<T>;
}
}
2 changes: 2 additions & 0 deletions apps/client-server/src/app/database/mikro-orm.providers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
Account,
AltFile,
DirectoryWatcher,
PostQueueRecord,
PostRecord,
PrimaryFile,
Settings,
Expand Down Expand Up @@ -36,6 +37,7 @@ const entities = [
WebsiteData,
PostRecord,
WebsitePostRecord,
PostQueueRecord,
];

const mikroOrmOptions: MikroOrmModuleSyncOptions = {
Expand Down
10 changes: 10 additions & 0 deletions apps/client-server/src/app/post/dtos/post-queue-action.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { ApiProperty } from '@nestjs/swagger';
import { IPostQueueActionDto } from '@postybirb/types';
import { ArrayNotEmpty, IsArray } from 'class-validator';

export class PostQueueActionDto implements IPostQueueActionDto {
@ApiProperty()
@IsArray()
@ArrayNotEmpty()
submissionIds: string[];
}
17 changes: 2 additions & 15 deletions apps/client-server/src/app/post/post.controller.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { Body, Controller, Post } from '@nestjs/common';
import { ApiOkResponse, ApiTags } from '@nestjs/swagger';
import { Controller } from '@nestjs/common';
import { ApiTags } from '@nestjs/swagger';
import { PostyBirbController } from '../common/controller/postybirb-controller';
import { PostRecord } from '../database/entities';
import { QueuePostRecordRequestDto } from './dtos/queue-post-record.dto';
import { PostService } from './post.service';

/**
Expand All @@ -15,16 +14,4 @@ export class PostController extends PostyBirbController<PostRecord> {
constructor(readonly service: PostService) {
super(service);
}

@Post('enqueue')
@ApiOkResponse({ description: 'Post(s) queued.' })
async enqueue(@Body() request: QueuePostRecordRequestDto) {
return this.service.enqueue(request);
}

@Post('dequeue')
@ApiOkResponse({ description: 'Post(s) dequeued.' })
async dequeue(@Body() request: QueuePostRecordRequestDto) {
this.service.dequeue(request);
}
}
11 changes: 8 additions & 3 deletions apps/client-server/src/app/post/post.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,17 @@ import { Module } from '@nestjs/common';
import { DatabaseModule } from '../database/database.module';
import { FileConverterModule } from '../file-converter/file-converter.module';
import { PostParsersModule } from '../post-parsers/post-parsers.module';
import { SettingsModule } from '../settings/settings.module';
import { ValidationModule } from '../validation/validation.module';
import { WebsiteOptionsModule } from '../website-options/website-options.module';
import { WebsiteImplProvider } from '../websites/implementations';
import { WebsitesModule } from '../websites/websites.module';
import { PostFileResizerService } from './post-file-resizer.service';
import { PostManagerService } from './post-manager.service';
import { PostController } from './post.controller';
import { PostService } from './post.service';
import { PostFileResizerService } from './services/post-file-resizer/post-file-resizer.service';
import { PostManagerService } from './services/post-manager/post-manager.service';
import { PostQueueController } from './services/post-queue/post-queue.controller';
import { PostQueueService } from './services/post-queue/post-queue.service';

@Module({
imports: [
Expand All @@ -19,13 +22,15 @@ import { PostService } from './post.service';
PostParsersModule,
ValidationModule,
FileConverterModule,
SettingsModule,
],
controllers: [PostController],
controllers: [PostController, PostQueueController],
providers: [
PostService,
PostManagerService,
PostFileResizerService,
WebsiteImplProvider,
PostQueueService,
],
})
export class PostModule {}
4 changes: 2 additions & 2 deletions apps/client-server/src/app/post/post.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ import { UserSpecifiedWebsiteOptionsModule } from '../user-specified-website-opt
import { ValidationModule } from '../validation/validation.module';
import { WebsiteOptionsModule } from '../website-options/website-options.module';
import { WebsitesModule } from '../websites/websites.module';
import { PostFileResizerService } from './post-file-resizer.service';
import { PostManagerService } from './post-manager.service';
import { PostModule } from './post.module';
import { PostService } from './post.service';
import { PostFileResizerService } from './services/post-file-resizer/post-file-resizer.service';
import { PostManagerService } from './services/post-manager/post-manager.service';

describe('PostService', () => {
let service: PostService;
Expand Down
Loading

0 comments on commit 9ff7d80

Please sign in to comment.