diff options
| author | syuilo <Syuilotan@yahoo.co.jp> | 2021-03-24 11:05:37 +0900 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-03-24 11:05:37 +0900 |
| commit | ce340aba7a37394c70b9f3d7cece9cfa5e91d94c (patch) | |
| tree | 99612ea0d039f20e0baa9ca243e8cec0af96b11a /src/models | |
| parent | fix bug (diff) | |
| download | sharkey-ce340aba7a37394c70b9f3d7cece9cfa5e91d94c.tar.gz sharkey-ce340aba7a37394c70b9f3d7cece9cfa5e91d94c.tar.bz2 sharkey-ce340aba7a37394c70b9f3d7cece9cfa5e91d94c.zip | |
Refactor (#7394)
* wip
* wip
* wip
* wip
* wip
* Update define.ts
* Update update.ts
* Update user.ts
* wip
* wip
* Update request.ts
* URL
* wip
* wip
* wip
* wip
* Update invite.ts
* Update create.ts
Diffstat (limited to 'src/models')
| -rw-r--r-- | src/models/repositories/app.ts | 2 | ||||
| -rw-r--r-- | src/models/repositories/auth-session.ts | 3 | ||||
| -rw-r--r-- | src/models/repositories/blocking.ts | 5 | ||||
| -rw-r--r-- | src/models/repositories/channel.ts | 10 | ||||
| -rw-r--r-- | src/models/repositories/drive-file.ts | 2 | ||||
| -rw-r--r-- | src/models/repositories/follow-request.ts | 3 | ||||
| -rw-r--r-- | src/models/repositories/following.ts | 5 | ||||
| -rw-r--r-- | src/models/repositories/games/reversi/game.ts | 10 | ||||
| -rw-r--r-- | src/models/repositories/games/reversi/matching.ts | 3 | ||||
| -rw-r--r-- | src/models/repositories/messaging-message.ts | 3 | ||||
| -rw-r--r-- | src/models/repositories/muting.ts | 5 | ||||
| -rw-r--r-- | src/models/repositories/note-favorite.ts | 5 | ||||
| -rw-r--r-- | src/models/repositories/note-reaction.ts | 3 | ||||
| -rw-r--r-- | src/models/repositories/note.ts | 14 | ||||
| -rw-r--r-- | src/models/repositories/notification.ts | 12 | ||||
| -rw-r--r-- | src/models/repositories/page-like.ts | 5 | ||||
| -rw-r--r-- | src/models/repositories/page.ts | 6 | ||||
| -rw-r--r-- | src/models/repositories/user.ts | 18 |
18 files changed, 64 insertions, 50 deletions
diff --git a/src/models/repositories/app.ts b/src/models/repositories/app.ts index 12c78fe87a..c919a9bdb1 100644 --- a/src/models/repositories/app.ts +++ b/src/models/repositories/app.ts @@ -9,7 +9,7 @@ export type PackedApp = SchemaType<typeof packedAppSchema>; export class AppRepository extends Repository<App> { public async pack( src: App['id'] | App, - me?: any, + me?: { id: User['id'] } | null | undefined, options?: { detail?: boolean, includeSecret?: boolean, diff --git a/src/models/repositories/auth-session.ts b/src/models/repositories/auth-session.ts index e985d6925f..f513357e1b 100644 --- a/src/models/repositories/auth-session.ts +++ b/src/models/repositories/auth-session.ts @@ -2,12 +2,13 @@ import { EntityRepository, Repository } from 'typeorm'; import { Apps } from '..'; import { AuthSession } from '../entities/auth-session'; import { awaitAll } from '../../prelude/await-all'; +import { User } from '../entities/user'; @EntityRepository(AuthSession) export class AuthSessionRepository extends Repository<AuthSession> { public async pack( src: AuthSession['id'] | AuthSession, - me?: any + me?: { id: User['id'] } | null | undefined ) { const session = typeof src === 'object' ? src : await this.findOneOrFail(src); diff --git a/src/models/repositories/blocking.ts b/src/models/repositories/blocking.ts index 1ecdbd5d05..60b43fae3e 100644 --- a/src/models/repositories/blocking.ts +++ b/src/models/repositories/blocking.ts @@ -3,6 +3,7 @@ import { Users } from '..'; import { Blocking } from '../entities/blocking'; import { awaitAll } from '../../prelude/await-all'; import { SchemaType } from '@/misc/schema'; +import { User } from '../entities/user'; export type PackedBlocking = SchemaType<typeof packedBlockingSchema>; @@ -10,7 +11,7 @@ export type PackedBlocking = SchemaType<typeof packedBlockingSchema>; export class BlockingRepository extends Repository<Blocking> { public async pack( src: Blocking['id'] | Blocking, - me?: any + me?: { id: User['id'] } | null | undefined ): Promise<PackedBlocking> { const blocking = typeof src === 'object' ? src : await this.findOneOrFail(src); @@ -26,7 +27,7 @@ export class BlockingRepository extends Repository<Blocking> { public packMany( blockings: any[], - me: any + me: { id: User['id'] } ) { return Promise.all(blockings.map(x => this.pack(x, me))); } diff --git a/src/models/repositories/channel.ts b/src/models/repositories/channel.ts index b15cf8b35f..a1c85f2bd7 100644 --- a/src/models/repositories/channel.ts +++ b/src/models/repositories/channel.ts @@ -10,19 +10,19 @@ export type PackedChannel = SchemaType<typeof packedChannelSchema>; export class ChannelRepository extends Repository<Channel> { public async pack( src: Channel['id'] | Channel, - me?: User['id'] | User | null | undefined, + me?: { id: User['id'] } | null | undefined, ): Promise<PackedChannel> { const channel = typeof src === 'object' ? src : await this.findOneOrFail(src); - const meId = me ? typeof me === 'string' ? me : me.id : null; + const meId = me ? me.id : null; const banner = channel.bannerId ? await DriveFiles.findOne(channel.bannerId) : null; - const hasUnreadNote = me ? (await NoteUnreads.findOne({ noteChannelId: channel.id, userId: meId })) != null : undefined; + const hasUnreadNote = meId ? (await NoteUnreads.findOne({ noteChannelId: channel.id, userId: meId })) != null : undefined; - const following = await ChannelFollowings.findOne({ + const following = meId ? await ChannelFollowings.findOne({ followerId: meId, followeeId: channel.id, - }); + }) : null; return { id: channel.id, diff --git a/src/models/repositories/drive-file.ts b/src/models/repositories/drive-file.ts index 3a036beb88..590079fe4a 100644 --- a/src/models/repositories/drive-file.ts +++ b/src/models/repositories/drive-file.ts @@ -53,7 +53,7 @@ export class DriveFileRepository extends Repository<DriveFile> { return thumbnail ? (file.thumbnailUrl || (isImage ? (file.webpublicUrl || file.url) : null)) : (file.webpublicUrl || file.url); } - public async calcDriveUsageOf(user: User['id'] | User): Promise<number> { + public async calcDriveUsageOf(user: User['id'] | { id: User['id'] }): Promise<number> { const id = typeof user === 'object' ? user.id : user; const { sum } = await this diff --git a/src/models/repositories/follow-request.ts b/src/models/repositories/follow-request.ts index 0d96b8eb53..31e5fb2d90 100644 --- a/src/models/repositories/follow-request.ts +++ b/src/models/repositories/follow-request.ts @@ -1,12 +1,13 @@ import { EntityRepository, Repository } from 'typeorm'; import { FollowRequest } from '../entities/follow-request'; import { Users } from '..'; +import { User } from '../entities/user'; @EntityRepository(FollowRequest) export class FollowRequestRepository extends Repository<FollowRequest> { public async pack( src: FollowRequest['id'] | FollowRequest, - me?: any + me?: { id: User['id'] } | null | undefined ) { const request = typeof src === 'object' ? src : await this.findOneOrFail(src); diff --git a/src/models/repositories/following.ts b/src/models/repositories/following.ts index d754661b2d..b886432978 100644 --- a/src/models/repositories/following.ts +++ b/src/models/repositories/following.ts @@ -3,6 +3,7 @@ import { Users } from '..'; import { Following } from '../entities/following'; import { awaitAll } from '../../prelude/await-all'; import { SchemaType } from '@/misc/schema'; +import { User } from '../entities/user'; type LocalFollowerFollowing = Following & { followerHost: null; @@ -50,7 +51,7 @@ export class FollowingRepository extends Repository<Following> { public async pack( src: Following['id'] | Following, - me?: any, + me?: { id: User['id'] } | null | undefined, opts?: { populateFollowee?: boolean; populateFollower?: boolean; @@ -76,7 +77,7 @@ export class FollowingRepository extends Repository<Following> { public packMany( followings: any[], - me?: any, + me?: { id: User['id'] } | null | undefined, opts?: { populateFollowee?: boolean; populateFollower?: boolean; diff --git a/src/models/repositories/games/reversi/game.ts b/src/models/repositories/games/reversi/game.ts index e23247f664..344cf7b20e 100644 --- a/src/models/repositories/games/reversi/game.ts +++ b/src/models/repositories/games/reversi/game.ts @@ -1,3 +1,4 @@ +import { User } from '@/models/entities/user'; import { EntityRepository, Repository } from 'typeorm'; import { Users } from '../../..'; import { ReversiGame } from '../../../entities/games/reversi/game'; @@ -6,7 +7,7 @@ import { ReversiGame } from '../../../entities/games/reversi/game'; export class ReversiGameRepository extends Repository<ReversiGame> { public async pack( src: ReversiGame['id'] | ReversiGame, - me?: any, + me?: { id: User['id'] } | null | undefined, options?: { detail?: boolean } @@ -16,7 +17,6 @@ export class ReversiGameRepository extends Repository<ReversiGame> { }, options); const game = typeof src === 'object' ? src : await this.findOneOrFail(src); - const meId = me ? typeof me === 'string' ? me : me.id : null; return { id: game.id, @@ -30,10 +30,10 @@ export class ReversiGameRepository extends Repository<ReversiGame> { user2Accepted: game.user2Accepted, user1Id: game.user1Id, user2Id: game.user2Id, - user1: await Users.pack(game.user1Id, meId), - user2: await Users.pack(game.user2Id, meId), + user1: await Users.pack(game.user1Id, me), + user2: await Users.pack(game.user2Id, me), winnerId: game.winnerId, - winner: game.winnerId ? await Users.pack(game.winnerId, meId) : null, + winner: game.winnerId ? await Users.pack(game.winnerId, me) : null, surrendered: game.surrendered, black: game.black, bw: game.bw, diff --git a/src/models/repositories/games/reversi/matching.ts b/src/models/repositories/games/reversi/matching.ts index 51f17c9a4e..013021eb90 100644 --- a/src/models/repositories/games/reversi/matching.ts +++ b/src/models/repositories/games/reversi/matching.ts @@ -2,12 +2,13 @@ import { EntityRepository, Repository } from 'typeorm'; import { ReversiMatching } from '../../../entities/games/reversi/matching'; import { Users } from '../../..'; import { awaitAll } from '../../../../prelude/await-all'; +import { User } from '@/models/entities/user'; @EntityRepository(ReversiMatching) export class ReversiMatchingRepository extends Repository<ReversiMatching> { public async pack( src: ReversiMatching['id'] | ReversiMatching, - me: any + me: { id: User['id'] } ) { const matching = typeof src === 'object' ? src : await this.findOneOrFail(src); diff --git a/src/models/repositories/messaging-message.ts b/src/models/repositories/messaging-message.ts index 2c78e7fbd3..8d6d03a236 100644 --- a/src/models/repositories/messaging-message.ts +++ b/src/models/repositories/messaging-message.ts @@ -2,6 +2,7 @@ import { EntityRepository, Repository } from 'typeorm'; import { MessagingMessage } from '../entities/messaging-message'; import { Users, DriveFiles, UserGroups } from '..'; import { SchemaType } from '@/misc/schema'; +import { User } from '../entities/user'; export type PackedMessagingMessage = SchemaType<typeof packedMessagingMessageSchema>; @@ -13,7 +14,7 @@ export class MessagingMessageRepository extends Repository<MessagingMessage> { public async pack( src: MessagingMessage['id'] | MessagingMessage, - me?: any, + me?: { id: User['id'] } | null | undefined, options?: { populateRecipient?: boolean, populateGroup?: boolean, diff --git a/src/models/repositories/muting.ts b/src/models/repositories/muting.ts index 8a5c57a38f..b5bbe8a0a3 100644 --- a/src/models/repositories/muting.ts +++ b/src/models/repositories/muting.ts @@ -3,6 +3,7 @@ import { Users } from '..'; import { Muting } from '../entities/muting'; import { awaitAll } from '../../prelude/await-all'; import { SchemaType } from '@/misc/schema'; +import { User } from '../entities/user'; export type PackedMuting = SchemaType<typeof packedMutingSchema>; @@ -10,7 +11,7 @@ export type PackedMuting = SchemaType<typeof packedMutingSchema>; export class MutingRepository extends Repository<Muting> { public async pack( src: Muting['id'] | Muting, - me?: any + me?: { id: User['id'] } | null | undefined ): Promise<PackedMuting> { const muting = typeof src === 'object' ? src : await this.findOneOrFail(src); @@ -26,7 +27,7 @@ export class MutingRepository extends Repository<Muting> { public packMany( mutings: any[], - me: any + me: { id: User['id'] } ) { return Promise.all(mutings.map(x => this.pack(x, me))); } diff --git a/src/models/repositories/note-favorite.ts b/src/models/repositories/note-favorite.ts index eb2ffff4c1..316ebdff35 100644 --- a/src/models/repositories/note-favorite.ts +++ b/src/models/repositories/note-favorite.ts @@ -1,12 +1,13 @@ import { EntityRepository, Repository } from 'typeorm'; import { NoteFavorite } from '../entities/note-favorite'; import { Notes } from '..'; +import { User } from '../entities/user'; @EntityRepository(NoteFavorite) export class NoteFavoriteRepository extends Repository<NoteFavorite> { public async pack( src: NoteFavorite['id'] | NoteFavorite, - me?: any + me?: { id: User['id'] } | null | undefined ) { const favorite = typeof src === 'object' ? src : await this.findOneOrFail(src); @@ -20,7 +21,7 @@ export class NoteFavoriteRepository extends Repository<NoteFavorite> { public packMany( favorites: any[], - me: any + me: { id: User['id'] } ) { return Promise.all(favorites.map(x => this.pack(x, me))); } diff --git a/src/models/repositories/note-reaction.ts b/src/models/repositories/note-reaction.ts index e04a493007..4c56809603 100644 --- a/src/models/repositories/note-reaction.ts +++ b/src/models/repositories/note-reaction.ts @@ -3,6 +3,7 @@ import { NoteReaction } from '../entities/note-reaction'; import { Users } from '..'; import { SchemaType } from '@/misc/schema'; import { convertLegacyReaction } from '@/misc/reaction-lib'; +import { User } from '../entities/user'; export type PackedNoteReaction = SchemaType<typeof packedNoteReactionSchema>; @@ -10,7 +11,7 @@ export type PackedNoteReaction = SchemaType<typeof packedNoteReactionSchema>; export class NoteReactionRepository extends Repository<NoteReaction> { public async pack( src: NoteReaction['id'] | NoteReaction, - me?: any + me?: { id: User['id'] } | null | undefined ): Promise<PackedNoteReaction> { const reaction = typeof src === 'object' ? src : await this.findOneOrFail(src); diff --git a/src/models/repositories/note.ts b/src/models/repositories/note.ts index aa5f93a5dd..3642a03c2c 100644 --- a/src/models/repositories/note.ts +++ b/src/models/repositories/note.ts @@ -79,7 +79,7 @@ export class NoteRepository extends Repository<Note> { public async pack( src: Note['id'] | Note, - me?: User['id'] | User | null | undefined, + me?: { id: User['id'] } | null | undefined, options?: { detail?: boolean; skipHide?: boolean; @@ -93,7 +93,7 @@ export class NoteRepository extends Repository<Note> { skipHide: false }, options); - const meId = me ? typeof me === 'string' ? me : me.id : null; + const meId = me ? me.id : null; const note = typeof src === 'object' ? src : await this.findOneOrFail(src); const host = note.userHost; @@ -174,7 +174,7 @@ export class NoteRepository extends Repository<Note> { id: note.id, createdAt: note.createdAt.toISOString(), userId: note.userId, - user: Users.pack(note.user || note.userId, meId, { + user: Users.pack(note.user || note.userId, me, { detail: false, }), text: text, @@ -204,12 +204,12 @@ export class NoteRepository extends Repository<Note> { _prId_: (note as any)._prId_ || undefined, ...(opts.detail ? { - reply: note.replyId ? this.pack(note.reply || note.replyId, meId, { + reply: note.replyId ? this.pack(note.reply || note.replyId, me, { detail: false, _hint_: options?._hint_ }) : undefined, - renote: note.renoteId ? this.pack(note.renote || note.renoteId, meId, { + renote: note.renoteId ? this.pack(note.renote || note.renoteId, me, { detail: true, _hint_: options?._hint_ }) : undefined, @@ -236,7 +236,7 @@ export class NoteRepository extends Repository<Note> { public async packMany( notes: Note[], - me?: User['id'] | User | null | undefined, + me?: { id: User['id'] } | null | undefined, options?: { detail?: boolean; skipHide?: boolean; @@ -244,7 +244,7 @@ export class NoteRepository extends Repository<Note> { ) { if (notes.length === 0) return []; - const meId = me ? typeof me === 'string' ? me : me.id : null; + const meId = me ? me.id : null; const myReactionsMap = new Map<Note['id'], NoteReaction | null>(); if (meId) { const renoteIds = notes.filter(n => n.renoteId != null).map(n => n.renoteId!); diff --git a/src/models/repositories/notification.ts b/src/models/repositories/notification.ts index 7a7ac52018..abadea4632 100644 --- a/src/models/repositories/notification.ts +++ b/src/models/repositories/notification.ts @@ -31,38 +31,38 @@ export class NotificationRepository extends Repository<Notification> { userId: notification.notifierId, user: notification.notifierId ? Users.pack(notification.notifier || notification.notifierId) : null, ...(notification.type === 'mention' ? { - note: Notes.pack(notification.note || notification.noteId!, notification.notifieeId, { + note: Notes.pack(notification.note || notification.noteId!, { id: notification.notifieeId }, { detail: true, _hint_: options._hintForEachNotes_ }), } : {}), ...(notification.type === 'reply' ? { - note: Notes.pack(notification.note || notification.noteId!, notification.notifieeId, { + note: Notes.pack(notification.note || notification.noteId!, { id: notification.notifieeId }, { detail: true, _hint_: options._hintForEachNotes_ }), } : {}), ...(notification.type === 'renote' ? { - note: Notes.pack(notification.note || notification.noteId!, notification.notifieeId, { + note: Notes.pack(notification.note || notification.noteId!, { id: notification.notifieeId }, { detail: true, _hint_: options._hintForEachNotes_ }), } : {}), ...(notification.type === 'quote' ? { - note: Notes.pack(notification.note || notification.noteId!, notification.notifieeId, { + note: Notes.pack(notification.note || notification.noteId!, { id: notification.notifieeId }, { detail: true, _hint_: options._hintForEachNotes_ }), } : {}), ...(notification.type === 'reaction' ? { - note: Notes.pack(notification.note || notification.noteId!, notification.notifieeId, { + note: Notes.pack(notification.note || notification.noteId!, { id: notification.notifieeId }, { detail: true, _hint_: options._hintForEachNotes_ }), reaction: notification.reaction } : {}), ...(notification.type === 'pollVote' ? { - note: Notes.pack(notification.note || notification.noteId!, notification.notifieeId, { + note: Notes.pack(notification.note || notification.noteId!, { id: notification.notifieeId }, { detail: true, _hint_: options._hintForEachNotes_ }), diff --git a/src/models/repositories/page-like.ts b/src/models/repositories/page-like.ts index 94b1685e5e..cfef950f3b 100644 --- a/src/models/repositories/page-like.ts +++ b/src/models/repositories/page-like.ts @@ -1,12 +1,13 @@ import { EntityRepository, Repository } from 'typeorm'; import { PageLike } from '../entities/page-like'; import { Pages } from '..'; +import { User } from '../entities/user'; @EntityRepository(PageLike) export class PageLikeRepository extends Repository<PageLike> { public async pack( src: PageLike['id'] | PageLike, - me?: any + me?: { id: User['id'] } | null | undefined ) { const like = typeof src === 'object' ? src : await this.findOneOrFail(src); @@ -18,7 +19,7 @@ export class PageLikeRepository extends Repository<PageLike> { public packMany( likes: any[], - me: any + me: { id: User['id'] } ) { return Promise.all(likes.map(x => this.pack(x, me))); } diff --git a/src/models/repositories/page.ts b/src/models/repositories/page.ts index 3017769d6b..a162a50321 100644 --- a/src/models/repositories/page.ts +++ b/src/models/repositories/page.ts @@ -12,9 +12,9 @@ export type PackedPage = SchemaType<typeof packedPageSchema>; export class PageRepository extends Repository<Page> { public async pack( src: Page['id'] | Page, - me?: User['id'] | User | null | undefined, + me?: { id: User['id'] } | null | undefined, ): Promise<PackedPage> { - const meId = me ? typeof me === 'string' ? me : me.id : null; + const meId = me ? me.id : null; const page = typeof src === 'object' ? src : await this.findOneOrFail(src); const attachedFiles: Promise<DriveFile | undefined>[] = []; @@ -84,7 +84,7 @@ export class PageRepository extends Repository<Page> { public packMany( pages: Page[], - me?: User['id'] | User | null | undefined, + me?: { id: User['id'] } | null | undefined, ) { return Promise.all(pages.map(x => this.pack(x, me))); } diff --git a/src/models/repositories/user.ts b/src/models/repositories/user.ts index 51b67a27ea..bb084f0245 100644 --- a/src/models/repositories/user.ts +++ b/src/models/repositories/user.ts @@ -147,7 +147,7 @@ export class UserRepository extends Repository<User> { public async pack( src: User['id'] | User, - me?: User['id'] | User | null | undefined, + me?: { id: User['id'] } | null | undefined, options?: { detail?: boolean, includeSecrets?: boolean, @@ -159,7 +159,7 @@ export class UserRepository extends Repository<User> { }, options); const user = typeof src === 'object' ? src : await this.findOneOrFail(src); - const meId = me ? typeof me === 'string' ? me : me.id : null; + const meId = me ? me.id : null; const relation = meId && (meId !== user.id) && opts.detail ? await this.getRelation(meId, user.id) : null; const pins = opts.detail ? await UserNotePinings.createQueryBuilder('pin') @@ -213,11 +213,11 @@ export class UserRepository extends Repository<User> { followingCount: user.followingCount, notesCount: user.notesCount, pinnedNoteIds: pins.map(pin => pin.noteId), - pinnedNotes: Notes.packMany(pins.map(pin => pin.note!), meId, { + pinnedNotes: Notes.packMany(pins.map(pin => pin.note!), me, { detail: true }), pinnedPageId: profile!.pinnedPageId, - pinnedPage: profile!.pinnedPageId ? Pages.pack(profile!.pinnedPageId, meId) : null, + pinnedPage: profile!.pinnedPageId ? Pages.pack(profile!.pinnedPageId, me) : null, twoFactorEnabled: profile!.twoFactorEnabled, usePasswordLessLogin: profile!.usePasswordLessLogin, securityKeys: profile!.twoFactorEnabled @@ -286,7 +286,7 @@ export class UserRepository extends Repository<User> { public packMany( users: (User['id'] | User)[], - me?: User['id'] | User | null | undefined, + me?: { id: User['id'] } | null | undefined, options?: { detail?: boolean, includeSecrets?: boolean, @@ -295,11 +295,15 @@ export class UserRepository extends Repository<User> { return Promise.all(users.map(u => this.pack(u, me, options))); } - public isLocalUser(user: User): user is ILocalUser { + public isLocalUser(user: User): user is ILocalUser; + public isLocalUser<T extends { host: User['host'] }>(user: T): user is T & { host: null; }; + public isLocalUser(user: User | { host: User['host'] }): boolean { return user.host == null; } - public isRemoteUser(user: User): user is IRemoteUser { + public isRemoteUser(user: User): user is IRemoteUser; + public isRemoteUser<T extends { host: User['host'] }>(user: T): user is T & { host: string; }; + public isRemoteUser(user: User | { host: User['host'] }): boolean { return !this.isLocalUser(user); } |