Files
misskey/packages/backend/src/server/api/endpoints/gallery/posts/update.ts
yupix b50eb511b0 refactor: api/*/update系の必須キーを最低限に (#13824)
* refactor: clips/updateの必須キーをclipIdのみに

* refactor: admin/roles/update の必須キーをroleIdのみに

* feat: pages/update の必須キーをpageIdのみに

* refactor: gallery/posts/update の必須キーをpostidのみに

* feat: misskey-jsの型を更新

* feat: i/webhooks/updateの必須キーをwebhookIdのみに

* feat: admin/ad/updateの必須キーをidのみに

* feat: misskey-jsの型を更新

* chore: update CHANGELOG.md

* docs: update CHANGELOG.md

* fix: secretが更新できなくなる場合がある

Co-authored-by: zyoshoka <107108195+zyoshoka@users.noreply.github.com>

* Update packages/backend/src/server/api/endpoints/gallery/posts/update.ts

---------

Co-authored-by: zyoshoka <107108195+zyoshoka@users.noreply.github.com>
Co-authored-by: syuilo <4439005+syuilo@users.noreply.github.com>
2024-06-22 14:52:27 +09:00

97 lines
2.4 KiB
TypeScript

/*
* SPDX-FileCopyrightText: syuilo and misskey-project
* SPDX-License-Identifier: AGPL-3.0-only
*/
import ms from 'ms';
import { Inject, Injectable } from '@nestjs/common';
import { Endpoint } from '@/server/api/endpoint-base.js';
import type { DriveFilesRepository, GalleryPostsRepository } from '@/models/_.js';
import type { MiDriveFile } from '@/models/DriveFile.js';
import { GalleryPostEntityService } from '@/core/entities/GalleryPostEntityService.js';
import { DI } from '@/di-symbols.js';
export const meta = {
tags: ['gallery'],
requireCredential: true,
prohibitMoved: true,
kind: 'write:gallery',
limit: {
duration: ms('1hour'),
max: 300,
},
res: {
type: 'object',
optional: false, nullable: false,
ref: 'GalleryPost',
},
errors: {
},
} as const;
export const paramDef = {
type: 'object',
properties: {
postId: { type: 'string', format: 'misskey:id' },
title: { type: 'string', minLength: 1 },
description: { type: 'string', nullable: true },
fileIds: { type: 'array', uniqueItems: true, minItems: 1, maxItems: 32, items: {
type: 'string', format: 'misskey:id',
} },
isSensitive: { type: 'boolean', default: false },
},
required: ['postId'],
} as const;
@Injectable()
export default class extends Endpoint<typeof meta, typeof paramDef> { // eslint-disable-line import/no-default-export
constructor(
@Inject(DI.galleryPostsRepository)
private galleryPostsRepository: GalleryPostsRepository,
@Inject(DI.driveFilesRepository)
private driveFilesRepository: DriveFilesRepository,
private galleryPostEntityService: GalleryPostEntityService,
) {
super(meta, paramDef, async (ps, me) => {
let files: Array<MiDriveFile> | undefined;
if (ps.fileIds) {
files = (await Promise.all(ps.fileIds.map(fileId =>
this.driveFilesRepository.findOneBy({
id: fileId,
userId: me.id,
}),
))).filter(x => x != null);
if (files.length === 0) {
throw new Error();
}
}
await this.galleryPostsRepository.update({
id: ps.postId,
userId: me.id,
}, {
updatedAt: new Date(),
title: ps.title,
description: ps.description,
isSensitive: ps.isSensitive,
fileIds: files ? files.map(file => file.id) : undefined,
});
const post = await this.galleryPostsRepository.findOneByOrFail({ id: ps.postId });
return await this.galleryPostEntityService.pack(post, me);
});
}
}