use count instead of find to check existence

This commit is contained in:
Johann150 2023-01-02 14:43:27 +01:00
parent d28931bf00
commit 7bf4d4426a
Signed by untrusted user: Johann150
GPG key ID: 9EE6577A2A06F8F1

View file

@ -7,6 +7,8 @@ import { ApiError } from '../../error.js';
export const meta = {
tags: ['channels'],
description: 'Creates a new channel with the current user as its administrator.',
requireCredential: true,
kind: 'write:channels',
@ -32,14 +34,13 @@ export const paramDef = {
// eslint-disable-next-line import/no-default-export
export default define(meta, paramDef, async (ps, user) => {
let banner = null;
if (ps.bannerId != null) {
banner = await DriveFiles.findOneBy({
const bannerExists = await DriveFiles.countBy({
id: ps.bannerId,
userId: user.id,
});
if (banner == null) throw new ApiError('NO_SUCH_FILE');
if (!bannerExists) throw new ApiError('NO_SUCH_FILE');
}
const channel = await Channels.insert({
@ -47,8 +48,8 @@ export default define(meta, paramDef, async (ps, user) => {
createdAt: new Date(),
userId: user.id,
name: ps.name,
description: ps.description || null,
bannerId: banner ? banner.id : null,
description: ps.description,
bannerId: ps.bannerId,
} as Channel).then(x => Channels.findOneByOrFail(x.identifiers[0]));
return await Channels.pack(channel, user);