Skip to content

Commit

Permalink
Revert "fix(backend): use insertOne insteadof insert/findOneOrFail co…
Browse files Browse the repository at this point in the history
…mbination (misskey-dev#13908)"

This reverts commit 2b8056a.
  • Loading branch information
kanarikanaru committed Jun 13, 2024
1 parent 61f0d5b commit 1265636
Show file tree
Hide file tree
Showing 36 changed files with 215 additions and 319 deletions.
4 changes: 2 additions & 2 deletions packages/backend/src/core/AnnouncementService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ export class AnnouncementService {

@bindThis
public async create(values: Partial<MiAnnouncement>, moderator?: MiUser): Promise<{ raw: MiAnnouncement; packed: Packed<'Announcement'> }> {
const announcement = await this.announcementsRepository.insertOne({
const announcement = await this.announcementsRepository.insert({
id: this.idService.gen(),
updatedAt: null,
title: values.title,
Expand All @@ -79,7 +79,7 @@ export class AnnouncementService {
silence: values.silence,
needConfirmationToRead: values.needConfirmationToRead,
userId: values.userId,
});
}).then(x => this.announcementsRepository.findOneByOrFail(x.identifiers[0]));

const packed = await this.announcementEntityService.pack(announcement);

Expand Down
4 changes: 2 additions & 2 deletions packages/backend/src/core/AvatarDecorationService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,10 @@ export class AvatarDecorationService implements OnApplicationShutdown {

@bindThis
public async create(options: Partial<MiAvatarDecoration>, moderator?: MiUser): Promise<MiAvatarDecoration> {
const created = await this.avatarDecorationsRepository.insertOne({
const created = await this.avatarDecorationsRepository.insert({
id: this.idService.gen(),
...options,
});
}).then(x => this.avatarDecorationsRepository.findOneByOrFail(x.identifiers[0]));

this.globalEventService.publishInternalEvent('avatarDecorationCreated', created);

Expand Down
4 changes: 2 additions & 2 deletions packages/backend/src/core/ClipService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,13 @@ export class ClipService {
throw new ClipService.TooManyClipsError();
}

const clip = await this.clipsRepository.insertOne({
const clip = await this.clipsRepository.insert({
id: this.idService.gen(),
userId: me.id,
name: name,
isPublic: isPublic,
description: description,
});
}).then(x => this.clipsRepository.findOneByOrFail(x.identifiers[0]));

return clip;
}
Expand Down
4 changes: 2 additions & 2 deletions packages/backend/src/core/CustomEmojiService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ export class CustomEmojiService implements OnApplicationShutdown {
localOnly: boolean;
roleIdsThatCanBeUsedThisEmojiAsReaction: MiRole['id'][];
}, moderator?: MiUser): Promise<MiEmoji> {
const emoji = await this.emojisRepository.insertOne({
const emoji = await this.emojisRepository.insert({
id: this.idService.gen(),
updatedAt: new Date(),
name: data.name,
Expand All @@ -82,7 +82,7 @@ export class CustomEmojiService implements OnApplicationShutdown {
isSensitive: data.isSensitive,
localOnly: data.localOnly,
roleIdsThatCanBeUsedThisEmojiAsReaction: data.roleIdsThatCanBeUsedThisEmojiAsReaction,
});
}).then(x => this.emojisRepository.findOneByOrFail(x.identifiers[0]));

if (data.host == null) {
this.localEmojisCache.refresh();
Expand Down
6 changes: 3 additions & 3 deletions packages/backend/src/core/DriveService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ export class DriveService {
file.size = size;
file.storedInternal = false;

return await this.driveFilesRepository.insertOne(file);
return await this.driveFilesRepository.insert(file).then(x => this.driveFilesRepository.findOneByOrFail(x.identifiers[0]));
} else { // use internal storage
const accessKey = randomUUID();
const thumbnailAccessKey = 'thumbnail-' + randomUUID();
Expand Down Expand Up @@ -254,7 +254,7 @@ export class DriveService {
file.md5 = hash;
file.size = size;

return await this.driveFilesRepository.insertOne(file);
return await this.driveFilesRepository.insert(file).then(x => this.driveFilesRepository.findOneByOrFail(x.identifiers[0]));
}
}

Expand Down Expand Up @@ -615,7 +615,7 @@ export class DriveService {
file.type = info.type.mime;
file.storedInternal = false;

file = await this.driveFilesRepository.insertOne(file);
file = await this.driveFilesRepository.insert(file).then(x => this.driveFilesRepository.findOneByOrFail(x.identifiers[0]));
} catch (err) {
// duplicate key error (when already registered)
if (isDuplicateKeyValueError(err)) {
Expand Down
4 changes: 2 additions & 2 deletions packages/backend/src/core/FederatedInstanceService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,11 @@ export class FederatedInstanceService implements OnApplicationShutdown {
const index = await this.instancesRepository.findOneBy({ host });

if (index == null) {
const i = await this.instancesRepository.insertOne({
const i = await this.instancesRepository.insert({
id: this.idService.gen(),
host,
firstRetrievedAt: new Date(),
});
}).then(x => this.instancesRepository.findOneByOrFail(x.identifiers[0]));

this.federatedInstanceCache.set(host, i);
return i;
Expand Down
4 changes: 2 additions & 2 deletions packages/backend/src/core/RelayService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,11 @@ export class RelayService {

@bindThis
public async addRelay(inbox: string): Promise<MiRelay> {
const relay = await this.relaysRepository.insertOne({
const relay = await this.relaysRepository.insert({
id: this.idService.gen(),
inbox,
status: 'requesting',
});
}).then(x => this.relaysRepository.findOneByOrFail(x.identifiers[0]));

const relayActor = await this.getRelayActor();
const follow = await this.apRendererService.renderFollowRelay(relay, relayActor);
Expand Down
7 changes: 5 additions & 2 deletions packages/backend/src/core/ReversiService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ export class ReversiService implements OnApplicationShutdown, OnModuleInit {

@bindThis
private async matched(parentId: MiUser['id'], childId: MiUser['id'], options: { noIrregularRules: boolean; }): Promise<MiReversiGame> {
const game = await this.reversiGamesRepository.insertOne({
const game = await this.reversiGamesRepository.insert({
id: this.idService.gen(),
user1Id: parentId,
user2Id: childId,
Expand All @@ -294,7 +294,10 @@ export class ReversiService implements OnApplicationShutdown, OnModuleInit {
bw: 'random',
isLlotheo: false,
noIrregularRules: options.noIrregularRules,
}, { relations: ['user1', 'user2'] });
}).then(x => this.reversiGamesRepository.findOneOrFail({
where: { id: x.identifiers[0].id },
relations: ['user1', 'user2'],
}));
this.cacheGame(game);

const packed = await this.reversiGameEntityService.packDetail(game);
Expand Down
8 changes: 4 additions & 4 deletions packages/backend/src/core/RoleService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -471,12 +471,12 @@ export class RoleService implements OnApplicationShutdown, OnModuleInit {
}
}

const created = await this.roleAssignmentsRepository.insertOne({
const created = await this.roleAssignmentsRepository.insert({
id: this.idService.gen(now),
expiresAt: expiresAt,
roleId: roleId,
userId: userId,
});
}).then(x => this.roleAssignmentsRepository.findOneByOrFail(x.identifiers[0]));

this.rolesRepository.update(roleId, {
lastUsedAt: new Date(),
Expand Down Expand Up @@ -558,7 +558,7 @@ export class RoleService implements OnApplicationShutdown, OnModuleInit {
@bindThis
public async create(values: Partial<MiRole>, moderator?: MiUser): Promise<MiRole> {
const date = new Date();
const created = await this.rolesRepository.insertOne({
const created = await this.rolesRepository.insert({
id: this.idService.gen(date.getTime()),
updatedAt: date,
lastUsedAt: date,
Expand All @@ -576,7 +576,7 @@ export class RoleService implements OnApplicationShutdown, OnModuleInit {
canEditMembersByModerator: values.canEditMembersByModerator,
displayOrder: values.displayOrder,
policies: values.policies,
});
}).then(x => this.rolesRepository.findOneByOrFail(x.identifiers[0]));

this.globalEventService.publishInternalEvent('roleCreated', created);

Expand Down
4 changes: 2 additions & 2 deletions packages/backend/src/core/UserFollowingService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -517,7 +517,7 @@ export class UserFollowingService implements OnModuleInit {
followerId: follower.id,
});

const followRequest = await this.followRequestsRepository.insertOne({
const followRequest = await this.followRequestsRepository.insert({
id: this.idService.gen(),
followerId: follower.id,
followeeId: followee.id,
Expand All @@ -531,7 +531,7 @@ export class UserFollowingService implements OnModuleInit {
followeeHost: followee.host,
followeeInbox: this.userEntityService.isRemoteUser(followee) ? followee.inbox : undefined,
followeeSharedInbox: this.userEntityService.isRemoteUser(followee) ? followee.sharedInbox : undefined,
});
}).then(x => this.followRequestsRepository.findOneByOrFail(x.identifiers[0]));

// Publish receiveRequest event
if (this.userEntityService.isLocalUser(followee)) {
Expand Down
4 changes: 2 additions & 2 deletions packages/backend/src/core/activitypub/models/ApNoteService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -407,7 +407,7 @@ export class ApNoteService {

this.logger.info(`register emoji host=${host}, name=${name}`);

return await this.emojisRepository.insertOne({
return await this.emojisRepository.insert({
id: this.idService.gen(),
host,
name,
Expand All @@ -416,7 +416,7 @@ export class ApNoteService {
publicUrl: tag.icon.url,
updatedAt: new Date(),
aliases: [],
});
}).then(x => this.emojisRepository.findOneByOrFail(x.identifiers[0]));
}));
}
}
23 changes: 9 additions & 14 deletions packages/backend/src/core/chart/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@ import { EntitySchema, LessThan, Between } from 'typeorm';
import { dateUTC, isTimeSame, isTimeBefore, subtractTime, addTime } from '@/misc/prelude/time.js';
import type Logger from '@/logger.js';
import { bindThis } from '@/decorators.js';
import { MiRepository, miRepository } from '@/models/_.js';
import type { DataSource, Repository } from 'typeorm';
import type { Repository, DataSource } from 'typeorm';

const COLUMN_PREFIX = '___' as const;
const UNIQUE_TEMP_COLUMN_PREFIX = 'unique_temp___' as const;
Expand Down Expand Up @@ -146,10 +145,10 @@ export default abstract class Chart<T extends Schema> {
group: string | null;
}[] = [];
// ↓にしたいけどfindOneとかで型エラーになる
//private repositoryForHour: Repository<RawRecord<T>> & MiRepository<RawRecord<T>>;
//private repositoryForDay: Repository<RawRecord<T>> & MiRepository<RawRecord<T>>;
private repositoryForHour: Repository<{ id: number; group?: string | null; date: number; }> & MiRepository<{ id: number; group?: string | null; date: number; }>;
private repositoryForDay: Repository<{ id: number; group?: string | null; date: number; }> & MiRepository<{ id: number; group?: string | null; date: number; }>;
//private repositoryForHour: Repository<RawRecord<T>>;
//private repositoryForDay: Repository<RawRecord<T>>;
private repositoryForHour: Repository<{ id: number; group?: string | null; date: number; }>;
private repositoryForDay: Repository<{ id: number; group?: string | null; date: number; }>;

/**
* 1日に一回程度実行されれば良いような計算処理を入れる(主にCASCADE削除などアプリケーション側で感知できない変動によるズレの修正用)
Expand Down Expand Up @@ -212,10 +211,6 @@ export default abstract class Chart<T extends Schema> {
} {
const createEntity = (span: 'hour' | 'day'): EntitySchema => new EntitySchema({
name:
span === 'hour' ? `ChartX${name}` :
span === 'day' ? `ChartDayX${name}` :
new Error('not happen') as never,
tableName:
span === 'hour' ? `__chart__${camelToSnake(name)}` :
span === 'day' ? `__chart_day__${camelToSnake(name)}` :
new Error('not happen') as never,
Expand Down Expand Up @@ -276,8 +271,8 @@ export default abstract class Chart<T extends Schema> {
this.logger = logger;

const { hour, day } = Chart.schemaToEntity(name, schema, grouped);
this.repositoryForHour = db.getRepository<{ id: number; group?: string | null; date: number; }>(hour).extend(miRepository as MiRepository<{ id: number; group?: string | null; date: number; }>);
this.repositoryForDay = db.getRepository<{ id: number; group?: string | null; date: number; }>(day).extend(miRepository as MiRepository<{ id: number; group?: string | null; date: number; }>);
this.repositoryForHour = db.getRepository<{ id: number; group?: string | null; date: number; }>(hour);
this.repositoryForDay = db.getRepository<{ id: number; group?: string | null; date: number; }>(day);
}

@bindThis
Expand Down Expand Up @@ -392,11 +387,11 @@ export default abstract class Chart<T extends Schema> {
}

// 新規ログ挿入
log = await repository.insertOne({
log = await repository.insert({
date: date,
...(group ? { group: group } : {}),
...columns,
}) as RawRecord<T>;
}).then(x => repository.findOneByOrFail(x.identifiers[0])) as RawRecord<T>;

this.logger.info(`${this.name + (group ? `:${group}` : '')}(${span}): New commit created`);

Expand Down
Loading

0 comments on commit 1265636

Please sign in to comment.