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; 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 // Start transaction
await db.transaction(async (transactionalEntityManager) => { 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(); const now = new Date();
account = await transactionalEntityManager.save( account = await transactionalEntityManager.save(

View file

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

View file

@ -45,18 +45,18 @@ export default define(meta, paramDef, async (ps, user) => {
}); });
// Check not muting // Check not muting
const exist = await ReplyMutings.findOneBy({ const record = await ReplyMutings.findOneBy({
muterId: muter.id, muterId: muter.id,
muteeId: mutee.id, muteeId: mutee.id,
}); });
if (exist == null) { if (record == null) {
throw new ApiError(meta.errors.notMuting); throw new ApiError(meta.errors.notMuting);
} }
// Delete mute // Delete mute
await ReplyMutings.delete({ await ReplyMutings.delete({
id: exist.id, id: record.id,
}); });
// publishUserEvent(user.id, "unmute", mutee); // 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 { genRsaKeyPair } from "@/misc/gen-key-pair.js";
import { User } from "@/models/entities/user.js"; import { User } from "@/models/entities/user.js";
import { UserProfile } from "@/models/entities/user-profile.js"; import { UserProfile } from "@/models/entities/user-profile.js";
import { Users } from "@/models/index.js";
import { IsNull } from "typeorm"; import { IsNull } from "typeorm";
import { generateUserToken, genIdAt, hashPassword } from "backend-rs"; import { generateUserToken, genIdAt, hashPassword } from "backend-rs";
import { UserKeypair } from "@/models/entities/user-keypair.js"; import { UserKeypair } from "@/models/entities/user-keypair.js";
@ -21,17 +22,19 @@ export async function createSystemUser(username: string) {
let account!: User; 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(); const now = new Date();
// Start transaction // Start transaction
await db.transaction(async (transactionalEntityManager) => { 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 account = await transactionalEntityManager
.insert(User, { .insert(User, {
id: genIdAt(now), id: genIdAt(now),