Update NoteDraftService.ts

This commit is contained in:
syuilo 2025-09-24 19:03:21 +09:00
parent f1421644cd
commit 4b378af63d
1 changed files with 14 additions and 47 deletions

View File

@ -17,14 +17,13 @@ import { IdentifiableError } from '@/misc/identifiable-error.js';
import { isRenote, isQuote } from '@/misc/is-renote.js'; import { isRenote, isQuote } from '@/misc/is-renote.js';
import { NoteEntityService } from '@/core/entities/NoteEntityService.js'; import { NoteEntityService } from '@/core/entities/NoteEntityService.js';
import { QueueService } from '@/core/QueueService.js'; import { QueueService } from '@/core/QueueService.js';
import { deepClone } from '@/misc/clone.js';
export type NoteDraftOptions = { export type NoteDraftOptions = {
replyId: MiNote['id'] | null; replyId: MiNote['id'] | null;
renoteId: MiNote['id'] | null; renoteId: MiNote['id'] | null;
text: string | null; text: string | null;
cw: string | null; cw: string | null;
localOnly: boolean | null; localOnly: boolean;
reactionAcceptance: typeof noteReactionAcceptances[number]; reactionAcceptance: typeof noteReactionAcceptances[number];
visibility: typeof noteVisibilities[number]; visibility: typeof noteVisibilities[number];
fileIds: MiDriveFile['id'][]; fileIds: MiDriveFile['id'][];
@ -77,7 +76,6 @@ export class NoteDraftService {
@bindThis @bindThis
public async create(me: MiLocalUser, data: NoteDraftOptions): Promise<MiNoteDraft> { public async create(me: MiLocalUser, data: NoteDraftOptions): Promise<MiNoteDraft> {
//#region check draft limit //#region check draft limit
const currentCount = await this.noteDraftsRepository.countBy({ const currentCount = await this.noteDraftsRepository.countBy({
userId: me.id, userId: me.id,
}); });
@ -96,11 +94,13 @@ export class NoteDraftService {
} }
} }
const appliedDraft = await this.checkAndSetDraftNoteOptions(me, this.noteDraftsRepository.create(), data); await this.validate(me, data);
appliedDraft.id = this.idService.gen(); const draft = await this.noteDraftsRepository.insertOne({
appliedDraft.userId = me.id; ...data,
const draft = await this.noteDraftsRepository.insertOne(appliedDraft); id: this.idService.gen(),
userId: me.id,
});
if (draft.scheduledAt && draft.isActuallyScheduled) { if (draft.scheduledAt && draft.isActuallyScheduled) {
this.schedule(draft); this.schedule(draft);
@ -130,19 +130,19 @@ export class NoteDraftService {
} }
} }
const appliedDraft = await this.checkAndSetDraftNoteOptions(me, draft, data); await this.validate(me, data);
await this.noteDraftsRepository.update(draftId, appliedDraft); await this.noteDraftsRepository.update(draftId, data);
this.clearSchedule(draft).then(() => { this.clearSchedule(draft).then(() => {
if (appliedDraft.scheduledAt != null && appliedDraft.isActuallyScheduled) { if (data.scheduledAt != null && data.isActuallyScheduled) {
this.schedule(draft); this.schedule(draft);
} }
}); });
return { return {
...draft, ...draft,
...appliedDraft, ...data,
}; };
} }
@ -176,25 +176,14 @@ export class NoteDraftService {
return draft; return draft;
} }
// 関連エンティティを取得し紐づける部分を共通化する
@bindThis @bindThis
public async checkAndSetDraftNoteOptions( public async validate(
me: MiLocalUser, me: MiLocalUser,
draft: MiNoteDraft,
data: Partial<NoteDraftOptions>, data: Partial<NoteDraftOptions>,
): Promise<MiNoteDraft> { ): Promise<void> {
data.visibility ??= 'public';
data.localOnly ??= false;
if (data.reactionAcceptance === undefined) data.reactionAcceptance = null;
if (data.channelId != null) {
data.visibility = 'public';
data.visibleUserIds = [];
data.localOnly = true;
}
//#region visibleUsers //#region visibleUsers
let visibleUsers: MiUser[] = []; let visibleUsers: MiUser[] = [];
if (data.visibleUserIds != null) { if (data.visibleUserIds != null && data.visibleUserIds.length > 0) {
visibleUsers = await this.usersRepository.findBy({ visibleUsers = await this.usersRepository.findBy({
id: In(data.visibleUserIds), id: In(data.visibleUserIds),
}); });
@ -308,28 +297,6 @@ export class NoteDraftService {
} }
} }
//#endregion //#endregion
return {
...draft,
visibility: data.visibility,
cw: data.cw ?? null,
fileIds: fileIds ?? [],
replyId: data.replyId ?? null,
renoteId: data.renoteId ?? null,
channelId: data.channelId ?? null,
text: data.text ?? null,
hashtag: data.hashtag ?? null,
hasPoll: data.poll != null,
pollChoices: data.poll ? data.poll.choices : [],
pollMultiple: data.poll ? data.poll.multiple : false,
pollExpiresAt: data.poll ? data.poll.expiresAt : null,
pollExpiredAfter: data.poll ? data.poll.expiredAfter ?? null : null,
visibleUserIds: data.visibleUserIds ?? [],
localOnly: data.localOnly,
reactionAcceptance: data.reactionAcceptance,
scheduledAt: data.scheduledAt ?? null,
isActuallyScheduled: data.isActuallyScheduled ?? false,
} satisfies MiNoteDraft;
} }
@bindThis @bindThis