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