From ce340aba7a37394c70b9f3d7cece9cfa5e91d94c Mon Sep 17 00:00:00 2001 From: syuilo Date: Wed, 24 Mar 2021 11:05:37 +0900 Subject: [PATCH] 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 --- src/misc/convert-host.ts | 1 + src/misc/download-url.ts | 1 + src/models/repositories/app.ts | 2 +- src/models/repositories/auth-session.ts | 3 ++- src/models/repositories/blocking.ts | 5 +++-- src/models/repositories/channel.ts | 10 +++++----- src/models/repositories/drive-file.ts | 2 +- src/models/repositories/follow-request.ts | 3 ++- src/models/repositories/following.ts | 5 +++-- src/models/repositories/games/reversi/game.ts | 10 +++++----- .../repositories/games/reversi/matching.ts | 3 ++- src/models/repositories/messaging-message.ts | 3 ++- src/models/repositories/muting.ts | 5 +++-- src/models/repositories/note-favorite.ts | 5 +++-- src/models/repositories/note-reaction.ts | 3 ++- src/models/repositories/note.ts | 14 ++++++------- src/models/repositories/notification.ts | 12 +++++------ src/models/repositories/page-like.ts | 5 +++-- src/models/repositories/page.ts | 6 +++--- src/models/repositories/user.ts | 18 ++++++++++------- src/queue/index.ts | 20 +++++++++---------- src/queue/processors/deliver.ts | 1 + src/queue/processors/inbox.ts | 1 + src/remote/activitypub/deliver-manager.ts | 6 +++--- src/remote/activitypub/models/person.ts | 1 + src/remote/activitypub/renderer/delete.ts | 4 ++-- src/remote/activitypub/renderer/follow.ts | 2 +- src/remote/activitypub/renderer/index.ts | 4 ++-- src/remote/activitypub/renderer/person.ts | 1 + src/remote/activitypub/renderer/question.ts | 4 ++-- src/remote/activitypub/renderer/read.ts | 4 ++-- src/remote/activitypub/renderer/reject.ts | 4 ++-- src/remote/activitypub/renderer/remove.ts | 4 ++-- src/remote/activitypub/renderer/undo.ts | 2 +- src/remote/activitypub/renderer/update.ts | 4 ++-- src/remote/activitypub/renderer/vote.ts | 4 ++-- src/remote/activitypub/request.ts | 8 ++++---- src/remote/resolve-user.ts | 1 + src/remote/webfinger.ts | 1 + src/server/api/common/generate-block-query.ts | 2 +- .../api/common/generate-channel-query.ts | 2 +- .../api/common/generate-muted-note-query.ts | 2 +- .../api/common/generate-muted-user-query.ts | 4 ++-- .../api/common/generate-replies-query.ts | 2 +- .../api/common/generate-visibility-query.ts | 2 +- .../api/common/read-messaging-message.ts | 4 ++-- src/server/api/define.ts | 19 ++++++++++++++---- .../api/endpoints/admin/accounts/create.ts | 3 ++- src/server/api/endpoints/admin/emoji/list.ts | 6 +++--- .../endpoints/admin/queue/deliver-delayed.ts | 1 + .../endpoints/admin/queue/inbox-delayed.ts | 1 + src/server/api/endpoints/admin/relays/add.ts | 1 + src/server/api/endpoints/antennas/create.ts | 4 ++-- src/server/api/endpoints/antennas/update.ts | 4 ++-- src/server/api/endpoints/app/show.ts | 2 +- src/server/api/endpoints/blocking/create.ts | 4 ++-- src/server/api/endpoints/blocking/delete.ts | 4 ++-- src/server/api/endpoints/channels/timeline.ts | 2 +- src/server/api/endpoints/clips/create.ts | 4 ++-- src/server/api/endpoints/drive.ts | 2 +- .../api/endpoints/drive/folders/create.ts | 4 ++-- .../api/endpoints/federation/show-instance.ts | 1 - .../api/endpoints/games/reversi/games.ts | 2 +- .../api/endpoints/games/reversi/match.ts | 2 +- src/server/api/endpoints/i/pin.ts | 2 +- src/server/api/endpoints/i/registry/scopes.ts | 1 - src/server/api/endpoints/i/unpin.ts | 2 +- src/server/api/endpoints/i/update.ts | 3 ++- src/server/api/endpoints/notes/unrenote.ts | 4 ++-- src/server/api/endpoints/page-push.ts | 2 +- .../api/endpoints/users/groups/create.ts | 4 ++-- .../api/endpoints/users/groups/invite.ts | 4 ++-- .../api/endpoints/users/lists/create.ts | 4 ++-- src/server/api/endpoints/users/show.ts | 5 +++-- src/server/api/limiter.ts | 2 +- .../api/stream/channels/games/reversi.ts | 2 +- .../api/stream/channels/global-timeline.ts | 3 ++- .../api/stream/channels/home-timeline.ts | 3 ++- .../api/stream/channels/hybrid-timeline.ts | 3 ++- .../api/stream/channels/local-timeline.ts | 3 ++- src/server/api/stream/index.ts | 4 ++-- src/server/index.ts | 2 +- src/services/add-note-to-antenna.ts | 2 +- .../chart/charts/classes/active-users.ts | 2 +- src/services/chart/charts/classes/hashtag.ts | 2 +- .../charts/classes/per-user-following.ts | 2 +- .../chart/charts/classes/per-user-notes.ts | 2 +- .../charts/classes/per-user-reactions.ts | 2 +- src/services/chart/charts/classes/users.ts | 2 +- src/services/drive/add-file.ts | 4 ++-- src/services/drive/s3.ts | 1 + src/services/drive/upload-from-url.ts | 3 ++- src/services/following/create.ts | 13 ++++++++---- src/services/following/delete.ts | 6 +++--- src/services/following/requests/accept-all.ts | 2 +- src/services/following/requests/accept.ts | 10 +++++----- src/services/following/requests/cancel.ts | 9 ++++++--- src/services/following/requests/create.ts | 6 +++--- src/services/following/requests/reject.ts | 10 +++++----- src/services/i/pin.ts | 4 ++-- src/services/insert-moderation-log.ts | 4 ++-- src/services/messages/create.ts | 2 +- src/services/note/create.ts | 18 ++++++++--------- src/services/note/reaction/create.ts | 2 +- src/services/note/reaction/delete.ts | 2 +- src/services/relay.ts | 4 ++-- src/services/suspend-user.ts | 4 ++-- src/services/update-hashtag.ts | 4 ++-- src/tsconfig.json | 2 +- 109 files changed, 252 insertions(+), 201 deletions(-) diff --git a/src/misc/convert-host.ts b/src/misc/convert-host.ts index ef0ff18fc..f8957a64a 100644 --- a/src/misc/convert-host.ts +++ b/src/misc/convert-host.ts @@ -1,3 +1,4 @@ +import { URL } from 'url'; import config from '@/config'; import { toASCII } from 'punycode'; diff --git a/src/misc/download-url.ts b/src/misc/download-url.ts index d3abf9c34..dafeff6fc 100644 --- a/src/misc/download-url.ts +++ b/src/misc/download-url.ts @@ -1,6 +1,7 @@ import * as fs from 'fs'; import * as stream from 'stream'; import * as util from 'util'; +import { URL } from 'url'; import fetch from 'node-fetch'; import { getAgentByUrl } from './fetch'; import { AbortController } from 'abort-controller'; diff --git a/src/models/repositories/app.ts b/src/models/repositories/app.ts index 12c78fe87..c919a9bdb 100644 --- a/src/models/repositories/app.ts +++ b/src/models/repositories/app.ts @@ -9,7 +9,7 @@ export type PackedApp = SchemaType; export class AppRepository extends Repository { 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 e985d6925..f513357e1 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 { 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 1ecdbd5d0..60b43fae3 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; @@ -10,7 +11,7 @@ export type PackedBlocking = SchemaType; export class BlockingRepository extends Repository { public async pack( src: Blocking['id'] | Blocking, - me?: any + me?: { id: User['id'] } | null | undefined ): Promise { const blocking = typeof src === 'object' ? src : await this.findOneOrFail(src); @@ -26,7 +27,7 @@ export class BlockingRepository extends Repository { 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 b15cf8b35..a1c85f2bd 100644 --- a/src/models/repositories/channel.ts +++ b/src/models/repositories/channel.ts @@ -10,19 +10,19 @@ export type PackedChannel = SchemaType; export class ChannelRepository extends Repository { public async pack( src: Channel['id'] | Channel, - me?: User['id'] | User | null | undefined, + me?: { id: User['id'] } | null | undefined, ): Promise { 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 3a036beb8..590079fe4 100644 --- a/src/models/repositories/drive-file.ts +++ b/src/models/repositories/drive-file.ts @@ -53,7 +53,7 @@ export class DriveFileRepository extends Repository { return thumbnail ? (file.thumbnailUrl || (isImage ? (file.webpublicUrl || file.url) : null)) : (file.webpublicUrl || file.url); } - public async calcDriveUsageOf(user: User['id'] | User): Promise { + public async calcDriveUsageOf(user: User['id'] | { id: User['id'] }): Promise { 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 0d96b8eb5..31e5fb2d9 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 { 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 d754661b2..b88643297 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 { 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 { 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 e23247f66..344cf7b20 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 { 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 { }, 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 { 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 51f17c9a4..013021eb9 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 { 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 2c78e7fbd..8d6d03a23 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; @@ -13,7 +14,7 @@ export class MessagingMessageRepository extends Repository { 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 8a5c57a38..b5bbe8a0a 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; @@ -10,7 +11,7 @@ export type PackedMuting = SchemaType; export class MutingRepository extends Repository { public async pack( src: Muting['id'] | Muting, - me?: any + me?: { id: User['id'] } | null | undefined ): Promise { const muting = typeof src === 'object' ? src : await this.findOneOrFail(src); @@ -26,7 +27,7 @@ export class MutingRepository extends Repository { 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 eb2ffff4c..316ebdff3 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 { 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 { 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 e04a49300..4c5680960 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; @@ -10,7 +11,7 @@ export type PackedNoteReaction = SchemaType; export class NoteReactionRepository extends Repository { public async pack( src: NoteReaction['id'] | NoteReaction, - me?: any + me?: { id: User['id'] } | null | undefined ): Promise { 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 aa5f93a5d..3642a03c2 100644 --- a/src/models/repositories/note.ts +++ b/src/models/repositories/note.ts @@ -79,7 +79,7 @@ export class NoteRepository extends Repository { 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 { 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 { 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 { _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 { 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 { ) { 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(); 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 7a7ac5201..abadea463 100644 --- a/src/models/repositories/notification.ts +++ b/src/models/repositories/notification.ts @@ -31,38 +31,38 @@ export class NotificationRepository extends Repository { 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 94b1685e5..cfef950f3 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 { 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 { 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 3017769d6..a162a5032 100644 --- a/src/models/repositories/page.ts +++ b/src/models/repositories/page.ts @@ -12,9 +12,9 @@ export type PackedPage = SchemaType; export class PageRepository extends Repository { public async pack( src: Page['id'] | Page, - me?: User['id'] | User | null | undefined, + me?: { id: User['id'] } | null | undefined, ): Promise { - 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[] = []; @@ -84,7 +84,7 @@ export class PageRepository extends Repository { 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 51b67a27e..bb084f024 100644 --- a/src/models/repositories/user.ts +++ b/src/models/repositories/user.ts @@ -147,7 +147,7 @@ export class UserRepository extends Repository { 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 { }, 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 { 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 { 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 { 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(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(user: T): user is T & { host: string; }; + public isRemoteUser(user: User | { host: User['host'] }): boolean { return !this.isLocalUser(user); } diff --git a/src/queue/index.ts b/src/queue/index.ts index 6c0dba79c..201b66379 100644 --- a/src/queue/index.ts +++ b/src/queue/index.ts @@ -1,7 +1,7 @@ import * as httpSignature from 'http-signature'; import config from '@/config'; -import { ILocalUser } from '../models/entities/user'; +import { User } from '../models/entities/user'; import { program } from '../argv'; import processDeliver from './processors/deliver'; @@ -65,7 +65,7 @@ objectStorageQueue .on('error', (job: any, err: Error) => objectStorageLogger.error(`error ${err}`, { job, e: renderError(err) })) .on('stalled', (job) => objectStorageLogger.warn(`stalled id=${job.id}`)); -export function deliver(user: ILocalUser, content: any, to: any) { +export function deliver(user: { id: User['id']; host: null; }, content: any, to: any) { if (content == null) return null; const data = { @@ -102,7 +102,7 @@ export function inbox(activity: any, signature: httpSignature.IParsedSignature) }); } -export function createDeleteDriveFilesJob(user: ILocalUser) { +export function createDeleteDriveFilesJob(user: { id: User['id'] }) { return dbQueue.add('deleteDriveFiles', { user: user }, { @@ -111,7 +111,7 @@ export function createDeleteDriveFilesJob(user: ILocalUser) { }); } -export function createExportNotesJob(user: ILocalUser) { +export function createExportNotesJob(user: { id: User['id'] }) { return dbQueue.add('exportNotes', { user: user }, { @@ -120,7 +120,7 @@ export function createExportNotesJob(user: ILocalUser) { }); } -export function createExportFollowingJob(user: ILocalUser) { +export function createExportFollowingJob(user: { id: User['id'] }) { return dbQueue.add('exportFollowing', { user: user }, { @@ -129,7 +129,7 @@ export function createExportFollowingJob(user: ILocalUser) { }); } -export function createExportMuteJob(user: ILocalUser) { +export function createExportMuteJob(user: { id: User['id'] }) { return dbQueue.add('exportMute', { user: user }, { @@ -138,7 +138,7 @@ export function createExportMuteJob(user: ILocalUser) { }); } -export function createExportBlockingJob(user: ILocalUser) { +export function createExportBlockingJob(user: { id: User['id'] }) { return dbQueue.add('exportBlocking', { user: user }, { @@ -147,7 +147,7 @@ export function createExportBlockingJob(user: ILocalUser) { }); } -export function createExportUserListsJob(user: ILocalUser) { +export function createExportUserListsJob(user: { id: User['id'] }) { return dbQueue.add('exportUserLists', { user: user }, { @@ -156,7 +156,7 @@ export function createExportUserListsJob(user: ILocalUser) { }); } -export function createImportFollowingJob(user: ILocalUser, fileId: DriveFile['id']) { +export function createImportFollowingJob(user: { id: User['id'] }, fileId: DriveFile['id']) { return dbQueue.add('importFollowing', { user: user, fileId: fileId @@ -166,7 +166,7 @@ export function createImportFollowingJob(user: ILocalUser, fileId: DriveFile['id }); } -export function createImportUserListsJob(user: ILocalUser, fileId: DriveFile['id']) { +export function createImportUserListsJob(user: { id: User['id'] }, fileId: DriveFile['id']) { return dbQueue.add('importUserLists', { user: user, fileId: fileId diff --git a/src/queue/processors/deliver.ts b/src/queue/processors/deliver.ts index d1a3eac17..b167154fc 100644 --- a/src/queue/processors/deliver.ts +++ b/src/queue/processors/deliver.ts @@ -1,3 +1,4 @@ +import { URL } from 'url'; import * as Bull from 'bull'; import request from '../../remote/activitypub/request'; import { registerOrFetchInstanceDoc } from '../../services/register-or-fetch-instance-doc'; diff --git a/src/queue/processors/inbox.ts b/src/queue/processors/inbox.ts index 39ac55758..7c746eb25 100644 --- a/src/queue/processors/inbox.ts +++ b/src/queue/processors/inbox.ts @@ -1,3 +1,4 @@ +import { URL } from 'url'; import * as Bull from 'bull'; import * as httpSignature from 'http-signature'; import perform from '../../remote/activitypub/perform'; diff --git a/src/remote/activitypub/deliver-manager.ts b/src/remote/activitypub/deliver-manager.ts index 92721f552..f112b02b4 100644 --- a/src/remote/activitypub/deliver-manager.ts +++ b/src/remote/activitypub/deliver-manager.ts @@ -1,5 +1,5 @@ import { Users, Followings } from '../../models'; -import { ILocalUser, IRemoteUser } from '../../models/entities/user'; +import { ILocalUser, IRemoteUser, User } from '../../models/entities/user'; import { deliver } from '../../queue'; //#region types @@ -24,7 +24,7 @@ const isDirect = (recipe: any): recipe is IDirectRecipe => //#endregion export default class DeliverManager { - private actor: ILocalUser; + private actor: { id: User['id']; host: null; }; private activity: any; private recipes: IRecipe[] = []; @@ -33,7 +33,7 @@ export default class DeliverManager { * @param actor Actor * @param activity Activity to deliver */ - constructor(actor: ILocalUser, activity: any) { + constructor(actor: { id: User['id']; host: null; }, activity: any) { this.actor = actor; this.activity = activity; } diff --git a/src/remote/activitypub/models/person.ts b/src/remote/activitypub/models/person.ts index 734275a47..5b032d9d9 100644 --- a/src/remote/activitypub/models/person.ts +++ b/src/remote/activitypub/models/person.ts @@ -1,3 +1,4 @@ +import { URL } from 'url'; import * as promiseLimit from 'promise-limit'; import config from '@/config'; diff --git a/src/remote/activitypub/renderer/delete.ts b/src/remote/activitypub/renderer/delete.ts index dd4919342..710f0482a 100644 --- a/src/remote/activitypub/renderer/delete.ts +++ b/src/remote/activitypub/renderer/delete.ts @@ -1,7 +1,7 @@ import config from '@/config'; -import { ILocalUser } from '../../../models/entities/user'; +import { User } from '../../../models/entities/user'; -export default (object: any, user: ILocalUser) => ({ +export default (object: any, user: { id: User['id']; host: null }) => ({ type: 'Delete', actor: `${config.url}/users/${user.id}`, object diff --git a/src/remote/activitypub/renderer/follow.ts b/src/remote/activitypub/renderer/follow.ts index fb8bca52a..252b0b283 100644 --- a/src/remote/activitypub/renderer/follow.ts +++ b/src/remote/activitypub/renderer/follow.ts @@ -2,7 +2,7 @@ import config from '@/config'; import { User } from '../../../models/entities/user'; import { Users } from '../../../models'; -export default (follower: User, followee: User, requestId?: string) => { +export default (follower: { id: User['id']; host: User['host']; uri: User['host'] }, followee: { id: User['id']; host: User['host']; uri: User['host'] }, requestId?: string) => { const follow = { type: 'Follow', actor: Users.isLocalUser(follower) ? `${config.url}/users/${follower.id}` : follower.uri, diff --git a/src/remote/activitypub/renderer/index.ts b/src/remote/activitypub/renderer/index.ts index d96c27788..3283c88bd 100644 --- a/src/remote/activitypub/renderer/index.ts +++ b/src/remote/activitypub/renderer/index.ts @@ -2,8 +2,8 @@ import config from '@/config'; import { v4 as uuid } from 'uuid'; import { IActivity } from '../type'; import { LdSignature } from '../misc/ld-signature'; -import { ILocalUser } from '../../../models/entities/user'; import { getUserKeypair } from '@/misc/keypair-store'; +import { User } from '@/models/entities/user'; export const renderActivity = (x: any): IActivity | null => { if (x == null) return null; @@ -20,7 +20,7 @@ export const renderActivity = (x: any): IActivity | null => { }, x); }; -export const attachLdSignature = async (activity: any, user: ILocalUser): Promise => { +export const attachLdSignature = async (activity: any, user: { id: User['id']; host: null; }): Promise => { if (activity == null) return null; const keypair = await getUserKeypair(user.id); diff --git a/src/remote/activitypub/renderer/person.ts b/src/remote/activitypub/renderer/person.ts index 894a6edf0..e4e8f24f1 100644 --- a/src/remote/activitypub/renderer/person.ts +++ b/src/remote/activitypub/renderer/person.ts @@ -1,3 +1,4 @@ +import { URL } from 'url'; import renderImage from './image'; import renderKey from './key'; import config from '@/config'; diff --git a/src/remote/activitypub/renderer/question.ts b/src/remote/activitypub/renderer/question.ts index 3b17c7dc9..99670f80a 100644 --- a/src/remote/activitypub/renderer/question.ts +++ b/src/remote/activitypub/renderer/question.ts @@ -1,9 +1,9 @@ import config from '@/config'; -import { ILocalUser } from '../../../models/entities/user'; +import { User } from '@/models/entities/user'; import { Note } from '../../../models/entities/note'; import { Poll } from '../../../models/entities/poll'; -export default async function renderQuestion(user: ILocalUser, note: Note, poll: Poll) { +export default async function renderQuestion(user: { id: User['id'] }, note: Note, poll: Poll) { const question = { type: 'Question', id: `${config.url}/questions/${note.id}`, diff --git a/src/remote/activitypub/renderer/read.ts b/src/remote/activitypub/renderer/read.ts index 1b5fef298..1287f4ccb 100644 --- a/src/remote/activitypub/renderer/read.ts +++ b/src/remote/activitypub/renderer/read.ts @@ -1,8 +1,8 @@ import config from '@/config'; -import { ILocalUser } from '../../../models/entities/user'; +import { User } from '@/models/entities/user'; import { MessagingMessage } from '../../../models/entities/messaging-message'; -export const renderReadActivity = (user: ILocalUser, message: MessagingMessage) => ({ +export const renderReadActivity = (user: { id: User['id'] }, message: MessagingMessage) => ({ type: 'Read', actor: `${config.url}/users/${user.id}`, object: message.uri diff --git a/src/remote/activitypub/renderer/reject.ts b/src/remote/activitypub/renderer/reject.ts index e29057ae9..e1eb5b004 100644 --- a/src/remote/activitypub/renderer/reject.ts +++ b/src/remote/activitypub/renderer/reject.ts @@ -1,7 +1,7 @@ import config from '@/config'; -import { ILocalUser } from '../../../models/entities/user'; +import { User } from '@/models/entities/user'; -export default (object: any, user: ILocalUser) => ({ +export default (object: any, user: { id: User['id'] }) => ({ type: 'Reject', actor: `${config.url}/users/${user.id}`, object diff --git a/src/remote/activitypub/renderer/remove.ts b/src/remote/activitypub/renderer/remove.ts index 1d5d2ffe7..ff1fab8e5 100644 --- a/src/remote/activitypub/renderer/remove.ts +++ b/src/remote/activitypub/renderer/remove.ts @@ -1,7 +1,7 @@ import config from '@/config'; -import { ILocalUser } from '../../../models/entities/user'; +import { User } from '@/models/entities/user'; -export default (user: ILocalUser, target: any, object: any) => ({ +export default (user: { id: User['id'] }, target: any, object: any) => ({ type: 'Remove', actor: `${config.url}/users/${user.id}`, target, diff --git a/src/remote/activitypub/renderer/undo.ts b/src/remote/activitypub/renderer/undo.ts index 0e9998a41..6f367415c 100644 --- a/src/remote/activitypub/renderer/undo.ts +++ b/src/remote/activitypub/renderer/undo.ts @@ -1,7 +1,7 @@ import config from '@/config'; import { ILocalUser, User } from '../../../models/entities/user'; -export default (object: any, user: ILocalUser | User) => ({ +export default (object: any, user: { id: User['id'] }) => ({ type: 'Undo', actor: `${config.url}/users/${user.id}`, object diff --git a/src/remote/activitypub/renderer/update.ts b/src/remote/activitypub/renderer/update.ts index 636dc5c55..4295fc64f 100644 --- a/src/remote/activitypub/renderer/update.ts +++ b/src/remote/activitypub/renderer/update.ts @@ -1,7 +1,7 @@ import config from '@/config'; -import { ILocalUser } from '../../../models/entities/user'; +import { User } from '@/models/entities/user'; -export default (object: any, user: ILocalUser) => { +export default (object: any, user: { id: User['id'] }) => { const activity = { id: `${config.url}/users/${user.id}#updates/${new Date().getTime()}`, actor: `${config.url}/users/${user.id}`, diff --git a/src/remote/activitypub/renderer/vote.ts b/src/remote/activitypub/renderer/vote.ts index 1eb3acc3d..529fdaafc 100644 --- a/src/remote/activitypub/renderer/vote.ts +++ b/src/remote/activitypub/renderer/vote.ts @@ -1,10 +1,10 @@ import config from '@/config'; import { Note } from '../../../models/entities/note'; -import { IRemoteUser, ILocalUser } from '../../../models/entities/user'; +import { IRemoteUser, User } from '../../../models/entities/user'; import { PollVote } from '../../../models/entities/poll-vote'; import { Poll } from '../../../models/entities/poll'; -export default async function renderVote(user: ILocalUser, vote: PollVote, note: Note, poll: Poll, pollOwner: IRemoteUser): Promise { +export default async function renderVote(user: { id: User['id'] }, vote: PollVote, note: Note, poll: Poll, pollOwner: IRemoteUser): Promise { return { id: `${config.url}/users/${user.id}#votes/${vote.id}/activity`, actor: `${config.url}/users/${user.id}`, diff --git a/src/remote/activitypub/request.ts b/src/remote/activitypub/request.ts index fc774bc9a..e4dca3232 100644 --- a/src/remote/activitypub/request.ts +++ b/src/remote/activitypub/request.ts @@ -4,14 +4,14 @@ import { sign } from 'http-signature'; import * as crypto from 'crypto'; import config from '@/config'; -import { ILocalUser } from '../../models/entities/user'; +import { User } from '@/models/entities/user'; import { getAgentByUrl } from '@/misc/fetch'; import { URL } from 'url'; import got from 'got'; import * as Got from 'got'; import { getUserKeypair } from '@/misc/keypair-store'; -export default async (user: ILocalUser, url: string, object: any) => { +export default async (user: { id: User['id'] }, url: string, object: any) => { const timeout = 10 * 1000; const { protocol, hostname, port, pathname, search } = new URL(url); @@ -24,7 +24,7 @@ export default async (user: ILocalUser, url: string, object: any) => { const keypair = await getUserKeypair(user.id); - await new Promise((resolve, reject) => { + await new Promise((resolve, reject) => { const req = https.request({ agent: getAgentByUrl(new URL(`https://example.net`)), protocol, @@ -69,7 +69,7 @@ export default async (user: ILocalUser, url: string, object: any) => { * @param user http-signature user * @param url URL to fetch */ -export async function signedGet(url: string, user: ILocalUser) { +export async function signedGet(url: string, user: { id: User['id'] }) { const timeout = 10 * 1000; const keypair = await getUserKeypair(user.id); diff --git a/src/remote/resolve-user.ts b/src/remote/resolve-user.ts index eec66bb0e..1bcecee3c 100644 --- a/src/remote/resolve-user.ts +++ b/src/remote/resolve-user.ts @@ -1,3 +1,4 @@ +import { URL } from 'url'; import webFinger from './webfinger'; import config from '@/config'; import { createPerson, updatePerson } from './activitypub/models/person'; diff --git a/src/remote/webfinger.ts b/src/remote/webfinger.ts index 48cfc4d50..744ab3639 100644 --- a/src/remote/webfinger.ts +++ b/src/remote/webfinger.ts @@ -1,3 +1,4 @@ +import { URL } from 'url'; import { getJson } from '@/misc/fetch'; import { query as urlQuery } from '../prelude/url'; diff --git a/src/server/api/common/generate-block-query.ts b/src/server/api/common/generate-block-query.ts index 363641048..fa2179ae6 100644 --- a/src/server/api/common/generate-block-query.ts +++ b/src/server/api/common/generate-block-query.ts @@ -2,7 +2,7 @@ import { User } from '../../../models/entities/user'; import { Blockings } from '../../../models'; import { SelectQueryBuilder } from 'typeorm'; -export function generateBlockQueryForUsers(q: SelectQueryBuilder, me: User) { +export function generateBlockQueryForUsers(q: SelectQueryBuilder, me: { id: User['id'] }) { const blockingQuery = Blockings.createQueryBuilder('blocking') .select('blocking.blockeeId') .where('blocking.blockerId = :blockerId', { blockerId: me.id }); diff --git a/src/server/api/common/generate-channel-query.ts b/src/server/api/common/generate-channel-query.ts index c0337b2c6..74a6d68c4 100644 --- a/src/server/api/common/generate-channel-query.ts +++ b/src/server/api/common/generate-channel-query.ts @@ -2,7 +2,7 @@ import { User } from '../../../models/entities/user'; import { ChannelFollowings } from '../../../models'; import { Brackets, SelectQueryBuilder } from 'typeorm'; -export function generateChannelQuery(q: SelectQueryBuilder, me?: User | null) { +export function generateChannelQuery(q: SelectQueryBuilder, me?: { id: User['id'] } | null) { if (me == null) { q.andWhere('note.channelId IS NULL'); } else { diff --git a/src/server/api/common/generate-muted-note-query.ts b/src/server/api/common/generate-muted-note-query.ts index 498930476..c7f9f7c5a 100644 --- a/src/server/api/common/generate-muted-note-query.ts +++ b/src/server/api/common/generate-muted-note-query.ts @@ -2,7 +2,7 @@ import { User } from '../../../models/entities/user'; import { MutedNotes } from '../../../models'; import { SelectQueryBuilder } from 'typeorm'; -export function generateMutedNoteQuery(q: SelectQueryBuilder, me: User) { +export function generateMutedNoteQuery(q: SelectQueryBuilder, me: { id: User['id'] }) { const mutedQuery = MutedNotes.createQueryBuilder('muted') .select('muted.noteId') .where('muted.userId = :userId', { userId: me.id }); diff --git a/src/server/api/common/generate-muted-user-query.ts b/src/server/api/common/generate-muted-user-query.ts index b346f2f0f..f5e072ba9 100644 --- a/src/server/api/common/generate-muted-user-query.ts +++ b/src/server/api/common/generate-muted-user-query.ts @@ -2,7 +2,7 @@ import { User } from '../../../models/entities/user'; import { Mutings } from '../../../models'; import { SelectQueryBuilder, Brackets } from 'typeorm'; -export function generateMutedUserQuery(q: SelectQueryBuilder, me: User, exclude?: User) { +export function generateMutedUserQuery(q: SelectQueryBuilder, me: { id: User['id'] }, exclude?: User) { const mutingQuery = Mutings.createQueryBuilder('muting') .select('muting.muteeId') .where('muting.muterId = :muterId', { muterId: me.id }); @@ -28,7 +28,7 @@ export function generateMutedUserQuery(q: SelectQueryBuilder, me: User, exc q.setParameters(mutingQuery.getParameters()); } -export function generateMutedUserQueryForUsers(q: SelectQueryBuilder, me: User) { +export function generateMutedUserQueryForUsers(q: SelectQueryBuilder, me: { id: User['id'] }) { const mutingQuery = Mutings.createQueryBuilder('muting') .select('muting.muteeId') .where('muting.muterId = :muterId', { muterId: me.id }); diff --git a/src/server/api/common/generate-replies-query.ts b/src/server/api/common/generate-replies-query.ts index 8ebe93f20..29b1e17c2 100644 --- a/src/server/api/common/generate-replies-query.ts +++ b/src/server/api/common/generate-replies-query.ts @@ -1,7 +1,7 @@ import { User } from '../../../models/entities/user'; import { Brackets, SelectQueryBuilder } from 'typeorm'; -export function generateRepliesQuery(q: SelectQueryBuilder, me?: User | null) { +export function generateRepliesQuery(q: SelectQueryBuilder, me?: { id: User['id'] } | null) { if (me == null) { q.andWhere(new Brackets(qb => { qb .where(`note.replyId IS NULL`) // 返信ではない diff --git a/src/server/api/common/generate-visibility-query.ts b/src/server/api/common/generate-visibility-query.ts index d981b4a61..72ed1c46e 100644 --- a/src/server/api/common/generate-visibility-query.ts +++ b/src/server/api/common/generate-visibility-query.ts @@ -2,7 +2,7 @@ import { User } from '../../../models/entities/user'; import { Followings } from '../../../models'; import { Brackets, SelectQueryBuilder } from 'typeorm'; -export function generateVisibilityQuery(q: SelectQueryBuilder, me?: User | null) { +export function generateVisibilityQuery(q: SelectQueryBuilder, me?: { id: User['id'] } | null) { if (me == null) { q.andWhere(new Brackets(qb => { qb .where(`note.visibility = 'public'`) diff --git a/src/server/api/common/read-messaging-message.ts b/src/server/api/common/read-messaging-message.ts index 950ab2b7b..6c9fcf973 100644 --- a/src/server/api/common/read-messaging-message.ts +++ b/src/server/api/common/read-messaging-message.ts @@ -1,7 +1,7 @@ import { publishMainStream, publishGroupMessagingStream } from '../../../services/stream'; import { publishMessagingStream } from '../../../services/stream'; import { publishMessagingIndexStream } from '../../../services/stream'; -import { User, ILocalUser, IRemoteUser } from '../../../models/entities/user'; +import { User, IRemoteUser } from '../../../models/entities/user'; import { MessagingMessage } from '../../../models/entities/messaging-message'; import { MessagingMessages, UserGroupJoinings, Users } from '../../../models'; import { In } from 'typeorm'; @@ -107,7 +107,7 @@ export async function readGroupMessagingMessage( } } -export async function deliverReadActivity(user: ILocalUser, recipient: IRemoteUser, messages: MessagingMessage | MessagingMessage[]) { +export async function deliverReadActivity(user: { id: User['id']; host: null; }, recipient: IRemoteUser, messages: MessagingMessage | MessagingMessage[]) { messages = toArray(messages).filter(x => x.uri); const contents = messages.map(x => renderReadActivity(user, x)); diff --git a/src/server/api/define.ts b/src/server/api/define.ts index 7f6cceab8..432d5017e 100644 --- a/src/server/api/define.ts +++ b/src/server/api/define.ts @@ -5,6 +5,18 @@ import { ApiError } from './error'; import { SchemaType } from '@/misc/schema'; import { AccessToken } from '../../models/entities/access-token'; +type SimpleUserInfo = { + id: ILocalUser['id']; + host: ILocalUser['host']; + username: ILocalUser['username']; + uri: ILocalUser['uri']; + inbox: ILocalUser['inbox']; + sharedInbox: ILocalUser['sharedInbox']; + isAdmin: ILocalUser['isAdmin']; + isModerator: ILocalUser['isModerator']; + isSilenced: ILocalUser['isSilenced']; +}; + // TODO: defaultが設定されている場合はその型も考慮する type Params = { [P in keyof T['params']]: NonNullable[P]['transform'] extends Function @@ -15,13 +27,12 @@ type Params = { export type Response = Record | void; type executor = - (params: Params, user: T['requireCredential'] extends true ? ILocalUser : ILocalUser | null, token: AccessToken | null, file?: any, cleanup?: Function) => + (params: Params, user: T['requireCredential'] extends true ? SimpleUserInfo : SimpleUserInfo | null, token: AccessToken | null, file?: any, cleanup?: Function) => Promise>>; -// TODO: API関数に user まるごと渡すのではなくユーザーIDなどの最小限のプロパティだけ渡すようにしたい(キャッシュとか考えないでよくなるため) export default function (meta: T, cb: executor) - : (params: any, user: T['requireCredential'] extends true ? ILocalUser : ILocalUser | null, token: AccessToken | null, file?: any) => Promise { - return (params: any, user: T['requireCredential'] extends true ? ILocalUser : ILocalUser | null, token: AccessToken | null, file?: any) => { + : (params: any, user: T['requireCredential'] extends true ? SimpleUserInfo : SimpleUserInfo | null, token: AccessToken | null, file?: any) => Promise { + return (params: any, user: T['requireCredential'] extends true ? SimpleUserInfo : SimpleUserInfo | null, token: AccessToken | null, file?: any) => { function cleanup() { fs.unlink(file.path, () => {}); } diff --git a/src/server/api/endpoints/admin/accounts/create.ts b/src/server/api/endpoints/admin/accounts/create.ts index 27ceca158..9e3fc46ac 100644 --- a/src/server/api/endpoints/admin/accounts/create.ts +++ b/src/server/api/endpoints/admin/accounts/create.ts @@ -34,7 +34,8 @@ export const meta = { } }; -export default define(meta, async (ps, me) => { +export default define(meta, async (ps, _me) => { + const me = _me ? await Users.findOneOrFail(_me.id) : null; const noUsers = (await Users.count({ host: null, })) === 0; diff --git a/src/server/api/endpoints/admin/emoji/list.ts b/src/server/api/endpoints/admin/emoji/list.ts index 4f8377796..9e50ce61c 100644 --- a/src/server/api/endpoints/admin/emoji/list.ts +++ b/src/server/api/endpoints/admin/emoji/list.ts @@ -96,9 +96,9 @@ export default define(meta, async (ps) => { emojis = await q.getMany(); emojis = emojis.filter(emoji => - emoji.name.includes(ps.query) || - emoji.aliases.some(a => a.includes(ps.query)) || - emoji.category?.includes(ps.query)); + emoji.name.includes(ps.query!) || + emoji.aliases.some(a => a.includes(ps.query!)) || + emoji.category?.includes(ps.query!)); emojis.splice(ps.limit! + 1); } else { diff --git a/src/server/api/endpoints/admin/queue/deliver-delayed.ts b/src/server/api/endpoints/admin/queue/deliver-delayed.ts index 221aa35e4..d2f8797eb 100644 --- a/src/server/api/endpoints/admin/queue/deliver-delayed.ts +++ b/src/server/api/endpoints/admin/queue/deliver-delayed.ts @@ -1,3 +1,4 @@ +import { URL } from 'url'; import define from '../../../define'; import { deliverQueue } from '../../../../../queue'; diff --git a/src/server/api/endpoints/admin/queue/inbox-delayed.ts b/src/server/api/endpoints/admin/queue/inbox-delayed.ts index 52f67cb51..3599c012a 100644 --- a/src/server/api/endpoints/admin/queue/inbox-delayed.ts +++ b/src/server/api/endpoints/admin/queue/inbox-delayed.ts @@ -1,3 +1,4 @@ +import { URL } from 'url'; import define from '../../../define'; import { inboxQueue } from '../../../../../queue'; diff --git a/src/server/api/endpoints/admin/relays/add.ts b/src/server/api/endpoints/admin/relays/add.ts index 831e05c83..e10bd92c8 100644 --- a/src/server/api/endpoints/admin/relays/add.ts +++ b/src/server/api/endpoints/admin/relays/add.ts @@ -1,3 +1,4 @@ +import { URL } from 'url'; import $ from 'cafy'; import define from '../../../define'; import { addRelay } from '../../../../../services/relay'; diff --git a/src/server/api/endpoints/antennas/create.ts b/src/server/api/endpoints/antennas/create.ts index 9c8d8adf8..04ca21bba 100644 --- a/src/server/api/endpoints/antennas/create.ts +++ b/src/server/api/endpoints/antennas/create.ts @@ -89,7 +89,7 @@ export default define(meta, async (ps, user) => { let userList; let userGroupJoining; - if (ps.src === 'list') { + if (ps.src === 'list' && ps.userListId) { userList = await UserLists.findOne({ id: ps.userListId, userId: user.id, @@ -98,7 +98,7 @@ export default define(meta, async (ps, user) => { if (userList == null) { throw new ApiError(meta.errors.noSuchUserList); } - } else if (ps.src === 'group') { + } else if (ps.src === 'group' && ps.userGroupId) { userGroupJoining = await UserGroupJoinings.findOne({ userGroupId: ps.userGroupId, userId: user.id, diff --git a/src/server/api/endpoints/antennas/update.ts b/src/server/api/endpoints/antennas/update.ts index e23bff9da..17b0f854f 100644 --- a/src/server/api/endpoints/antennas/update.ts +++ b/src/server/api/endpoints/antennas/update.ts @@ -108,7 +108,7 @@ export default define(meta, async (ps, user) => { let userList; let userGroupJoining; - if (ps.src === 'list') { + if (ps.src === 'list' && ps.userListId) { userList = await UserLists.findOne({ id: ps.userListId, userId: user.id, @@ -117,7 +117,7 @@ export default define(meta, async (ps, user) => { if (userList == null) { throw new ApiError(meta.errors.noSuchUserList); } - } else if (ps.src === 'group') { + } else if (ps.src === 'group' && ps.userGroupId) { userGroupJoining = await UserGroupJoinings.findOne({ userGroupId: ps.userGroupId, userId: user.id, diff --git a/src/server/api/endpoints/app/show.ts b/src/server/api/endpoints/app/show.ts index 419e47d07..65f8c7798 100644 --- a/src/server/api/endpoints/app/show.ts +++ b/src/server/api/endpoints/app/show.ts @@ -46,6 +46,6 @@ export default define(meta, async (ps, user, token) => { return await Apps.pack(ap, user, { detail: true, - includeSecret: isSecure && (ap.userId === user.id) + includeSecret: isSecure && (ap.userId === user!.id) }); }); diff --git a/src/server/api/endpoints/blocking/create.ts b/src/server/api/endpoints/blocking/create.ts index 25bc36c6f..60fde8ad7 100644 --- a/src/server/api/endpoints/blocking/create.ts +++ b/src/server/api/endpoints/blocking/create.ts @@ -62,7 +62,7 @@ export const meta = { }; export default define(meta, async (ps, user) => { - const blocker = user; + const blocker = await Users.findOneOrFail(user.id); // 自分自身 if (user.id === ps.userId) { @@ -93,7 +93,7 @@ export default define(meta, async (ps, user) => { noteUserId: blockee.id }); - return await Users.pack(blockee.id, user, { + return await Users.pack(blockee.id, blocker, { detail: true }); }); diff --git a/src/server/api/endpoints/blocking/delete.ts b/src/server/api/endpoints/blocking/delete.ts index 8391fd82f..8caa3efc8 100644 --- a/src/server/api/endpoints/blocking/delete.ts +++ b/src/server/api/endpoints/blocking/delete.ts @@ -126,7 +126,7 @@ export const meta = { }; export default define(meta, async (ps, user) => { - const blocker = user; + const blocker = await Users.findOneOrFail(user.id); // Check if the blockee is yourself if (user.id === ps.userId) { @@ -152,7 +152,7 @@ export default define(meta, async (ps, user) => { // Delete blocking await deleteBlocking(blocker, blockee); - return await Users.pack(blockee.id, user, { + return await Users.pack(blockee.id, blocker, { detail: true }); }); diff --git a/src/server/api/endpoints/channels/timeline.ts b/src/server/api/endpoints/channels/timeline.ts index 5c844bf5c..494221010 100644 --- a/src/server/api/endpoints/channels/timeline.ts +++ b/src/server/api/endpoints/channels/timeline.ts @@ -97,7 +97,7 @@ export default define(meta, async (ps, user) => { const timeline = await query.take(ps.limit!).getMany(); - activeUsersChart.update(user); + if (user) activeUsersChart.update(user); return await Notes.packMany(timeline, user); }); diff --git a/src/server/api/endpoints/clips/create.ts b/src/server/api/endpoints/clips/create.ts index de01aaa5b..fb2a77fe5 100644 --- a/src/server/api/endpoints/clips/create.ts +++ b/src/server/api/endpoints/clips/create.ts @@ -32,14 +32,14 @@ export const meta = { }; export default define(meta, async (ps, user) => { - const clip = await Clips.save({ + const clip = await Clips.insert({ id: genId(), createdAt: new Date(), userId: user.id, name: ps.name, isPublic: ps.isPublic, description: ps.description, - }); + }).then(x => Clips.findOneOrFail(x.identifiers[0])); return await Clips.pack(clip); }); diff --git a/src/server/api/endpoints/drive.ts b/src/server/api/endpoints/drive.ts index a2dd49d32..0100d4da7 100644 --- a/src/server/api/endpoints/drive.ts +++ b/src/server/api/endpoints/drive.ts @@ -34,7 +34,7 @@ export default define(meta, async (ps, user) => { const instance = await fetchMeta(true); // Calculate drive usage - const usage = await DriveFiles.calcDriveUsageOf(user); + const usage = await DriveFiles.calcDriveUsageOf(user.id); return { capacity: 1024 * 1024 * instance.localDriveCapacityMb, diff --git a/src/server/api/endpoints/drive/folders/create.ts b/src/server/api/endpoints/drive/folders/create.ts index 218cecff2..d2c5e00a9 100644 --- a/src/server/api/endpoints/drive/folders/create.ts +++ b/src/server/api/endpoints/drive/folders/create.ts @@ -68,13 +68,13 @@ export default define(meta, async (ps, user) => { } // Create folder - const folder = await DriveFolders.save({ + const folder = await DriveFolders.insert({ id: genId(), createdAt: new Date(), name: ps.name, parentId: parent !== null ? parent.id : null, userId: user.id - }); + }).then(x => DriveFolders.findOneOrFail(x.identifiers[0])); const folderObj = await DriveFolders.pack(folder); diff --git a/src/server/api/endpoints/federation/show-instance.ts b/src/server/api/endpoints/federation/show-instance.ts index ebea14b12..549d7340f 100644 --- a/src/server/api/endpoints/federation/show-instance.ts +++ b/src/server/api/endpoints/federation/show-instance.ts @@ -2,7 +2,6 @@ import $ from 'cafy'; import define from '../../define'; import { Instances } from '../../../../models'; import { toPuny } from '@/misc/convert-host'; -import config from '@/config'; export const meta = { tags: ['federation'], diff --git a/src/server/api/endpoints/games/reversi/games.ts b/src/server/api/endpoints/games/reversi/games.ts index 96ffd31c6..6ac150ef2 100644 --- a/src/server/api/endpoints/games/reversi/games.ts +++ b/src/server/api/endpoints/games/reversi/games.ts @@ -140,7 +140,7 @@ export default define(meta, async (ps, user) => { const query = makePaginationQuery(ReversiGames.createQueryBuilder('game'), ps.sinceId, ps.untilId) .andWhere('game.isStarted = TRUE'); - if (ps.my) { + if (ps.my && user) { query.andWhere(new Brackets(qb => { qb .where('game.user1Id = :userId', { userId: user.id }) .orWhere('game.user2Id = :userId', { userId: user.id }); diff --git a/src/server/api/endpoints/games/reversi/match.ts b/src/server/api/endpoints/games/reversi/match.ts index fca4b0964..2c4ec2ebd 100644 --- a/src/server/api/endpoints/games/reversi/match.ts +++ b/src/server/api/endpoints/games/reversi/match.ts @@ -72,7 +72,7 @@ export default define(meta, async (ps, user) => { isLlotheo: false } as Partial); - publishReversiStream(exist.parentId, 'matched', await ReversiGames.pack(game, exist.parentId)); + publishReversiStream(exist.parentId, 'matched', await ReversiGames.pack(game, { id: exist.parentId })); const other = await ReversiMatchings.count({ childId: user.id diff --git a/src/server/api/endpoints/i/pin.ts b/src/server/api/endpoints/i/pin.ts index a4f333476..4bed56693 100644 --- a/src/server/api/endpoints/i/pin.ts +++ b/src/server/api/endpoints/i/pin.ts @@ -61,7 +61,7 @@ export default define(meta, async (ps, user) => { throw e; }); - return await Users.pack(user, user, { + return await Users.pack(user.id, user, { detail: true }); }); diff --git a/src/server/api/endpoints/i/registry/scopes.ts b/src/server/api/endpoints/i/registry/scopes.ts index 8b0e1a7fd..baf3ebdec 100644 --- a/src/server/api/endpoints/i/registry/scopes.ts +++ b/src/server/api/endpoints/i/registry/scopes.ts @@ -1,4 +1,3 @@ -import $ from 'cafy'; import define from '../../../define'; import { RegistryItems } from '../../../../../models'; diff --git a/src/server/api/endpoints/i/unpin.ts b/src/server/api/endpoints/i/unpin.ts index d0757af6b..bb8b36a16 100644 --- a/src/server/api/endpoints/i/unpin.ts +++ b/src/server/api/endpoints/i/unpin.ts @@ -47,7 +47,7 @@ export default define(meta, async (ps, user) => { throw e; }); - return await Users.pack(user, user, { + return await Users.pack(user.id, user, { detail: true }); }); diff --git a/src/server/api/endpoints/i/update.ts b/src/server/api/endpoints/i/update.ts index 8acc38f89..0554fe76f 100644 --- a/src/server/api/endpoints/i/update.ts +++ b/src/server/api/endpoints/i/update.ts @@ -205,7 +205,8 @@ export const meta = { } }; -export default define(meta, async (ps, user, token) => { +export default define(meta, async (ps, _user, token) => { + const user = await Users.findOneOrFail(_user.id); const isSecure = token == null; const updates = {} as Partial; diff --git a/src/server/api/endpoints/notes/unrenote.ts b/src/server/api/endpoints/notes/unrenote.ts index 6fbdd5b02..0233b9db1 100644 --- a/src/server/api/endpoints/notes/unrenote.ts +++ b/src/server/api/endpoints/notes/unrenote.ts @@ -5,7 +5,7 @@ import define from '../../define'; import * as ms from 'ms'; import { getNote } from '../../common/getters'; import { ApiError } from '../../error'; -import { Notes } from '../../../../models'; +import { Notes, Users } from '../../../../models'; export const meta = { desc: { @@ -55,6 +55,6 @@ export default define(meta, async (ps, user) => { }); for (const note of renotes) { - deleteNote(user, note); + deleteNote(await Users.findOneOrFail(user.id), note); } }); diff --git a/src/server/api/endpoints/page-push.ts b/src/server/api/endpoints/page-push.ts index e2b84c9e0..9ec9f9184 100644 --- a/src/server/api/endpoints/page-push.ts +++ b/src/server/api/endpoints/page-push.ts @@ -43,7 +43,7 @@ export default define(meta, async (ps, user) => { event: ps.event, var: ps.var, userId: user.id, - user: await Users.pack(user, page.userId, { + user: await Users.pack(user.id, { id: page.userId }, { detail: true }) }); diff --git a/src/server/api/endpoints/users/groups/create.ts b/src/server/api/endpoints/users/groups/create.ts index 2c397b9cd..0966ef781 100644 --- a/src/server/api/endpoints/users/groups/create.ts +++ b/src/server/api/endpoints/users/groups/create.ts @@ -31,12 +31,12 @@ export const meta = { }; export default define(meta, async (ps, user) => { - const userGroup = await UserGroups.save({ + const userGroup = await UserGroups.insert({ id: genId(), createdAt: new Date(), userId: user.id, name: ps.name, - } as UserGroup); + } as UserGroup).then(x => UserGroups.findOneOrFail(x.identifiers[0])); // Push the owner await UserGroupJoinings.insert({ diff --git a/src/server/api/endpoints/users/groups/invite.ts b/src/server/api/endpoints/users/groups/invite.ts index 8b5ee1224..00d4932f5 100644 --- a/src/server/api/endpoints/users/groups/invite.ts +++ b/src/server/api/endpoints/users/groups/invite.ts @@ -96,12 +96,12 @@ export default define(meta, async (ps, me) => { throw new ApiError(meta.errors.alreadyInvited); } - const invitation = await UserGroupInvitations.save({ + const invitation = await UserGroupInvitations.insert({ id: genId(), createdAt: new Date(), userId: user.id, userGroupId: userGroup.id - } as UserGroupInvitation); + } as UserGroupInvitation).then(x => UserGroupInvitations.findOneOrFail(x.identifiers[0])); // 通知を作成 createNotification(user.id, 'groupInvited', { diff --git a/src/server/api/endpoints/users/lists/create.ts b/src/server/api/endpoints/users/lists/create.ts index b1714fc32..9891a6dae 100644 --- a/src/server/api/endpoints/users/lists/create.ts +++ b/src/server/api/endpoints/users/lists/create.ts @@ -30,12 +30,12 @@ export const meta = { }; export default define(meta, async (ps, user) => { - const userList = await UserLists.save({ + const userList = await UserLists.insert({ id: genId(), createdAt: new Date(), userId: user.id, name: ps.name, - } as UserList); + } as UserList).then(x => UserLists.findOneOrFail(x.identifiers[0])); return await UserLists.pack(userList); }); diff --git a/src/server/api/endpoints/users/show.ts b/src/server/api/endpoints/users/show.ts index 21a0cb747..11d36d2ae 100644 --- a/src/server/api/endpoints/users/show.ts +++ b/src/server/api/endpoints/users/show.ts @@ -6,6 +6,7 @@ import { ApiError } from '../../error'; import { ID } from '@/misc/cafy-id'; import { Users } from '../../../../models'; import { In } from 'typeorm'; +import { User } from '@/models/entities/user'; export const meta = { desc: { @@ -81,9 +82,9 @@ export default define(meta, async (ps, me) => { }); // リクエストされた通りに並べ替え - const _users = []; + const _users: User[] = []; for (const id of ps.userIds) { - _users.push(users.find(x => x.id === id)); + _users.push(users.find(x => x.id === id)!); } return await Promise.all(_users.map(u => Users.pack(u, me, { diff --git a/src/server/api/limiter.ts b/src/server/api/limiter.ts index b639ef08b..1e8715a7c 100644 --- a/src/server/api/limiter.ts +++ b/src/server/api/limiter.ts @@ -7,7 +7,7 @@ import Logger from '../../services/logger'; const logger = new Logger('limiter'); -export default (endpoint: IEndpoint, user: User) => new Promise((ok, reject) => { +export default (endpoint: IEndpoint, user: User) => new Promise((ok, reject) => { const limitation = endpoint.meta.limit!; const key = limitation.hasOwnProperty('key') diff --git a/src/server/api/stream/channels/games/reversi.ts b/src/server/api/stream/channels/games/reversi.ts index 3db338386..e0e41d9ac 100644 --- a/src/server/api/stream/channels/games/reversi.ts +++ b/src/server/api/stream/channels/games/reversi.ts @@ -26,7 +26,7 @@ export default class extends Channel { childId: body.id }); if (matching == null) return; - publishMainStream(matching.childId, 'reversiInvited', await ReversiMatchings.pack(matching, matching.childId)); + publishMainStream(matching.childId, 'reversiInvited', await ReversiMatchings.pack(matching, { id: matching.childId })); break; } } diff --git a/src/server/api/stream/channels/global-timeline.ts b/src/server/api/stream/channels/global-timeline.ts index e47291de5..c59eb3777 100644 --- a/src/server/api/stream/channels/global-timeline.ts +++ b/src/server/api/stream/channels/global-timeline.ts @@ -42,8 +42,9 @@ export default class extends Channel { // 関係ない返信は除外 if (note.reply) { + const reply = note.reply as PackedNote; // 「チャンネル接続主への返信」でもなければ、「チャンネル接続主が行った返信」でもなければ、「投稿者の投稿者自身への返信」でもない場合 - if (note.reply.userId !== this.user!.id && note.userId !== this.user!.id && note.reply.userId !== note.userId) return; + if (reply.userId !== this.user!.id && note.userId !== this.user!.id && reply.userId !== note.userId) return; } // 流れてきたNoteがミュートしているユーザーが関わるものだったら無視する diff --git a/src/server/api/stream/channels/home-timeline.ts b/src/server/api/stream/channels/home-timeline.ts index a37058067..b1091a46e 100644 --- a/src/server/api/stream/channels/home-timeline.ts +++ b/src/server/api/stream/channels/home-timeline.ts @@ -50,8 +50,9 @@ export default class extends Channel { // 関係ない返信は除外 if (note.reply) { + const reply = note.reply as PackedNote; // 「チャンネル接続主への返信」でもなければ、「チャンネル接続主が行った返信」でもなければ、「投稿者の投稿者自身への返信」でもない場合 - if (note.reply.userId !== this.user!.id && note.userId !== this.user!.id && note.reply.userId !== note.userId) return; + if (reply.userId !== this.user!.id && note.userId !== this.user!.id && reply.userId !== note.userId) return; } // 流れてきたNoteがミュートしているユーザーが関わるものだったら無視する diff --git a/src/server/api/stream/channels/hybrid-timeline.ts b/src/server/api/stream/channels/hybrid-timeline.ts index 678554a01..d769a2437 100644 --- a/src/server/api/stream/channels/hybrid-timeline.ts +++ b/src/server/api/stream/channels/hybrid-timeline.ts @@ -59,8 +59,9 @@ export default class extends Channel { // 関係ない返信は除外 if (note.reply) { + const reply = note.reply as PackedNote; // 「チャンネル接続主への返信」でもなければ、「チャンネル接続主が行った返信」でもなければ、「投稿者の投稿者自身への返信」でもない場合 - if (note.reply.userId !== this.user!.id && note.userId !== this.user!.id && note.reply.userId !== note.userId) return; + if (reply.userId !== this.user!.id && note.userId !== this.user!.id && reply.userId !== note.userId) return; } // 流れてきたNoteがミュートしているユーザーが関わるものだったら無視する diff --git a/src/server/api/stream/channels/local-timeline.ts b/src/server/api/stream/channels/local-timeline.ts index 2c70f4695..aa0b6c402 100644 --- a/src/server/api/stream/channels/local-timeline.ts +++ b/src/server/api/stream/channels/local-timeline.ts @@ -44,8 +44,9 @@ export default class extends Channel { // 関係ない返信は除外 if (note.reply) { + const reply = note.reply as PackedNote; // 「チャンネル接続主への返信」でもなければ、「チャンネル接続主が行った返信」でもなければ、「投稿者の投稿者自身への返信」でもない場合 - if (note.reply.userId !== this.user!.id && note.userId !== this.user!.id && note.reply.userId !== note.userId) return; + if (reply.userId !== this.user!.id && note.userId !== this.user!.id && reply.userId !== note.userId) return; } // 流れてきたNoteがミュートしているユーザーが関わるものだったら無視する diff --git a/src/server/api/stream/index.ts b/src/server/api/stream/index.ts index 1dcc31d32..647b890ff 100644 --- a/src/server/api/stream/index.ts +++ b/src/server/api/stream/index.ts @@ -156,8 +156,8 @@ export default class Connection { }; add(note); - if (note.reply) add(note.reply); - if (note.renote) add(note.renote); + if (note.reply) add(note.reply as PackedNote); + if (note.renote) add(note.renote as PackedNote); } @autobind diff --git a/src/server/index.ts b/src/server/index.ts index f46e851ba..3b43aa767 100644 --- a/src/server/index.ts +++ b/src/server/index.ts @@ -89,7 +89,7 @@ router.get('/verify-email/:code', async ctx => { emailVerifyCode: null }); - publishMainStream(profile.userId, 'meUpdated', await Users.pack(profile.userId, profile.userId, { + publishMainStream(profile.userId, 'meUpdated', await Users.pack(profile.userId, { id: profile.userId }, { detail: true, includeSecrets: true })); diff --git a/src/services/add-note-to-antenna.ts b/src/services/add-note-to-antenna.ts index 108957a89..56d014915 100644 --- a/src/services/add-note-to-antenna.ts +++ b/src/services/add-note-to-antenna.ts @@ -6,7 +6,7 @@ import { isMutedUserRelated } from '@/misc/is-muted-user-related'; import { publishAntennaStream, publishMainStream } from './stream'; import { User } from '../models/entities/user'; -export async function addNoteToAntenna(antenna: Antenna, note: Note, noteUser: User) { +export async function addNoteToAntenna(antenna: Antenna, note: Note, noteUser: { id: User['id']; }) { // 通知しない設定になっているか、自分自身の投稿なら既読にする const read = !antenna.notify || (antenna.userId === noteUser.id); diff --git a/src/services/chart/charts/classes/active-users.ts b/src/services/chart/charts/classes/active-users.ts index 95ed23a55..e732b25d2 100644 --- a/src/services/chart/charts/classes/active-users.ts +++ b/src/services/chart/charts/classes/active-users.ts @@ -35,7 +35,7 @@ export default class ActiveUsersChart extends Chart { } @autobind - public async update(user: User) { + public async update(user: { id: User['id'], host: User['host'] }) { const update: Obj = { users: [user.id] }; diff --git a/src/services/chart/charts/classes/hashtag.ts b/src/services/chart/charts/classes/hashtag.ts index c3eb42497..98d5421c9 100644 --- a/src/services/chart/charts/classes/hashtag.ts +++ b/src/services/chart/charts/classes/hashtag.ts @@ -35,7 +35,7 @@ export default class HashtagChart extends Chart { } @autobind - public async update(hashtag: string, user: User) { + public async update(hashtag: string, user: { id: User['id'], host: User['host'] }) { const update: Obj = { users: [user.id] }; diff --git a/src/services/chart/charts/classes/per-user-following.ts b/src/services/chart/charts/classes/per-user-following.ts index 6e069fb0d..5972d3759 100644 --- a/src/services/chart/charts/classes/per-user-following.ts +++ b/src/services/chart/charts/classes/per-user-following.ts @@ -100,7 +100,7 @@ export default class PerUserFollowingChart extends Chart { } @autobind - public async update(follower: User, followee: User, isFollow: boolean) { + public async update(follower: { id: User['id']; host: User['host']; }, followee: { id: User['id']; host: User['host']; }, isFollow: boolean) { const update: Obj = {}; update.total = isFollow ? 1 : -1; diff --git a/src/services/chart/charts/classes/per-user-notes.ts b/src/services/chart/charts/classes/per-user-notes.ts index f29a75019..7f93965ad 100644 --- a/src/services/chart/charts/classes/per-user-notes.ts +++ b/src/services/chart/charts/classes/per-user-notes.ts @@ -46,7 +46,7 @@ export default class PerUserNotesChart extends Chart { } @autobind - public async update(user: User, note: Note, isAdditional: boolean) { + public async update(user: { id: User['id'] }, note: Note, isAdditional: boolean) { const update: Obj = { diffs: {} }; diff --git a/src/services/chart/charts/classes/per-user-reactions.ts b/src/services/chart/charts/classes/per-user-reactions.ts index d2d6b42e5..69ed81cd4 100644 --- a/src/services/chart/charts/classes/per-user-reactions.ts +++ b/src/services/chart/charts/classes/per-user-reactions.ts @@ -36,7 +36,7 @@ export default class PerUserReactionsChart extends Chart { } @autobind - public async update(user: User, note: Note) { + public async update(user: { id: User['id'], host: User['host'] }, note: Note) { this.inc({ [Users.isLocalUser(user) ? 'local' : 'remote']: { count: 1 } }, note.userId); diff --git a/src/services/chart/charts/classes/users.ts b/src/services/chart/charts/classes/users.ts index d50db1703..8f208de6d 100644 --- a/src/services/chart/charts/classes/users.ts +++ b/src/services/chart/charts/classes/users.ts @@ -59,7 +59,7 @@ export default class UsersChart extends Chart { } @autobind - public async update(user: User, isAdditional: boolean) { + public async update(user: { id: User['id'], host: User['host'] }, isAdditional: boolean) { const update: Obj = {}; update.total = isAdditional ? 1 : -1; diff --git a/src/services/drive/add-file.ts b/src/services/drive/add-file.ts index f502c6315..ad9f753e7 100644 --- a/src/services/drive/add-file.ts +++ b/src/services/drive/add-file.ts @@ -302,7 +302,7 @@ async function deleteOldFile(user: IRemoteUser) { * @return Created drive file */ export default async function( - user: User | null, + user: { id: User['id']; host: User['host'] } | null, path: string, name: string | null = null, comment: string | null = null, @@ -347,7 +347,7 @@ export default async function( throw new Error('no-free-space'); } else { // (アバターまたはバナーを含まず)最も古いファイルを削除する - deleteOldFile(user as IRemoteUser); + deleteOldFile(await Users.findOneOrFail(user.id) as IRemoteUser); } } } diff --git a/src/services/drive/s3.ts b/src/services/drive/s3.ts index 8bd51ce02..e75937861 100644 --- a/src/services/drive/s3.ts +++ b/src/services/drive/s3.ts @@ -1,3 +1,4 @@ +import { URL } from 'url'; import * as S3 from 'aws-sdk/clients/s3'; import { Meta } from '../../models/entities/meta'; import { getAgentByUrl } from '@/misc/fetch'; diff --git a/src/services/drive/upload-from-url.ts b/src/services/drive/upload-from-url.ts index db2c20b3a..2f4c5aeea 100644 --- a/src/services/drive/upload-from-url.ts +++ b/src/services/drive/upload-from-url.ts @@ -1,3 +1,4 @@ +import { URL } from 'url'; import create from './add-file'; import { User } from '../../models/entities/user'; import { driveLogger } from './logger'; @@ -11,7 +12,7 @@ const logger = driveLogger.createSubLogger('downloader'); export default async ( url: string, - user: User | null, + user: { id: User['id']; host: User['host'] } | null, folderId: DriveFolder['id'] | null = null, uri: string | null = null, sensitive = false, diff --git a/src/services/following/create.ts b/src/services/following/create.ts index a759bef0f..de12285fc 100644 --- a/src/services/following/create.ts +++ b/src/services/following/create.ts @@ -17,7 +17,7 @@ import { isDuplicateKeyValueError } from '@/misc/is-duplicate-key-value-error'; const logger = new Logger('following/create'); -export async function insertFollowingDoc(followee: User, follower: User) { +export async function insertFollowingDoc(followee: { id: User['id']; host: User['host']; uri: User['host']; inbox: User['inbox']; sharedInbox: User['sharedInbox'] }, follower: { id: User['id']; host: User['host']; uri: User['host']; inbox: User['inbox']; sharedInbox: User['sharedInbox'] }) { if (follower.id === followee.id) return; let alreadyFollowed = false; @@ -86,7 +86,7 @@ export async function insertFollowingDoc(followee: User, follower: User) { // Publish follow event if (Users.isLocalUser(follower)) { - Users.pack(followee, follower, { + Users.pack(followee.id, follower, { detail: true }).then(packed => { publishUserEvent(follower.id, 'follow', packed); @@ -96,7 +96,7 @@ export async function insertFollowingDoc(followee: User, follower: User) { // Publish followed event if (Users.isLocalUser(followee)) { - Users.pack(follower, followee).then(packed => publishMainStream(followee.id, 'followed', packed)); + Users.pack(follower.id, followee).then(packed => publishMainStream(followee.id, 'followed', packed)); // 通知を作成 createNotification(followee.id, 'follow', { @@ -105,7 +105,12 @@ export async function insertFollowingDoc(followee: User, follower: User) { } } -export default async function(follower: User, followee: User, requestId?: string) { +export default async function(_follower: { id: User['id'] }, _followee: { id: User['id'] }, requestId?: string) { + const [follower, followee] = await Promise.all([ + Users.findOneOrFail(_follower.id), + Users.findOneOrFail(_followee.id) + ]); + // check blocking const [blocking, blocked] = await Promise.all([ Blockings.findOne({ diff --git a/src/services/following/delete.ts b/src/services/following/delete.ts index 32c47ea7f..d0df0de6a 100644 --- a/src/services/following/delete.ts +++ b/src/services/following/delete.ts @@ -11,7 +11,7 @@ import { instanceChart, perUserFollowingChart } from '../chart'; const logger = new Logger('following/delete'); -export default async function(follower: User, followee: User, silent = false) { +export default async function(follower: { id: User['id']; host: User['host']; uri: User['host']; inbox: User['inbox']; sharedInbox: User['sharedInbox']; }, followee: { id: User['id']; host: User['host']; uri: User['host']; inbox: User['inbox']; sharedInbox: User['sharedInbox']; }, silent = false) { const following = await Followings.findOne({ followerId: follower.id, followeeId: followee.id @@ -28,7 +28,7 @@ export default async function(follower: User, followee: User, silent = false) { // Publish unfollow event if (!silent && Users.isLocalUser(follower)) { - Users.pack(followee, follower, { + Users.pack(followee.id, follower, { detail: true }).then(packed => { publishUserEvent(follower.id, 'unfollow', packed); @@ -42,7 +42,7 @@ export default async function(follower: User, followee: User, silent = false) { } } -export async function decrementFollowing(follower: User, followee: User) { +export async function decrementFollowing(follower: { id: User['id']; host: User['host']; }, followee: { id: User['id']; host: User['host']; }) { //#region Decrement following count Users.decrement({ id: follower.id }, 'followingCount', 1); //#endregion diff --git a/src/services/following/requests/accept-all.ts b/src/services/following/requests/accept-all.ts index da3996534..5fc70fcf1 100644 --- a/src/services/following/requests/accept-all.ts +++ b/src/services/following/requests/accept-all.ts @@ -6,7 +6,7 @@ import { FollowRequests, Users } from '../../../models'; * 指定したユーザー宛てのフォローリクエストをすべて承認 * @param user ユーザー */ -export default async function(user: User) { +export default async function(user: { id: User['id']; host: User['host']; uri: User['host']; inbox: User['inbox']; sharedInbox: User['sharedInbox']; }) { const requests = await FollowRequests.find({ followeeId: user.id }); diff --git a/src/services/following/requests/accept.ts b/src/services/following/requests/accept.ts index 470458d5d..4277a1a1e 100644 --- a/src/services/following/requests/accept.ts +++ b/src/services/following/requests/accept.ts @@ -8,7 +8,7 @@ import { User, ILocalUser } from '../../../models/entities/user'; import { FollowRequests, Users } from '../../../models'; import { IdentifiableError } from '@/misc/identifiable-error'; -export default async function(followee: User, follower: User) { +export default async function(followee: { id: User['id']; host: User['host']; uri: User['host']; inbox: User['inbox']; sharedInbox: User['sharedInbox']; }, follower: User) { const request = await FollowRequests.findOne({ followeeId: followee.id, followerId: follower.id @@ -20,12 +20,12 @@ export default async function(followee: User, follower: User) { await insertFollowingDoc(followee, follower); - if (Users.isRemoteUser(follower)) { - const content = renderActivity(renderAccept(renderFollow(follower, followee, request.requestId!), followee as ILocalUser)); - deliver(followee as ILocalUser, content, follower.inbox); + if (Users.isRemoteUser(follower) && Users.isLocalUser(followee)) { + const content = renderActivity(renderAccept(renderFollow(follower, followee, request.requestId!), followee)); + deliver(followee, content, follower.inbox); } - Users.pack(followee, followee, { + Users.pack(followee.id, followee, { detail: true }).then(packed => publishMainStream(followee.id, 'meUpdated', packed)); } diff --git a/src/services/following/requests/cancel.ts b/src/services/following/requests/cancel.ts index 82f6efc40..53c20088c 100644 --- a/src/services/following/requests/cancel.ts +++ b/src/services/following/requests/cancel.ts @@ -7,10 +7,13 @@ import { IdentifiableError } from '@/misc/identifiable-error'; import { User, ILocalUser } from '../../../models/entities/user'; import { Users, FollowRequests } from '../../../models'; -export default async function(followee: User, follower: User) { +export default async function(followee: { id: User['id']; host: User['host']; uri: User['host']; inbox: User['inbox'] }, follower: { id: User['id']; host: User['host']; uri: User['host'] }) { if (Users.isRemoteUser(followee)) { const content = renderActivity(renderUndo(renderFollow(follower, followee), follower)); - deliver(follower as ILocalUser, content, followee.inbox); + + if (Users.isLocalUser(follower)) { // 本来このチェックは不要だけどTSに怒られるので + deliver(follower, content, followee.inbox); + } } const request = await FollowRequests.findOne({ @@ -27,7 +30,7 @@ export default async function(followee: User, follower: User) { followerId: follower.id }); - Users.pack(followee, followee, { + Users.pack(followee.id, followee, { detail: true }).then(packed => publishMainStream(followee.id, 'meUpdated', packed)); } diff --git a/src/services/following/requests/create.ts b/src/services/following/requests/create.ts index bab47caaa..584591b00 100644 --- a/src/services/following/requests/create.ts +++ b/src/services/following/requests/create.ts @@ -7,7 +7,7 @@ import { Blockings, FollowRequests, Users } from '../../../models'; import { genId } from '@/misc/gen-id'; import { createNotification } from '../../create-notification'; -export default async function(follower: User, followee: User, requestId?: string) { +export default async function(follower: { id: User['id']; host: User['host']; uri: User['host']; inbox: User['inbox']; sharedInbox: User['sharedInbox']; }, followee: { id: User['id']; host: User['host']; uri: User['host']; inbox: User['inbox']; sharedInbox: User['sharedInbox']; }, requestId?: string) { if (follower.id === followee.id) return; // check blocking @@ -43,9 +43,9 @@ export default async function(follower: User, followee: User, requestId?: string // Publish receiveRequest event if (Users.isLocalUser(followee)) { - Users.pack(follower, followee).then(packed => publishMainStream(followee.id, 'receiveFollowRequest', packed)); + Users.pack(follower.id, followee).then(packed => publishMainStream(followee.id, 'receiveFollowRequest', packed)); - Users.pack(followee, followee, { + Users.pack(followee.id, followee, { detail: true }).then(packed => publishMainStream(followee.id, 'meUpdated', packed)); diff --git a/src/services/following/requests/reject.ts b/src/services/following/requests/reject.ts index d8d378808..45effc580 100644 --- a/src/services/following/requests/reject.ts +++ b/src/services/following/requests/reject.ts @@ -7,15 +7,15 @@ import { User, ILocalUser } from '../../../models/entities/user'; import { Users, FollowRequests, Followings } from '../../../models'; import { decrementFollowing } from '../delete'; -export default async function(followee: User, follower: User) { - if (Users.isRemoteUser(follower)) { +export default async function(followee: { id: User['id']; host: User['host']; uri: User['host'] }, follower: User) { + if (Users.isRemoteUser(follower) && Users.isLocalUser(followee)) { const request = await FollowRequests.findOne({ followeeId: followee.id, followerId: follower.id }); - const content = renderActivity(renderReject(renderFollow(follower, followee, request!.requestId!), followee as ILocalUser)); - deliver(followee as ILocalUser, content, follower.inbox); + const content = renderActivity(renderReject(renderFollow(follower, followee, request!.requestId!), followee)); + deliver(followee, content, follower.inbox); } const request = await FollowRequests.findOne({ @@ -37,7 +37,7 @@ export default async function(followee: User, follower: User) { } } - Users.pack(followee, follower, { + Users.pack(followee.id, follower, { detail: true }).then(packed => { publishUserEvent(follower.id, 'unfollow', packed); diff --git a/src/services/i/pin.ts b/src/services/i/pin.ts index 98e9c22df..e3a73b5b9 100644 --- a/src/services/i/pin.ts +++ b/src/services/i/pin.ts @@ -16,7 +16,7 @@ import { deliverToRelays } from '../relay'; * @param user * @param noteId */ -export async function addPinned(user: User, noteId: Note['id']) { +export async function addPinned(user: { id: User['id']; host: User['host']; }, noteId: Note['id']) { // Fetch pinee const note = await Notes.findOne({ id: noteId, @@ -55,7 +55,7 @@ export async function addPinned(user: User, noteId: Note['id']) { * @param user * @param noteId */ -export async function removePinned(user: User, noteId: Note['id']) { +export async function removePinned(user: { id: User['id']; host: User['host']; }, noteId: Note['id']) { // Fetch unpinee const note = await Notes.findOne({ id: noteId, diff --git a/src/services/insert-moderation-log.ts b/src/services/insert-moderation-log.ts index 1e04ddf54..8ba64fa34 100644 --- a/src/services/insert-moderation-log.ts +++ b/src/services/insert-moderation-log.ts @@ -1,8 +1,8 @@ -import { ILocalUser } from '../models/entities/user'; import { ModerationLogs } from '../models'; import { genId } from '@/misc/gen-id'; +import { User } from '@/models/entities/user'; -export async function insertModerationLog(moderator: ILocalUser, type: string, info?: Record) { +export async function insertModerationLog(moderator: { id: User['id'] }, type: string, info?: Record) { await ModerationLogs.insert({ id: genId(), createdAt: new Date(), diff --git a/src/services/messages/create.ts b/src/services/messages/create.ts index 3a5e7a7c9..f84729cde 100644 --- a/src/services/messages/create.ts +++ b/src/services/messages/create.ts @@ -13,7 +13,7 @@ import renderCreate from '../../remote/activitypub/renderer/create'; import { renderActivity } from '../../remote/activitypub/renderer'; import { deliver } from '../../queue'; -export async function createMessage(user: User, recipientUser: User | undefined, recipientGroup: UserGroup | undefined, text: string | undefined, file: DriveFile | null, uri?: string) { +export async function createMessage(user: { id: User['id']; host: User['host']; }, recipientUser: User | undefined, recipientGroup: UserGroup | undefined, text: string | undefined, file: DriveFile | null, uri?: string) { const message = { id: genId(), createdAt: new Date(), diff --git a/src/services/note/create.ts b/src/services/note/create.ts index 23ccf9042..64d5513ec 100644 --- a/src/services/note/create.ts +++ b/src/services/note/create.ts @@ -38,14 +38,14 @@ import { getAntennas } from '@/misc/antenna-cache'; type NotificationType = 'reply' | 'renote' | 'quote' | 'mention'; class NotificationManager { - private notifier: User; + private notifier: { id: User['id']; }; private note: Note; private queue: { target: ILocalUser['id']; reason: NotificationType; }[]; - constructor(notifier: User, note: Note) { + constructor(notifier: { id: User['id']; }, note: Note) { this.notifier = notifier; this.note = note; this.queue = []; @@ -112,7 +112,7 @@ type Option = { app?: App | null; }; -export default async (user: User, data: Option, silent = false) => new Promise(async (res, rej) => { +export default async (user: { id: User['id']; username: User['username']; host: User['host']; isSilenced: User['isSilenced']; }, data: Option, silent = false) => new Promise(async (res, rej) => { // チャンネル外にリプライしたら対象のスコープに合わせる // (クライアントサイドでやっても良い処理だと思うけどとりあえずサーバーサイドで) if (data.reply && data.channel && data.reply.channelId !== data.channel.id) { @@ -420,7 +420,7 @@ export default async (user: User, data: Option, silent = false) => new Promise): Promise { +async function extractMentionedUsers(user: { host: User['host']; }, tokens: ReturnType): Promise { if (tokens == null) return []; const mentions = extractMentions(tokens); diff --git a/src/services/note/reaction/create.ts b/src/services/note/reaction/create.ts index 952927f19..e2e7fc54e 100644 --- a/src/services/note/reaction/create.ts +++ b/src/services/note/reaction/create.ts @@ -14,7 +14,7 @@ import deleteReaction from './delete'; import { isDuplicateKeyValueError } from '@/misc/is-duplicate-key-value-error'; import { NoteReaction } from '../../../models/entities/note-reaction'; -export default async (user: User, note: Note, reaction?: string) => { +export default async (user: { id: User['id']; host: User['host']; }, note: Note, reaction?: string) => { // TODO: cache reaction = await toDbReaction(reaction, user.host); diff --git a/src/services/note/reaction/delete.ts b/src/services/note/reaction/delete.ts index c0787eee7..712031fa8 100644 --- a/src/services/note/reaction/delete.ts +++ b/src/services/note/reaction/delete.ts @@ -9,7 +9,7 @@ import { Note } from '../../../models/entities/note'; import { NoteReactions, Users, Notes } from '../../../models'; import { decodeReaction } from '@/misc/reaction-lib'; -export default async (user: User, note: Note) => { +export default async (user: { id: User['id']; host: User['host']; }, note: Note) => { // if already unreacted const exist = await NoteReactions.findOne({ noteId: note.id, diff --git a/src/services/relay.ts b/src/services/relay.ts index 5b1d1c847..a2cc711bd 100644 --- a/src/services/relay.ts +++ b/src/services/relay.ts @@ -3,7 +3,7 @@ import { renderFollowRelay } from '../remote/activitypub/renderer/follow-relay'; import { renderActivity, attachLdSignature } from '../remote/activitypub/renderer'; import renderUndo from '../remote/activitypub/renderer/undo'; import { deliver } from '../queue'; -import { ILocalUser } from '../models/entities/user'; +import { ILocalUser, User } from '../models/entities/user'; import { Users, Relays } from '../models'; import { genId } from '@/misc/gen-id'; @@ -75,7 +75,7 @@ export async function relayRejected(id: string) { return JSON.stringify(result); } -export async function deliverToRelays(user: ILocalUser, activity: any) { +export async function deliverToRelays(user: { id: User['id']; host: null; }, activity: any) { if (activity == null) return; const relays = await Relays.find({ diff --git a/src/services/suspend-user.ts b/src/services/suspend-user.ts index 597433d6d..c868c0130 100644 --- a/src/services/suspend-user.ts +++ b/src/services/suspend-user.ts @@ -6,7 +6,7 @@ import { User } from '../models/entities/user'; import { Users, Followings } from '../models'; import { Not, IsNull } from 'typeorm'; -export async function doPostSuspend(user: User) { +export async function doPostSuspend(user: { id: User['id']; host: User['host'] }) { if (Users.isLocalUser(user)) { // 知り得る全SharedInboxにDelete配信 const content = renderActivity(renderDelete(`${config.url}/users/${user.id}`, user)); @@ -28,7 +28,7 @@ export async function doPostSuspend(user: User) { } for (const inbox of queue) { - deliver(user as any, content, inbox); + deliver(user, content, inbox); } } } diff --git a/src/services/update-hashtag.ts b/src/services/update-hashtag.ts index e9508be33..6b7208f00 100644 --- a/src/services/update-hashtag.ts +++ b/src/services/update-hashtag.ts @@ -5,7 +5,7 @@ import { genId } from '@/misc/gen-id'; import { Hashtag } from '../models/entities/hashtag'; import { normalizeForSearch } from '@/misc/normalize-for-search'; -export async function updateHashtags(user: User, tags: string[]) { +export async function updateHashtags(user: { id: User['id']; host: User['host']; }, tags: string[]) { for (const tag of tags) { await updateHashtag(user, tag); } @@ -21,7 +21,7 @@ export async function updateUsertags(user: User, tags: string[]) { } } -export async function updateHashtag(user: User, tag: string, isUserAttached = false, inc = true) { +export async function updateHashtag(user: { id: User['id']; host: User['host']; }, tag: string, isUserAttached = false, inc = true) { tag = normalizeForSearch(tag); const index = await Hashtags.findOne({ name: tag }); diff --git a/src/tsconfig.json b/src/tsconfig.json index 6992d6437..9389008ee 100644 --- a/src/tsconfig.json +++ b/src/tsconfig.json @@ -5,7 +5,7 @@ "noImplicitAny": true, "noImplicitReturns": true, "noUnusedParameters": false, - "noUnusedLocals": true, + "noUnusedLocals": false, "noFallthroughCasesInSwitch": true, "declaration": false, "sourceMap": true,