Skip to content

Commit

Permalink
予約投稿をdbに保存する
Browse files Browse the repository at this point in the history
cherrypick from Type4ny-Project/Type4ny@387faf5

# Conflicts:
#	package.json
#	packages/backend/src/models/RepositoryModule.ts
#	packages/backend/src/postgres.ts

Co-authored-by: まっちゃてぃー。 <[email protected]>Signed-off-by: mattyatea <[email protected]>
  • Loading branch information
kozakura913 and mattyatea committed Sep 28, 2024
1 parent 1892003 commit 3bc7787
Show file tree
Hide file tree
Showing 7 changed files with 92 additions and 40 deletions.
11 changes: 11 additions & 0 deletions packages/backend/migration/1699337454434-schedulenote.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
export class Schedulenote1699337454434 {
name = 'Schedulenote1699337454434'

async up(queryRunner) {
await queryRunner.query(`CREATE TABLE "note_schedule" ("id" character varying(32) NOT NULL, "note" jsonb NOT NULL, "userId" character varying(260) NOT NULL, CONSTRAINT "PK_3a1ae2db41988f4994268218436" PRIMARY KEY ("id"))`);
}

async down(queryRunner) {
await queryRunner.query(`DROP TABLE "note_schedule"`);
}
}
15 changes: 6 additions & 9 deletions packages/backend/src/models/NoteSchedule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,13 @@ import type { MiDriveFile } from './DriveFile.js';
@Entity('note_schedule')
export class MiNoteSchedule {
@PrimaryColumn(id())
public noteId: MiNote['id'];
public id: string;

@Column('string')
public note:{ apEmojis: any[] | undefined; visibility: any; apMentions: any[] | undefined; visibleUsers: MiUser[]; channel: null | MiChannel; poll: { multiple: any; choices: any; expiresAt: Date | null } | undefined; renote: null | MiNote; localOnly: any; cw: any; apHashtags: any[] | undefined; reactionAcceptance: any; files: MiDriveFile[]; text: any; reply: null | MiNote };
@Column('jsonb')
public note:{createdAt?: Date | undefined ; apEmojis: any[] | undefined; visibility: any; apMentions: any[] | undefined; visibleUsers: MiUser[]; channel: null | MiChannel; poll: { multiple: any; choices: any; expiresAt: Date | null } | undefined; renote: null | MiNote; localOnly: any; cw: any; apHashtags: any[] | undefined; reactionAcceptance: any; files: MiDriveFile[]; text: any; reply: null | MiNote };

@Column('string')
public user: MiUser & {host: null;uri: null;};

@Column('timestamp with time zone', {
nullable: true,
@Column('varchar', {
length: 260,
})
public expiresAt: Date;
public userId: MiUser['id'];
}
9 changes: 9 additions & 0 deletions packages/backend/src/models/RepositoryModule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ import {
MiUserSecurityKey,
MiWebhook,
MiOfficialTag,
MiNoteSchedule,
} from './_.js';
import type { Provider } from '@nestjs/common';
import type { DataSource } from 'typeorm';
Expand All @@ -102,6 +103,12 @@ const $notesRepository: Provider = {
inject: [DI.db],
};

const $noteScheduleRepository: Provider = {
provide: DI.noteScheduleRepository,
useFactory: (db: DataSource) => db.getRepository(MiNoteSchedule).extend(miRepository as MiRepository<MiNoteSchedule>),
inject: [DI.db],
};

const $announcementsRepository: Provider = {
provide: DI.announcementsRepository,
useFactory: (db: DataSource) => db.getRepository(MiAnnouncement).extend(miRepository as MiRepository<MiAnnouncement>),
Expand Down Expand Up @@ -563,6 +570,7 @@ const $officialTagRepository: Provider = {
providers: [
$usersRepository,
$notesRepository,
$noteScheduleRepository,
$announcementsRepository,
$announcementReadsRepository,
$appsRepository,
Expand Down Expand Up @@ -643,6 +651,7 @@ const $officialTagRepository: Provider = {
exports: [
$usersRepository,
$notesRepository,
$noteScheduleRepository,
$announcementsRepository,
$announcementReadsRepository,
$appsRepository,
Expand Down
2 changes: 2 additions & 0 deletions packages/backend/src/postgres.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ import { MiFlashLikeRemote } from '@/models/FlashLikeRemote.js';
import { MiUserMemo } from '@/models/UserMemo.js';
import { MiBubbleGameRecord } from '@/models/BubbleGameRecord.js';
import { MiReversiGame } from '@/models/ReversiGame.js';
import { MiNoteSchedule } from '@/models/NoteSchedule.js';

import { Config } from '@/config.js';
import MisskeyLogger from '@/logger.js';
Expand Down Expand Up @@ -166,6 +167,7 @@ export const entities = [
MiRenoteMuting,
MiBlocking,
MiNote,
MiNoteSchedule,
MiNoteFavorite,
MiNoteReaction,
MiNoteThreadMuting,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import { Inject, Injectable } from '@nestjs/common';
import type Logger from '@/logger.js';
import { bindThis } from '@/decorators.js';
import { NoteCreateService } from '@/core/NoteCreateService.js';
import type { NoteScheduleRepository, UsersRepository } from '@/models/_.js';
import { DI } from '@/di-symbols.js';
import { QueueLoggerService } from '../QueueLoggerService.js';
import type * as Bull from 'bullmq';
import type { ScheduleNotePostJobData } from '../types.js';
Expand All @@ -16,6 +18,12 @@ export class ScheduleNotePostProcessorService {
private logger: Logger;

constructor(
@Inject(DI.noteScheduleRepository)
private noteScheduleRepository: NoteScheduleRepository,

@Inject(DI.usersRepository)
private usersRepository: UsersRepository,

private noteCreateService: NoteCreateService,
private queueLoggerService: QueueLoggerService,
) {
Expand All @@ -24,7 +32,15 @@ export class ScheduleNotePostProcessorService {

@bindThis
public async process(job: Bull.Job<ScheduleNotePostJobData>): Promise<void> {
job.data.note.createdAt = new Date();
await this.noteCreateService.create(job.data.user, job.data.note);
this.noteScheduleRepository.findOneBy({ id: job.data.scheduleNoteId }).then(async (data) => {
if (!data) {
this.logger.warn(`Schedule note ${job.data.scheduleNoteId} not found`);
} else {
data.note.createdAt = new Date();
const me = await this.usersRepository.findOneByOrFail({ id: data.userId });
await this.noteCreateService.create(me, data.note);
await this.noteScheduleRepository.remove(data);
}
});
}
}
23 changes: 1 addition & 22 deletions packages/backend/src/queue/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,28 +114,7 @@ export type EndedPollNotificationJobData = {
};

export type ScheduleNotePostJobData = {
note: {
name?: string | null;
text?: string | null;
reply?: MiNote | null;
renote?: MiNote | null;
files?: MiDriveFile[] | null;
poll?: IPoll | null;
schedule?: MiNoteSchedule | null;
localOnly?: boolean | null;
reactionAcceptance?: MiNote['reactionAcceptance'];
cw?: string | null;
visibility?: string;
visibleUsers?: MinimumUser[] | null;
channel?: MiChannel | null;
apMentions?: MinimumUser[] | null;
apHashtags?: string[] | null;
apEmojis?: string[] | null;
uri?: string | null;
url?: string | null;
app?: MiApp | null;
};
user: MiUser & {host: null, uri: null};
scheduleNoteId: MiNote['id'];
}

type MinimumUser = {
Expand Down
52 changes: 45 additions & 7 deletions packages/backend/src/server/api/endpoints/notes/create-schedule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,14 @@ import { DataSource, In } from 'typeorm';
import { Inject, Injectable } from '@nestjs/common';
import { isPureRenote } from 'cherrypick-js/note.js';
import type { MiUser } from '@/models/User.js';
import type { UsersRepository, NotesRepository, BlockingsRepository, DriveFilesRepository, ChannelsRepository } from '@/models/_.js';
import type {
UsersRepository,
NotesRepository,
BlockingsRepository,
DriveFilesRepository,
ChannelsRepository,
NoteScheduleRepository,
} from '@/models/_.js';
import type { MiDriveFile } from '@/models/DriveFile.js';
import type { MiNote } from '@/models/Note.js';
import type { MiChannel } from '@/models/Channel.js';
Expand All @@ -18,6 +25,8 @@ import { NoteEntityService } from '@/core/entities/NoteEntityService.js';
import { NoteCreateService } from '@/core/NoteCreateService.js';
import { DI } from '@/di-symbols.js';
import { QueueService } from '@/core/QueueService.js';
import { MiNoteSchedule } from '@/models/_.js';
import { IdService } from '@/core/IdService.js';
import { ApiError } from '../../error.js';

export const meta = {
Expand Down Expand Up @@ -194,6 +203,9 @@ export default class extends Endpoint<typeof meta, typeof paramDef> { // eslint-
@Inject(DI.notesRepository)
private notesRepository: NotesRepository,

@Inject(DI.noteScheduleRepository)
private noteScheduleRepository: NoteScheduleRepository,

@Inject(DI.blockingsRepository)
private blockingsRepository: BlockingsRepository,

Expand All @@ -203,9 +215,8 @@ export default class extends Endpoint<typeof meta, typeof paramDef> { // eslint-
@Inject(DI.channelsRepository)
private channelsRepository: ChannelsRepository,

private noteEntityService: NoteEntityService,
private queueService: QueueService,
private noteCreateService: NoteCreateService,
private idService: IdService,
) {
super(meta, paramDef, async (ps, me) => {
let visibleUsers: MiUser[] = [];
Expand Down Expand Up @@ -328,8 +339,28 @@ export default class extends Endpoint<typeof meta, typeof paramDef> { // eslint-
}
}
}

const note = {
type NoteType = {
createdAt?: Date | undefined;
apEmojis: any[] | undefined;
visibility: any;
apMentions: any[] | undefined;
visibleUsers: MiUser[];
channel: null | MiChannel;
poll: {
multiple: any;
choices: any;
expiresAt: Date | null;
} | undefined;
renote: null | MiNote;
localOnly: any;
cw: any;
apHashtags: any[] | undefined;
reactionAcceptance: any;
files: MiDriveFile[];
text: any;
reply: null | MiNote;
};
const note:NoteType = {
files: files,
poll: ps.poll ? {
choices: ps.poll.choices,
Expand All @@ -351,10 +382,17 @@ export default class extends Endpoint<typeof meta, typeof paramDef> { // eslint-
};

if (ps.schedule && ps.schedule.expiresAt) {
me.token = null;
const noteId = this.idService.gen(new Date().getTime());
await this.noteScheduleRepository.insert({
id: noteId,
note: note,
userId: me.id,
});

const delay = new Date(ps.schedule.expiresAt).getTime() - Date.now();
await this.queueService.ScheduleNotePostQueue.add(String(delay), {
note: note,
user: me,
scheduleNoteId: noteId,
}, {
delay,
removeOnComplete: true,
Expand Down

0 comments on commit 3bc7787

Please sign in to comment.