Skip to content

Commit

Permalink
No findOne(id) (mei23#2524)
Browse files Browse the repository at this point in the history
* No findOne(id)

* fiz
  • Loading branch information
mei23 authored Mar 31, 2024
1 parent efb381c commit 01fe3bc
Show file tree
Hide file tree
Showing 109 changed files with 143 additions and 143 deletions.
6 changes: 3 additions & 3 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ MongoDBは`null`で返してきてたので、その感覚で`if (x === null)`
でもいちいち複数行を費やして、発生するはずのない`undefined`をチェックするのも面倒なので、`ensure`というユーティリティ関数を用意しています。
例えば、
``` ts
const user = await Users.findOne(userId);
const user = await Users.findOne({ id: userId });
// この時点で user の型は User | undefined
if (user == null) {
throw 'missing user';
Expand All @@ -223,13 +223,13 @@ if (user == null) {
```
という処理を`ensure`を使うと
``` ts
const user = await Users.findOne(userId).then(ensure);
const user = await Users.findOne({ id: userId }).then(ensure);
// この時点で user の型は User
```
という風に書けます。
もちろん`ensure`内部でエラーを握りつぶすようなことはしておらず、万が一`undefined`だった場合はPromiseがRejectされ後続の処理は実行されません。
``` ts
const user = await Users.findOne(userId).then(ensure);
const user = await Users.findOne({ id: userId }).then(ensure);
// 万が一 Users.findOne の結果が undefined だったら、ensure でエラーが発生するので
// この行に到達することは無い
// なので、.then(ensure) は
Expand Down
2 changes: 1 addition & 1 deletion src/models/repositories/abuse-user-report.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export class AbuseUserReportRepository extends Repository<AbuseUserReport> {
public async pack(
src: AbuseUserReport['id'] | AbuseUserReport,
) {
const report = typeof src === 'object' ? src : await this.findOne(src).then(ensure);
const report = typeof src === 'object' ? src : await this.findOne({ id: src }).then(ensure);

return await awaitAll({
id: report.id,
Expand Down
2 changes: 1 addition & 1 deletion src/models/repositories/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export class AppRepository extends Repository<App> {
includeProfileImageIds: false
}, options);

const app = typeof src === 'object' ? src : await this.findOne(src).then(ensure);
const app = typeof src === 'object' ? src : await this.findOne({ id: src }).then(ensure);

return {
id: app.id,
Expand Down
2 changes: 1 addition & 1 deletion src/models/repositories/auth-session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export class AuthSessionRepository extends Repository<AuthSession> {
src: AuthSession['id'] | AuthSession,
me?: any
) {
const session = typeof src === 'object' ? src : await this.findOne(src).then(ensure);
const session = typeof src === 'object' ? src : await this.findOne({ id: src }).then(ensure);

return await awaitAll({
id: session.id,
Expand Down
2 changes: 1 addition & 1 deletion src/models/repositories/blocking.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export class BlockingRepository extends Repository<Blocking> {
src: Blocking['id'] | Blocking,
me?: any
): Promise<PackedBlocking> {
const blocking = typeof src === 'object' ? src : await this.findOne(src).then(ensure);
const blocking = typeof src === 'object' ? src : await this.findOne({ id: src }).then(ensure);

return await awaitAll({
id: blocking.id,
Expand Down
2 changes: 1 addition & 1 deletion src/models/repositories/drive-file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ export class DriveFileRepository extends Repository<DriveFile> {
self: false
}, options);

const file = typeof src === 'object' ? src : await this.findOne(src);
const file = typeof src === 'object' ? src : await this.findOne({ id: src });
if (file == null) return null;

const meta = await fetchMeta();
Expand Down
2 changes: 1 addition & 1 deletion src/models/repositories/drive-folder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export class DriveFolderRepository extends Repository<DriveFolder> {
detail: false
}, options);

const folder = typeof src === 'object' ? src : await this.findOne(src).then(ensure);
const folder = typeof src === 'object' ? src : await this.findOne({ id: src }).then(ensure);

return await awaitAll({
id: folder.id,
Expand Down
2 changes: 1 addition & 1 deletion src/models/repositories/follow-request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export class FollowRequestRepository extends Repository<FollowRequest> {
src: FollowRequest['id'] | FollowRequest,
me?: any
) {
const request = typeof src === 'object' ? src : await this.findOne(src).then(ensure);
const request = typeof src === 'object' ? src : await this.findOne({ id: src }).then(ensure);

return {
id: request.id,
Expand Down
2 changes: 1 addition & 1 deletion src/models/repositories/following.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ export class FollowingRepository extends Repository<Following> {
populateFollower?: boolean;
}
): Promise<PackedFollowing> {
const following = typeof src === 'object' ? src : await this.findOne(src).then(ensure);
const following = typeof src === 'object' ? src : await this.findOne({ id: src }).then(ensure);

if (opts == null) opts = {};

Expand Down
2 changes: 1 addition & 1 deletion src/models/repositories/games/reversi/game.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export class ReversiGameRepository extends Repository<ReversiGame> {
detail: true
}, options);

const game = typeof src === 'object' ? src : await this.findOne(src).then(ensure);
const game = typeof src === 'object' ? src : await this.findOne({ id: src }).then(ensure);
const meId = me ? typeof me === 'string' ? me : me.id : null;

return {
Expand Down
2 changes: 1 addition & 1 deletion src/models/repositories/games/reversi/matching.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export class ReversiMatchingRepository extends Repository<ReversiMatching> {
src: ReversiMatching['id'] | ReversiMatching,
me: any
) {
const matching = typeof src === 'object' ? src : await this.findOne(src).then(ensure);
const matching = typeof src === 'object' ? src : await this.findOne({ id: src }).then(ensure);

return await awaitAll({
id: matching.id,
Expand Down
2 changes: 1 addition & 1 deletion src/models/repositories/messaging-message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export class MessagingMessageRepository extends Repository<MessagingMessage> {
populateGroup: true,
};

const message = typeof src === 'object' ? src : await this.findOne(src).then(ensure);
const message = typeof src === 'object' ? src : await this.findOne({ id: src }).then(ensure);

return {
id: message.id,
Expand Down
2 changes: 1 addition & 1 deletion src/models/repositories/moderation-logs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export class ModerationLogRepository extends Repository<ModerationLog> {
public async pack(
src: ModerationLog['id'] | ModerationLog,
) {
const log = typeof src === 'object' ? src : await this.findOne(src).then(ensure);
const log = typeof src === 'object' ? src : await this.findOne({ id: src }).then(ensure);

return await awaitAll({
id: log.id,
Expand Down
2 changes: 1 addition & 1 deletion src/models/repositories/muting.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export class MutingRepository extends Repository<Muting> {
src: Muting['id'] | Muting,
me?: any
): Promise<PackedMuting> {
const muting = typeof src === 'object' ? src : await this.findOne(src).then(ensure);
const muting = typeof src === 'object' ? src : await this.findOne({ id: src }).then(ensure);

return await awaitAll({
id: muting.id,
Expand Down
2 changes: 1 addition & 1 deletion src/models/repositories/note-favorite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export class NoteFavoriteRepository extends Repository<NoteFavorite> {
src: NoteFavorite['id'] | NoteFavorite,
me?: any
) {
const favorite = typeof src === 'object' ? src : await this.findOne(src).then(ensure);
const favorite = typeof src === 'object' ? src : await this.findOne({ id: src }).then(ensure);

return {
id: favorite.id,
Expand Down
2 changes: 1 addition & 1 deletion src/models/repositories/note-reaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export class NoteReactionRepository extends Repository<NoteReaction> {
src: NoteReaction['id'] | NoteReaction,
me?: any
): Promise<PackedNoteReaction> {
const reaction = typeof src === 'object' ? src : await this.findOne(src).then(ensure);
const reaction = typeof src === 'object' ? src : await this.findOne({ id: src }).then(ensure);

return {
id: reaction.id,
Expand Down
4 changes: 2 additions & 2 deletions src/models/repositories/note.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,11 +94,11 @@ export class NoteRepository extends Repository<Note> {
}, options);

const meId = me ? typeof me === 'string' ? me : me.id : null;
const note = typeof src === 'object' ? src : await this.findOne(src).then(ensure);
const note = typeof src === 'object' ? src : await this.findOne({ id: src }).then(ensure);
const host = note.userHost;

async function populatePoll() {
const poll = await Polls.findOne(note.id).then(ensure);
const poll = await Polls.findOne({ noteId: note.id }).then(ensure);
const choices = poll.choices.map(c => ({
text: c,
votes: poll.votes[poll.choices.indexOf(c)],
Expand Down
2 changes: 1 addition & 1 deletion src/models/repositories/notification.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export class NotificationRepository extends Repository<Notification> {
public async pack(
src: Notification['id'] | Notification,
): Promise<PackedNotification> {
const notification = typeof src === 'object' ? src : await this.findOne(src).then(ensure);
const notification = typeof src === 'object' ? src : await this.findOne({ id: src }).then(ensure);

return await awaitAll({
id: notification.id,
Expand Down
2 changes: 1 addition & 1 deletion src/models/repositories/page-like.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export class PageLikeRepository extends Repository<PageLike> {
src: PageLike['id'] | PageLike,
me?: any
) {
const like = typeof src === 'object' ? src : await this.findOne(src).then(ensure);
const like = typeof src === 'object' ? src : await this.findOne({ id: src }).then(ensure);

return {
id: like.id,
Expand Down
2 changes: 1 addition & 1 deletion src/models/repositories/page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export class PageRepository extends Repository<Page> {
me?: User['id'] | User | null | undefined,
): Promise<PackedPage> {
const meId = me ? typeof me === 'string' ? me : me.id : null;
const page = typeof src === 'object' ? src : await this.findOne(src).then(ensure);
const page = typeof src === 'object' ? src : await this.findOne({ id: src }).then(ensure);

const attachedFiles: Promise<DriveFile | undefined>[] = [];
const collectFile = (xs: any[]) => {
Expand Down
2 changes: 1 addition & 1 deletion src/models/repositories/user-group-invite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export class UserGroupInviteRepository extends Repository<UserGroupInvite> {
public async pack(
src: UserGroupInvite['id'] | UserGroupInvite,
) {
const invite = typeof src === 'object' ? src : await this.findOne(src).then(ensure);
const invite = typeof src === 'object' ? src : await this.findOne({ id: src }).then(ensure);

return {
id: invite.id,
Expand Down
2 changes: 1 addition & 1 deletion src/models/repositories/user-group.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export class UserGroupRepository extends Repository<UserGroup> {
public async pack(
src: UserGroup['id'] | UserGroup,
): Promise<PackedUserGroup> {
const userGroup = typeof src === 'object' ? src : await this.findOne(src).then(ensure);
const userGroup = typeof src === 'object' ? src : await this.findOne({ id: src }).then(ensure);

const users = await UserGroupJoinings.find({
userGroupId: userGroup.id
Expand Down
2 changes: 1 addition & 1 deletion src/models/repositories/user-list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export class UserListRepository extends Repository<UserList> {
public async pack(
src: UserList['id'] | UserList,
): Promise<PackedUserList> {
const userList = typeof src === 'object' ? src : await this.findOne(src).then(ensure);
const userList = typeof src === 'object' ? src : await this.findOne({ id: src }).then(ensure);

const users = await UserListJoinings.find({
userListId: userList.id
Expand Down
6 changes: 3 additions & 3 deletions src/models/repositories/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,8 @@ export class UserRepository extends Repository<User> {

if (typeof src === 'object') {
user = src;
if (src.avatar === undefined && src.avatarId) src.avatar = await DriveFiles.findOne(src.avatarId) || null;
if (src.banner === undefined && src.bannerId) src.banner = await DriveFiles.findOne(src.bannerId) || null;
if (src.avatar === undefined && src.avatarId) src.avatar = await DriveFiles.findOne({ id: src.avatarId }) || null;
if (src.banner === undefined && src.bannerId) src.banner = await DriveFiles.findOne({ id: src.bannerId }) || null;
} else {
user = await this.findOneOrFail(src, {
relations: ['avatar', 'banner']
Expand All @@ -137,7 +137,7 @@ export class UserRepository extends Repository<User> {
where: { userId: user.id },
order: { id: 'DESC' }
}) : [];
const profile = opts.detail ? await UserProfiles.findOne(user.id).then(ensure) : null;
const profile = opts.detail ? await UserProfiles.findOne({ userId: user.id }).then(ensure) : null;

const falsy = opts.detail ? false : undefined;

Expand Down
2 changes: 1 addition & 1 deletion src/queue/processors/db/delete-account.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const logger = queueLogger.createSubLogger('delete-account');
export async function deleteAccount(job: Bull.Job<DbUserJobData>): Promise<string | void> {
logger.info(`Deleting account of ${job.data.user.id} ...`);

const user = await Users.findOne(job.data.user.id);
const user = await Users.findOne({ id: job.data.user.id });
if (user == null) {
return;
}
Expand Down
2 changes: 1 addition & 1 deletion src/queue/processors/db/delete-drive-files.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const logger = queueLogger.createSubLogger('delete-drive-files');
export async function deleteDriveFiles(job: Bull.Job<DbUserJobData>, done: any): Promise<void> {
logger.info(`Deleting drive files of ${job.data.user.id} ...`);

const user = await Users.findOne(job.data.user.id);
const user = await Users.findOne({ id: job.data.user.id });
if (user == null) {
done();
return;
Expand Down
2 changes: 1 addition & 1 deletion src/queue/processors/db/export-blocking.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const logger = queueLogger.createSubLogger('export-blocking');
export async function exportBlocking(job: Bull.Job<DbUserJobData>, done: any): Promise<void> {
logger.info(`Exporting blocking of ${job.data.user.id} ...`);

const user = await Users.findOne(job.data.user.id);
const user = await Users.findOne({ id: job.data.user.id });
if (user == null) {
done();
return;
Expand Down
2 changes: 1 addition & 1 deletion src/queue/processors/db/export-following.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const logger = queueLogger.createSubLogger('export-following');
export async function exportFollowing(job: Bull.Job<DbUserJobData>, done: any): Promise<void> {
logger.info(`Exporting following of ${job.data.user.id} ...`);

const user = await Users.findOne(job.data.user.id);
const user = await Users.findOne({ id: job.data.user.id });
if (user == null) {
done();
return;
Expand Down
2 changes: 1 addition & 1 deletion src/queue/processors/db/export-mute.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const logger = queueLogger.createSubLogger('export-mute');
export async function exportMute(job: Bull.Job<DbUserJobData>, done: any): Promise<void> {
logger.info(`Exporting mute of ${job.data.user.id} ...`);

const user = await Users.findOne(job.data.user.id);
const user = await Users.findOne({ id: job.data.user.id });
if (user == null) {
done();
return;
Expand Down
2 changes: 1 addition & 1 deletion src/queue/processors/db/export-notes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const logger = queueLogger.createSubLogger('export-notes');
export async function exportNotes(job: Bull.Job<DbUserJobData>, done: any): Promise<void> {
logger.info(`Exporting notes of ${job.data.user.id} ...`);

const user = await Users.findOne(job.data.user.id);
const user = await Users.findOne({ id: job.data.user.id });
if (user == null) {
done();
return;
Expand Down
2 changes: 1 addition & 1 deletion src/queue/processors/db/export-user-lists.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const logger = queueLogger.createSubLogger('export-user-lists');
export async function exportUserLists(job: Bull.Job<DbUserJobData>, done: any): Promise<void> {
logger.info(`Exporting user lists of ${job.data.user.id} ...`);

const user = await Users.findOne(job.data.user.id);
const user = await Users.findOne({ id: job.data.user.id });
if (user == null) {
done();
return;
Expand Down
2 changes: 1 addition & 1 deletion src/queue/processors/db/import-blocking.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const logger = queueLogger.createSubLogger('import-blocking');
export async function importBlocking(job: Bull.Job<DbUserImportJobData>, done: any): Promise<void> {
logger.info(`Importing blocking of ${job.data.user.id} ...`);

const user = await Users.findOne(job.data.user.id);
const user = await Users.findOne({ id: job.data.user.id });
if (user == null) {
done();
return;
Expand Down
2 changes: 1 addition & 1 deletion src/queue/processors/db/import-following.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const logger = queueLogger.createSubLogger('import-following');
export async function importFollowing(job: Bull.Job<DbUserImportJobData>, done: any): Promise<void> {
logger.info(`Importing following of ${job.data.user.id} ...`);

const user = await Users.findOne(job.data.user.id);
const user = await Users.findOne({ id: job.data.user.id });
if (user == null) {
done();
return;
Expand Down
2 changes: 1 addition & 1 deletion src/queue/processors/db/import-user-lists.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const logger = queueLogger.createSubLogger('import-user-lists');
export async function importUserLists(job: Bull.Job<DbUserImportJobData>, done: any): Promise<void> {
logger.info(`Importing user lists of ${job.data.user.id} ...`);

const user = await Users.findOne(job.data.user.id);
const user = await Users.findOne({ id: job.data.user.id });
if (user == null) {
done();
return;
Expand Down
4 changes: 2 additions & 2 deletions src/remote/activitypub/db-resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ export default class DbResolver {

if (key == null) return null;

const user = await Users.findOne(key.userId) as IRemoteUser;
const user = await Users.findOne({ id: key.userId }) as IRemoteUser;

return {
user,
Expand All @@ -98,7 +98,7 @@ export default class DbResolver {

if (user == null) return null;

const key = await UserPublickeys.findOne(user.id);
const key = await UserPublickeys.findOne({ userId: user.id });

return {
user,
Expand Down
2 changes: 1 addition & 1 deletion src/remote/activitypub/kernel/read.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export const performReadActivity = async (actor: IRemoteUser, activity: IRead):

const messageId = id.split('/').pop();

const message = await MessagingMessages.findOne(messageId);
const message = await MessagingMessages.findOne({ id: messageId });
if (message == null) {
return `skip: message not found`;
}
Expand Down
2 changes: 1 addition & 1 deletion src/remote/activitypub/models/image.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export async function createImage(actor: IRemoteUser, value: any): Promise<Drive
uri: image.url
});

file = await DriveFiles.findOne(file.id).then(ensure);
file = await DriveFiles.findOne({ id: file.id }).then(ensure);
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/remote/activitypub/models/note.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ export async function createNote(value: string | IObject, resolver?: Resolver, s
const uri = getApId(note.inReplyTo);
if (isSelfOrigin(uri)) {
const id = uri.split('/').pop();
const talk = await MessagingMessages.findOne(id);
const talk = await MessagingMessages.findOne({ id: id });
if (talk) {
isTalk = true;
return null;
Expand Down Expand Up @@ -202,7 +202,7 @@ export async function createNote(value: string | IObject, resolver?: Resolver, s

// vote
if (reply && reply.hasPoll) {
const poll = await Polls.findOne(reply.id).then(ensure);
const poll = await Polls.findOne({ noteId: reply.id }).then(ensure);

const tryCreateVote = async (name: string, index: number): Promise<null> => {
if (poll.expiresAt && Date.now() > new Date(poll.expiresAt).getTime()) {
Expand Down
Loading

0 comments on commit 01fe3bc

Please sign in to comment.