refactor: migrate to typeorm 3.0 (#8443)

* wip

* wip

* wip

* Update following.ts

* wip

* wip

* wip

* Update resolve-user.ts

* maxQueryExecutionTime

* wip

* wip
This commit is contained in:
syuilo
2022-03-26 15:34:00 +09:00
committed by GitHub
parent 41c87074e6
commit 1c67c26bd8
325 changed files with 1314 additions and 1494 deletions

View File

@@ -3,6 +3,7 @@ import { ApiError } from '../../error.js';
import { Users, Followings, UserProfiles } from '@/models/index.js';
import { makePaginationQuery } from '../../common/make-pagination-query.js';
import { toPunyNullable } from '@/misc/convert-host.js';
import { IsNull } from 'typeorm';
export const meta = {
tags: ['users'],
@@ -49,15 +50,15 @@ export const paramDef = {
// eslint-disable-next-line import/no-default-export
export default define(meta, paramDef, async (ps, me) => {
const user = await Users.findOne(ps.userId != null
const user = await Users.findOneBy(ps.userId != null
? { id: ps.userId }
: { usernameLower: ps.username!.toLowerCase(), host: toPunyNullable(ps.host) });
: { usernameLower: ps.username!.toLowerCase(), host: toPunyNullable(ps.host) ?? IsNull() });
if (user == null) {
throw new ApiError(meta.errors.noSuchUser);
}
const profile = await UserProfiles.findOneOrFail(user.id);
const profile = await UserProfiles.findOneByOrFail({ userId: user.id });
if (profile.ffVisibility === 'private') {
if (me == null || (me.id !== user.id)) {
@@ -67,7 +68,7 @@ export default define(meta, paramDef, async (ps, me) => {
if (me == null) {
throw new ApiError(meta.errors.forbidden);
} else if (me.id !== user.id) {
const following = await Followings.findOne({
const following = await Followings.findOneBy({
followeeId: user.id,
followerId: me.id,
});

View File

@@ -3,6 +3,7 @@ import { ApiError } from '../../error.js';
import { Users, Followings, UserProfiles } from '@/models/index.js';
import { makePaginationQuery } from '../../common/make-pagination-query.js';
import { toPunyNullable } from '@/misc/convert-host.js';
import { IsNull } from 'typeorm';
export const meta = {
tags: ['users'],
@@ -49,15 +50,15 @@ export const paramDef = {
// eslint-disable-next-line import/no-default-export
export default define(meta, paramDef, async (ps, me) => {
const user = await Users.findOne(ps.userId != null
const user = await Users.findOneBy(ps.userId != null
? { id: ps.userId }
: { usernameLower: ps.username!.toLowerCase(), host: toPunyNullable(ps.host) });
: { usernameLower: ps.username!.toLowerCase(), host: toPunyNullable(ps.host) ?? IsNull() });
if (user == null) {
throw new ApiError(meta.errors.noSuchUser);
}
const profile = await UserProfiles.findOneOrFail(user.id);
const profile = await UserProfiles.findOneByOrFail({ userId: user.id });
if (profile.ffVisibility === 'private') {
if (me == null || (me.id !== user.id)) {
@@ -67,7 +68,7 @@ export default define(meta, paramDef, async (ps, me) => {
if (me == null) {
throw new ApiError(meta.errors.forbidden);
} else if (me.id !== user.id) {
const following = await Followings.findOne({
const following = await Followings.findOneBy({
followeeId: user.id,
followerId: me.id,
});

View File

@@ -33,7 +33,7 @@ export default define(meta, paramDef, async (ps, user) => {
createdAt: new Date(),
userId: user.id,
name: ps.name,
} as UserGroup).then(x => UserGroups.findOneOrFail(x.identifiers[0]));
} as UserGroup).then(x => UserGroups.findOneByOrFail(x.identifiers[0]));
// Push the owner
await UserGroupJoinings.insert({

View File

@@ -28,7 +28,7 @@ export const paramDef = {
// eslint-disable-next-line import/no-default-export
export default define(meta, paramDef, async (ps, user) => {
const userGroup = await UserGroups.findOne({
const userGroup = await UserGroups.findOneBy({
id: ps.groupId,
userId: user.id,
});

View File

@@ -31,7 +31,7 @@ export const paramDef = {
// eslint-disable-next-line import/no-default-export
export default define(meta, paramDef, async (ps, user) => {
// Fetch the invitation
const invitation = await UserGroupInvitations.findOne({
const invitation = await UserGroupInvitations.findOneBy({
id: ps.invitationId,
});

View File

@@ -29,7 +29,7 @@ export const paramDef = {
// eslint-disable-next-line import/no-default-export
export default define(meta, paramDef, async (ps, user) => {
// Fetch the invitation
const invitation = await UserGroupInvitations.findOne({
const invitation = await UserGroupInvitations.findOneBy({
id: ps.invitationId,
});

View File

@@ -52,7 +52,7 @@ export const paramDef = {
// eslint-disable-next-line import/no-default-export
export default define(meta, paramDef, async (ps, me) => {
// Fetch the group
const userGroup = await UserGroups.findOne({
const userGroup = await UserGroups.findOneBy({
id: ps.groupId,
userId: me.id,
});
@@ -67,7 +67,7 @@ export default define(meta, paramDef, async (ps, me) => {
throw e;
});
const joining = await UserGroupJoinings.findOne({
const joining = await UserGroupJoinings.findOneBy({
userGroupId: userGroup.id,
userId: user.id,
});
@@ -76,7 +76,7 @@ export default define(meta, paramDef, async (ps, me) => {
throw new ApiError(meta.errors.alreadyAdded);
}
const existInvitation = await UserGroupInvitations.findOne({
const existInvitation = await UserGroupInvitations.findOneBy({
userGroupId: userGroup.id,
userId: user.id,
});
@@ -90,7 +90,7 @@ export default define(meta, paramDef, async (ps, me) => {
createdAt: new Date(),
userId: user.id,
userGroupId: userGroup.id,
} as UserGroupInvitation).then(x => UserGroupInvitations.findOneOrFail(x.identifiers[0]));
} as UserGroupInvitation).then(x => UserGroupInvitations.findOneByOrFail(x.identifiers[0]));
// 通知を作成
createNotification(user.id, 'groupInvited', {

View File

@@ -28,11 +28,11 @@ export const paramDef = {
// eslint-disable-next-line import/no-default-export
export default define(meta, paramDef, async (ps, me) => {
const ownedGroups = await UserGroups.find({
const ownedGroups = await UserGroups.findBy({
userId: me.id,
});
const joinings = await UserGroupJoinings.find({
const joinings = await UserGroupJoinings.findBy({
userId: me.id,
...(ownedGroups.length > 0 ? {
userGroupId: Not(In(ownedGroups.map(x => x.id))),

View File

@@ -35,7 +35,7 @@ export const paramDef = {
// eslint-disable-next-line import/no-default-export
export default define(meta, paramDef, async (ps, me) => {
// Fetch the group
const userGroup = await UserGroups.findOne({
const userGroup = await UserGroups.findOneBy({
id: ps.groupId,
});

View File

@@ -27,7 +27,7 @@ export const paramDef = {
// eslint-disable-next-line import/no-default-export
export default define(meta, paramDef, async (ps, me) => {
const userGroups = await UserGroups.find({
const userGroups = await UserGroups.findBy({
userId: me.id,
});

View File

@@ -43,7 +43,7 @@ export const paramDef = {
// eslint-disable-next-line import/no-default-export
export default define(meta, paramDef, async (ps, me) => {
// Fetch the group
const userGroup = await UserGroups.findOne({
const userGroup = await UserGroups.findOneBy({
id: ps.groupId,
userId: me.id,
});

View File

@@ -35,7 +35,7 @@ export const paramDef = {
// eslint-disable-next-line import/no-default-export
export default define(meta, paramDef, async (ps, me) => {
// Fetch the group
const userGroup = await UserGroups.findOne({
const userGroup = await UserGroups.findOneBy({
id: ps.groupId,
});
@@ -43,7 +43,7 @@ export default define(meta, paramDef, async (ps, me) => {
throw new ApiError(meta.errors.noSuchGroup);
}
const joining = await UserGroupJoinings.findOne({
const joining = await UserGroupJoinings.findOneBy({
userId: me.id,
userGroupId: userGroup.id,
});

View File

@@ -49,7 +49,7 @@ export const paramDef = {
// eslint-disable-next-line import/no-default-export
export default define(meta, paramDef, async (ps, me) => {
// Fetch the group
const userGroup = await UserGroups.findOne({
const userGroup = await UserGroups.findOneBy({
id: ps.groupId,
userId: me.id,
});
@@ -64,7 +64,7 @@ export default define(meta, paramDef, async (ps, me) => {
throw e;
});
const joining = await UserGroupJoinings.findOne({
const joining = await UserGroupJoinings.findOneBy({
userGroupId: userGroup.id,
userId: user.id,
});

View File

@@ -36,7 +36,7 @@ export const paramDef = {
// eslint-disable-next-line import/no-default-export
export default define(meta, paramDef, async (ps, me) => {
// Fetch the group
const userGroup = await UserGroups.findOne({
const userGroup = await UserGroups.findOneBy({
id: ps.groupId,
userId: me.id,
});

View File

@@ -32,7 +32,7 @@ export default define(meta, paramDef, async (ps, user) => {
createdAt: new Date(),
userId: user.id,
name: ps.name,
} as UserList).then(x => UserLists.findOneOrFail(x.identifiers[0]));
} as UserList).then(x => UserLists.findOneByOrFail(x.identifiers[0]));
return await UserLists.pack(userList);
});

View File

@@ -28,7 +28,7 @@ export const paramDef = {
// eslint-disable-next-line import/no-default-export
export default define(meta, paramDef, async (ps, user) => {
const userList = await UserLists.findOne({
const userList = await UserLists.findOneBy({
id: ps.listId,
userId: user.id,
});

View File

@@ -27,7 +27,7 @@ export const paramDef = {
// eslint-disable-next-line import/no-default-export
export default define(meta, paramDef, async (ps, me) => {
const userLists = await UserLists.find({
const userLists = await UserLists.findBy({
userId: me.id,
});

View File

@@ -38,7 +38,7 @@ export const paramDef = {
// eslint-disable-next-line import/no-default-export
export default define(meta, paramDef, async (ps, me) => {
// Fetch the list
const userList = await UserLists.findOne({
const userList = await UserLists.findOneBy({
id: ps.listId,
userId: me.id,
});

View File

@@ -50,7 +50,7 @@ export const paramDef = {
// eslint-disable-next-line import/no-default-export
export default define(meta, paramDef, async (ps, me) => {
// Fetch the list
const userList = await UserLists.findOne({
const userList = await UserLists.findOneBy({
id: ps.listId,
userId: me.id,
});
@@ -67,7 +67,7 @@ export default define(meta, paramDef, async (ps, me) => {
// Check blocking
if (user.id !== me.id) {
const block = await Blockings.findOne({
const block = await Blockings.findOneBy({
blockerId: user.id,
blockeeId: me.id,
});
@@ -76,7 +76,7 @@ export default define(meta, paramDef, async (ps, me) => {
}
}
const exist = await UserListJoinings.findOne({
const exist = await UserListJoinings.findOneBy({
userListId: userList.id,
userId: user.id,
});

View File

@@ -35,7 +35,7 @@ export const paramDef = {
// eslint-disable-next-line import/no-default-export
export default define(meta, paramDef, async (ps, me) => {
// Fetch the list
const userList = await UserLists.findOne({
const userList = await UserLists.findOneBy({
id: ps.listId,
userId: me.id,
});

View File

@@ -36,7 +36,7 @@ export const paramDef = {
// eslint-disable-next-line import/no-default-export
export default define(meta, paramDef, async (ps, user) => {
// Fetch the list
const userList = await UserLists.findOne({
const userList = await UserLists.findOneBy({
id: ps.listId,
userId: user.id,
});

View File

@@ -43,7 +43,7 @@ export const paramDef = {
// eslint-disable-next-line import/no-default-export
export default define(meta, paramDef, async (ps, me) => {
const profile = await UserProfiles.findOneOrFail(ps.userId);
const profile = await UserProfiles.findOneByOrFail({ userId: ps.userId });
if (me == null || (me.id !== ps.userId && !profile.publicReactions)) {
throw new ApiError(meta.errors.reactionsNotPublic);

View File

@@ -67,7 +67,7 @@ export default define(meta, paramDef, async (ps, me) => {
reporterId: me.id,
reporterHost: null,
comment: ps.comment,
}).then(x => AbuseUserReports.findOneOrFail(x.identifiers[0]));
}).then(x => AbuseUserReports.findOneByOrFail(x.identifiers[0]));
// Publish event to moderators
setImmediate(async () => {

View File

@@ -3,7 +3,7 @@ import define from '../../define.js';
import { apiLogger } from '../../logger.js';
import { ApiError } from '../../error.js';
import { Users } from '@/models/index.js';
import { In } from 'typeorm';
import { FindOptionsWhere, In, IsNull } from 'typeorm';
import { User } from '@/models/entities/user.js';
export const meta = {
@@ -68,7 +68,7 @@ export default define(meta, paramDef, async (ps, me) => {
return [];
}
const users = await Users.find(isAdminOrModerator ? {
const users = await Users.findBy(isAdminOrModerator ? {
id: In(ps.userIds),
} : {
id: In(ps.userIds),
@@ -92,11 +92,11 @@ export default define(meta, paramDef, async (ps, me) => {
throw new ApiError(meta.errors.failedToResolveRemoteUser);
});
} else {
const q: any = ps.userId != null
const q: FindOptionsWhere<User> = ps.userId != null
? { id: ps.userId }
: { usernameLower: ps.username!.toLowerCase(), host: null };
: { usernameLower: ps.username!.toLowerCase(), host: IsNull() };
user = await Users.findOne(q);
user = await Users.findOneBy(q);
}
if (user == null || (!isAdminOrModerator && user.isSuspended)) {

View File

@@ -26,7 +26,7 @@ export const paramDef = {
// eslint-disable-next-line import/no-default-export
export default define(meta, paramDef, async (ps, me) => {
const user = await Users.findOne(ps.userId);
const user = await Users.findOneBy({ id: ps.userId });
if (user == null) {
throw new ApiError(meta.errors.noSuchUser);
}