fix (backend): don't use findOneBy
to check existence
This commit is contained in:
parent
071ba83704
commit
ff5bb2ed3d
4 changed files with 24 additions and 19 deletions
|
@ -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(
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -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),
|
||||
|
|
Loading…
Reference in a new issue