2023-07-27 07:31:52 +02:00
|
|
|
|
/*
|
|
|
|
|
* SPDX-FileCopyrightText: syuilo and other misskey contributors
|
|
|
|
|
* SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
*/
|
|
|
|
|
|
2023-02-16 15:09:41 +01:00
|
|
|
|
import { Inject, Injectable } from '@nestjs/common';
|
2023-08-05 03:33:00 +02:00
|
|
|
|
import { In } from 'typeorm';
|
2022-09-17 20:27:08 +02:00
|
|
|
|
import { ModuleRef } from '@nestjs/core';
|
|
|
|
|
import { DI } from '@/di-symbols.js';
|
2023-03-10 06:22:37 +01:00
|
|
|
|
import type { Packed } from '@/misc/json-schema.js';
|
2022-09-17 20:27:08 +02:00
|
|
|
|
import { awaitAll } from '@/misc/prelude/await-all.js';
|
2023-09-20 04:33:36 +02:00
|
|
|
|
import type { MiUser } from '@/models/User.js';
|
|
|
|
|
import type { MiNote } from '@/models/Note.js';
|
|
|
|
|
import type { MiNoteReaction } from '@/models/NoteReaction.js';
|
2023-09-15 07:28:29 +02:00
|
|
|
|
import type { UsersRepository, NotesRepository, FollowingsRepository, PollsRepository, PollVotesRepository, NoteReactionsRepository, ChannelsRepository } from '@/models/_.js';
|
2022-12-31 00:43:13 +01:00
|
|
|
|
import { bindThis } from '@/decorators.js';
|
2023-03-04 08:48:50 +01:00
|
|
|
|
import { isNotNull } from '@/misc/is-not-null.js';
|
2023-10-08 01:10:28 +02:00
|
|
|
|
import { DebounceLoader } from '@/misc/loader.js';
|
2023-10-15 03:36:22 +02:00
|
|
|
|
import { IdService } from '@/core/IdService.js';
|
2022-09-17 20:27:08 +02:00
|
|
|
|
import type { OnModuleInit } from '@nestjs/common';
|
|
|
|
|
import type { CustomEmojiService } from '../CustomEmojiService.js';
|
|
|
|
|
import type { ReactionService } from '../ReactionService.js';
|
|
|
|
|
import type { UserEntityService } from './UserEntityService.js';
|
|
|
|
|
import type { DriveFileEntityService } from './DriveFileEntityService.js';
|
|
|
|
|
|
|
|
|
|
@Injectable()
|
|
|
|
|
export class NoteEntityService implements OnModuleInit {
|
|
|
|
|
private userEntityService: UserEntityService;
|
|
|
|
|
private driveFileEntityService: DriveFileEntityService;
|
|
|
|
|
private customEmojiService: CustomEmojiService;
|
|
|
|
|
private reactionService: ReactionService;
|
2023-10-15 03:36:22 +02:00
|
|
|
|
private idService: IdService;
|
2023-10-08 01:10:28 +02:00
|
|
|
|
private noteLoader = new DebounceLoader(this.findNoteOrFail);
|
2023-07-08 00:08:16 +02:00
|
|
|
|
|
2022-09-17 20:27:08 +02:00
|
|
|
|
constructor(
|
|
|
|
|
private moduleRef: ModuleRef,
|
|
|
|
|
|
|
|
|
|
@Inject(DI.usersRepository)
|
|
|
|
|
private usersRepository: UsersRepository,
|
|
|
|
|
|
|
|
|
|
@Inject(DI.notesRepository)
|
|
|
|
|
private notesRepository: NotesRepository,
|
|
|
|
|
|
|
|
|
|
@Inject(DI.followingsRepository)
|
|
|
|
|
private followingsRepository: FollowingsRepository,
|
|
|
|
|
|
|
|
|
|
@Inject(DI.pollsRepository)
|
|
|
|
|
private pollsRepository: PollsRepository,
|
|
|
|
|
|
|
|
|
|
@Inject(DI.pollVotesRepository)
|
|
|
|
|
private pollVotesRepository: PollVotesRepository,
|
|
|
|
|
|
|
|
|
|
@Inject(DI.noteReactionsRepository)
|
|
|
|
|
private noteReactionsRepository: NoteReactionsRepository,
|
|
|
|
|
|
|
|
|
|
@Inject(DI.channelsRepository)
|
|
|
|
|
private channelsRepository: ChannelsRepository,
|
|
|
|
|
|
|
|
|
|
//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.reactionService = this.moduleRef.get('ReactionService');
|
2023-10-15 03:36:22 +02:00
|
|
|
|
this.idService = this.moduleRef.get('IdService');
|
2022-09-17 20:27:08 +02:00
|
|
|
|
}
|
2023-07-08 00:08:16 +02:00
|
|
|
|
|
2022-12-04 07:03:09 +01:00
|
|
|
|
@bindThis
|
2023-08-16 10:51:28 +02:00
|
|
|
|
private async hideNote(packedNote: Packed<'Note'>, meId: MiUser['id'] | null) {
|
2022-09-17 20:27:08 +02:00
|
|
|
|
// TODO: isVisibleForMe を使うようにしても良さそう(型違うけど)
|
|
|
|
|
let hide = false;
|
|
|
|
|
|
|
|
|
|
// visibility が specified かつ自分が指定されていなかったら非表示
|
|
|
|
|
if (packedNote.visibility === 'specified') {
|
|
|
|
|
if (meId == null) {
|
|
|
|
|
hide = true;
|
|
|
|
|
} else if (meId === packedNote.userId) {
|
|
|
|
|
hide = false;
|
|
|
|
|
} else {
|
|
|
|
|
// 指定されているかどうか
|
|
|
|
|
const specified = packedNote.visibleUserIds!.some((id: any) => meId === id);
|
|
|
|
|
|
|
|
|
|
if (specified) {
|
|
|
|
|
hide = false;
|
|
|
|
|
} else {
|
|
|
|
|
hide = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// visibility が followers かつ自分が投稿者のフォロワーでなかったら非表示
|
|
|
|
|
if (packedNote.visibility === 'followers') {
|
|
|
|
|
if (meId == null) {
|
|
|
|
|
hide = true;
|
|
|
|
|
} else if (meId === packedNote.userId) {
|
|
|
|
|
hide = false;
|
|
|
|
|
} else if (packedNote.reply && (meId === packedNote.reply.userId)) {
|
2023-10-03 13:26:11 +02:00
|
|
|
|
// 自分の投稿に対するリプライ
|
2022-09-17 20:27:08 +02:00
|
|
|
|
hide = false;
|
|
|
|
|
} else if (packedNote.mentions && packedNote.mentions.some(id => meId === id)) {
|
2023-10-03 13:26:11 +02:00
|
|
|
|
// 自分へのメンション
|
2022-09-17 20:27:08 +02:00
|
|
|
|
hide = false;
|
|
|
|
|
} else {
|
2023-10-03 13:26:11 +02:00
|
|
|
|
// フォロワーかどうか
|
2023-07-11 07:58:58 +02:00
|
|
|
|
const isFollowing = await this.followingsRepository.exist({
|
|
|
|
|
where: {
|
|
|
|
|
followeeId: packedNote.userId,
|
|
|
|
|
followerId: meId,
|
|
|
|
|
},
|
2022-09-17 20:27:08 +02:00
|
|
|
|
});
|
|
|
|
|
|
2023-07-11 07:58:58 +02:00
|
|
|
|
hide = !isFollowing;
|
2022-09-17 20:27:08 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (hide) {
|
|
|
|
|
packedNote.visibleUserIds = undefined;
|
|
|
|
|
packedNote.fileIds = [];
|
|
|
|
|
packedNote.files = [];
|
|
|
|
|
packedNote.text = null;
|
|
|
|
|
packedNote.poll = undefined;
|
|
|
|
|
packedNote.cw = null;
|
|
|
|
|
packedNote.isHidden = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-04 07:03:09 +01:00
|
|
|
|
@bindThis
|
2023-08-16 10:51:28 +02:00
|
|
|
|
private async populatePoll(note: MiNote, meId: MiUser['id'] | null) {
|
2022-09-17 20:27:08 +02:00
|
|
|
|
const poll = await this.pollsRepository.findOneByOrFail({ noteId: note.id });
|
|
|
|
|
const choices = poll.choices.map(c => ({
|
|
|
|
|
text: c,
|
|
|
|
|
votes: poll.votes[poll.choices.indexOf(c)],
|
|
|
|
|
isVoted: false,
|
|
|
|
|
}));
|
|
|
|
|
|
|
|
|
|
if (meId) {
|
|
|
|
|
if (poll.multiple) {
|
|
|
|
|
const votes = await this.pollVotesRepository.findBy({
|
|
|
|
|
userId: meId,
|
|
|
|
|
noteId: note.id,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const myChoices = votes.map(v => v.choice);
|
|
|
|
|
for (const myChoice of myChoices) {
|
|
|
|
|
choices[myChoice].isVoted = true;
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
const vote = await this.pollVotesRepository.findOneBy({
|
|
|
|
|
userId: meId,
|
|
|
|
|
noteId: note.id,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (vote) {
|
|
|
|
|
choices[vote.choice].isVoted = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
multiple: poll.multiple,
|
|
|
|
|
expiresAt: poll.expiresAt,
|
|
|
|
|
choices,
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-04 07:03:09 +01:00
|
|
|
|
@bindThis
|
2023-10-19 02:20:19 +02:00
|
|
|
|
public async populateMyReaction(note: { id: MiNote['id']; reactions: MiNote['reactions']; reactionAndUserPairCache?: MiNote['reactionAndUserPairCache']; }, meId: MiUser['id'], _hint_?: {
|
|
|
|
|
myReactions: Map<MiNote['id'], string | null>;
|
2022-09-17 20:27:08 +02:00
|
|
|
|
}) {
|
|
|
|
|
if (_hint_?.myReactions) {
|
2023-10-19 02:20:19 +02:00
|
|
|
|
const reaction = _hint_.myReactions.get(note.id);
|
2022-09-17 20:27:08 +02:00
|
|
|
|
if (reaction) {
|
2023-10-19 02:20:19 +02:00
|
|
|
|
return this.reactionService.convertLegacyReaction(reaction);
|
|
|
|
|
} else {
|
|
|
|
|
return undefined;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const reactionsCount = Object.values(note.reactions).reduce((a, b) => a + b, 0);
|
|
|
|
|
if (reactionsCount === 0) return undefined;
|
|
|
|
|
if (note.reactionAndUserPairCache && reactionsCount <= note.reactionAndUserPairCache.length) {
|
|
|
|
|
const pair = note.reactionAndUserPairCache.find(p => p.startsWith(meId));
|
|
|
|
|
if (pair) {
|
2023-10-19 04:17:59 +02:00
|
|
|
|
return this.reactionService.convertLegacyReaction(pair.split('/')[1]);
|
2023-10-19 01:07:22 +02:00
|
|
|
|
} else {
|
2022-09-17 20:27:08 +02:00
|
|
|
|
return undefined;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-14 03:05:44 +02:00
|
|
|
|
// パフォーマンスのためノートが作成されてから2秒以上経っていない場合はリアクションを取得しない
|
2023-10-19 02:20:19 +02:00
|
|
|
|
if (this.idService.parse(note.id).date.getTime() + 2000 > Date.now()) {
|
2023-04-06 11:27:42 +02:00
|
|
|
|
return undefined;
|
|
|
|
|
}
|
|
|
|
|
|
2022-09-17 20:27:08 +02:00
|
|
|
|
const reaction = await this.noteReactionsRepository.findOneBy({
|
|
|
|
|
userId: meId,
|
2023-10-19 02:20:19 +02:00
|
|
|
|
noteId: note.id,
|
2022-09-17 20:27:08 +02:00
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (reaction) {
|
|
|
|
|
return this.reactionService.convertLegacyReaction(reaction.reaction);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return undefined;
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-04 07:03:09 +01:00
|
|
|
|
@bindThis
|
2023-08-16 10:51:28 +02:00
|
|
|
|
public async isVisibleForMe(note: MiNote, meId: MiUser['id'] | null): Promise<boolean> {
|
2022-09-17 20:27:08 +02:00
|
|
|
|
// This code must always be synchronized with the checks in generateVisibilityQuery.
|
|
|
|
|
// visibility が specified かつ自分が指定されていなかったら非表示
|
|
|
|
|
if (note.visibility === 'specified') {
|
|
|
|
|
if (meId == null) {
|
|
|
|
|
return false;
|
|
|
|
|
} else if (meId === note.userId) {
|
|
|
|
|
return true;
|
|
|
|
|
} else {
|
|
|
|
|
// 指定されているかどうか
|
|
|
|
|
return note.visibleUserIds.some((id: any) => meId === id);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// visibility が followers かつ自分が投稿者のフォロワーでなかったら非表示
|
|
|
|
|
if (note.visibility === 'followers') {
|
|
|
|
|
if (meId == null) {
|
|
|
|
|
return false;
|
|
|
|
|
} else if (meId === note.userId) {
|
|
|
|
|
return true;
|
|
|
|
|
} else if (note.reply && (meId === note.reply.userId)) {
|
|
|
|
|
// 自分の投稿に対するリプライ
|
|
|
|
|
return true;
|
|
|
|
|
} else if (note.mentions && note.mentions.some(id => meId === id)) {
|
|
|
|
|
// 自分へのメンション
|
|
|
|
|
return true;
|
|
|
|
|
} else {
|
|
|
|
|
// フォロワーかどうか
|
|
|
|
|
const [following, user] = await Promise.all([
|
|
|
|
|
this.followingsRepository.count({
|
|
|
|
|
where: {
|
|
|
|
|
followeeId: note.userId,
|
|
|
|
|
followerId: meId,
|
|
|
|
|
},
|
|
|
|
|
take: 1,
|
|
|
|
|
}),
|
|
|
|
|
this.usersRepository.findOneByOrFail({ id: meId }),
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
/* If we know the following, everyhting is fine.
|
|
|
|
|
|
|
|
|
|
But if we do not know the following, it might be that both the
|
|
|
|
|
author of the note and the author of the like are remote users,
|
|
|
|
|
in which case we can never know the following. Instead we have
|
|
|
|
|
to assume that the users are following each other.
|
|
|
|
|
*/
|
|
|
|
|
return following > 0 || (note.userHost != null && user.host != null);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-04 08:48:50 +01:00
|
|
|
|
@bindThis
|
2023-08-16 10:51:28 +02:00
|
|
|
|
public async packAttachedFiles(fileIds: MiNote['fileIds'], packedFiles: Map<MiNote['fileIds'][number], Packed<'DriveFile'> | null>): Promise<Packed<'DriveFile'>[]> {
|
2023-03-04 08:48:50 +01:00
|
|
|
|
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(isNotNull);
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-04 07:03:09 +01:00
|
|
|
|
@bindThis
|
2022-09-17 20:27:08 +02:00
|
|
|
|
public async pack(
|
2023-08-16 10:51:28 +02:00
|
|
|
|
src: MiNote['id'] | MiNote,
|
|
|
|
|
me?: { id: MiUser['id'] } | null | undefined,
|
2022-09-17 20:27:08 +02:00
|
|
|
|
options?: {
|
|
|
|
|
detail?: boolean;
|
|
|
|
|
skipHide?: boolean;
|
2023-10-19 02:20:19 +02:00
|
|
|
|
withReactionAndUserPairCache?: boolean;
|
2022-09-17 20:27:08 +02:00
|
|
|
|
_hint_?: {
|
2023-10-19 02:20:19 +02:00
|
|
|
|
myReactions: Map<MiNote['id'], string | null>;
|
2023-08-16 10:51:28 +02:00
|
|
|
|
packedFiles: Map<MiNote['fileIds'][number], Packed<'DriveFile'> | null>;
|
2022-09-17 20:27:08 +02:00
|
|
|
|
};
|
|
|
|
|
},
|
|
|
|
|
): Promise<Packed<'Note'>> {
|
|
|
|
|
const opts = Object.assign({
|
|
|
|
|
detail: true,
|
|
|
|
|
skipHide: false,
|
2023-10-19 02:20:19 +02:00
|
|
|
|
withReactionAndUserPairCache: false,
|
2022-09-17 20:27:08 +02:00
|
|
|
|
}, options);
|
|
|
|
|
|
|
|
|
|
const meId = me ? me.id : null;
|
2023-10-08 01:10:28 +02:00
|
|
|
|
const note = typeof src === 'object' ? src : await this.noteLoader.load(src);
|
2022-09-17 20:27:08 +02:00
|
|
|
|
const host = note.userHost;
|
|
|
|
|
|
|
|
|
|
let text = note.text;
|
|
|
|
|
|
|
|
|
|
if (note.name && (note.url ?? note.uri)) {
|
2022-09-22 23:21:31 +02:00
|
|
|
|
text = `【${note.name}】\n${(note.text ?? '').trim()}\n\n${note.url ?? note.uri}`;
|
2022-09-17 20:27:08 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const channel = note.channelId
|
|
|
|
|
? note.channel
|
|
|
|
|
? note.channel
|
|
|
|
|
: await this.channelsRepository.findOneBy({ id: note.channelId })
|
|
|
|
|
: null;
|
|
|
|
|
|
2023-01-26 07:48:12 +01:00
|
|
|
|
const reactionEmojiNames = Object.keys(note.reactions)
|
|
|
|
|
.filter(x => x.startsWith(':') && x.includes('@') && !x.includes('@.')) // リモートカスタム絵文字のみ
|
|
|
|
|
.map(x => this.reactionService.decodeReaction(x).reaction.replaceAll(':', ''));
|
2023-03-04 08:48:50 +01:00
|
|
|
|
const packedFiles = options?._hint_?.packedFiles;
|
2022-09-17 20:27:08 +02:00
|
|
|
|
|
|
|
|
|
const packed: Packed<'Note'> = await awaitAll({
|
|
|
|
|
id: note.id,
|
2023-10-16 03:45:22 +02:00
|
|
|
|
createdAt: this.idService.parse(note.id).date.toISOString(),
|
2022-09-17 20:27:08 +02:00
|
|
|
|
userId: note.userId,
|
|
|
|
|
user: this.userEntityService.pack(note.user ?? note.userId, me, {
|
|
|
|
|
detail: false,
|
|
|
|
|
}),
|
|
|
|
|
text: text,
|
|
|
|
|
cw: note.cw,
|
|
|
|
|
visibility: note.visibility,
|
2023-10-18 02:54:18 +02:00
|
|
|
|
localOnly: note.localOnly,
|
2023-03-08 00:56:47 +01:00
|
|
|
|
reactionAcceptance: note.reactionAcceptance,
|
2022-09-17 20:27:08 +02:00
|
|
|
|
visibleUserIds: note.visibility === 'specified' ? note.visibleUserIds : undefined,
|
|
|
|
|
renoteCount: note.renoteCount,
|
|
|
|
|
repliesCount: note.repliesCount,
|
|
|
|
|
reactions: this.reactionService.convertLegacyReactions(note.reactions),
|
2023-01-26 07:48:12 +01:00
|
|
|
|
reactionEmojis: this.customEmojiService.populateEmojis(reactionEmojiNames, host),
|
2023-10-19 02:20:19 +02:00
|
|
|
|
reactionAndUserPairCache: opts.withReactionAndUserPairCache ? note.reactionAndUserPairCache : undefined,
|
2023-01-26 07:48:12 +01:00
|
|
|
|
emojis: host != null ? this.customEmojiService.populateEmojis(note.emojis, host) : undefined,
|
2022-09-17 20:27:08 +02:00
|
|
|
|
tags: note.tags.length > 0 ? note.tags : undefined,
|
|
|
|
|
fileIds: note.fileIds,
|
2023-03-04 08:48:50 +01:00
|
|
|
|
files: packedFiles != null ? this.packAttachedFiles(note.fileIds, packedFiles) : this.driveFileEntityService.packManyByIds(note.fileIds),
|
2022-09-17 20:27:08 +02:00
|
|
|
|
replyId: note.replyId,
|
|
|
|
|
renoteId: note.renoteId,
|
|
|
|
|
channelId: note.channelId ?? undefined,
|
|
|
|
|
channel: channel ? {
|
|
|
|
|
id: channel.id,
|
|
|
|
|
name: channel.name,
|
2023-05-02 02:36:40 +02:00
|
|
|
|
color: channel.color,
|
2023-08-05 06:58:31 +02:00
|
|
|
|
isSensitive: channel.isSensitive,
|
2022-09-17 20:27:08 +02:00
|
|
|
|
} : undefined,
|
|
|
|
|
mentions: note.mentions.length > 0 ? note.mentions : undefined,
|
|
|
|
|
uri: note.uri ?? undefined,
|
|
|
|
|
url: note.url ?? undefined,
|
|
|
|
|
|
|
|
|
|
...(opts.detail ? {
|
2023-09-17 03:55:26 +02:00
|
|
|
|
clippedCount: note.clippedCount,
|
|
|
|
|
|
2022-09-17 20:27:08 +02:00
|
|
|
|
reply: note.replyId ? this.pack(note.reply ?? note.replyId, me, {
|
|
|
|
|
detail: false,
|
2023-10-19 02:20:19 +02:00
|
|
|
|
withReactionAndUserPairCache: opts.withReactionAndUserPairCache,
|
2022-09-17 20:27:08 +02:00
|
|
|
|
_hint_: options?._hint_,
|
|
|
|
|
}) : undefined,
|
|
|
|
|
|
|
|
|
|
renote: note.renoteId ? this.pack(note.renote ?? note.renoteId, me, {
|
|
|
|
|
detail: true,
|
2023-10-19 02:20:19 +02:00
|
|
|
|
withReactionAndUserPairCache: opts.withReactionAndUserPairCache,
|
2022-09-17 20:27:08 +02:00
|
|
|
|
_hint_: options?._hint_,
|
|
|
|
|
}) : undefined,
|
|
|
|
|
|
2022-09-18 20:11:50 +02:00
|
|
|
|
poll: note.hasPoll ? this.populatePoll(note, meId) : undefined,
|
2022-09-17 20:27:08 +02:00
|
|
|
|
|
2023-10-19 00:56:25 +02:00
|
|
|
|
...(meId && Object.keys(note.reactions).length > 0 ? {
|
2023-10-19 02:20:19 +02:00
|
|
|
|
myReaction: this.populateMyReaction(note, meId, options?._hint_),
|
2022-09-17 20:27:08 +02:00
|
|
|
|
} : {}),
|
|
|
|
|
} : {}),
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (!opts.skipHide) {
|
2022-09-18 20:11:50 +02:00
|
|
|
|
await this.hideNote(packed, meId);
|
2022-09-17 20:27:08 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return packed;
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-04 07:03:09 +01:00
|
|
|
|
@bindThis
|
2022-09-17 20:27:08 +02:00
|
|
|
|
public async packMany(
|
2023-08-16 10:51:28 +02:00
|
|
|
|
notes: MiNote[],
|
|
|
|
|
me?: { id: MiUser['id'] } | null | undefined,
|
2022-09-17 20:27:08 +02:00
|
|
|
|
options?: {
|
|
|
|
|
detail?: boolean;
|
|
|
|
|
skipHide?: boolean;
|
|
|
|
|
},
|
|
|
|
|
) {
|
|
|
|
|
if (notes.length === 0) return [];
|
|
|
|
|
|
|
|
|
|
const meId = me ? me.id : null;
|
2023-10-19 02:20:19 +02:00
|
|
|
|
const myReactionsMap = new Map<MiNote['id'], string | null>();
|
2022-09-17 20:27:08 +02:00
|
|
|
|
if (meId) {
|
2023-10-19 02:20:19 +02:00
|
|
|
|
const idsNeedFetchMyReaction = new Set<MiNote['id']>();
|
|
|
|
|
|
2023-10-14 03:05:44 +02:00
|
|
|
|
// パフォーマンスのためノートが作成されてから2秒以上経っていない場合はリアクションを取得しない
|
2023-10-16 03:45:22 +02:00
|
|
|
|
const oldId = this.idService.gen(Date.now() - 2000);
|
2023-10-19 02:20:19 +02:00
|
|
|
|
|
|
|
|
|
for (const note of notes) {
|
|
|
|
|
if (note.renote && (note.text == null && note.fileIds.length === 0)) { // pure renote
|
|
|
|
|
const reactionsCount = Object.values(note.renote.reactions).reduce((a, b) => a + b, 0);
|
|
|
|
|
if (reactionsCount === 0) {
|
|
|
|
|
myReactionsMap.set(note.renote.id, null);
|
|
|
|
|
} else if (reactionsCount <= note.renote.reactionAndUserPairCache.length) {
|
|
|
|
|
const pair = note.renote.reactionAndUserPairCache.find(p => p.startsWith(meId));
|
2023-10-19 04:17:59 +02:00
|
|
|
|
myReactionsMap.set(note.renote.id, pair ? pair.split('/')[1] : null);
|
2023-10-19 02:20:19 +02:00
|
|
|
|
} else {
|
|
|
|
|
idsNeedFetchMyReaction.add(note.renote.id);
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
if (note.id < oldId) {
|
|
|
|
|
const reactionsCount = Object.values(note.reactions).reduce((a, b) => a + b, 0);
|
|
|
|
|
if (reactionsCount === 0) {
|
|
|
|
|
myReactionsMap.set(note.id, null);
|
|
|
|
|
} else if (reactionsCount <= note.reactionAndUserPairCache.length) {
|
|
|
|
|
const pair = note.reactionAndUserPairCache.find(p => p.startsWith(meId));
|
2023-10-19 04:17:59 +02:00
|
|
|
|
myReactionsMap.set(note.id, pair ? pair.split('/')[1] : null);
|
2023-10-19 02:20:19 +02:00
|
|
|
|
} else {
|
|
|
|
|
idsNeedFetchMyReaction.add(note.id);
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
myReactionsMap.set(note.id, null);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const myReactions = idsNeedFetchMyReaction.size > 0 ? await this.noteReactionsRepository.findBy({
|
2022-09-17 20:27:08 +02:00
|
|
|
|
userId: meId,
|
2023-10-19 02:20:19 +02:00
|
|
|
|
noteId: In(Array.from(idsNeedFetchMyReaction)),
|
2023-10-19 01:07:22 +02:00
|
|
|
|
}) : [];
|
2022-09-17 20:27:08 +02:00
|
|
|
|
|
2023-10-19 02:20:19 +02:00
|
|
|
|
for (const id of idsNeedFetchMyReaction) {
|
|
|
|
|
myReactionsMap.set(id, myReactions.find(reaction => reaction.noteId === id)?.reaction ?? null);
|
2022-09-17 20:27:08 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-06 04:14:43 +02:00
|
|
|
|
await this.customEmojiService.prefetchEmojis(this.aggregateNoteEmojis(notes));
|
2023-03-04 08:48:50 +01:00
|
|
|
|
// TODO: 本当は renote とか reply がないのに renoteId とか replyId があったらここで解決しておく
|
|
|
|
|
const fileIds = notes.map(n => [n.fileIds, n.renote?.fileIds, n.reply?.fileIds]).flat(2).filter(isNotNull);
|
2023-04-06 12:48:24 +02:00
|
|
|
|
const packedFiles = fileIds.length > 0 ? await this.driveFileEntityService.packManyByIdsMap(fileIds) : new Map();
|
2023-01-26 07:48:12 +01:00
|
|
|
|
|
2022-09-17 20:27:08 +02:00
|
|
|
|
return await Promise.all(notes.map(n => this.pack(n, me, {
|
|
|
|
|
...options,
|
|
|
|
|
_hint_: {
|
|
|
|
|
myReactions: myReactionsMap,
|
2023-03-04 08:48:50 +01:00
|
|
|
|
packedFiles,
|
2022-09-17 20:27:08 +02:00
|
|
|
|
},
|
|
|
|
|
})));
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-06 04:14:43 +02:00
|
|
|
|
@bindThis
|
2023-08-16 10:51:28 +02:00
|
|
|
|
public aggregateNoteEmojis(notes: MiNote[]) {
|
2023-04-06 04:14:43 +02:00
|
|
|
|
let emojis: { name: string | null; host: string | null; }[] = [];
|
|
|
|
|
for (const note of notes) {
|
|
|
|
|
emojis = emojis.concat(note.emojis
|
|
|
|
|
.map(e => this.customEmojiService.parseEmojiStr(e, note.userHost)));
|
|
|
|
|
if (note.renote) {
|
|
|
|
|
emojis = emojis.concat(note.renote.emojis
|
|
|
|
|
.map(e => this.customEmojiService.parseEmojiStr(e, note.renote!.userHost)));
|
|
|
|
|
if (note.renote.user) {
|
|
|
|
|
emojis = emojis.concat(note.renote.user.emojis
|
|
|
|
|
.map(e => this.customEmojiService.parseEmojiStr(e, note.renote!.userHost)));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
const customReactions = Object.keys(note.reactions).map(x => this.reactionService.decodeReaction(x)).filter(x => x.name != null) as typeof emojis;
|
|
|
|
|
emojis = emojis.concat(customReactions);
|
|
|
|
|
if (note.user) {
|
|
|
|
|
emojis = emojis.concat(note.user.emojis
|
|
|
|
|
.map(e => this.customEmojiService.parseEmojiStr(e, note.userHost)));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return emojis.filter(x => x.name != null && x.host != null) as { name: string; host: string; }[];
|
|
|
|
|
}
|
2023-10-08 01:10:28 +02:00
|
|
|
|
|
|
|
|
|
@bindThis
|
|
|
|
|
private findNoteOrFail(id: string): Promise<MiNote> {
|
|
|
|
|
return this.notesRepository.findOneOrFail({
|
|
|
|
|
where: { id },
|
|
|
|
|
relations: ['user'],
|
|
|
|
|
});
|
|
|
|
|
}
|
2022-09-17 20:27:08 +02:00
|
|
|
|
}
|