fix (backend): don't use findOneBy to check existence

This commit is contained in:
naskya 2024-06-02 07:28:37 +09:00
parent 071ba83704
commit ff5bb2ed3d
No known key found for this signature in database
GPG key ID: 712D413B3A9FED5C
4 changed files with 24 additions and 19 deletions

View file

@ -82,15 +82,17 @@ export async function signup(opts: {
let account!: User;
const exists = await Users.existsBy({
usernameLower: username.toLowerCase(),
host: IsNull(),
});
if (exists) {
throw new Error("the username is already used");
}
// Start transaction
await db.transaction(async (transactionalEntityManager) => {
const exist = await transactionalEntityManager.findOneBy(User, {
usernameLower: username.toLowerCase(),
host: IsNull(),
});
if (exist) throw new Error(" the username is already used");
const now = new Date();
account = await transactionalEntityManager.save(

View file

@ -47,12 +47,12 @@ export default define(meta, paramDef, async (ps, user) => {
});
// Check if already muting
const exist = await ReplyMutings.findOneBy({
const exists = await ReplyMutings.existsBy({
muterId: muter.id,
muteeId: mutee.id,
});
if (exist != null) {
if (exists) {
throw new ApiError(meta.errors.alreadyMuting);
}

View file

@ -45,18 +45,18 @@ export default define(meta, paramDef, async (ps, user) => {
});
// Check not muting
const exist = await ReplyMutings.findOneBy({
const record = await ReplyMutings.findOneBy({
muterId: muter.id,
muteeId: mutee.id,
});
if (exist == null) {
if (record == null) {
throw new ApiError(meta.errors.notMuting);
}
// Delete mute
await ReplyMutings.delete({
id: exist.id,
id: record.id,
});
// publishUserEvent(user.id, "unmute", mutee);

View file

@ -2,6 +2,7 @@ import { v4 as uuid } from "uuid";
import { genRsaKeyPair } from "@/misc/gen-key-pair.js";
import { User } from "@/models/entities/user.js";
import { UserProfile } from "@/models/entities/user-profile.js";
import { Users } from "@/models/index.js";
import { IsNull } from "typeorm";
import { generateUserToken, genIdAt, hashPassword } from "backend-rs";
import { UserKeypair } from "@/models/entities/user-keypair.js";
@ -21,17 +22,19 @@ export async function createSystemUser(username: string) {
let account!: User;
const exists = await Users.existsBy({
usernameLower: username.toLowerCase(),
host: IsNull(),
});
if (exists) {
throw new Error("the user already exists");
}
const now = new Date();
// Start transaction
await db.transaction(async (transactionalEntityManager) => {
const exist = await transactionalEntityManager.findOneBy(User, {
usernameLower: username.toLowerCase(),
host: IsNull(),
});
if (exist) throw new Error("the user is already exists");
account = await transactionalEntityManager
.insert(User, {
id: genIdAt(now),