summaryrefslogtreecommitdiff
path: root/packages/backend/src/queue/processors/ScheduleNotePostProcessorService.ts
blob: 59e23b865e0e1c1839a0b6e732d961f1b7dfc710 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
/*
 * SPDX-FileCopyrightText: syuilo and other misskey contributors
 * SPDX-License-Identifier: AGPL-3.0-only
 */

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 { ChannelsRepository, DriveFilesRepository, MiDriveFile, NoteScheduleRepository, NotesRepository, UsersRepository } from '@/models/_.js';
import { DI } from '@/di-symbols.js';
import { NotificationService } from '@/core/NotificationService.js';
import { QueueLoggerService } from '../QueueLoggerService.js';
import type * as Bull from 'bullmq';
import type { ScheduleNotePostJobData } from '../types.js';

@Injectable()
export class ScheduleNotePostProcessorService {
	private logger: Logger;

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

		@Inject(DI.usersRepository)
		private usersRepository: UsersRepository,
		@Inject(DI.driveFilesRepository)
		private driveFilesRepository: DriveFilesRepository,
		@Inject(DI.notesRepository)
		private notesRepository: NotesRepository,
		@Inject(DI.channelsRepository)
		private channelsRepository: ChannelsRepository,

		private noteCreateService: NoteCreateService,
		private queueLoggerService: QueueLoggerService,
		private notificationService: NotificationService,
	) {
		this.logger = this.queueLoggerService.logger.createSubLogger('schedule-note-post');
	}

    @bindThis
	public async process(job: Bull.Job<ScheduleNotePostJobData>): Promise<void> {
		this.noteScheduleRepository.findOneBy({ id: job.data.scheduleNoteId }).then(async (data) => {
			if (!data) {
				this.logger.warn(`Schedule note ${job.data.scheduleNoteId} not found`);
			} else {
				const me = await this.usersRepository.findOneBy({ id: data.userId });
				const note = data.note;

				//idの形式でキューに積んであったのをDBから取り寄せる
				const reply = note.reply ? await this.notesRepository.findOneBy({ id: note.reply }) : undefined;
				const renote = note.reply ? await this.notesRepository.findOneBy({ id: note.renote }) : undefined;
				const channel = note.channel ? await this.channelsRepository.findOneBy({ id: note.channel, isArchived: false }) : undefined;
				let files: MiDriveFile[] = [];
				const fileIds = note.files;

				if (fileIds.length > 0 && me) {
					files = await this.driveFilesRepository.createQueryBuilder('file')
						.where('file.userId = :userId AND file.id IN (:...fileIds)', {
							userId: me.id,
							fileIds,
						})
						.orderBy('array_position(ARRAY[:...fileIds], "id"::text)')
						.setParameters({ fileIds })
						.getMany();
				}

				if (!data.userId || !me) {
					this.logger.warn('Schedule Note Failed Reason: User Not Found');
					await this.noteScheduleRepository.remove(data);
					return;
				}

				if (note.files.length !== files.length) {
					this.logger.warn('Schedule Note Failed Reason: files are missing in the user\'s drive');
					this.notificationService.createNotification(me.id, 'scheduledNoteFailed', {
						reason: 'Some attached files on your scheduled note no longer exist',
					});
					await this.noteScheduleRepository.remove(data);
					return;
				}

				if (note.reply && !reply) {
					this.logger.warn('Schedule Note Failed Reason: parent note to reply does not exist');
					this.notificationService.createNotification(me.id, 'scheduledNoteFailed', {
						reason: 'Replied to note on your scheduled note no longer exists',
					});
					await this.noteScheduleRepository.remove(data);
					return;
				}

				if (note.renote && !renote) {
					this.logger.warn('Schedule Note Failed Reason: attached quote note no longer exists');
					this.notificationService.createNotification(me.id, 'scheduledNoteFailed', {
						reason: 'A quoted note from one of your scheduled notes no longer exists',
					});
					await this.noteScheduleRepository.remove(data);
					return;
				}

				if (note.channel && !channel) {
					this.logger.warn('Schedule Note Failed Reason: Channel does not exist');
					this.notificationService.createNotification(me.id, 'scheduledNoteFailed', {
						reason: 'An attached channel on your scheduled note no longer exists',
					});
					await this.noteScheduleRepository.remove(data);
					return;
				}

				await this.noteCreateService.create(me, {
					...note,
					createdAt: new Date(),
					files,
					poll: note.poll ? {
						choices: note.poll.choices,
						multiple: note.poll.multiple,
						expiresAt: note.poll.expiresAt ? new Date(note.poll.expiresAt) : null,
					} : undefined,
					reply,
					renote,
					channel,
				});
				await this.noteScheduleRepository.remove(data);
			}
		});
	}
}