forked from misskey-dev/misskey
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- DB에 새로운 키들 추가 - 마이그레이션 새로 만들었다 - API에서 나올 JSON스키마 설계 (초안) - NoteHistoryEntityService (초안) - NoteUpdateService 에서 NoteHistory를 기록하도록 호출 추가
- Loading branch information
Showing
7 changed files
with
222 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
114 changes: 114 additions & 0 deletions
114
packages/backend/src/core/entities/NoteHistoryEntityService.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
/* | ||
* SPDX-FileCopyrightText: syuilo and misskey-project | ||
* SPDX-License-Identifier: AGPL-3.0-only | ||
*/ | ||
|
||
import { Inject, Injectable } from '@nestjs/common'; | ||
import { In } from 'typeorm'; | ||
import { ModuleRef } from '@nestjs/core'; | ||
import { DI } from '@/di-symbols.js'; | ||
import type { Packed } from '@/misc/json-schema.js'; | ||
import { awaitAll } from '@/misc/prelude/await-all.js'; | ||
import type { MiUser } from '@/models/User.js'; | ||
import type { MiNote } from '@/models/Note.js'; | ||
import type { UsersRepository, NotesRepository, FollowingsRepository, PollsRepository, PollVotesRepository, NoteReactionsRepository, ChannelsRepository, NoteHistoryRepository } from '@/models/_.js'; | ||
import { bindThis } from '@/decorators.js'; | ||
import { DebounceLoader } from '@/misc/loader.js'; | ||
import { IdService } from '@/core/IdService.js'; | ||
import { NoteHistory } from '@/models/NoteHistory.js'; | ||
import type { OnModuleInit } from '@nestjs/common'; | ||
import type { CustomEmojiService } from '../CustomEmojiService.js'; | ||
import type { UserEntityService } from './UserEntityService.js'; | ||
import type { DriveFileEntityService } from './DriveFileEntityService.js'; | ||
|
||
@Injectable() | ||
export class NoteHistoryEntityService implements OnModuleInit { | ||
private userEntityService: UserEntityService; | ||
private driveFileEntityService: DriveFileEntityService; | ||
private customEmojiService: CustomEmojiService; | ||
private idService: IdService; | ||
private noteLoader = new DebounceLoader(this.findNoteOrFail); | ||
|
||
constructor( | ||
private moduleRef: ModuleRef, | ||
|
||
@Inject(DI.usersRepository) | ||
private usersRepository: UsersRepository, | ||
|
||
@Inject(DI.notesRepository) | ||
private notesRepository: NotesRepository, | ||
|
||
@Inject(DI.noteHistoryRepository) | ||
private noteHistoryRepository: NoteHistoryRepository, | ||
|
||
@Inject(DI.followingsRepository) | ||
private followingsRepository: FollowingsRepository, | ||
|
||
//private userEntityService: UserEntityService, | ||
//private driveFileEntityService: DriveFileEntityService, | ||
//private customEmojiService: CustomEmojiService, | ||
//private reactionService: ReactionService, | ||
) { | ||
} | ||
|
||
onModuleInit() { | ||
this.userEntityService = this.moduleRef.get('UserEntityService'); | ||
this.driveFileEntityService = this.moduleRef.get('DriveFileEntityService'); | ||
this.customEmojiService = this.moduleRef.get('CustomEmojiService'); | ||
this.idService = this.moduleRef.get('IdService'); | ||
} | ||
|
||
@bindThis | ||
public async packAttachedFiles(fileIds: NoteHistory['fileIds'], packedFiles: Map<NoteHistory['fileIds'][number], Packed<'DriveFile'> | null>): Promise<Packed<'DriveFile'>[]> { | ||
const missingIds = []; | ||
for (const id of fileIds) { | ||
if (!packedFiles.has(id)) missingIds.push(id); | ||
} | ||
if (missingIds.length) { | ||
const additionalMap = await this.driveFileEntityService.packManyByIdsMap(missingIds); | ||
for (const [k, v] of additionalMap) { | ||
packedFiles.set(k, v); | ||
} | ||
} | ||
return fileIds.map(id => packedFiles.get(id)).filter(x => x != null); | ||
} | ||
|
||
@bindThis | ||
public async pack( | ||
src: NoteHistory['id'], | ||
host: MiNote['userHost'], | ||
options?: { | ||
_hint_?: { | ||
packedFiles: Map<NoteHistory['fileIds'][number], Packed<'DriveFile'> | null>; | ||
}; | ||
}, | ||
): Promise<Packed<'NoteHistory'>> { | ||
const note = await this.noteLoader.load(src); | ||
|
||
const text = note.text; | ||
const packedFiles = options?._hint_?.packedFiles; | ||
|
||
const packed: Packed<'NoteHistory'> = await awaitAll({ | ||
id: note.id, | ||
noteId: note.noteId, | ||
updatedAt: note.updatedAt.toISOString(), | ||
userId: note.userId, | ||
text: text, | ||
visibility: note.visibility, | ||
visibleUserIds: note.visibility === 'specified' ? note.visibleUserIds : undefined, | ||
emojis: host != null ? this.customEmojiService.populateEmojis(note.emojis, host) : undefined, | ||
fileIds: note.fileIds, | ||
files: packedFiles != null ? this.packAttachedFiles(note.fileIds, packedFiles) : this.driveFileEntityService.packManyByIds(note.fileIds), | ||
}); | ||
|
||
return packed; | ||
} | ||
|
||
@bindThis | ||
private findNoteOrFail(id: string): Promise<NoteHistory> { | ||
return this.noteHistoryRepository.findOneOrFail({ | ||
where: { id }, | ||
relations: ['user'], | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
/* | ||
* SPDX-FileCopyrightText: syuilo and misskey-project | ||
* SPDX-License-Identifier: AGPL-3.0-only | ||
*/ | ||
|
||
export const packedNoteHistorySchema = { | ||
type: 'object', | ||
properties: { | ||
id: { | ||
type: 'string', | ||
optional: false, nullable: false, | ||
format: 'id', | ||
example: 'xxxxxxxxxx', | ||
}, | ||
noteId: { | ||
type: 'string', | ||
optional: false, nullable: false, | ||
}, | ||
updatedAt: { | ||
type: 'string', | ||
optional: false, nullable: false, | ||
format: 'date-time', | ||
}, | ||
userId: { | ||
type: 'string', | ||
optional: false, nullable: false, | ||
format: 'id', | ||
}, | ||
text: { | ||
type: 'string', | ||
optional: false, nullable: true, | ||
}, | ||
fileIds: { | ||
type: 'array', | ||
optional: true, nullable: false, | ||
items: { | ||
type: 'string', | ||
optional: false, nullable: false, | ||
format: 'id', | ||
}, | ||
}, | ||
files: { | ||
type: 'array', | ||
optional: true, nullable: false, | ||
items: { | ||
type: 'object', | ||
optional: false, nullable: false, | ||
ref: 'DriveFile', | ||
}, | ||
}, | ||
visibility: { | ||
type: 'string', | ||
optional: false, nullable: false, | ||
enum: ['public', 'home', 'followers', 'specified'], | ||
}, | ||
visibleUserIds: { | ||
type: 'array', | ||
optional: true, nullable: false, | ||
items: { | ||
type: 'string', | ||
optional: false, nullable: false, | ||
format: 'id', | ||
}, | ||
}, | ||
emojis: { | ||
type: 'object', | ||
optional: true, nullable: false, | ||
additionalProperties: { | ||
anyOf: [{ | ||
type: 'string', | ||
}], | ||
}, | ||
}, | ||
}, | ||
} as const; |