server: fix custom lint typeorm-prefer-count

This commit is contained in:
Johann150 2023-01-02 21:07:02 +01:00
parent 29714d1ae0
commit b423d23cf6
Signed by untrusted user: Johann150
GPG key ID: 9EE6577A2A06F8F1
30 changed files with 73 additions and 72 deletions

View file

@ -75,7 +75,7 @@ export async function toDbReaction(reaction?: string | null, idnReacterHost?: st
const custom = reaction.match(/^:([\w+-]+)(?:@\.)?:$/);
if (custom) {
const name = custom[1];
const emoji = await Emojis.findOneBy({
const emoji = await Emojis.countBy({
host: reacterHost ?? IsNull(),
name,
});

View file

@ -12,7 +12,7 @@ export default async (actor: CacheableRemoteUser, activity: IAccept): Promise<st
return 'skip: follower not found';
}
const following = await Followings.findOneBy({
const following = await Followings.countBy({
followerId: follower.id,
followeeId: actor.id,
});

View file

@ -17,18 +17,18 @@ export default async (actor: CacheableRemoteUser, activity: IFollow): Promise<st
return 'skip: the unfollowed user is not local';
}
const [req, following] = await Promise.all([
FollowRequests.findOneBy({
const [requested, following] = await Promise.all([
FollowRequests.countBy({
followerId: actor.id,
followeeId: followee.id,
}),
Followings.findOneBy({
Followings.countBy({
followerId: actor.id,
followeeId: followee.id,
}),
]);
if (req) {
if (requested) {
await cancelFollowRequest(followee, actor);
return 'ok: follow request canceled';
} else if (following) {

View file

@ -142,7 +142,7 @@ export async function createNote(value: string | IObject, resolver: Resolver, si
const uri = getApId(note.inReplyTo);
if (uri.startsWith(config.url + '/')) {
const id = uri.split('/').pop();
const talk = await MessagingMessages.findOneBy({ id });
const talk = await MessagingMessages.countBy({ id });
if (talk) {
isTalk = true;
return null;

View file

@ -70,7 +70,7 @@ export async function signup(opts: {
// Start transaction
await db.transaction(async transactionalEntityManager => {
const exist = await transactionalEntityManager.findOneBy(User, {
const exist = await transactionalEntityManager.countBy(User, {
usernameLower: username.toLowerCase(),
host: IsNull(),
});

View file

@ -23,9 +23,9 @@ export const paramDef = {
// eslint-disable-next-line import/no-default-export
export default define(meta, paramDef, async (ps, me) => {
const instance = await Instances.findOneBy({ host: toPuny(ps.host) });
const instanceExists = await Instances.countBy({ host: toPuny(ps.host) });
if (instance == null) {
if (!instanceExists) {
throw new ApiError('NO_SUCH_OBJECT');
}

View file

@ -48,12 +48,12 @@ export default define(meta, paramDef, async (ps, user) => {
});
// Check not blocking
const exist = await Blockings.findOneBy({
const exist = await Blockings.countBy({
blockerId: blocker.id,
blockeeId: blockee.id,
});
if (exist == null) throw new ApiError('NOT_BLOCKING');
if (!exist) throw new ApiError('NOT_BLOCKING');
// Delete blocking
await deleteBlocking(blocker, blockee);

View file

@ -48,12 +48,12 @@ export default define(meta, paramDef, async (ps, user) => {
});
// Check not following
const exist = await Followings.findOneBy({
const exist = await Followings.countBy({
followerId: follower.id,
followeeId: followee.id,
});
if (exist == null) throw new ApiError('NOT_FOLLOWING');
if (!exist) throw new ApiError('NOT_FOLLOWING');
await deleteFollowing(follower, followee);

View file

@ -48,12 +48,12 @@ export default define(meta, paramDef, async (ps, user) => {
});
// Check not following
const exist = await Followings.findOneBy({
const exist = await Followings.countBy({
followerId: follower.id,
followeeId: followee.id,
});
if (exist == null) throw new ApiError('NOT_FOLLOWING');
if (!exist) throw new ApiError('NOT_FOLLOWING');
await deleteFollowing(follower, followee);

View file

@ -25,9 +25,9 @@ export const paramDef = {
// eslint-disable-next-line import/no-default-export
export default define(meta, paramDef, async (ps, user) => {
// Check if announcement exists
const announcement = await Announcements.findOneBy({ id: ps.announcementId });
const exists = await Announcements.countBy({ id: ps.announcementId });
if (announcement == null) throw new ApiError('NO_SUCH_ANNOUNCEMENT');
if (!exists) throw new ApiError('NO_SUCH_ANNOUNCEMENT');
// Check if already read
const read = await AnnouncementReads.findOneBy({

View file

@ -18,9 +18,9 @@ export const paramDef = {
// eslint-disable-next-line import/no-default-export
export default define(meta, paramDef, async (ps, user) => {
const token = await AccessTokens.findOneBy({ id: ps.tokenId });
const exists = await AccessTokens.countBy({ id: ps.tokenId });
if (token) {
if (exists) {
await AccessTokens.delete({
id: ps.tokenId,
userId: user.id,

View file

@ -95,12 +95,12 @@ export default define(meta, paramDef, async (ps, user) => {
if (recipientGroup == null) throw new ApiError('NO_SUCH_GROUP');
// check joined
const joining = await UserGroupJoinings.findOneBy({
const joined = await UserGroupJoinings.countBy({
userId: user.id,
userGroupId: recipientGroup.id,
});
if (joining == null) throw new ApiError('ACCESS_DENIED', 'You have to join a group to read messages in it.');
if (!joined) throw new ApiError('ACCESS_DENIED', 'You have to join a group to read messages in it.');
const query = makePaginationQuery(MessagingMessages.createQueryBuilder('message'), ps.sinceId, ps.untilId)
.andWhere('message.groupId = :groupId', { groupId: recipientGroup.id });

View file

@ -106,7 +106,7 @@ export default define(meta, paramDef, async (ps, user) => {
});
// Check blocking
const block = await Blockings.findOneBy({
const block = await Blockings.countBy({
blockerId: recipientUser.id,
blockeeId: user.id,
});
@ -118,12 +118,12 @@ export default define(meta, paramDef, async (ps, user) => {
if (recipientGroup == null) throw new ApiError('NO_SUCH_GROUP');
// check joined
const joining = await UserGroupJoinings.findOneBy({
const joined = await UserGroupJoinings.countBy({
userId: user.id,
userGroupId: recipientGroup.id,
});
if (joining == null) throw new ApiError('ACCESS_DENIED', 'You have to join a group to send a message in it.');
if (!joined) throw new ApiError('ACCESS_DENIED', 'You have to join a group to send a message in it.');
}
let file = null;

View file

@ -169,11 +169,11 @@ export default define(meta, paramDef, async (ps, user) => {
// Check blocking
if (renote.userId !== user.id) {
const block = await Blockings.findOneBy({
const blocked = await Blockings.countBy({
blockerId: renote.userId,
blockeeId: user.id,
});
if (block) throw new ApiError('BLOCKED', 'Blocked by author of note to be renoted.');
if (blocked) throw new ApiError('BLOCKED', 'Blocked by author of note to be renoted.');
}
}
@ -194,11 +194,11 @@ export default define(meta, paramDef, async (ps, user) => {
// Check blocking
if (reply.userId !== user.id) {
const block = await Blockings.findOneBy({
const blocked = await Blockings.countBy({
blockerId: reply.userId,
blockeeId: user.id,
});
if (block) throw new ApiError('BLOCKED', 'Blocked by author of replied to note.');
if (blocked) throw new ApiError('BLOCKED', 'Blocked by author of replied to note.');
}
}

View file

@ -47,11 +47,11 @@ export default define(meta, paramDef, async (ps, user) => {
// Check blocking
if (note.userId !== user.id) {
const block = await Blockings.findOneBy({
const blocked = await Blockings.countBy({
blockerId: note.userId,
blockeeId: user.id,
});
if (block) throw new ApiError('BLOCKED');
if (blocked) throw new ApiError('BLOCKED');
}
const poll = await Polls.findOneByOrFail({ noteId: note.id });
@ -99,7 +99,7 @@ export default define(meta, paramDef, async (ps, user) => {
});
// check if this thread and notification type is muted
const threadMuted = await NoteThreadMutings.findOneBy({
const threadMuted = await NoteThreadMutings.countBy({
userId: note.userId,
threadId: note.threadId || note.id,
mutingNotificationTypes: ArrayOverlap(['pollVote']),

View file

@ -71,11 +71,11 @@ export default define(meta, paramDef, async (ps, me) => {
if (me == null) {
throw new ApiError('ACCESS_DENIED');
} else if (me.id !== user.id) {
const following = await Followings.findOneBy({
const following = await Followings.countBy({
followeeId: user.id,
followerId: me.id,
});
if (following == null) {
if (!following) {
throw new ApiError('ACCESS_DENIED');
}
}

View file

@ -71,11 +71,11 @@ export default define(meta, paramDef, async (ps, me) => {
if (me == null) {
throw new ApiError('ACCESS_DENIED');
} else if (me.id !== user.id) {
const following = await Followings.findOneBy({
const following = await Followings.countBy({
followeeId: user.id,
followerId: me.id,
});
if (following == null) {
if (!following) {
throw new ApiError('ACCESS_DENIED');
}
}

View file

@ -43,14 +43,14 @@ export default define(meta, paramDef, async (ps, me) => {
throw e;
});
const joining = await UserGroupJoinings.findOneBy({
const joined = await UserGroupJoinings.countBy({
userGroupId: userGroup.id,
userId: user.id,
});
if (joining) throw new ApiError('ALREADY_ADDED');
if (joined) throw new ApiError('ALREADY_ADDED');
const existInvitation = await UserGroupInvitations.findOneBy({
const existInvitation = await UserGroupInvitations.countBy({
userGroupId: userGroup.id,
userId: user.id,
});

View file

@ -37,12 +37,12 @@ export default define(meta, paramDef, async (ps, me) => {
if (userGroup == null) throw new ApiError('NO_SUCH_GROUP');
const joining = await UserGroupJoinings.findOneBy({
const joined = await UserGroupJoinings.countBy({
userId: me.id,
userGroupId: userGroup.id,
});
if (joining == null && userGroup.userId !== me.id) {
if (!joined && userGroup.userId !== me.id) {
throw new ApiError('NO_SUCH_GROUP');
}

View file

@ -46,12 +46,12 @@ export default define(meta, paramDef, async (ps, me) => {
throw e;
});
const joining = await UserGroupJoinings.findOneBy({
const joined = await UserGroupJoinings.countBy({
userGroupId: userGroup.id,
userId: user.id,
});
if (joining == null) throw new ApiError('NO_SUCH_USER', 'The user exists but is not a member of the group.');
if (!joined) throw new ApiError('NO_SUCH_USER', 'The user exists but is not a member of the group.');
await UserGroups.update(userGroup.id, {
userId: ps.userId,

View file

@ -43,14 +43,14 @@ export default define(meta, paramDef, async (ps, me) => {
// Check blocking
if (user.id !== me.id) {
const block = await Blockings.findOneBy({
const blocked = await Blockings.countBy({
blockerId: user.id,
blockeeId: me.id,
});
if (block) throw new ApiError('BLOCKED');
if (blocked) throw new ApiError('BLOCKED');
}
const exist = await UserListJoinings.findOneBy({
const exist = await UserListJoinings.countBy({
userListId: userList.id,
userId: user.id,
});

View file

@ -31,12 +31,12 @@ export default class extends Channel {
// Check joining
if (this.groupId) {
const joining = await UserGroupJoinings.findOneBy({
const joined = await UserGroupJoinings.countBy({
userId: this.user!.id,
userGroupId: this.groupId,
});
if (joining == null) {
if (!joined) {
return;
}
}

View file

@ -24,11 +24,11 @@ export default class extends Channel {
this.listId = params.listId as string;
// Check existence and owner
const list = await UserLists.findOneBy({
const exists = await UserLists.countBy({
id: this.listId,
userId: this.user!.id,
});
if (!list) return;
if (!exists) return;
// Subscribe stream
this.subscriber.on(`userListStream:${this.listId}`, this.send);

View file

@ -22,7 +22,7 @@ export async function createSystemUser(username: string): Promise<User> {
// Start transaction
await db.transaction(async transactionalEntityManager => {
const exist = await transactionalEntityManager.findOneBy(User, {
const exist = await transactionalEntityManager.countBy(User, {
usernameLower: username.toLowerCase(),
host: IsNull(),
});

View file

@ -46,12 +46,12 @@ export async function insertFollowingDoc(followee: { id: User['id']; host: User[
}
});
const req = await FollowRequests.findOneBy({
const requested = await FollowRequests.countBy({
followeeId: followee.id,
followerId: follower.id,
});
if (req) {
if (requested) {
await FollowRequests.delete({
followeeId: followee.id,
followerId: follower.id,
@ -167,7 +167,7 @@ export default async function(_follower: { id: User['id'] }, _followee: { id: Us
let autoAccept = false;
// 鍵アカウントであっても、既にフォローされていた場合はスルー
const following = await Followings.findOneBy({
const following = await Followings.countBy({
followerId: follower.id,
followeeId: followee.id,
});
@ -177,7 +177,7 @@ export default async function(_follower: { id: User['id'] }, _followee: { id: Us
// フォローしているユーザーは自動承認オプション
if (!autoAccept && (Users.isLocalUser(followee) && followeeProfile.autoAcceptFollowed)) {
const followed = await Followings.findOneBy({
const followed = await Followings.countBy({
followerId: followee.id,
followeeId: follower.id,
});

View file

@ -16,17 +16,17 @@ export async function cancelFollowRequest(followee: User, follower: User): Promi
if (Users.isRemoteUser(followee)) {
const content = renderActivity(renderUndo(renderFollow(follower, followee), follower));
if (Users.isLocalUser(follower)) { // 本来このチェックは不要だけどTSに怒られるので
if (Users.isLocalUser(follower)) {
deliver(follower, content, followee.inbox);
}
}
const request = await FollowRequests.findOneBy({
const requested = await FollowRequests.countBy({
followeeId: followee.id,
followerId: follower.id,
});
if (request == null) {
if (!requested) {
throw new IdentifiableError('17447091-ce07-46dd-b331-c1fd4f15b1e7', 'request not found');
}

View file

@ -91,12 +91,12 @@ class NotificationManager {
public async deliver(): Promise<void> {
for (const x of this.queue) {
// check if the sender or thread are muted
const userMuted = await Mutings.findOneBy({
const userMuted = await Mutings.countBy({
muterId: x.target,
muteeId: this.notifier.id,
});
const threadMuted = await NoteThreadMutings.findOneBy({
const threadMuted = await NoteThreadMutings.countBy({
userId: x.target,
threadId: In([
// replies
@ -376,7 +376,7 @@ export default async (user: { id: User['id']; username: User['username']; host:
// 通知
if (data.reply.userHost === null) {
const threadMuted = await NoteThreadMutings.findOneBy({
const threadMuted = await NoteThreadMutings.countBy({
userId: data.reply.userId,
threadId: data.reply.threadId || data.reply.id,
});
@ -623,7 +623,7 @@ async function notifyToWatchersOfReplyee(reply: Note, user: { id: User['id']; },
async function createMentionedEvents(mentionedUsers: MinimumUser[], note: Note, nm: NotificationManager): Promise<void> {
for (const u of mentionedUsers.filter(u => Users.isLocalUser(u))) {
const threadMuted = await NoteThreadMutings.findOneBy({
const threadMuted = await NoteThreadMutings.countBy({
userId: u.id,
threadId: note.threadId || note.id,
});

View file

@ -16,7 +16,7 @@ export async function vote(user: CacheableUser, note: Note, choice: number): Pro
// Check blocking
if (note.userId !== user.id) {
const block = await Blockings.findOneBy({
const block = await Blockings.countBy({
blockerId: note.userId,
blockeeId: user.id,
});
@ -58,7 +58,7 @@ export async function vote(user: CacheableUser, note: Note, choice: number): Pro
});
// check if this thread and notification type is muted
const muted = await NoteThreadMutings.findOneBy({
const muted = await NoteThreadMutings.countBy({
userId: note.userId,
threadId: note.threadId || note.id,
mutingNotificationTypes: ArrayOverlap(['pollVote']),

View file

@ -18,7 +18,7 @@ import { deleteReaction } from './delete.js';
export async function createReaction(user: { id: User['id']; host: User['host']; }, note: Note, reaction?: string): Promise<void> {
// Check blocking
if (note.userId !== user.id) {
const block = await Blockings.findOneBy({
const block = await Blockings.countBy({
blockerId: note.userId,
blockeeId: user.id,
});
@ -99,7 +99,7 @@ export async function createReaction(user: { id: User['id']; host: User['host'];
});
// check if this thread is muted
const threadMuted = await NoteThreadMutings.findOneBy({
const threadMuted = await NoteThreadMutings.countBy({
userId: note.userId,
threadId: note.threadId || note.id,
mutingNotificationTypes: ArrayOverlap(['reaction']),

View file

@ -11,18 +11,19 @@ export async function insertNoteUnread(userId: User['id'], note: Note, params: {
}): Promise<void> {
//#region ミュートしているなら無視
// TODO: 現在の仕様ではChannelにミュートは適用されないのでよしなにケアする
const mute = await Mutings.findBy({
const muted = await Mutings.countBy({
muterId: userId,
muteeId: note.userId,
});
if (mute.map(m => m.muteeId).includes(note.userId)) return;
if (muted) return;
//#endregion
const threadMuted = await NoteThreadMutings.countBy({
// スレッドミュート
const threadMute = await NoteThreadMutings.findOneBy({
userId,
threadId: note.threadId || note.id,
});
if (threadMute) return;
if (threadMuted) return;
const unread = {
id: genId(),
@ -38,9 +39,9 @@ export async function insertNoteUnread(userId: User['id'], note: Note, params: {
// 2秒経っても既読にならなかったら「未読の投稿がありますよ」イベントを発行する
setTimeout(async () => {
const exist = await NoteUnreads.findOneBy({ id: unread.id });
const exist = await NoteUnreads.countBy({ id: unread.id });
if (exist == null) return;
if (!exist) return;
if (params.isMentioned) {
publishMainStream(userId, 'unreadMention', note.id);